blob: 9c58c9d686bc8c849f9bb21d68ed4594dc9a5c15 [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>
45#include "osdefs.h"
46#endif
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#ifdef HAVE_SIGNAL_H
57#include <signal.h>
58#endif
59
Guido van Rossumb6775db1994-08-01 11:34:53 +000060#ifdef HAVE_FCNTL_H
61#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000062#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000063
Guido van Rossuma6535fd2001-10-18 19:44:10 +000064#ifdef HAVE_GRP_H
65#include <grp.h>
66#endif
67
Barry Warsaw5676bd12003-01-07 20:57:09 +000068#ifdef HAVE_SYSEXITS_H
69#include <sysexits.h>
70#endif /* HAVE_SYSEXITS_H */
71
Guido van Rossuma4916fa1996-05-23 22:58:55 +000072/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000073/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000074#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000075#include <process.h>
76#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000077#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000078#define HAVE_GETCWD 1
79#define HAVE_OPENDIR 1
80#define HAVE_SYSTEM 1
81#if defined(__OS2__)
82#define HAVE_EXECV 1
83#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000084#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000085#include <process.h>
86#else
87#ifdef __BORLANDC__ /* Borland compiler */
88#define HAVE_EXECV 1
89#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000090#define HAVE_OPENDIR 1
91#define HAVE_PIPE 1
92#define HAVE_POPEN 1
93#define HAVE_SYSTEM 1
94#define HAVE_WAIT 1
95#else
96#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000097#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +000098#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099#define HAVE_EXECV 1
100#define HAVE_PIPE 1
101#define HAVE_POPEN 1
102#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000103#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000104#define HAVE_FSYNC 1
105#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000106#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000107#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
108/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#else /* all other compilers */
110/* Unix functions that the configure script doesn't check for */
111#define HAVE_EXECV 1
112#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000113#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
114#define HAVE_FORK1 1
115#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_GETCWD 1
117#define HAVE_GETEGID 1
118#define HAVE_GETEUID 1
119#define HAVE_GETGID 1
120#define HAVE_GETPPID 1
121#define HAVE_GETUID 1
122#define HAVE_KILL 1
123#define HAVE_OPENDIR 1
124#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000125#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000127#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128#define HAVE_SYSTEM 1
129#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000130#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000131#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#endif /* _MSC_VER */
133#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000134#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000135#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000138
Guido van Rossumb00adfb2000-09-25 13:22:00 +0000139#if defined(sun) && !defined(__SVR4)
140/* SunOS 4.1.4 doesn't have prototypes for these: */
141extern int rename(const char *, const char *);
142extern int pclose(FILE *);
143extern int fclose(FILE *);
Thomas Wouters0f954a42001-02-15 08:46:56 +0000144extern int fsync(int);
145extern int lstat(const char *, struct stat *);
146extern int symlink(const char *, const char *);
Guido van Rossumb00adfb2000-09-25 13:22:00 +0000147#endif
148
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000149#if defined(__sgi)&&_COMPILER_VERSION>=700
150/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
151 (default) */
152extern char *ctermid_r(char *);
153#endif
154
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000155#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000157extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000159#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000160extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000162extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000163#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000164#endif
165#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000166extern int chdir(char *);
167extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int chdir(const char *);
170extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000172#ifdef __BORLANDC__
173extern int chmod(const char *, int);
174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000176#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int chown(const char *, uid_t, gid_t);
178extern char *getcwd(char *, int);
179extern char *strerror(int);
180extern int link(const char *, const char *);
181extern int rename(const char *, const char *);
182extern int stat(const char *, struct stat *);
183extern int unlink(const char *);
184extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000187#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000190#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000192
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#ifdef HAVE_UTIME_H
196#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000197#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000199#ifdef HAVE_SYS_UTIME_H
200#include <sys/utime.h>
201#define HAVE_UTIME_H /* pretend we do for the rest of this file */
202#endif /* HAVE_SYS_UTIME_H */
203
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_SYS_TIMES_H
205#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
208#ifdef HAVE_SYS_PARAM_H
209#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
212#ifdef HAVE_SYS_UTSNAME_H
213#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000216#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000218#define NAMLEN(dirent) strlen((dirent)->d_name)
219#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000220#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#include <direct.h>
222#define NAMLEN(dirent) strlen((dirent)->d_name)
223#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000225#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#endif
230#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232#endif
233#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#endif
236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <direct.h>
240#include <io.h>
241#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000242#include "osdefs.h"
Tim Petersee66d0c2002-07-15 16:10:55 +0000243#define WIN32_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000245#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000246#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000247#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249
Guido van Rossumd48f2521997-12-05 22:19:34 +0000250#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000252#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253
Tim Petersbc2e10e2002-03-03 23:17:02 +0000254#ifndef MAXPATHLEN
255#define MAXPATHLEN 1024
256#endif /* MAXPATHLEN */
257
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000258#ifdef UNION_WAIT
259/* Emulate some macros on systems that have a union instead of macros */
260
261#ifndef WIFEXITED
262#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
263#endif
264
265#ifndef WEXITSTATUS
266#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
267#endif
268
269#ifndef WTERMSIG
270#define WTERMSIG(u_wait) ((u_wait).w_termsig)
271#endif
272
273#endif /* UNION_WAIT */
274
Greg Wardb48bc172000-03-01 21:51:56 +0000275/* Don't use the "_r" form if we don't need it (also, won't have a
276 prototype for it, at least on Solaris -- maybe others as well?). */
277#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
278#define USE_CTERMID_R
279#endif
280
281#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
282#define USE_TMPNAM_R
283#endif
284
Fred Drake699f3522000-06-29 21:12:41 +0000285/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000286#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000287#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +0000288# define STAT _stati64
289# define FSTAT _fstati64
290# define STRUCT_STAT struct _stati64
291#else
292# define STAT stat
293# define FSTAT fstat
294# define STRUCT_STAT struct stat
295#endif
296
Tim Peters11b23062003-04-23 02:39:17 +0000297#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000298#include <sys/mkdev.h>
299#else
300#if defined(MAJOR_IN_SYSMACROS)
301#include <sys/sysmacros.h>
302#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000303#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
304#include <sys/mkdev.h>
305#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000306#endif
Fred Drake699f3522000-06-29 21:12:41 +0000307
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000309#ifdef WITH_NEXT_FRAMEWORK
310/* On Darwin/MacOSX a shared library or framework has no access to
311** environ directly, we must obtain it with _NSGetEnviron().
312*/
313#include <crt_externs.h>
314static char **environ;
315#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000317#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318
Barry Warsaw53699e91996-12-10 23:23:01 +0000319static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000320convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321{
Barry Warsaw53699e91996-12-10 23:23:01 +0000322 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000324 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 if (d == NULL)
326 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000327#ifdef WITH_NEXT_FRAMEWORK
328 if (environ == NULL)
329 environ = *_NSGetEnviron();
330#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 if (environ == NULL)
332 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000333 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000335 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000336 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337 char *p = strchr(*e, '=');
338 if (p == NULL)
339 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000340 k = PyString_FromStringAndSize(*e, (int)(p-*e));
341 if (k == NULL) {
342 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000344 }
345 v = PyString_FromString(p+1);
346 if (v == NULL) {
347 PyErr_Clear();
348 Py_DECREF(k);
349 continue;
350 }
351 if (PyDict_GetItem(d, k) == NULL) {
352 if (PyDict_SetItem(d, k, v) != 0)
353 PyErr_Clear();
354 }
355 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000356 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000358#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000359 {
360 APIRET rc;
361 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
362
363 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000364 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000365 PyObject *v = PyString_FromString(buffer);
366 PyDict_SetItemString(d, "BEGINLIBPATH", v);
367 Py_DECREF(v);
368 }
369 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
370 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
371 PyObject *v = PyString_FromString(buffer);
372 PyDict_SetItemString(d, "ENDLIBPATH", v);
373 Py_DECREF(v);
374 }
375 }
376#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 return d;
378}
379
380
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381/* Set a POSIX-specific error from errno, and return NULL */
382
Barry Warsawd58d7641998-07-23 16:14:40 +0000383static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000384posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000385{
Barry Warsawca74da41999-02-09 19:31:45 +0000386 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387}
Barry Warsawd58d7641998-07-23 16:14:40 +0000388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000389posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000390{
Barry Warsawca74da41999-02-09 19:31:45 +0000391 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000392}
393
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000394#ifdef Py_WIN_WIDE_FILENAMES
395static PyObject *
396posix_error_with_unicode_filename(Py_UNICODE* name)
397{
398 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
399}
400#endif /* Py_WIN_WIDE_FILENAMES */
401
402
Mark Hammondef8b6542001-05-13 08:04:26 +0000403static PyObject *
404posix_error_with_allocated_filename(char* name)
405{
406 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
407 PyMem_Free(name);
408 return rc;
409}
410
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000411#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000412static PyObject *
413win32_error(char* function, char* filename)
414{
Mark Hammond33a6da92000-08-15 00:46:38 +0000415 /* XXX We should pass the function name along in the future.
416 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000417 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000418 Windows error object, which is non-trivial.
419 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000420 errno = GetLastError();
421 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000422 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000423 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000424 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000425}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000426
427#ifdef Py_WIN_WIDE_FILENAMES
428static PyObject *
429win32_error_unicode(char* function, Py_UNICODE* filename)
430{
431 /* XXX - see win32_error for comments on 'function' */
432 errno = GetLastError();
433 if (filename)
434 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
435 else
436 return PyErr_SetFromWindowsErr(errno);
437}
438
439static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
440{
441 /* XXX Perhaps we should make this API an alias of
442 PyObject_Unicode() instead ?! */
443 if (PyUnicode_CheckExact(obj)) {
444 Py_INCREF(obj);
445 return obj;
446 }
447 if (PyUnicode_Check(obj)) {
448 /* For a Unicode subtype that's not a Unicode object,
449 return a true Unicode object with the same data. */
450 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
451 PyUnicode_GET_SIZE(obj));
452 }
Tim Peters11b23062003-04-23 02:39:17 +0000453 return PyUnicode_FromEncodedObject(obj,
454 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000455 "strict");
456}
457
458#endif /* Py_WIN_WIDE_FILENAMES */
459
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000460#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461
Guido van Rossumd48f2521997-12-05 22:19:34 +0000462#if defined(PYOS_OS2)
463/**********************************************************************
464 * Helper Function to Trim and Format OS/2 Messages
465 **********************************************************************/
466 static void
467os2_formatmsg(char *msgbuf, int msglen, char *reason)
468{
469 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
470
471 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
472 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
473
474 while (lastc > msgbuf && isspace(*lastc))
475 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
476 }
477
478 /* Add Optional Reason Text */
479 if (reason) {
480 strcat(msgbuf, " : ");
481 strcat(msgbuf, reason);
482 }
483}
484
485/**********************************************************************
486 * Decode an OS/2 Operating System Error Code
487 *
488 * A convenience function to lookup an OS/2 error code and return a
489 * text message we can use to raise a Python exception.
490 *
491 * Notes:
492 * The messages for errors returned from the OS/2 kernel reside in
493 * the file OSO001.MSG in the \OS2 directory hierarchy.
494 *
495 **********************************************************************/
496 static char *
497os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
498{
499 APIRET rc;
500 ULONG msglen;
501
502 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
503 Py_BEGIN_ALLOW_THREADS
504 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
505 errorcode, "oso001.msg", &msglen);
506 Py_END_ALLOW_THREADS
507
508 if (rc == NO_ERROR)
509 os2_formatmsg(msgbuf, msglen, reason);
510 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000511 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000512 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000513
514 return msgbuf;
515}
516
517/* Set an OS/2-specific error and return NULL. OS/2 kernel
518 errors are not in a global variable e.g. 'errno' nor are
519 they congruent with posix error numbers. */
520
521static PyObject * os2_error(int code)
522{
523 char text[1024];
524 PyObject *v;
525
526 os2_strerror(text, sizeof(text), code, "");
527
528 v = Py_BuildValue("(is)", code, text);
529 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000530 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000531 Py_DECREF(v);
532 }
533 return NULL; /* Signal to Python that an Exception is Pending */
534}
535
536#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537
538/* POSIX generic methods */
539
Barry Warsaw53699e91996-12-10 23:23:01 +0000540static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000541posix_fildes(PyObject *fdobj, int (*func)(int))
542{
543 int fd;
544 int res;
545 fd = PyObject_AsFileDescriptor(fdobj);
546 if (fd < 0)
547 return NULL;
548 Py_BEGIN_ALLOW_THREADS
549 res = (*func)(fd);
550 Py_END_ALLOW_THREADS
551 if (res < 0)
552 return posix_error();
553 Py_INCREF(Py_None);
554 return Py_None;
555}
Guido van Rossum21142a01999-01-08 21:05:37 +0000556
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000558static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000559unicode_file_names(void)
560{
561 static int canusewide = -1;
562 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000563 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000564 the Windows NT family. */
565 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
566 }
567 return canusewide;
568}
569#endif
Tim Peters11b23062003-04-23 02:39:17 +0000570
Guido van Rossum21142a01999-01-08 21:05:37 +0000571static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000572posix_1str(PyObject *args, char *format, int (*func)(const char*),
573 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574{
Mark Hammondef8b6542001-05-13 08:04:26 +0000575 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000576 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000577#ifdef Py_WIN_WIDE_FILENAMES
578 if (unicode_file_names()) {
579 PyUnicodeObject *po;
580 if (PyArg_ParseTuple(args, wformat, &po)) {
581 Py_BEGIN_ALLOW_THREADS
582 /* PyUnicode_AS_UNICODE OK without thread
583 lock as it is a simple dereference. */
584 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
585 Py_END_ALLOW_THREADS
586 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000587 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000588 Py_INCREF(Py_None);
589 return Py_None;
590 }
591 /* Drop the argument parsing error as narrow
592 strings are also valid. */
593 PyErr_Clear();
594 }
595#else
596 /* Platforms that don't support Unicode filenames
597 shouldn't be passing these extra params */
598 assert(wformat==NULL && wfunc == NULL);
599#endif
600
Tim Peters5aa91602002-01-30 05:46:57 +0000601 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000602 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000603 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000604 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000605 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000606 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000608 return posix_error_with_allocated_filename(path1);
609 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000610 Py_INCREF(Py_None);
611 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612}
613
Barry Warsaw53699e91996-12-10 23:23:01 +0000614static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000615posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000616 char *format,
617 int (*func)(const char *, const char *),
618 char *wformat,
619 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620{
Mark Hammondef8b6542001-05-13 08:04:26 +0000621 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000622 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623#ifdef Py_WIN_WIDE_FILENAMES
624 if (unicode_file_names()) {
625 PyObject *po1;
626 PyObject *po2;
627 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
628 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
629 PyObject *wpath1;
630 PyObject *wpath2;
631 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
632 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
633 if (!wpath1 || !wpath2) {
634 Py_XDECREF(wpath1);
635 Py_XDECREF(wpath2);
636 return NULL;
637 }
638 Py_BEGIN_ALLOW_THREADS
639 /* PyUnicode_AS_UNICODE OK without thread
640 lock as it is a simple dereference. */
641 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
642 PyUnicode_AS_UNICODE(wpath2));
643 Py_END_ALLOW_THREADS
644 Py_XDECREF(wpath1);
645 Py_XDECREF(wpath2);
646 if (res != 0)
647 return posix_error();
648 Py_INCREF(Py_None);
649 return Py_None;
650 }
651 /* Else flow through as neither is Unicode. */
652 }
653 /* Drop the argument parsing error as narrow
654 strings are also valid. */
655 PyErr_Clear();
656 }
657#else
658 /* Platforms that don't support Unicode filenames
659 shouldn't be passing these extra params */
660 assert(wformat==NULL && wfunc == NULL);
661#endif
662
Mark Hammondef8b6542001-05-13 08:04:26 +0000663 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000664 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000665 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000667 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000668 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000670 PyMem_Free(path1);
671 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000672 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000673 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000675 Py_INCREF(Py_None);
676 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677}
678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000679PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000680"stat_result: Result from stat or lstat.\n\n\
681This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000682 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000683or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
684\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000685Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000686they are available as attributes only.\n\
687\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000688See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000689
690static PyStructSequence_Field stat_result_fields[] = {
691 {"st_mode", "protection bits"},
692 {"st_ino", "inode"},
693 {"st_dev", "device"},
694 {"st_nlink", "number of hard links"},
695 {"st_uid", "user ID of owner"},
696 {"st_gid", "group ID of owner"},
697 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000698 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
699 {NULL, "integer time of last access"},
700 {NULL, "integer time of last modification"},
701 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000702 {"st_atime", "time of last access"},
703 {"st_mtime", "time of last modification"},
704 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000705#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000706 {"st_blksize", "blocksize for filesystem I/O"},
707#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000708#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000709 {"st_blocks", "number of blocks allocated"},
710#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000711#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712 {"st_rdev", "device type (if inode device)"},
713#endif
714 {0}
715};
716
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000717#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000718#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000719#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000720#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000721#endif
722
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000723#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000724#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
725#else
726#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
727#endif
728
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000729#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000730#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
731#else
732#define ST_RDEV_IDX ST_BLOCKS_IDX
733#endif
734
735static PyStructSequence_Desc stat_result_desc = {
736 "stat_result", /* name */
737 stat_result__doc__, /* doc */
738 stat_result_fields,
739 10
740};
741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000742PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000743"statvfs_result: Result from statvfs or fstatvfs.\n\n\
744This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000745 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000746or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000747\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000748See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000749
750static PyStructSequence_Field statvfs_result_fields[] = {
751 {"f_bsize", },
752 {"f_frsize", },
753 {"f_blocks", },
754 {"f_bfree", },
755 {"f_bavail", },
756 {"f_files", },
757 {"f_ffree", },
758 {"f_favail", },
759 {"f_flag", },
760 {"f_namemax",},
761 {0}
762};
763
764static PyStructSequence_Desc statvfs_result_desc = {
765 "statvfs_result", /* name */
766 statvfs_result__doc__, /* doc */
767 statvfs_result_fields,
768 10
769};
770
771static PyTypeObject StatResultType;
772static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000773static newfunc structseq_new;
774
775static PyObject *
776statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
777{
778 PyStructSequence *result;
779 int i;
780
781 result = (PyStructSequence*)structseq_new(type, args, kwds);
782 if (!result)
783 return NULL;
784 /* If we have been initialized from a tuple,
785 st_?time might be set to None. Initialize it
786 from the int slots. */
787 for (i = 7; i <= 9; i++) {
788 if (result->ob_item[i+3] == Py_None) {
789 Py_DECREF(Py_None);
790 Py_INCREF(result->ob_item[i]);
791 result->ob_item[i+3] = result->ob_item[i];
792 }
793 }
794 return (PyObject*)result;
795}
796
797
798
799/* If true, st_?time is float. */
800static int _stat_float_times = 0;
801
802PyDoc_STRVAR(stat_float_times__doc__,
803"stat_float_times([newval]) -> oldval\n\n\
804Determine whether os.[lf]stat represents time stamps as float objects.\n\
805If newval is True, future calls to stat() return floats, if it is False,\n\
806future calls return ints. \n\
807If newval is omitted, return the current setting.\n");
808
809static PyObject*
810stat_float_times(PyObject* self, PyObject *args)
811{
812 int newval = -1;
813 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
814 return NULL;
815 if (newval == -1)
816 /* Return old value */
817 return PyBool_FromLong(_stat_float_times);
818 _stat_float_times = newval;
819 Py_INCREF(Py_None);
820 return Py_None;
821}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000822
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000823static void
824fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
825{
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000826 PyObject *fval,*ival;
827#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000828 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000829#else
830 ival = PyInt_FromLong((long)sec);
831#endif
832 if (_stat_float_times) {
833 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
834 } else {
835 fval = ival;
836 Py_INCREF(fval);
837 }
838 PyStructSequence_SET_ITEM(v, index, ival);
839 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000840}
841
Tim Peters5aa91602002-01-30 05:46:57 +0000842/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +0000843 (used by posix_stat() and posix_fstat()) */
844static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000845_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000846{
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000847 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000848 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +0000849 if (v == NULL)
850 return NULL;
851
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000852 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
Fred Drake699f3522000-06-29 21:12:41 +0000853#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000854 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000855 PyLong_FromLongLong((PY_LONG_LONG)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000856#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000857 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000858#endif
859#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +0000860 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000861 PyLong_FromLongLong((PY_LONG_LONG)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000862#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000863 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000864#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000865 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
866 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
867 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
Fred Drake699f3522000-06-29 21:12:41 +0000868#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000869 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000870 PyLong_FromLongLong((PY_LONG_LONG)st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000871#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000872 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000873#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000874
875#ifdef HAVE_STAT_TV_NSEC
876 ansec = st.st_atim.tv_nsec;
877 mnsec = st.st_mtim.tv_nsec;
878 cnsec = st.st_ctim.tv_nsec;
Fred Drake699f3522000-06-29 21:12:41 +0000879#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000880 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000881#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000882 fill_time(v, 7, st.st_atime, ansec);
883 fill_time(v, 8, st.st_mtime, mnsec);
884 fill_time(v, 9, st.st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000885
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000886#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +0000887 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000888 PyInt_FromLong((long)st.st_blksize));
889#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000890#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +0000891 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000892 PyInt_FromLong((long)st.st_blocks));
893#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000894#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000895 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
896 PyInt_FromLong((long)st.st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +0000897#endif
898
899 if (PyErr_Occurred()) {
900 Py_DECREF(v);
901 return NULL;
902 }
903
904 return v;
905}
906
Barry Warsaw53699e91996-12-10 23:23:01 +0000907static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000908posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000909 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000910#ifdef __VMS
911 int (*statfunc)(const char *, STRUCT_STAT *, ...),
912#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000913 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000914#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000915 char *wformat,
916 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917{
Fred Drake699f3522000-06-29 21:12:41 +0000918 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +0000919 char *path = NULL; /* pass this to stat; do not free() it */
920 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000921 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000922
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000923#ifdef MS_WINDOWS
Tim Peters500bd032001-12-19 19:05:01 +0000924 int pathlen;
925 char pathcopy[MAX_PATH];
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000926#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000927
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000928
929#ifdef Py_WIN_WIDE_FILENAMES
930 /* If on wide-character-capable OS see if argument
931 is Unicode and if so use wide API. */
932 if (unicode_file_names()) {
933 PyUnicodeObject *po;
934 if (PyArg_ParseTuple(args, wformat, &po)) {
935 Py_UNICODE wpath[MAX_PATH+1];
936 pathlen = wcslen(PyUnicode_AS_UNICODE(po));
937 /* the library call can blow up if the file name is too long! */
938 if (pathlen > MAX_PATH) {
939 errno = ENAMETOOLONG;
940 return posix_error();
941 }
942 wcscpy(wpath, PyUnicode_AS_UNICODE(po));
943 /* Remove trailing slash or backslash, unless it's the current
944 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
945 */
946 if (pathlen > 0 &&
947 (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) {
948 /* It does end with a slash -- exempt the root drive cases. */
949 /* XXX UNC root drives should also be exempted? */
950 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':'))
951 /* leave it alone */;
952 else {
953 /* nuke the trailing backslash */
954 wpath[pathlen-1] = L'\0';
955 }
956 }
957 Py_BEGIN_ALLOW_THREADS
958 /* PyUnicode_AS_UNICODE result OK without
959 thread lock as it is a simple dereference. */
960 res = wstatfunc(wpath, &st);
961 Py_END_ALLOW_THREADS
962 if (res != 0)
963 return posix_error_with_unicode_filename(wpath);
964 return _pystat_fromstructstat(st);
965 }
966 /* Drop the argument parsing error as narrow strings
967 are also valid. */
968 PyErr_Clear();
969 }
970#endif
971
Tim Peters5aa91602002-01-30 05:46:57 +0000972 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000973 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000974 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +0000975 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000976
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000977#ifdef MS_WINDOWS
Guido van Rossumace88ae2000-04-21 18:54:45 +0000978 pathlen = strlen(path);
979 /* the library call can blow up if the file name is too long! */
980 if (pathlen > MAX_PATH) {
Tim Peters500bd032001-12-19 19:05:01 +0000981 PyMem_Free(pathfree);
Guido van Rossumace88ae2000-04-21 18:54:45 +0000982 errno = ENAMETOOLONG;
983 return posix_error();
984 }
985
Tim Peters500bd032001-12-19 19:05:01 +0000986 /* Remove trailing slash or backslash, unless it's the current
987 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
988 */
989 if (pathlen > 0 &&
990 (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) {
991 /* It does end with a slash -- exempt the root drive cases. */
992 /* XXX UNC root drives should also be exempted? */
993 if (pathlen == 1 || (pathlen == 3 && path[1] == ':'))
994 /* leave it alone */;
995 else {
996 /* nuke the trailing backslash */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000997 strncpy(pathcopy, path, pathlen);
Tim Peters500bd032001-12-19 19:05:01 +0000998 pathcopy[pathlen-1] = '\0';
Guido van Rossumace88ae2000-04-21 18:54:45 +0000999 path = pathcopy;
1000 }
1001 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001002#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001003
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001005 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001006 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001007 if (res != 0)
Tim Peters500bd032001-12-19 19:05:01 +00001008 return posix_error_with_allocated_filename(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001009
Tim Peters500bd032001-12-19 19:05:01 +00001010 PyMem_Free(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001011 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001012}
1013
1014
1015/* POSIX methods */
1016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001018"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001019Use the real uid/gid to test for access to a path. Note that most\n\
1020operations will use the effective uid/gid, therefore this routine can\n\
1021be used in a suid/sgid environment to test if the invoking user has the\n\
1022specified access to the path. The mode argument can be F_OK to test\n\
1023existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001024
1025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001026posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001027{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001028 char *path;
1029 int mode;
1030 int res;
1031
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001032#ifdef Py_WIN_WIDE_FILENAMES
1033 if (unicode_file_names()) {
1034 PyUnicodeObject *po;
1035 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1036 Py_BEGIN_ALLOW_THREADS
1037 /* PyUnicode_AS_UNICODE OK without thread lock as
1038 it is a simple dereference. */
1039 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1040 Py_END_ALLOW_THREADS
1041 return(PyBool_FromLong(res == 0));
1042 }
1043 /* Drop the argument parsing error as narrow strings
1044 are also valid. */
1045 PyErr_Clear();
1046 }
1047#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001048 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001049 return NULL;
1050 Py_BEGIN_ALLOW_THREADS
1051 res = access(path, mode);
1052 Py_END_ALLOW_THREADS
Guido van Rossum674deb22002-09-01 15:06:28 +00001053 return(PyBool_FromLong(res == 0));
Guido van Rossum94f6f721999-01-06 18:42:14 +00001054}
1055
Guido van Rossumd371ff11999-01-25 16:12:23 +00001056#ifndef F_OK
1057#define F_OK 0
1058#endif
1059#ifndef R_OK
1060#define R_OK 4
1061#endif
1062#ifndef W_OK
1063#define W_OK 2
1064#endif
1065#ifndef X_OK
1066#define X_OK 1
1067#endif
1068
1069#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001071"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001073
1074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001075posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001076{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001077 int id;
1078 char *ret;
1079
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001080 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001081 return NULL;
1082
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001083#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001084 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001085 if (id == 0) {
1086 ret = ttyname();
1087 }
1088 else {
1089 ret = NULL;
1090 }
1091#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001092 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001093#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001094 if (ret == NULL)
1095 return(posix_error());
1096 return(PyString_FromString(ret));
1097}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001098#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001099
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001100#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001102"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001103Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001104
1105static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001106posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001107{
1108 char *ret;
1109 char buffer[L_ctermid];
1110
Greg Wardb48bc172000-03-01 21:51:56 +00001111#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001112 ret = ctermid_r(buffer);
1113#else
1114 ret = ctermid(buffer);
1115#endif
1116 if (ret == NULL)
1117 return(posix_error());
1118 return(PyString_FromString(buffer));
1119}
1120#endif
1121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001123"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001124Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001125
Barry Warsaw53699e91996-12-10 23:23:01 +00001126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001127posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001129#ifdef MS_WINDOWS
1130 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1131#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1132 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001133#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001134 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001135 NULL, NULL);
1136#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001137 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001138#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001139}
1140
Fred Drake4d1e64b2002-04-15 19:40:07 +00001141#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001143"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001144Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001146
1147static PyObject *
1148posix_fchdir(PyObject *self, PyObject *fdobj)
1149{
1150 return posix_fildes(fdobj, fchdir);
1151}
1152#endif /* HAVE_FCHDIR */
1153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001155PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001156"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001157Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001158
Barry Warsaw53699e91996-12-10 23:23:01 +00001159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001160posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161{
Mark Hammondef8b6542001-05-13 08:04:26 +00001162 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001163 int i;
1164 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001165 if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001166 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001167 return NULL;
1168 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001169 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001170 Py_END_ALLOW_THREADS
1171 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001172 return posix_error_with_allocated_filename(path);
1173 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001174 Py_INCREF(Py_None);
1175 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001176}
1177
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001178
Martin v. Löwis244edc82001-10-04 22:44:26 +00001179#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001180PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001181"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001182Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001183
1184static PyObject *
1185posix_chroot(PyObject *self, PyObject *args)
1186{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001187 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001188}
1189#endif
1190
Guido van Rossum21142a01999-01-08 21:05:37 +00001191#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001193"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001194force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001195
1196static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001197posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001198{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001199 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001200}
1201#endif /* HAVE_FSYNC */
1202
1203#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001204
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001205#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001206extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1207#endif
1208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001209PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001210"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001211force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001212 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001213
1214static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001215posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001216{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001217 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001218}
1219#endif /* HAVE_FDATASYNC */
1220
1221
Fredrik Lundh10723342000-07-10 16:38:09 +00001222#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001223PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001224"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001225Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001226
Barry Warsaw53699e91996-12-10 23:23:01 +00001227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001228posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001229{
Mark Hammondef8b6542001-05-13 08:04:26 +00001230 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001231 int uid, gid;
1232 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001233 if (!PyArg_ParseTuple(args, "etii:chown",
1234 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001235 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001236 return NULL;
1237 Py_BEGIN_ALLOW_THREADS
1238 res = chown(path, (uid_t) uid, (gid_t) gid);
1239 Py_END_ALLOW_THREADS
1240 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001241 return posix_error_with_allocated_filename(path);
1242 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001243 Py_INCREF(Py_None);
1244 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001245}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001246#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001247
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001248#ifdef HAVE_LCHOWN
1249PyDoc_STRVAR(posix_lchown__doc__,
1250"lchown(path, uid, gid)\n\n\
1251Change the owner and group id of path to the numeric uid and gid.\n\
1252This function will not follow symbolic links.");
1253
1254static PyObject *
1255posix_lchown(PyObject *self, PyObject *args)
1256{
1257 char *path = NULL;
1258 int uid, gid;
1259 int res;
1260 if (!PyArg_ParseTuple(args, "etii:lchown",
1261 Py_FileSystemDefaultEncoding, &path,
1262 &uid, &gid))
1263 return NULL;
1264 Py_BEGIN_ALLOW_THREADS
1265 res = lchown(path, (uid_t) uid, (gid_t) gid);
1266 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001267 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001268 return posix_error_with_allocated_filename(path);
1269 PyMem_Free(path);
1270 Py_INCREF(Py_None);
1271 return Py_None;
1272}
1273#endif /* HAVE_LCHOWN */
1274
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001275
Guido van Rossum36bc6801995-06-14 22:54:23 +00001276#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001277PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001278"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001279Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001280
Barry Warsaw53699e91996-12-10 23:23:01 +00001281static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001282posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001283{
1284 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001285 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001286
Barry Warsaw53699e91996-12-10 23:23:01 +00001287 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001288#if defined(PYOS_OS2) && defined(PYCC_GCC)
1289 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001290#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001291 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001292#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001293 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001294 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001295 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001296 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001298
Walter Dörwald3b918c32002-11-21 20:18:46 +00001299#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001300PyDoc_STRVAR(posix_getcwdu__doc__,
1301"getcwdu() -> path\n\n\
1302Return a unicode string representing the current working directory.");
1303
1304static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001305posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001306{
1307 char buf[1026];
1308 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001309
1310#ifdef Py_WIN_WIDE_FILENAMES
1311 if (unicode_file_names()) {
1312 wchar_t *wres;
1313 wchar_t wbuf[1026];
1314 Py_BEGIN_ALLOW_THREADS
1315 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1316 Py_END_ALLOW_THREADS
1317 if (wres == NULL)
1318 return posix_error();
1319 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1320 }
1321#endif
1322
1323 Py_BEGIN_ALLOW_THREADS
1324#if defined(PYOS_OS2) && defined(PYCC_GCC)
1325 res = _getcwd2(buf, sizeof buf);
1326#else
1327 res = getcwd(buf, sizeof buf);
1328#endif
1329 Py_END_ALLOW_THREADS
1330 if (res == NULL)
1331 return posix_error();
1332 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1333}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001334#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001335#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001336
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001337
Guido van Rossumb6775db1994-08-01 11:34:53 +00001338#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001340"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001341Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001342
Barry Warsaw53699e91996-12-10 23:23:01 +00001343static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001344posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001346 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001348#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001351PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001352"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001353Return a list containing the names of the entries in the directory.\n\
1354\n\
1355 path: path of directory to list\n\
1356\n\
1357The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001358entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001359
Barry Warsaw53699e91996-12-10 23:23:01 +00001360static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001361posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001362{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001363 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001364 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001365#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001366
Barry Warsaw53699e91996-12-10 23:23:01 +00001367 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001368 HANDLE hFindFile;
1369 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001370 /* MAX_PATH characters could mean a bigger encoded string */
1371 char namebuf[MAX_PATH*2+5];
1372 char *bufptr = namebuf;
1373 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001374
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001375#ifdef Py_WIN_WIDE_FILENAMES
1376 /* If on wide-character-capable OS see if argument
1377 is Unicode and if so use wide API. */
1378 if (unicode_file_names()) {
1379 PyUnicodeObject *po;
1380 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1381 WIN32_FIND_DATAW wFileData;
1382 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1383 Py_UNICODE wch;
1384 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1385 wnamebuf[MAX_PATH] = L'\0';
1386 len = wcslen(wnamebuf);
1387 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1388 if (wch != L'/' && wch != L'\\' && wch != L':')
1389 wnamebuf[len++] = L'/';
1390 wcscpy(wnamebuf + len, L"*.*");
1391 if ((d = PyList_New(0)) == NULL)
1392 return NULL;
1393 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1394 if (hFindFile == INVALID_HANDLE_VALUE) {
1395 errno = GetLastError();
1396 if (errno == ERROR_FILE_NOT_FOUND) {
1397 return d;
1398 }
1399 Py_DECREF(d);
1400 return win32_error_unicode("FindFirstFileW", wnamebuf);
1401 }
1402 do {
1403 if (wFileData.cFileName[0] == L'.' &&
1404 (wFileData.cFileName[1] == L'\0' ||
1405 wFileData.cFileName[1] == L'.' &&
1406 wFileData.cFileName[2] == L'\0'))
1407 continue;
1408 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1409 if (v == NULL) {
1410 Py_DECREF(d);
1411 d = NULL;
1412 break;
1413 }
1414 if (PyList_Append(d, v) != 0) {
1415 Py_DECREF(v);
1416 Py_DECREF(d);
1417 d = NULL;
1418 break;
1419 }
1420 Py_DECREF(v);
1421 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1422
1423 if (FindClose(hFindFile) == FALSE) {
1424 Py_DECREF(d);
1425 return win32_error_unicode("FindClose", wnamebuf);
1426 }
1427 return d;
1428 }
1429 /* Drop the argument parsing error as narrow strings
1430 are also valid. */
1431 PyErr_Clear();
1432 }
1433#endif
1434
Tim Peters5aa91602002-01-30 05:46:57 +00001435 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001436 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001437 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001438 if (len > 0) {
1439 char ch = namebuf[len-1];
1440 if (ch != SEP && ch != ALTSEP && ch != ':')
1441 namebuf[len++] = '/';
1442 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001443 strcpy(namebuf + len, "*.*");
1444
Barry Warsaw53699e91996-12-10 23:23:01 +00001445 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001446 return NULL;
1447
1448 hFindFile = FindFirstFile(namebuf, &FileData);
1449 if (hFindFile == INVALID_HANDLE_VALUE) {
1450 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001451 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001452 return d;
1453 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001454 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001455 }
1456 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001457 if (FileData.cFileName[0] == '.' &&
1458 (FileData.cFileName[1] == '\0' ||
1459 FileData.cFileName[1] == '.' &&
1460 FileData.cFileName[2] == '\0'))
1461 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001462 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001463 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001464 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001465 d = NULL;
1466 break;
1467 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001468 if (PyList_Append(d, v) != 0) {
1469 Py_DECREF(v);
1470 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 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001475 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1476
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001477 if (FindClose(hFindFile) == FALSE) {
1478 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001479 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001480 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001481
1482 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001483
Tim Peters0bb44a42000-09-15 07:44:49 +00001484#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001485
1486#ifndef MAX_PATH
1487#define MAX_PATH CCHMAXPATH
1488#endif
1489 char *name, *pt;
1490 int len;
1491 PyObject *d, *v;
1492 char namebuf[MAX_PATH+5];
1493 HDIR hdir = 1;
1494 ULONG srchcnt = 1;
1495 FILEFINDBUF3 ep;
1496 APIRET rc;
1497
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001498 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001499 return NULL;
1500 if (len >= MAX_PATH) {
1501 PyErr_SetString(PyExc_ValueError, "path too long");
1502 return NULL;
1503 }
1504 strcpy(namebuf, name);
1505 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001506 if (*pt == ALTSEP)
1507 *pt = SEP;
1508 if (namebuf[len-1] != SEP)
1509 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001510 strcpy(namebuf + len, "*.*");
1511
1512 if ((d = PyList_New(0)) == NULL)
1513 return NULL;
1514
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001515 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1516 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001517 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001518 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1519 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1520 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001521
1522 if (rc != NO_ERROR) {
1523 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001524 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001525 }
1526
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001527 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001528 do {
1529 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001530 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001531 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001532
1533 strcpy(namebuf, ep.achName);
1534
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001535 /* Leave Case of Name Alone -- In Native Form */
1536 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001537
1538 v = PyString_FromString(namebuf);
1539 if (v == NULL) {
1540 Py_DECREF(d);
1541 d = NULL;
1542 break;
1543 }
1544 if (PyList_Append(d, v) != 0) {
1545 Py_DECREF(v);
1546 Py_DECREF(d);
1547 d = NULL;
1548 break;
1549 }
1550 Py_DECREF(v);
1551 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1552 }
1553
1554 return d;
1555#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001556
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001557 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001558 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001559 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001560 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001561 int arg_is_unicode = 1;
1562
1563 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1564 arg_is_unicode = 0;
1565 PyErr_Clear();
1566 }
1567 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001568 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001569 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001570 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001571 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001572 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001573 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001574 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001575 return NULL;
1576 }
1577 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001578 if (ep->d_name[0] == '.' &&
1579 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001580 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001581 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001582 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001583 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001584 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001585 d = NULL;
1586 break;
1587 }
Just van Rossum46c97842003-02-25 21:42:15 +00001588#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001589 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001590 PyObject *w;
1591
1592 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001593 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001594 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001595 if (w != NULL) {
1596 Py_DECREF(v);
1597 v = w;
1598 }
1599 else {
1600 /* fall back to the original byte string, as
1601 discussed in patch #683592 */
1602 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001603 }
Just van Rossum46c97842003-02-25 21:42:15 +00001604 }
1605#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001606 if (PyList_Append(d, v) != 0) {
1607 Py_DECREF(v);
1608 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609 d = NULL;
1610 break;
1611 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001612 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001613 }
1614 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001615 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001616
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001617 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001618
Tim Peters0bb44a42000-09-15 07:44:49 +00001619#endif /* which OS */
1620} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001621
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001622#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001623/* A helper function for abspath on win32 */
1624static PyObject *
1625posix__getfullpathname(PyObject *self, PyObject *args)
1626{
1627 /* assume encoded strings wont more than double no of chars */
1628 char inbuf[MAX_PATH*2];
1629 char *inbufp = inbuf;
1630 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1631 char outbuf[MAX_PATH*2];
1632 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001633#ifdef Py_WIN_WIDE_FILENAMES
1634 if (unicode_file_names()) {
1635 PyUnicodeObject *po;
1636 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1637 Py_UNICODE woutbuf[MAX_PATH*2];
1638 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001639 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001640 sizeof(woutbuf)/sizeof(woutbuf[0]),
1641 woutbuf, &wtemp))
1642 return win32_error("GetFullPathName", "");
1643 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1644 }
1645 /* Drop the argument parsing error as narrow strings
1646 are also valid. */
1647 PyErr_Clear();
1648 }
1649#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001650 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1651 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001652 &insize))
1653 return NULL;
1654 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1655 outbuf, &temp))
1656 return win32_error("GetFullPathName", inbuf);
1657 return PyString_FromString(outbuf);
1658} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001659#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001661PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001662"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001663Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001664
Barry Warsaw53699e91996-12-10 23:23:01 +00001665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001666posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001667{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001668 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001669 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001670 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001671
1672#ifdef Py_WIN_WIDE_FILENAMES
1673 if (unicode_file_names()) {
1674 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001675 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001676 Py_BEGIN_ALLOW_THREADS
1677 /* PyUnicode_AS_UNICODE OK without thread lock as
1678 it is a simple dereference. */
1679 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1680 Py_END_ALLOW_THREADS
1681 if (res < 0)
1682 return posix_error();
1683 Py_INCREF(Py_None);
1684 return Py_None;
1685 }
1686 /* Drop the argument parsing error as narrow strings
1687 are also valid. */
1688 PyErr_Clear();
1689 }
1690#endif
1691
Tim Peters5aa91602002-01-30 05:46:57 +00001692 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001693 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001694 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001695 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001696#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001697 res = mkdir(path);
1698#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001699 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001700#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001701 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001702 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001703 return posix_error_with_allocated_filename(path);
1704 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001705 Py_INCREF(Py_None);
1706 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001707}
1708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001709
Guido van Rossumb6775db1994-08-01 11:34:53 +00001710#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001711#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1712#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1713#include <sys/resource.h>
1714#endif
1715#endif
1716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001718"nice(inc) -> new_priority\n\n\
1719Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001720
Barry Warsaw53699e91996-12-10 23:23:01 +00001721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001722posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001723{
1724 int increment, value;
1725
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001726 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001727 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001728
1729 /* There are two flavours of 'nice': one that returns the new
1730 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001731 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1732 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001733
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001734 If we are of the nice family that returns the new priority, we
1735 need to clear errno before the call, and check if errno is filled
1736 before calling posix_error() on a returnvalue of -1, because the
1737 -1 may be the actual new priority! */
1738
1739 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001740 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001741#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001742 if (value == 0)
1743 value = getpriority(PRIO_PROCESS, 0);
1744#endif
1745 if (value == -1 && errno != 0)
1746 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001747 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001748 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001749}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001750#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001754"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001755Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001756
Barry Warsaw53699e91996-12-10 23:23:01 +00001757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001758posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001759{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001760#ifdef MS_WINDOWS
1761 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1762#else
1763 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1764#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001765}
1766
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001769"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001771
Barry Warsaw53699e91996-12-10 23:23:01 +00001772static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001773posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001774{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001775#ifdef MS_WINDOWS
1776 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1777#else
1778 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1779#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001780}
1781
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001783PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001784"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001786
Barry Warsaw53699e91996-12-10 23:23:01 +00001787static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001788posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001789{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001790#ifdef MS_WINDOWS
1791 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1792#else
1793 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1794#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001795}
1796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001797
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001798#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001802
Barry Warsaw53699e91996-12-10 23:23:01 +00001803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001804posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001805{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001806 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001807 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001808 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001809 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001810 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001811 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001812 Py_END_ALLOW_THREADS
1813 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001814}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001815#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001816
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001817
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001819"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001821
Barry Warsaw53699e91996-12-10 23:23:01 +00001822static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001823posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001824{
1825 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001826 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001827 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001828 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001829 if (i < 0)
1830 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001831 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001832}
1833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001836"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001837Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001840"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001844posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef MS_WINDOWS
1847 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1848#else
1849 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1850#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001851}
1852
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001853
Guido van Rossumb6775db1994-08-01 11:34:53 +00001854#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001856"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001858
Barry Warsaw53699e91996-12-10 23:23:01 +00001859static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001860posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001861{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001862 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001863 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001864
Barry Warsaw53699e91996-12-10 23:23:01 +00001865 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001866 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001867 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001868 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001869 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001870 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001871 u.sysname,
1872 u.nodename,
1873 u.release,
1874 u.version,
1875 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001876}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001877#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001878
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001879static int
1880extract_time(PyObject *t, long* sec, long* usec)
1881{
1882 long intval;
1883 if (PyFloat_Check(t)) {
1884 double tval = PyFloat_AsDouble(t);
1885 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1886 if (!intobj)
1887 return -1;
1888 intval = PyInt_AsLong(intobj);
1889 Py_DECREF(intobj);
1890 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001891 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001892 if (*usec < 0)
1893 /* If rounding gave us a negative number,
1894 truncate. */
1895 *usec = 0;
1896 return 0;
1897 }
1898 intval = PyInt_AsLong(t);
1899 if (intval == -1 && PyErr_Occurred())
1900 return -1;
1901 *sec = intval;
1902 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001903 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001904}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001907"utime(path, (atime, utime))\n\
1908utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001909Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001911
Barry Warsaw53699e91996-12-10 23:23:01 +00001912static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001913posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001914{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001915 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001916 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001917 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001918 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001919
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001920#if defined(HAVE_UTIMES)
1921 struct timeval buf[2];
1922#define ATIME buf[0].tv_sec
1923#define MTIME buf[1].tv_sec
1924#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001925/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001926 struct utimbuf buf;
1927#define ATIME buf.actime
1928#define MTIME buf.modtime
1929#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001930#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001931 time_t buf[2];
1932#define ATIME buf[0]
1933#define MTIME buf[1]
1934#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001935#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001936
Barry Warsaw3cef8562000-05-01 16:17:24 +00001937 if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001938 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001939 if (arg == Py_None) {
1940 /* optional time values not given */
1941 Py_BEGIN_ALLOW_THREADS
1942 res = utime(path, NULL);
1943 Py_END_ALLOW_THREADS
1944 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001945 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00001946 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001947 "utime() arg 2 must be a tuple (atime, mtime)");
Barry Warsaw3cef8562000-05-01 16:17:24 +00001948 return NULL;
1949 }
1950 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001951 if (extract_time(PyTuple_GET_ITEM(arg, 0),
1952 &atime, &ausec) == -1)
1953 return NULL;
1954 if (extract_time(PyTuple_GET_ITEM(arg, 1),
1955 &mtime, &musec) == -1)
1956 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001957 ATIME = atime;
1958 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001959#ifdef HAVE_UTIMES
1960 buf[0].tv_usec = ausec;
1961 buf[1].tv_usec = musec;
1962 Py_BEGIN_ALLOW_THREADS
1963 res = utimes(path, buf);
1964 Py_END_ALLOW_THREADS
1965#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00001966 Py_BEGIN_ALLOW_THREADS
1967 res = utime(path, UTIME_ARG);
1968 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001969#endif
Barry Warsaw3cef8562000-05-01 16:17:24 +00001970 }
Guido van Rossumff4949e1992-08-05 19:58:53 +00001971 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001972 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001973 Py_INCREF(Py_None);
1974 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001975#undef UTIME_ARG
1976#undef ATIME
1977#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978}
1979
Guido van Rossum85e3b011991-06-03 12:42:10 +00001980
Guido van Rossum3b066191991-06-04 19:40:25 +00001981/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001983PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001984"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001985Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001986
Barry Warsaw53699e91996-12-10 23:23:01 +00001987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001988posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001989{
1990 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001991 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001992 return NULL;
1993 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001994 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001995}
1996
Martin v. Löwis114619e2002-10-07 06:44:21 +00001997#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
1998static void
1999free_string_array(char **array, int count)
2000{
2001 int i;
2002 for (i = 0; i < count; i++)
2003 PyMem_Free(array[i]);
2004 PyMem_DEL(array);
2005}
2006#endif
2007
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002008
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002009#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002010PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002011"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002012Execute an executable path with arguments, replacing current process.\n\
2013\n\
2014 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002016
Barry Warsaw53699e91996-12-10 23:23:01 +00002017static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002018posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002019{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002020 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002022 char **argvlist;
2023 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002024 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002025
Guido van Rossum89b33251993-10-22 14:26:06 +00002026 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002027 argv is a list or tuple of strings. */
2028
Martin v. Löwis114619e2002-10-07 06:44:21 +00002029 if (!PyArg_ParseTuple(args, "etO:execv",
2030 Py_FileSystemDefaultEncoding,
2031 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002032 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002033 if (PyList_Check(argv)) {
2034 argc = PyList_Size(argv);
2035 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002036 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002037 else if (PyTuple_Check(argv)) {
2038 argc = PyTuple_Size(argv);
2039 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002040 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002041 else {
Fred Drake661ea262000-10-24 19:57:45 +00002042 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002043 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002044 return NULL;
2045 }
2046
2047 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00002048 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002049 PyMem_Free(path);
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002050 return NULL;
2051 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002052
Barry Warsaw53699e91996-12-10 23:23:01 +00002053 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002054 if (argvlist == NULL) {
2055 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002056 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002057 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002058 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002059 if (!PyArg_Parse((*getitem)(argv, i), "et",
2060 Py_FileSystemDefaultEncoding,
2061 &argvlist[i])) {
2062 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002063 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002064 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002065 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002066 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002067
Guido van Rossum85e3b011991-06-03 12:42:10 +00002068 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002069 }
2070 argvlist[argc] = NULL;
2071
Guido van Rossumb6775db1994-08-01 11:34:53 +00002072#ifdef BAD_EXEC_PROTOTYPES
2073 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002074#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002075 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002076#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077
Guido van Rossum85e3b011991-06-03 12:42:10 +00002078 /* If we get here it's definitely an error */
2079
Martin v. Löwis114619e2002-10-07 06:44:21 +00002080 free_string_array(argvlist, argc);
2081 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002082 return posix_error();
2083}
2084
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002086PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002087"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088Execute a path with arguments and environment, replacing current process.\n\
2089\n\
2090 path: path of executable file\n\
2091 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002092 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002093
Barry Warsaw53699e91996-12-10 23:23:01 +00002094static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002095posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002096{
2097 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002098 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002099 char **argvlist;
2100 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002101 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002102 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002103 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002104 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002105
2106 /* execve has three arguments: (path, argv, env), where
2107 argv is a list or tuple of strings and env is a dictionary
2108 like posix.environ. */
2109
Martin v. Löwis114619e2002-10-07 06:44:21 +00002110 if (!PyArg_ParseTuple(args, "etOO:execve",
2111 Py_FileSystemDefaultEncoding,
2112 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002113 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002114 if (PyList_Check(argv)) {
2115 argc = PyList_Size(argv);
2116 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002117 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002118 else if (PyTuple_Check(argv)) {
2119 argc = PyTuple_Size(argv);
2120 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002121 }
2122 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002123 PyErr_SetString(PyExc_TypeError,
2124 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002125 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002126 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002127 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002128 PyErr_SetString(PyExc_TypeError,
2129 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002130 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002131 }
2132
Guido van Rossum50422b42000-04-26 20:34:28 +00002133 if (argc == 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00002134 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00002135 "execve() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002136 goto fail_0;
Guido van Rossum50422b42000-04-26 20:34:28 +00002137 }
2138
Barry Warsaw53699e91996-12-10 23:23:01 +00002139 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002140 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002141 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002142 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002143 }
2144 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002145 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002146 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002147 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002148 &argvlist[i]))
2149 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002150 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002151 goto fail_1;
2152 }
2153 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002154 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002155 argvlist[argc] = NULL;
2156
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002157 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002158 if (i < 0)
2159 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002160 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002161 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002162 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002163 goto fail_1;
2164 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002165 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002166 keys = PyMapping_Keys(env);
2167 vals = PyMapping_Values(env);
2168 if (!keys || !vals)
2169 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002170 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2171 PyErr_SetString(PyExc_TypeError,
2172 "execve(): env.keys() or env.values() is not a list");
2173 goto fail_2;
2174 }
Tim Peters5aa91602002-01-30 05:46:57 +00002175
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002176 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002177 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002178 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002179
2180 key = PyList_GetItem(keys, pos);
2181 val = PyList_GetItem(vals, pos);
2182 if (!key || !val)
2183 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002184
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002185 if (!PyArg_Parse(
2186 key,
2187 "s;execve() arg 3 contains a non-string key",
2188 &k) ||
2189 !PyArg_Parse(
2190 val,
2191 "s;execve() arg 3 contains a non-string value",
2192 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002193 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002194 goto fail_2;
2195 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002196
2197#if defined(PYOS_OS2)
2198 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2199 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2200#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002201 len = PyString_Size(key) + PyString_Size(val) + 2;
2202 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002203 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002204 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002205 goto fail_2;
2206 }
Tim Petersc8996f52001-12-03 20:41:00 +00002207 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002208 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002209#if defined(PYOS_OS2)
2210 }
2211#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002212 }
2213 envlist[envc] = 0;
2214
Guido van Rossumb6775db1994-08-01 11:34:53 +00002215
2216#ifdef BAD_EXEC_PROTOTYPES
2217 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002218#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002219 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002220#endif /* BAD_EXEC_PROTOTYPES */
Tim Peters5aa91602002-01-30 05:46:57 +00002221
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002222 /* If we get here it's definitely an error */
2223
2224 (void) posix_error();
2225
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002226 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002227 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 PyMem_DEL(envlist[envc]);
2229 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002230 fail_1:
2231 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002232 Py_XDECREF(vals);
2233 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002234 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002235 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002236 return NULL;
2237}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002238#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002240
Guido van Rossuma1065681999-01-25 23:20:23 +00002241#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002242PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002243"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002244Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002245\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002246 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002247 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002248 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002249
2250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002251posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002252{
2253 char *path;
2254 PyObject *argv;
2255 char **argvlist;
2256 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002257 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002258 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002259
2260 /* spawnv has three arguments: (mode, path, argv), where
2261 argv is a list or tuple of strings. */
2262
Martin v. Löwis114619e2002-10-07 06:44:21 +00002263 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2264 Py_FileSystemDefaultEncoding,
2265 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002266 return NULL;
2267 if (PyList_Check(argv)) {
2268 argc = PyList_Size(argv);
2269 getitem = PyList_GetItem;
2270 }
2271 else if (PyTuple_Check(argv)) {
2272 argc = PyTuple_Size(argv);
2273 getitem = PyTuple_GetItem;
2274 }
2275 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002276 PyErr_SetString(PyExc_TypeError,
2277 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002278 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002279 return NULL;
2280 }
2281
2282 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002283 if (argvlist == NULL) {
2284 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002285 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002286 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002287 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002288 if (!PyArg_Parse((*getitem)(argv, i), "et",
2289 Py_FileSystemDefaultEncoding,
2290 &argvlist[i])) {
2291 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002292 PyErr_SetString(
2293 PyExc_TypeError,
2294 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002295 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002296 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002297 }
2298 }
2299 argvlist[argc] = NULL;
2300
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002301#if defined(PYOS_OS2) && defined(PYCC_GCC)
2302 Py_BEGIN_ALLOW_THREADS
2303 spawnval = spawnv(mode, path, argvlist);
2304 Py_END_ALLOW_THREADS
2305#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002306 if (mode == _OLD_P_OVERLAY)
2307 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002308
Tim Peters25059d32001-12-07 20:35:43 +00002309 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002310 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002311 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002312#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002313
Martin v. Löwis114619e2002-10-07 06:44:21 +00002314 free_string_array(argvlist, argc);
2315 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002316
Fred Drake699f3522000-06-29 21:12:41 +00002317 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002318 return posix_error();
2319 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002320#if SIZEOF_LONG == SIZEOF_VOID_P
2321 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002322#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002323 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002324#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002325}
2326
2327
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002328PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002329"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002330Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002331\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002332 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002333 path: path of executable file\n\
2334 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002335 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002336
2337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002338posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002339{
2340 char *path;
2341 PyObject *argv, *env;
2342 char **argvlist;
2343 char **envlist;
2344 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2345 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002346 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002347 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002348 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002349
2350 /* spawnve has four arguments: (mode, path, argv, env), where
2351 argv is a list or tuple of strings and env is a dictionary
2352 like posix.environ. */
2353
Martin v. Löwis114619e2002-10-07 06:44:21 +00002354 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2355 Py_FileSystemDefaultEncoding,
2356 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002357 return NULL;
2358 if (PyList_Check(argv)) {
2359 argc = PyList_Size(argv);
2360 getitem = PyList_GetItem;
2361 }
2362 else if (PyTuple_Check(argv)) {
2363 argc = PyTuple_Size(argv);
2364 getitem = PyTuple_GetItem;
2365 }
2366 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002367 PyErr_SetString(PyExc_TypeError,
2368 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002369 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002370 }
2371 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002372 PyErr_SetString(PyExc_TypeError,
2373 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002374 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002375 }
2376
2377 argvlist = PyMem_NEW(char *, argc+1);
2378 if (argvlist == NULL) {
2379 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002380 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002381 }
2382 for (i = 0; i < argc; i++) {
2383 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002384 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002385 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002386 &argvlist[i]))
2387 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002388 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002389 goto fail_1;
2390 }
2391 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002392 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002393 argvlist[argc] = NULL;
2394
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002395 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002396 if (i < 0)
2397 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002398 envlist = PyMem_NEW(char *, i + 1);
2399 if (envlist == NULL) {
2400 PyErr_NoMemory();
2401 goto fail_1;
2402 }
2403 envc = 0;
2404 keys = PyMapping_Keys(env);
2405 vals = PyMapping_Values(env);
2406 if (!keys || !vals)
2407 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002408 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2409 PyErr_SetString(PyExc_TypeError,
2410 "spawnve(): env.keys() or env.values() is not a list");
2411 goto fail_2;
2412 }
Tim Peters5aa91602002-01-30 05:46:57 +00002413
Guido van Rossuma1065681999-01-25 23:20:23 +00002414 for (pos = 0; pos < i; pos++) {
2415 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002416 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002417
2418 key = PyList_GetItem(keys, pos);
2419 val = PyList_GetItem(vals, pos);
2420 if (!key || !val)
2421 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002422
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002423 if (!PyArg_Parse(
2424 key,
2425 "s;spawnve() arg 3 contains a non-string key",
2426 &k) ||
2427 !PyArg_Parse(
2428 val,
2429 "s;spawnve() arg 3 contains a non-string value",
2430 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002431 {
2432 goto fail_2;
2433 }
Tim Petersc8996f52001-12-03 20:41:00 +00002434 len = PyString_Size(key) + PyString_Size(val) + 2;
2435 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002436 if (p == NULL) {
2437 PyErr_NoMemory();
2438 goto fail_2;
2439 }
Tim Petersc8996f52001-12-03 20:41:00 +00002440 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002441 envlist[envc++] = p;
2442 }
2443 envlist[envc] = 0;
2444
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002445#if defined(PYOS_OS2) && defined(PYCC_GCC)
2446 Py_BEGIN_ALLOW_THREADS
2447 spawnval = spawnve(mode, path, argvlist, envlist);
2448 Py_END_ALLOW_THREADS
2449#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002450 if (mode == _OLD_P_OVERLAY)
2451 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002452
2453 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002454 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002455 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002456#endif
Tim Peters25059d32001-12-07 20:35:43 +00002457
Fred Drake699f3522000-06-29 21:12:41 +00002458 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002459 (void) posix_error();
2460 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002461#if SIZEOF_LONG == SIZEOF_VOID_P
2462 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002463#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002464 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002465#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002466
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002467 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002468 while (--envc >= 0)
2469 PyMem_DEL(envlist[envc]);
2470 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002471 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002472 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002473 Py_XDECREF(vals);
2474 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002475 fail_0:
2476 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002477 return res;
2478}
2479#endif /* HAVE_SPAWNV */
2480
2481
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002482#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002483PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002484"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002485Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2486\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002487Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002488
2489static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002490posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002491{
Neal Norwitze241ce82003-02-17 18:17:05 +00002492 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002493 if (pid == -1)
2494 return posix_error();
2495 PyOS_AfterFork();
2496 return PyInt_FromLong((long)pid);
2497}
2498#endif
2499
2500
Guido van Rossumad0ee831995-03-01 10:34:45 +00002501#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002502PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002503"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002504Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002505Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002506
Barry Warsaw53699e91996-12-10 23:23:01 +00002507static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002508posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002509{
Neal Norwitze241ce82003-02-17 18:17:05 +00002510 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002511 if (pid == -1)
2512 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002513 if (pid == 0)
2514 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002515 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002516}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002517#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002518
Neal Norwitzb59798b2003-03-21 01:43:31 +00002519/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002520/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2521#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002522#define DEV_PTY_FILE "/dev/ptc"
2523#define HAVE_DEV_PTMX
2524#else
2525#define DEV_PTY_FILE "/dev/ptmx"
2526#endif
2527
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002528#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002529#ifdef HAVE_PTY_H
2530#include <pty.h>
2531#else
2532#ifdef HAVE_LIBUTIL_H
2533#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002534#endif /* HAVE_LIBUTIL_H */
2535#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002536#ifdef HAVE_STROPTS_H
2537#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002538#endif
2539#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002540
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002541#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002543"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002544Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002545
2546static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002547posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002548{
2549 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002550#ifndef HAVE_OPENPTY
2551 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002552#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002553#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002554 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002555#ifdef sun
2556 extern char *ptsname();
2557#endif
2558#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002559
Thomas Wouters70c21a12000-07-14 14:28:33 +00002560#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002561 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2562 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002563#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002564 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2565 if (slave_name == NULL)
2566 return posix_error();
2567
2568 slave_fd = open(slave_name, O_RDWR);
2569 if (slave_fd < 0)
2570 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002571#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002572 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002573 if (master_fd < 0)
2574 return posix_error();
2575 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002576 /* change permission of slave */
2577 if (grantpt(master_fd) < 0) {
2578 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002579 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002580 }
2581 /* unlock slave */
2582 if (unlockpt(master_fd) < 0) {
2583 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002584 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002585 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002586 signal(SIGCHLD, sig_saved);
2587 slave_name = ptsname(master_fd); /* get name of slave */
2588 if (slave_name == NULL)
2589 return posix_error();
2590 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2591 if (slave_fd < 0)
2592 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002593#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002594 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2595 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002596#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002597 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002598#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002599#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002600#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002601
Fred Drake8cef4cf2000-06-28 16:40:38 +00002602 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002603
Fred Drake8cef4cf2000-06-28 16:40:38 +00002604}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002605#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002606
2607#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002608PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002609"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002610Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2611Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002612To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002613
2614static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002615posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002616{
2617 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002618
Fred Drake8cef4cf2000-06-28 16:40:38 +00002619 pid = forkpty(&master_fd, NULL, NULL, NULL);
2620 if (pid == -1)
2621 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002622 if (pid == 0)
2623 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002624 return Py_BuildValue("(ii)", pid, master_fd);
2625}
2626#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002627
Guido van Rossumad0ee831995-03-01 10:34:45 +00002628#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002630"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002631Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002632
Barry Warsaw53699e91996-12-10 23:23:01 +00002633static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002634posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002635{
Barry Warsaw53699e91996-12-10 23:23:01 +00002636 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002637}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002638#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002639
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002640
Guido van Rossumad0ee831995-03-01 10:34:45 +00002641#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002642PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002643"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002644Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Barry Warsaw53699e91996-12-10 23:23:01 +00002646static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002647posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002648{
Barry Warsaw53699e91996-12-10 23:23:01 +00002649 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002650}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002651#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002652
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002653
Guido van Rossumad0ee831995-03-01 10:34:45 +00002654#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002655PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002656"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002657Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002658
Barry Warsaw53699e91996-12-10 23:23:01 +00002659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002660posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002661{
Barry Warsaw53699e91996-12-10 23:23:01 +00002662 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002663}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002664#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002667PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002668"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002669Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002670
Barry Warsaw53699e91996-12-10 23:23:01 +00002671static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002672posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002673{
Barry Warsaw53699e91996-12-10 23:23:01 +00002674 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002675}
2676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002677
Fred Drakec9680921999-12-13 16:37:25 +00002678#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002680"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00002682
2683static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002684posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00002685{
2686 PyObject *result = NULL;
2687
Fred Drakec9680921999-12-13 16:37:25 +00002688#ifdef NGROUPS_MAX
2689#define MAX_GROUPS NGROUPS_MAX
2690#else
2691 /* defined to be 16 on Solaris7, so this should be a small number */
2692#define MAX_GROUPS 64
2693#endif
2694 gid_t grouplist[MAX_GROUPS];
2695 int n;
2696
2697 n = getgroups(MAX_GROUPS, grouplist);
2698 if (n < 0)
2699 posix_error();
2700 else {
2701 result = PyList_New(n);
2702 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00002703 int i;
2704 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00002705 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00002706 if (o == NULL) {
2707 Py_DECREF(result);
2708 result = NULL;
2709 break;
2710 }
2711 PyList_SET_ITEM(result, i, o);
2712 }
2713 }
2714 }
Neal Norwitze241ce82003-02-17 18:17:05 +00002715
Fred Drakec9680921999-12-13 16:37:25 +00002716 return result;
2717}
2718#endif
2719
Martin v. Löwis606edc12002-06-13 21:09:11 +00002720#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002721PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002722"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002723Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00002724
2725static PyObject *
2726posix_getpgid(PyObject *self, PyObject *args)
2727{
2728 int pid, pgid;
2729 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
2730 return NULL;
2731 pgid = getpgid(pid);
2732 if (pgid < 0)
2733 return posix_error();
2734 return PyInt_FromLong((long)pgid);
2735}
2736#endif /* HAVE_GETPGID */
2737
2738
Guido van Rossumb6775db1994-08-01 11:34:53 +00002739#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002740PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002741"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002742Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002743
Barry Warsaw53699e91996-12-10 23:23:01 +00002744static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002745posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00002746{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002747#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00002748 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002749#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00002750 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002751#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00002752}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002753#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00002754
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002755
Guido van Rossumb6775db1994-08-01 11:34:53 +00002756#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002757PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002758"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002759Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002760
Barry Warsaw53699e91996-12-10 23:23:01 +00002761static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002762posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00002763{
Guido van Rossum64933891994-10-20 21:56:42 +00002764#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00002765 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002766#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002767 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002768#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00002769 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002770 Py_INCREF(Py_None);
2771 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002772}
2773
Guido van Rossumb6775db1994-08-01 11:34:53 +00002774#endif /* HAVE_SETPGRP */
2775
Guido van Rossumad0ee831995-03-01 10:34:45 +00002776#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002777PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002778"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002780
Barry Warsaw53699e91996-12-10 23:23:01 +00002781static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002782posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002783{
Barry Warsaw53699e91996-12-10 23:23:01 +00002784 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002785}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002786#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002788
Fred Drake12c6e2d1999-12-14 21:25:03 +00002789#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002791"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00002793
2794static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002795posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00002796{
Neal Norwitze241ce82003-02-17 18:17:05 +00002797 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00002798 char *name;
2799 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00002800
Fred Drakea30680b2000-12-06 21:24:28 +00002801 errno = 0;
2802 name = getlogin();
2803 if (name == NULL) {
2804 if (errno)
2805 posix_error();
2806 else
2807 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00002808 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00002809 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00002810 else
2811 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00002812 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00002813
Fred Drake12c6e2d1999-12-14 21:25:03 +00002814 return result;
2815}
2816#endif
2817
Guido van Rossumad0ee831995-03-01 10:34:45 +00002818#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002819PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002820"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002821Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002822
Barry Warsaw53699e91996-12-10 23:23:01 +00002823static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002824posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002825{
Barry Warsaw53699e91996-12-10 23:23:01 +00002826 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002827}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002828#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002829
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002830
Guido van Rossumad0ee831995-03-01 10:34:45 +00002831#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002832PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002833"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002834Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002835
Barry Warsaw53699e91996-12-10 23:23:01 +00002836static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002837posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002838{
2839 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002840 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002841 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002842#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002843 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
2844 APIRET rc;
2845 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002846 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002847
2848 } else if (sig == XCPT_SIGNAL_KILLPROC) {
2849 APIRET rc;
2850 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002851 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002852
2853 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002854 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002855#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00002856 if (kill(pid, sig) == -1)
2857 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002858#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002859 Py_INCREF(Py_None);
2860 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002861}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002862#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002863
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002864#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002865PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002866"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002867Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002868
2869static PyObject *
2870posix_killpg(PyObject *self, PyObject *args)
2871{
2872 int pgid, sig;
2873 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
2874 return NULL;
2875 if (killpg(pgid, sig) == -1)
2876 return posix_error();
2877 Py_INCREF(Py_None);
2878 return Py_None;
2879}
2880#endif
2881
Guido van Rossumc0125471996-06-28 18:55:32 +00002882#ifdef HAVE_PLOCK
2883
2884#ifdef HAVE_SYS_LOCK_H
2885#include <sys/lock.h>
2886#endif
2887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002888PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002889"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002890Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002891
Barry Warsaw53699e91996-12-10 23:23:01 +00002892static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002893posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00002894{
2895 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002896 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00002897 return NULL;
2898 if (plock(op) == -1)
2899 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002900 Py_INCREF(Py_None);
2901 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00002902}
2903#endif
2904
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002905
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002906#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002907PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002908"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002909Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002910
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002911#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002912#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002913static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002914async_system(const char *command)
2915{
2916 char *p, errormsg[256], args[1024];
2917 RESULTCODES rcodes;
2918 APIRET rc;
2919 char *shell = getenv("COMSPEC");
2920 if (!shell)
2921 shell = "cmd";
2922
2923 strcpy(args, shell);
2924 p = &args[ strlen(args)+1 ];
2925 strcpy(p, "/c ");
2926 strcat(p, command);
2927 p += strlen(p) + 1;
2928 *p = '\0';
2929
2930 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002931 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002932 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002933 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002934 &rcodes, shell);
2935 return rc;
2936}
2937
Guido van Rossumd48f2521997-12-05 22:19:34 +00002938static FILE *
2939popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002940{
2941 HFILE rhan, whan;
2942 FILE *retfd = NULL;
2943 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2944
Guido van Rossumd48f2521997-12-05 22:19:34 +00002945 if (rc != NO_ERROR) {
2946 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002947 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002948 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002949
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002950 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2951 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002952
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002953 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2954 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002955
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002956 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
2957 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002958
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002959 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002960 }
2961
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002962 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
2963 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002964
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002965 if (rc == NO_ERROR)
2966 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
2967
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002968 close(oldfd); /* And Close Saved STDOUT Handle */
2969 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002970
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002971 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
2972 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002973
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002974 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2975 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002976
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002977 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
2978 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002979
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002980 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002981 }
2982
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002983 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
2984 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002985
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002986 if (rc == NO_ERROR)
2987 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
2988
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002989 close(oldfd); /* And Close Saved STDIN Handle */
2990 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002991
Guido van Rossumd48f2521997-12-05 22:19:34 +00002992 } else {
2993 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002994 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002995 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002996}
2997
2998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002999posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003000{
3001 char *name;
3002 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003003 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003004 FILE *fp;
3005 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003006 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003007 return NULL;
3008 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003009 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003010 Py_END_ALLOW_THREADS
3011 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003012 return os2_error(err);
3013
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003014 f = PyFile_FromFile(fp, name, mode, fclose);
3015 if (f != NULL)
3016 PyFile_SetBufSize(f, bufsize);
3017 return f;
3018}
3019
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003020#elif defined(PYCC_GCC)
3021
3022/* standard posix version of popen() support */
3023static PyObject *
3024posix_popen(PyObject *self, PyObject *args)
3025{
3026 char *name;
3027 char *mode = "r";
3028 int bufsize = -1;
3029 FILE *fp;
3030 PyObject *f;
3031 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3032 return NULL;
3033 Py_BEGIN_ALLOW_THREADS
3034 fp = popen(name, mode);
3035 Py_END_ALLOW_THREADS
3036 if (fp == NULL)
3037 return posix_error();
3038 f = PyFile_FromFile(fp, name, mode, pclose);
3039 if (f != NULL)
3040 PyFile_SetBufSize(f, bufsize);
3041 return f;
3042}
3043
3044/* fork() under OS/2 has lots'o'warts
3045 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3046 * most of this code is a ripoff of the win32 code, but using the
3047 * capabilities of EMX's C library routines
3048 */
3049
3050/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3051#define POPEN_1 1
3052#define POPEN_2 2
3053#define POPEN_3 3
3054#define POPEN_4 4
3055
3056static PyObject *_PyPopen(char *, int, int, int);
3057static int _PyPclose(FILE *file);
3058
3059/*
3060 * Internal dictionary mapping popen* file pointers to process handles,
3061 * for use when retrieving the process exit code. See _PyPclose() below
3062 * for more information on this dictionary's use.
3063 */
3064static PyObject *_PyPopenProcs = NULL;
3065
3066/* os2emx version of popen2()
3067 *
3068 * The result of this function is a pipe (file) connected to the
3069 * process's stdin, and a pipe connected to the process's stdout.
3070 */
3071
3072static PyObject *
3073os2emx_popen2(PyObject *self, PyObject *args)
3074{
3075 PyObject *f;
3076 int tm=0;
3077
3078 char *cmdstring;
3079 char *mode = "t";
3080 int bufsize = -1;
3081 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3082 return NULL;
3083
3084 if (*mode == 't')
3085 tm = O_TEXT;
3086 else if (*mode != 'b') {
3087 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3088 return NULL;
3089 } else
3090 tm = O_BINARY;
3091
3092 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3093
3094 return f;
3095}
3096
3097/*
3098 * Variation on os2emx.popen2
3099 *
3100 * The result of this function is 3 pipes - the process's stdin,
3101 * stdout and stderr
3102 */
3103
3104static PyObject *
3105os2emx_popen3(PyObject *self, PyObject *args)
3106{
3107 PyObject *f;
3108 int tm = 0;
3109
3110 char *cmdstring;
3111 char *mode = "t";
3112 int bufsize = -1;
3113 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3114 return NULL;
3115
3116 if (*mode == 't')
3117 tm = O_TEXT;
3118 else if (*mode != 'b') {
3119 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3120 return NULL;
3121 } else
3122 tm = O_BINARY;
3123
3124 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3125
3126 return f;
3127}
3128
3129/*
3130 * Variation on os2emx.popen2
3131 *
Tim Peters11b23062003-04-23 02:39:17 +00003132 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003133 * and stdout+stderr combined as a single pipe.
3134 */
3135
3136static PyObject *
3137os2emx_popen4(PyObject *self, PyObject *args)
3138{
3139 PyObject *f;
3140 int tm = 0;
3141
3142 char *cmdstring;
3143 char *mode = "t";
3144 int bufsize = -1;
3145 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3146 return NULL;
3147
3148 if (*mode == 't')
3149 tm = O_TEXT;
3150 else if (*mode != 'b') {
3151 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3152 return NULL;
3153 } else
3154 tm = O_BINARY;
3155
3156 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3157
3158 return f;
3159}
3160
3161/* a couple of structures for convenient handling of multiple
3162 * file handles and pipes
3163 */
3164struct file_ref
3165{
3166 int handle;
3167 int flags;
3168};
3169
3170struct pipe_ref
3171{
3172 int rd;
3173 int wr;
3174};
3175
3176/* The following code is derived from the win32 code */
3177
3178static PyObject *
3179_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3180{
3181 struct file_ref stdio[3];
3182 struct pipe_ref p_fd[3];
3183 FILE *p_s[3];
3184 int file_count, i, pipe_err, pipe_pid;
3185 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3186 PyObject *f, *p_f[3];
3187
3188 /* file modes for subsequent fdopen's on pipe handles */
3189 if (mode == O_TEXT)
3190 {
3191 rd_mode = "rt";
3192 wr_mode = "wt";
3193 }
3194 else
3195 {
3196 rd_mode = "rb";
3197 wr_mode = "wb";
3198 }
3199
3200 /* prepare shell references */
3201 if ((shell = getenv("EMXSHELL")) == NULL)
3202 if ((shell = getenv("COMSPEC")) == NULL)
3203 {
3204 errno = ENOENT;
3205 return posix_error();
3206 }
3207
3208 sh_name = _getname(shell);
3209 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3210 opt = "/c";
3211 else
3212 opt = "-c";
3213
3214 /* save current stdio fds + their flags, and set not inheritable */
3215 i = pipe_err = 0;
3216 while (pipe_err >= 0 && i < 3)
3217 {
3218 pipe_err = stdio[i].handle = dup(i);
3219 stdio[i].flags = fcntl(i, F_GETFD, 0);
3220 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3221 i++;
3222 }
3223 if (pipe_err < 0)
3224 {
3225 /* didn't get them all saved - clean up and bail out */
3226 int saved_err = errno;
3227 while (i-- > 0)
3228 {
3229 close(stdio[i].handle);
3230 }
3231 errno = saved_err;
3232 return posix_error();
3233 }
3234
3235 /* create pipe ends */
3236 file_count = 2;
3237 if (n == POPEN_3)
3238 file_count = 3;
3239 i = pipe_err = 0;
3240 while ((pipe_err == 0) && (i < file_count))
3241 pipe_err = pipe((int *)&p_fd[i++]);
3242 if (pipe_err < 0)
3243 {
3244 /* didn't get them all made - clean up and bail out */
3245 while (i-- > 0)
3246 {
3247 close(p_fd[i].wr);
3248 close(p_fd[i].rd);
3249 }
3250 errno = EPIPE;
3251 return posix_error();
3252 }
3253
3254 /* change the actual standard IO streams over temporarily,
3255 * making the retained pipe ends non-inheritable
3256 */
3257 pipe_err = 0;
3258
3259 /* - stdin */
3260 if (dup2(p_fd[0].rd, 0) == 0)
3261 {
3262 close(p_fd[0].rd);
3263 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3264 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3265 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3266 {
3267 close(p_fd[0].wr);
3268 pipe_err = -1;
3269 }
3270 }
3271 else
3272 {
3273 pipe_err = -1;
3274 }
3275
3276 /* - stdout */
3277 if (pipe_err == 0)
3278 {
3279 if (dup2(p_fd[1].wr, 1) == 1)
3280 {
3281 close(p_fd[1].wr);
3282 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3283 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3284 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3285 {
3286 close(p_fd[1].rd);
3287 pipe_err = -1;
3288 }
3289 }
3290 else
3291 {
3292 pipe_err = -1;
3293 }
3294 }
3295
3296 /* - stderr, as required */
3297 if (pipe_err == 0)
3298 switch (n)
3299 {
3300 case POPEN_3:
3301 {
3302 if (dup2(p_fd[2].wr, 2) == 2)
3303 {
3304 close(p_fd[2].wr);
3305 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3306 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3307 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3308 {
3309 close(p_fd[2].rd);
3310 pipe_err = -1;
3311 }
3312 }
3313 else
3314 {
3315 pipe_err = -1;
3316 }
3317 break;
3318 }
3319
3320 case POPEN_4:
3321 {
3322 if (dup2(1, 2) != 2)
3323 {
3324 pipe_err = -1;
3325 }
3326 break;
3327 }
3328 }
3329
3330 /* spawn the child process */
3331 if (pipe_err == 0)
3332 {
3333 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3334 if (pipe_pid == -1)
3335 {
3336 pipe_err = -1;
3337 }
3338 else
3339 {
3340 /* save the PID into the FILE structure
3341 * NOTE: this implementation doesn't actually
3342 * take advantage of this, but do it for
3343 * completeness - AIM Apr01
3344 */
3345 for (i = 0; i < file_count; i++)
3346 p_s[i]->_pid = pipe_pid;
3347 }
3348 }
3349
3350 /* reset standard IO to normal */
3351 for (i = 0; i < 3; i++)
3352 {
3353 dup2(stdio[i].handle, i);
3354 fcntl(i, F_SETFD, stdio[i].flags);
3355 close(stdio[i].handle);
3356 }
3357
3358 /* if any remnant problems, clean up and bail out */
3359 if (pipe_err < 0)
3360 {
3361 for (i = 0; i < 3; i++)
3362 {
3363 close(p_fd[i].rd);
3364 close(p_fd[i].wr);
3365 }
3366 errno = EPIPE;
3367 return posix_error_with_filename(cmdstring);
3368 }
3369
3370 /* build tuple of file objects to return */
3371 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3372 PyFile_SetBufSize(p_f[0], bufsize);
3373 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3374 PyFile_SetBufSize(p_f[1], bufsize);
3375 if (n == POPEN_3)
3376 {
3377 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3378 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003379 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003380 }
3381 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003382 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003383
3384 /*
3385 * Insert the files we've created into the process dictionary
3386 * all referencing the list with the process handle and the
3387 * initial number of files (see description below in _PyPclose).
3388 * Since if _PyPclose later tried to wait on a process when all
3389 * handles weren't closed, it could create a deadlock with the
3390 * child, we spend some energy here to try to ensure that we
3391 * either insert all file handles into the dictionary or none
3392 * at all. It's a little clumsy with the various popen modes
3393 * and variable number of files involved.
3394 */
3395 if (!_PyPopenProcs)
3396 {
3397 _PyPopenProcs = PyDict_New();
3398 }
3399
3400 if (_PyPopenProcs)
3401 {
3402 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3403 int ins_rc[3];
3404
3405 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3406 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3407
3408 procObj = PyList_New(2);
3409 pidObj = PyInt_FromLong((long) pipe_pid);
3410 intObj = PyInt_FromLong((long) file_count);
3411
3412 if (procObj && pidObj && intObj)
3413 {
3414 PyList_SetItem(procObj, 0, pidObj);
3415 PyList_SetItem(procObj, 1, intObj);
3416
3417 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3418 if (fileObj[0])
3419 {
3420 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3421 fileObj[0],
3422 procObj);
3423 }
3424 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3425 if (fileObj[1])
3426 {
3427 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3428 fileObj[1],
3429 procObj);
3430 }
3431 if (file_count >= 3)
3432 {
3433 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3434 if (fileObj[2])
3435 {
3436 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3437 fileObj[2],
3438 procObj);
3439 }
3440 }
3441
3442 if (ins_rc[0] < 0 || !fileObj[0] ||
3443 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3444 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3445 {
3446 /* Something failed - remove any dictionary
3447 * entries that did make it.
3448 */
3449 if (!ins_rc[0] && fileObj[0])
3450 {
3451 PyDict_DelItem(_PyPopenProcs,
3452 fileObj[0]);
3453 }
3454 if (!ins_rc[1] && fileObj[1])
3455 {
3456 PyDict_DelItem(_PyPopenProcs,
3457 fileObj[1]);
3458 }
3459 if (!ins_rc[2] && fileObj[2])
3460 {
3461 PyDict_DelItem(_PyPopenProcs,
3462 fileObj[2]);
3463 }
3464 }
3465 }
Tim Peters11b23062003-04-23 02:39:17 +00003466
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003467 /*
3468 * Clean up our localized references for the dictionary keys
3469 * and value since PyDict_SetItem will Py_INCREF any copies
3470 * that got placed in the dictionary.
3471 */
3472 Py_XDECREF(procObj);
3473 Py_XDECREF(fileObj[0]);
3474 Py_XDECREF(fileObj[1]);
3475 Py_XDECREF(fileObj[2]);
3476 }
3477
3478 /* Child is launched. */
3479 return f;
3480}
3481
3482/*
3483 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3484 * exit code for the child process and return as a result of the close.
3485 *
3486 * This function uses the _PyPopenProcs dictionary in order to map the
3487 * input file pointer to information about the process that was
3488 * originally created by the popen* call that created the file pointer.
3489 * The dictionary uses the file pointer as a key (with one entry
3490 * inserted for each file returned by the original popen* call) and a
3491 * single list object as the value for all files from a single call.
3492 * The list object contains the Win32 process handle at [0], and a file
3493 * count at [1], which is initialized to the total number of file
3494 * handles using that list.
3495 *
3496 * This function closes whichever handle it is passed, and decrements
3497 * the file count in the dictionary for the process handle pointed to
3498 * by this file. On the last close (when the file count reaches zero),
3499 * this function will wait for the child process and then return its
3500 * exit code as the result of the close() operation. This permits the
3501 * files to be closed in any order - it is always the close() of the
3502 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003503 *
3504 * NOTE: This function is currently called with the GIL released.
3505 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003506 */
3507
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003508static int _PyPclose(FILE *file)
3509{
3510 int result;
3511 int exit_code;
3512 int pipe_pid;
3513 PyObject *procObj, *pidObj, *intObj, *fileObj;
3514 int file_count;
3515#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003516 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003517#endif
3518
3519 /* Close the file handle first, to ensure it can't block the
3520 * child from exiting if it's the last handle.
3521 */
3522 result = fclose(file);
3523
3524#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003525 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003526#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003527 if (_PyPopenProcs)
3528 {
3529 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3530 (procObj = PyDict_GetItem(_PyPopenProcs,
3531 fileObj)) != NULL &&
3532 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3533 (intObj = PyList_GetItem(procObj,1)) != NULL)
3534 {
3535 pipe_pid = (int) PyInt_AsLong(pidObj);
3536 file_count = (int) PyInt_AsLong(intObj);
3537
3538 if (file_count > 1)
3539 {
3540 /* Still other files referencing process */
3541 file_count--;
3542 PyList_SetItem(procObj,1,
3543 PyInt_FromLong((long) file_count));
3544 }
3545 else
3546 {
3547 /* Last file for this process */
3548 if (result != EOF &&
3549 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3550 {
3551 /* extract exit status */
3552 if (WIFEXITED(exit_code))
3553 {
3554 result = WEXITSTATUS(exit_code);
3555 }
3556 else
3557 {
3558 errno = EPIPE;
3559 result = -1;
3560 }
3561 }
3562 else
3563 {
3564 /* Indicate failure - this will cause the file object
3565 * to raise an I/O error and translate the last
3566 * error code from errno. We do have a problem with
3567 * last errors that overlap the normal errno table,
3568 * but that's a consistent problem with the file object.
3569 */
3570 result = -1;
3571 }
3572 }
3573
3574 /* Remove this file pointer from dictionary */
3575 PyDict_DelItem(_PyPopenProcs, fileObj);
3576
3577 if (PyDict_Size(_PyPopenProcs) == 0)
3578 {
3579 Py_DECREF(_PyPopenProcs);
3580 _PyPopenProcs = NULL;
3581 }
3582
3583 } /* if object retrieval ok */
3584
3585 Py_XDECREF(fileObj);
3586 } /* if _PyPopenProcs */
3587
3588#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003589 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003590#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003591 return result;
3592}
3593
3594#endif /* PYCC_??? */
3595
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003596#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003597
3598/*
3599 * Portable 'popen' replacement for Win32.
3600 *
3601 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3602 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003603 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003604 */
3605
3606#include <malloc.h>
3607#include <io.h>
3608#include <fcntl.h>
3609
3610/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3611#define POPEN_1 1
3612#define POPEN_2 2
3613#define POPEN_3 3
3614#define POPEN_4 4
3615
3616static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003617static int _PyPclose(FILE *file);
3618
3619/*
3620 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003621 * for use when retrieving the process exit code. See _PyPclose() below
3622 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003623 */
3624static PyObject *_PyPopenProcs = NULL;
3625
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003626
3627/* popen that works from a GUI.
3628 *
3629 * The result of this function is a pipe (file) connected to the
3630 * processes stdin or stdout, depending on the requested mode.
3631 */
3632
3633static PyObject *
3634posix_popen(PyObject *self, PyObject *args)
3635{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003636 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003637 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003638
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003639 char *cmdstring;
3640 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003641 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003642 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003643 return NULL;
3644
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003645 if (*mode == 'r')
3646 tm = _O_RDONLY;
3647 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003648 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003649 return NULL;
3650 } else
3651 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003652
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003653 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003654 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003655 return NULL;
3656 }
3657
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003658 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003659 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003660 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003661 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003662 else
3663 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
3664
3665 return f;
3666}
3667
3668/* Variation on win32pipe.popen
3669 *
3670 * The result of this function is a pipe (file) connected to the
3671 * process's stdin, and a pipe connected to the process's stdout.
3672 */
3673
3674static PyObject *
3675win32_popen2(PyObject *self, PyObject *args)
3676{
3677 PyObject *f;
3678 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00003679
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003680 char *cmdstring;
3681 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003682 int bufsize = -1;
3683 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003684 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003685
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003686 if (*mode == 't')
3687 tm = _O_TEXT;
3688 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003689 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003690 return NULL;
3691 } else
3692 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003693
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003694 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003695 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003696 return NULL;
3697 }
3698
3699 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00003700
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003701 return f;
3702}
3703
3704/*
3705 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003706 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003707 * The result of this function is 3 pipes - the process's stdin,
3708 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003709 */
3710
3711static PyObject *
3712win32_popen3(PyObject *self, PyObject *args)
3713{
3714 PyObject *f;
3715 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003716
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003717 char *cmdstring;
3718 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003719 int bufsize = -1;
3720 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003721 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003722
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003723 if (*mode == 't')
3724 tm = _O_TEXT;
3725 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003726 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003727 return NULL;
3728 } else
3729 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003730
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003731 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003732 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003733 return NULL;
3734 }
3735
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003736 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00003737
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003738 return f;
3739}
3740
3741/*
3742 * Variation on win32pipe.popen
3743 *
Tim Peters5aa91602002-01-30 05:46:57 +00003744 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003745 * and stdout+stderr combined as a single pipe.
3746 */
3747
3748static PyObject *
3749win32_popen4(PyObject *self, PyObject *args)
3750{
3751 PyObject *f;
3752 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003753
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003754 char *cmdstring;
3755 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003756 int bufsize = -1;
3757 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003758 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003759
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003760 if (*mode == 't')
3761 tm = _O_TEXT;
3762 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003763 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003764 return NULL;
3765 } else
3766 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003767
3768 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003769 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003770 return NULL;
3771 }
3772
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003773 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003774
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003775 return f;
3776}
3777
Mark Hammond08501372001-01-31 07:30:29 +00003778static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00003779_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003780 HANDLE hStdin,
3781 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00003782 HANDLE hStderr,
3783 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003784{
3785 PROCESS_INFORMATION piProcInfo;
3786 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003787 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003788 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00003789 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003790 int i;
3791 int x;
3792
3793 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00003794 char *comshell;
3795
Tim Peters92e4dd82002-10-05 01:47:34 +00003796 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003797 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
3798 return x;
Tim Peters402d5982001-08-27 06:37:48 +00003799
3800 /* Explicitly check if we are using COMMAND.COM. If we are
3801 * then use the w9xpopen hack.
3802 */
3803 comshell = s1 + x;
3804 while (comshell >= s1 && *comshell != '\\')
3805 --comshell;
3806 ++comshell;
3807
3808 if (GetVersion() < 0x80000000 &&
3809 _stricmp(comshell, "command.com") != 0) {
3810 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003811 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00003812 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003813 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00003814 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003815 }
3816 else {
3817 /*
Tim Peters402d5982001-08-27 06:37:48 +00003818 * Oh gag, we're on Win9x or using COMMAND.COM. Use
3819 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003820 */
Mark Hammond08501372001-01-31 07:30:29 +00003821 char modulepath[_MAX_PATH];
3822 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003823 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
3824 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003825 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003826 x = i+1;
3827 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00003828 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00003829 strncat(modulepath,
3830 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003831 (sizeof(modulepath)/sizeof(modulepath[0]))
3832 -strlen(modulepath));
3833 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003834 /* Eeek - file-not-found - possibly an embedding
3835 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00003836 */
Tim Peters5aa91602002-01-30 05:46:57 +00003837 strncpy(modulepath,
3838 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00003839 sizeof(modulepath)/sizeof(modulepath[0]));
3840 if (modulepath[strlen(modulepath)-1] != '\\')
3841 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00003842 strncat(modulepath,
3843 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003844 (sizeof(modulepath)/sizeof(modulepath[0]))
3845 -strlen(modulepath));
3846 /* No where else to look - raise an easily identifiable
3847 error, rather than leaving Windows to report
3848 "file not found" - as the user is probably blissfully
3849 unaware this shim EXE is used, and it will confuse them.
3850 (well, it confused me for a while ;-)
3851 */
3852 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003853 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00003854 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00003855 "for popen to work with your shell "
3856 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00003857 szConsoleSpawn);
3858 return FALSE;
3859 }
3860 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003861 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00003862 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003863 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00003864
Tim Peters92e4dd82002-10-05 01:47:34 +00003865 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003866 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003867 /* To maintain correct argument passing semantics,
3868 we pass the command-line as it stands, and allow
3869 quoting to be applied. w9xpopen.exe will then
3870 use its argv vector, and re-quote the necessary
3871 args for the ultimate child process.
3872 */
Tim Peters75cdad52001-11-28 22:07:30 +00003873 PyOS_snprintf(
3874 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003875 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003876 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003877 s1,
3878 s3,
3879 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003880 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00003881 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003882 dialog:
3883 "Your program accessed mem currently in use at xxx"
3884 and a hopeful warning about the stability of your
3885 system.
3886 Cost is Ctrl+C wont kill children, but anyone
3887 who cares can have a go!
3888 */
3889 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003890 }
3891 }
3892
3893 /* Could be an else here to try cmd.exe / command.com in the path
3894 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00003895 else {
Tim Peters402d5982001-08-27 06:37:48 +00003896 PyErr_SetString(PyExc_RuntimeError,
3897 "Cannot locate a COMSPEC environment variable to "
3898 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00003899 return FALSE;
3900 }
Tim Peters5aa91602002-01-30 05:46:57 +00003901
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003902 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
3903 siStartInfo.cb = sizeof(STARTUPINFO);
3904 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
3905 siStartInfo.hStdInput = hStdin;
3906 siStartInfo.hStdOutput = hStdout;
3907 siStartInfo.hStdError = hStderr;
3908 siStartInfo.wShowWindow = SW_HIDE;
3909
3910 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003911 s2,
3912 NULL,
3913 NULL,
3914 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003915 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003916 NULL,
3917 NULL,
3918 &siStartInfo,
3919 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003920 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003921 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003922
Mark Hammondb37a3732000-08-14 04:47:33 +00003923 /* Return process handle */
3924 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003925 return TRUE;
3926 }
Tim Peters402d5982001-08-27 06:37:48 +00003927 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003928 return FALSE;
3929}
3930
3931/* The following code is based off of KB: Q190351 */
3932
3933static PyObject *
3934_PyPopen(char *cmdstring, int mode, int n)
3935{
3936 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
3937 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00003938 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00003939
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003940 SECURITY_ATTRIBUTES saAttr;
3941 BOOL fSuccess;
3942 int fd1, fd2, fd3;
3943 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00003944 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003945 PyObject *f;
3946
3947 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
3948 saAttr.bInheritHandle = TRUE;
3949 saAttr.lpSecurityDescriptor = NULL;
3950
3951 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
3952 return win32_error("CreatePipe", NULL);
3953
3954 /* Create new output read handle and the input write handle. Set
3955 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00003956 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003957 * being created. */
3958 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003959 GetCurrentProcess(), &hChildStdinWrDup, 0,
3960 FALSE,
3961 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003962 if (!fSuccess)
3963 return win32_error("DuplicateHandle", NULL);
3964
3965 /* Close the inheritable version of ChildStdin
3966 that we're using. */
3967 CloseHandle(hChildStdinWr);
3968
3969 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
3970 return win32_error("CreatePipe", NULL);
3971
3972 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003973 GetCurrentProcess(), &hChildStdoutRdDup, 0,
3974 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003975 if (!fSuccess)
3976 return win32_error("DuplicateHandle", NULL);
3977
3978 /* Close the inheritable version of ChildStdout
3979 that we're using. */
3980 CloseHandle(hChildStdoutRd);
3981
3982 if (n != POPEN_4) {
3983 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
3984 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003985 fSuccess = DuplicateHandle(GetCurrentProcess(),
3986 hChildStderrRd,
3987 GetCurrentProcess(),
3988 &hChildStderrRdDup, 0,
3989 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003990 if (!fSuccess)
3991 return win32_error("DuplicateHandle", NULL);
3992 /* Close the inheritable version of ChildStdErr that we're using. */
3993 CloseHandle(hChildStderrRd);
3994 }
Tim Peters5aa91602002-01-30 05:46:57 +00003995
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003996 switch (n) {
3997 case POPEN_1:
3998 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
3999 case _O_WRONLY | _O_TEXT:
4000 /* Case for writing to child Stdin in text mode. */
4001 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4002 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004003 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004004 PyFile_SetBufSize(f, 0);
4005 /* We don't care about these pipes anymore, so close them. */
4006 CloseHandle(hChildStdoutRdDup);
4007 CloseHandle(hChildStderrRdDup);
4008 break;
4009
4010 case _O_RDONLY | _O_TEXT:
4011 /* Case for reading from child Stdout in text mode. */
4012 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4013 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004014 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004015 PyFile_SetBufSize(f, 0);
4016 /* We don't care about these pipes anymore, so close them. */
4017 CloseHandle(hChildStdinWrDup);
4018 CloseHandle(hChildStderrRdDup);
4019 break;
4020
4021 case _O_RDONLY | _O_BINARY:
4022 /* Case for readinig from child Stdout in binary mode. */
4023 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4024 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004025 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004026 PyFile_SetBufSize(f, 0);
4027 /* We don't care about these pipes anymore, so close them. */
4028 CloseHandle(hChildStdinWrDup);
4029 CloseHandle(hChildStderrRdDup);
4030 break;
4031
4032 case _O_WRONLY | _O_BINARY:
4033 /* Case for writing to child Stdin in binary mode. */
4034 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4035 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004036 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004037 PyFile_SetBufSize(f, 0);
4038 /* We don't care about these pipes anymore, so close them. */
4039 CloseHandle(hChildStdoutRdDup);
4040 CloseHandle(hChildStderrRdDup);
4041 break;
4042 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004043 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004044 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004045
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004046 case POPEN_2:
4047 case POPEN_4:
4048 {
4049 char *m1, *m2;
4050 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004051
Tim Peters7dca21e2002-08-19 00:42:29 +00004052 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004053 m1 = "r";
4054 m2 = "w";
4055 } else {
4056 m1 = "rb";
4057 m2 = "wb";
4058 }
4059
4060 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4061 f1 = _fdopen(fd1, m2);
4062 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4063 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004064 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004065 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004066 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004067 PyFile_SetBufSize(p2, 0);
4068
4069 if (n != 4)
4070 CloseHandle(hChildStderrRdDup);
4071
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004072 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004073 Py_XDECREF(p1);
4074 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004075 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004076 break;
4077 }
Tim Peters5aa91602002-01-30 05:46:57 +00004078
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004079 case POPEN_3:
4080 {
4081 char *m1, *m2;
4082 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004083
Tim Peters7dca21e2002-08-19 00:42:29 +00004084 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004085 m1 = "r";
4086 m2 = "w";
4087 } else {
4088 m1 = "rb";
4089 m2 = "wb";
4090 }
4091
4092 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4093 f1 = _fdopen(fd1, m2);
4094 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4095 f2 = _fdopen(fd2, m1);
4096 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4097 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004098 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004099 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4100 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004101 PyFile_SetBufSize(p1, 0);
4102 PyFile_SetBufSize(p2, 0);
4103 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004104 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004105 Py_XDECREF(p1);
4106 Py_XDECREF(p2);
4107 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004108 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004109 break;
4110 }
4111 }
4112
4113 if (n == POPEN_4) {
4114 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004115 hChildStdinRd,
4116 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004117 hChildStdoutWr,
4118 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004119 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004120 }
4121 else {
4122 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004123 hChildStdinRd,
4124 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004125 hChildStderrWr,
4126 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004127 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004128 }
4129
Mark Hammondb37a3732000-08-14 04:47:33 +00004130 /*
4131 * Insert the files we've created into the process dictionary
4132 * all referencing the list with the process handle and the
4133 * initial number of files (see description below in _PyPclose).
4134 * Since if _PyPclose later tried to wait on a process when all
4135 * handles weren't closed, it could create a deadlock with the
4136 * child, we spend some energy here to try to ensure that we
4137 * either insert all file handles into the dictionary or none
4138 * at all. It's a little clumsy with the various popen modes
4139 * and variable number of files involved.
4140 */
4141 if (!_PyPopenProcs) {
4142 _PyPopenProcs = PyDict_New();
4143 }
4144
4145 if (_PyPopenProcs) {
4146 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4147 int ins_rc[3];
4148
4149 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4150 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4151
4152 procObj = PyList_New(2);
4153 hProcessObj = PyLong_FromVoidPtr(hProcess);
4154 intObj = PyInt_FromLong(file_count);
4155
4156 if (procObj && hProcessObj && intObj) {
4157 PyList_SetItem(procObj,0,hProcessObj);
4158 PyList_SetItem(procObj,1,intObj);
4159
4160 fileObj[0] = PyLong_FromVoidPtr(f1);
4161 if (fileObj[0]) {
4162 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4163 fileObj[0],
4164 procObj);
4165 }
4166 if (file_count >= 2) {
4167 fileObj[1] = PyLong_FromVoidPtr(f2);
4168 if (fileObj[1]) {
4169 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4170 fileObj[1],
4171 procObj);
4172 }
4173 }
4174 if (file_count >= 3) {
4175 fileObj[2] = PyLong_FromVoidPtr(f3);
4176 if (fileObj[2]) {
4177 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4178 fileObj[2],
4179 procObj);
4180 }
4181 }
4182
4183 if (ins_rc[0] < 0 || !fileObj[0] ||
4184 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4185 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4186 /* Something failed - remove any dictionary
4187 * entries that did make it.
4188 */
4189 if (!ins_rc[0] && fileObj[0]) {
4190 PyDict_DelItem(_PyPopenProcs,
4191 fileObj[0]);
4192 }
4193 if (!ins_rc[1] && fileObj[1]) {
4194 PyDict_DelItem(_PyPopenProcs,
4195 fileObj[1]);
4196 }
4197 if (!ins_rc[2] && fileObj[2]) {
4198 PyDict_DelItem(_PyPopenProcs,
4199 fileObj[2]);
4200 }
4201 }
4202 }
Tim Peters5aa91602002-01-30 05:46:57 +00004203
Mark Hammondb37a3732000-08-14 04:47:33 +00004204 /*
4205 * Clean up our localized references for the dictionary keys
4206 * and value since PyDict_SetItem will Py_INCREF any copies
4207 * that got placed in the dictionary.
4208 */
4209 Py_XDECREF(procObj);
4210 Py_XDECREF(fileObj[0]);
4211 Py_XDECREF(fileObj[1]);
4212 Py_XDECREF(fileObj[2]);
4213 }
4214
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004215 /* Child is launched. Close the parents copy of those pipe
4216 * handles that only the child should have open. You need to
4217 * make sure that no handles to the write end of the output pipe
4218 * are maintained in this process or else the pipe will not close
4219 * when the child process exits and the ReadFile will hang. */
4220
4221 if (!CloseHandle(hChildStdinRd))
4222 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004223
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004224 if (!CloseHandle(hChildStdoutWr))
4225 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004226
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004227 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4228 return win32_error("CloseHandle", NULL);
4229
4230 return f;
4231}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004232
4233/*
4234 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4235 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004236 *
4237 * This function uses the _PyPopenProcs dictionary in order to map the
4238 * input file pointer to information about the process that was
4239 * originally created by the popen* call that created the file pointer.
4240 * The dictionary uses the file pointer as a key (with one entry
4241 * inserted for each file returned by the original popen* call) and a
4242 * single list object as the value for all files from a single call.
4243 * The list object contains the Win32 process handle at [0], and a file
4244 * count at [1], which is initialized to the total number of file
4245 * handles using that list.
4246 *
4247 * This function closes whichever handle it is passed, and decrements
4248 * the file count in the dictionary for the process handle pointed to
4249 * by this file. On the last close (when the file count reaches zero),
4250 * this function will wait for the child process and then return its
4251 * exit code as the result of the close() operation. This permits the
4252 * files to be closed in any order - it is always the close() of the
4253 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004254 *
4255 * NOTE: This function is currently called with the GIL released.
4256 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004257 */
Tim Peters736aa322000-09-01 06:51:24 +00004258
Fredrik Lundh56055a42000-07-23 19:47:12 +00004259static int _PyPclose(FILE *file)
4260{
Fredrik Lundh20318932000-07-26 17:29:12 +00004261 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004262 DWORD exit_code;
4263 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004264 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4265 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004266#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004267 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004268#endif
4269
Fredrik Lundh20318932000-07-26 17:29:12 +00004270 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004271 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004272 */
4273 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004274#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004275 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004276#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004277 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004278 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4279 (procObj = PyDict_GetItem(_PyPopenProcs,
4280 fileObj)) != NULL &&
4281 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4282 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4283
4284 hProcess = PyLong_AsVoidPtr(hProcessObj);
4285 file_count = PyInt_AsLong(intObj);
4286
4287 if (file_count > 1) {
4288 /* Still other files referencing process */
4289 file_count--;
4290 PyList_SetItem(procObj,1,
4291 PyInt_FromLong(file_count));
4292 } else {
4293 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004294 if (result != EOF &&
4295 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4296 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004297 /* Possible truncation here in 16-bit environments, but
4298 * real exit codes are just the lower byte in any event.
4299 */
4300 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004301 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004302 /* Indicate failure - this will cause the file object
4303 * to raise an I/O error and translate the last Win32
4304 * error code from errno. We do have a problem with
4305 * last errors that overlap the normal errno table,
4306 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004307 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004308 if (result != EOF) {
4309 /* If the error wasn't from the fclose(), then
4310 * set errno for the file object error handling.
4311 */
4312 errno = GetLastError();
4313 }
4314 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004315 }
4316
4317 /* Free up the native handle at this point */
4318 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004319 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004320
Mark Hammondb37a3732000-08-14 04:47:33 +00004321 /* Remove this file pointer from dictionary */
4322 PyDict_DelItem(_PyPopenProcs, fileObj);
4323
4324 if (PyDict_Size(_PyPopenProcs) == 0) {
4325 Py_DECREF(_PyPopenProcs);
4326 _PyPopenProcs = NULL;
4327 }
4328
4329 } /* if object retrieval ok */
4330
4331 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004332 } /* if _PyPopenProcs */
4333
Tim Peters736aa322000-09-01 06:51:24 +00004334#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004335 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004336#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004337 return result;
4338}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004339
4340#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004342posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004343{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004344 char *name;
4345 char *mode = "r";
4346 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004347 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004348 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004349 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004350 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004351 /* Strip mode of binary or text modifiers */
4352 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4353 mode = "r";
4354 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4355 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004356 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004357 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004358 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004359 if (fp == NULL)
4360 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004361 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004362 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004363 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004364 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004365}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004366
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004367#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004368#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004369
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004370
Guido van Rossumb6775db1994-08-01 11:34:53 +00004371#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004372PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004373"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004374Set the current process's user id.");
4375
Barry Warsaw53699e91996-12-10 23:23:01 +00004376static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004377posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004378{
4379 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004380 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004381 return NULL;
4382 if (setuid(uid) < 0)
4383 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004384 Py_INCREF(Py_None);
4385 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004386}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004387#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004389
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004390#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004391PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004392"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004393Set the current process's effective user id.");
4394
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004395static PyObject *
4396posix_seteuid (PyObject *self, PyObject *args)
4397{
4398 int euid;
4399 if (!PyArg_ParseTuple(args, "i", &euid)) {
4400 return NULL;
4401 } else if (seteuid(euid) < 0) {
4402 return posix_error();
4403 } else {
4404 Py_INCREF(Py_None);
4405 return Py_None;
4406 }
4407}
4408#endif /* HAVE_SETEUID */
4409
4410#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004411PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004412"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004413Set the current process's effective group id.");
4414
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004415static PyObject *
4416posix_setegid (PyObject *self, PyObject *args)
4417{
4418 int egid;
4419 if (!PyArg_ParseTuple(args, "i", &egid)) {
4420 return NULL;
4421 } else if (setegid(egid) < 0) {
4422 return posix_error();
4423 } else {
4424 Py_INCREF(Py_None);
4425 return Py_None;
4426 }
4427}
4428#endif /* HAVE_SETEGID */
4429
4430#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431PyDoc_STRVAR(posix_setreuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004432"seteuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004433Set the current process's real and effective user ids.");
4434
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004435static PyObject *
4436posix_setreuid (PyObject *self, PyObject *args)
4437{
4438 int ruid, euid;
4439 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4440 return NULL;
4441 } else if (setreuid(ruid, euid) < 0) {
4442 return posix_error();
4443 } else {
4444 Py_INCREF(Py_None);
4445 return Py_None;
4446 }
4447}
4448#endif /* HAVE_SETREUID */
4449
4450#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004451PyDoc_STRVAR(posix_setregid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004452"setegid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004453Set the current process's real and effective group ids.");
4454
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004455static PyObject *
4456posix_setregid (PyObject *self, PyObject *args)
4457{
4458 int rgid, egid;
4459 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4460 return NULL;
4461 } else if (setregid(rgid, egid) < 0) {
4462 return posix_error();
4463 } else {
4464 Py_INCREF(Py_None);
4465 return Py_None;
4466 }
4467}
4468#endif /* HAVE_SETREGID */
4469
Guido van Rossumb6775db1994-08-01 11:34:53 +00004470#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004471PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004472"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004473Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004474
Barry Warsaw53699e91996-12-10 23:23:01 +00004475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004476posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004477{
4478 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004479 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004480 return NULL;
4481 if (setgid(gid) < 0)
4482 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004483 Py_INCREF(Py_None);
4484 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004485}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004486#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004487
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004488#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004489PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004490"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004491Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004492
4493static PyObject *
4494posix_setgroups(PyObject *self, PyObject *args)
4495{
4496 PyObject *groups;
4497 int i, len;
4498 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004499
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004500 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4501 return NULL;
4502 if (!PySequence_Check(groups)) {
4503 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4504 return NULL;
4505 }
4506 len = PySequence_Size(groups);
4507 if (len > MAX_GROUPS) {
4508 PyErr_SetString(PyExc_ValueError, "too many groups");
4509 return NULL;
4510 }
4511 for(i = 0; i < len; i++) {
4512 PyObject *elem;
4513 elem = PySequence_GetItem(groups, i);
4514 if (!elem)
4515 return NULL;
4516 if (!PyInt_Check(elem)) {
4517 PyErr_SetString(PyExc_TypeError,
4518 "groups must be integers");
4519 Py_DECREF(elem);
4520 return NULL;
4521 }
4522 /* XXX: check that value fits into gid_t. */
4523 grouplist[i] = PyInt_AsLong(elem);
4524 Py_DECREF(elem);
4525 }
4526
4527 if (setgroups(len, grouplist) < 0)
4528 return posix_error();
4529 Py_INCREF(Py_None);
4530 return Py_None;
4531}
4532#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004533
Guido van Rossumb6775db1994-08-01 11:34:53 +00004534#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004535PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004536"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004537Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004538
Barry Warsaw53699e91996-12-10 23:23:01 +00004539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004540posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004541{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004542 int pid, options;
4543#ifdef UNION_WAIT
4544 union wait status;
4545#define status_i (status.w_status)
4546#else
4547 int status;
4548#define status_i status
4549#endif
4550 status_i = 0;
4551
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004552 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004553 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004554 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004555 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004556 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004557 if (pid == -1)
4558 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004559 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004560 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004561}
4562
Tim Petersab034fa2002-02-01 11:27:43 +00004563#elif defined(HAVE_CWAIT)
4564
4565/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004566PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004567"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004568"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004569
4570static PyObject *
4571posix_waitpid(PyObject *self, PyObject *args)
4572{
4573 int pid, options;
4574 int status;
4575
4576 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4577 return NULL;
4578 Py_BEGIN_ALLOW_THREADS
4579 pid = _cwait(&status, pid, options);
4580 Py_END_ALLOW_THREADS
4581 if (pid == -1)
4582 return posix_error();
4583 else
4584 /* shift the status left a byte so this is more like the
4585 POSIX waitpid */
4586 return Py_BuildValue("ii", pid, status << 8);
4587}
4588#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004589
Guido van Rossumad0ee831995-03-01 10:34:45 +00004590#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004591PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004592"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004593Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004594
Barry Warsaw53699e91996-12-10 23:23:01 +00004595static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004596posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004597{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004598 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004599#ifdef UNION_WAIT
4600 union wait status;
4601#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004602#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004603 int status;
4604#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004605#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004606
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004607 status_i = 0;
4608 Py_BEGIN_ALLOW_THREADS
4609 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004610 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004611 if (pid == -1)
4612 return posix_error();
4613 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004614 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004615#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004616}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004617#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004618
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004619
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004620PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004621"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004622Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004623
Barry Warsaw53699e91996-12-10 23:23:01 +00004624static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004625posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004626{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004627#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004628 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004629#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004630#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004631 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004632#else
4633 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4634#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004635#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004636}
4637
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004638
Guido van Rossumb6775db1994-08-01 11:34:53 +00004639#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004640PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004641"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004642Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004643
Barry Warsaw53699e91996-12-10 23:23:01 +00004644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004645posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004646{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004647 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004648 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004649 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004650 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004651 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004652 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004653 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004654 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004655 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004656 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004657 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004658}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004659#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004660
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004661
Guido van Rossumb6775db1994-08-01 11:34:53 +00004662#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004663PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004664"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004665Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004666
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004667static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004668posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004669{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004670 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004671}
4672#endif /* HAVE_SYMLINK */
4673
4674
4675#ifdef HAVE_TIMES
4676#ifndef HZ
4677#define HZ 60 /* Universal constant :-) */
4678#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004679
Guido van Rossumd48f2521997-12-05 22:19:34 +00004680#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4681static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004682system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004683{
4684 ULONG value = 0;
4685
4686 Py_BEGIN_ALLOW_THREADS
4687 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4688 Py_END_ALLOW_THREADS
4689
4690 return value;
4691}
4692
4693static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004694posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004695{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004696 /* Currently Only Uptime is Provided -- Others Later */
4697 return Py_BuildValue("ddddd",
4698 (double)0 /* t.tms_utime / HZ */,
4699 (double)0 /* t.tms_stime / HZ */,
4700 (double)0 /* t.tms_cutime / HZ */,
4701 (double)0 /* t.tms_cstime / HZ */,
4702 (double)system_uptime() / 1000);
4703}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004704#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004705static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004706posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004707{
4708 struct tms t;
4709 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004710 errno = 0;
4711 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004712 if (c == (clock_t) -1)
4713 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004714 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004715 (double)t.tms_utime / HZ,
4716 (double)t.tms_stime / HZ,
4717 (double)t.tms_cutime / HZ,
4718 (double)t.tms_cstime / HZ,
4719 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004720}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004721#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004722#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004723
4724
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004725#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004726#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004727static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004728posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004729{
4730 FILETIME create, exit, kernel, user;
4731 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004732 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004733 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4734 /* The fields of a FILETIME structure are the hi and lo part
4735 of a 64-bit value expressed in 100 nanosecond units.
4736 1e7 is one second in such units; 1e-7 the inverse.
4737 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4738 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004739 return Py_BuildValue(
4740 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004741 (double)(kernel.dwHighDateTime*429.4967296 +
4742 kernel.dwLowDateTime*1e-7),
4743 (double)(user.dwHighDateTime*429.4967296 +
4744 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004745 (double)0,
4746 (double)0,
4747 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004748}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004749#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004750
4751#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004752PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004753"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004754Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004755#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004757
Guido van Rossumb6775db1994-08-01 11:34:53 +00004758#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004759PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004760"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004761Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004762
Barry Warsaw53699e91996-12-10 23:23:01 +00004763static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004764posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004765{
Guido van Rossum687dd131993-05-17 08:34:16 +00004766 if (setsid() < 0)
4767 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004768 Py_INCREF(Py_None);
4769 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004770}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004771#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004772
Guido van Rossumb6775db1994-08-01 11:34:53 +00004773#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004774PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004775"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004776Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004777
Barry Warsaw53699e91996-12-10 23:23:01 +00004778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004779posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004780{
4781 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004782 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004783 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004784 if (setpgid(pid, pgrp) < 0)
4785 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004786 Py_INCREF(Py_None);
4787 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004788}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004789#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004791
Guido van Rossumb6775db1994-08-01 11:34:53 +00004792#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004793PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004794"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004796
Barry Warsaw53699e91996-12-10 23:23:01 +00004797static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004798posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004799{
4800 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004801 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004802 return NULL;
4803 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004804 if (pgid < 0)
4805 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004806 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004807}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004808#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004809
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004810
Guido van Rossumb6775db1994-08-01 11:34:53 +00004811#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004812PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004813"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004814Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004815
Barry Warsaw53699e91996-12-10 23:23:01 +00004816static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004817posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004818{
4819 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004820 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004821 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004822 if (tcsetpgrp(fd, pgid) < 0)
4823 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004824 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004825 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004826}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004827#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004828
Guido van Rossum687dd131993-05-17 08:34:16 +00004829/* Functions acting on file descriptors */
4830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004831PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004832"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004833Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004834
Barry Warsaw53699e91996-12-10 23:23:01 +00004835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004836posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004837{
Mark Hammondef8b6542001-05-13 08:04:26 +00004838 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004839 int flag;
4840 int mode = 0777;
4841 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004842
4843#ifdef MS_WINDOWS
4844 if (unicode_file_names()) {
4845 PyUnicodeObject *po;
4846 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4847 Py_BEGIN_ALLOW_THREADS
4848 /* PyUnicode_AS_UNICODE OK without thread
4849 lock as it is a simple dereference. */
4850 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4851 Py_END_ALLOW_THREADS
4852 if (fd < 0)
4853 return posix_error();
4854 return PyInt_FromLong((long)fd);
4855 }
4856 /* Drop the argument parsing error as narrow strings
4857 are also valid. */
4858 PyErr_Clear();
4859 }
4860#endif
4861
Tim Peters5aa91602002-01-30 05:46:57 +00004862 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004863 Py_FileSystemDefaultEncoding, &file,
4864 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004865 return NULL;
4866
Barry Warsaw53699e91996-12-10 23:23:01 +00004867 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004868 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004869 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004870 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004871 return posix_error_with_allocated_filename(file);
4872 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004873 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004874}
4875
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004877PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004878"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004879Close a file descriptor (for low level IO).");
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_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004883{
4884 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004885 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004886 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004887 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004888 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004889 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004890 if (res < 0)
4891 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004892 Py_INCREF(Py_None);
4893 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004894}
4895
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004897PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004898"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899Return a duplicate of a file descriptor.");
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_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004903{
4904 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004905 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004906 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004907 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004908 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004909 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004910 if (fd < 0)
4911 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004912 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004913}
4914
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004915
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004916PyDoc_STRVAR(posix_dup2__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004917"dup2(fd, fd2)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004918Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004919
Barry Warsaw53699e91996-12-10 23:23:01 +00004920static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004921posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004922{
4923 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004924 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004925 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004926 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004927 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004928 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004929 if (res < 0)
4930 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004931 Py_INCREF(Py_None);
4932 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004933}
4934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004936PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004937"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004938Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004939
Barry Warsaw53699e91996-12-10 23:23:01 +00004940static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004941posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004942{
4943 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004944#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004945 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004946#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004947 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004948#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004949 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004950 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004951 return NULL;
4952#ifdef SEEK_SET
4953 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4954 switch (how) {
4955 case 0: how = SEEK_SET; break;
4956 case 1: how = SEEK_CUR; break;
4957 case 2: how = SEEK_END; break;
4958 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004959#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004960
4961#if !defined(HAVE_LARGEFILE_SUPPORT)
4962 pos = PyInt_AsLong(posobj);
4963#else
4964 pos = PyLong_Check(posobj) ?
4965 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4966#endif
4967 if (PyErr_Occurred())
4968 return NULL;
4969
Barry Warsaw53699e91996-12-10 23:23:01 +00004970 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004971#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004972 res = _lseeki64(fd, pos, how);
4973#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004974 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004975#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004976 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004977 if (res < 0)
4978 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004979
4980#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004981 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004982#else
4983 return PyLong_FromLongLong(res);
4984#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004985}
4986
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004988PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004989"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004990Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004991
Barry Warsaw53699e91996-12-10 23:23:01 +00004992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004993posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004994{
Guido van Rossum8bac5461996-06-11 18:38:48 +00004995 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004996 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004997 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004998 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004999 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005000 if (buffer == NULL)
5001 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005002 Py_BEGIN_ALLOW_THREADS
5003 n = read(fd, PyString_AsString(buffer), size);
5004 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005005 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005006 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005007 return posix_error();
5008 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005009 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005010 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005011 return buffer;
5012}
5013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005015PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005016"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005017Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005018
Barry Warsaw53699e91996-12-10 23:23:01 +00005019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005020posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005021{
5022 int fd, size;
5023 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005024 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005025 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005026 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005027 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005028 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005029 if (size < 0)
5030 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005031 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005032}
5033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005035PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005036"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005037Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005038
Barry Warsaw53699e91996-12-10 23:23:01 +00005039static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005040posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005041{
5042 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005043 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005044 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005045 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005046 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005047#ifdef __VMS
5048 /* on OpenVMS we must ensure that all bytes are written to the file */
5049 fsync(fd);
5050#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005051 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005052 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005053 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005054 if (res != 0)
5055 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005056
Fred Drake699f3522000-06-29 21:12:41 +00005057 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005058}
5059
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005061PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005062"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005063Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005064
Barry Warsaw53699e91996-12-10 23:23:01 +00005065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005066posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005067{
Guido van Rossum687dd131993-05-17 08:34:16 +00005068 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005069 char *mode = "r";
5070 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005071 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005072 PyObject *f;
5073 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005074 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005075
Thomas Heller1f043e22002-11-07 16:00:59 +00005076 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5077 PyErr_Format(PyExc_ValueError,
5078 "invalid file mode '%s'", mode);
5079 return NULL;
5080 }
5081
Barry Warsaw53699e91996-12-10 23:23:01 +00005082 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005083 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005084 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005085 if (fp == NULL)
5086 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005087 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005088 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005089 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005090 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005091}
5092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005093PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005094"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005095Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005096connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005097
5098static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005099posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005100{
5101 int fd;
5102 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5103 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005104 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005105}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005106
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005107#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005108PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005109"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005110Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005111
Barry Warsaw53699e91996-12-10 23:23:01 +00005112static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005113posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005114{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005115#if defined(PYOS_OS2)
5116 HFILE read, write;
5117 APIRET rc;
5118
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005119 Py_BEGIN_ALLOW_THREADS
5120 rc = DosCreatePipe( &read, &write, 4096);
5121 Py_END_ALLOW_THREADS
5122 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005123 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005124
5125 return Py_BuildValue("(ii)", read, write);
5126#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005127#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005128 int fds[2];
5129 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005130 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005131 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005132 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005133 if (res != 0)
5134 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005135 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005136#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005137 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005138 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005139 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005140 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005141 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005142 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005143 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005144 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005145 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5146 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005147 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005148#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005149#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005150}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005151#endif /* HAVE_PIPE */
5152
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005153
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005154#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005155PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005156"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005157Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005158
Barry Warsaw53699e91996-12-10 23:23:01 +00005159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005160posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005161{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005162 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005163 int mode = 0666;
5164 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005165 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005166 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005167 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005168 res = mkfifo(filename, mode);
5169 Py_END_ALLOW_THREADS
5170 if (res < 0)
5171 return posix_error();
5172 Py_INCREF(Py_None);
5173 return Py_None;
5174}
5175#endif
5176
5177
Neal Norwitz11690112002-07-30 01:08:28 +00005178#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005179PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005180"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005181Create a filesystem node (file, device special file or named pipe)\n\
5182named filename. mode specifies both the permissions to use and the\n\
5183type of node to be created, being combined (bitwise OR) with one of\n\
5184S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005185device defines the newly created device special file (probably using\n\
5186os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005187
5188
5189static PyObject *
5190posix_mknod(PyObject *self, PyObject *args)
5191{
5192 char *filename;
5193 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005194 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005195 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005196 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005197 return NULL;
5198 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005199 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005200 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005201 if (res < 0)
5202 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005203 Py_INCREF(Py_None);
5204 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005205}
5206#endif
5207
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005208#ifdef HAVE_DEVICE_MACROS
5209PyDoc_STRVAR(posix_major__doc__,
5210"major(device) -> major number\n\
5211Extracts a device major number from a raw device number.");
5212
5213static PyObject *
5214posix_major(PyObject *self, PyObject *args)
5215{
5216 int device;
5217 if (!PyArg_ParseTuple(args, "i:major", &device))
5218 return NULL;
5219 return PyInt_FromLong((long)major(device));
5220}
5221
5222PyDoc_STRVAR(posix_minor__doc__,
5223"minor(device) -> minor number\n\
5224Extracts a device minor number from a raw device number.");
5225
5226static PyObject *
5227posix_minor(PyObject *self, PyObject *args)
5228{
5229 int device;
5230 if (!PyArg_ParseTuple(args, "i:minor", &device))
5231 return NULL;
5232 return PyInt_FromLong((long)minor(device));
5233}
5234
5235PyDoc_STRVAR(posix_makedev__doc__,
5236"makedev(major, minor) -> device number\n\
5237Composes a raw device number from the major and minor device numbers.");
5238
5239static PyObject *
5240posix_makedev(PyObject *self, PyObject *args)
5241{
5242 int major, minor;
5243 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5244 return NULL;
5245 return PyInt_FromLong((long)makedev(major, minor));
5246}
5247#endif /* device macros */
5248
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005249
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005250#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005251PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005252"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005253Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005254
Barry Warsaw53699e91996-12-10 23:23:01 +00005255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005256posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005257{
5258 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005259 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005260 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005261 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005262
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005263 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005264 return NULL;
5265
5266#if !defined(HAVE_LARGEFILE_SUPPORT)
5267 length = PyInt_AsLong(lenobj);
5268#else
5269 length = PyLong_Check(lenobj) ?
5270 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5271#endif
5272 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005273 return NULL;
5274
Barry Warsaw53699e91996-12-10 23:23:01 +00005275 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005276 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005277 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005278 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005279 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005280 return NULL;
5281 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005282 Py_INCREF(Py_None);
5283 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005284}
5285#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005286
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005287#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005288PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005289"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005290Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005291
Fred Drake762e2061999-08-26 17:23:54 +00005292/* Save putenv() parameters as values here, so we can collect them when they
5293 * get re-set with another call for the same key. */
5294static PyObject *posix_putenv_garbage;
5295
Tim Peters5aa91602002-01-30 05:46:57 +00005296static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005297posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005298{
5299 char *s1, *s2;
5300 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005301 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005302 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005303
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005304 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005305 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005306
5307#if defined(PYOS_OS2)
5308 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5309 APIRET rc;
5310
Guido van Rossumd48f2521997-12-05 22:19:34 +00005311 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5312 if (rc != NO_ERROR)
5313 return os2_error(rc);
5314
5315 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5316 APIRET rc;
5317
Guido van Rossumd48f2521997-12-05 22:19:34 +00005318 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5319 if (rc != NO_ERROR)
5320 return os2_error(rc);
5321 } else {
5322#endif
5323
Fred Drake762e2061999-08-26 17:23:54 +00005324 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005325 len = strlen(s1) + strlen(s2) + 2;
5326 /* len includes space for a trailing \0; the size arg to
5327 PyString_FromStringAndSize does not count that */
5328 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005329 if (newstr == NULL)
5330 return PyErr_NoMemory();
5331 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005332 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005333 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005334 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005335 posix_error();
5336 return NULL;
5337 }
Fred Drake762e2061999-08-26 17:23:54 +00005338 /* Install the first arg and newstr in posix_putenv_garbage;
5339 * this will cause previous value to be collected. This has to
5340 * happen after the real putenv() call because the old value
5341 * was still accessible until then. */
5342 if (PyDict_SetItem(posix_putenv_garbage,
5343 PyTuple_GET_ITEM(args, 0), newstr)) {
5344 /* really not much we can do; just leak */
5345 PyErr_Clear();
5346 }
5347 else {
5348 Py_DECREF(newstr);
5349 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005350
5351#if defined(PYOS_OS2)
5352 }
5353#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005354 Py_INCREF(Py_None);
5355 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005356}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005357#endif /* putenv */
5358
Guido van Rossumc524d952001-10-19 01:31:59 +00005359#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005361"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005362Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005363
5364static PyObject *
5365posix_unsetenv(PyObject *self, PyObject *args)
5366{
5367 char *s1;
5368
5369 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5370 return NULL;
5371
5372 unsetenv(s1);
5373
5374 /* Remove the key from posix_putenv_garbage;
5375 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005376 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005377 * old value was still accessible until then.
5378 */
5379 if (PyDict_DelItem(posix_putenv_garbage,
5380 PyTuple_GET_ITEM(args, 0))) {
5381 /* really not much we can do; just leak */
5382 PyErr_Clear();
5383 }
5384
5385 Py_INCREF(Py_None);
5386 return Py_None;
5387}
5388#endif /* unsetenv */
5389
Guido van Rossumb6a47161997-09-15 22:54:34 +00005390#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005391PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005392"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005393Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005394
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005395static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005396posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005397{
5398 int code;
5399 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005400 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005401 return NULL;
5402 message = strerror(code);
5403 if (message == NULL) {
5404 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005405 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005406 return NULL;
5407 }
5408 return PyString_FromString(message);
5409}
5410#endif /* strerror */
5411
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005412
Guido van Rossumc9641791998-08-04 15:26:23 +00005413#ifdef HAVE_SYS_WAIT_H
5414
Fred Drake106c1a02002-04-23 15:58:02 +00005415#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005417"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005418Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005419
5420static PyObject *
5421posix_WCOREDUMP(PyObject *self, PyObject *args)
5422{
5423#ifdef UNION_WAIT
5424 union wait status;
5425#define status_i (status.w_status)
5426#else
5427 int status;
5428#define status_i status
5429#endif
5430 status_i = 0;
5431
5432 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5433 {
5434 return NULL;
5435 }
5436
5437 return PyBool_FromLong(WCOREDUMP(status));
5438#undef status_i
5439}
5440#endif /* WCOREDUMP */
5441
5442#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005443PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005444"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005445Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005446job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005447
5448static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005449posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005450{
5451#ifdef UNION_WAIT
5452 union wait status;
5453#define status_i (status.w_status)
5454#else
5455 int status;
5456#define status_i status
5457#endif
5458 status_i = 0;
5459
5460 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5461 {
5462 return NULL;
5463 }
5464
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005465 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005466#undef status_i
5467}
5468#endif /* WIFCONTINUED */
5469
Guido van Rossumc9641791998-08-04 15:26:23 +00005470#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005471PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005472"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005473Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005474
5475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005476posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005477{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005478#ifdef UNION_WAIT
5479 union wait status;
5480#define status_i (status.w_status)
5481#else
5482 int status;
5483#define status_i status
5484#endif
5485 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005486
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005487 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005488 {
5489 return NULL;
5490 }
Tim Peters5aa91602002-01-30 05:46:57 +00005491
Fred Drake106c1a02002-04-23 15:58:02 +00005492 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005493#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005494}
5495#endif /* WIFSTOPPED */
5496
5497#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005498PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005499"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005501
5502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005503posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005504{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005505#ifdef UNION_WAIT
5506 union wait status;
5507#define status_i (status.w_status)
5508#else
5509 int status;
5510#define status_i status
5511#endif
5512 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005513
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005514 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005515 {
5516 return NULL;
5517 }
Tim Peters5aa91602002-01-30 05:46:57 +00005518
Fred Drake106c1a02002-04-23 15:58:02 +00005519 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005520#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005521}
5522#endif /* WIFSIGNALED */
5523
5524#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005525PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005526"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005527Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005528system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005529
5530static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005531posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005532{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005533#ifdef UNION_WAIT
5534 union wait status;
5535#define status_i (status.w_status)
5536#else
5537 int status;
5538#define status_i status
5539#endif
5540 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005541
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005542 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005543 {
5544 return NULL;
5545 }
Tim Peters5aa91602002-01-30 05:46:57 +00005546
Fred Drake106c1a02002-04-23 15:58:02 +00005547 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005548#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005549}
5550#endif /* WIFEXITED */
5551
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005552#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005553PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005554"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005555Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005556
5557static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005558posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005559{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005560#ifdef UNION_WAIT
5561 union wait status;
5562#define status_i (status.w_status)
5563#else
5564 int status;
5565#define status_i status
5566#endif
5567 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005568
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005569 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005570 {
5571 return NULL;
5572 }
Tim Peters5aa91602002-01-30 05:46:57 +00005573
Guido van Rossumc9641791998-08-04 15:26:23 +00005574 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005575#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005576}
5577#endif /* WEXITSTATUS */
5578
5579#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005581"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005582Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005583value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005584
5585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005586posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005587{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005588#ifdef UNION_WAIT
5589 union wait status;
5590#define status_i (status.w_status)
5591#else
5592 int status;
5593#define status_i status
5594#endif
5595 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005596
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005597 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005598 {
5599 return NULL;
5600 }
Tim Peters5aa91602002-01-30 05:46:57 +00005601
Guido van Rossumc9641791998-08-04 15:26:23 +00005602 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005603#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005604}
5605#endif /* WTERMSIG */
5606
5607#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005608PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005609"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005610Return the signal that stopped the process that provided\n\
5611the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005612
5613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005614posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005615{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005616#ifdef UNION_WAIT
5617 union wait status;
5618#define status_i (status.w_status)
5619#else
5620 int status;
5621#define status_i status
5622#endif
5623 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005624
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005625 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005626 {
5627 return NULL;
5628 }
Tim Peters5aa91602002-01-30 05:46:57 +00005629
Guido van Rossumc9641791998-08-04 15:26:23 +00005630 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005631#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005632}
5633#endif /* WSTOPSIG */
5634
5635#endif /* HAVE_SYS_WAIT_H */
5636
5637
Guido van Rossum94f6f721999-01-06 18:42:14 +00005638#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005639#ifdef _SCO_DS
5640/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5641 needed definitions in sys/statvfs.h */
5642#define _SVID3
5643#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005644#include <sys/statvfs.h>
5645
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005646static PyObject*
5647_pystatvfs_fromstructstatvfs(struct statvfs st) {
5648 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5649 if (v == NULL)
5650 return NULL;
5651
5652#if !defined(HAVE_LARGEFILE_SUPPORT)
5653 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5654 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5655 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5656 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5657 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5658 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5659 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5660 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5661 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5662 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5663#else
5664 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5665 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005666 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005667 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005668 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005669 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005670 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005671 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005672 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005673 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005674 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005675 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005676 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005677 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005678 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5679 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5680#endif
5681
5682 return v;
5683}
5684
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005685PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005686"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005687Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005688
5689static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005690posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005691{
5692 int fd, res;
5693 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005694
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005695 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005696 return NULL;
5697 Py_BEGIN_ALLOW_THREADS
5698 res = fstatvfs(fd, &st);
5699 Py_END_ALLOW_THREADS
5700 if (res != 0)
5701 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005702
5703 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005704}
5705#endif /* HAVE_FSTATVFS */
5706
5707
5708#if defined(HAVE_STATVFS)
5709#include <sys/statvfs.h>
5710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005711PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005712"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005713Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005714
5715static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005716posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005717{
5718 char *path;
5719 int res;
5720 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005721 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005722 return NULL;
5723 Py_BEGIN_ALLOW_THREADS
5724 res = statvfs(path, &st);
5725 Py_END_ALLOW_THREADS
5726 if (res != 0)
5727 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005728
5729 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005730}
5731#endif /* HAVE_STATVFS */
5732
5733
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005734#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005735PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005736"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005737Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005738The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005739or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005740
5741static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005742posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005743{
5744 PyObject *result = NULL;
5745 char *dir = NULL;
5746 char *pfx = NULL;
5747 char *name;
5748
5749 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5750 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005751
5752 if (PyErr_Warn(PyExc_RuntimeWarning,
5753 "tempnam is a potential security risk to your program") < 0)
5754 return NULL;
5755
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005756#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005757 name = _tempnam(dir, pfx);
5758#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005759 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005760#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005761 if (name == NULL)
5762 return PyErr_NoMemory();
5763 result = PyString_FromString(name);
5764 free(name);
5765 return result;
5766}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005767#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005768
5769
5770#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005771PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005772"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005773Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005774
5775static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005776posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005777{
5778 FILE *fp;
5779
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005780 fp = tmpfile();
5781 if (fp == NULL)
5782 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005783 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005784}
5785#endif
5786
5787
5788#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005789PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005790"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005791Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005792
5793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005794posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005795{
5796 char buffer[L_tmpnam];
5797 char *name;
5798
Skip Montanaro95618b52001-08-18 18:52:10 +00005799 if (PyErr_Warn(PyExc_RuntimeWarning,
5800 "tmpnam is a potential security risk to your program") < 0)
5801 return NULL;
5802
Greg Wardb48bc172000-03-01 21:51:56 +00005803#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005804 name = tmpnam_r(buffer);
5805#else
5806 name = tmpnam(buffer);
5807#endif
5808 if (name == NULL) {
5809 PyErr_SetObject(PyExc_OSError,
5810 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005811#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005812 "unexpected NULL from tmpnam_r"
5813#else
5814 "unexpected NULL from tmpnam"
5815#endif
5816 ));
5817 return NULL;
5818 }
5819 return PyString_FromString(buffer);
5820}
5821#endif
5822
5823
Fred Drakec9680921999-12-13 16:37:25 +00005824/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5825 * It maps strings representing configuration variable names to
5826 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005827 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005828 * rarely-used constants. There are three separate tables that use
5829 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005830 *
5831 * This code is always included, even if none of the interfaces that
5832 * need it are included. The #if hackery needed to avoid it would be
5833 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005834 */
5835struct constdef {
5836 char *name;
5837 long value;
5838};
5839
Fred Drake12c6e2d1999-12-14 21:25:03 +00005840static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005841conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5842 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005843{
5844 if (PyInt_Check(arg)) {
5845 *valuep = PyInt_AS_LONG(arg);
5846 return 1;
5847 }
5848 if (PyString_Check(arg)) {
5849 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005850 size_t lo = 0;
5851 size_t mid;
5852 size_t hi = tablesize;
5853 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005854 char *confname = PyString_AS_STRING(arg);
5855 while (lo < hi) {
5856 mid = (lo + hi) / 2;
5857 cmp = strcmp(confname, table[mid].name);
5858 if (cmp < 0)
5859 hi = mid;
5860 else if (cmp > 0)
5861 lo = mid + 1;
5862 else {
5863 *valuep = table[mid].value;
5864 return 1;
5865 }
5866 }
5867 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5868 }
5869 else
5870 PyErr_SetString(PyExc_TypeError,
5871 "configuration names must be strings or integers");
5872 return 0;
5873}
5874
5875
5876#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5877static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005878#ifdef _PC_ABI_AIO_XFER_MAX
5879 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5880#endif
5881#ifdef _PC_ABI_ASYNC_IO
5882 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5883#endif
Fred Drakec9680921999-12-13 16:37:25 +00005884#ifdef _PC_ASYNC_IO
5885 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5886#endif
5887#ifdef _PC_CHOWN_RESTRICTED
5888 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5889#endif
5890#ifdef _PC_FILESIZEBITS
5891 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5892#endif
5893#ifdef _PC_LAST
5894 {"PC_LAST", _PC_LAST},
5895#endif
5896#ifdef _PC_LINK_MAX
5897 {"PC_LINK_MAX", _PC_LINK_MAX},
5898#endif
5899#ifdef _PC_MAX_CANON
5900 {"PC_MAX_CANON", _PC_MAX_CANON},
5901#endif
5902#ifdef _PC_MAX_INPUT
5903 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5904#endif
5905#ifdef _PC_NAME_MAX
5906 {"PC_NAME_MAX", _PC_NAME_MAX},
5907#endif
5908#ifdef _PC_NO_TRUNC
5909 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5910#endif
5911#ifdef _PC_PATH_MAX
5912 {"PC_PATH_MAX", _PC_PATH_MAX},
5913#endif
5914#ifdef _PC_PIPE_BUF
5915 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5916#endif
5917#ifdef _PC_PRIO_IO
5918 {"PC_PRIO_IO", _PC_PRIO_IO},
5919#endif
5920#ifdef _PC_SOCK_MAXBUF
5921 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5922#endif
5923#ifdef _PC_SYNC_IO
5924 {"PC_SYNC_IO", _PC_SYNC_IO},
5925#endif
5926#ifdef _PC_VDISABLE
5927 {"PC_VDISABLE", _PC_VDISABLE},
5928#endif
5929};
5930
Fred Drakec9680921999-12-13 16:37:25 +00005931static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005932conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005933{
5934 return conv_confname(arg, valuep, posix_constants_pathconf,
5935 sizeof(posix_constants_pathconf)
5936 / sizeof(struct constdef));
5937}
5938#endif
5939
5940#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005941PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005942"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005943Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005944If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005945
5946static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005947posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005948{
5949 PyObject *result = NULL;
5950 int name, fd;
5951
Fred Drake12c6e2d1999-12-14 21:25:03 +00005952 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5953 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005954 long limit;
5955
5956 errno = 0;
5957 limit = fpathconf(fd, name);
5958 if (limit == -1 && errno != 0)
5959 posix_error();
5960 else
5961 result = PyInt_FromLong(limit);
5962 }
5963 return result;
5964}
5965#endif
5966
5967
5968#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005969PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005970"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005971Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005972If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005973
5974static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005975posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005976{
5977 PyObject *result = NULL;
5978 int name;
5979 char *path;
5980
5981 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5982 conv_path_confname, &name)) {
5983 long limit;
5984
5985 errno = 0;
5986 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005987 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005988 if (errno == EINVAL)
5989 /* could be a path or name problem */
5990 posix_error();
5991 else
5992 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005993 }
Fred Drakec9680921999-12-13 16:37:25 +00005994 else
5995 result = PyInt_FromLong(limit);
5996 }
5997 return result;
5998}
5999#endif
6000
6001#ifdef HAVE_CONFSTR
6002static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006003#ifdef _CS_ARCHITECTURE
6004 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6005#endif
6006#ifdef _CS_HOSTNAME
6007 {"CS_HOSTNAME", _CS_HOSTNAME},
6008#endif
6009#ifdef _CS_HW_PROVIDER
6010 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6011#endif
6012#ifdef _CS_HW_SERIAL
6013 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6014#endif
6015#ifdef _CS_INITTAB_NAME
6016 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6017#endif
Fred Drakec9680921999-12-13 16:37:25 +00006018#ifdef _CS_LFS64_CFLAGS
6019 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6020#endif
6021#ifdef _CS_LFS64_LDFLAGS
6022 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6023#endif
6024#ifdef _CS_LFS64_LIBS
6025 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6026#endif
6027#ifdef _CS_LFS64_LINTFLAGS
6028 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6029#endif
6030#ifdef _CS_LFS_CFLAGS
6031 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6032#endif
6033#ifdef _CS_LFS_LDFLAGS
6034 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6035#endif
6036#ifdef _CS_LFS_LIBS
6037 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6038#endif
6039#ifdef _CS_LFS_LINTFLAGS
6040 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6041#endif
Fred Draked86ed291999-12-15 15:34:33 +00006042#ifdef _CS_MACHINE
6043 {"CS_MACHINE", _CS_MACHINE},
6044#endif
Fred Drakec9680921999-12-13 16:37:25 +00006045#ifdef _CS_PATH
6046 {"CS_PATH", _CS_PATH},
6047#endif
Fred Draked86ed291999-12-15 15:34:33 +00006048#ifdef _CS_RELEASE
6049 {"CS_RELEASE", _CS_RELEASE},
6050#endif
6051#ifdef _CS_SRPC_DOMAIN
6052 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6053#endif
6054#ifdef _CS_SYSNAME
6055 {"CS_SYSNAME", _CS_SYSNAME},
6056#endif
6057#ifdef _CS_VERSION
6058 {"CS_VERSION", _CS_VERSION},
6059#endif
Fred Drakec9680921999-12-13 16:37:25 +00006060#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6061 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6062#endif
6063#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6064 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6065#endif
6066#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6067 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6068#endif
6069#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6070 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6071#endif
6072#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6073 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6074#endif
6075#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6076 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6077#endif
6078#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6079 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6080#endif
6081#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6082 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6083#endif
6084#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6085 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6086#endif
6087#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6088 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6089#endif
6090#ifdef _CS_XBS5_LP64_OFF64_LIBS
6091 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6092#endif
6093#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6094 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6095#endif
6096#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6097 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6098#endif
6099#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6100 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6101#endif
6102#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6103 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6104#endif
6105#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6106 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6107#endif
Fred Draked86ed291999-12-15 15:34:33 +00006108#ifdef _MIPS_CS_AVAIL_PROCESSORS
6109 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6110#endif
6111#ifdef _MIPS_CS_BASE
6112 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6113#endif
6114#ifdef _MIPS_CS_HOSTID
6115 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6116#endif
6117#ifdef _MIPS_CS_HW_NAME
6118 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6119#endif
6120#ifdef _MIPS_CS_NUM_PROCESSORS
6121 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6122#endif
6123#ifdef _MIPS_CS_OSREL_MAJ
6124 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6125#endif
6126#ifdef _MIPS_CS_OSREL_MIN
6127 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6128#endif
6129#ifdef _MIPS_CS_OSREL_PATCH
6130 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6131#endif
6132#ifdef _MIPS_CS_OS_NAME
6133 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6134#endif
6135#ifdef _MIPS_CS_OS_PROVIDER
6136 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6137#endif
6138#ifdef _MIPS_CS_PROCESSORS
6139 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6140#endif
6141#ifdef _MIPS_CS_SERIAL
6142 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6143#endif
6144#ifdef _MIPS_CS_VENDOR
6145 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6146#endif
Fred Drakec9680921999-12-13 16:37:25 +00006147};
6148
6149static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006150conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006151{
6152 return conv_confname(arg, valuep, posix_constants_confstr,
6153 sizeof(posix_constants_confstr)
6154 / sizeof(struct constdef));
6155}
6156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006157PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006158"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006159Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006160
6161static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006162posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006163{
6164 PyObject *result = NULL;
6165 int name;
6166 char buffer[64];
6167
6168 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6169 int len = confstr(name, buffer, sizeof(buffer));
6170
Fred Drakec9680921999-12-13 16:37:25 +00006171 errno = 0;
6172 if (len == 0) {
6173 if (errno != 0)
6174 posix_error();
6175 else
6176 result = PyString_FromString("");
6177 }
6178 else {
6179 if (len >= sizeof(buffer)) {
6180 result = PyString_FromStringAndSize(NULL, len);
6181 if (result != NULL)
6182 confstr(name, PyString_AS_STRING(result), len+1);
6183 }
6184 else
6185 result = PyString_FromString(buffer);
6186 }
6187 }
6188 return result;
6189}
6190#endif
6191
6192
6193#ifdef HAVE_SYSCONF
6194static struct constdef posix_constants_sysconf[] = {
6195#ifdef _SC_2_CHAR_TERM
6196 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6197#endif
6198#ifdef _SC_2_C_BIND
6199 {"SC_2_C_BIND", _SC_2_C_BIND},
6200#endif
6201#ifdef _SC_2_C_DEV
6202 {"SC_2_C_DEV", _SC_2_C_DEV},
6203#endif
6204#ifdef _SC_2_C_VERSION
6205 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6206#endif
6207#ifdef _SC_2_FORT_DEV
6208 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6209#endif
6210#ifdef _SC_2_FORT_RUN
6211 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6212#endif
6213#ifdef _SC_2_LOCALEDEF
6214 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6215#endif
6216#ifdef _SC_2_SW_DEV
6217 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6218#endif
6219#ifdef _SC_2_UPE
6220 {"SC_2_UPE", _SC_2_UPE},
6221#endif
6222#ifdef _SC_2_VERSION
6223 {"SC_2_VERSION", _SC_2_VERSION},
6224#endif
Fred Draked86ed291999-12-15 15:34:33 +00006225#ifdef _SC_ABI_ASYNCHRONOUS_IO
6226 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6227#endif
6228#ifdef _SC_ACL
6229 {"SC_ACL", _SC_ACL},
6230#endif
Fred Drakec9680921999-12-13 16:37:25 +00006231#ifdef _SC_AIO_LISTIO_MAX
6232 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6233#endif
Fred Drakec9680921999-12-13 16:37:25 +00006234#ifdef _SC_AIO_MAX
6235 {"SC_AIO_MAX", _SC_AIO_MAX},
6236#endif
6237#ifdef _SC_AIO_PRIO_DELTA_MAX
6238 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6239#endif
6240#ifdef _SC_ARG_MAX
6241 {"SC_ARG_MAX", _SC_ARG_MAX},
6242#endif
6243#ifdef _SC_ASYNCHRONOUS_IO
6244 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6245#endif
6246#ifdef _SC_ATEXIT_MAX
6247 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6248#endif
Fred Draked86ed291999-12-15 15:34:33 +00006249#ifdef _SC_AUDIT
6250 {"SC_AUDIT", _SC_AUDIT},
6251#endif
Fred Drakec9680921999-12-13 16:37:25 +00006252#ifdef _SC_AVPHYS_PAGES
6253 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6254#endif
6255#ifdef _SC_BC_BASE_MAX
6256 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6257#endif
6258#ifdef _SC_BC_DIM_MAX
6259 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6260#endif
6261#ifdef _SC_BC_SCALE_MAX
6262 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6263#endif
6264#ifdef _SC_BC_STRING_MAX
6265 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6266#endif
Fred Draked86ed291999-12-15 15:34:33 +00006267#ifdef _SC_CAP
6268 {"SC_CAP", _SC_CAP},
6269#endif
Fred Drakec9680921999-12-13 16:37:25 +00006270#ifdef _SC_CHARCLASS_NAME_MAX
6271 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6272#endif
6273#ifdef _SC_CHAR_BIT
6274 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6275#endif
6276#ifdef _SC_CHAR_MAX
6277 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6278#endif
6279#ifdef _SC_CHAR_MIN
6280 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6281#endif
6282#ifdef _SC_CHILD_MAX
6283 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6284#endif
6285#ifdef _SC_CLK_TCK
6286 {"SC_CLK_TCK", _SC_CLK_TCK},
6287#endif
6288#ifdef _SC_COHER_BLKSZ
6289 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6290#endif
6291#ifdef _SC_COLL_WEIGHTS_MAX
6292 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6293#endif
6294#ifdef _SC_DCACHE_ASSOC
6295 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6296#endif
6297#ifdef _SC_DCACHE_BLKSZ
6298 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6299#endif
6300#ifdef _SC_DCACHE_LINESZ
6301 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6302#endif
6303#ifdef _SC_DCACHE_SZ
6304 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6305#endif
6306#ifdef _SC_DCACHE_TBLKSZ
6307 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6308#endif
6309#ifdef _SC_DELAYTIMER_MAX
6310 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6311#endif
6312#ifdef _SC_EQUIV_CLASS_MAX
6313 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6314#endif
6315#ifdef _SC_EXPR_NEST_MAX
6316 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6317#endif
6318#ifdef _SC_FSYNC
6319 {"SC_FSYNC", _SC_FSYNC},
6320#endif
6321#ifdef _SC_GETGR_R_SIZE_MAX
6322 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6323#endif
6324#ifdef _SC_GETPW_R_SIZE_MAX
6325 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6326#endif
6327#ifdef _SC_ICACHE_ASSOC
6328 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6329#endif
6330#ifdef _SC_ICACHE_BLKSZ
6331 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6332#endif
6333#ifdef _SC_ICACHE_LINESZ
6334 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6335#endif
6336#ifdef _SC_ICACHE_SZ
6337 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6338#endif
Fred Draked86ed291999-12-15 15:34:33 +00006339#ifdef _SC_INF
6340 {"SC_INF", _SC_INF},
6341#endif
Fred Drakec9680921999-12-13 16:37:25 +00006342#ifdef _SC_INT_MAX
6343 {"SC_INT_MAX", _SC_INT_MAX},
6344#endif
6345#ifdef _SC_INT_MIN
6346 {"SC_INT_MIN", _SC_INT_MIN},
6347#endif
6348#ifdef _SC_IOV_MAX
6349 {"SC_IOV_MAX", _SC_IOV_MAX},
6350#endif
Fred Draked86ed291999-12-15 15:34:33 +00006351#ifdef _SC_IP_SECOPTS
6352 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6353#endif
Fred Drakec9680921999-12-13 16:37:25 +00006354#ifdef _SC_JOB_CONTROL
6355 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6356#endif
Fred Draked86ed291999-12-15 15:34:33 +00006357#ifdef _SC_KERN_POINTERS
6358 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6359#endif
6360#ifdef _SC_KERN_SIM
6361 {"SC_KERN_SIM", _SC_KERN_SIM},
6362#endif
Fred Drakec9680921999-12-13 16:37:25 +00006363#ifdef _SC_LINE_MAX
6364 {"SC_LINE_MAX", _SC_LINE_MAX},
6365#endif
6366#ifdef _SC_LOGIN_NAME_MAX
6367 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6368#endif
6369#ifdef _SC_LOGNAME_MAX
6370 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6371#endif
6372#ifdef _SC_LONG_BIT
6373 {"SC_LONG_BIT", _SC_LONG_BIT},
6374#endif
Fred Draked86ed291999-12-15 15:34:33 +00006375#ifdef _SC_MAC
6376 {"SC_MAC", _SC_MAC},
6377#endif
Fred Drakec9680921999-12-13 16:37:25 +00006378#ifdef _SC_MAPPED_FILES
6379 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6380#endif
6381#ifdef _SC_MAXPID
6382 {"SC_MAXPID", _SC_MAXPID},
6383#endif
6384#ifdef _SC_MB_LEN_MAX
6385 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6386#endif
6387#ifdef _SC_MEMLOCK
6388 {"SC_MEMLOCK", _SC_MEMLOCK},
6389#endif
6390#ifdef _SC_MEMLOCK_RANGE
6391 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6392#endif
6393#ifdef _SC_MEMORY_PROTECTION
6394 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6395#endif
6396#ifdef _SC_MESSAGE_PASSING
6397 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6398#endif
Fred Draked86ed291999-12-15 15:34:33 +00006399#ifdef _SC_MMAP_FIXED_ALIGNMENT
6400 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6401#endif
Fred Drakec9680921999-12-13 16:37:25 +00006402#ifdef _SC_MQ_OPEN_MAX
6403 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6404#endif
6405#ifdef _SC_MQ_PRIO_MAX
6406 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6407#endif
Fred Draked86ed291999-12-15 15:34:33 +00006408#ifdef _SC_NACLS_MAX
6409 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6410#endif
Fred Drakec9680921999-12-13 16:37:25 +00006411#ifdef _SC_NGROUPS_MAX
6412 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6413#endif
6414#ifdef _SC_NL_ARGMAX
6415 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6416#endif
6417#ifdef _SC_NL_LANGMAX
6418 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6419#endif
6420#ifdef _SC_NL_MSGMAX
6421 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6422#endif
6423#ifdef _SC_NL_NMAX
6424 {"SC_NL_NMAX", _SC_NL_NMAX},
6425#endif
6426#ifdef _SC_NL_SETMAX
6427 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6428#endif
6429#ifdef _SC_NL_TEXTMAX
6430 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6431#endif
6432#ifdef _SC_NPROCESSORS_CONF
6433 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6434#endif
6435#ifdef _SC_NPROCESSORS_ONLN
6436 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6437#endif
Fred Draked86ed291999-12-15 15:34:33 +00006438#ifdef _SC_NPROC_CONF
6439 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6440#endif
6441#ifdef _SC_NPROC_ONLN
6442 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6443#endif
Fred Drakec9680921999-12-13 16:37:25 +00006444#ifdef _SC_NZERO
6445 {"SC_NZERO", _SC_NZERO},
6446#endif
6447#ifdef _SC_OPEN_MAX
6448 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6449#endif
6450#ifdef _SC_PAGESIZE
6451 {"SC_PAGESIZE", _SC_PAGESIZE},
6452#endif
6453#ifdef _SC_PAGE_SIZE
6454 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6455#endif
6456#ifdef _SC_PASS_MAX
6457 {"SC_PASS_MAX", _SC_PASS_MAX},
6458#endif
6459#ifdef _SC_PHYS_PAGES
6460 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6461#endif
6462#ifdef _SC_PII
6463 {"SC_PII", _SC_PII},
6464#endif
6465#ifdef _SC_PII_INTERNET
6466 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6467#endif
6468#ifdef _SC_PII_INTERNET_DGRAM
6469 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6470#endif
6471#ifdef _SC_PII_INTERNET_STREAM
6472 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6473#endif
6474#ifdef _SC_PII_OSI
6475 {"SC_PII_OSI", _SC_PII_OSI},
6476#endif
6477#ifdef _SC_PII_OSI_CLTS
6478 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6479#endif
6480#ifdef _SC_PII_OSI_COTS
6481 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6482#endif
6483#ifdef _SC_PII_OSI_M
6484 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6485#endif
6486#ifdef _SC_PII_SOCKET
6487 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6488#endif
6489#ifdef _SC_PII_XTI
6490 {"SC_PII_XTI", _SC_PII_XTI},
6491#endif
6492#ifdef _SC_POLL
6493 {"SC_POLL", _SC_POLL},
6494#endif
6495#ifdef _SC_PRIORITIZED_IO
6496 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6497#endif
6498#ifdef _SC_PRIORITY_SCHEDULING
6499 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6500#endif
6501#ifdef _SC_REALTIME_SIGNALS
6502 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6503#endif
6504#ifdef _SC_RE_DUP_MAX
6505 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6506#endif
6507#ifdef _SC_RTSIG_MAX
6508 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6509#endif
6510#ifdef _SC_SAVED_IDS
6511 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6512#endif
6513#ifdef _SC_SCHAR_MAX
6514 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6515#endif
6516#ifdef _SC_SCHAR_MIN
6517 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6518#endif
6519#ifdef _SC_SELECT
6520 {"SC_SELECT", _SC_SELECT},
6521#endif
6522#ifdef _SC_SEMAPHORES
6523 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6524#endif
6525#ifdef _SC_SEM_NSEMS_MAX
6526 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6527#endif
6528#ifdef _SC_SEM_VALUE_MAX
6529 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6530#endif
6531#ifdef _SC_SHARED_MEMORY_OBJECTS
6532 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6533#endif
6534#ifdef _SC_SHRT_MAX
6535 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6536#endif
6537#ifdef _SC_SHRT_MIN
6538 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6539#endif
6540#ifdef _SC_SIGQUEUE_MAX
6541 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6542#endif
6543#ifdef _SC_SIGRT_MAX
6544 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6545#endif
6546#ifdef _SC_SIGRT_MIN
6547 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6548#endif
Fred Draked86ed291999-12-15 15:34:33 +00006549#ifdef _SC_SOFTPOWER
6550 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6551#endif
Fred Drakec9680921999-12-13 16:37:25 +00006552#ifdef _SC_SPLIT_CACHE
6553 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6554#endif
6555#ifdef _SC_SSIZE_MAX
6556 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6557#endif
6558#ifdef _SC_STACK_PROT
6559 {"SC_STACK_PROT", _SC_STACK_PROT},
6560#endif
6561#ifdef _SC_STREAM_MAX
6562 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6563#endif
6564#ifdef _SC_SYNCHRONIZED_IO
6565 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6566#endif
6567#ifdef _SC_THREADS
6568 {"SC_THREADS", _SC_THREADS},
6569#endif
6570#ifdef _SC_THREAD_ATTR_STACKADDR
6571 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6572#endif
6573#ifdef _SC_THREAD_ATTR_STACKSIZE
6574 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6575#endif
6576#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6577 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6578#endif
6579#ifdef _SC_THREAD_KEYS_MAX
6580 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6581#endif
6582#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6583 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6584#endif
6585#ifdef _SC_THREAD_PRIO_INHERIT
6586 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6587#endif
6588#ifdef _SC_THREAD_PRIO_PROTECT
6589 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6590#endif
6591#ifdef _SC_THREAD_PROCESS_SHARED
6592 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6593#endif
6594#ifdef _SC_THREAD_SAFE_FUNCTIONS
6595 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6596#endif
6597#ifdef _SC_THREAD_STACK_MIN
6598 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6599#endif
6600#ifdef _SC_THREAD_THREADS_MAX
6601 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6602#endif
6603#ifdef _SC_TIMERS
6604 {"SC_TIMERS", _SC_TIMERS},
6605#endif
6606#ifdef _SC_TIMER_MAX
6607 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6608#endif
6609#ifdef _SC_TTY_NAME_MAX
6610 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6611#endif
6612#ifdef _SC_TZNAME_MAX
6613 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6614#endif
6615#ifdef _SC_T_IOV_MAX
6616 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6617#endif
6618#ifdef _SC_UCHAR_MAX
6619 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6620#endif
6621#ifdef _SC_UINT_MAX
6622 {"SC_UINT_MAX", _SC_UINT_MAX},
6623#endif
6624#ifdef _SC_UIO_MAXIOV
6625 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6626#endif
6627#ifdef _SC_ULONG_MAX
6628 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6629#endif
6630#ifdef _SC_USHRT_MAX
6631 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6632#endif
6633#ifdef _SC_VERSION
6634 {"SC_VERSION", _SC_VERSION},
6635#endif
6636#ifdef _SC_WORD_BIT
6637 {"SC_WORD_BIT", _SC_WORD_BIT},
6638#endif
6639#ifdef _SC_XBS5_ILP32_OFF32
6640 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6641#endif
6642#ifdef _SC_XBS5_ILP32_OFFBIG
6643 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6644#endif
6645#ifdef _SC_XBS5_LP64_OFF64
6646 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6647#endif
6648#ifdef _SC_XBS5_LPBIG_OFFBIG
6649 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6650#endif
6651#ifdef _SC_XOPEN_CRYPT
6652 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6653#endif
6654#ifdef _SC_XOPEN_ENH_I18N
6655 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6656#endif
6657#ifdef _SC_XOPEN_LEGACY
6658 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6659#endif
6660#ifdef _SC_XOPEN_REALTIME
6661 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6662#endif
6663#ifdef _SC_XOPEN_REALTIME_THREADS
6664 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6665#endif
6666#ifdef _SC_XOPEN_SHM
6667 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6668#endif
6669#ifdef _SC_XOPEN_UNIX
6670 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6671#endif
6672#ifdef _SC_XOPEN_VERSION
6673 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6674#endif
6675#ifdef _SC_XOPEN_XCU_VERSION
6676 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6677#endif
6678#ifdef _SC_XOPEN_XPG2
6679 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6680#endif
6681#ifdef _SC_XOPEN_XPG3
6682 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6683#endif
6684#ifdef _SC_XOPEN_XPG4
6685 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6686#endif
6687};
6688
6689static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006690conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006691{
6692 return conv_confname(arg, valuep, posix_constants_sysconf,
6693 sizeof(posix_constants_sysconf)
6694 / sizeof(struct constdef));
6695}
6696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006697PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006698"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006699Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006700
6701static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006702posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006703{
6704 PyObject *result = NULL;
6705 int name;
6706
6707 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6708 int value;
6709
6710 errno = 0;
6711 value = sysconf(name);
6712 if (value == -1 && errno != 0)
6713 posix_error();
6714 else
6715 result = PyInt_FromLong(value);
6716 }
6717 return result;
6718}
6719#endif
6720
6721
Fred Drakebec628d1999-12-15 18:31:10 +00006722/* This code is used to ensure that the tables of configuration value names
6723 * are in sorted order as required by conv_confname(), and also to build the
6724 * the exported dictionaries that are used to publish information about the
6725 * names available on the host platform.
6726 *
6727 * Sorting the table at runtime ensures that the table is properly ordered
6728 * when used, even for platforms we're not able to test on. It also makes
6729 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006730 */
Fred Drakebec628d1999-12-15 18:31:10 +00006731
6732static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006733cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006734{
6735 const struct constdef *c1 =
6736 (const struct constdef *) v1;
6737 const struct constdef *c2 =
6738 (const struct constdef *) v2;
6739
6740 return strcmp(c1->name, c2->name);
6741}
6742
6743static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006744setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006745 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006746{
Fred Drakebec628d1999-12-15 18:31:10 +00006747 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006748 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006749
6750 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6751 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006752 if (d == NULL)
6753 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006754
Barry Warsaw3155db32000-04-13 15:20:40 +00006755 for (i=0; i < tablesize; ++i) {
6756 PyObject *o = PyInt_FromLong(table[i].value);
6757 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6758 Py_XDECREF(o);
6759 Py_DECREF(d);
6760 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006761 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006762 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006763 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006764 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006765}
6766
Fred Drakebec628d1999-12-15 18:31:10 +00006767/* Return -1 on failure, 0 on success. */
6768static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006769setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006770{
6771#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006772 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006773 sizeof(posix_constants_pathconf)
6774 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006775 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006776 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006777#endif
6778#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006779 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006780 sizeof(posix_constants_confstr)
6781 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006782 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006783 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006784#endif
6785#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006786 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006787 sizeof(posix_constants_sysconf)
6788 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006789 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006790 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006791#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006792 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006793}
Fred Draked86ed291999-12-15 15:34:33 +00006794
6795
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006796PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006797"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006798Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006799in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006800
6801static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006802posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006803{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006804 abort();
6805 /*NOTREACHED*/
6806 Py_FatalError("abort() called from Python code didn't abort!");
6807 return NULL;
6808}
Fred Drakebec628d1999-12-15 18:31:10 +00006809
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006810#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006811PyDoc_STRVAR(win32_startfile__doc__,
6812"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006813\n\
6814This acts like double-clicking the file in Explorer, or giving the file\n\
6815name as an argument to the DOS \"start\" command: the file is opened\n\
6816with whatever application (if any) its extension is associated.\n\
6817\n\
6818startfile returns as soon as the associated application is launched.\n\
6819There is no option to wait for the application to close, and no way\n\
6820to retrieve the application's exit status.\n\
6821\n\
6822The filepath is relative to the current directory. If you want to use\n\
6823an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006824the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006825
6826static PyObject *
6827win32_startfile(PyObject *self, PyObject *args)
6828{
6829 char *filepath;
6830 HINSTANCE rc;
6831 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
6832 return NULL;
6833 Py_BEGIN_ALLOW_THREADS
6834 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
6835 Py_END_ALLOW_THREADS
6836 if (rc <= (HINSTANCE)32)
6837 return win32_error("startfile", filepath);
6838 Py_INCREF(Py_None);
6839 return Py_None;
6840}
6841#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006842
Martin v. Löwis438b5342002-12-27 10:16:42 +00006843#ifdef HAVE_GETLOADAVG
6844PyDoc_STRVAR(posix_getloadavg__doc__,
6845"getloadavg() -> (float, float, float)\n\n\
6846Return the number of processes in the system run queue averaged over\n\
6847the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6848was unobtainable");
6849
6850static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006851posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006852{
6853 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006854 if (getloadavg(loadavg, 3)!=3) {
6855 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6856 return NULL;
6857 } else
6858 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6859}
6860#endif
6861
6862
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006863static PyMethodDef posix_methods[] = {
6864 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6865#ifdef HAVE_TTYNAME
6866 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6867#endif
6868 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
6869 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006870#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006871 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006872#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006873#ifdef HAVE_LCHOWN
6874 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6875#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006876#ifdef HAVE_CHROOT
6877 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6878#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006879#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006880 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006881#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006882#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006883 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00006884#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00006885 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006886#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00006887#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006888#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006889 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006890#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006891 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6892 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6893 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006894#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006895 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006896#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006897#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006898 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006899#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006900 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6901 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6902 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006903 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006904#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006905 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006906#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006907#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006908 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006909#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006910 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006911#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006912 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006913#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006914 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6915 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6916 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006917#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006918 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006919#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006920 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006921#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006922 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6923 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006924#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006925#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006926 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6927 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00006928#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006929#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006930 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006931#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006932#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006933 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006934#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006935#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006936 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006937#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006938#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006939 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006940#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006941#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006942 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006943#endif /* HAVE_GETEGID */
6944#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006945 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006946#endif /* HAVE_GETEUID */
6947#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006948 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006949#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006950#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006951 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006952#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006953 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006954#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006955 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006956#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006957#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006958 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006959#endif /* HAVE_GETPPID */
6960#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006961 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006962#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006963#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006964 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006965#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006966#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006967 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006968#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006969#ifdef HAVE_KILLPG
6970 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6971#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006972#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006973 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006974#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006975#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006976 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006977#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006978 {"popen2", win32_popen2, METH_VARARGS},
6979 {"popen3", win32_popen3, METH_VARARGS},
6980 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00006981 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006982#else
6983#if defined(PYOS_OS2) && defined(PYCC_GCC)
6984 {"popen2", os2emx_popen2, METH_VARARGS},
6985 {"popen3", os2emx_popen3, METH_VARARGS},
6986 {"popen4", os2emx_popen4, METH_VARARGS},
6987#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006988#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006989#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006990#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006991 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006992#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006993#ifdef HAVE_SETEUID
6994 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6995#endif /* HAVE_SETEUID */
6996#ifdef HAVE_SETEGID
6997 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6998#endif /* HAVE_SETEGID */
6999#ifdef HAVE_SETREUID
7000 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7001#endif /* HAVE_SETREUID */
7002#ifdef HAVE_SETREGID
7003 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7004#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007005#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007006 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007007#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007008#ifdef HAVE_SETGROUPS
7009 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7010#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007011#ifdef HAVE_GETPGID
7012 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7013#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007014#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007015 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007016#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007017#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007018 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007019#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007020#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007021 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007022#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007023#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007024 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007025#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007026#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007027 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007028#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007029#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007030 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007031#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007032#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007033 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007034#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007035 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7036 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7037 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7038 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7039 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7040 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7041 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7042 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7043 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007044 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007045#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007046 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007047#endif
7048#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007049 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007050#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007051#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007052 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7053#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007054#ifdef HAVE_DEVICE_MACROS
7055 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7056 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7057 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7058#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007059#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007060 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007061#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007062#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007063 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007064#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007065#ifdef HAVE_UNSETENV
7066 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7067#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007068#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007069 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007070#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007071#ifdef HAVE_FCHDIR
7072 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7073#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007074#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007075 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007076#endif
7077#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007078 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007079#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007080#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007081#ifdef WCOREDUMP
7082 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7083#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007084#ifdef WIFCONTINUED
7085 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7086#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007087#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007088 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007089#endif /* WIFSTOPPED */
7090#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007091 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007092#endif /* WIFSIGNALED */
7093#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007094 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007095#endif /* WIFEXITED */
7096#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007097 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007098#endif /* WEXITSTATUS */
7099#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007100 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007101#endif /* WTERMSIG */
7102#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007103 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007104#endif /* WSTOPSIG */
7105#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007106#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007107 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007108#endif
7109#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007110 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007111#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007112#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007113 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007114#endif
7115#ifdef HAVE_TEMPNAM
7116 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7117#endif
7118#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007119 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007120#endif
Fred Drakec9680921999-12-13 16:37:25 +00007121#ifdef HAVE_CONFSTR
7122 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7123#endif
7124#ifdef HAVE_SYSCONF
7125 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7126#endif
7127#ifdef HAVE_FPATHCONF
7128 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7129#endif
7130#ifdef HAVE_PATHCONF
7131 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7132#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007133 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007134#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007135 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7136#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007137#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007138 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007139#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007140 {NULL, NULL} /* Sentinel */
7141};
7142
7143
Barry Warsaw4a342091996-12-19 23:50:02 +00007144static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007145ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007146{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007147 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007148}
7149
Guido van Rossumd48f2521997-12-05 22:19:34 +00007150#if defined(PYOS_OS2)
7151/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007152static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007153{
7154 APIRET rc;
7155 ULONG values[QSV_MAX+1];
7156 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007157 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007158
7159 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007160 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007161 Py_END_ALLOW_THREADS
7162
7163 if (rc != NO_ERROR) {
7164 os2_error(rc);
7165 return -1;
7166 }
7167
Fred Drake4d1e64b2002-04-15 19:40:07 +00007168 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7169 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7170 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7171 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7172 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7173 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7174 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007175
7176 switch (values[QSV_VERSION_MINOR]) {
7177 case 0: ver = "2.00"; break;
7178 case 10: ver = "2.10"; break;
7179 case 11: ver = "2.11"; break;
7180 case 30: ver = "3.00"; break;
7181 case 40: ver = "4.00"; break;
7182 case 50: ver = "5.00"; break;
7183 default:
Tim Peters885d4572001-11-28 20:27:42 +00007184 PyOS_snprintf(tmp, sizeof(tmp),
7185 "%d-%d", values[QSV_VERSION_MAJOR],
7186 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007187 ver = &tmp[0];
7188 }
7189
7190 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007191 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007192 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007193
7194 /* Add Indicator of Which Drive was Used to Boot the System */
7195 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7196 tmp[1] = ':';
7197 tmp[2] = '\0';
7198
Fred Drake4d1e64b2002-04-15 19:40:07 +00007199 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007200}
7201#endif
7202
Barry Warsaw4a342091996-12-19 23:50:02 +00007203static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007204all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007205{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007206#ifdef F_OK
7207 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007208#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007209#ifdef R_OK
7210 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007211#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007212#ifdef W_OK
7213 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007214#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007215#ifdef X_OK
7216 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007217#endif
Fred Drakec9680921999-12-13 16:37:25 +00007218#ifdef NGROUPS_MAX
7219 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7220#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007221#ifdef TMP_MAX
7222 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7223#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007224#ifdef WCONTINUED
7225 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7226#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007227#ifdef WNOHANG
7228 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007229#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007230#ifdef WUNTRACED
7231 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7232#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007233#ifdef O_RDONLY
7234 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7235#endif
7236#ifdef O_WRONLY
7237 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7238#endif
7239#ifdef O_RDWR
7240 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7241#endif
7242#ifdef O_NDELAY
7243 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7244#endif
7245#ifdef O_NONBLOCK
7246 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7247#endif
7248#ifdef O_APPEND
7249 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7250#endif
7251#ifdef O_DSYNC
7252 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7253#endif
7254#ifdef O_RSYNC
7255 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7256#endif
7257#ifdef O_SYNC
7258 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7259#endif
7260#ifdef O_NOCTTY
7261 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7262#endif
7263#ifdef O_CREAT
7264 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7265#endif
7266#ifdef O_EXCL
7267 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7268#endif
7269#ifdef O_TRUNC
7270 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7271#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007272#ifdef O_BINARY
7273 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7274#endif
7275#ifdef O_TEXT
7276 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7277#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007278#ifdef O_LARGEFILE
7279 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7280#endif
7281
Tim Peters5aa91602002-01-30 05:46:57 +00007282/* MS Windows */
7283#ifdef O_NOINHERIT
7284 /* Don't inherit in child processes. */
7285 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7286#endif
7287#ifdef _O_SHORT_LIVED
7288 /* Optimize for short life (keep in memory). */
7289 /* MS forgot to define this one with a non-underscore form too. */
7290 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7291#endif
7292#ifdef O_TEMPORARY
7293 /* Automatically delete when last handle is closed. */
7294 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7295#endif
7296#ifdef O_RANDOM
7297 /* Optimize for random access. */
7298 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7299#endif
7300#ifdef O_SEQUENTIAL
7301 /* Optimize for sequential access. */
7302 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7303#endif
7304
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007305/* GNU extensions. */
7306#ifdef O_DIRECT
7307 /* Direct disk access. */
7308 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7309#endif
7310#ifdef O_DIRECTORY
7311 /* Must be a directory. */
7312 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7313#endif
7314#ifdef O_NOFOLLOW
7315 /* Do not follow links. */
7316 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7317#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007318
Barry Warsaw5676bd12003-01-07 20:57:09 +00007319 /* These come from sysexits.h */
7320#ifdef EX_OK
7321 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007322#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007323#ifdef EX_USAGE
7324 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007325#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007326#ifdef EX_DATAERR
7327 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007328#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007329#ifdef EX_NOINPUT
7330 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007331#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007332#ifdef EX_NOUSER
7333 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007334#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007335#ifdef EX_NOHOST
7336 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007337#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007338#ifdef EX_UNAVAILABLE
7339 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007340#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007341#ifdef EX_SOFTWARE
7342 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007343#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007344#ifdef EX_OSERR
7345 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007346#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007347#ifdef EX_OSFILE
7348 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007349#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007350#ifdef EX_CANTCREAT
7351 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007352#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007353#ifdef EX_IOERR
7354 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007355#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007356#ifdef EX_TEMPFAIL
7357 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007358#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007359#ifdef EX_PROTOCOL
7360 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007361#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007362#ifdef EX_NOPERM
7363 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007364#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007365#ifdef EX_CONFIG
7366 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007367#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007368#ifdef EX_NOTFOUND
7369 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007370#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007371
Guido van Rossum246bc171999-02-01 23:54:31 +00007372#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007373#if defined(PYOS_OS2) && defined(PYCC_GCC)
7374 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7375 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7376 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7377 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7378 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7379 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7380 if (ins(d, "P_PM", (long)P_PM)) return -1;
7381 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7382 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7383 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7384 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7385 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7386 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7387 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7388 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7389 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7390 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7391 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7392 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7393 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7394#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007395 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7396 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7397 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7398 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7399 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007400#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007401#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007402
Guido van Rossumd48f2521997-12-05 22:19:34 +00007403#if defined(PYOS_OS2)
7404 if (insertvalues(d)) return -1;
7405#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007406 return 0;
7407}
7408
7409
Tim Peters5aa91602002-01-30 05:46:57 +00007410#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007411#define INITFUNC initnt
7412#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007413
7414#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007415#define INITFUNC initos2
7416#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007417
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007418#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007419#define INITFUNC initposix
7420#define MODNAME "posix"
7421#endif
7422
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007423PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007424INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007425{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007426 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007427
Fred Drake4d1e64b2002-04-15 19:40:07 +00007428 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007429 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007430 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007431
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007432 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007433 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007434 Py_XINCREF(v);
7435 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007436 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007437 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007438
Fred Drake4d1e64b2002-04-15 19:40:07 +00007439 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007440 return;
7441
Fred Drake4d1e64b2002-04-15 19:40:07 +00007442 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007443 return;
7444
Fred Drake4d1e64b2002-04-15 19:40:07 +00007445 Py_INCREF(PyExc_OSError);
7446 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007447
Guido van Rossumb3d39562000-01-31 18:41:26 +00007448#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007449 if (posix_putenv_garbage == NULL)
7450 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007451#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007452
Guido van Rossum14648392001-12-08 18:02:58 +00007453 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007454 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7455 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7456 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007457 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007458 structseq_new = StatResultType.tp_new;
7459 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007460 Py_INCREF((PyObject*) &StatResultType);
7461 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007462
Guido van Rossum14648392001-12-08 18:02:58 +00007463 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007464 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007465 Py_INCREF((PyObject*) &StatVFSResultType);
7466 PyModule_AddObject(m, "statvfs_result",
7467 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007468}