blob: 4b3d82502b53ab3c5c718c8a290c780aaa10a6b4 [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
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004758#ifdef HAVE_GETSID
4759PyDoc_STRVAR(posix_getsid__doc__,
4760"getsid(pid) -> sid\n\n\
4761Call the system call getsid().");
4762
4763static PyObject *
4764posix_getsid(PyObject *self, PyObject *args)
4765{
4766 int pid, sid;
4767 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4768 return NULL;
4769 sid = getsid(pid);
4770 if (sid < 0)
4771 return posix_error();
4772 return PyInt_FromLong((long)sid);
4773}
4774#endif /* HAVE_GETSID */
4775
4776
Guido van Rossumb6775db1994-08-01 11:34:53 +00004777#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004778PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004779"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004780Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004781
Barry Warsaw53699e91996-12-10 23:23:01 +00004782static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004783posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004784{
Guido van Rossum687dd131993-05-17 08:34:16 +00004785 if (setsid() < 0)
4786 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004787 Py_INCREF(Py_None);
4788 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004789}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004790#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004791
Guido van Rossumb6775db1994-08-01 11:34:53 +00004792#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004793PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004794"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795Call the system call setpgid().");
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_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004799{
4800 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004801 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004802 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004803 if (setpgid(pid, pgrp) < 0)
4804 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004805 Py_INCREF(Py_None);
4806 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004807}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004808#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004809
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004810
Guido van Rossumb6775db1994-08-01 11:34:53 +00004811#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004812PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004813"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004814Return 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_tcgetpgrp(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, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004821 return NULL;
4822 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004823 if (pgid < 0)
4824 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004825 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004826}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004827#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004829
Guido van Rossumb6775db1994-08-01 11:34:53 +00004830#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004831PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004832"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004833Set the process group associated with the terminal given by a fd.");
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_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004837{
4838 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004839 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004840 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004841 if (tcsetpgrp(fd, pgid) < 0)
4842 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004843 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004844 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004845}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004846#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004847
Guido van Rossum687dd131993-05-17 08:34:16 +00004848/* Functions acting on file descriptors */
4849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004850PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004851"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004852Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004853
Barry Warsaw53699e91996-12-10 23:23:01 +00004854static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004855posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004856{
Mark Hammondef8b6542001-05-13 08:04:26 +00004857 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004858 int flag;
4859 int mode = 0777;
4860 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004861
4862#ifdef MS_WINDOWS
4863 if (unicode_file_names()) {
4864 PyUnicodeObject *po;
4865 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4866 Py_BEGIN_ALLOW_THREADS
4867 /* PyUnicode_AS_UNICODE OK without thread
4868 lock as it is a simple dereference. */
4869 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4870 Py_END_ALLOW_THREADS
4871 if (fd < 0)
4872 return posix_error();
4873 return PyInt_FromLong((long)fd);
4874 }
4875 /* Drop the argument parsing error as narrow strings
4876 are also valid. */
4877 PyErr_Clear();
4878 }
4879#endif
4880
Tim Peters5aa91602002-01-30 05:46:57 +00004881 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004882 Py_FileSystemDefaultEncoding, &file,
4883 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004884 return NULL;
4885
Barry Warsaw53699e91996-12-10 23:23:01 +00004886 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004887 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004888 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004889 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004890 return posix_error_with_allocated_filename(file);
4891 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004892 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004893}
4894
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004896PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004897"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004898Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004899
Barry Warsaw53699e91996-12-10 23:23:01 +00004900static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004901posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004902{
4903 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004904 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004905 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004906 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004907 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004908 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004909 if (res < 0)
4910 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004911 Py_INCREF(Py_None);
4912 return Py_None;
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_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004917"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004918Return a duplicate of a 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_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004922{
4923 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004924 if (!PyArg_ParseTuple(args, "i:dup", &fd))
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 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004928 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004929 if (fd < 0)
4930 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004931 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004932}
4933
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004934
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004935PyDoc_STRVAR(posix_dup2__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004936"dup2(fd, fd2)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004937Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004938
Barry Warsaw53699e91996-12-10 23:23:01 +00004939static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004940posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004941{
4942 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004943 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004944 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004945 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004946 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004947 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004948 if (res < 0)
4949 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004950 Py_INCREF(Py_None);
4951 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004952}
4953
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004955PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004956"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004957Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004958
Barry Warsaw53699e91996-12-10 23:23:01 +00004959static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004960posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004961{
4962 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004963#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004964 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004965#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004966 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004967#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004968 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004969 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004970 return NULL;
4971#ifdef SEEK_SET
4972 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4973 switch (how) {
4974 case 0: how = SEEK_SET; break;
4975 case 1: how = SEEK_CUR; break;
4976 case 2: how = SEEK_END; break;
4977 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004978#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004979
4980#if !defined(HAVE_LARGEFILE_SUPPORT)
4981 pos = PyInt_AsLong(posobj);
4982#else
4983 pos = PyLong_Check(posobj) ?
4984 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4985#endif
4986 if (PyErr_Occurred())
4987 return NULL;
4988
Barry Warsaw53699e91996-12-10 23:23:01 +00004989 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004990#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004991 res = _lseeki64(fd, pos, how);
4992#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004993 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004994#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004995 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004996 if (res < 0)
4997 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004998
4999#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005000 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005001#else
5002 return PyLong_FromLongLong(res);
5003#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005004}
5005
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005007PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005008"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005009Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005010
Barry Warsaw53699e91996-12-10 23:23:01 +00005011static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005012posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005013{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005014 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005015 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005016 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005017 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005018 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005019 if (buffer == NULL)
5020 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005021 Py_BEGIN_ALLOW_THREADS
5022 n = read(fd, PyString_AsString(buffer), size);
5023 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005024 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005025 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005026 return posix_error();
5027 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005028 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005029 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005030 return buffer;
5031}
5032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005034PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005035"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005036Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005037
Barry Warsaw53699e91996-12-10 23:23:01 +00005038static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005039posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005040{
5041 int fd, size;
5042 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005043 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005044 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005045 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005046 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005047 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005048 if (size < 0)
5049 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005050 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005051}
5052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005054PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005055"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005056Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005057
Barry Warsaw53699e91996-12-10 23:23:01 +00005058static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005059posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005060{
5061 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005062 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005063 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005064 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005065 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005066#ifdef __VMS
5067 /* on OpenVMS we must ensure that all bytes are written to the file */
5068 fsync(fd);
5069#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005070 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005071 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005072 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005073 if (res != 0)
5074 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005075
Fred Drake699f3522000-06-29 21:12:41 +00005076 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005077}
5078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005080PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005081"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005082Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005083
Barry Warsaw53699e91996-12-10 23:23:01 +00005084static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005085posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005086{
Guido van Rossum687dd131993-05-17 08:34:16 +00005087 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005088 char *mode = "r";
5089 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005090 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005091 PyObject *f;
5092 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005093 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005094
Thomas Heller1f043e22002-11-07 16:00:59 +00005095 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5096 PyErr_Format(PyExc_ValueError,
5097 "invalid file mode '%s'", mode);
5098 return NULL;
5099 }
5100
Barry Warsaw53699e91996-12-10 23:23:01 +00005101 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005102 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005103 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005104 if (fp == NULL)
5105 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005106 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005107 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005108 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005109 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005110}
5111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005112PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005113"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005114Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005115connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005116
5117static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005118posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005119{
5120 int fd;
5121 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5122 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005123 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005124}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005125
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005126#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005127PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005128"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005129Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005130
Barry Warsaw53699e91996-12-10 23:23:01 +00005131static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005132posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005133{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005134#if defined(PYOS_OS2)
5135 HFILE read, write;
5136 APIRET rc;
5137
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005138 Py_BEGIN_ALLOW_THREADS
5139 rc = DosCreatePipe( &read, &write, 4096);
5140 Py_END_ALLOW_THREADS
5141 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005142 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005143
5144 return Py_BuildValue("(ii)", read, write);
5145#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005146#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005147 int fds[2];
5148 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005149 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005150 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005151 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005152 if (res != 0)
5153 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005154 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005155#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005156 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005157 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005158 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005159 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005160 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005161 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005162 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005163 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005164 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5165 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005166 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005167#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005168#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005169}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005170#endif /* HAVE_PIPE */
5171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005172
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005173#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005174PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005175"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005176Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005177
Barry Warsaw53699e91996-12-10 23:23:01 +00005178static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005179posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005180{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005181 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005182 int mode = 0666;
5183 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005184 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005185 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005186 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005187 res = mkfifo(filename, mode);
5188 Py_END_ALLOW_THREADS
5189 if (res < 0)
5190 return posix_error();
5191 Py_INCREF(Py_None);
5192 return Py_None;
5193}
5194#endif
5195
5196
Neal Norwitz11690112002-07-30 01:08:28 +00005197#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005198PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005199"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005200Create a filesystem node (file, device special file or named pipe)\n\
5201named filename. mode specifies both the permissions to use and the\n\
5202type of node to be created, being combined (bitwise OR) with one of\n\
5203S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005204device defines the newly created device special file (probably using\n\
5205os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005206
5207
5208static PyObject *
5209posix_mknod(PyObject *self, PyObject *args)
5210{
5211 char *filename;
5212 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005213 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005214 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005215 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005216 return NULL;
5217 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005218 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005219 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005220 if (res < 0)
5221 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005222 Py_INCREF(Py_None);
5223 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005224}
5225#endif
5226
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005227#ifdef HAVE_DEVICE_MACROS
5228PyDoc_STRVAR(posix_major__doc__,
5229"major(device) -> major number\n\
5230Extracts a device major number from a raw device number.");
5231
5232static PyObject *
5233posix_major(PyObject *self, PyObject *args)
5234{
5235 int device;
5236 if (!PyArg_ParseTuple(args, "i:major", &device))
5237 return NULL;
5238 return PyInt_FromLong((long)major(device));
5239}
5240
5241PyDoc_STRVAR(posix_minor__doc__,
5242"minor(device) -> minor number\n\
5243Extracts a device minor number from a raw device number.");
5244
5245static PyObject *
5246posix_minor(PyObject *self, PyObject *args)
5247{
5248 int device;
5249 if (!PyArg_ParseTuple(args, "i:minor", &device))
5250 return NULL;
5251 return PyInt_FromLong((long)minor(device));
5252}
5253
5254PyDoc_STRVAR(posix_makedev__doc__,
5255"makedev(major, minor) -> device number\n\
5256Composes a raw device number from the major and minor device numbers.");
5257
5258static PyObject *
5259posix_makedev(PyObject *self, PyObject *args)
5260{
5261 int major, minor;
5262 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5263 return NULL;
5264 return PyInt_FromLong((long)makedev(major, minor));
5265}
5266#endif /* device macros */
5267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005268
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005269#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005270PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005271"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005273
Barry Warsaw53699e91996-12-10 23:23:01 +00005274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005275posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005276{
5277 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005278 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005279 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005280 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005281
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005282 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005283 return NULL;
5284
5285#if !defined(HAVE_LARGEFILE_SUPPORT)
5286 length = PyInt_AsLong(lenobj);
5287#else
5288 length = PyLong_Check(lenobj) ?
5289 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5290#endif
5291 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005292 return NULL;
5293
Barry Warsaw53699e91996-12-10 23:23:01 +00005294 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005295 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005296 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005297 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005298 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005299 return NULL;
5300 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005301 Py_INCREF(Py_None);
5302 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005303}
5304#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005305
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005306#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005307PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005308"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005309Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005310
Fred Drake762e2061999-08-26 17:23:54 +00005311/* Save putenv() parameters as values here, so we can collect them when they
5312 * get re-set with another call for the same key. */
5313static PyObject *posix_putenv_garbage;
5314
Tim Peters5aa91602002-01-30 05:46:57 +00005315static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005316posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005317{
5318 char *s1, *s2;
5319 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005320 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005321 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005322
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005323 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005324 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005325
5326#if defined(PYOS_OS2)
5327 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5328 APIRET rc;
5329
Guido van Rossumd48f2521997-12-05 22:19:34 +00005330 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5331 if (rc != NO_ERROR)
5332 return os2_error(rc);
5333
5334 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5335 APIRET rc;
5336
Guido van Rossumd48f2521997-12-05 22:19:34 +00005337 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5338 if (rc != NO_ERROR)
5339 return os2_error(rc);
5340 } else {
5341#endif
5342
Fred Drake762e2061999-08-26 17:23:54 +00005343 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005344 len = strlen(s1) + strlen(s2) + 2;
5345 /* len includes space for a trailing \0; the size arg to
5346 PyString_FromStringAndSize does not count that */
5347 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005348 if (newstr == NULL)
5349 return PyErr_NoMemory();
5350 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005351 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005352 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005353 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005354 posix_error();
5355 return NULL;
5356 }
Fred Drake762e2061999-08-26 17:23:54 +00005357 /* Install the first arg and newstr in posix_putenv_garbage;
5358 * this will cause previous value to be collected. This has to
5359 * happen after the real putenv() call because the old value
5360 * was still accessible until then. */
5361 if (PyDict_SetItem(posix_putenv_garbage,
5362 PyTuple_GET_ITEM(args, 0), newstr)) {
5363 /* really not much we can do; just leak */
5364 PyErr_Clear();
5365 }
5366 else {
5367 Py_DECREF(newstr);
5368 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005369
5370#if defined(PYOS_OS2)
5371 }
5372#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005373 Py_INCREF(Py_None);
5374 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005375}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005376#endif /* putenv */
5377
Guido van Rossumc524d952001-10-19 01:31:59 +00005378#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005379PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005380"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005381Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005382
5383static PyObject *
5384posix_unsetenv(PyObject *self, PyObject *args)
5385{
5386 char *s1;
5387
5388 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5389 return NULL;
5390
5391 unsetenv(s1);
5392
5393 /* Remove the key from posix_putenv_garbage;
5394 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005395 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005396 * old value was still accessible until then.
5397 */
5398 if (PyDict_DelItem(posix_putenv_garbage,
5399 PyTuple_GET_ITEM(args, 0))) {
5400 /* really not much we can do; just leak */
5401 PyErr_Clear();
5402 }
5403
5404 Py_INCREF(Py_None);
5405 return Py_None;
5406}
5407#endif /* unsetenv */
5408
Guido van Rossumb6a47161997-09-15 22:54:34 +00005409#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005410PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005411"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005412Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005413
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005415posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005416{
5417 int code;
5418 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005419 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005420 return NULL;
5421 message = strerror(code);
5422 if (message == NULL) {
5423 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005424 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005425 return NULL;
5426 }
5427 return PyString_FromString(message);
5428}
5429#endif /* strerror */
5430
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005431
Guido van Rossumc9641791998-08-04 15:26:23 +00005432#ifdef HAVE_SYS_WAIT_H
5433
Fred Drake106c1a02002-04-23 15:58:02 +00005434#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005435PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005436"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005437Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005438
5439static PyObject *
5440posix_WCOREDUMP(PyObject *self, PyObject *args)
5441{
5442#ifdef UNION_WAIT
5443 union wait status;
5444#define status_i (status.w_status)
5445#else
5446 int status;
5447#define status_i status
5448#endif
5449 status_i = 0;
5450
5451 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5452 {
5453 return NULL;
5454 }
5455
5456 return PyBool_FromLong(WCOREDUMP(status));
5457#undef status_i
5458}
5459#endif /* WCOREDUMP */
5460
5461#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005462PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005463"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005464Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005465job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005466
5467static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005468posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005469{
5470#ifdef UNION_WAIT
5471 union wait status;
5472#define status_i (status.w_status)
5473#else
5474 int status;
5475#define status_i status
5476#endif
5477 status_i = 0;
5478
5479 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5480 {
5481 return NULL;
5482 }
5483
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005484 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005485#undef status_i
5486}
5487#endif /* WIFCONTINUED */
5488
Guido van Rossumc9641791998-08-04 15:26:23 +00005489#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005490PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005491"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005492Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005493
5494static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005495posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005496{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005497#ifdef UNION_WAIT
5498 union wait status;
5499#define status_i (status.w_status)
5500#else
5501 int status;
5502#define status_i status
5503#endif
5504 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005505
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005506 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005507 {
5508 return NULL;
5509 }
Tim Peters5aa91602002-01-30 05:46:57 +00005510
Fred Drake106c1a02002-04-23 15:58:02 +00005511 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005512#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005513}
5514#endif /* WIFSTOPPED */
5515
5516#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005517PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005518"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005520
5521static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005522posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005523{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005524#ifdef UNION_WAIT
5525 union wait status;
5526#define status_i (status.w_status)
5527#else
5528 int status;
5529#define status_i status
5530#endif
5531 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005532
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005533 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005534 {
5535 return NULL;
5536 }
Tim Peters5aa91602002-01-30 05:46:57 +00005537
Fred Drake106c1a02002-04-23 15:58:02 +00005538 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005539#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005540}
5541#endif /* WIFSIGNALED */
5542
5543#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005544PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005545"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005546Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005547system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005548
5549static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005550posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005551{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005552#ifdef UNION_WAIT
5553 union wait status;
5554#define status_i (status.w_status)
5555#else
5556 int status;
5557#define status_i status
5558#endif
5559 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005560
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005561 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005562 {
5563 return NULL;
5564 }
Tim Peters5aa91602002-01-30 05:46:57 +00005565
Fred Drake106c1a02002-04-23 15:58:02 +00005566 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005567#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005568}
5569#endif /* WIFEXITED */
5570
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005571#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005572PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005573"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005574Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005575
5576static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005577posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005578{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005579#ifdef UNION_WAIT
5580 union wait status;
5581#define status_i (status.w_status)
5582#else
5583 int status;
5584#define status_i status
5585#endif
5586 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005587
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005588 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005589 {
5590 return NULL;
5591 }
Tim Peters5aa91602002-01-30 05:46:57 +00005592
Guido van Rossumc9641791998-08-04 15:26:23 +00005593 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005594#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005595}
5596#endif /* WEXITSTATUS */
5597
5598#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005599PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005600"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005601Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005602value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005603
5604static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005605posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005606{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005607#ifdef UNION_WAIT
5608 union wait status;
5609#define status_i (status.w_status)
5610#else
5611 int status;
5612#define status_i status
5613#endif
5614 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005615
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005616 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005617 {
5618 return NULL;
5619 }
Tim Peters5aa91602002-01-30 05:46:57 +00005620
Guido van Rossumc9641791998-08-04 15:26:23 +00005621 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005622#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005623}
5624#endif /* WTERMSIG */
5625
5626#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005627PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005628"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005629Return the signal that stopped the process that provided\n\
5630the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005631
5632static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005633posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005634{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005635#ifdef UNION_WAIT
5636 union wait status;
5637#define status_i (status.w_status)
5638#else
5639 int status;
5640#define status_i status
5641#endif
5642 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005643
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005644 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005645 {
5646 return NULL;
5647 }
Tim Peters5aa91602002-01-30 05:46:57 +00005648
Guido van Rossumc9641791998-08-04 15:26:23 +00005649 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005650#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005651}
5652#endif /* WSTOPSIG */
5653
5654#endif /* HAVE_SYS_WAIT_H */
5655
5656
Guido van Rossum94f6f721999-01-06 18:42:14 +00005657#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005658#ifdef _SCO_DS
5659/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5660 needed definitions in sys/statvfs.h */
5661#define _SVID3
5662#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005663#include <sys/statvfs.h>
5664
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005665static PyObject*
5666_pystatvfs_fromstructstatvfs(struct statvfs st) {
5667 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5668 if (v == NULL)
5669 return NULL;
5670
5671#if !defined(HAVE_LARGEFILE_SUPPORT)
5672 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5673 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5674 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5675 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5676 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5677 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5678 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5679 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5680 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5681 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5682#else
5683 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5684 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005685 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005686 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005687 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005688 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005689 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005690 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005691 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005692 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005693 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005694 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005695 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005696 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005697 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5698 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5699#endif
5700
5701 return v;
5702}
5703
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005704PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005705"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005706Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005707
5708static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005709posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005710{
5711 int fd, res;
5712 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005713
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005714 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005715 return NULL;
5716 Py_BEGIN_ALLOW_THREADS
5717 res = fstatvfs(fd, &st);
5718 Py_END_ALLOW_THREADS
5719 if (res != 0)
5720 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005721
5722 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005723}
5724#endif /* HAVE_FSTATVFS */
5725
5726
5727#if defined(HAVE_STATVFS)
5728#include <sys/statvfs.h>
5729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005730PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005731"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005732Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005733
5734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005735posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005736{
5737 char *path;
5738 int res;
5739 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005740 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005741 return NULL;
5742 Py_BEGIN_ALLOW_THREADS
5743 res = statvfs(path, &st);
5744 Py_END_ALLOW_THREADS
5745 if (res != 0)
5746 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005747
5748 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005749}
5750#endif /* HAVE_STATVFS */
5751
5752
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005753#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005755"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005756Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005757The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005758or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005759
5760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005761posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005762{
5763 PyObject *result = NULL;
5764 char *dir = NULL;
5765 char *pfx = NULL;
5766 char *name;
5767
5768 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5769 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005770
5771 if (PyErr_Warn(PyExc_RuntimeWarning,
5772 "tempnam is a potential security risk to your program") < 0)
5773 return NULL;
5774
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005775#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005776 name = _tempnam(dir, pfx);
5777#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005778 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005779#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005780 if (name == NULL)
5781 return PyErr_NoMemory();
5782 result = PyString_FromString(name);
5783 free(name);
5784 return result;
5785}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005786#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005787
5788
5789#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005790PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005791"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005792Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005793
5794static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005795posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005796{
5797 FILE *fp;
5798
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005799 fp = tmpfile();
5800 if (fp == NULL)
5801 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005802 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005803}
5804#endif
5805
5806
5807#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005808PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005809"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005810Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005811
5812static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005813posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005814{
5815 char buffer[L_tmpnam];
5816 char *name;
5817
Skip Montanaro95618b52001-08-18 18:52:10 +00005818 if (PyErr_Warn(PyExc_RuntimeWarning,
5819 "tmpnam is a potential security risk to your program") < 0)
5820 return NULL;
5821
Greg Wardb48bc172000-03-01 21:51:56 +00005822#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005823 name = tmpnam_r(buffer);
5824#else
5825 name = tmpnam(buffer);
5826#endif
5827 if (name == NULL) {
5828 PyErr_SetObject(PyExc_OSError,
5829 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005830#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005831 "unexpected NULL from tmpnam_r"
5832#else
5833 "unexpected NULL from tmpnam"
5834#endif
5835 ));
5836 return NULL;
5837 }
5838 return PyString_FromString(buffer);
5839}
5840#endif
5841
5842
Fred Drakec9680921999-12-13 16:37:25 +00005843/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5844 * It maps strings representing configuration variable names to
5845 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005846 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005847 * rarely-used constants. There are three separate tables that use
5848 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005849 *
5850 * This code is always included, even if none of the interfaces that
5851 * need it are included. The #if hackery needed to avoid it would be
5852 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005853 */
5854struct constdef {
5855 char *name;
5856 long value;
5857};
5858
Fred Drake12c6e2d1999-12-14 21:25:03 +00005859static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005860conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5861 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005862{
5863 if (PyInt_Check(arg)) {
5864 *valuep = PyInt_AS_LONG(arg);
5865 return 1;
5866 }
5867 if (PyString_Check(arg)) {
5868 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005869 size_t lo = 0;
5870 size_t mid;
5871 size_t hi = tablesize;
5872 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005873 char *confname = PyString_AS_STRING(arg);
5874 while (lo < hi) {
5875 mid = (lo + hi) / 2;
5876 cmp = strcmp(confname, table[mid].name);
5877 if (cmp < 0)
5878 hi = mid;
5879 else if (cmp > 0)
5880 lo = mid + 1;
5881 else {
5882 *valuep = table[mid].value;
5883 return 1;
5884 }
5885 }
5886 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5887 }
5888 else
5889 PyErr_SetString(PyExc_TypeError,
5890 "configuration names must be strings or integers");
5891 return 0;
5892}
5893
5894
5895#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5896static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005897#ifdef _PC_ABI_AIO_XFER_MAX
5898 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5899#endif
5900#ifdef _PC_ABI_ASYNC_IO
5901 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5902#endif
Fred Drakec9680921999-12-13 16:37:25 +00005903#ifdef _PC_ASYNC_IO
5904 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5905#endif
5906#ifdef _PC_CHOWN_RESTRICTED
5907 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5908#endif
5909#ifdef _PC_FILESIZEBITS
5910 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5911#endif
5912#ifdef _PC_LAST
5913 {"PC_LAST", _PC_LAST},
5914#endif
5915#ifdef _PC_LINK_MAX
5916 {"PC_LINK_MAX", _PC_LINK_MAX},
5917#endif
5918#ifdef _PC_MAX_CANON
5919 {"PC_MAX_CANON", _PC_MAX_CANON},
5920#endif
5921#ifdef _PC_MAX_INPUT
5922 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5923#endif
5924#ifdef _PC_NAME_MAX
5925 {"PC_NAME_MAX", _PC_NAME_MAX},
5926#endif
5927#ifdef _PC_NO_TRUNC
5928 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5929#endif
5930#ifdef _PC_PATH_MAX
5931 {"PC_PATH_MAX", _PC_PATH_MAX},
5932#endif
5933#ifdef _PC_PIPE_BUF
5934 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5935#endif
5936#ifdef _PC_PRIO_IO
5937 {"PC_PRIO_IO", _PC_PRIO_IO},
5938#endif
5939#ifdef _PC_SOCK_MAXBUF
5940 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5941#endif
5942#ifdef _PC_SYNC_IO
5943 {"PC_SYNC_IO", _PC_SYNC_IO},
5944#endif
5945#ifdef _PC_VDISABLE
5946 {"PC_VDISABLE", _PC_VDISABLE},
5947#endif
5948};
5949
Fred Drakec9680921999-12-13 16:37:25 +00005950static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005951conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005952{
5953 return conv_confname(arg, valuep, posix_constants_pathconf,
5954 sizeof(posix_constants_pathconf)
5955 / sizeof(struct constdef));
5956}
5957#endif
5958
5959#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005960PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005961"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005962Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005963If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005964
5965static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005966posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005967{
5968 PyObject *result = NULL;
5969 int name, fd;
5970
Fred Drake12c6e2d1999-12-14 21:25:03 +00005971 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5972 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005973 long limit;
5974
5975 errno = 0;
5976 limit = fpathconf(fd, name);
5977 if (limit == -1 && errno != 0)
5978 posix_error();
5979 else
5980 result = PyInt_FromLong(limit);
5981 }
5982 return result;
5983}
5984#endif
5985
5986
5987#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005988PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005989"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005990Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005991If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005992
5993static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005994posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005995{
5996 PyObject *result = NULL;
5997 int name;
5998 char *path;
5999
6000 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6001 conv_path_confname, &name)) {
6002 long limit;
6003
6004 errno = 0;
6005 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006006 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006007 if (errno == EINVAL)
6008 /* could be a path or name problem */
6009 posix_error();
6010 else
6011 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006012 }
Fred Drakec9680921999-12-13 16:37:25 +00006013 else
6014 result = PyInt_FromLong(limit);
6015 }
6016 return result;
6017}
6018#endif
6019
6020#ifdef HAVE_CONFSTR
6021static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006022#ifdef _CS_ARCHITECTURE
6023 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6024#endif
6025#ifdef _CS_HOSTNAME
6026 {"CS_HOSTNAME", _CS_HOSTNAME},
6027#endif
6028#ifdef _CS_HW_PROVIDER
6029 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6030#endif
6031#ifdef _CS_HW_SERIAL
6032 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6033#endif
6034#ifdef _CS_INITTAB_NAME
6035 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6036#endif
Fred Drakec9680921999-12-13 16:37:25 +00006037#ifdef _CS_LFS64_CFLAGS
6038 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6039#endif
6040#ifdef _CS_LFS64_LDFLAGS
6041 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6042#endif
6043#ifdef _CS_LFS64_LIBS
6044 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6045#endif
6046#ifdef _CS_LFS64_LINTFLAGS
6047 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6048#endif
6049#ifdef _CS_LFS_CFLAGS
6050 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6051#endif
6052#ifdef _CS_LFS_LDFLAGS
6053 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6054#endif
6055#ifdef _CS_LFS_LIBS
6056 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6057#endif
6058#ifdef _CS_LFS_LINTFLAGS
6059 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6060#endif
Fred Draked86ed291999-12-15 15:34:33 +00006061#ifdef _CS_MACHINE
6062 {"CS_MACHINE", _CS_MACHINE},
6063#endif
Fred Drakec9680921999-12-13 16:37:25 +00006064#ifdef _CS_PATH
6065 {"CS_PATH", _CS_PATH},
6066#endif
Fred Draked86ed291999-12-15 15:34:33 +00006067#ifdef _CS_RELEASE
6068 {"CS_RELEASE", _CS_RELEASE},
6069#endif
6070#ifdef _CS_SRPC_DOMAIN
6071 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6072#endif
6073#ifdef _CS_SYSNAME
6074 {"CS_SYSNAME", _CS_SYSNAME},
6075#endif
6076#ifdef _CS_VERSION
6077 {"CS_VERSION", _CS_VERSION},
6078#endif
Fred Drakec9680921999-12-13 16:37:25 +00006079#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6080 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6081#endif
6082#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6083 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6084#endif
6085#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6086 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6087#endif
6088#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6089 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6090#endif
6091#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6092 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6093#endif
6094#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6095 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6096#endif
6097#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6098 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6099#endif
6100#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6101 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6102#endif
6103#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6104 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6105#endif
6106#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6107 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6108#endif
6109#ifdef _CS_XBS5_LP64_OFF64_LIBS
6110 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6111#endif
6112#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6113 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6114#endif
6115#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6116 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6117#endif
6118#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6119 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6120#endif
6121#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6122 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6123#endif
6124#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6125 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6126#endif
Fred Draked86ed291999-12-15 15:34:33 +00006127#ifdef _MIPS_CS_AVAIL_PROCESSORS
6128 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6129#endif
6130#ifdef _MIPS_CS_BASE
6131 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6132#endif
6133#ifdef _MIPS_CS_HOSTID
6134 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6135#endif
6136#ifdef _MIPS_CS_HW_NAME
6137 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6138#endif
6139#ifdef _MIPS_CS_NUM_PROCESSORS
6140 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6141#endif
6142#ifdef _MIPS_CS_OSREL_MAJ
6143 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6144#endif
6145#ifdef _MIPS_CS_OSREL_MIN
6146 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6147#endif
6148#ifdef _MIPS_CS_OSREL_PATCH
6149 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6150#endif
6151#ifdef _MIPS_CS_OS_NAME
6152 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6153#endif
6154#ifdef _MIPS_CS_OS_PROVIDER
6155 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6156#endif
6157#ifdef _MIPS_CS_PROCESSORS
6158 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6159#endif
6160#ifdef _MIPS_CS_SERIAL
6161 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6162#endif
6163#ifdef _MIPS_CS_VENDOR
6164 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6165#endif
Fred Drakec9680921999-12-13 16:37:25 +00006166};
6167
6168static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006169conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006170{
6171 return conv_confname(arg, valuep, posix_constants_confstr,
6172 sizeof(posix_constants_confstr)
6173 / sizeof(struct constdef));
6174}
6175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006176PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006177"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006178Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006179
6180static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006181posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006182{
6183 PyObject *result = NULL;
6184 int name;
6185 char buffer[64];
6186
6187 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6188 int len = confstr(name, buffer, sizeof(buffer));
6189
Fred Drakec9680921999-12-13 16:37:25 +00006190 errno = 0;
6191 if (len == 0) {
6192 if (errno != 0)
6193 posix_error();
6194 else
6195 result = PyString_FromString("");
6196 }
6197 else {
6198 if (len >= sizeof(buffer)) {
6199 result = PyString_FromStringAndSize(NULL, len);
6200 if (result != NULL)
6201 confstr(name, PyString_AS_STRING(result), len+1);
6202 }
6203 else
6204 result = PyString_FromString(buffer);
6205 }
6206 }
6207 return result;
6208}
6209#endif
6210
6211
6212#ifdef HAVE_SYSCONF
6213static struct constdef posix_constants_sysconf[] = {
6214#ifdef _SC_2_CHAR_TERM
6215 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6216#endif
6217#ifdef _SC_2_C_BIND
6218 {"SC_2_C_BIND", _SC_2_C_BIND},
6219#endif
6220#ifdef _SC_2_C_DEV
6221 {"SC_2_C_DEV", _SC_2_C_DEV},
6222#endif
6223#ifdef _SC_2_C_VERSION
6224 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6225#endif
6226#ifdef _SC_2_FORT_DEV
6227 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6228#endif
6229#ifdef _SC_2_FORT_RUN
6230 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6231#endif
6232#ifdef _SC_2_LOCALEDEF
6233 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6234#endif
6235#ifdef _SC_2_SW_DEV
6236 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6237#endif
6238#ifdef _SC_2_UPE
6239 {"SC_2_UPE", _SC_2_UPE},
6240#endif
6241#ifdef _SC_2_VERSION
6242 {"SC_2_VERSION", _SC_2_VERSION},
6243#endif
Fred Draked86ed291999-12-15 15:34:33 +00006244#ifdef _SC_ABI_ASYNCHRONOUS_IO
6245 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6246#endif
6247#ifdef _SC_ACL
6248 {"SC_ACL", _SC_ACL},
6249#endif
Fred Drakec9680921999-12-13 16:37:25 +00006250#ifdef _SC_AIO_LISTIO_MAX
6251 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6252#endif
Fred Drakec9680921999-12-13 16:37:25 +00006253#ifdef _SC_AIO_MAX
6254 {"SC_AIO_MAX", _SC_AIO_MAX},
6255#endif
6256#ifdef _SC_AIO_PRIO_DELTA_MAX
6257 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6258#endif
6259#ifdef _SC_ARG_MAX
6260 {"SC_ARG_MAX", _SC_ARG_MAX},
6261#endif
6262#ifdef _SC_ASYNCHRONOUS_IO
6263 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6264#endif
6265#ifdef _SC_ATEXIT_MAX
6266 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6267#endif
Fred Draked86ed291999-12-15 15:34:33 +00006268#ifdef _SC_AUDIT
6269 {"SC_AUDIT", _SC_AUDIT},
6270#endif
Fred Drakec9680921999-12-13 16:37:25 +00006271#ifdef _SC_AVPHYS_PAGES
6272 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6273#endif
6274#ifdef _SC_BC_BASE_MAX
6275 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6276#endif
6277#ifdef _SC_BC_DIM_MAX
6278 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6279#endif
6280#ifdef _SC_BC_SCALE_MAX
6281 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6282#endif
6283#ifdef _SC_BC_STRING_MAX
6284 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6285#endif
Fred Draked86ed291999-12-15 15:34:33 +00006286#ifdef _SC_CAP
6287 {"SC_CAP", _SC_CAP},
6288#endif
Fred Drakec9680921999-12-13 16:37:25 +00006289#ifdef _SC_CHARCLASS_NAME_MAX
6290 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6291#endif
6292#ifdef _SC_CHAR_BIT
6293 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6294#endif
6295#ifdef _SC_CHAR_MAX
6296 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6297#endif
6298#ifdef _SC_CHAR_MIN
6299 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6300#endif
6301#ifdef _SC_CHILD_MAX
6302 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6303#endif
6304#ifdef _SC_CLK_TCK
6305 {"SC_CLK_TCK", _SC_CLK_TCK},
6306#endif
6307#ifdef _SC_COHER_BLKSZ
6308 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6309#endif
6310#ifdef _SC_COLL_WEIGHTS_MAX
6311 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6312#endif
6313#ifdef _SC_DCACHE_ASSOC
6314 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6315#endif
6316#ifdef _SC_DCACHE_BLKSZ
6317 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6318#endif
6319#ifdef _SC_DCACHE_LINESZ
6320 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6321#endif
6322#ifdef _SC_DCACHE_SZ
6323 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6324#endif
6325#ifdef _SC_DCACHE_TBLKSZ
6326 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6327#endif
6328#ifdef _SC_DELAYTIMER_MAX
6329 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6330#endif
6331#ifdef _SC_EQUIV_CLASS_MAX
6332 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6333#endif
6334#ifdef _SC_EXPR_NEST_MAX
6335 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6336#endif
6337#ifdef _SC_FSYNC
6338 {"SC_FSYNC", _SC_FSYNC},
6339#endif
6340#ifdef _SC_GETGR_R_SIZE_MAX
6341 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6342#endif
6343#ifdef _SC_GETPW_R_SIZE_MAX
6344 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6345#endif
6346#ifdef _SC_ICACHE_ASSOC
6347 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6348#endif
6349#ifdef _SC_ICACHE_BLKSZ
6350 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6351#endif
6352#ifdef _SC_ICACHE_LINESZ
6353 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6354#endif
6355#ifdef _SC_ICACHE_SZ
6356 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6357#endif
Fred Draked86ed291999-12-15 15:34:33 +00006358#ifdef _SC_INF
6359 {"SC_INF", _SC_INF},
6360#endif
Fred Drakec9680921999-12-13 16:37:25 +00006361#ifdef _SC_INT_MAX
6362 {"SC_INT_MAX", _SC_INT_MAX},
6363#endif
6364#ifdef _SC_INT_MIN
6365 {"SC_INT_MIN", _SC_INT_MIN},
6366#endif
6367#ifdef _SC_IOV_MAX
6368 {"SC_IOV_MAX", _SC_IOV_MAX},
6369#endif
Fred Draked86ed291999-12-15 15:34:33 +00006370#ifdef _SC_IP_SECOPTS
6371 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6372#endif
Fred Drakec9680921999-12-13 16:37:25 +00006373#ifdef _SC_JOB_CONTROL
6374 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6375#endif
Fred Draked86ed291999-12-15 15:34:33 +00006376#ifdef _SC_KERN_POINTERS
6377 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6378#endif
6379#ifdef _SC_KERN_SIM
6380 {"SC_KERN_SIM", _SC_KERN_SIM},
6381#endif
Fred Drakec9680921999-12-13 16:37:25 +00006382#ifdef _SC_LINE_MAX
6383 {"SC_LINE_MAX", _SC_LINE_MAX},
6384#endif
6385#ifdef _SC_LOGIN_NAME_MAX
6386 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6387#endif
6388#ifdef _SC_LOGNAME_MAX
6389 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6390#endif
6391#ifdef _SC_LONG_BIT
6392 {"SC_LONG_BIT", _SC_LONG_BIT},
6393#endif
Fred Draked86ed291999-12-15 15:34:33 +00006394#ifdef _SC_MAC
6395 {"SC_MAC", _SC_MAC},
6396#endif
Fred Drakec9680921999-12-13 16:37:25 +00006397#ifdef _SC_MAPPED_FILES
6398 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6399#endif
6400#ifdef _SC_MAXPID
6401 {"SC_MAXPID", _SC_MAXPID},
6402#endif
6403#ifdef _SC_MB_LEN_MAX
6404 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6405#endif
6406#ifdef _SC_MEMLOCK
6407 {"SC_MEMLOCK", _SC_MEMLOCK},
6408#endif
6409#ifdef _SC_MEMLOCK_RANGE
6410 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6411#endif
6412#ifdef _SC_MEMORY_PROTECTION
6413 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6414#endif
6415#ifdef _SC_MESSAGE_PASSING
6416 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6417#endif
Fred Draked86ed291999-12-15 15:34:33 +00006418#ifdef _SC_MMAP_FIXED_ALIGNMENT
6419 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6420#endif
Fred Drakec9680921999-12-13 16:37:25 +00006421#ifdef _SC_MQ_OPEN_MAX
6422 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6423#endif
6424#ifdef _SC_MQ_PRIO_MAX
6425 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6426#endif
Fred Draked86ed291999-12-15 15:34:33 +00006427#ifdef _SC_NACLS_MAX
6428 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6429#endif
Fred Drakec9680921999-12-13 16:37:25 +00006430#ifdef _SC_NGROUPS_MAX
6431 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6432#endif
6433#ifdef _SC_NL_ARGMAX
6434 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6435#endif
6436#ifdef _SC_NL_LANGMAX
6437 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6438#endif
6439#ifdef _SC_NL_MSGMAX
6440 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6441#endif
6442#ifdef _SC_NL_NMAX
6443 {"SC_NL_NMAX", _SC_NL_NMAX},
6444#endif
6445#ifdef _SC_NL_SETMAX
6446 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6447#endif
6448#ifdef _SC_NL_TEXTMAX
6449 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6450#endif
6451#ifdef _SC_NPROCESSORS_CONF
6452 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6453#endif
6454#ifdef _SC_NPROCESSORS_ONLN
6455 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6456#endif
Fred Draked86ed291999-12-15 15:34:33 +00006457#ifdef _SC_NPROC_CONF
6458 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6459#endif
6460#ifdef _SC_NPROC_ONLN
6461 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6462#endif
Fred Drakec9680921999-12-13 16:37:25 +00006463#ifdef _SC_NZERO
6464 {"SC_NZERO", _SC_NZERO},
6465#endif
6466#ifdef _SC_OPEN_MAX
6467 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6468#endif
6469#ifdef _SC_PAGESIZE
6470 {"SC_PAGESIZE", _SC_PAGESIZE},
6471#endif
6472#ifdef _SC_PAGE_SIZE
6473 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6474#endif
6475#ifdef _SC_PASS_MAX
6476 {"SC_PASS_MAX", _SC_PASS_MAX},
6477#endif
6478#ifdef _SC_PHYS_PAGES
6479 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6480#endif
6481#ifdef _SC_PII
6482 {"SC_PII", _SC_PII},
6483#endif
6484#ifdef _SC_PII_INTERNET
6485 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6486#endif
6487#ifdef _SC_PII_INTERNET_DGRAM
6488 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6489#endif
6490#ifdef _SC_PII_INTERNET_STREAM
6491 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6492#endif
6493#ifdef _SC_PII_OSI
6494 {"SC_PII_OSI", _SC_PII_OSI},
6495#endif
6496#ifdef _SC_PII_OSI_CLTS
6497 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6498#endif
6499#ifdef _SC_PII_OSI_COTS
6500 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6501#endif
6502#ifdef _SC_PII_OSI_M
6503 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6504#endif
6505#ifdef _SC_PII_SOCKET
6506 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6507#endif
6508#ifdef _SC_PII_XTI
6509 {"SC_PII_XTI", _SC_PII_XTI},
6510#endif
6511#ifdef _SC_POLL
6512 {"SC_POLL", _SC_POLL},
6513#endif
6514#ifdef _SC_PRIORITIZED_IO
6515 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6516#endif
6517#ifdef _SC_PRIORITY_SCHEDULING
6518 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6519#endif
6520#ifdef _SC_REALTIME_SIGNALS
6521 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6522#endif
6523#ifdef _SC_RE_DUP_MAX
6524 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6525#endif
6526#ifdef _SC_RTSIG_MAX
6527 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6528#endif
6529#ifdef _SC_SAVED_IDS
6530 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6531#endif
6532#ifdef _SC_SCHAR_MAX
6533 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6534#endif
6535#ifdef _SC_SCHAR_MIN
6536 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6537#endif
6538#ifdef _SC_SELECT
6539 {"SC_SELECT", _SC_SELECT},
6540#endif
6541#ifdef _SC_SEMAPHORES
6542 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6543#endif
6544#ifdef _SC_SEM_NSEMS_MAX
6545 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6546#endif
6547#ifdef _SC_SEM_VALUE_MAX
6548 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6549#endif
6550#ifdef _SC_SHARED_MEMORY_OBJECTS
6551 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6552#endif
6553#ifdef _SC_SHRT_MAX
6554 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6555#endif
6556#ifdef _SC_SHRT_MIN
6557 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6558#endif
6559#ifdef _SC_SIGQUEUE_MAX
6560 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6561#endif
6562#ifdef _SC_SIGRT_MAX
6563 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6564#endif
6565#ifdef _SC_SIGRT_MIN
6566 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6567#endif
Fred Draked86ed291999-12-15 15:34:33 +00006568#ifdef _SC_SOFTPOWER
6569 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6570#endif
Fred Drakec9680921999-12-13 16:37:25 +00006571#ifdef _SC_SPLIT_CACHE
6572 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6573#endif
6574#ifdef _SC_SSIZE_MAX
6575 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6576#endif
6577#ifdef _SC_STACK_PROT
6578 {"SC_STACK_PROT", _SC_STACK_PROT},
6579#endif
6580#ifdef _SC_STREAM_MAX
6581 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6582#endif
6583#ifdef _SC_SYNCHRONIZED_IO
6584 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6585#endif
6586#ifdef _SC_THREADS
6587 {"SC_THREADS", _SC_THREADS},
6588#endif
6589#ifdef _SC_THREAD_ATTR_STACKADDR
6590 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6591#endif
6592#ifdef _SC_THREAD_ATTR_STACKSIZE
6593 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6594#endif
6595#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6596 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6597#endif
6598#ifdef _SC_THREAD_KEYS_MAX
6599 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6600#endif
6601#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6602 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6603#endif
6604#ifdef _SC_THREAD_PRIO_INHERIT
6605 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6606#endif
6607#ifdef _SC_THREAD_PRIO_PROTECT
6608 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6609#endif
6610#ifdef _SC_THREAD_PROCESS_SHARED
6611 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6612#endif
6613#ifdef _SC_THREAD_SAFE_FUNCTIONS
6614 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6615#endif
6616#ifdef _SC_THREAD_STACK_MIN
6617 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6618#endif
6619#ifdef _SC_THREAD_THREADS_MAX
6620 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6621#endif
6622#ifdef _SC_TIMERS
6623 {"SC_TIMERS", _SC_TIMERS},
6624#endif
6625#ifdef _SC_TIMER_MAX
6626 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6627#endif
6628#ifdef _SC_TTY_NAME_MAX
6629 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6630#endif
6631#ifdef _SC_TZNAME_MAX
6632 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6633#endif
6634#ifdef _SC_T_IOV_MAX
6635 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6636#endif
6637#ifdef _SC_UCHAR_MAX
6638 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6639#endif
6640#ifdef _SC_UINT_MAX
6641 {"SC_UINT_MAX", _SC_UINT_MAX},
6642#endif
6643#ifdef _SC_UIO_MAXIOV
6644 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6645#endif
6646#ifdef _SC_ULONG_MAX
6647 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6648#endif
6649#ifdef _SC_USHRT_MAX
6650 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6651#endif
6652#ifdef _SC_VERSION
6653 {"SC_VERSION", _SC_VERSION},
6654#endif
6655#ifdef _SC_WORD_BIT
6656 {"SC_WORD_BIT", _SC_WORD_BIT},
6657#endif
6658#ifdef _SC_XBS5_ILP32_OFF32
6659 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6660#endif
6661#ifdef _SC_XBS5_ILP32_OFFBIG
6662 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6663#endif
6664#ifdef _SC_XBS5_LP64_OFF64
6665 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6666#endif
6667#ifdef _SC_XBS5_LPBIG_OFFBIG
6668 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6669#endif
6670#ifdef _SC_XOPEN_CRYPT
6671 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6672#endif
6673#ifdef _SC_XOPEN_ENH_I18N
6674 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6675#endif
6676#ifdef _SC_XOPEN_LEGACY
6677 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6678#endif
6679#ifdef _SC_XOPEN_REALTIME
6680 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6681#endif
6682#ifdef _SC_XOPEN_REALTIME_THREADS
6683 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6684#endif
6685#ifdef _SC_XOPEN_SHM
6686 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6687#endif
6688#ifdef _SC_XOPEN_UNIX
6689 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6690#endif
6691#ifdef _SC_XOPEN_VERSION
6692 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6693#endif
6694#ifdef _SC_XOPEN_XCU_VERSION
6695 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6696#endif
6697#ifdef _SC_XOPEN_XPG2
6698 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6699#endif
6700#ifdef _SC_XOPEN_XPG3
6701 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6702#endif
6703#ifdef _SC_XOPEN_XPG4
6704 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6705#endif
6706};
6707
6708static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006709conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006710{
6711 return conv_confname(arg, valuep, posix_constants_sysconf,
6712 sizeof(posix_constants_sysconf)
6713 / sizeof(struct constdef));
6714}
6715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006716PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006717"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006718Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006719
6720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006721posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006722{
6723 PyObject *result = NULL;
6724 int name;
6725
6726 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6727 int value;
6728
6729 errno = 0;
6730 value = sysconf(name);
6731 if (value == -1 && errno != 0)
6732 posix_error();
6733 else
6734 result = PyInt_FromLong(value);
6735 }
6736 return result;
6737}
6738#endif
6739
6740
Fred Drakebec628d1999-12-15 18:31:10 +00006741/* This code is used to ensure that the tables of configuration value names
6742 * are in sorted order as required by conv_confname(), and also to build the
6743 * the exported dictionaries that are used to publish information about the
6744 * names available on the host platform.
6745 *
6746 * Sorting the table at runtime ensures that the table is properly ordered
6747 * when used, even for platforms we're not able to test on. It also makes
6748 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006749 */
Fred Drakebec628d1999-12-15 18:31:10 +00006750
6751static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006752cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006753{
6754 const struct constdef *c1 =
6755 (const struct constdef *) v1;
6756 const struct constdef *c2 =
6757 (const struct constdef *) v2;
6758
6759 return strcmp(c1->name, c2->name);
6760}
6761
6762static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006763setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006764 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006765{
Fred Drakebec628d1999-12-15 18:31:10 +00006766 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006767 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006768
6769 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6770 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006771 if (d == NULL)
6772 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006773
Barry Warsaw3155db32000-04-13 15:20:40 +00006774 for (i=0; i < tablesize; ++i) {
6775 PyObject *o = PyInt_FromLong(table[i].value);
6776 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6777 Py_XDECREF(o);
6778 Py_DECREF(d);
6779 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006780 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006781 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006782 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006783 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006784}
6785
Fred Drakebec628d1999-12-15 18:31:10 +00006786/* Return -1 on failure, 0 on success. */
6787static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006788setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006789{
6790#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006791 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006792 sizeof(posix_constants_pathconf)
6793 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006794 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006795 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006796#endif
6797#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006798 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006799 sizeof(posix_constants_confstr)
6800 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006801 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006802 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006803#endif
6804#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006805 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006806 sizeof(posix_constants_sysconf)
6807 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006808 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006809 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006810#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006811 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006812}
Fred Draked86ed291999-12-15 15:34:33 +00006813
6814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006815PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006816"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006817Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006818in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006819
6820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006821posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006822{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006823 abort();
6824 /*NOTREACHED*/
6825 Py_FatalError("abort() called from Python code didn't abort!");
6826 return NULL;
6827}
Fred Drakebec628d1999-12-15 18:31:10 +00006828
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006829#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006830PyDoc_STRVAR(win32_startfile__doc__,
6831"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006832\n\
6833This acts like double-clicking the file in Explorer, or giving the file\n\
6834name as an argument to the DOS \"start\" command: the file is opened\n\
6835with whatever application (if any) its extension is associated.\n\
6836\n\
6837startfile returns as soon as the associated application is launched.\n\
6838There is no option to wait for the application to close, and no way\n\
6839to retrieve the application's exit status.\n\
6840\n\
6841The filepath is relative to the current directory. If you want to use\n\
6842an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006843the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006844
6845static PyObject *
6846win32_startfile(PyObject *self, PyObject *args)
6847{
6848 char *filepath;
6849 HINSTANCE rc;
6850 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
6851 return NULL;
6852 Py_BEGIN_ALLOW_THREADS
6853 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
6854 Py_END_ALLOW_THREADS
6855 if (rc <= (HINSTANCE)32)
6856 return win32_error("startfile", filepath);
6857 Py_INCREF(Py_None);
6858 return Py_None;
6859}
6860#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006861
Martin v. Löwis438b5342002-12-27 10:16:42 +00006862#ifdef HAVE_GETLOADAVG
6863PyDoc_STRVAR(posix_getloadavg__doc__,
6864"getloadavg() -> (float, float, float)\n\n\
6865Return the number of processes in the system run queue averaged over\n\
6866the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6867was unobtainable");
6868
6869static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006870posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006871{
6872 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006873 if (getloadavg(loadavg, 3)!=3) {
6874 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6875 return NULL;
6876 } else
6877 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6878}
6879#endif
6880
6881
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006882static PyMethodDef posix_methods[] = {
6883 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6884#ifdef HAVE_TTYNAME
6885 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6886#endif
6887 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
6888 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006889#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006890 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006891#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006892#ifdef HAVE_LCHOWN
6893 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6894#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006895#ifdef HAVE_CHROOT
6896 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6897#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006898#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006899 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006900#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006901#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006902 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00006903#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00006904 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006905#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00006906#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006907#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006908 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006909#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006910 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6911 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6912 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006913#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006914 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006915#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006916#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006917 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006918#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006919 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6920 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6921 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006922 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006923#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006924 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006925#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006926#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006927 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006928#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006929 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006930#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006931 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006932#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006933 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6934 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6935 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006936#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006937 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006938#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006939 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006940#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006941 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6942 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006943#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006944#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006945 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6946 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00006947#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006948#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006949 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006950#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006951#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006952 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006953#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006954#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006955 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006956#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006957#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006958 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006959#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006960#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006961 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006962#endif /* HAVE_GETEGID */
6963#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006964 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006965#endif /* HAVE_GETEUID */
6966#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006967 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006968#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006969#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006970 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006972 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006973#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006974 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006975#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006976#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006977 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006978#endif /* HAVE_GETPPID */
6979#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006980 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006981#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006982#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006983 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006984#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006985#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006986 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006987#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006988#ifdef HAVE_KILLPG
6989 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6990#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006991#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006992 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006993#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006994#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006995 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006996#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006997 {"popen2", win32_popen2, METH_VARARGS},
6998 {"popen3", win32_popen3, METH_VARARGS},
6999 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007000 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007001#else
7002#if defined(PYOS_OS2) && defined(PYCC_GCC)
7003 {"popen2", os2emx_popen2, METH_VARARGS},
7004 {"popen3", os2emx_popen3, METH_VARARGS},
7005 {"popen4", os2emx_popen4, METH_VARARGS},
7006#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007007#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007008#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007009#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007010 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007011#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007012#ifdef HAVE_SETEUID
7013 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7014#endif /* HAVE_SETEUID */
7015#ifdef HAVE_SETEGID
7016 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7017#endif /* HAVE_SETEGID */
7018#ifdef HAVE_SETREUID
7019 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7020#endif /* HAVE_SETREUID */
7021#ifdef HAVE_SETREGID
7022 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7023#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007024#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007025 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007026#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007027#ifdef HAVE_SETGROUPS
7028 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7029#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007030#ifdef HAVE_GETPGID
7031 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7032#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007033#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007034 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007035#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007036#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007037 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007038#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007039#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007040 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007041#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007042#ifdef HAVE_GETSID
7043 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7044#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007045#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007046 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007047#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007048#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007049 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007050#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007051#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007052 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007053#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007054#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007055 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007056#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007057 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7058 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7059 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7060 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7061 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7062 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7063 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7064 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7065 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007066 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007067#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007068 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007069#endif
7070#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007071 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007072#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007073#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007074 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7075#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007076#ifdef HAVE_DEVICE_MACROS
7077 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7078 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7079 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7080#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007081#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007082 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007083#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007084#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007085 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007086#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007087#ifdef HAVE_UNSETENV
7088 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7089#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007090#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007091 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007092#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007093#ifdef HAVE_FCHDIR
7094 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7095#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007096#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007097 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007098#endif
7099#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007100 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007101#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007102#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007103#ifdef WCOREDUMP
7104 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7105#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007106#ifdef WIFCONTINUED
7107 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7108#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007109#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007110 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007111#endif /* WIFSTOPPED */
7112#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007113 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007114#endif /* WIFSIGNALED */
7115#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007116 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007117#endif /* WIFEXITED */
7118#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007119 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007120#endif /* WEXITSTATUS */
7121#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007122 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007123#endif /* WTERMSIG */
7124#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007125 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007126#endif /* WSTOPSIG */
7127#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007128#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007129 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007130#endif
7131#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007132 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007133#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007134#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007135 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007136#endif
7137#ifdef HAVE_TEMPNAM
7138 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7139#endif
7140#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007141 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007142#endif
Fred Drakec9680921999-12-13 16:37:25 +00007143#ifdef HAVE_CONFSTR
7144 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7145#endif
7146#ifdef HAVE_SYSCONF
7147 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7148#endif
7149#ifdef HAVE_FPATHCONF
7150 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7151#endif
7152#ifdef HAVE_PATHCONF
7153 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7154#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007155 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007156#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007157 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7158#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007159#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007160 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007161#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007162 {NULL, NULL} /* Sentinel */
7163};
7164
7165
Barry Warsaw4a342091996-12-19 23:50:02 +00007166static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007167ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007168{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007169 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007170}
7171
Guido van Rossumd48f2521997-12-05 22:19:34 +00007172#if defined(PYOS_OS2)
7173/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007174static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007175{
7176 APIRET rc;
7177 ULONG values[QSV_MAX+1];
7178 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007179 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007180
7181 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007182 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007183 Py_END_ALLOW_THREADS
7184
7185 if (rc != NO_ERROR) {
7186 os2_error(rc);
7187 return -1;
7188 }
7189
Fred Drake4d1e64b2002-04-15 19:40:07 +00007190 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7191 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7192 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7193 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7194 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7195 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7196 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007197
7198 switch (values[QSV_VERSION_MINOR]) {
7199 case 0: ver = "2.00"; break;
7200 case 10: ver = "2.10"; break;
7201 case 11: ver = "2.11"; break;
7202 case 30: ver = "3.00"; break;
7203 case 40: ver = "4.00"; break;
7204 case 50: ver = "5.00"; break;
7205 default:
Tim Peters885d4572001-11-28 20:27:42 +00007206 PyOS_snprintf(tmp, sizeof(tmp),
7207 "%d-%d", values[QSV_VERSION_MAJOR],
7208 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007209 ver = &tmp[0];
7210 }
7211
7212 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007213 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007214 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007215
7216 /* Add Indicator of Which Drive was Used to Boot the System */
7217 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7218 tmp[1] = ':';
7219 tmp[2] = '\0';
7220
Fred Drake4d1e64b2002-04-15 19:40:07 +00007221 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007222}
7223#endif
7224
Barry Warsaw4a342091996-12-19 23:50:02 +00007225static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007226all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007227{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007228#ifdef F_OK
7229 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007230#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007231#ifdef R_OK
7232 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007233#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007234#ifdef W_OK
7235 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007236#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007237#ifdef X_OK
7238 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007239#endif
Fred Drakec9680921999-12-13 16:37:25 +00007240#ifdef NGROUPS_MAX
7241 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7242#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007243#ifdef TMP_MAX
7244 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7245#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007246#ifdef WCONTINUED
7247 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7248#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007249#ifdef WNOHANG
7250 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007251#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007252#ifdef WUNTRACED
7253 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7254#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007255#ifdef O_RDONLY
7256 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7257#endif
7258#ifdef O_WRONLY
7259 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7260#endif
7261#ifdef O_RDWR
7262 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7263#endif
7264#ifdef O_NDELAY
7265 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7266#endif
7267#ifdef O_NONBLOCK
7268 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7269#endif
7270#ifdef O_APPEND
7271 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7272#endif
7273#ifdef O_DSYNC
7274 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7275#endif
7276#ifdef O_RSYNC
7277 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7278#endif
7279#ifdef O_SYNC
7280 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7281#endif
7282#ifdef O_NOCTTY
7283 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7284#endif
7285#ifdef O_CREAT
7286 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7287#endif
7288#ifdef O_EXCL
7289 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7290#endif
7291#ifdef O_TRUNC
7292 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7293#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007294#ifdef O_BINARY
7295 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7296#endif
7297#ifdef O_TEXT
7298 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7299#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007300#ifdef O_LARGEFILE
7301 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7302#endif
7303
Tim Peters5aa91602002-01-30 05:46:57 +00007304/* MS Windows */
7305#ifdef O_NOINHERIT
7306 /* Don't inherit in child processes. */
7307 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7308#endif
7309#ifdef _O_SHORT_LIVED
7310 /* Optimize for short life (keep in memory). */
7311 /* MS forgot to define this one with a non-underscore form too. */
7312 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7313#endif
7314#ifdef O_TEMPORARY
7315 /* Automatically delete when last handle is closed. */
7316 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7317#endif
7318#ifdef O_RANDOM
7319 /* Optimize for random access. */
7320 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7321#endif
7322#ifdef O_SEQUENTIAL
7323 /* Optimize for sequential access. */
7324 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7325#endif
7326
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007327/* GNU extensions. */
7328#ifdef O_DIRECT
7329 /* Direct disk access. */
7330 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7331#endif
7332#ifdef O_DIRECTORY
7333 /* Must be a directory. */
7334 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7335#endif
7336#ifdef O_NOFOLLOW
7337 /* Do not follow links. */
7338 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7339#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007340
Barry Warsaw5676bd12003-01-07 20:57:09 +00007341 /* These come from sysexits.h */
7342#ifdef EX_OK
7343 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007344#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007345#ifdef EX_USAGE
7346 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007347#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007348#ifdef EX_DATAERR
7349 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007350#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007351#ifdef EX_NOINPUT
7352 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007353#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007354#ifdef EX_NOUSER
7355 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007356#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007357#ifdef EX_NOHOST
7358 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007359#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007360#ifdef EX_UNAVAILABLE
7361 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007362#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007363#ifdef EX_SOFTWARE
7364 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007365#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007366#ifdef EX_OSERR
7367 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007368#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007369#ifdef EX_OSFILE
7370 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007371#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007372#ifdef EX_CANTCREAT
7373 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007374#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007375#ifdef EX_IOERR
7376 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007377#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007378#ifdef EX_TEMPFAIL
7379 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007380#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007381#ifdef EX_PROTOCOL
7382 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007383#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007384#ifdef EX_NOPERM
7385 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007386#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007387#ifdef EX_CONFIG
7388 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007389#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007390#ifdef EX_NOTFOUND
7391 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007392#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007393
Guido van Rossum246bc171999-02-01 23:54:31 +00007394#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007395#if defined(PYOS_OS2) && defined(PYCC_GCC)
7396 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7397 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7398 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7399 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7400 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7401 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7402 if (ins(d, "P_PM", (long)P_PM)) return -1;
7403 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7404 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7405 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7406 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7407 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7408 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7409 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7410 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7411 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7412 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7413 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7414 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7415 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7416#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007417 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7418 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7419 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7420 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7421 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007422#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007423#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007424
Guido van Rossumd48f2521997-12-05 22:19:34 +00007425#if defined(PYOS_OS2)
7426 if (insertvalues(d)) return -1;
7427#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007428 return 0;
7429}
7430
7431
Tim Peters5aa91602002-01-30 05:46:57 +00007432#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007433#define INITFUNC initnt
7434#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007435
7436#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007437#define INITFUNC initos2
7438#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007439
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007440#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007441#define INITFUNC initposix
7442#define MODNAME "posix"
7443#endif
7444
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007445PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007446INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007447{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007448 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007449
Fred Drake4d1e64b2002-04-15 19:40:07 +00007450 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007451 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007452 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007453
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007454 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007455 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007456 Py_XINCREF(v);
7457 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007458 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007459 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007460
Fred Drake4d1e64b2002-04-15 19:40:07 +00007461 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007462 return;
7463
Fred Drake4d1e64b2002-04-15 19:40:07 +00007464 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007465 return;
7466
Fred Drake4d1e64b2002-04-15 19:40:07 +00007467 Py_INCREF(PyExc_OSError);
7468 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007469
Guido van Rossumb3d39562000-01-31 18:41:26 +00007470#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007471 if (posix_putenv_garbage == NULL)
7472 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007473#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007474
Guido van Rossum14648392001-12-08 18:02:58 +00007475 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007476 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7477 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7478 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007479 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007480 structseq_new = StatResultType.tp_new;
7481 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007482 Py_INCREF((PyObject*) &StatResultType);
7483 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007484
Guido van Rossum14648392001-12-08 18:02:58 +00007485 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007486 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007487 Py_INCREF((PyObject*) &StatVFSResultType);
7488 PyModule_AddObject(m, "statvfs_result",
7489 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007490}