blob: 1df5af0329f6367b6df7d305a7b3e487563942c8 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000016#include "Python.h"
17#include "structseq.h"
18
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000019#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000020# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000021#endif /* defined(__VMS) */
22
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000023PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000024"This module provides access to operating system functionality that is\n\
25standardized by the C Standard and the POSIX standard (a thinly\n\
26disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000029#ifndef Py_USING_UNICODE
30/* This is used in signatures of functions. */
31#define Py_UNICODE void
32#endif
33
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000034#if defined(PYOS_OS2)
35#define INCL_DOS
36#define INCL_DOSERRORS
37#define INCL_DOSPROCESS
38#define INCL_NOPMAPI
39#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000040#if defined(PYCC_GCC)
41#include <ctype.h>
42#include <io.h>
43#include <stdio.h>
44#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000045#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000046#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#endif
48
Guido van Rossumb6775db1994-08-01 11:34:53 +000049#include <sys/types.h>
50#include <sys/stat.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000051
Guido van Rossum36bc6801995-06-14 22:54:23 +000052#ifdef HAVE_SYS_WAIT_H
53#include <sys/wait.h> /* For WNOHANG */
54#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000055
Guido van Rossuma376cc51996-12-05 23:43:35 +000056#include <signal.h>
Guido van Rossuma376cc51996-12-05 23:43:35 +000057
Guido van Rossumb6775db1994-08-01 11:34:53 +000058#ifdef HAVE_FCNTL_H
59#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000060#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000061
Guido van Rossuma6535fd2001-10-18 19:44:10 +000062#ifdef HAVE_GRP_H
63#include <grp.h>
64#endif
65
Barry Warsaw5676bd12003-01-07 20:57:09 +000066#ifdef HAVE_SYSEXITS_H
67#include <sysexits.h>
68#endif /* HAVE_SYSEXITS_H */
69
Guido van Rossuma4916fa1996-05-23 22:58:55 +000070/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000071/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000072#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000073#include <process.h>
74#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000075#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000076#define HAVE_GETCWD 1
77#define HAVE_OPENDIR 1
78#define HAVE_SYSTEM 1
79#if defined(__OS2__)
80#define HAVE_EXECV 1
81#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000082#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000083#include <process.h>
84#else
85#ifdef __BORLANDC__ /* Borland compiler */
86#define HAVE_EXECV 1
87#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000088#define HAVE_OPENDIR 1
89#define HAVE_PIPE 1
90#define HAVE_POPEN 1
91#define HAVE_SYSTEM 1
92#define HAVE_WAIT 1
93#else
94#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000095#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +000096#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000097#define HAVE_EXECV 1
98#define HAVE_PIPE 1
99#define HAVE_POPEN 1
100#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000101#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000102#define HAVE_FSYNC 1
103#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000104#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000105#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
106/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#else /* all other compilers */
108/* Unix functions that the configure script doesn't check for */
109#define HAVE_EXECV 1
110#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000111#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
112#define HAVE_FORK1 1
113#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_GETCWD 1
115#define HAVE_GETEGID 1
116#define HAVE_GETEUID 1
117#define HAVE_GETGID 1
118#define HAVE_GETPPID 1
119#define HAVE_GETUID 1
120#define HAVE_KILL 1
121#define HAVE_OPENDIR 1
122#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000123#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000125#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_SYSTEM 1
127#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000128#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000129#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#endif /* _MSC_VER */
131#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000132#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000133#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000134
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000136
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000137#if defined(__sgi)&&_COMPILER_VERSION>=700
138/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
139 (default) */
140extern char *ctermid_r(char *);
141#endif
142
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000143#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000144#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000145extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000146#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000147#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000148extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000150extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000152#endif
153#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000154extern int chdir(char *);
155extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000157extern int chdir(const char *);
158extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000159#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000160#ifdef __BORLANDC__
161extern int chmod(const char *, int);
162#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000163extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000164#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000165extern int chown(const char *, uid_t, gid_t);
166extern char *getcwd(char *, int);
167extern char *strerror(int);
168extern int link(const char *, const char *);
169extern int rename(const char *, const char *);
170extern int stat(const char *, struct stat *);
171extern int unlink(const char *);
172extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000175#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000178#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000180
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183#ifdef HAVE_UTIME_H
184#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000185#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000187#ifdef HAVE_SYS_UTIME_H
188#include <sys/utime.h>
189#define HAVE_UTIME_H /* pretend we do for the rest of this file */
190#endif /* HAVE_SYS_UTIME_H */
191
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#ifdef HAVE_SYS_TIMES_H
193#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000194#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195
196#ifdef HAVE_SYS_PARAM_H
197#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000198#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199
200#ifdef HAVE_SYS_UTSNAME_H
201#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000202#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000204#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000206#define NAMLEN(dirent) strlen((dirent)->d_name)
207#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000208#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#include <direct.h>
210#define NAMLEN(dirent) strlen((dirent)->d_name)
211#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000213#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000214#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000215#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000217#endif
218#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000220#endif
221#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000223#endif
224#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#include <direct.h>
228#include <io.h>
229#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000230#include "osdefs.h"
Tim Petersee66d0c2002-07-15 16:10:55 +0000231#define WIN32_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000233#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000234#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000235#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossumd48f2521997-12-05 22:19:34 +0000238#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000240#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241
Tim Petersbc2e10e2002-03-03 23:17:02 +0000242#ifndef MAXPATHLEN
243#define MAXPATHLEN 1024
244#endif /* MAXPATHLEN */
245
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000246#ifdef UNION_WAIT
247/* Emulate some macros on systems that have a union instead of macros */
248
249#ifndef WIFEXITED
250#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
251#endif
252
253#ifndef WEXITSTATUS
254#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
255#endif
256
257#ifndef WTERMSIG
258#define WTERMSIG(u_wait) ((u_wait).w_termsig)
259#endif
260
261#endif /* UNION_WAIT */
262
Greg Wardb48bc172000-03-01 21:51:56 +0000263/* Don't use the "_r" form if we don't need it (also, won't have a
264 prototype for it, at least on Solaris -- maybe others as well?). */
265#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
266#define USE_CTERMID_R
267#endif
268
269#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
270#define USE_TMPNAM_R
271#endif
272
Fred Drake699f3522000-06-29 21:12:41 +0000273/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000274#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000275#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +0000276# define STAT _stati64
277# define FSTAT _fstati64
278# define STRUCT_STAT struct _stati64
279#else
280# define STAT stat
281# define FSTAT fstat
282# define STRUCT_STAT struct stat
283#endif
284
Tim Peters11b23062003-04-23 02:39:17 +0000285#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000286#include <sys/mkdev.h>
287#else
288#if defined(MAJOR_IN_SYSMACROS)
289#include <sys/sysmacros.h>
290#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000291#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
292#include <sys/mkdev.h>
293#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000294#endif
Fred Drake699f3522000-06-29 21:12:41 +0000295
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000297#ifdef WITH_NEXT_FRAMEWORK
298/* On Darwin/MacOSX a shared library or framework has no access to
299** environ directly, we must obtain it with _NSGetEnviron().
300*/
301#include <crt_externs.h>
302static char **environ;
303#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000305#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306
Barry Warsaw53699e91996-12-10 23:23:01 +0000307static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000308convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309{
Barry Warsaw53699e91996-12-10 23:23:01 +0000310 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000312 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 if (d == NULL)
314 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000315#ifdef WITH_NEXT_FRAMEWORK
316 if (environ == NULL)
317 environ = *_NSGetEnviron();
318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319 if (environ == NULL)
320 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000321 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000323 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000324 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 char *p = strchr(*e, '=');
326 if (p == NULL)
327 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000328 k = PyString_FromStringAndSize(*e, (int)(p-*e));
329 if (k == NULL) {
330 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000332 }
333 v = PyString_FromString(p+1);
334 if (v == NULL) {
335 PyErr_Clear();
336 Py_DECREF(k);
337 continue;
338 }
339 if (PyDict_GetItem(d, k) == NULL) {
340 if (PyDict_SetItem(d, k, v) != 0)
341 PyErr_Clear();
342 }
343 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000344 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000346#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000347 {
348 APIRET rc;
349 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
350
351 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000352 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000353 PyObject *v = PyString_FromString(buffer);
354 PyDict_SetItemString(d, "BEGINLIBPATH", v);
355 Py_DECREF(v);
356 }
357 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
358 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
359 PyObject *v = PyString_FromString(buffer);
360 PyDict_SetItemString(d, "ENDLIBPATH", v);
361 Py_DECREF(v);
362 }
363 }
364#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 return d;
366}
367
368
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369/* Set a POSIX-specific error from errno, and return NULL */
370
Barry Warsawd58d7641998-07-23 16:14:40 +0000371static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000372posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000373{
Barry Warsawca74da41999-02-09 19:31:45 +0000374 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
Barry Warsawd58d7641998-07-23 16:14:40 +0000376static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000377posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000378{
Barry Warsawca74da41999-02-09 19:31:45 +0000379 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000380}
381
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000382#ifdef Py_WIN_WIDE_FILENAMES
383static PyObject *
384posix_error_with_unicode_filename(Py_UNICODE* name)
385{
386 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
387}
388#endif /* Py_WIN_WIDE_FILENAMES */
389
390
Mark Hammondef8b6542001-05-13 08:04:26 +0000391static PyObject *
392posix_error_with_allocated_filename(char* name)
393{
394 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
395 PyMem_Free(name);
396 return rc;
397}
398
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000399#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000400static PyObject *
401win32_error(char* function, char* filename)
402{
Mark Hammond33a6da92000-08-15 00:46:38 +0000403 /* XXX We should pass the function name along in the future.
404 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000405 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000406 Windows error object, which is non-trivial.
407 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000408 errno = GetLastError();
409 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000410 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000411 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000412 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000413}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000414
415#ifdef Py_WIN_WIDE_FILENAMES
416static PyObject *
417win32_error_unicode(char* function, Py_UNICODE* filename)
418{
419 /* XXX - see win32_error for comments on 'function' */
420 errno = GetLastError();
421 if (filename)
422 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
423 else
424 return PyErr_SetFromWindowsErr(errno);
425}
426
427static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
428{
429 /* XXX Perhaps we should make this API an alias of
430 PyObject_Unicode() instead ?! */
431 if (PyUnicode_CheckExact(obj)) {
432 Py_INCREF(obj);
433 return obj;
434 }
435 if (PyUnicode_Check(obj)) {
436 /* For a Unicode subtype that's not a Unicode object,
437 return a true Unicode object with the same data. */
438 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
439 PyUnicode_GET_SIZE(obj));
440 }
Tim Peters11b23062003-04-23 02:39:17 +0000441 return PyUnicode_FromEncodedObject(obj,
442 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000443 "strict");
444}
445
446#endif /* Py_WIN_WIDE_FILENAMES */
447
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000448#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449
Guido van Rossumd48f2521997-12-05 22:19:34 +0000450#if defined(PYOS_OS2)
451/**********************************************************************
452 * Helper Function to Trim and Format OS/2 Messages
453 **********************************************************************/
454 static void
455os2_formatmsg(char *msgbuf, int msglen, char *reason)
456{
457 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
458
459 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
460 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
461
462 while (lastc > msgbuf && isspace(*lastc))
463 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
464 }
465
466 /* Add Optional Reason Text */
467 if (reason) {
468 strcat(msgbuf, " : ");
469 strcat(msgbuf, reason);
470 }
471}
472
473/**********************************************************************
474 * Decode an OS/2 Operating System Error Code
475 *
476 * A convenience function to lookup an OS/2 error code and return a
477 * text message we can use to raise a Python exception.
478 *
479 * Notes:
480 * The messages for errors returned from the OS/2 kernel reside in
481 * the file OSO001.MSG in the \OS2 directory hierarchy.
482 *
483 **********************************************************************/
484 static char *
485os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
486{
487 APIRET rc;
488 ULONG msglen;
489
490 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
491 Py_BEGIN_ALLOW_THREADS
492 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
493 errorcode, "oso001.msg", &msglen);
494 Py_END_ALLOW_THREADS
495
496 if (rc == NO_ERROR)
497 os2_formatmsg(msgbuf, msglen, reason);
498 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000499 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000500 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000501
502 return msgbuf;
503}
504
505/* Set an OS/2-specific error and return NULL. OS/2 kernel
506 errors are not in a global variable e.g. 'errno' nor are
507 they congruent with posix error numbers. */
508
509static PyObject * os2_error(int code)
510{
511 char text[1024];
512 PyObject *v;
513
514 os2_strerror(text, sizeof(text), code, "");
515
516 v = Py_BuildValue("(is)", code, text);
517 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000518 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000519 Py_DECREF(v);
520 }
521 return NULL; /* Signal to Python that an Exception is Pending */
522}
523
524#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525
526/* POSIX generic methods */
527
Barry Warsaw53699e91996-12-10 23:23:01 +0000528static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000529posix_fildes(PyObject *fdobj, int (*func)(int))
530{
531 int fd;
532 int res;
533 fd = PyObject_AsFileDescriptor(fdobj);
534 if (fd < 0)
535 return NULL;
536 Py_BEGIN_ALLOW_THREADS
537 res = (*func)(fd);
538 Py_END_ALLOW_THREADS
539 if (res < 0)
540 return posix_error();
541 Py_INCREF(Py_None);
542 return Py_None;
543}
Guido van Rossum21142a01999-01-08 21:05:37 +0000544
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000545#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000546static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000547unicode_file_names(void)
548{
549 static int canusewide = -1;
550 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000551 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000552 the Windows NT family. */
553 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
554 }
555 return canusewide;
556}
557#endif
Tim Peters11b23062003-04-23 02:39:17 +0000558
Guido van Rossum21142a01999-01-08 21:05:37 +0000559static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000560posix_1str(PyObject *args, char *format, int (*func)(const char*),
561 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562{
Mark Hammondef8b6542001-05-13 08:04:26 +0000563 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000564 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000565#ifdef Py_WIN_WIDE_FILENAMES
566 if (unicode_file_names()) {
567 PyUnicodeObject *po;
568 if (PyArg_ParseTuple(args, wformat, &po)) {
569 Py_BEGIN_ALLOW_THREADS
570 /* PyUnicode_AS_UNICODE OK without thread
571 lock as it is a simple dereference. */
572 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
573 Py_END_ALLOW_THREADS
574 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000575 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576 Py_INCREF(Py_None);
577 return Py_None;
578 }
579 /* Drop the argument parsing error as narrow
580 strings are also valid. */
581 PyErr_Clear();
582 }
583#else
584 /* Platforms that don't support Unicode filenames
585 shouldn't be passing these extra params */
586 assert(wformat==NULL && wfunc == NULL);
587#endif
588
Tim Peters5aa91602002-01-30 05:46:57 +0000589 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000590 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000592 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000593 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000594 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000595 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000596 return posix_error_with_allocated_filename(path1);
597 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000598 Py_INCREF(Py_None);
599 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600}
601
Barry Warsaw53699e91996-12-10 23:23:01 +0000602static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000603posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000604 char *format,
605 int (*func)(const char *, const char *),
606 char *wformat,
607 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608{
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000610 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000611#ifdef Py_WIN_WIDE_FILENAMES
612 if (unicode_file_names()) {
613 PyObject *po1;
614 PyObject *po2;
615 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
616 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
617 PyObject *wpath1;
618 PyObject *wpath2;
619 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
620 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
621 if (!wpath1 || !wpath2) {
622 Py_XDECREF(wpath1);
623 Py_XDECREF(wpath2);
624 return NULL;
625 }
626 Py_BEGIN_ALLOW_THREADS
627 /* PyUnicode_AS_UNICODE OK without thread
628 lock as it is a simple dereference. */
629 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
630 PyUnicode_AS_UNICODE(wpath2));
631 Py_END_ALLOW_THREADS
632 Py_XDECREF(wpath1);
633 Py_XDECREF(wpath2);
634 if (res != 0)
635 return posix_error();
636 Py_INCREF(Py_None);
637 return Py_None;
638 }
639 /* Else flow through as neither is Unicode. */
640 }
641 /* Drop the argument parsing error as narrow
642 strings are also valid. */
643 PyErr_Clear();
644 }
645#else
646 /* Platforms that don't support Unicode filenames
647 shouldn't be passing these extra params */
648 assert(wformat==NULL && wfunc == NULL);
649#endif
650
Mark Hammondef8b6542001-05-13 08:04:26 +0000651 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000652 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000653 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000655 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000656 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000657 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000658 PyMem_Free(path1);
659 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000660 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000661 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000663 Py_INCREF(Py_None);
664 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665}
666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000667PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000668"stat_result: Result from stat or lstat.\n\n\
669This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000670 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000671or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
672\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000673Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000674they are available as attributes only.\n\
675\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000677
678static PyStructSequence_Field stat_result_fields[] = {
679 {"st_mode", "protection bits"},
680 {"st_ino", "inode"},
681 {"st_dev", "device"},
682 {"st_nlink", "number of hard links"},
683 {"st_uid", "user ID of owner"},
684 {"st_gid", "group ID of owner"},
685 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000686 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
687 {NULL, "integer time of last access"},
688 {NULL, "integer time of last modification"},
689 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000690 {"st_atime", "time of last access"},
691 {"st_mtime", "time of last modification"},
692 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000693#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000694 {"st_blksize", "blocksize for filesystem I/O"},
695#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000696#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000697 {"st_blocks", "number of blocks allocated"},
698#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000699#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000700 {"st_rdev", "device type (if inode device)"},
701#endif
702 {0}
703};
704
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000705#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000706#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000707#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000708#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000709#endif
710
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000711#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
713#else
714#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
715#endif
716
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000717#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000718#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
719#else
720#define ST_RDEV_IDX ST_BLOCKS_IDX
721#endif
722
723static PyStructSequence_Desc stat_result_desc = {
724 "stat_result", /* name */
725 stat_result__doc__, /* doc */
726 stat_result_fields,
727 10
728};
729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000731"statvfs_result: Result from statvfs or fstatvfs.\n\n\
732This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000733 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000734or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000735\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000736See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000737
738static PyStructSequence_Field statvfs_result_fields[] = {
739 {"f_bsize", },
740 {"f_frsize", },
741 {"f_blocks", },
742 {"f_bfree", },
743 {"f_bavail", },
744 {"f_files", },
745 {"f_ffree", },
746 {"f_favail", },
747 {"f_flag", },
748 {"f_namemax",},
749 {0}
750};
751
752static PyStructSequence_Desc statvfs_result_desc = {
753 "statvfs_result", /* name */
754 statvfs_result__doc__, /* doc */
755 statvfs_result_fields,
756 10
757};
758
759static PyTypeObject StatResultType;
760static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000761static newfunc structseq_new;
762
763static PyObject *
764statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
765{
766 PyStructSequence *result;
767 int i;
768
769 result = (PyStructSequence*)structseq_new(type, args, kwds);
770 if (!result)
771 return NULL;
772 /* If we have been initialized from a tuple,
773 st_?time might be set to None. Initialize it
774 from the int slots. */
775 for (i = 7; i <= 9; i++) {
776 if (result->ob_item[i+3] == Py_None) {
777 Py_DECREF(Py_None);
778 Py_INCREF(result->ob_item[i]);
779 result->ob_item[i+3] = result->ob_item[i];
780 }
781 }
782 return (PyObject*)result;
783}
784
785
786
787/* If true, st_?time is float. */
788static int _stat_float_times = 0;
789
790PyDoc_STRVAR(stat_float_times__doc__,
791"stat_float_times([newval]) -> oldval\n\n\
792Determine whether os.[lf]stat represents time stamps as float objects.\n\
793If newval is True, future calls to stat() return floats, if it is False,\n\
794future calls return ints. \n\
795If newval is omitted, return the current setting.\n");
796
797static PyObject*
798stat_float_times(PyObject* self, PyObject *args)
799{
800 int newval = -1;
801 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
802 return NULL;
803 if (newval == -1)
804 /* Return old value */
805 return PyBool_FromLong(_stat_float_times);
806 _stat_float_times = newval;
807 Py_INCREF(Py_None);
808 return Py_None;
809}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000810
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000811static void
812fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
813{
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000814 PyObject *fval,*ival;
815#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000816 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000817#else
818 ival = PyInt_FromLong((long)sec);
819#endif
820 if (_stat_float_times) {
821 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
822 } else {
823 fval = ival;
824 Py_INCREF(fval);
825 }
826 PyStructSequence_SET_ITEM(v, index, ival);
827 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000828}
829
Tim Peters5aa91602002-01-30 05:46:57 +0000830/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +0000831 (used by posix_stat() and posix_fstat()) */
832static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000833_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000834{
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000835 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000836 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +0000837 if (v == NULL)
838 return NULL;
839
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000840 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
Fred Drake699f3522000-06-29 21:12:41 +0000841#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000842 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000843 PyLong_FromLongLong((PY_LONG_LONG)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000844#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000845 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000846#endif
847#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +0000848 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000849 PyLong_FromLongLong((PY_LONG_LONG)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000850#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000851 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000852#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000853 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
854 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
855 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
Fred Drake699f3522000-06-29 21:12:41 +0000856#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000857 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000858 PyLong_FromLongLong((PY_LONG_LONG)st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000859#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000860 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000861#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000862
863#ifdef HAVE_STAT_TV_NSEC
864 ansec = st.st_atim.tv_nsec;
865 mnsec = st.st_mtim.tv_nsec;
866 cnsec = st.st_ctim.tv_nsec;
Fred Drake699f3522000-06-29 21:12:41 +0000867#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000868 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000869#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000870 fill_time(v, 7, st.st_atime, ansec);
871 fill_time(v, 8, st.st_mtime, mnsec);
872 fill_time(v, 9, st.st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000873
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000874#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +0000875 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000876 PyInt_FromLong((long)st.st_blksize));
877#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000878#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +0000879 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000880 PyInt_FromLong((long)st.st_blocks));
881#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000882#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000883 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
884 PyInt_FromLong((long)st.st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +0000885#endif
886
887 if (PyErr_Occurred()) {
888 Py_DECREF(v);
889 return NULL;
890 }
891
892 return v;
893}
894
Barry Warsaw53699e91996-12-10 23:23:01 +0000895static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000896posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000897 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000898#ifdef __VMS
899 int (*statfunc)(const char *, STRUCT_STAT *, ...),
900#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000901 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000902#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000903 char *wformat,
904 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000905{
Fred Drake699f3522000-06-29 21:12:41 +0000906 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +0000907 char *path = NULL; /* pass this to stat; do not free() it */
908 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000909 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000910
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000911#ifdef MS_WINDOWS
Tim Peters500bd032001-12-19 19:05:01 +0000912 int pathlen;
913 char pathcopy[MAX_PATH];
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000914#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000915
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000916
917#ifdef Py_WIN_WIDE_FILENAMES
918 /* If on wide-character-capable OS see if argument
919 is Unicode and if so use wide API. */
920 if (unicode_file_names()) {
921 PyUnicodeObject *po;
922 if (PyArg_ParseTuple(args, wformat, &po)) {
923 Py_UNICODE wpath[MAX_PATH+1];
924 pathlen = wcslen(PyUnicode_AS_UNICODE(po));
925 /* the library call can blow up if the file name is too long! */
926 if (pathlen > MAX_PATH) {
927 errno = ENAMETOOLONG;
928 return posix_error();
929 }
930 wcscpy(wpath, PyUnicode_AS_UNICODE(po));
931 /* Remove trailing slash or backslash, unless it's the current
932 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
933 */
934 if (pathlen > 0 &&
935 (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) {
936 /* It does end with a slash -- exempt the root drive cases. */
937 /* XXX UNC root drives should also be exempted? */
938 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':'))
939 /* leave it alone */;
940 else {
941 /* nuke the trailing backslash */
942 wpath[pathlen-1] = L'\0';
943 }
944 }
945 Py_BEGIN_ALLOW_THREADS
946 /* PyUnicode_AS_UNICODE result OK without
947 thread lock as it is a simple dereference. */
948 res = wstatfunc(wpath, &st);
949 Py_END_ALLOW_THREADS
950 if (res != 0)
951 return posix_error_with_unicode_filename(wpath);
952 return _pystat_fromstructstat(st);
953 }
954 /* Drop the argument parsing error as narrow strings
955 are also valid. */
956 PyErr_Clear();
957 }
958#endif
959
Tim Peters5aa91602002-01-30 05:46:57 +0000960 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000961 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000962 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +0000963 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000964
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000965#ifdef MS_WINDOWS
Guido van Rossumace88ae2000-04-21 18:54:45 +0000966 pathlen = strlen(path);
967 /* the library call can blow up if the file name is too long! */
968 if (pathlen > MAX_PATH) {
Tim Peters500bd032001-12-19 19:05:01 +0000969 PyMem_Free(pathfree);
Guido van Rossumace88ae2000-04-21 18:54:45 +0000970 errno = ENAMETOOLONG;
971 return posix_error();
972 }
973
Tim Peters500bd032001-12-19 19:05:01 +0000974 /* Remove trailing slash or backslash, unless it's the current
975 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
976 */
977 if (pathlen > 0 &&
978 (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) {
979 /* It does end with a slash -- exempt the root drive cases. */
980 /* XXX UNC root drives should also be exempted? */
981 if (pathlen == 1 || (pathlen == 3 && path[1] == ':'))
982 /* leave it alone */;
983 else {
984 /* nuke the trailing backslash */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000985 strncpy(pathcopy, path, pathlen);
Tim Peters500bd032001-12-19 19:05:01 +0000986 pathcopy[pathlen-1] = '\0';
Guido van Rossumace88ae2000-04-21 18:54:45 +0000987 path = pathcopy;
988 }
989 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000990#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000991
Barry Warsaw53699e91996-12-10 23:23:01 +0000992 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000993 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000994 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000995 if (res != 0)
Tim Peters500bd032001-12-19 19:05:01 +0000996 return posix_error_with_allocated_filename(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +0000997
Tim Peters500bd032001-12-19 19:05:01 +0000998 PyMem_Free(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +0000999 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000}
1001
1002
1003/* POSIX methods */
1004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001005PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001006"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001007Use the real uid/gid to test for access to a path. Note that most\n\
1008operations will use the effective uid/gid, therefore this routine can\n\
1009be used in a suid/sgid environment to test if the invoking user has the\n\
1010specified access to the path. The mode argument can be F_OK to test\n\
1011existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001012
1013static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001014posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001015{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001016 char *path;
1017 int mode;
1018 int res;
1019
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001020#ifdef Py_WIN_WIDE_FILENAMES
1021 if (unicode_file_names()) {
1022 PyUnicodeObject *po;
1023 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1024 Py_BEGIN_ALLOW_THREADS
1025 /* PyUnicode_AS_UNICODE OK without thread lock as
1026 it is a simple dereference. */
1027 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1028 Py_END_ALLOW_THREADS
1029 return(PyBool_FromLong(res == 0));
1030 }
1031 /* Drop the argument parsing error as narrow strings
1032 are also valid. */
1033 PyErr_Clear();
1034 }
1035#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001036 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001037 return NULL;
1038 Py_BEGIN_ALLOW_THREADS
1039 res = access(path, mode);
1040 Py_END_ALLOW_THREADS
Guido van Rossum674deb22002-09-01 15:06:28 +00001041 return(PyBool_FromLong(res == 0));
Guido van Rossum94f6f721999-01-06 18:42:14 +00001042}
1043
Guido van Rossumd371ff11999-01-25 16:12:23 +00001044#ifndef F_OK
1045#define F_OK 0
1046#endif
1047#ifndef R_OK
1048#define R_OK 4
1049#endif
1050#ifndef W_OK
1051#define W_OK 2
1052#endif
1053#ifndef X_OK
1054#define X_OK 1
1055#endif
1056
1057#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001058PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001059"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001060Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001061
1062static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001063posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001064{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001065 int id;
1066 char *ret;
1067
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001068 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001069 return NULL;
1070
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001071#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001072 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001073 if (id == 0) {
1074 ret = ttyname();
1075 }
1076 else {
1077 ret = NULL;
1078 }
1079#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001080 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001081#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001082 if (ret == NULL)
1083 return(posix_error());
1084 return(PyString_FromString(ret));
1085}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001086#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001087
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001088#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001089PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001090"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001092
1093static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001094posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001095{
1096 char *ret;
1097 char buffer[L_ctermid];
1098
Greg Wardb48bc172000-03-01 21:51:56 +00001099#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001100 ret = ctermid_r(buffer);
1101#else
1102 ret = ctermid(buffer);
1103#endif
1104 if (ret == NULL)
1105 return(posix_error());
1106 return(PyString_FromString(buffer));
1107}
1108#endif
1109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001110PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001111"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001112Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001113
Barry Warsaw53699e91996-12-10 23:23:01 +00001114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001115posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001116{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001117#ifdef MS_WINDOWS
1118 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1119#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1120 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001121#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001122 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001123 NULL, NULL);
1124#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001125 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001126#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001127}
1128
Fred Drake4d1e64b2002-04-15 19:40:07 +00001129#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001130PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001131"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001132Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001134
1135static PyObject *
1136posix_fchdir(PyObject *self, PyObject *fdobj)
1137{
1138 return posix_fildes(fdobj, fchdir);
1139}
1140#endif /* HAVE_FCHDIR */
1141
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001142
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001143PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001144"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001146
Barry Warsaw53699e91996-12-10 23:23:01 +00001147static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001148posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001149{
Mark Hammondef8b6542001-05-13 08:04:26 +00001150 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001151 int i;
1152 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001153#ifdef Py_WIN_WIDE_FILENAMES
1154 if (unicode_file_names()) {
1155 PyUnicodeObject *po;
1156 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1157 Py_BEGIN_ALLOW_THREADS
1158 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1159 Py_END_ALLOW_THREADS
1160 if (res < 0)
1161 return posix_error_with_unicode_filename(
1162 PyUnicode_AS_UNICODE(po));
1163 Py_INCREF(Py_None);
1164 return Py_None;
1165 }
1166 /* Drop the argument parsing error as narrow strings
1167 are also valid. */
1168 PyErr_Clear();
1169 }
1170#endif /* Py_WIN_WIDE_FILENAMES */
1171 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001172 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001173 return NULL;
1174 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001175 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001176 Py_END_ALLOW_THREADS
1177 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001178 return posix_error_with_allocated_filename(path);
1179 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001180 Py_INCREF(Py_None);
1181 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182}
1183
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001184
Martin v. Löwis244edc82001-10-04 22:44:26 +00001185#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001187"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001189
1190static PyObject *
1191posix_chroot(PyObject *self, PyObject *args)
1192{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001193 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001194}
1195#endif
1196
Guido van Rossum21142a01999-01-08 21:05:37 +00001197#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001199"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001200force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001201
1202static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001203posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001204{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001205 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001206}
1207#endif /* HAVE_FSYNC */
1208
1209#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001210
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001211#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001212extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1213#endif
1214
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001215PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001216"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001217force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001218 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001219
1220static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001221posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001222{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001223 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001224}
1225#endif /* HAVE_FDATASYNC */
1226
1227
Fredrik Lundh10723342000-07-10 16:38:09 +00001228#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001229PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001230"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001231Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001232
Barry Warsaw53699e91996-12-10 23:23:01 +00001233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001234posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001235{
Mark Hammondef8b6542001-05-13 08:04:26 +00001236 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001237 int uid, gid;
1238 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001239 if (!PyArg_ParseTuple(args, "etii:chown",
1240 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001241 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001242 return NULL;
1243 Py_BEGIN_ALLOW_THREADS
1244 res = chown(path, (uid_t) uid, (gid_t) gid);
1245 Py_END_ALLOW_THREADS
1246 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001247 return posix_error_with_allocated_filename(path);
1248 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001249 Py_INCREF(Py_None);
1250 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001251}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001252#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001253
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001254#ifdef HAVE_LCHOWN
1255PyDoc_STRVAR(posix_lchown__doc__,
1256"lchown(path, uid, gid)\n\n\
1257Change the owner and group id of path to the numeric uid and gid.\n\
1258This function will not follow symbolic links.");
1259
1260static PyObject *
1261posix_lchown(PyObject *self, PyObject *args)
1262{
1263 char *path = NULL;
1264 int uid, gid;
1265 int res;
1266 if (!PyArg_ParseTuple(args, "etii:lchown",
1267 Py_FileSystemDefaultEncoding, &path,
1268 &uid, &gid))
1269 return NULL;
1270 Py_BEGIN_ALLOW_THREADS
1271 res = lchown(path, (uid_t) uid, (gid_t) gid);
1272 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001273 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001274 return posix_error_with_allocated_filename(path);
1275 PyMem_Free(path);
1276 Py_INCREF(Py_None);
1277 return Py_None;
1278}
1279#endif /* HAVE_LCHOWN */
1280
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001281
Guido van Rossum36bc6801995-06-14 22:54:23 +00001282#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001283PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001284"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001285Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001286
Barry Warsaw53699e91996-12-10 23:23:01 +00001287static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001288posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001289{
1290 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001291 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001292
Barry Warsaw53699e91996-12-10 23:23:01 +00001293 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001294#if defined(PYOS_OS2) && defined(PYCC_GCC)
1295 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001296#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001297 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001298#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001299 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001300 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001301 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001302 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001303}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001304
Walter Dörwald3b918c32002-11-21 20:18:46 +00001305#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001306PyDoc_STRVAR(posix_getcwdu__doc__,
1307"getcwdu() -> path\n\n\
1308Return a unicode string representing the current working directory.");
1309
1310static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001311posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001312{
1313 char buf[1026];
1314 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001315
1316#ifdef Py_WIN_WIDE_FILENAMES
1317 if (unicode_file_names()) {
1318 wchar_t *wres;
1319 wchar_t wbuf[1026];
1320 Py_BEGIN_ALLOW_THREADS
1321 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1322 Py_END_ALLOW_THREADS
1323 if (wres == NULL)
1324 return posix_error();
1325 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1326 }
1327#endif
1328
1329 Py_BEGIN_ALLOW_THREADS
1330#if defined(PYOS_OS2) && defined(PYCC_GCC)
1331 res = _getcwd2(buf, sizeof buf);
1332#else
1333 res = getcwd(buf, sizeof buf);
1334#endif
1335 Py_END_ALLOW_THREADS
1336 if (res == NULL)
1337 return posix_error();
1338 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1339}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001340#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001341#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001343
Guido van Rossumb6775db1994-08-01 11:34:53 +00001344#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001345PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001346"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001347Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001348
Barry Warsaw53699e91996-12-10 23:23:01 +00001349static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001350posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001351{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001352 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001353}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001354#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001355
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001358"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001359Return a list containing the names of the entries in the directory.\n\
1360\n\
1361 path: path of directory to list\n\
1362\n\
1363The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001364entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001365
Barry Warsaw53699e91996-12-10 23:23:01 +00001366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001367posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001368{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001369 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001370 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001371#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001372
Barry Warsaw53699e91996-12-10 23:23:01 +00001373 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001374 HANDLE hFindFile;
1375 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001376 /* MAX_PATH characters could mean a bigger encoded string */
1377 char namebuf[MAX_PATH*2+5];
1378 char *bufptr = namebuf;
1379 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001380
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001381#ifdef Py_WIN_WIDE_FILENAMES
1382 /* If on wide-character-capable OS see if argument
1383 is Unicode and if so use wide API. */
1384 if (unicode_file_names()) {
1385 PyUnicodeObject *po;
1386 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1387 WIN32_FIND_DATAW wFileData;
1388 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1389 Py_UNICODE wch;
1390 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1391 wnamebuf[MAX_PATH] = L'\0';
1392 len = wcslen(wnamebuf);
1393 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1394 if (wch != L'/' && wch != L'\\' && wch != L':')
1395 wnamebuf[len++] = L'/';
1396 wcscpy(wnamebuf + len, L"*.*");
1397 if ((d = PyList_New(0)) == NULL)
1398 return NULL;
1399 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1400 if (hFindFile == INVALID_HANDLE_VALUE) {
1401 errno = GetLastError();
1402 if (errno == ERROR_FILE_NOT_FOUND) {
1403 return d;
1404 }
1405 Py_DECREF(d);
1406 return win32_error_unicode("FindFirstFileW", wnamebuf);
1407 }
1408 do {
1409 if (wFileData.cFileName[0] == L'.' &&
1410 (wFileData.cFileName[1] == L'\0' ||
1411 wFileData.cFileName[1] == L'.' &&
1412 wFileData.cFileName[2] == L'\0'))
1413 continue;
1414 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1415 if (v == NULL) {
1416 Py_DECREF(d);
1417 d = NULL;
1418 break;
1419 }
1420 if (PyList_Append(d, v) != 0) {
1421 Py_DECREF(v);
1422 Py_DECREF(d);
1423 d = NULL;
1424 break;
1425 }
1426 Py_DECREF(v);
1427 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1428
1429 if (FindClose(hFindFile) == FALSE) {
1430 Py_DECREF(d);
1431 return win32_error_unicode("FindClose", wnamebuf);
1432 }
1433 return d;
1434 }
1435 /* Drop the argument parsing error as narrow strings
1436 are also valid. */
1437 PyErr_Clear();
1438 }
1439#endif
1440
Tim Peters5aa91602002-01-30 05:46:57 +00001441 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001442 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001443 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001444 if (len > 0) {
1445 char ch = namebuf[len-1];
1446 if (ch != SEP && ch != ALTSEP && ch != ':')
1447 namebuf[len++] = '/';
1448 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001449 strcpy(namebuf + len, "*.*");
1450
Barry Warsaw53699e91996-12-10 23:23:01 +00001451 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001452 return NULL;
1453
1454 hFindFile = FindFirstFile(namebuf, &FileData);
1455 if (hFindFile == INVALID_HANDLE_VALUE) {
1456 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001457 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001458 return d;
1459 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001460 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001461 }
1462 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001463 if (FileData.cFileName[0] == '.' &&
1464 (FileData.cFileName[1] == '\0' ||
1465 FileData.cFileName[1] == '.' &&
1466 FileData.cFileName[2] == '\0'))
1467 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001468 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001469 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001470 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001471 d = NULL;
1472 break;
1473 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001474 if (PyList_Append(d, v) != 0) {
1475 Py_DECREF(v);
1476 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001477 d = NULL;
1478 break;
1479 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001480 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001481 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1482
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001483 if (FindClose(hFindFile) == FALSE) {
1484 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001485 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001486 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001487
1488 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001489
Tim Peters0bb44a42000-09-15 07:44:49 +00001490#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001491
1492#ifndef MAX_PATH
1493#define MAX_PATH CCHMAXPATH
1494#endif
1495 char *name, *pt;
1496 int len;
1497 PyObject *d, *v;
1498 char namebuf[MAX_PATH+5];
1499 HDIR hdir = 1;
1500 ULONG srchcnt = 1;
1501 FILEFINDBUF3 ep;
1502 APIRET rc;
1503
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001504 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001505 return NULL;
1506 if (len >= MAX_PATH) {
1507 PyErr_SetString(PyExc_ValueError, "path too long");
1508 return NULL;
1509 }
1510 strcpy(namebuf, name);
1511 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001512 if (*pt == ALTSEP)
1513 *pt = SEP;
1514 if (namebuf[len-1] != SEP)
1515 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001516 strcpy(namebuf + len, "*.*");
1517
1518 if ((d = PyList_New(0)) == NULL)
1519 return NULL;
1520
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001521 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1522 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001523 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001524 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1525 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1526 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001527
1528 if (rc != NO_ERROR) {
1529 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001530 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001531 }
1532
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001533 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001534 do {
1535 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001536 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001537 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001538
1539 strcpy(namebuf, ep.achName);
1540
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001541 /* Leave Case of Name Alone -- In Native Form */
1542 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001543
1544 v = PyString_FromString(namebuf);
1545 if (v == NULL) {
1546 Py_DECREF(d);
1547 d = NULL;
1548 break;
1549 }
1550 if (PyList_Append(d, v) != 0) {
1551 Py_DECREF(v);
1552 Py_DECREF(d);
1553 d = NULL;
1554 break;
1555 }
1556 Py_DECREF(v);
1557 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1558 }
1559
1560 return d;
1561#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001562
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001563 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001564 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001565 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001566 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001567 int arg_is_unicode = 1;
1568
1569 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1570 arg_is_unicode = 0;
1571 PyErr_Clear();
1572 }
1573 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001574 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001575 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001576 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001577 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001578 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001579 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001580 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001581 return NULL;
1582 }
1583 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001584 if (ep->d_name[0] == '.' &&
1585 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001586 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001587 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001588 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001589 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001590 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001591 d = NULL;
1592 break;
1593 }
Just van Rossum46c97842003-02-25 21:42:15 +00001594#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001595 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001596 PyObject *w;
1597
1598 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001599 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001600 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001601 if (w != NULL) {
1602 Py_DECREF(v);
1603 v = w;
1604 }
1605 else {
1606 /* fall back to the original byte string, as
1607 discussed in patch #683592 */
1608 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001609 }
Just van Rossum46c97842003-02-25 21:42:15 +00001610 }
1611#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001612 if (PyList_Append(d, v) != 0) {
1613 Py_DECREF(v);
1614 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001615 d = NULL;
1616 break;
1617 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001618 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001619 }
1620 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001621 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001622
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001623 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001624
Tim Peters0bb44a42000-09-15 07:44:49 +00001625#endif /* which OS */
1626} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001628#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001629/* A helper function for abspath on win32 */
1630static PyObject *
1631posix__getfullpathname(PyObject *self, PyObject *args)
1632{
1633 /* assume encoded strings wont more than double no of chars */
1634 char inbuf[MAX_PATH*2];
1635 char *inbufp = inbuf;
1636 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1637 char outbuf[MAX_PATH*2];
1638 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001639#ifdef Py_WIN_WIDE_FILENAMES
1640 if (unicode_file_names()) {
1641 PyUnicodeObject *po;
1642 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1643 Py_UNICODE woutbuf[MAX_PATH*2];
1644 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001645 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001646 sizeof(woutbuf)/sizeof(woutbuf[0]),
1647 woutbuf, &wtemp))
1648 return win32_error("GetFullPathName", "");
1649 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1650 }
1651 /* Drop the argument parsing error as narrow strings
1652 are also valid. */
1653 PyErr_Clear();
1654 }
1655#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001656 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1657 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001658 &insize))
1659 return NULL;
1660 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1661 outbuf, &temp))
1662 return win32_error("GetFullPathName", inbuf);
1663 return PyString_FromString(outbuf);
1664} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001665#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001667PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001668"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001669Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001670
Barry Warsaw53699e91996-12-10 23:23:01 +00001671static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001672posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001674 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001675 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001676 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001677
1678#ifdef Py_WIN_WIDE_FILENAMES
1679 if (unicode_file_names()) {
1680 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001681 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001682 Py_BEGIN_ALLOW_THREADS
1683 /* PyUnicode_AS_UNICODE OK without thread lock as
1684 it is a simple dereference. */
1685 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1686 Py_END_ALLOW_THREADS
1687 if (res < 0)
1688 return posix_error();
1689 Py_INCREF(Py_None);
1690 return Py_None;
1691 }
1692 /* Drop the argument parsing error as narrow strings
1693 are also valid. */
1694 PyErr_Clear();
1695 }
1696#endif
1697
Tim Peters5aa91602002-01-30 05:46:57 +00001698 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001699 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001700 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001701 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001702#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001703 res = mkdir(path);
1704#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001705 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001706#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001707 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001708 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001709 return posix_error_with_allocated_filename(path);
1710 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001711 Py_INCREF(Py_None);
1712 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001713}
1714
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001715
Guido van Rossumb6775db1994-08-01 11:34:53 +00001716#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001717#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1718#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1719#include <sys/resource.h>
1720#endif
1721#endif
1722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001723PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001724"nice(inc) -> new_priority\n\n\
1725Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001726
Barry Warsaw53699e91996-12-10 23:23:01 +00001727static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001728posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001729{
1730 int increment, value;
1731
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001732 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001733 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001734
1735 /* There are two flavours of 'nice': one that returns the new
1736 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001737 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1738 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001739
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001740 If we are of the nice family that returns the new priority, we
1741 need to clear errno before the call, and check if errno is filled
1742 before calling posix_error() on a returnvalue of -1, because the
1743 -1 may be the actual new priority! */
1744
1745 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001746 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001747#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001748 if (value == 0)
1749 value = getpriority(PRIO_PROCESS, 0);
1750#endif
1751 if (value == -1 && errno != 0)
1752 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001753 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001754 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001755}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001756#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001757
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001759PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001760"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001761Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001762
Barry Warsaw53699e91996-12-10 23:23:01 +00001763static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001764posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001765{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001766#ifdef MS_WINDOWS
1767 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1768#else
1769 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1770#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001771}
1772
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001774PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001775"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001776Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001777
Barry Warsaw53699e91996-12-10 23:23:01 +00001778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001779posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001780{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001781#ifdef MS_WINDOWS
1782 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1783#else
1784 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1785#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001786}
1787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001790"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001791Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001792
Barry Warsaw53699e91996-12-10 23:23:01 +00001793static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001794posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001795{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001796#ifdef MS_WINDOWS
1797 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1798#else
1799 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1800#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001801}
1802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001803
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001804#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001805PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001806"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001808
Barry Warsaw53699e91996-12-10 23:23:01 +00001809static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001810posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001811{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001812 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001813 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001814 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001815 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001816 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001817 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001818 Py_END_ALLOW_THREADS
1819 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001820}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001821#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001822
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001823
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001824PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001825"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001826Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001827
Barry Warsaw53699e91996-12-10 23:23:01 +00001828static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001829posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001830{
1831 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001832 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001833 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001834 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001835 if (i < 0)
1836 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001837 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001838}
1839
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001840
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001842"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001843Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001846"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001848
Barry Warsaw53699e91996-12-10 23:23:01 +00001849static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001850posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001851{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001852#ifdef MS_WINDOWS
1853 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1854#else
1855 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1856#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001857}
1858
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001859
Guido van Rossumb6775db1994-08-01 11:34:53 +00001860#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001862"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001864
Barry Warsaw53699e91996-12-10 23:23:01 +00001865static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001866posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001867{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001868 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001869 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001870
Barry Warsaw53699e91996-12-10 23:23:01 +00001871 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001872 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001873 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001874 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001875 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001876 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001877 u.sysname,
1878 u.nodename,
1879 u.release,
1880 u.version,
1881 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001882}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001883#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001884
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001885static int
1886extract_time(PyObject *t, long* sec, long* usec)
1887{
1888 long intval;
1889 if (PyFloat_Check(t)) {
1890 double tval = PyFloat_AsDouble(t);
1891 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1892 if (!intobj)
1893 return -1;
1894 intval = PyInt_AsLong(intobj);
1895 Py_DECREF(intobj);
1896 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001897 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001898 if (*usec < 0)
1899 /* If rounding gave us a negative number,
1900 truncate. */
1901 *usec = 0;
1902 return 0;
1903 }
1904 intval = PyInt_AsLong(t);
1905 if (intval == -1 && PyErr_Occurred())
1906 return -1;
1907 *sec = intval;
1908 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001909 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001910}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001913"utime(path, (atime, utime))\n\
1914utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001915Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001916second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001917
Barry Warsaw53699e91996-12-10 23:23:01 +00001918static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001919posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001920{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001921 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001922 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001923 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001924 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001925
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001926#if defined(HAVE_UTIMES)
1927 struct timeval buf[2];
1928#define ATIME buf[0].tv_sec
1929#define MTIME buf[1].tv_sec
1930#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001931/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001932 struct utimbuf buf;
1933#define ATIME buf.actime
1934#define MTIME buf.modtime
1935#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001936#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001937 time_t buf[2];
1938#define ATIME buf[0]
1939#define MTIME buf[1]
1940#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001941#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001942
Mark Hammond817c9292003-12-03 01:22:38 +00001943 int have_unicode_filename = 0;
1944#ifdef Py_WIN_WIDE_FILENAMES
1945 PyUnicodeObject *obwpath;
1946 wchar_t *wpath;
1947 if (unicode_file_names()) {
1948 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
1949 wpath = PyUnicode_AS_UNICODE(obwpath);
1950 have_unicode_filename = 1;
1951 } else
1952 /* Drop the argument parsing error as narrow strings
1953 are also valid. */
1954 PyErr_Clear();
1955 }
1956#endif /* Py_WIN_WIDE_FILENAMES */
1957
1958 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00001959 !PyArg_ParseTuple(args, "etO:utime",
1960 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001961 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001962 if (arg == Py_None) {
1963 /* optional time values not given */
1964 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00001965#ifdef Py_WIN_WIDE_FILENAMES
1966 if (have_unicode_filename)
1967 res = _wutime(wpath, NULL);
1968 else
1969#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00001970 res = utime(path, NULL);
1971 Py_END_ALLOW_THREADS
1972 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001973 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00001974 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001975 "utime() arg 2 must be a tuple (atime, mtime)");
Barry Warsaw3cef8562000-05-01 16:17:24 +00001976 return NULL;
1977 }
1978 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001979 if (extract_time(PyTuple_GET_ITEM(arg, 0),
1980 &atime, &ausec) == -1)
1981 return NULL;
1982 if (extract_time(PyTuple_GET_ITEM(arg, 1),
1983 &mtime, &musec) == -1)
1984 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001985 ATIME = atime;
1986 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001987#ifdef HAVE_UTIMES
1988 buf[0].tv_usec = ausec;
1989 buf[1].tv_usec = musec;
1990 Py_BEGIN_ALLOW_THREADS
1991 res = utimes(path, buf);
1992 Py_END_ALLOW_THREADS
1993#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00001994 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00001995#ifdef Py_WIN_WIDE_FILENAMES
1996 if (have_unicode_filename)
1997 /* utime is OK with utimbuf, but _wutime insists
1998 on _utimbuf (the msvc headers assert the
1999 underscore version is ansi) */
2000 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2001 else
2002#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002003 res = utime(path, UTIME_ARG);
2004 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002005#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002006 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002007 if (res < 0) {
2008#ifdef Py_WIN_WIDE_FILENAMES
2009 if (have_unicode_filename)
2010 return posix_error_with_unicode_filename(wpath);
2011#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsawd58d7641998-07-23 16:14:40 +00002012 return posix_error_with_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002013 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002014 Py_INCREF(Py_None);
2015 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002016#undef UTIME_ARG
2017#undef ATIME
2018#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002019}
2020
Guido van Rossum85e3b011991-06-03 12:42:10 +00002021
Guido van Rossum3b066191991-06-04 19:40:25 +00002022/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002024PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002025"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002026Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002027
Barry Warsaw53699e91996-12-10 23:23:01 +00002028static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002029posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002030{
2031 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002032 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002033 return NULL;
2034 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002035 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002036}
2037
Martin v. Löwis114619e2002-10-07 06:44:21 +00002038#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2039static void
2040free_string_array(char **array, int count)
2041{
2042 int i;
2043 for (i = 0; i < count; i++)
2044 PyMem_Free(array[i]);
2045 PyMem_DEL(array);
2046}
2047#endif
2048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002049
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002050#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002051PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002052"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002053Execute an executable path with arguments, replacing current process.\n\
2054\n\
2055 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057
Barry Warsaw53699e91996-12-10 23:23:01 +00002058static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002059posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002060{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002061 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002062 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002063 char **argvlist;
2064 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002065 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002066
Guido van Rossum89b33251993-10-22 14:26:06 +00002067 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002068 argv is a list or tuple of strings. */
2069
Martin v. Löwis114619e2002-10-07 06:44:21 +00002070 if (!PyArg_ParseTuple(args, "etO:execv",
2071 Py_FileSystemDefaultEncoding,
2072 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002073 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002074 if (PyList_Check(argv)) {
2075 argc = PyList_Size(argv);
2076 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002077 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002078 else if (PyTuple_Check(argv)) {
2079 argc = PyTuple_Size(argv);
2080 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002081 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002082 else {
Fred Drake661ea262000-10-24 19:57:45 +00002083 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002084 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002085 return NULL;
2086 }
2087
2088 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00002089 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002090 PyMem_Free(path);
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002091 return NULL;
2092 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002093
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002095 if (argvlist == NULL) {
2096 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002097 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002098 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002099 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002100 if (!PyArg_Parse((*getitem)(argv, i), "et",
2101 Py_FileSystemDefaultEncoding,
2102 &argvlist[i])) {
2103 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002104 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002105 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002106 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002107 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002108
Guido van Rossum85e3b011991-06-03 12:42:10 +00002109 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002110 }
2111 argvlist[argc] = NULL;
2112
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002113 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002114
Guido van Rossum85e3b011991-06-03 12:42:10 +00002115 /* If we get here it's definitely an error */
2116
Martin v. Löwis114619e2002-10-07 06:44:21 +00002117 free_string_array(argvlist, argc);
2118 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002119 return posix_error();
2120}
2121
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002123PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002124"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002125Execute a path with arguments and environment, replacing current process.\n\
2126\n\
2127 path: path of executable file\n\
2128 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002129 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002130
Barry Warsaw53699e91996-12-10 23:23:01 +00002131static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002132posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002133{
2134 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002135 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002136 char **argvlist;
2137 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002138 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002139 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002140 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002141 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002142
2143 /* execve has three arguments: (path, argv, env), where
2144 argv is a list or tuple of strings and env is a dictionary
2145 like posix.environ. */
2146
Martin v. Löwis114619e2002-10-07 06:44:21 +00002147 if (!PyArg_ParseTuple(args, "etOO:execve",
2148 Py_FileSystemDefaultEncoding,
2149 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002150 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002151 if (PyList_Check(argv)) {
2152 argc = PyList_Size(argv);
2153 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002154 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002155 else if (PyTuple_Check(argv)) {
2156 argc = PyTuple_Size(argv);
2157 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002158 }
2159 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002160 PyErr_SetString(PyExc_TypeError,
2161 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002162 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002163 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002164 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002165 PyErr_SetString(PyExc_TypeError,
2166 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002167 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002168 }
2169
Guido van Rossum50422b42000-04-26 20:34:28 +00002170 if (argc == 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00002171 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00002172 "execve() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002173 goto fail_0;
Guido van Rossum50422b42000-04-26 20:34:28 +00002174 }
2175
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002177 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002178 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002179 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002180 }
2181 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002182 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002183 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002184 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002185 &argvlist[i]))
2186 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002187 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002188 goto fail_1;
2189 }
2190 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002191 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002192 argvlist[argc] = NULL;
2193
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002194 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002195 if (i < 0)
2196 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002197 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002198 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002199 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002200 goto fail_1;
2201 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002202 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002203 keys = PyMapping_Keys(env);
2204 vals = PyMapping_Values(env);
2205 if (!keys || !vals)
2206 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002207 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2208 PyErr_SetString(PyExc_TypeError,
2209 "execve(): env.keys() or env.values() is not a list");
2210 goto fail_2;
2211 }
Tim Peters5aa91602002-01-30 05:46:57 +00002212
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002213 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002214 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002215 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002216
2217 key = PyList_GetItem(keys, pos);
2218 val = PyList_GetItem(vals, pos);
2219 if (!key || !val)
2220 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002221
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002222 if (!PyArg_Parse(
2223 key,
2224 "s;execve() arg 3 contains a non-string key",
2225 &k) ||
2226 !PyArg_Parse(
2227 val,
2228 "s;execve() arg 3 contains a non-string value",
2229 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002230 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002231 goto fail_2;
2232 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002233
2234#if defined(PYOS_OS2)
2235 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2236 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2237#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002238 len = PyString_Size(key) + PyString_Size(val) + 2;
2239 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002240 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002241 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002242 goto fail_2;
2243 }
Tim Petersc8996f52001-12-03 20:41:00 +00002244 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002245 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002246#if defined(PYOS_OS2)
2247 }
2248#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002249 }
2250 envlist[envc] = 0;
2251
2252 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002253
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002254 /* If we get here it's definitely an error */
2255
2256 (void) posix_error();
2257
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002258 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002259 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002260 PyMem_DEL(envlist[envc]);
2261 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002262 fail_1:
2263 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002264 Py_XDECREF(vals);
2265 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002266 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002267 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002268 return NULL;
2269}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002270#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002271
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002272
Guido van Rossuma1065681999-01-25 23:20:23 +00002273#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002274PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002275"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002276Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002277\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002278 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002279 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002280 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002281
2282static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002283posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002284{
2285 char *path;
2286 PyObject *argv;
2287 char **argvlist;
2288 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002289 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002290 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002291
2292 /* spawnv has three arguments: (mode, path, argv), where
2293 argv is a list or tuple of strings. */
2294
Martin v. Löwis114619e2002-10-07 06:44:21 +00002295 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2296 Py_FileSystemDefaultEncoding,
2297 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002298 return NULL;
2299 if (PyList_Check(argv)) {
2300 argc = PyList_Size(argv);
2301 getitem = PyList_GetItem;
2302 }
2303 else if (PyTuple_Check(argv)) {
2304 argc = PyTuple_Size(argv);
2305 getitem = PyTuple_GetItem;
2306 }
2307 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002308 PyErr_SetString(PyExc_TypeError,
2309 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002310 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002311 return NULL;
2312 }
2313
2314 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002315 if (argvlist == NULL) {
2316 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002317 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002318 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002319 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002320 if (!PyArg_Parse((*getitem)(argv, i), "et",
2321 Py_FileSystemDefaultEncoding,
2322 &argvlist[i])) {
2323 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002324 PyErr_SetString(
2325 PyExc_TypeError,
2326 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002327 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002328 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002329 }
2330 }
2331 argvlist[argc] = NULL;
2332
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002333#if defined(PYOS_OS2) && defined(PYCC_GCC)
2334 Py_BEGIN_ALLOW_THREADS
2335 spawnval = spawnv(mode, path, argvlist);
2336 Py_END_ALLOW_THREADS
2337#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002338 if (mode == _OLD_P_OVERLAY)
2339 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002340
Tim Peters25059d32001-12-07 20:35:43 +00002341 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002342 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002343 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002344#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002345
Martin v. Löwis114619e2002-10-07 06:44:21 +00002346 free_string_array(argvlist, argc);
2347 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002348
Fred Drake699f3522000-06-29 21:12:41 +00002349 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002350 return posix_error();
2351 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002352#if SIZEOF_LONG == SIZEOF_VOID_P
2353 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002354#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002355 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002356#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002357}
2358
2359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002360PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002361"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002362Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002363\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002364 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002365 path: path of executable file\n\
2366 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002367 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002368
2369static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002370posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002371{
2372 char *path;
2373 PyObject *argv, *env;
2374 char **argvlist;
2375 char **envlist;
2376 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2377 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002378 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002379 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002380 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002381
2382 /* spawnve has four arguments: (mode, path, argv, env), where
2383 argv is a list or tuple of strings and env is a dictionary
2384 like posix.environ. */
2385
Martin v. Löwis114619e2002-10-07 06:44:21 +00002386 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2387 Py_FileSystemDefaultEncoding,
2388 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002389 return NULL;
2390 if (PyList_Check(argv)) {
2391 argc = PyList_Size(argv);
2392 getitem = PyList_GetItem;
2393 }
2394 else if (PyTuple_Check(argv)) {
2395 argc = PyTuple_Size(argv);
2396 getitem = PyTuple_GetItem;
2397 }
2398 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002399 PyErr_SetString(PyExc_TypeError,
2400 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002401 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002402 }
2403 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002404 PyErr_SetString(PyExc_TypeError,
2405 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002406 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002407 }
2408
2409 argvlist = PyMem_NEW(char *, argc+1);
2410 if (argvlist == NULL) {
2411 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002412 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002413 }
2414 for (i = 0; i < argc; i++) {
2415 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002416 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002417 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002418 &argvlist[i]))
2419 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002420 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002421 goto fail_1;
2422 }
2423 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002424 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002425 argvlist[argc] = NULL;
2426
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002427 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002428 if (i < 0)
2429 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002430 envlist = PyMem_NEW(char *, i + 1);
2431 if (envlist == NULL) {
2432 PyErr_NoMemory();
2433 goto fail_1;
2434 }
2435 envc = 0;
2436 keys = PyMapping_Keys(env);
2437 vals = PyMapping_Values(env);
2438 if (!keys || !vals)
2439 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002440 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2441 PyErr_SetString(PyExc_TypeError,
2442 "spawnve(): env.keys() or env.values() is not a list");
2443 goto fail_2;
2444 }
Tim Peters5aa91602002-01-30 05:46:57 +00002445
Guido van Rossuma1065681999-01-25 23:20:23 +00002446 for (pos = 0; pos < i; pos++) {
2447 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002448 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002449
2450 key = PyList_GetItem(keys, pos);
2451 val = PyList_GetItem(vals, pos);
2452 if (!key || !val)
2453 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002454
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002455 if (!PyArg_Parse(
2456 key,
2457 "s;spawnve() arg 3 contains a non-string key",
2458 &k) ||
2459 !PyArg_Parse(
2460 val,
2461 "s;spawnve() arg 3 contains a non-string value",
2462 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002463 {
2464 goto fail_2;
2465 }
Tim Petersc8996f52001-12-03 20:41:00 +00002466 len = PyString_Size(key) + PyString_Size(val) + 2;
2467 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002468 if (p == NULL) {
2469 PyErr_NoMemory();
2470 goto fail_2;
2471 }
Tim Petersc8996f52001-12-03 20:41:00 +00002472 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002473 envlist[envc++] = p;
2474 }
2475 envlist[envc] = 0;
2476
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002477#if defined(PYOS_OS2) && defined(PYCC_GCC)
2478 Py_BEGIN_ALLOW_THREADS
2479 spawnval = spawnve(mode, path, argvlist, envlist);
2480 Py_END_ALLOW_THREADS
2481#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002482 if (mode == _OLD_P_OVERLAY)
2483 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002484
2485 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002486 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002487 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002488#endif
Tim Peters25059d32001-12-07 20:35:43 +00002489
Fred Drake699f3522000-06-29 21:12:41 +00002490 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002491 (void) posix_error();
2492 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002493#if SIZEOF_LONG == SIZEOF_VOID_P
2494 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002495#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002496 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002497#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002498
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002499 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002500 while (--envc >= 0)
2501 PyMem_DEL(envlist[envc]);
2502 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002503 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002504 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002505 Py_XDECREF(vals);
2506 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002507 fail_0:
2508 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002509 return res;
2510}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002511
2512/* OS/2 supports spawnvp & spawnvpe natively */
2513#if defined(PYOS_OS2)
2514PyDoc_STRVAR(posix_spawnvp__doc__,
2515"spawnvp(mode, file, args)\n\n\
2516Execute the program 'file' in a new process, using the environment\n\
2517search path to find the file.\n\
2518\n\
2519 mode: mode of process creation\n\
2520 file: executable file name\n\
2521 args: tuple or list of strings");
2522
2523static PyObject *
2524posix_spawnvp(PyObject *self, PyObject *args)
2525{
2526 char *path;
2527 PyObject *argv;
2528 char **argvlist;
2529 int mode, i, argc;
2530 Py_intptr_t spawnval;
2531 PyObject *(*getitem)(PyObject *, int);
2532
2533 /* spawnvp has three arguments: (mode, path, argv), where
2534 argv is a list or tuple of strings. */
2535
2536 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
2537 Py_FileSystemDefaultEncoding,
2538 &path, &argv))
2539 return NULL;
2540 if (PyList_Check(argv)) {
2541 argc = PyList_Size(argv);
2542 getitem = PyList_GetItem;
2543 }
2544 else if (PyTuple_Check(argv)) {
2545 argc = PyTuple_Size(argv);
2546 getitem = PyTuple_GetItem;
2547 }
2548 else {
2549 PyErr_SetString(PyExc_TypeError,
2550 "spawnvp() arg 2 must be a tuple or list");
2551 PyMem_Free(path);
2552 return NULL;
2553 }
2554
2555 argvlist = PyMem_NEW(char *, argc+1);
2556 if (argvlist == NULL) {
2557 PyMem_Free(path);
2558 return PyErr_NoMemory();
2559 }
2560 for (i = 0; i < argc; i++) {
2561 if (!PyArg_Parse((*getitem)(argv, i), "et",
2562 Py_FileSystemDefaultEncoding,
2563 &argvlist[i])) {
2564 free_string_array(argvlist, i);
2565 PyErr_SetString(
2566 PyExc_TypeError,
2567 "spawnvp() arg 2 must contain only strings");
2568 PyMem_Free(path);
2569 return NULL;
2570 }
2571 }
2572 argvlist[argc] = NULL;
2573
2574 Py_BEGIN_ALLOW_THREADS
2575#if defined(PYCC_GCC)
2576 spawnval = spawnvp(mode, path, argvlist);
2577#else
2578 spawnval = _spawnvp(mode, path, argvlist);
2579#endif
2580 Py_END_ALLOW_THREADS
2581
2582 free_string_array(argvlist, argc);
2583 PyMem_Free(path);
2584
2585 if (spawnval == -1)
2586 return posix_error();
2587 else
2588 return Py_BuildValue("l", (long) spawnval);
2589}
2590
2591
2592PyDoc_STRVAR(posix_spawnvpe__doc__,
2593"spawnvpe(mode, file, args, env)\n\n\
2594Execute the program 'file' in a new process, using the environment\n\
2595search path to find the file.\n\
2596\n\
2597 mode: mode of process creation\n\
2598 file: executable file name\n\
2599 args: tuple or list of arguments\n\
2600 env: dictionary of strings mapping to strings");
2601
2602static PyObject *
2603posix_spawnvpe(PyObject *self, PyObject *args)
2604{
2605 char *path;
2606 PyObject *argv, *env;
2607 char **argvlist;
2608 char **envlist;
2609 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2610 int mode, i, pos, argc, envc;
2611 Py_intptr_t spawnval;
2612 PyObject *(*getitem)(PyObject *, int);
2613 int lastarg = 0;
2614
2615 /* spawnvpe has four arguments: (mode, path, argv, env), where
2616 argv is a list or tuple of strings and env is a dictionary
2617 like posix.environ. */
2618
2619 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
2620 Py_FileSystemDefaultEncoding,
2621 &path, &argv, &env))
2622 return NULL;
2623 if (PyList_Check(argv)) {
2624 argc = PyList_Size(argv);
2625 getitem = PyList_GetItem;
2626 }
2627 else if (PyTuple_Check(argv)) {
2628 argc = PyTuple_Size(argv);
2629 getitem = PyTuple_GetItem;
2630 }
2631 else {
2632 PyErr_SetString(PyExc_TypeError,
2633 "spawnvpe() arg 2 must be a tuple or list");
2634 goto fail_0;
2635 }
2636 if (!PyMapping_Check(env)) {
2637 PyErr_SetString(PyExc_TypeError,
2638 "spawnvpe() arg 3 must be a mapping object");
2639 goto fail_0;
2640 }
2641
2642 argvlist = PyMem_NEW(char *, argc+1);
2643 if (argvlist == NULL) {
2644 PyErr_NoMemory();
2645 goto fail_0;
2646 }
2647 for (i = 0; i < argc; i++) {
2648 if (!PyArg_Parse((*getitem)(argv, i),
2649 "et;spawnvpe() arg 2 must contain only strings",
2650 Py_FileSystemDefaultEncoding,
2651 &argvlist[i]))
2652 {
2653 lastarg = i;
2654 goto fail_1;
2655 }
2656 }
2657 lastarg = argc;
2658 argvlist[argc] = NULL;
2659
2660 i = PyMapping_Size(env);
2661 if (i < 0)
2662 goto fail_1;
2663 envlist = PyMem_NEW(char *, i + 1);
2664 if (envlist == NULL) {
2665 PyErr_NoMemory();
2666 goto fail_1;
2667 }
2668 envc = 0;
2669 keys = PyMapping_Keys(env);
2670 vals = PyMapping_Values(env);
2671 if (!keys || !vals)
2672 goto fail_2;
2673 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2674 PyErr_SetString(PyExc_TypeError,
2675 "spawnvpe(): env.keys() or env.values() is not a list");
2676 goto fail_2;
2677 }
2678
2679 for (pos = 0; pos < i; pos++) {
2680 char *p, *k, *v;
2681 size_t len;
2682
2683 key = PyList_GetItem(keys, pos);
2684 val = PyList_GetItem(vals, pos);
2685 if (!key || !val)
2686 goto fail_2;
2687
2688 if (!PyArg_Parse(
2689 key,
2690 "s;spawnvpe() arg 3 contains a non-string key",
2691 &k) ||
2692 !PyArg_Parse(
2693 val,
2694 "s;spawnvpe() arg 3 contains a non-string value",
2695 &v))
2696 {
2697 goto fail_2;
2698 }
2699 len = PyString_Size(key) + PyString_Size(val) + 2;
2700 p = PyMem_NEW(char, len);
2701 if (p == NULL) {
2702 PyErr_NoMemory();
2703 goto fail_2;
2704 }
2705 PyOS_snprintf(p, len, "%s=%s", k, v);
2706 envlist[envc++] = p;
2707 }
2708 envlist[envc] = 0;
2709
2710 Py_BEGIN_ALLOW_THREADS
2711#if defined(PYCC_GCC)
2712 spawnval = spawnve(mode, path, argvlist, envlist);
2713#else
2714 spawnval = _spawnve(mode, path, argvlist, envlist);
2715#endif
2716 Py_END_ALLOW_THREADS
2717
2718 if (spawnval == -1)
2719 (void) posix_error();
2720 else
2721 res = Py_BuildValue("l", (long) spawnval);
2722
2723 fail_2:
2724 while (--envc >= 0)
2725 PyMem_DEL(envlist[envc]);
2726 PyMem_DEL(envlist);
2727 fail_1:
2728 free_string_array(argvlist, lastarg);
2729 Py_XDECREF(vals);
2730 Py_XDECREF(keys);
2731 fail_0:
2732 PyMem_Free(path);
2733 return res;
2734}
2735#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00002736#endif /* HAVE_SPAWNV */
2737
2738
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002739#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002740PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002741"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002742Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2743\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002744Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002745
2746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002747posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002748{
Neal Norwitze241ce82003-02-17 18:17:05 +00002749 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002750 if (pid == -1)
2751 return posix_error();
2752 PyOS_AfterFork();
2753 return PyInt_FromLong((long)pid);
2754}
2755#endif
2756
2757
Guido van Rossumad0ee831995-03-01 10:34:45 +00002758#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002759PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002760"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002761Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002762Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002763
Barry Warsaw53699e91996-12-10 23:23:01 +00002764static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002765posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002766{
Neal Norwitze241ce82003-02-17 18:17:05 +00002767 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002768 if (pid == -1)
2769 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002770 if (pid == 0)
2771 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002772 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002773}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002774#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002775
Neal Norwitzb59798b2003-03-21 01:43:31 +00002776/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002777/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2778#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002779#define DEV_PTY_FILE "/dev/ptc"
2780#define HAVE_DEV_PTMX
2781#else
2782#define DEV_PTY_FILE "/dev/ptmx"
2783#endif
2784
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002785#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002786#ifdef HAVE_PTY_H
2787#include <pty.h>
2788#else
2789#ifdef HAVE_LIBUTIL_H
2790#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002791#endif /* HAVE_LIBUTIL_H */
2792#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002793#ifdef HAVE_STROPTS_H
2794#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002795#endif
2796#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002797
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002798#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002799PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002800"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002801Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002802
2803static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002804posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002805{
2806 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002807#ifndef HAVE_OPENPTY
2808 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002809#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002810#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002811 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002812#ifdef sun
2813 extern char *ptsname();
2814#endif
2815#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002816
Thomas Wouters70c21a12000-07-14 14:28:33 +00002817#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002818 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2819 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002820#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002821 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2822 if (slave_name == NULL)
2823 return posix_error();
2824
2825 slave_fd = open(slave_name, O_RDWR);
2826 if (slave_fd < 0)
2827 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002828#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002829 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002830 if (master_fd < 0)
2831 return posix_error();
2832 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002833 /* change permission of slave */
2834 if (grantpt(master_fd) < 0) {
2835 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002836 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002837 }
2838 /* unlock slave */
2839 if (unlockpt(master_fd) < 0) {
2840 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002841 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002842 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002843 signal(SIGCHLD, sig_saved);
2844 slave_name = ptsname(master_fd); /* get name of slave */
2845 if (slave_name == NULL)
2846 return posix_error();
2847 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2848 if (slave_fd < 0)
2849 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002850#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002851 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2852 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002853#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002854 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002855#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002856#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002857#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002858
Fred Drake8cef4cf2000-06-28 16:40:38 +00002859 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002860
Fred Drake8cef4cf2000-06-28 16:40:38 +00002861}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002862#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002863
2864#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002865PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002866"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002867Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2868Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002869To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002870
2871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002872posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002873{
2874 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002875
Fred Drake8cef4cf2000-06-28 16:40:38 +00002876 pid = forkpty(&master_fd, NULL, NULL, NULL);
2877 if (pid == -1)
2878 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002879 if (pid == 0)
2880 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002881 return Py_BuildValue("(ii)", pid, master_fd);
2882}
2883#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002884
Guido van Rossumad0ee831995-03-01 10:34:45 +00002885#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002886PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002887"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002888Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002889
Barry Warsaw53699e91996-12-10 23:23:01 +00002890static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002891posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002892{
Barry Warsaw53699e91996-12-10 23:23:01 +00002893 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002894}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002895#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002896
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002897
Guido van Rossumad0ee831995-03-01 10:34:45 +00002898#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002899PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002900"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002901Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002902
Barry Warsaw53699e91996-12-10 23:23:01 +00002903static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002904posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002905{
Barry Warsaw53699e91996-12-10 23:23:01 +00002906 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002907}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002908#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002909
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002910
Guido van Rossumad0ee831995-03-01 10:34:45 +00002911#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002912PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002913"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002914Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002915
Barry Warsaw53699e91996-12-10 23:23:01 +00002916static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002917posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002918{
Barry Warsaw53699e91996-12-10 23:23:01 +00002919 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002920}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002921#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002924PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002925"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002926Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002927
Barry Warsaw53699e91996-12-10 23:23:01 +00002928static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002929posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002930{
Barry Warsaw53699e91996-12-10 23:23:01 +00002931 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002932}
2933
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002934
Fred Drakec9680921999-12-13 16:37:25 +00002935#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002936PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002937"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002938Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00002939
2940static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002941posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00002942{
2943 PyObject *result = NULL;
2944
Fred Drakec9680921999-12-13 16:37:25 +00002945#ifdef NGROUPS_MAX
2946#define MAX_GROUPS NGROUPS_MAX
2947#else
2948 /* defined to be 16 on Solaris7, so this should be a small number */
2949#define MAX_GROUPS 64
2950#endif
2951 gid_t grouplist[MAX_GROUPS];
2952 int n;
2953
2954 n = getgroups(MAX_GROUPS, grouplist);
2955 if (n < 0)
2956 posix_error();
2957 else {
2958 result = PyList_New(n);
2959 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00002960 int i;
2961 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00002962 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00002963 if (o == NULL) {
2964 Py_DECREF(result);
2965 result = NULL;
2966 break;
2967 }
2968 PyList_SET_ITEM(result, i, o);
2969 }
2970 }
2971 }
Neal Norwitze241ce82003-02-17 18:17:05 +00002972
Fred Drakec9680921999-12-13 16:37:25 +00002973 return result;
2974}
2975#endif
2976
Martin v. Löwis606edc12002-06-13 21:09:11 +00002977#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002978PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002979"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002980Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00002981
2982static PyObject *
2983posix_getpgid(PyObject *self, PyObject *args)
2984{
2985 int pid, pgid;
2986 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
2987 return NULL;
2988 pgid = getpgid(pid);
2989 if (pgid < 0)
2990 return posix_error();
2991 return PyInt_FromLong((long)pgid);
2992}
2993#endif /* HAVE_GETPGID */
2994
2995
Guido van Rossumb6775db1994-08-01 11:34:53 +00002996#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002997PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002998"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002999Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003000
Barry Warsaw53699e91996-12-10 23:23:01 +00003001static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003002posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003003{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003004#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003005 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003006#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003007 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003008#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003009}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003010#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003012
Guido van Rossumb6775db1994-08-01 11:34:53 +00003013#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003014PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003015"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003016Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003017
Barry Warsaw53699e91996-12-10 23:23:01 +00003018static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003019posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003020{
Guido van Rossum64933891994-10-20 21:56:42 +00003021#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003022 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003023#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003024 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003025#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003026 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003027 Py_INCREF(Py_None);
3028 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003029}
3030
Guido van Rossumb6775db1994-08-01 11:34:53 +00003031#endif /* HAVE_SETPGRP */
3032
Guido van Rossumad0ee831995-03-01 10:34:45 +00003033#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003034PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003035"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003037
Barry Warsaw53699e91996-12-10 23:23:01 +00003038static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003039posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003040{
Barry Warsaw53699e91996-12-10 23:23:01 +00003041 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003042}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003043#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003044
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003045
Fred Drake12c6e2d1999-12-14 21:25:03 +00003046#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003047PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003048"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003049Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003050
3051static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003052posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003053{
Neal Norwitze241ce82003-02-17 18:17:05 +00003054 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003055 char *name;
3056 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003057
Fred Drakea30680b2000-12-06 21:24:28 +00003058 errno = 0;
3059 name = getlogin();
3060 if (name == NULL) {
3061 if (errno)
3062 posix_error();
3063 else
3064 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003065 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003066 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003067 else
3068 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003069 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003070
Fred Drake12c6e2d1999-12-14 21:25:03 +00003071 return result;
3072}
3073#endif
3074
Guido van Rossumad0ee831995-03-01 10:34:45 +00003075#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003076PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003077"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003078Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003079
Barry Warsaw53699e91996-12-10 23:23:01 +00003080static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003081posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003082{
Barry Warsaw53699e91996-12-10 23:23:01 +00003083 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003084}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003085#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003086
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003087
Guido van Rossumad0ee831995-03-01 10:34:45 +00003088#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003089PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003090"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003091Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003092
Barry Warsaw53699e91996-12-10 23:23:01 +00003093static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003094posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003095{
3096 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003097 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003098 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003099#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003100 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3101 APIRET rc;
3102 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003103 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003104
3105 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3106 APIRET rc;
3107 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003108 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003109
3110 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003111 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003112#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003113 if (kill(pid, sig) == -1)
3114 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003115#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003116 Py_INCREF(Py_None);
3117 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003118}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003119#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003120
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003121#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003122PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003123"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003124Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003125
3126static PyObject *
3127posix_killpg(PyObject *self, PyObject *args)
3128{
3129 int pgid, sig;
3130 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3131 return NULL;
3132 if (killpg(pgid, sig) == -1)
3133 return posix_error();
3134 Py_INCREF(Py_None);
3135 return Py_None;
3136}
3137#endif
3138
Guido van Rossumc0125471996-06-28 18:55:32 +00003139#ifdef HAVE_PLOCK
3140
3141#ifdef HAVE_SYS_LOCK_H
3142#include <sys/lock.h>
3143#endif
3144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003145PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003146"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003147Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003148
Barry Warsaw53699e91996-12-10 23:23:01 +00003149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003150posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003151{
3152 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003153 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003154 return NULL;
3155 if (plock(op) == -1)
3156 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003157 Py_INCREF(Py_None);
3158 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003159}
3160#endif
3161
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003162
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003163#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003165"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003166Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003168#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003169#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003170static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003171async_system(const char *command)
3172{
3173 char *p, errormsg[256], args[1024];
3174 RESULTCODES rcodes;
3175 APIRET rc;
3176 char *shell = getenv("COMSPEC");
3177 if (!shell)
3178 shell = "cmd";
3179
3180 strcpy(args, shell);
3181 p = &args[ strlen(args)+1 ];
3182 strcpy(p, "/c ");
3183 strcat(p, command);
3184 p += strlen(p) + 1;
3185 *p = '\0';
3186
3187 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003188 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003189 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003190 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003191 &rcodes, shell);
3192 return rc;
3193}
3194
Guido van Rossumd48f2521997-12-05 22:19:34 +00003195static FILE *
3196popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003197{
3198 HFILE rhan, whan;
3199 FILE *retfd = NULL;
3200 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
3201
Guido van Rossumd48f2521997-12-05 22:19:34 +00003202 if (rc != NO_ERROR) {
3203 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003204 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003205 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003206
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003207 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
3208 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003209
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003210 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3211 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003212
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003213 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
3214 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003215
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003216 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003217 }
3218
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003219 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
3220 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003221
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003222 if (rc == NO_ERROR)
3223 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
3224
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003225 close(oldfd); /* And Close Saved STDOUT Handle */
3226 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003227
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003228 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
3229 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003230
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003231 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3232 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003233
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003234 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
3235 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003236
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003237 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003238 }
3239
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003240 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
3241 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003242
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003243 if (rc == NO_ERROR)
3244 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
3245
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003246 close(oldfd); /* And Close Saved STDIN Handle */
3247 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003248
Guido van Rossumd48f2521997-12-05 22:19:34 +00003249 } else {
3250 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003251 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003252 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003253}
3254
3255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003256posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003257{
3258 char *name;
3259 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003260 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003261 FILE *fp;
3262 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003263 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003264 return NULL;
3265 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003266 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003267 Py_END_ALLOW_THREADS
3268 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003269 return os2_error(err);
3270
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003271 f = PyFile_FromFile(fp, name, mode, fclose);
3272 if (f != NULL)
3273 PyFile_SetBufSize(f, bufsize);
3274 return f;
3275}
3276
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003277#elif defined(PYCC_GCC)
3278
3279/* standard posix version of popen() support */
3280static PyObject *
3281posix_popen(PyObject *self, PyObject *args)
3282{
3283 char *name;
3284 char *mode = "r";
3285 int bufsize = -1;
3286 FILE *fp;
3287 PyObject *f;
3288 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3289 return NULL;
3290 Py_BEGIN_ALLOW_THREADS
3291 fp = popen(name, mode);
3292 Py_END_ALLOW_THREADS
3293 if (fp == NULL)
3294 return posix_error();
3295 f = PyFile_FromFile(fp, name, mode, pclose);
3296 if (f != NULL)
3297 PyFile_SetBufSize(f, bufsize);
3298 return f;
3299}
3300
3301/* fork() under OS/2 has lots'o'warts
3302 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3303 * most of this code is a ripoff of the win32 code, but using the
3304 * capabilities of EMX's C library routines
3305 */
3306
3307/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3308#define POPEN_1 1
3309#define POPEN_2 2
3310#define POPEN_3 3
3311#define POPEN_4 4
3312
3313static PyObject *_PyPopen(char *, int, int, int);
3314static int _PyPclose(FILE *file);
3315
3316/*
3317 * Internal dictionary mapping popen* file pointers to process handles,
3318 * for use when retrieving the process exit code. See _PyPclose() below
3319 * for more information on this dictionary's use.
3320 */
3321static PyObject *_PyPopenProcs = NULL;
3322
3323/* os2emx version of popen2()
3324 *
3325 * The result of this function is a pipe (file) connected to the
3326 * process's stdin, and a pipe connected to the process's stdout.
3327 */
3328
3329static PyObject *
3330os2emx_popen2(PyObject *self, PyObject *args)
3331{
3332 PyObject *f;
3333 int tm=0;
3334
3335 char *cmdstring;
3336 char *mode = "t";
3337 int bufsize = -1;
3338 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3339 return NULL;
3340
3341 if (*mode == 't')
3342 tm = O_TEXT;
3343 else if (*mode != 'b') {
3344 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3345 return NULL;
3346 } else
3347 tm = O_BINARY;
3348
3349 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3350
3351 return f;
3352}
3353
3354/*
3355 * Variation on os2emx.popen2
3356 *
3357 * The result of this function is 3 pipes - the process's stdin,
3358 * stdout and stderr
3359 */
3360
3361static PyObject *
3362os2emx_popen3(PyObject *self, PyObject *args)
3363{
3364 PyObject *f;
3365 int tm = 0;
3366
3367 char *cmdstring;
3368 char *mode = "t";
3369 int bufsize = -1;
3370 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3371 return NULL;
3372
3373 if (*mode == 't')
3374 tm = O_TEXT;
3375 else if (*mode != 'b') {
3376 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3377 return NULL;
3378 } else
3379 tm = O_BINARY;
3380
3381 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3382
3383 return f;
3384}
3385
3386/*
3387 * Variation on os2emx.popen2
3388 *
Tim Peters11b23062003-04-23 02:39:17 +00003389 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003390 * and stdout+stderr combined as a single pipe.
3391 */
3392
3393static PyObject *
3394os2emx_popen4(PyObject *self, PyObject *args)
3395{
3396 PyObject *f;
3397 int tm = 0;
3398
3399 char *cmdstring;
3400 char *mode = "t";
3401 int bufsize = -1;
3402 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3403 return NULL;
3404
3405 if (*mode == 't')
3406 tm = O_TEXT;
3407 else if (*mode != 'b') {
3408 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3409 return NULL;
3410 } else
3411 tm = O_BINARY;
3412
3413 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3414
3415 return f;
3416}
3417
3418/* a couple of structures for convenient handling of multiple
3419 * file handles and pipes
3420 */
3421struct file_ref
3422{
3423 int handle;
3424 int flags;
3425};
3426
3427struct pipe_ref
3428{
3429 int rd;
3430 int wr;
3431};
3432
3433/* The following code is derived from the win32 code */
3434
3435static PyObject *
3436_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3437{
3438 struct file_ref stdio[3];
3439 struct pipe_ref p_fd[3];
3440 FILE *p_s[3];
3441 int file_count, i, pipe_err, pipe_pid;
3442 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3443 PyObject *f, *p_f[3];
3444
3445 /* file modes for subsequent fdopen's on pipe handles */
3446 if (mode == O_TEXT)
3447 {
3448 rd_mode = "rt";
3449 wr_mode = "wt";
3450 }
3451 else
3452 {
3453 rd_mode = "rb";
3454 wr_mode = "wb";
3455 }
3456
3457 /* prepare shell references */
3458 if ((shell = getenv("EMXSHELL")) == NULL)
3459 if ((shell = getenv("COMSPEC")) == NULL)
3460 {
3461 errno = ENOENT;
3462 return posix_error();
3463 }
3464
3465 sh_name = _getname(shell);
3466 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3467 opt = "/c";
3468 else
3469 opt = "-c";
3470
3471 /* save current stdio fds + their flags, and set not inheritable */
3472 i = pipe_err = 0;
3473 while (pipe_err >= 0 && i < 3)
3474 {
3475 pipe_err = stdio[i].handle = dup(i);
3476 stdio[i].flags = fcntl(i, F_GETFD, 0);
3477 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3478 i++;
3479 }
3480 if (pipe_err < 0)
3481 {
3482 /* didn't get them all saved - clean up and bail out */
3483 int saved_err = errno;
3484 while (i-- > 0)
3485 {
3486 close(stdio[i].handle);
3487 }
3488 errno = saved_err;
3489 return posix_error();
3490 }
3491
3492 /* create pipe ends */
3493 file_count = 2;
3494 if (n == POPEN_3)
3495 file_count = 3;
3496 i = pipe_err = 0;
3497 while ((pipe_err == 0) && (i < file_count))
3498 pipe_err = pipe((int *)&p_fd[i++]);
3499 if (pipe_err < 0)
3500 {
3501 /* didn't get them all made - clean up and bail out */
3502 while (i-- > 0)
3503 {
3504 close(p_fd[i].wr);
3505 close(p_fd[i].rd);
3506 }
3507 errno = EPIPE;
3508 return posix_error();
3509 }
3510
3511 /* change the actual standard IO streams over temporarily,
3512 * making the retained pipe ends non-inheritable
3513 */
3514 pipe_err = 0;
3515
3516 /* - stdin */
3517 if (dup2(p_fd[0].rd, 0) == 0)
3518 {
3519 close(p_fd[0].rd);
3520 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3521 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3522 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3523 {
3524 close(p_fd[0].wr);
3525 pipe_err = -1;
3526 }
3527 }
3528 else
3529 {
3530 pipe_err = -1;
3531 }
3532
3533 /* - stdout */
3534 if (pipe_err == 0)
3535 {
3536 if (dup2(p_fd[1].wr, 1) == 1)
3537 {
3538 close(p_fd[1].wr);
3539 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3540 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3541 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3542 {
3543 close(p_fd[1].rd);
3544 pipe_err = -1;
3545 }
3546 }
3547 else
3548 {
3549 pipe_err = -1;
3550 }
3551 }
3552
3553 /* - stderr, as required */
3554 if (pipe_err == 0)
3555 switch (n)
3556 {
3557 case POPEN_3:
3558 {
3559 if (dup2(p_fd[2].wr, 2) == 2)
3560 {
3561 close(p_fd[2].wr);
3562 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3563 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3564 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3565 {
3566 close(p_fd[2].rd);
3567 pipe_err = -1;
3568 }
3569 }
3570 else
3571 {
3572 pipe_err = -1;
3573 }
3574 break;
3575 }
3576
3577 case POPEN_4:
3578 {
3579 if (dup2(1, 2) != 2)
3580 {
3581 pipe_err = -1;
3582 }
3583 break;
3584 }
3585 }
3586
3587 /* spawn the child process */
3588 if (pipe_err == 0)
3589 {
3590 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3591 if (pipe_pid == -1)
3592 {
3593 pipe_err = -1;
3594 }
3595 else
3596 {
3597 /* save the PID into the FILE structure
3598 * NOTE: this implementation doesn't actually
3599 * take advantage of this, but do it for
3600 * completeness - AIM Apr01
3601 */
3602 for (i = 0; i < file_count; i++)
3603 p_s[i]->_pid = pipe_pid;
3604 }
3605 }
3606
3607 /* reset standard IO to normal */
3608 for (i = 0; i < 3; i++)
3609 {
3610 dup2(stdio[i].handle, i);
3611 fcntl(i, F_SETFD, stdio[i].flags);
3612 close(stdio[i].handle);
3613 }
3614
3615 /* if any remnant problems, clean up and bail out */
3616 if (pipe_err < 0)
3617 {
3618 for (i = 0; i < 3; i++)
3619 {
3620 close(p_fd[i].rd);
3621 close(p_fd[i].wr);
3622 }
3623 errno = EPIPE;
3624 return posix_error_with_filename(cmdstring);
3625 }
3626
3627 /* build tuple of file objects to return */
3628 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3629 PyFile_SetBufSize(p_f[0], bufsize);
3630 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3631 PyFile_SetBufSize(p_f[1], bufsize);
3632 if (n == POPEN_3)
3633 {
3634 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3635 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003636 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003637 }
3638 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003639 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003640
3641 /*
3642 * Insert the files we've created into the process dictionary
3643 * all referencing the list with the process handle and the
3644 * initial number of files (see description below in _PyPclose).
3645 * Since if _PyPclose later tried to wait on a process when all
3646 * handles weren't closed, it could create a deadlock with the
3647 * child, we spend some energy here to try to ensure that we
3648 * either insert all file handles into the dictionary or none
3649 * at all. It's a little clumsy with the various popen modes
3650 * and variable number of files involved.
3651 */
3652 if (!_PyPopenProcs)
3653 {
3654 _PyPopenProcs = PyDict_New();
3655 }
3656
3657 if (_PyPopenProcs)
3658 {
3659 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3660 int ins_rc[3];
3661
3662 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3663 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3664
3665 procObj = PyList_New(2);
3666 pidObj = PyInt_FromLong((long) pipe_pid);
3667 intObj = PyInt_FromLong((long) file_count);
3668
3669 if (procObj && pidObj && intObj)
3670 {
3671 PyList_SetItem(procObj, 0, pidObj);
3672 PyList_SetItem(procObj, 1, intObj);
3673
3674 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3675 if (fileObj[0])
3676 {
3677 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3678 fileObj[0],
3679 procObj);
3680 }
3681 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3682 if (fileObj[1])
3683 {
3684 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3685 fileObj[1],
3686 procObj);
3687 }
3688 if (file_count >= 3)
3689 {
3690 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3691 if (fileObj[2])
3692 {
3693 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3694 fileObj[2],
3695 procObj);
3696 }
3697 }
3698
3699 if (ins_rc[0] < 0 || !fileObj[0] ||
3700 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3701 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3702 {
3703 /* Something failed - remove any dictionary
3704 * entries that did make it.
3705 */
3706 if (!ins_rc[0] && fileObj[0])
3707 {
3708 PyDict_DelItem(_PyPopenProcs,
3709 fileObj[0]);
3710 }
3711 if (!ins_rc[1] && fileObj[1])
3712 {
3713 PyDict_DelItem(_PyPopenProcs,
3714 fileObj[1]);
3715 }
3716 if (!ins_rc[2] && fileObj[2])
3717 {
3718 PyDict_DelItem(_PyPopenProcs,
3719 fileObj[2]);
3720 }
3721 }
3722 }
Tim Peters11b23062003-04-23 02:39:17 +00003723
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003724 /*
3725 * Clean up our localized references for the dictionary keys
3726 * and value since PyDict_SetItem will Py_INCREF any copies
3727 * that got placed in the dictionary.
3728 */
3729 Py_XDECREF(procObj);
3730 Py_XDECREF(fileObj[0]);
3731 Py_XDECREF(fileObj[1]);
3732 Py_XDECREF(fileObj[2]);
3733 }
3734
3735 /* Child is launched. */
3736 return f;
3737}
3738
3739/*
3740 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3741 * exit code for the child process and return as a result of the close.
3742 *
3743 * This function uses the _PyPopenProcs dictionary in order to map the
3744 * input file pointer to information about the process that was
3745 * originally created by the popen* call that created the file pointer.
3746 * The dictionary uses the file pointer as a key (with one entry
3747 * inserted for each file returned by the original popen* call) and a
3748 * single list object as the value for all files from a single call.
3749 * The list object contains the Win32 process handle at [0], and a file
3750 * count at [1], which is initialized to the total number of file
3751 * handles using that list.
3752 *
3753 * This function closes whichever handle it is passed, and decrements
3754 * the file count in the dictionary for the process handle pointed to
3755 * by this file. On the last close (when the file count reaches zero),
3756 * this function will wait for the child process and then return its
3757 * exit code as the result of the close() operation. This permits the
3758 * files to be closed in any order - it is always the close() of the
3759 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003760 *
3761 * NOTE: This function is currently called with the GIL released.
3762 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003763 */
3764
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003765static int _PyPclose(FILE *file)
3766{
3767 int result;
3768 int exit_code;
3769 int pipe_pid;
3770 PyObject *procObj, *pidObj, *intObj, *fileObj;
3771 int file_count;
3772#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003773 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003774#endif
3775
3776 /* Close the file handle first, to ensure it can't block the
3777 * child from exiting if it's the last handle.
3778 */
3779 result = fclose(file);
3780
3781#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003782 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003783#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003784 if (_PyPopenProcs)
3785 {
3786 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3787 (procObj = PyDict_GetItem(_PyPopenProcs,
3788 fileObj)) != NULL &&
3789 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3790 (intObj = PyList_GetItem(procObj,1)) != NULL)
3791 {
3792 pipe_pid = (int) PyInt_AsLong(pidObj);
3793 file_count = (int) PyInt_AsLong(intObj);
3794
3795 if (file_count > 1)
3796 {
3797 /* Still other files referencing process */
3798 file_count--;
3799 PyList_SetItem(procObj,1,
3800 PyInt_FromLong((long) file_count));
3801 }
3802 else
3803 {
3804 /* Last file for this process */
3805 if (result != EOF &&
3806 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3807 {
3808 /* extract exit status */
3809 if (WIFEXITED(exit_code))
3810 {
3811 result = WEXITSTATUS(exit_code);
3812 }
3813 else
3814 {
3815 errno = EPIPE;
3816 result = -1;
3817 }
3818 }
3819 else
3820 {
3821 /* Indicate failure - this will cause the file object
3822 * to raise an I/O error and translate the last
3823 * error code from errno. We do have a problem with
3824 * last errors that overlap the normal errno table,
3825 * but that's a consistent problem with the file object.
3826 */
3827 result = -1;
3828 }
3829 }
3830
3831 /* Remove this file pointer from dictionary */
3832 PyDict_DelItem(_PyPopenProcs, fileObj);
3833
3834 if (PyDict_Size(_PyPopenProcs) == 0)
3835 {
3836 Py_DECREF(_PyPopenProcs);
3837 _PyPopenProcs = NULL;
3838 }
3839
3840 } /* if object retrieval ok */
3841
3842 Py_XDECREF(fileObj);
3843 } /* if _PyPopenProcs */
3844
3845#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003846 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003847#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003848 return result;
3849}
3850
3851#endif /* PYCC_??? */
3852
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003853#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003854
3855/*
3856 * Portable 'popen' replacement for Win32.
3857 *
3858 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3859 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003860 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003861 */
3862
3863#include <malloc.h>
3864#include <io.h>
3865#include <fcntl.h>
3866
3867/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3868#define POPEN_1 1
3869#define POPEN_2 2
3870#define POPEN_3 3
3871#define POPEN_4 4
3872
3873static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003874static int _PyPclose(FILE *file);
3875
3876/*
3877 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003878 * for use when retrieving the process exit code. See _PyPclose() below
3879 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003880 */
3881static PyObject *_PyPopenProcs = NULL;
3882
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003883
3884/* popen that works from a GUI.
3885 *
3886 * The result of this function is a pipe (file) connected to the
3887 * processes stdin or stdout, depending on the requested mode.
3888 */
3889
3890static PyObject *
3891posix_popen(PyObject *self, PyObject *args)
3892{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003893 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003894 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003895
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003896 char *cmdstring;
3897 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003898 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003899 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003900 return NULL;
3901
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003902 if (*mode == 'r')
3903 tm = _O_RDONLY;
3904 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003905 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003906 return NULL;
3907 } else
3908 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003909
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003910 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003911 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003912 return NULL;
3913 }
3914
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003915 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003916 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003917 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003918 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003919 else
3920 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
3921
3922 return f;
3923}
3924
3925/* Variation on win32pipe.popen
3926 *
3927 * The result of this function is a pipe (file) connected to the
3928 * process's stdin, and a pipe connected to the process's stdout.
3929 */
3930
3931static PyObject *
3932win32_popen2(PyObject *self, PyObject *args)
3933{
3934 PyObject *f;
3935 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00003936
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003937 char *cmdstring;
3938 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003939 int bufsize = -1;
3940 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003941 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003942
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003943 if (*mode == 't')
3944 tm = _O_TEXT;
3945 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003946 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003947 return NULL;
3948 } else
3949 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003950
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003951 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003952 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003953 return NULL;
3954 }
3955
3956 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00003957
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003958 return f;
3959}
3960
3961/*
3962 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003963 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003964 * The result of this function is 3 pipes - the process's stdin,
3965 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003966 */
3967
3968static PyObject *
3969win32_popen3(PyObject *self, PyObject *args)
3970{
3971 PyObject *f;
3972 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003973
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003974 char *cmdstring;
3975 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003976 int bufsize = -1;
3977 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003978 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003979
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003980 if (*mode == 't')
3981 tm = _O_TEXT;
3982 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003983 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003984 return NULL;
3985 } else
3986 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003987
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003988 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003989 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003990 return NULL;
3991 }
3992
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003993 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00003994
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003995 return f;
3996}
3997
3998/*
3999 * Variation on win32pipe.popen
4000 *
Tim Peters5aa91602002-01-30 05:46:57 +00004001 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004002 * and stdout+stderr combined as a single pipe.
4003 */
4004
4005static PyObject *
4006win32_popen4(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:popen4", &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, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004021 return NULL;
4022 } else
4023 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004024
4025 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004026 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004027 return NULL;
4028 }
4029
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004030 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004031
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004032 return f;
4033}
4034
Mark Hammond08501372001-01-31 07:30:29 +00004035static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004036_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004037 HANDLE hStdin,
4038 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004039 HANDLE hStderr,
4040 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004041{
4042 PROCESS_INFORMATION piProcInfo;
4043 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004044 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004045 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004046 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004047 int i;
4048 int x;
4049
4050 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004051 char *comshell;
4052
Tim Peters92e4dd82002-10-05 01:47:34 +00004053 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004054 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
4055 return x;
Tim Peters402d5982001-08-27 06:37:48 +00004056
4057 /* Explicitly check if we are using COMMAND.COM. If we are
4058 * then use the w9xpopen hack.
4059 */
4060 comshell = s1 + x;
4061 while (comshell >= s1 && *comshell != '\\')
4062 --comshell;
4063 ++comshell;
4064
4065 if (GetVersion() < 0x80000000 &&
4066 _stricmp(comshell, "command.com") != 0) {
4067 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004068 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004069 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004070 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004071 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004072 }
4073 else {
4074 /*
Tim Peters402d5982001-08-27 06:37:48 +00004075 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4076 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004077 */
Mark Hammond08501372001-01-31 07:30:29 +00004078 char modulepath[_MAX_PATH];
4079 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004080 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4081 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004082 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004083 x = i+1;
4084 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004085 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004086 strncat(modulepath,
4087 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004088 (sizeof(modulepath)/sizeof(modulepath[0]))
4089 -strlen(modulepath));
4090 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004091 /* Eeek - file-not-found - possibly an embedding
4092 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004093 */
Tim Peters5aa91602002-01-30 05:46:57 +00004094 strncpy(modulepath,
4095 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004096 sizeof(modulepath)/sizeof(modulepath[0]));
4097 if (modulepath[strlen(modulepath)-1] != '\\')
4098 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004099 strncat(modulepath,
4100 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004101 (sizeof(modulepath)/sizeof(modulepath[0]))
4102 -strlen(modulepath));
4103 /* No where else to look - raise an easily identifiable
4104 error, rather than leaving Windows to report
4105 "file not found" - as the user is probably blissfully
4106 unaware this shim EXE is used, and it will confuse them.
4107 (well, it confused me for a while ;-)
4108 */
4109 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004110 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004111 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004112 "for popen to work with your shell "
4113 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004114 szConsoleSpawn);
4115 return FALSE;
4116 }
4117 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004118 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004119 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004120 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004121
Tim Peters92e4dd82002-10-05 01:47:34 +00004122 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004123 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004124 /* To maintain correct argument passing semantics,
4125 we pass the command-line as it stands, and allow
4126 quoting to be applied. w9xpopen.exe will then
4127 use its argv vector, and re-quote the necessary
4128 args for the ultimate child process.
4129 */
Tim Peters75cdad52001-11-28 22:07:30 +00004130 PyOS_snprintf(
4131 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004132 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004133 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004134 s1,
4135 s3,
4136 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004137 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004138 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004139 dialog:
4140 "Your program accessed mem currently in use at xxx"
4141 and a hopeful warning about the stability of your
4142 system.
4143 Cost is Ctrl+C wont kill children, but anyone
4144 who cares can have a go!
4145 */
4146 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004147 }
4148 }
4149
4150 /* Could be an else here to try cmd.exe / command.com in the path
4151 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004152 else {
Tim Peters402d5982001-08-27 06:37:48 +00004153 PyErr_SetString(PyExc_RuntimeError,
4154 "Cannot locate a COMSPEC environment variable to "
4155 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004156 return FALSE;
4157 }
Tim Peters5aa91602002-01-30 05:46:57 +00004158
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004159 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4160 siStartInfo.cb = sizeof(STARTUPINFO);
4161 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4162 siStartInfo.hStdInput = hStdin;
4163 siStartInfo.hStdOutput = hStdout;
4164 siStartInfo.hStdError = hStderr;
4165 siStartInfo.wShowWindow = SW_HIDE;
4166
4167 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004168 s2,
4169 NULL,
4170 NULL,
4171 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004172 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004173 NULL,
4174 NULL,
4175 &siStartInfo,
4176 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004177 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004178 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004179
Mark Hammondb37a3732000-08-14 04:47:33 +00004180 /* Return process handle */
4181 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004182 return TRUE;
4183 }
Tim Peters402d5982001-08-27 06:37:48 +00004184 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004185 return FALSE;
4186}
4187
4188/* The following code is based off of KB: Q190351 */
4189
4190static PyObject *
4191_PyPopen(char *cmdstring, int mode, int n)
4192{
4193 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4194 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004195 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004196
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004197 SECURITY_ATTRIBUTES saAttr;
4198 BOOL fSuccess;
4199 int fd1, fd2, fd3;
4200 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004201 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004202 PyObject *f;
4203
4204 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4205 saAttr.bInheritHandle = TRUE;
4206 saAttr.lpSecurityDescriptor = NULL;
4207
4208 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4209 return win32_error("CreatePipe", NULL);
4210
4211 /* Create new output read handle and the input write handle. Set
4212 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004213 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004214 * being created. */
4215 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004216 GetCurrentProcess(), &hChildStdinWrDup, 0,
4217 FALSE,
4218 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004219 if (!fSuccess)
4220 return win32_error("DuplicateHandle", NULL);
4221
4222 /* Close the inheritable version of ChildStdin
4223 that we're using. */
4224 CloseHandle(hChildStdinWr);
4225
4226 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4227 return win32_error("CreatePipe", NULL);
4228
4229 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004230 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4231 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004232 if (!fSuccess)
4233 return win32_error("DuplicateHandle", NULL);
4234
4235 /* Close the inheritable version of ChildStdout
4236 that we're using. */
4237 CloseHandle(hChildStdoutRd);
4238
4239 if (n != POPEN_4) {
4240 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4241 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004242 fSuccess = DuplicateHandle(GetCurrentProcess(),
4243 hChildStderrRd,
4244 GetCurrentProcess(),
4245 &hChildStderrRdDup, 0,
4246 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004247 if (!fSuccess)
4248 return win32_error("DuplicateHandle", NULL);
4249 /* Close the inheritable version of ChildStdErr that we're using. */
4250 CloseHandle(hChildStderrRd);
4251 }
Tim Peters5aa91602002-01-30 05:46:57 +00004252
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004253 switch (n) {
4254 case POPEN_1:
4255 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4256 case _O_WRONLY | _O_TEXT:
4257 /* Case for writing to child Stdin in text mode. */
4258 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4259 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004260 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004261 PyFile_SetBufSize(f, 0);
4262 /* We don't care about these pipes anymore, so close them. */
4263 CloseHandle(hChildStdoutRdDup);
4264 CloseHandle(hChildStderrRdDup);
4265 break;
4266
4267 case _O_RDONLY | _O_TEXT:
4268 /* Case for reading from child Stdout in text mode. */
4269 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4270 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004271 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004272 PyFile_SetBufSize(f, 0);
4273 /* We don't care about these pipes anymore, so close them. */
4274 CloseHandle(hChildStdinWrDup);
4275 CloseHandle(hChildStderrRdDup);
4276 break;
4277
4278 case _O_RDONLY | _O_BINARY:
4279 /* Case for readinig from child Stdout in binary mode. */
4280 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4281 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004282 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004283 PyFile_SetBufSize(f, 0);
4284 /* We don't care about these pipes anymore, so close them. */
4285 CloseHandle(hChildStdinWrDup);
4286 CloseHandle(hChildStderrRdDup);
4287 break;
4288
4289 case _O_WRONLY | _O_BINARY:
4290 /* Case for writing to child Stdin in binary mode. */
4291 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4292 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004293 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004294 PyFile_SetBufSize(f, 0);
4295 /* We don't care about these pipes anymore, so close them. */
4296 CloseHandle(hChildStdoutRdDup);
4297 CloseHandle(hChildStderrRdDup);
4298 break;
4299 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004300 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004301 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004302
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004303 case POPEN_2:
4304 case POPEN_4:
4305 {
4306 char *m1, *m2;
4307 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004308
Tim Peters7dca21e2002-08-19 00:42:29 +00004309 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004310 m1 = "r";
4311 m2 = "w";
4312 } else {
4313 m1 = "rb";
4314 m2 = "wb";
4315 }
4316
4317 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4318 f1 = _fdopen(fd1, m2);
4319 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4320 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004321 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004322 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004323 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004324 PyFile_SetBufSize(p2, 0);
4325
4326 if (n != 4)
4327 CloseHandle(hChildStderrRdDup);
4328
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004329 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004330 Py_XDECREF(p1);
4331 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004332 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004333 break;
4334 }
Tim Peters5aa91602002-01-30 05:46:57 +00004335
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004336 case POPEN_3:
4337 {
4338 char *m1, *m2;
4339 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004340
Tim Peters7dca21e2002-08-19 00:42:29 +00004341 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004342 m1 = "r";
4343 m2 = "w";
4344 } else {
4345 m1 = "rb";
4346 m2 = "wb";
4347 }
4348
4349 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4350 f1 = _fdopen(fd1, m2);
4351 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4352 f2 = _fdopen(fd2, m1);
4353 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4354 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004355 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004356 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4357 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004358 PyFile_SetBufSize(p1, 0);
4359 PyFile_SetBufSize(p2, 0);
4360 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004361 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004362 Py_XDECREF(p1);
4363 Py_XDECREF(p2);
4364 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004365 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004366 break;
4367 }
4368 }
4369
4370 if (n == POPEN_4) {
4371 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004372 hChildStdinRd,
4373 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004374 hChildStdoutWr,
4375 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004376 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004377 }
4378 else {
4379 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004380 hChildStdinRd,
4381 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004382 hChildStderrWr,
4383 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004384 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004385 }
4386
Mark Hammondb37a3732000-08-14 04:47:33 +00004387 /*
4388 * Insert the files we've created into the process dictionary
4389 * all referencing the list with the process handle and the
4390 * initial number of files (see description below in _PyPclose).
4391 * Since if _PyPclose later tried to wait on a process when all
4392 * handles weren't closed, it could create a deadlock with the
4393 * child, we spend some energy here to try to ensure that we
4394 * either insert all file handles into the dictionary or none
4395 * at all. It's a little clumsy with the various popen modes
4396 * and variable number of files involved.
4397 */
4398 if (!_PyPopenProcs) {
4399 _PyPopenProcs = PyDict_New();
4400 }
4401
4402 if (_PyPopenProcs) {
4403 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4404 int ins_rc[3];
4405
4406 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4407 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4408
4409 procObj = PyList_New(2);
4410 hProcessObj = PyLong_FromVoidPtr(hProcess);
4411 intObj = PyInt_FromLong(file_count);
4412
4413 if (procObj && hProcessObj && intObj) {
4414 PyList_SetItem(procObj,0,hProcessObj);
4415 PyList_SetItem(procObj,1,intObj);
4416
4417 fileObj[0] = PyLong_FromVoidPtr(f1);
4418 if (fileObj[0]) {
4419 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4420 fileObj[0],
4421 procObj);
4422 }
4423 if (file_count >= 2) {
4424 fileObj[1] = PyLong_FromVoidPtr(f2);
4425 if (fileObj[1]) {
4426 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4427 fileObj[1],
4428 procObj);
4429 }
4430 }
4431 if (file_count >= 3) {
4432 fileObj[2] = PyLong_FromVoidPtr(f3);
4433 if (fileObj[2]) {
4434 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4435 fileObj[2],
4436 procObj);
4437 }
4438 }
4439
4440 if (ins_rc[0] < 0 || !fileObj[0] ||
4441 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4442 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4443 /* Something failed - remove any dictionary
4444 * entries that did make it.
4445 */
4446 if (!ins_rc[0] && fileObj[0]) {
4447 PyDict_DelItem(_PyPopenProcs,
4448 fileObj[0]);
4449 }
4450 if (!ins_rc[1] && fileObj[1]) {
4451 PyDict_DelItem(_PyPopenProcs,
4452 fileObj[1]);
4453 }
4454 if (!ins_rc[2] && fileObj[2]) {
4455 PyDict_DelItem(_PyPopenProcs,
4456 fileObj[2]);
4457 }
4458 }
4459 }
Tim Peters5aa91602002-01-30 05:46:57 +00004460
Mark Hammondb37a3732000-08-14 04:47:33 +00004461 /*
4462 * Clean up our localized references for the dictionary keys
4463 * and value since PyDict_SetItem will Py_INCREF any copies
4464 * that got placed in the dictionary.
4465 */
4466 Py_XDECREF(procObj);
4467 Py_XDECREF(fileObj[0]);
4468 Py_XDECREF(fileObj[1]);
4469 Py_XDECREF(fileObj[2]);
4470 }
4471
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004472 /* Child is launched. Close the parents copy of those pipe
4473 * handles that only the child should have open. You need to
4474 * make sure that no handles to the write end of the output pipe
4475 * are maintained in this process or else the pipe will not close
4476 * when the child process exits and the ReadFile will hang. */
4477
4478 if (!CloseHandle(hChildStdinRd))
4479 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004480
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004481 if (!CloseHandle(hChildStdoutWr))
4482 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004483
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004484 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4485 return win32_error("CloseHandle", NULL);
4486
4487 return f;
4488}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004489
4490/*
4491 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4492 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004493 *
4494 * This function uses the _PyPopenProcs dictionary in order to map the
4495 * input file pointer to information about the process that was
4496 * originally created by the popen* call that created the file pointer.
4497 * The dictionary uses the file pointer as a key (with one entry
4498 * inserted for each file returned by the original popen* call) and a
4499 * single list object as the value for all files from a single call.
4500 * The list object contains the Win32 process handle at [0], and a file
4501 * count at [1], which is initialized to the total number of file
4502 * handles using that list.
4503 *
4504 * This function closes whichever handle it is passed, and decrements
4505 * the file count in the dictionary for the process handle pointed to
4506 * by this file. On the last close (when the file count reaches zero),
4507 * this function will wait for the child process and then return its
4508 * exit code as the result of the close() operation. This permits the
4509 * files to be closed in any order - it is always the close() of the
4510 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004511 *
4512 * NOTE: This function is currently called with the GIL released.
4513 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004514 */
Tim Peters736aa322000-09-01 06:51:24 +00004515
Fredrik Lundh56055a42000-07-23 19:47:12 +00004516static int _PyPclose(FILE *file)
4517{
Fredrik Lundh20318932000-07-26 17:29:12 +00004518 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004519 DWORD exit_code;
4520 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004521 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4522 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004523#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004524 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004525#endif
4526
Fredrik Lundh20318932000-07-26 17:29:12 +00004527 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004528 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004529 */
4530 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004531#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004532 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004533#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004534 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004535 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4536 (procObj = PyDict_GetItem(_PyPopenProcs,
4537 fileObj)) != NULL &&
4538 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4539 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4540
4541 hProcess = PyLong_AsVoidPtr(hProcessObj);
4542 file_count = PyInt_AsLong(intObj);
4543
4544 if (file_count > 1) {
4545 /* Still other files referencing process */
4546 file_count--;
4547 PyList_SetItem(procObj,1,
4548 PyInt_FromLong(file_count));
4549 } else {
4550 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004551 if (result != EOF &&
4552 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4553 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004554 /* Possible truncation here in 16-bit environments, but
4555 * real exit codes are just the lower byte in any event.
4556 */
4557 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004558 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004559 /* Indicate failure - this will cause the file object
4560 * to raise an I/O error and translate the last Win32
4561 * error code from errno. We do have a problem with
4562 * last errors that overlap the normal errno table,
4563 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004564 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004565 if (result != EOF) {
4566 /* If the error wasn't from the fclose(), then
4567 * set errno for the file object error handling.
4568 */
4569 errno = GetLastError();
4570 }
4571 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004572 }
4573
4574 /* Free up the native handle at this point */
4575 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004576 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004577
Mark Hammondb37a3732000-08-14 04:47:33 +00004578 /* Remove this file pointer from dictionary */
4579 PyDict_DelItem(_PyPopenProcs, fileObj);
4580
4581 if (PyDict_Size(_PyPopenProcs) == 0) {
4582 Py_DECREF(_PyPopenProcs);
4583 _PyPopenProcs = NULL;
4584 }
4585
4586 } /* if object retrieval ok */
4587
4588 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004589 } /* if _PyPopenProcs */
4590
Tim Peters736aa322000-09-01 06:51:24 +00004591#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004592 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004593#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004594 return result;
4595}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004596
4597#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004598static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004599posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004600{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004601 char *name;
4602 char *mode = "r";
4603 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004604 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004605 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004606 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004607 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004608 /* Strip mode of binary or text modifiers */
4609 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4610 mode = "r";
4611 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4612 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004613 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004614 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004615 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004616 if (fp == NULL)
4617 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004618 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004619 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004620 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004621 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004622}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004623
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004624#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004625#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004627
Guido van Rossumb6775db1994-08-01 11:34:53 +00004628#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004629PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004630"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631Set the current process's user id.");
4632
Barry Warsaw53699e91996-12-10 23:23:01 +00004633static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004634posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004635{
4636 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004637 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004638 return NULL;
4639 if (setuid(uid) < 0)
4640 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004641 Py_INCREF(Py_None);
4642 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004643}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004644#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004646
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004647#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004648PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004649"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004650Set the current process's effective user id.");
4651
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004652static PyObject *
4653posix_seteuid (PyObject *self, PyObject *args)
4654{
4655 int euid;
4656 if (!PyArg_ParseTuple(args, "i", &euid)) {
4657 return NULL;
4658 } else if (seteuid(euid) < 0) {
4659 return posix_error();
4660 } else {
4661 Py_INCREF(Py_None);
4662 return Py_None;
4663 }
4664}
4665#endif /* HAVE_SETEUID */
4666
4667#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004668PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004669"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004670Set the current process's effective group id.");
4671
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004672static PyObject *
4673posix_setegid (PyObject *self, PyObject *args)
4674{
4675 int egid;
4676 if (!PyArg_ParseTuple(args, "i", &egid)) {
4677 return NULL;
4678 } else if (setegid(egid) < 0) {
4679 return posix_error();
4680 } else {
4681 Py_INCREF(Py_None);
4682 return Py_None;
4683 }
4684}
4685#endif /* HAVE_SETEGID */
4686
4687#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004688PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004689"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004690Set the current process's real and effective user ids.");
4691
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004692static PyObject *
4693posix_setreuid (PyObject *self, PyObject *args)
4694{
4695 int ruid, euid;
4696 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4697 return NULL;
4698 } else if (setreuid(ruid, euid) < 0) {
4699 return posix_error();
4700 } else {
4701 Py_INCREF(Py_None);
4702 return Py_None;
4703 }
4704}
4705#endif /* HAVE_SETREUID */
4706
4707#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004708PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004709"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004710Set the current process's real and effective group ids.");
4711
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004712static PyObject *
4713posix_setregid (PyObject *self, PyObject *args)
4714{
4715 int rgid, egid;
4716 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4717 return NULL;
4718 } else if (setregid(rgid, egid) < 0) {
4719 return posix_error();
4720 } else {
4721 Py_INCREF(Py_None);
4722 return Py_None;
4723 }
4724}
4725#endif /* HAVE_SETREGID */
4726
Guido van Rossumb6775db1994-08-01 11:34:53 +00004727#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004728PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004729"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004730Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004731
Barry Warsaw53699e91996-12-10 23:23:01 +00004732static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004733posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004734{
4735 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004736 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004737 return NULL;
4738 if (setgid(gid) < 0)
4739 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004740 Py_INCREF(Py_None);
4741 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004742}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004743#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004744
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004745#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004746PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004747"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004748Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004749
4750static PyObject *
4751posix_setgroups(PyObject *self, PyObject *args)
4752{
4753 PyObject *groups;
4754 int i, len;
4755 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004756
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004757 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4758 return NULL;
4759 if (!PySequence_Check(groups)) {
4760 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4761 return NULL;
4762 }
4763 len = PySequence_Size(groups);
4764 if (len > MAX_GROUPS) {
4765 PyErr_SetString(PyExc_ValueError, "too many groups");
4766 return NULL;
4767 }
4768 for(i = 0; i < len; i++) {
4769 PyObject *elem;
4770 elem = PySequence_GetItem(groups, i);
4771 if (!elem)
4772 return NULL;
4773 if (!PyInt_Check(elem)) {
4774 PyErr_SetString(PyExc_TypeError,
4775 "groups must be integers");
4776 Py_DECREF(elem);
4777 return NULL;
4778 }
4779 /* XXX: check that value fits into gid_t. */
4780 grouplist[i] = PyInt_AsLong(elem);
4781 Py_DECREF(elem);
4782 }
4783
4784 if (setgroups(len, grouplist) < 0)
4785 return posix_error();
4786 Py_INCREF(Py_None);
4787 return Py_None;
4788}
4789#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004790
Guido van Rossumb6775db1994-08-01 11:34:53 +00004791#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004792PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004793"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004794Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004795
Barry Warsaw53699e91996-12-10 23:23:01 +00004796static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004797posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004798{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004799 int pid, options;
4800#ifdef UNION_WAIT
4801 union wait status;
4802#define status_i (status.w_status)
4803#else
4804 int status;
4805#define status_i status
4806#endif
4807 status_i = 0;
4808
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004809 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004810 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004811 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004812 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004813 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004814 if (pid == -1)
4815 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004816 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004817 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004818}
4819
Tim Petersab034fa2002-02-01 11:27:43 +00004820#elif defined(HAVE_CWAIT)
4821
4822/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004823PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004824"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004825"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004826
4827static PyObject *
4828posix_waitpid(PyObject *self, PyObject *args)
4829{
4830 int pid, options;
4831 int status;
4832
4833 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4834 return NULL;
4835 Py_BEGIN_ALLOW_THREADS
4836 pid = _cwait(&status, pid, options);
4837 Py_END_ALLOW_THREADS
4838 if (pid == -1)
4839 return posix_error();
4840 else
4841 /* shift the status left a byte so this is more like the
4842 POSIX waitpid */
4843 return Py_BuildValue("ii", pid, status << 8);
4844}
4845#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004846
Guido van Rossumad0ee831995-03-01 10:34:45 +00004847#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004848PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004849"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004850Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004851
Barry Warsaw53699e91996-12-10 23:23:01 +00004852static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004853posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004854{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004855 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004856#ifdef UNION_WAIT
4857 union wait status;
4858#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004859#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004860 int status;
4861#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004862#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004863
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004864 status_i = 0;
4865 Py_BEGIN_ALLOW_THREADS
4866 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004867 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004868 if (pid == -1)
4869 return posix_error();
4870 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004871 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004872#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004873}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004874#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004875
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004877PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004878"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004879Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004880
Barry Warsaw53699e91996-12-10 23:23:01 +00004881static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004882posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004883{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004884#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004885 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004886#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004887#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004888 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004889#else
4890 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4891#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004892#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004893}
4894
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004895
Guido van Rossumb6775db1994-08-01 11:34:53 +00004896#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004897PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004898"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004900
Barry Warsaw53699e91996-12-10 23:23:01 +00004901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004902posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004903{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004904 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004905 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004906 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004907 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004908 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004909 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004910 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004911 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004912 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004913 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004914 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004915}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004916#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004917
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004918
Guido van Rossumb6775db1994-08-01 11:34:53 +00004919#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004920PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004921"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004922Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004923
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004924static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004925posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004926{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004927 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004928}
4929#endif /* HAVE_SYMLINK */
4930
4931
4932#ifdef HAVE_TIMES
4933#ifndef HZ
4934#define HZ 60 /* Universal constant :-) */
4935#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004936
Guido van Rossumd48f2521997-12-05 22:19:34 +00004937#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4938static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004939system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004940{
4941 ULONG value = 0;
4942
4943 Py_BEGIN_ALLOW_THREADS
4944 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4945 Py_END_ALLOW_THREADS
4946
4947 return value;
4948}
4949
4950static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004951posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004952{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004953 /* Currently Only Uptime is Provided -- Others Later */
4954 return Py_BuildValue("ddddd",
4955 (double)0 /* t.tms_utime / HZ */,
4956 (double)0 /* t.tms_stime / HZ */,
4957 (double)0 /* t.tms_cutime / HZ */,
4958 (double)0 /* t.tms_cstime / HZ */,
4959 (double)system_uptime() / 1000);
4960}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004961#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004962static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004963posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004964{
4965 struct tms t;
4966 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004967 errno = 0;
4968 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004969 if (c == (clock_t) -1)
4970 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004971 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004972 (double)t.tms_utime / HZ,
4973 (double)t.tms_stime / HZ,
4974 (double)t.tms_cutime / HZ,
4975 (double)t.tms_cstime / HZ,
4976 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004977}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004978#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004979#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004980
4981
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004982#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004983#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004984static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004985posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004986{
4987 FILETIME create, exit, kernel, user;
4988 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004989 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004990 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4991 /* The fields of a FILETIME structure are the hi and lo part
4992 of a 64-bit value expressed in 100 nanosecond units.
4993 1e7 is one second in such units; 1e-7 the inverse.
4994 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4995 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004996 return Py_BuildValue(
4997 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004998 (double)(kernel.dwHighDateTime*429.4967296 +
4999 kernel.dwLowDateTime*1e-7),
5000 (double)(user.dwHighDateTime*429.4967296 +
5001 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005002 (double)0,
5003 (double)0,
5004 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005005}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005006#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005007
5008#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005009PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005010"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005011Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005012#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005014
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005015#ifdef HAVE_GETSID
5016PyDoc_STRVAR(posix_getsid__doc__,
5017"getsid(pid) -> sid\n\n\
5018Call the system call getsid().");
5019
5020static PyObject *
5021posix_getsid(PyObject *self, PyObject *args)
5022{
5023 int pid, sid;
5024 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5025 return NULL;
5026 sid = getsid(pid);
5027 if (sid < 0)
5028 return posix_error();
5029 return PyInt_FromLong((long)sid);
5030}
5031#endif /* HAVE_GETSID */
5032
5033
Guido van Rossumb6775db1994-08-01 11:34:53 +00005034#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005035PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005036"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005037Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005038
Barry Warsaw53699e91996-12-10 23:23:01 +00005039static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005040posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005041{
Guido van Rossum687dd131993-05-17 08:34:16 +00005042 if (setsid() < 0)
5043 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005044 Py_INCREF(Py_None);
5045 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005046}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005047#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005048
Guido van Rossumb6775db1994-08-01 11:34:53 +00005049#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005050PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005051"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005052Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005053
Barry Warsaw53699e91996-12-10 23:23:01 +00005054static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005055posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005056{
5057 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005058 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005059 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005060 if (setpgid(pid, pgrp) < 0)
5061 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005062 Py_INCREF(Py_None);
5063 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005064}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005065#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005067
Guido van Rossumb6775db1994-08-01 11:34:53 +00005068#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005069PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005070"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005071Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005072
Barry Warsaw53699e91996-12-10 23:23:01 +00005073static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005074posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005075{
5076 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005077 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005078 return NULL;
5079 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005080 if (pgid < 0)
5081 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005082 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005083}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005084#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005085
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005086
Guido van Rossumb6775db1994-08-01 11:34:53 +00005087#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005088PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005089"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005090Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005091
Barry Warsaw53699e91996-12-10 23:23:01 +00005092static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005093posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005094{
5095 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005096 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005097 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005098 if (tcsetpgrp(fd, pgid) < 0)
5099 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005100 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005101 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005102}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005103#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005104
Guido van Rossum687dd131993-05-17 08:34:16 +00005105/* Functions acting on file descriptors */
5106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005107PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005108"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005109Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005110
Barry Warsaw53699e91996-12-10 23:23:01 +00005111static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005112posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005113{
Mark Hammondef8b6542001-05-13 08:04:26 +00005114 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005115 int flag;
5116 int mode = 0777;
5117 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005118
5119#ifdef MS_WINDOWS
5120 if (unicode_file_names()) {
5121 PyUnicodeObject *po;
5122 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5123 Py_BEGIN_ALLOW_THREADS
5124 /* PyUnicode_AS_UNICODE OK without thread
5125 lock as it is a simple dereference. */
5126 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5127 Py_END_ALLOW_THREADS
5128 if (fd < 0)
5129 return posix_error();
5130 return PyInt_FromLong((long)fd);
5131 }
5132 /* Drop the argument parsing error as narrow strings
5133 are also valid. */
5134 PyErr_Clear();
5135 }
5136#endif
5137
Tim Peters5aa91602002-01-30 05:46:57 +00005138 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005139 Py_FileSystemDefaultEncoding, &file,
5140 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005141 return NULL;
5142
Barry Warsaw53699e91996-12-10 23:23:01 +00005143 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005144 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005145 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005146 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005147 return posix_error_with_allocated_filename(file);
5148 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005149 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005150}
5151
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005153PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005154"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005155Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005156
Barry Warsaw53699e91996-12-10 23:23:01 +00005157static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005158posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005159{
5160 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005161 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005162 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005163 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005164 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005165 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005166 if (res < 0)
5167 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005168 Py_INCREF(Py_None);
5169 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005170}
5171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005173PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005174"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005175Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005176
Barry Warsaw53699e91996-12-10 23:23:01 +00005177static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005178posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005179{
5180 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005181 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005182 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005183 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005184 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005185 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005186 if (fd < 0)
5187 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005188 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005189}
5190
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005192PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005193"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005194Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005195
Barry Warsaw53699e91996-12-10 23:23:01 +00005196static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005197posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005198{
5199 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005200 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005201 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005202 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005203 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005204 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005205 if (res < 0)
5206 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005207 Py_INCREF(Py_None);
5208 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005209}
5210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005213"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005214Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005215
Barry Warsaw53699e91996-12-10 23:23:01 +00005216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005217posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005218{
5219 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005220#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005221 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005222#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005223 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005224#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005225 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005226 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005227 return NULL;
5228#ifdef SEEK_SET
5229 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5230 switch (how) {
5231 case 0: how = SEEK_SET; break;
5232 case 1: how = SEEK_CUR; break;
5233 case 2: how = SEEK_END; break;
5234 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005235#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005236
5237#if !defined(HAVE_LARGEFILE_SUPPORT)
5238 pos = PyInt_AsLong(posobj);
5239#else
5240 pos = PyLong_Check(posobj) ?
5241 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5242#endif
5243 if (PyErr_Occurred())
5244 return NULL;
5245
Barry Warsaw53699e91996-12-10 23:23:01 +00005246 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005247#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005248 res = _lseeki64(fd, pos, how);
5249#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005250 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005251#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005252 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005253 if (res < 0)
5254 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005255
5256#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005257 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005258#else
5259 return PyLong_FromLongLong(res);
5260#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005261}
5262
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005264PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005265"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005266Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005267
Barry Warsaw53699e91996-12-10 23:23:01 +00005268static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005269posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005270{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005271 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005272 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005273 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005274 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005275 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005276 if (buffer == NULL)
5277 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005278 Py_BEGIN_ALLOW_THREADS
5279 n = read(fd, PyString_AsString(buffer), size);
5280 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005281 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005282 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005283 return posix_error();
5284 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005285 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005286 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005287 return buffer;
5288}
5289
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005291PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005292"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005293Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005294
Barry Warsaw53699e91996-12-10 23:23:01 +00005295static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005296posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005297{
5298 int fd, size;
5299 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005300 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005301 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005302 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005303 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005304 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005305 if (size < 0)
5306 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005307 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005308}
5309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005311PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005312"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005313Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005314
Barry Warsaw53699e91996-12-10 23:23:01 +00005315static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005316posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005317{
5318 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005319 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005320 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005321 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005322 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005323#ifdef __VMS
5324 /* on OpenVMS we must ensure that all bytes are written to the file */
5325 fsync(fd);
5326#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005327 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005328 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005329 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005330 if (res != 0)
5331 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005332
Fred Drake699f3522000-06-29 21:12:41 +00005333 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005334}
5335
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005337PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005338"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005339Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005340
Barry Warsaw53699e91996-12-10 23:23:01 +00005341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005342posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005343{
Guido van Rossum687dd131993-05-17 08:34:16 +00005344 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005345 char *mode = "r";
5346 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005347 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005348 PyObject *f;
5349 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005350 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005351
Thomas Heller1f043e22002-11-07 16:00:59 +00005352 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5353 PyErr_Format(PyExc_ValueError,
5354 "invalid file mode '%s'", mode);
5355 return NULL;
5356 }
5357
Barry Warsaw53699e91996-12-10 23:23:01 +00005358 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005359 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005360 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005361 if (fp == NULL)
5362 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005363 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005364 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005365 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005366 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005367}
5368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005369PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005370"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005371Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005372connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005373
5374static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005375posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005376{
5377 int fd;
5378 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5379 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005380 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005381}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005382
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005383#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005385"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005387
Barry Warsaw53699e91996-12-10 23:23:01 +00005388static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005389posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005390{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005391#if defined(PYOS_OS2)
5392 HFILE read, write;
5393 APIRET rc;
5394
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005395 Py_BEGIN_ALLOW_THREADS
5396 rc = DosCreatePipe( &read, &write, 4096);
5397 Py_END_ALLOW_THREADS
5398 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005399 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005400
5401 return Py_BuildValue("(ii)", read, write);
5402#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005403#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005404 int fds[2];
5405 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005406 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005407 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005408 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005409 if (res != 0)
5410 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005411 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005412#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005413 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005414 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005415 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005416 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005417 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005418 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005419 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005420 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005421 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5422 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005423 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005424#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005425#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005426}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005427#endif /* HAVE_PIPE */
5428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005429
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005430#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005431PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005432"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005433Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005434
Barry Warsaw53699e91996-12-10 23:23:01 +00005435static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005436posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005437{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005438 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005439 int mode = 0666;
5440 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005441 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005442 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005443 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005444 res = mkfifo(filename, mode);
5445 Py_END_ALLOW_THREADS
5446 if (res < 0)
5447 return posix_error();
5448 Py_INCREF(Py_None);
5449 return Py_None;
5450}
5451#endif
5452
5453
Neal Norwitz11690112002-07-30 01:08:28 +00005454#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005455PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005456"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005457Create a filesystem node (file, device special file or named pipe)\n\
5458named filename. mode specifies both the permissions to use and the\n\
5459type of node to be created, being combined (bitwise OR) with one of\n\
5460S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005461device defines the newly created device special file (probably using\n\
5462os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005463
5464
5465static PyObject *
5466posix_mknod(PyObject *self, PyObject *args)
5467{
5468 char *filename;
5469 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005470 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005471 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005472 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005473 return NULL;
5474 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005475 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005476 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005477 if (res < 0)
5478 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005479 Py_INCREF(Py_None);
5480 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005481}
5482#endif
5483
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005484#ifdef HAVE_DEVICE_MACROS
5485PyDoc_STRVAR(posix_major__doc__,
5486"major(device) -> major number\n\
5487Extracts a device major number from a raw device number.");
5488
5489static PyObject *
5490posix_major(PyObject *self, PyObject *args)
5491{
5492 int device;
5493 if (!PyArg_ParseTuple(args, "i:major", &device))
5494 return NULL;
5495 return PyInt_FromLong((long)major(device));
5496}
5497
5498PyDoc_STRVAR(posix_minor__doc__,
5499"minor(device) -> minor number\n\
5500Extracts a device minor number from a raw device number.");
5501
5502static PyObject *
5503posix_minor(PyObject *self, PyObject *args)
5504{
5505 int device;
5506 if (!PyArg_ParseTuple(args, "i:minor", &device))
5507 return NULL;
5508 return PyInt_FromLong((long)minor(device));
5509}
5510
5511PyDoc_STRVAR(posix_makedev__doc__,
5512"makedev(major, minor) -> device number\n\
5513Composes a raw device number from the major and minor device numbers.");
5514
5515static PyObject *
5516posix_makedev(PyObject *self, PyObject *args)
5517{
5518 int major, minor;
5519 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5520 return NULL;
5521 return PyInt_FromLong((long)makedev(major, minor));
5522}
5523#endif /* device macros */
5524
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005525
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005526#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005527PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005528"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005529Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005530
Barry Warsaw53699e91996-12-10 23:23:01 +00005531static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005532posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005533{
5534 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005535 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005536 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005537 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005538
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005539 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005540 return NULL;
5541
5542#if !defined(HAVE_LARGEFILE_SUPPORT)
5543 length = PyInt_AsLong(lenobj);
5544#else
5545 length = PyLong_Check(lenobj) ?
5546 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5547#endif
5548 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005549 return NULL;
5550
Barry Warsaw53699e91996-12-10 23:23:01 +00005551 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005552 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005553 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005554 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005555 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005556 return NULL;
5557 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005558 Py_INCREF(Py_None);
5559 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005560}
5561#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005562
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005563#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005565"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005567
Fred Drake762e2061999-08-26 17:23:54 +00005568/* Save putenv() parameters as values here, so we can collect them when they
5569 * get re-set with another call for the same key. */
5570static PyObject *posix_putenv_garbage;
5571
Tim Peters5aa91602002-01-30 05:46:57 +00005572static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005573posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005574{
5575 char *s1, *s2;
5576 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005577 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005578 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005579
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005580 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005581 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005582
5583#if defined(PYOS_OS2)
5584 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5585 APIRET rc;
5586
Guido van Rossumd48f2521997-12-05 22:19:34 +00005587 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5588 if (rc != NO_ERROR)
5589 return os2_error(rc);
5590
5591 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5592 APIRET rc;
5593
Guido van Rossumd48f2521997-12-05 22:19:34 +00005594 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5595 if (rc != NO_ERROR)
5596 return os2_error(rc);
5597 } else {
5598#endif
5599
Fred Drake762e2061999-08-26 17:23:54 +00005600 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005601 len = strlen(s1) + strlen(s2) + 2;
5602 /* len includes space for a trailing \0; the size arg to
5603 PyString_FromStringAndSize does not count that */
5604 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005605 if (newstr == NULL)
5606 return PyErr_NoMemory();
5607 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005608 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005609 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005610 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005611 posix_error();
5612 return NULL;
5613 }
Fred Drake762e2061999-08-26 17:23:54 +00005614 /* Install the first arg and newstr in posix_putenv_garbage;
5615 * this will cause previous value to be collected. This has to
5616 * happen after the real putenv() call because the old value
5617 * was still accessible until then. */
5618 if (PyDict_SetItem(posix_putenv_garbage,
5619 PyTuple_GET_ITEM(args, 0), newstr)) {
5620 /* really not much we can do; just leak */
5621 PyErr_Clear();
5622 }
5623 else {
5624 Py_DECREF(newstr);
5625 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005626
5627#if defined(PYOS_OS2)
5628 }
5629#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005630 Py_INCREF(Py_None);
5631 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005632}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005633#endif /* putenv */
5634
Guido van Rossumc524d952001-10-19 01:31:59 +00005635#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005636PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005637"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005638Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005639
5640static PyObject *
5641posix_unsetenv(PyObject *self, PyObject *args)
5642{
5643 char *s1;
5644
5645 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5646 return NULL;
5647
5648 unsetenv(s1);
5649
5650 /* Remove the key from posix_putenv_garbage;
5651 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005652 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005653 * old value was still accessible until then.
5654 */
5655 if (PyDict_DelItem(posix_putenv_garbage,
5656 PyTuple_GET_ITEM(args, 0))) {
5657 /* really not much we can do; just leak */
5658 PyErr_Clear();
5659 }
5660
5661 Py_INCREF(Py_None);
5662 return Py_None;
5663}
5664#endif /* unsetenv */
5665
Guido van Rossumb6a47161997-09-15 22:54:34 +00005666#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005667PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005668"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005669Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005670
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005671static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005672posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005673{
5674 int code;
5675 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005676 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005677 return NULL;
5678 message = strerror(code);
5679 if (message == NULL) {
5680 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005681 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005682 return NULL;
5683 }
5684 return PyString_FromString(message);
5685}
5686#endif /* strerror */
5687
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005688
Guido van Rossumc9641791998-08-04 15:26:23 +00005689#ifdef HAVE_SYS_WAIT_H
5690
Fred Drake106c1a02002-04-23 15:58:02 +00005691#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005692PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005693"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005694Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005695
5696static PyObject *
5697posix_WCOREDUMP(PyObject *self, PyObject *args)
5698{
5699#ifdef UNION_WAIT
5700 union wait status;
5701#define status_i (status.w_status)
5702#else
5703 int status;
5704#define status_i status
5705#endif
5706 status_i = 0;
5707
5708 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5709 {
5710 return NULL;
5711 }
5712
5713 return PyBool_FromLong(WCOREDUMP(status));
5714#undef status_i
5715}
5716#endif /* WCOREDUMP */
5717
5718#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005719PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005720"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005721Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005722job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005723
5724static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005725posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005726{
5727#ifdef UNION_WAIT
5728 union wait status;
5729#define status_i (status.w_status)
5730#else
5731 int status;
5732#define status_i status
5733#endif
5734 status_i = 0;
5735
5736 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5737 {
5738 return NULL;
5739 }
5740
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005741 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005742#undef status_i
5743}
5744#endif /* WIFCONTINUED */
5745
Guido van Rossumc9641791998-08-04 15:26:23 +00005746#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005748"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005749Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005750
5751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005752posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005753{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005754#ifdef UNION_WAIT
5755 union wait status;
5756#define status_i (status.w_status)
5757#else
5758 int status;
5759#define status_i status
5760#endif
5761 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005762
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005763 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005764 {
5765 return NULL;
5766 }
Tim Peters5aa91602002-01-30 05:46:57 +00005767
Fred Drake106c1a02002-04-23 15:58:02 +00005768 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005769#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005770}
5771#endif /* WIFSTOPPED */
5772
5773#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005774PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005775"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005776Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005777
5778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005779posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005780{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005781#ifdef UNION_WAIT
5782 union wait status;
5783#define status_i (status.w_status)
5784#else
5785 int status;
5786#define status_i status
5787#endif
5788 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005789
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005790 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005791 {
5792 return NULL;
5793 }
Tim Peters5aa91602002-01-30 05:46:57 +00005794
Fred Drake106c1a02002-04-23 15:58:02 +00005795 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005796#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005797}
5798#endif /* WIFSIGNALED */
5799
5800#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005802"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005803Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005804system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005805
5806static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005807posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005808{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005809#ifdef UNION_WAIT
5810 union wait status;
5811#define status_i (status.w_status)
5812#else
5813 int status;
5814#define status_i status
5815#endif
5816 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005817
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005818 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005819 {
5820 return NULL;
5821 }
Tim Peters5aa91602002-01-30 05:46:57 +00005822
Fred Drake106c1a02002-04-23 15:58:02 +00005823 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005824#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005825}
5826#endif /* WIFEXITED */
5827
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005828#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005829PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005830"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005831Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005832
5833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005834posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005835{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005836#ifdef UNION_WAIT
5837 union wait status;
5838#define status_i (status.w_status)
5839#else
5840 int status;
5841#define status_i status
5842#endif
5843 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005844
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005845 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005846 {
5847 return NULL;
5848 }
Tim Peters5aa91602002-01-30 05:46:57 +00005849
Guido van Rossumc9641791998-08-04 15:26:23 +00005850 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005851#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005852}
5853#endif /* WEXITSTATUS */
5854
5855#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005856PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005857"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005858Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005859value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005860
5861static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005862posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005863{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005864#ifdef UNION_WAIT
5865 union wait status;
5866#define status_i (status.w_status)
5867#else
5868 int status;
5869#define status_i status
5870#endif
5871 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005872
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005873 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005874 {
5875 return NULL;
5876 }
Tim Peters5aa91602002-01-30 05:46:57 +00005877
Guido van Rossumc9641791998-08-04 15:26:23 +00005878 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005879#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005880}
5881#endif /* WTERMSIG */
5882
5883#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005884PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005885"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005886Return the signal that stopped the process that provided\n\
5887the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005888
5889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005890posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005891{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005892#ifdef UNION_WAIT
5893 union wait status;
5894#define status_i (status.w_status)
5895#else
5896 int status;
5897#define status_i status
5898#endif
5899 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005900
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005901 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005902 {
5903 return NULL;
5904 }
Tim Peters5aa91602002-01-30 05:46:57 +00005905
Guido van Rossumc9641791998-08-04 15:26:23 +00005906 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005907#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005908}
5909#endif /* WSTOPSIG */
5910
5911#endif /* HAVE_SYS_WAIT_H */
5912
5913
Guido van Rossum94f6f721999-01-06 18:42:14 +00005914#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005915#ifdef _SCO_DS
5916/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5917 needed definitions in sys/statvfs.h */
5918#define _SVID3
5919#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005920#include <sys/statvfs.h>
5921
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005922static PyObject*
5923_pystatvfs_fromstructstatvfs(struct statvfs st) {
5924 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5925 if (v == NULL)
5926 return NULL;
5927
5928#if !defined(HAVE_LARGEFILE_SUPPORT)
5929 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5930 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5931 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5932 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5933 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5934 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5935 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5936 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5937 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5938 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5939#else
5940 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5941 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005942 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005943 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005944 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005945 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005946 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005947 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005948 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005949 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005950 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005951 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005952 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005953 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005954 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5955 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5956#endif
5957
5958 return v;
5959}
5960
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005961PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005962"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005963Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005964
5965static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005966posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005967{
5968 int fd, res;
5969 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005970
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005971 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005972 return NULL;
5973 Py_BEGIN_ALLOW_THREADS
5974 res = fstatvfs(fd, &st);
5975 Py_END_ALLOW_THREADS
5976 if (res != 0)
5977 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005978
5979 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005980}
5981#endif /* HAVE_FSTATVFS */
5982
5983
5984#if defined(HAVE_STATVFS)
5985#include <sys/statvfs.h>
5986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005987PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005988"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005989Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005990
5991static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005992posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005993{
5994 char *path;
5995 int res;
5996 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005997 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005998 return NULL;
5999 Py_BEGIN_ALLOW_THREADS
6000 res = statvfs(path, &st);
6001 Py_END_ALLOW_THREADS
6002 if (res != 0)
6003 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006004
6005 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006006}
6007#endif /* HAVE_STATVFS */
6008
6009
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006010#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006011PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006012"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006013Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006014The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006016
6017static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006018posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006019{
6020 PyObject *result = NULL;
6021 char *dir = NULL;
6022 char *pfx = NULL;
6023 char *name;
6024
6025 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6026 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006027
6028 if (PyErr_Warn(PyExc_RuntimeWarning,
6029 "tempnam is a potential security risk to your program") < 0)
6030 return NULL;
6031
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006032#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006033 name = _tempnam(dir, pfx);
6034#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006035 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006036#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006037 if (name == NULL)
6038 return PyErr_NoMemory();
6039 result = PyString_FromString(name);
6040 free(name);
6041 return result;
6042}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006043#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006044
6045
6046#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006047PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006048"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006049Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006050
6051static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006052posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006053{
6054 FILE *fp;
6055
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006056 fp = tmpfile();
6057 if (fp == NULL)
6058 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006059 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006060}
6061#endif
6062
6063
6064#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006065PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006066"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006067Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006068
6069static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006070posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006071{
6072 char buffer[L_tmpnam];
6073 char *name;
6074
Skip Montanaro95618b52001-08-18 18:52:10 +00006075 if (PyErr_Warn(PyExc_RuntimeWarning,
6076 "tmpnam is a potential security risk to your program") < 0)
6077 return NULL;
6078
Greg Wardb48bc172000-03-01 21:51:56 +00006079#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006080 name = tmpnam_r(buffer);
6081#else
6082 name = tmpnam(buffer);
6083#endif
6084 if (name == NULL) {
6085 PyErr_SetObject(PyExc_OSError,
6086 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006087#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006088 "unexpected NULL from tmpnam_r"
6089#else
6090 "unexpected NULL from tmpnam"
6091#endif
6092 ));
6093 return NULL;
6094 }
6095 return PyString_FromString(buffer);
6096}
6097#endif
6098
6099
Fred Drakec9680921999-12-13 16:37:25 +00006100/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6101 * It maps strings representing configuration variable names to
6102 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006103 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006104 * rarely-used constants. There are three separate tables that use
6105 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006106 *
6107 * This code is always included, even if none of the interfaces that
6108 * need it are included. The #if hackery needed to avoid it would be
6109 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006110 */
6111struct constdef {
6112 char *name;
6113 long value;
6114};
6115
Fred Drake12c6e2d1999-12-14 21:25:03 +00006116static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006117conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6118 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006119{
6120 if (PyInt_Check(arg)) {
6121 *valuep = PyInt_AS_LONG(arg);
6122 return 1;
6123 }
6124 if (PyString_Check(arg)) {
6125 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006126 size_t lo = 0;
6127 size_t mid;
6128 size_t hi = tablesize;
6129 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006130 char *confname = PyString_AS_STRING(arg);
6131 while (lo < hi) {
6132 mid = (lo + hi) / 2;
6133 cmp = strcmp(confname, table[mid].name);
6134 if (cmp < 0)
6135 hi = mid;
6136 else if (cmp > 0)
6137 lo = mid + 1;
6138 else {
6139 *valuep = table[mid].value;
6140 return 1;
6141 }
6142 }
6143 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6144 }
6145 else
6146 PyErr_SetString(PyExc_TypeError,
6147 "configuration names must be strings or integers");
6148 return 0;
6149}
6150
6151
6152#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6153static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006154#ifdef _PC_ABI_AIO_XFER_MAX
6155 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6156#endif
6157#ifdef _PC_ABI_ASYNC_IO
6158 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6159#endif
Fred Drakec9680921999-12-13 16:37:25 +00006160#ifdef _PC_ASYNC_IO
6161 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6162#endif
6163#ifdef _PC_CHOWN_RESTRICTED
6164 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6165#endif
6166#ifdef _PC_FILESIZEBITS
6167 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6168#endif
6169#ifdef _PC_LAST
6170 {"PC_LAST", _PC_LAST},
6171#endif
6172#ifdef _PC_LINK_MAX
6173 {"PC_LINK_MAX", _PC_LINK_MAX},
6174#endif
6175#ifdef _PC_MAX_CANON
6176 {"PC_MAX_CANON", _PC_MAX_CANON},
6177#endif
6178#ifdef _PC_MAX_INPUT
6179 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6180#endif
6181#ifdef _PC_NAME_MAX
6182 {"PC_NAME_MAX", _PC_NAME_MAX},
6183#endif
6184#ifdef _PC_NO_TRUNC
6185 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6186#endif
6187#ifdef _PC_PATH_MAX
6188 {"PC_PATH_MAX", _PC_PATH_MAX},
6189#endif
6190#ifdef _PC_PIPE_BUF
6191 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6192#endif
6193#ifdef _PC_PRIO_IO
6194 {"PC_PRIO_IO", _PC_PRIO_IO},
6195#endif
6196#ifdef _PC_SOCK_MAXBUF
6197 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6198#endif
6199#ifdef _PC_SYNC_IO
6200 {"PC_SYNC_IO", _PC_SYNC_IO},
6201#endif
6202#ifdef _PC_VDISABLE
6203 {"PC_VDISABLE", _PC_VDISABLE},
6204#endif
6205};
6206
Fred Drakec9680921999-12-13 16:37:25 +00006207static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006208conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006209{
6210 return conv_confname(arg, valuep, posix_constants_pathconf,
6211 sizeof(posix_constants_pathconf)
6212 / sizeof(struct constdef));
6213}
6214#endif
6215
6216#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006217PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006218"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006219Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006220If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006221
6222static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006223posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006224{
6225 PyObject *result = NULL;
6226 int name, fd;
6227
Fred Drake12c6e2d1999-12-14 21:25:03 +00006228 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6229 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006230 long limit;
6231
6232 errno = 0;
6233 limit = fpathconf(fd, name);
6234 if (limit == -1 && errno != 0)
6235 posix_error();
6236 else
6237 result = PyInt_FromLong(limit);
6238 }
6239 return result;
6240}
6241#endif
6242
6243
6244#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006245PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006246"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006247Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006248If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006249
6250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006251posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006252{
6253 PyObject *result = NULL;
6254 int name;
6255 char *path;
6256
6257 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6258 conv_path_confname, &name)) {
6259 long limit;
6260
6261 errno = 0;
6262 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006263 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006264 if (errno == EINVAL)
6265 /* could be a path or name problem */
6266 posix_error();
6267 else
6268 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006269 }
Fred Drakec9680921999-12-13 16:37:25 +00006270 else
6271 result = PyInt_FromLong(limit);
6272 }
6273 return result;
6274}
6275#endif
6276
6277#ifdef HAVE_CONFSTR
6278static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006279#ifdef _CS_ARCHITECTURE
6280 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6281#endif
6282#ifdef _CS_HOSTNAME
6283 {"CS_HOSTNAME", _CS_HOSTNAME},
6284#endif
6285#ifdef _CS_HW_PROVIDER
6286 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6287#endif
6288#ifdef _CS_HW_SERIAL
6289 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6290#endif
6291#ifdef _CS_INITTAB_NAME
6292 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6293#endif
Fred Drakec9680921999-12-13 16:37:25 +00006294#ifdef _CS_LFS64_CFLAGS
6295 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6296#endif
6297#ifdef _CS_LFS64_LDFLAGS
6298 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6299#endif
6300#ifdef _CS_LFS64_LIBS
6301 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6302#endif
6303#ifdef _CS_LFS64_LINTFLAGS
6304 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6305#endif
6306#ifdef _CS_LFS_CFLAGS
6307 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6308#endif
6309#ifdef _CS_LFS_LDFLAGS
6310 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6311#endif
6312#ifdef _CS_LFS_LIBS
6313 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6314#endif
6315#ifdef _CS_LFS_LINTFLAGS
6316 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6317#endif
Fred Draked86ed291999-12-15 15:34:33 +00006318#ifdef _CS_MACHINE
6319 {"CS_MACHINE", _CS_MACHINE},
6320#endif
Fred Drakec9680921999-12-13 16:37:25 +00006321#ifdef _CS_PATH
6322 {"CS_PATH", _CS_PATH},
6323#endif
Fred Draked86ed291999-12-15 15:34:33 +00006324#ifdef _CS_RELEASE
6325 {"CS_RELEASE", _CS_RELEASE},
6326#endif
6327#ifdef _CS_SRPC_DOMAIN
6328 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6329#endif
6330#ifdef _CS_SYSNAME
6331 {"CS_SYSNAME", _CS_SYSNAME},
6332#endif
6333#ifdef _CS_VERSION
6334 {"CS_VERSION", _CS_VERSION},
6335#endif
Fred Drakec9680921999-12-13 16:37:25 +00006336#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6337 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6338#endif
6339#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6340 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6341#endif
6342#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6343 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6344#endif
6345#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6346 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6347#endif
6348#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6349 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6350#endif
6351#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6352 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6353#endif
6354#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6355 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6356#endif
6357#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6358 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6359#endif
6360#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6361 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6362#endif
6363#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6364 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6365#endif
6366#ifdef _CS_XBS5_LP64_OFF64_LIBS
6367 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6368#endif
6369#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6370 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6371#endif
6372#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6373 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6374#endif
6375#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6376 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6377#endif
6378#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6379 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6380#endif
6381#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6382 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6383#endif
Fred Draked86ed291999-12-15 15:34:33 +00006384#ifdef _MIPS_CS_AVAIL_PROCESSORS
6385 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6386#endif
6387#ifdef _MIPS_CS_BASE
6388 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6389#endif
6390#ifdef _MIPS_CS_HOSTID
6391 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6392#endif
6393#ifdef _MIPS_CS_HW_NAME
6394 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6395#endif
6396#ifdef _MIPS_CS_NUM_PROCESSORS
6397 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6398#endif
6399#ifdef _MIPS_CS_OSREL_MAJ
6400 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6401#endif
6402#ifdef _MIPS_CS_OSREL_MIN
6403 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6404#endif
6405#ifdef _MIPS_CS_OSREL_PATCH
6406 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6407#endif
6408#ifdef _MIPS_CS_OS_NAME
6409 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6410#endif
6411#ifdef _MIPS_CS_OS_PROVIDER
6412 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6413#endif
6414#ifdef _MIPS_CS_PROCESSORS
6415 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6416#endif
6417#ifdef _MIPS_CS_SERIAL
6418 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6419#endif
6420#ifdef _MIPS_CS_VENDOR
6421 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6422#endif
Fred Drakec9680921999-12-13 16:37:25 +00006423};
6424
6425static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006426conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006427{
6428 return conv_confname(arg, valuep, posix_constants_confstr,
6429 sizeof(posix_constants_confstr)
6430 / sizeof(struct constdef));
6431}
6432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006433PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006434"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006435Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006436
6437static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006438posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006439{
6440 PyObject *result = NULL;
6441 int name;
6442 char buffer[64];
6443
6444 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6445 int len = confstr(name, buffer, sizeof(buffer));
6446
Fred Drakec9680921999-12-13 16:37:25 +00006447 errno = 0;
6448 if (len == 0) {
6449 if (errno != 0)
6450 posix_error();
6451 else
6452 result = PyString_FromString("");
6453 }
6454 else {
6455 if (len >= sizeof(buffer)) {
6456 result = PyString_FromStringAndSize(NULL, len);
6457 if (result != NULL)
6458 confstr(name, PyString_AS_STRING(result), len+1);
6459 }
6460 else
6461 result = PyString_FromString(buffer);
6462 }
6463 }
6464 return result;
6465}
6466#endif
6467
6468
6469#ifdef HAVE_SYSCONF
6470static struct constdef posix_constants_sysconf[] = {
6471#ifdef _SC_2_CHAR_TERM
6472 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6473#endif
6474#ifdef _SC_2_C_BIND
6475 {"SC_2_C_BIND", _SC_2_C_BIND},
6476#endif
6477#ifdef _SC_2_C_DEV
6478 {"SC_2_C_DEV", _SC_2_C_DEV},
6479#endif
6480#ifdef _SC_2_C_VERSION
6481 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6482#endif
6483#ifdef _SC_2_FORT_DEV
6484 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6485#endif
6486#ifdef _SC_2_FORT_RUN
6487 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6488#endif
6489#ifdef _SC_2_LOCALEDEF
6490 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6491#endif
6492#ifdef _SC_2_SW_DEV
6493 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6494#endif
6495#ifdef _SC_2_UPE
6496 {"SC_2_UPE", _SC_2_UPE},
6497#endif
6498#ifdef _SC_2_VERSION
6499 {"SC_2_VERSION", _SC_2_VERSION},
6500#endif
Fred Draked86ed291999-12-15 15:34:33 +00006501#ifdef _SC_ABI_ASYNCHRONOUS_IO
6502 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6503#endif
6504#ifdef _SC_ACL
6505 {"SC_ACL", _SC_ACL},
6506#endif
Fred Drakec9680921999-12-13 16:37:25 +00006507#ifdef _SC_AIO_LISTIO_MAX
6508 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6509#endif
Fred Drakec9680921999-12-13 16:37:25 +00006510#ifdef _SC_AIO_MAX
6511 {"SC_AIO_MAX", _SC_AIO_MAX},
6512#endif
6513#ifdef _SC_AIO_PRIO_DELTA_MAX
6514 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6515#endif
6516#ifdef _SC_ARG_MAX
6517 {"SC_ARG_MAX", _SC_ARG_MAX},
6518#endif
6519#ifdef _SC_ASYNCHRONOUS_IO
6520 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6521#endif
6522#ifdef _SC_ATEXIT_MAX
6523 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6524#endif
Fred Draked86ed291999-12-15 15:34:33 +00006525#ifdef _SC_AUDIT
6526 {"SC_AUDIT", _SC_AUDIT},
6527#endif
Fred Drakec9680921999-12-13 16:37:25 +00006528#ifdef _SC_AVPHYS_PAGES
6529 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6530#endif
6531#ifdef _SC_BC_BASE_MAX
6532 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6533#endif
6534#ifdef _SC_BC_DIM_MAX
6535 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6536#endif
6537#ifdef _SC_BC_SCALE_MAX
6538 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6539#endif
6540#ifdef _SC_BC_STRING_MAX
6541 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6542#endif
Fred Draked86ed291999-12-15 15:34:33 +00006543#ifdef _SC_CAP
6544 {"SC_CAP", _SC_CAP},
6545#endif
Fred Drakec9680921999-12-13 16:37:25 +00006546#ifdef _SC_CHARCLASS_NAME_MAX
6547 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6548#endif
6549#ifdef _SC_CHAR_BIT
6550 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6551#endif
6552#ifdef _SC_CHAR_MAX
6553 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6554#endif
6555#ifdef _SC_CHAR_MIN
6556 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6557#endif
6558#ifdef _SC_CHILD_MAX
6559 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6560#endif
6561#ifdef _SC_CLK_TCK
6562 {"SC_CLK_TCK", _SC_CLK_TCK},
6563#endif
6564#ifdef _SC_COHER_BLKSZ
6565 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6566#endif
6567#ifdef _SC_COLL_WEIGHTS_MAX
6568 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6569#endif
6570#ifdef _SC_DCACHE_ASSOC
6571 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6572#endif
6573#ifdef _SC_DCACHE_BLKSZ
6574 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6575#endif
6576#ifdef _SC_DCACHE_LINESZ
6577 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6578#endif
6579#ifdef _SC_DCACHE_SZ
6580 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6581#endif
6582#ifdef _SC_DCACHE_TBLKSZ
6583 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6584#endif
6585#ifdef _SC_DELAYTIMER_MAX
6586 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6587#endif
6588#ifdef _SC_EQUIV_CLASS_MAX
6589 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6590#endif
6591#ifdef _SC_EXPR_NEST_MAX
6592 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6593#endif
6594#ifdef _SC_FSYNC
6595 {"SC_FSYNC", _SC_FSYNC},
6596#endif
6597#ifdef _SC_GETGR_R_SIZE_MAX
6598 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6599#endif
6600#ifdef _SC_GETPW_R_SIZE_MAX
6601 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6602#endif
6603#ifdef _SC_ICACHE_ASSOC
6604 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6605#endif
6606#ifdef _SC_ICACHE_BLKSZ
6607 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6608#endif
6609#ifdef _SC_ICACHE_LINESZ
6610 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6611#endif
6612#ifdef _SC_ICACHE_SZ
6613 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6614#endif
Fred Draked86ed291999-12-15 15:34:33 +00006615#ifdef _SC_INF
6616 {"SC_INF", _SC_INF},
6617#endif
Fred Drakec9680921999-12-13 16:37:25 +00006618#ifdef _SC_INT_MAX
6619 {"SC_INT_MAX", _SC_INT_MAX},
6620#endif
6621#ifdef _SC_INT_MIN
6622 {"SC_INT_MIN", _SC_INT_MIN},
6623#endif
6624#ifdef _SC_IOV_MAX
6625 {"SC_IOV_MAX", _SC_IOV_MAX},
6626#endif
Fred Draked86ed291999-12-15 15:34:33 +00006627#ifdef _SC_IP_SECOPTS
6628 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6629#endif
Fred Drakec9680921999-12-13 16:37:25 +00006630#ifdef _SC_JOB_CONTROL
6631 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6632#endif
Fred Draked86ed291999-12-15 15:34:33 +00006633#ifdef _SC_KERN_POINTERS
6634 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6635#endif
6636#ifdef _SC_KERN_SIM
6637 {"SC_KERN_SIM", _SC_KERN_SIM},
6638#endif
Fred Drakec9680921999-12-13 16:37:25 +00006639#ifdef _SC_LINE_MAX
6640 {"SC_LINE_MAX", _SC_LINE_MAX},
6641#endif
6642#ifdef _SC_LOGIN_NAME_MAX
6643 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6644#endif
6645#ifdef _SC_LOGNAME_MAX
6646 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6647#endif
6648#ifdef _SC_LONG_BIT
6649 {"SC_LONG_BIT", _SC_LONG_BIT},
6650#endif
Fred Draked86ed291999-12-15 15:34:33 +00006651#ifdef _SC_MAC
6652 {"SC_MAC", _SC_MAC},
6653#endif
Fred Drakec9680921999-12-13 16:37:25 +00006654#ifdef _SC_MAPPED_FILES
6655 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6656#endif
6657#ifdef _SC_MAXPID
6658 {"SC_MAXPID", _SC_MAXPID},
6659#endif
6660#ifdef _SC_MB_LEN_MAX
6661 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6662#endif
6663#ifdef _SC_MEMLOCK
6664 {"SC_MEMLOCK", _SC_MEMLOCK},
6665#endif
6666#ifdef _SC_MEMLOCK_RANGE
6667 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6668#endif
6669#ifdef _SC_MEMORY_PROTECTION
6670 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6671#endif
6672#ifdef _SC_MESSAGE_PASSING
6673 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6674#endif
Fred Draked86ed291999-12-15 15:34:33 +00006675#ifdef _SC_MMAP_FIXED_ALIGNMENT
6676 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6677#endif
Fred Drakec9680921999-12-13 16:37:25 +00006678#ifdef _SC_MQ_OPEN_MAX
6679 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6680#endif
6681#ifdef _SC_MQ_PRIO_MAX
6682 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6683#endif
Fred Draked86ed291999-12-15 15:34:33 +00006684#ifdef _SC_NACLS_MAX
6685 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6686#endif
Fred Drakec9680921999-12-13 16:37:25 +00006687#ifdef _SC_NGROUPS_MAX
6688 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6689#endif
6690#ifdef _SC_NL_ARGMAX
6691 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6692#endif
6693#ifdef _SC_NL_LANGMAX
6694 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6695#endif
6696#ifdef _SC_NL_MSGMAX
6697 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6698#endif
6699#ifdef _SC_NL_NMAX
6700 {"SC_NL_NMAX", _SC_NL_NMAX},
6701#endif
6702#ifdef _SC_NL_SETMAX
6703 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6704#endif
6705#ifdef _SC_NL_TEXTMAX
6706 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6707#endif
6708#ifdef _SC_NPROCESSORS_CONF
6709 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6710#endif
6711#ifdef _SC_NPROCESSORS_ONLN
6712 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6713#endif
Fred Draked86ed291999-12-15 15:34:33 +00006714#ifdef _SC_NPROC_CONF
6715 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6716#endif
6717#ifdef _SC_NPROC_ONLN
6718 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6719#endif
Fred Drakec9680921999-12-13 16:37:25 +00006720#ifdef _SC_NZERO
6721 {"SC_NZERO", _SC_NZERO},
6722#endif
6723#ifdef _SC_OPEN_MAX
6724 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6725#endif
6726#ifdef _SC_PAGESIZE
6727 {"SC_PAGESIZE", _SC_PAGESIZE},
6728#endif
6729#ifdef _SC_PAGE_SIZE
6730 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6731#endif
6732#ifdef _SC_PASS_MAX
6733 {"SC_PASS_MAX", _SC_PASS_MAX},
6734#endif
6735#ifdef _SC_PHYS_PAGES
6736 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6737#endif
6738#ifdef _SC_PII
6739 {"SC_PII", _SC_PII},
6740#endif
6741#ifdef _SC_PII_INTERNET
6742 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6743#endif
6744#ifdef _SC_PII_INTERNET_DGRAM
6745 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6746#endif
6747#ifdef _SC_PII_INTERNET_STREAM
6748 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6749#endif
6750#ifdef _SC_PII_OSI
6751 {"SC_PII_OSI", _SC_PII_OSI},
6752#endif
6753#ifdef _SC_PII_OSI_CLTS
6754 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6755#endif
6756#ifdef _SC_PII_OSI_COTS
6757 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6758#endif
6759#ifdef _SC_PII_OSI_M
6760 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6761#endif
6762#ifdef _SC_PII_SOCKET
6763 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6764#endif
6765#ifdef _SC_PII_XTI
6766 {"SC_PII_XTI", _SC_PII_XTI},
6767#endif
6768#ifdef _SC_POLL
6769 {"SC_POLL", _SC_POLL},
6770#endif
6771#ifdef _SC_PRIORITIZED_IO
6772 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6773#endif
6774#ifdef _SC_PRIORITY_SCHEDULING
6775 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6776#endif
6777#ifdef _SC_REALTIME_SIGNALS
6778 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6779#endif
6780#ifdef _SC_RE_DUP_MAX
6781 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6782#endif
6783#ifdef _SC_RTSIG_MAX
6784 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6785#endif
6786#ifdef _SC_SAVED_IDS
6787 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6788#endif
6789#ifdef _SC_SCHAR_MAX
6790 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6791#endif
6792#ifdef _SC_SCHAR_MIN
6793 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6794#endif
6795#ifdef _SC_SELECT
6796 {"SC_SELECT", _SC_SELECT},
6797#endif
6798#ifdef _SC_SEMAPHORES
6799 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6800#endif
6801#ifdef _SC_SEM_NSEMS_MAX
6802 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6803#endif
6804#ifdef _SC_SEM_VALUE_MAX
6805 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6806#endif
6807#ifdef _SC_SHARED_MEMORY_OBJECTS
6808 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6809#endif
6810#ifdef _SC_SHRT_MAX
6811 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6812#endif
6813#ifdef _SC_SHRT_MIN
6814 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6815#endif
6816#ifdef _SC_SIGQUEUE_MAX
6817 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6818#endif
6819#ifdef _SC_SIGRT_MAX
6820 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6821#endif
6822#ifdef _SC_SIGRT_MIN
6823 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6824#endif
Fred Draked86ed291999-12-15 15:34:33 +00006825#ifdef _SC_SOFTPOWER
6826 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6827#endif
Fred Drakec9680921999-12-13 16:37:25 +00006828#ifdef _SC_SPLIT_CACHE
6829 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6830#endif
6831#ifdef _SC_SSIZE_MAX
6832 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6833#endif
6834#ifdef _SC_STACK_PROT
6835 {"SC_STACK_PROT", _SC_STACK_PROT},
6836#endif
6837#ifdef _SC_STREAM_MAX
6838 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6839#endif
6840#ifdef _SC_SYNCHRONIZED_IO
6841 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6842#endif
6843#ifdef _SC_THREADS
6844 {"SC_THREADS", _SC_THREADS},
6845#endif
6846#ifdef _SC_THREAD_ATTR_STACKADDR
6847 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6848#endif
6849#ifdef _SC_THREAD_ATTR_STACKSIZE
6850 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6851#endif
6852#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6853 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6854#endif
6855#ifdef _SC_THREAD_KEYS_MAX
6856 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6857#endif
6858#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6859 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6860#endif
6861#ifdef _SC_THREAD_PRIO_INHERIT
6862 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6863#endif
6864#ifdef _SC_THREAD_PRIO_PROTECT
6865 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6866#endif
6867#ifdef _SC_THREAD_PROCESS_SHARED
6868 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6869#endif
6870#ifdef _SC_THREAD_SAFE_FUNCTIONS
6871 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6872#endif
6873#ifdef _SC_THREAD_STACK_MIN
6874 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6875#endif
6876#ifdef _SC_THREAD_THREADS_MAX
6877 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6878#endif
6879#ifdef _SC_TIMERS
6880 {"SC_TIMERS", _SC_TIMERS},
6881#endif
6882#ifdef _SC_TIMER_MAX
6883 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6884#endif
6885#ifdef _SC_TTY_NAME_MAX
6886 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6887#endif
6888#ifdef _SC_TZNAME_MAX
6889 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6890#endif
6891#ifdef _SC_T_IOV_MAX
6892 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6893#endif
6894#ifdef _SC_UCHAR_MAX
6895 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6896#endif
6897#ifdef _SC_UINT_MAX
6898 {"SC_UINT_MAX", _SC_UINT_MAX},
6899#endif
6900#ifdef _SC_UIO_MAXIOV
6901 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6902#endif
6903#ifdef _SC_ULONG_MAX
6904 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6905#endif
6906#ifdef _SC_USHRT_MAX
6907 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6908#endif
6909#ifdef _SC_VERSION
6910 {"SC_VERSION", _SC_VERSION},
6911#endif
6912#ifdef _SC_WORD_BIT
6913 {"SC_WORD_BIT", _SC_WORD_BIT},
6914#endif
6915#ifdef _SC_XBS5_ILP32_OFF32
6916 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6917#endif
6918#ifdef _SC_XBS5_ILP32_OFFBIG
6919 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6920#endif
6921#ifdef _SC_XBS5_LP64_OFF64
6922 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6923#endif
6924#ifdef _SC_XBS5_LPBIG_OFFBIG
6925 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6926#endif
6927#ifdef _SC_XOPEN_CRYPT
6928 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6929#endif
6930#ifdef _SC_XOPEN_ENH_I18N
6931 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6932#endif
6933#ifdef _SC_XOPEN_LEGACY
6934 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6935#endif
6936#ifdef _SC_XOPEN_REALTIME
6937 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6938#endif
6939#ifdef _SC_XOPEN_REALTIME_THREADS
6940 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6941#endif
6942#ifdef _SC_XOPEN_SHM
6943 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6944#endif
6945#ifdef _SC_XOPEN_UNIX
6946 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6947#endif
6948#ifdef _SC_XOPEN_VERSION
6949 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6950#endif
6951#ifdef _SC_XOPEN_XCU_VERSION
6952 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6953#endif
6954#ifdef _SC_XOPEN_XPG2
6955 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6956#endif
6957#ifdef _SC_XOPEN_XPG3
6958 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6959#endif
6960#ifdef _SC_XOPEN_XPG4
6961 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6962#endif
6963};
6964
6965static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006966conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006967{
6968 return conv_confname(arg, valuep, posix_constants_sysconf,
6969 sizeof(posix_constants_sysconf)
6970 / sizeof(struct constdef));
6971}
6972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006973PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006974"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006975Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006976
6977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006978posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006979{
6980 PyObject *result = NULL;
6981 int name;
6982
6983 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6984 int value;
6985
6986 errno = 0;
6987 value = sysconf(name);
6988 if (value == -1 && errno != 0)
6989 posix_error();
6990 else
6991 result = PyInt_FromLong(value);
6992 }
6993 return result;
6994}
6995#endif
6996
6997
Fred Drakebec628d1999-12-15 18:31:10 +00006998/* This code is used to ensure that the tables of configuration value names
6999 * are in sorted order as required by conv_confname(), and also to build the
7000 * the exported dictionaries that are used to publish information about the
7001 * names available on the host platform.
7002 *
7003 * Sorting the table at runtime ensures that the table is properly ordered
7004 * when used, even for platforms we're not able to test on. It also makes
7005 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007006 */
Fred Drakebec628d1999-12-15 18:31:10 +00007007
7008static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007009cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007010{
7011 const struct constdef *c1 =
7012 (const struct constdef *) v1;
7013 const struct constdef *c2 =
7014 (const struct constdef *) v2;
7015
7016 return strcmp(c1->name, c2->name);
7017}
7018
7019static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007020setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007021 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007022{
Fred Drakebec628d1999-12-15 18:31:10 +00007023 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007024 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007025
7026 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7027 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007028 if (d == NULL)
7029 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007030
Barry Warsaw3155db32000-04-13 15:20:40 +00007031 for (i=0; i < tablesize; ++i) {
7032 PyObject *o = PyInt_FromLong(table[i].value);
7033 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7034 Py_XDECREF(o);
7035 Py_DECREF(d);
7036 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007037 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007038 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007039 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007040 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007041}
7042
Fred Drakebec628d1999-12-15 18:31:10 +00007043/* Return -1 on failure, 0 on success. */
7044static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007045setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007046{
7047#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007048 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007049 sizeof(posix_constants_pathconf)
7050 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007051 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007052 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007053#endif
7054#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007055 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007056 sizeof(posix_constants_confstr)
7057 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007058 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007059 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007060#endif
7061#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007062 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007063 sizeof(posix_constants_sysconf)
7064 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007065 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007066 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007067#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007068 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007069}
Fred Draked86ed291999-12-15 15:34:33 +00007070
7071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007072PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007073"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007074Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007075in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007076
7077static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007078posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007079{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007080 abort();
7081 /*NOTREACHED*/
7082 Py_FatalError("abort() called from Python code didn't abort!");
7083 return NULL;
7084}
Fred Drakebec628d1999-12-15 18:31:10 +00007085
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007086#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007087PyDoc_STRVAR(win32_startfile__doc__,
7088"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007089\n\
7090This acts like double-clicking the file in Explorer, or giving the file\n\
7091name as an argument to the DOS \"start\" command: the file is opened\n\
7092with whatever application (if any) its extension is associated.\n\
7093\n\
7094startfile returns as soon as the associated application is launched.\n\
7095There is no option to wait for the application to close, and no way\n\
7096to retrieve the application's exit status.\n\
7097\n\
7098The filepath is relative to the current directory. If you want to use\n\
7099an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007100the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007101
7102static PyObject *
7103win32_startfile(PyObject *self, PyObject *args)
7104{
7105 char *filepath;
7106 HINSTANCE rc;
7107 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
7108 return NULL;
7109 Py_BEGIN_ALLOW_THREADS
7110 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
7111 Py_END_ALLOW_THREADS
7112 if (rc <= (HINSTANCE)32)
7113 return win32_error("startfile", filepath);
7114 Py_INCREF(Py_None);
7115 return Py_None;
7116}
7117#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007118
Martin v. Löwis438b5342002-12-27 10:16:42 +00007119#ifdef HAVE_GETLOADAVG
7120PyDoc_STRVAR(posix_getloadavg__doc__,
7121"getloadavg() -> (float, float, float)\n\n\
7122Return the number of processes in the system run queue averaged over\n\
7123the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7124was unobtainable");
7125
7126static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007127posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007128{
7129 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007130 if (getloadavg(loadavg, 3)!=3) {
7131 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7132 return NULL;
7133 } else
7134 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7135}
7136#endif
7137
7138
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007139static PyMethodDef posix_methods[] = {
7140 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7141#ifdef HAVE_TTYNAME
7142 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7143#endif
7144 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7145 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007146#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007147 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007148#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007149#ifdef HAVE_LCHOWN
7150 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7151#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007152#ifdef HAVE_CHROOT
7153 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7154#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007155#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007156 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007157#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007158#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007159 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007160#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007161 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007162#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007163#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007164#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007165 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007166#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007167 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7168 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7169 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007170#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007171 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007172#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007173#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007174 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007175#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007176 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7177 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7178 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007179 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007180#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007181 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007182#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007183#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007184 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007185#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007186 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007187#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007188 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007189#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007190 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7191 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7192 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007193#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007194 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007195#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007196 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007197#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007198 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7199 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007200#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007201#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007202 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7203 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007204#if defined(PYOS_OS2)
7205 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7206 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7207#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007208#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007209#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007210 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007211#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007212#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007213 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007214#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007215#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007216 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007217#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007218#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007219 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007220#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007221#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007222 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007223#endif /* HAVE_GETEGID */
7224#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007225 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007226#endif /* HAVE_GETEUID */
7227#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007228 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007229#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007230#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007231 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007233 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007234#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007235 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007236#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007237#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007238 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007239#endif /* HAVE_GETPPID */
7240#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007241 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007242#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007243#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007244 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007245#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007246#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007247 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007248#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007249#ifdef HAVE_KILLPG
7250 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7251#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007252#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007253 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007254#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007255#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007256 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007257#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007258 {"popen2", win32_popen2, METH_VARARGS},
7259 {"popen3", win32_popen3, METH_VARARGS},
7260 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007261 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007262#else
7263#if defined(PYOS_OS2) && defined(PYCC_GCC)
7264 {"popen2", os2emx_popen2, METH_VARARGS},
7265 {"popen3", os2emx_popen3, METH_VARARGS},
7266 {"popen4", os2emx_popen4, METH_VARARGS},
7267#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007268#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007269#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007270#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007271 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007272#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007273#ifdef HAVE_SETEUID
7274 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7275#endif /* HAVE_SETEUID */
7276#ifdef HAVE_SETEGID
7277 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7278#endif /* HAVE_SETEGID */
7279#ifdef HAVE_SETREUID
7280 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7281#endif /* HAVE_SETREUID */
7282#ifdef HAVE_SETREGID
7283 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7284#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007285#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007286 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007287#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007288#ifdef HAVE_SETGROUPS
7289 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7290#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007291#ifdef HAVE_GETPGID
7292 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7293#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007294#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007295 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007296#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007297#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007298 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007299#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007300#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007301 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007302#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007303#ifdef HAVE_GETSID
7304 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7305#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007306#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007307 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007308#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007309#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007310 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007311#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007312#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007313 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007314#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007315#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007316 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007317#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007318 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7319 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7320 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7321 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7322 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7323 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7324 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7325 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7326 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007327 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007328#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007329 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007330#endif
7331#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007332 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007333#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007334#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007335 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7336#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007337#ifdef HAVE_DEVICE_MACROS
7338 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7339 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7340 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7341#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007342#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007343 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007344#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007345#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007346 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007347#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007348#ifdef HAVE_UNSETENV
7349 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7350#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007351#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007352 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007353#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007354#ifdef HAVE_FCHDIR
7355 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7356#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007357#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007358 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007359#endif
7360#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007361 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007362#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007363#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007364#ifdef WCOREDUMP
7365 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7366#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007367#ifdef WIFCONTINUED
7368 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7369#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007370#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007371 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007372#endif /* WIFSTOPPED */
7373#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007374 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007375#endif /* WIFSIGNALED */
7376#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007377 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007378#endif /* WIFEXITED */
7379#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007380 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007381#endif /* WEXITSTATUS */
7382#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007383 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007384#endif /* WTERMSIG */
7385#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007386 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007387#endif /* WSTOPSIG */
7388#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007389#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007390 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007391#endif
7392#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007393 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007394#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007395#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007396 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007397#endif
7398#ifdef HAVE_TEMPNAM
7399 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7400#endif
7401#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007402 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007403#endif
Fred Drakec9680921999-12-13 16:37:25 +00007404#ifdef HAVE_CONFSTR
7405 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7406#endif
7407#ifdef HAVE_SYSCONF
7408 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7409#endif
7410#ifdef HAVE_FPATHCONF
7411 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7412#endif
7413#ifdef HAVE_PATHCONF
7414 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7415#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007416 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007417#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007418 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7419#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007420#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007421 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007422#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007423 {NULL, NULL} /* Sentinel */
7424};
7425
7426
Barry Warsaw4a342091996-12-19 23:50:02 +00007427static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007428ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007429{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007430 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007431}
7432
Guido van Rossumd48f2521997-12-05 22:19:34 +00007433#if defined(PYOS_OS2)
7434/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007435static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007436{
7437 APIRET rc;
7438 ULONG values[QSV_MAX+1];
7439 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007440 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007441
7442 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007443 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007444 Py_END_ALLOW_THREADS
7445
7446 if (rc != NO_ERROR) {
7447 os2_error(rc);
7448 return -1;
7449 }
7450
Fred Drake4d1e64b2002-04-15 19:40:07 +00007451 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7452 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7453 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7454 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7455 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7456 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7457 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007458
7459 switch (values[QSV_VERSION_MINOR]) {
7460 case 0: ver = "2.00"; break;
7461 case 10: ver = "2.10"; break;
7462 case 11: ver = "2.11"; break;
7463 case 30: ver = "3.00"; break;
7464 case 40: ver = "4.00"; break;
7465 case 50: ver = "5.00"; break;
7466 default:
Tim Peters885d4572001-11-28 20:27:42 +00007467 PyOS_snprintf(tmp, sizeof(tmp),
7468 "%d-%d", values[QSV_VERSION_MAJOR],
7469 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007470 ver = &tmp[0];
7471 }
7472
7473 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007474 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007475 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007476
7477 /* Add Indicator of Which Drive was Used to Boot the System */
7478 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7479 tmp[1] = ':';
7480 tmp[2] = '\0';
7481
Fred Drake4d1e64b2002-04-15 19:40:07 +00007482 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007483}
7484#endif
7485
Barry Warsaw4a342091996-12-19 23:50:02 +00007486static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007487all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007488{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007489#ifdef F_OK
7490 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007491#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007492#ifdef R_OK
7493 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007494#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007495#ifdef W_OK
7496 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007497#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007498#ifdef X_OK
7499 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007500#endif
Fred Drakec9680921999-12-13 16:37:25 +00007501#ifdef NGROUPS_MAX
7502 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7503#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007504#ifdef TMP_MAX
7505 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7506#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007507#ifdef WCONTINUED
7508 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7509#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007510#ifdef WNOHANG
7511 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007512#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007513#ifdef WUNTRACED
7514 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7515#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007516#ifdef O_RDONLY
7517 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7518#endif
7519#ifdef O_WRONLY
7520 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7521#endif
7522#ifdef O_RDWR
7523 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7524#endif
7525#ifdef O_NDELAY
7526 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7527#endif
7528#ifdef O_NONBLOCK
7529 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7530#endif
7531#ifdef O_APPEND
7532 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7533#endif
7534#ifdef O_DSYNC
7535 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7536#endif
7537#ifdef O_RSYNC
7538 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7539#endif
7540#ifdef O_SYNC
7541 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7542#endif
7543#ifdef O_NOCTTY
7544 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7545#endif
7546#ifdef O_CREAT
7547 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7548#endif
7549#ifdef O_EXCL
7550 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7551#endif
7552#ifdef O_TRUNC
7553 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7554#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007555#ifdef O_BINARY
7556 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7557#endif
7558#ifdef O_TEXT
7559 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7560#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007561#ifdef O_LARGEFILE
7562 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7563#endif
7564
Tim Peters5aa91602002-01-30 05:46:57 +00007565/* MS Windows */
7566#ifdef O_NOINHERIT
7567 /* Don't inherit in child processes. */
7568 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7569#endif
7570#ifdef _O_SHORT_LIVED
7571 /* Optimize for short life (keep in memory). */
7572 /* MS forgot to define this one with a non-underscore form too. */
7573 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7574#endif
7575#ifdef O_TEMPORARY
7576 /* Automatically delete when last handle is closed. */
7577 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7578#endif
7579#ifdef O_RANDOM
7580 /* Optimize for random access. */
7581 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7582#endif
7583#ifdef O_SEQUENTIAL
7584 /* Optimize for sequential access. */
7585 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7586#endif
7587
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007588/* GNU extensions. */
7589#ifdef O_DIRECT
7590 /* Direct disk access. */
7591 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7592#endif
7593#ifdef O_DIRECTORY
7594 /* Must be a directory. */
7595 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7596#endif
7597#ifdef O_NOFOLLOW
7598 /* Do not follow links. */
7599 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7600#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007601
Barry Warsaw5676bd12003-01-07 20:57:09 +00007602 /* These come from sysexits.h */
7603#ifdef EX_OK
7604 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007605#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007606#ifdef EX_USAGE
7607 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007608#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007609#ifdef EX_DATAERR
7610 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007611#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007612#ifdef EX_NOINPUT
7613 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007614#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007615#ifdef EX_NOUSER
7616 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007617#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007618#ifdef EX_NOHOST
7619 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007620#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007621#ifdef EX_UNAVAILABLE
7622 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007623#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007624#ifdef EX_SOFTWARE
7625 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007626#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007627#ifdef EX_OSERR
7628 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007629#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007630#ifdef EX_OSFILE
7631 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007632#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007633#ifdef EX_CANTCREAT
7634 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007635#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007636#ifdef EX_IOERR
7637 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007638#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007639#ifdef EX_TEMPFAIL
7640 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007641#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007642#ifdef EX_PROTOCOL
7643 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007644#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007645#ifdef EX_NOPERM
7646 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007647#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007648#ifdef EX_CONFIG
7649 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007650#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007651#ifdef EX_NOTFOUND
7652 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007653#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007654
Guido van Rossum246bc171999-02-01 23:54:31 +00007655#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007656#if defined(PYOS_OS2) && defined(PYCC_GCC)
7657 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7658 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7659 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7660 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7661 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7662 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7663 if (ins(d, "P_PM", (long)P_PM)) return -1;
7664 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7665 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7666 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7667 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7668 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7669 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7670 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7671 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7672 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7673 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7674 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7675 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7676 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7677#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007678 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7679 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7680 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7681 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7682 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007683#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007684#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007685
Guido van Rossumd48f2521997-12-05 22:19:34 +00007686#if defined(PYOS_OS2)
7687 if (insertvalues(d)) return -1;
7688#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007689 return 0;
7690}
7691
7692
Tim Peters5aa91602002-01-30 05:46:57 +00007693#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007694#define INITFUNC initnt
7695#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007696
7697#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007698#define INITFUNC initos2
7699#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007700
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007701#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007702#define INITFUNC initposix
7703#define MODNAME "posix"
7704#endif
7705
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007706PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007707INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007708{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007709 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007710
Fred Drake4d1e64b2002-04-15 19:40:07 +00007711 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007712 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007713 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007714
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007715 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007716 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007717 Py_XINCREF(v);
7718 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007719 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007720 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007721
Fred Drake4d1e64b2002-04-15 19:40:07 +00007722 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007723 return;
7724
Fred Drake4d1e64b2002-04-15 19:40:07 +00007725 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007726 return;
7727
Fred Drake4d1e64b2002-04-15 19:40:07 +00007728 Py_INCREF(PyExc_OSError);
7729 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007730
Guido van Rossumb3d39562000-01-31 18:41:26 +00007731#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007732 if (posix_putenv_garbage == NULL)
7733 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007734#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007735
Guido van Rossum14648392001-12-08 18:02:58 +00007736 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007737 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7738 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7739 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007740 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007741 structseq_new = StatResultType.tp_new;
7742 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007743 Py_INCREF((PyObject*) &StatResultType);
7744 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007745
Guido van Rossum14648392001-12-08 18:02:58 +00007746 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007747 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007748 Py_INCREF((PyObject*) &StatVFSResultType);
7749 PyModule_AddObject(m, "statvfs_result",
7750 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007751}