blob: 53f35da4272ea530684d2c2e1111320d53454157 [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
Thomas Wouters68bc4f92006-03-01 01:05:10 +000016#define PY_SSIZE_T_CLEAN
17
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018#include "Python.h"
19#include "structseq.h"
20
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000021#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000022# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000023#endif /* defined(__VMS) */
24
Anthony Baxterac6bd462006-04-13 02:06:09 +000025#ifdef __cplusplus
26extern "C" {
27#endif
28
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000030"This module provides access to operating system functionality that is\n\
31standardized by the C Standard and the POSIX standard (a thinly\n\
32disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000035#ifndef Py_USING_UNICODE
36/* This is used in signatures of functions. */
37#define Py_UNICODE void
38#endif
39
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000040#if defined(PYOS_OS2)
41#define INCL_DOS
42#define INCL_DOSERRORS
43#define INCL_DOSPROCESS
44#define INCL_NOPMAPI
45#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000046#if defined(PYCC_GCC)
47#include <ctype.h>
48#include <io.h>
49#include <stdio.h>
50#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000051#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000052#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000053#endif
54
Guido van Rossumb6775db1994-08-01 11:34:53 +000055#include <sys/types.h>
56#include <sys/stat.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000057
Guido van Rossum36bc6801995-06-14 22:54:23 +000058#ifdef HAVE_SYS_WAIT_H
59#include <sys/wait.h> /* For WNOHANG */
60#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000061
Guido van Rossuma376cc51996-12-05 23:43:35 +000062#include <signal.h>
Guido van Rossuma376cc51996-12-05 23:43:35 +000063
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#ifdef HAVE_FCNTL_H
65#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000066#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000067
Guido van Rossuma6535fd2001-10-18 19:44:10 +000068#ifdef HAVE_GRP_H
69#include <grp.h>
70#endif
71
Barry Warsaw5676bd12003-01-07 20:57:09 +000072#ifdef HAVE_SYSEXITS_H
73#include <sysexits.h>
74#endif /* HAVE_SYSEXITS_H */
75
Anthony Baxter8a560de2004-10-13 15:30:56 +000076#ifdef HAVE_SYS_LOADAVG_H
77#include <sys/loadavg.h>
78#endif
79
Guido van Rossuma4916fa1996-05-23 22:58:55 +000080/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000081/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000082#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000083#include <process.h>
84#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000085#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000086#define HAVE_GETCWD 1
87#define HAVE_OPENDIR 1
88#define HAVE_SYSTEM 1
89#if defined(__OS2__)
90#define HAVE_EXECV 1
91#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000092#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000093#include <process.h>
94#else
95#ifdef __BORLANDC__ /* Borland compiler */
96#define HAVE_EXECV 1
97#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098#define HAVE_OPENDIR 1
99#define HAVE_PIPE 1
100#define HAVE_POPEN 1
101#define HAVE_SYSTEM 1
102#define HAVE_WAIT 1
103#else
104#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000105#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000106#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#define HAVE_EXECV 1
108#define HAVE_PIPE 1
109#define HAVE_POPEN 1
110#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000111#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000112#define HAVE_FSYNC 1
113#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000114#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
116/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#else /* all other compilers */
118/* Unix functions that the configure script doesn't check for */
119#define HAVE_EXECV 1
120#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000121#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
122#define HAVE_FORK1 1
123#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#define HAVE_GETCWD 1
125#define HAVE_GETEGID 1
126#define HAVE_GETEUID 1
127#define HAVE_GETGID 1
128#define HAVE_GETPPID 1
129#define HAVE_GETUID 1
130#define HAVE_KILL 1
131#define HAVE_OPENDIR 1
132#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000133#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000135#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#define HAVE_SYSTEM 1
137#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000138#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000139#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140#endif /* _MSC_VER */
141#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000142#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000143#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000144
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000145#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000146
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000147#if defined(__sgi)&&_COMPILER_VERSION>=700
148/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
149 (default) */
150extern char *ctermid_r(char *);
151#endif
152
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000153#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000154#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000155extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000158extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000160extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#endif
163#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000164extern int chdir(char *);
165extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000166#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000167extern int chdir(const char *);
168extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000170#ifdef __BORLANDC__
171extern int chmod(const char *, int);
172#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000174#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int chown(const char *, uid_t, gid_t);
176extern char *getcwd(char *, int);
177extern char *strerror(int);
178extern int link(const char *, const char *);
179extern int rename(const char *, const char *);
180extern int stat(const char *, struct stat *);
181extern int unlink(const char *);
182extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000184extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000185#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000188#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000190
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000191#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192
Guido van Rossumb6775db1994-08-01 11:34:53 +0000193#ifdef HAVE_UTIME_H
194#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000195#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000197#ifdef HAVE_SYS_UTIME_H
198#include <sys/utime.h>
199#define HAVE_UTIME_H /* pretend we do for the rest of this file */
200#endif /* HAVE_SYS_UTIME_H */
201
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYS_TIMES_H
203#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000204#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205
206#ifdef HAVE_SYS_PARAM_H
207#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
210#ifdef HAVE_SYS_UTSNAME_H
211#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000212#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000214#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000216#define NAMLEN(dirent) strlen((dirent)->d_name)
217#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000218#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000219#include <direct.h>
220#define NAMLEN(dirent) strlen((dirent)->d_name)
221#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000223#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000224#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000225#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#endif
228#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000230#endif
231#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#endif
234#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237#include <direct.h>
238#include <io.h>
239#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000240#include "osdefs.h"
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000241#define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000243#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000245#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000246#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
Guido van Rossumd48f2521997-12-05 22:19:34 +0000248#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000250#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251
Tim Petersbc2e10e2002-03-03 23:17:02 +0000252#ifndef MAXPATHLEN
253#define MAXPATHLEN 1024
254#endif /* MAXPATHLEN */
255
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000256#ifdef UNION_WAIT
257/* Emulate some macros on systems that have a union instead of macros */
258
259#ifndef WIFEXITED
260#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
261#endif
262
263#ifndef WEXITSTATUS
264#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
265#endif
266
267#ifndef WTERMSIG
268#define WTERMSIG(u_wait) ((u_wait).w_termsig)
269#endif
270
Neal Norwitzd5a37542006-03-20 06:48:34 +0000271#define WAIT_TYPE union wait
272#define WAIT_STATUS_INT(s) (s.w_status)
273
274#else /* !UNION_WAIT */
275#define WAIT_TYPE int
276#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000277#endif /* UNION_WAIT */
278
Greg Wardb48bc172000-03-01 21:51:56 +0000279/* Don't use the "_r" form if we don't need it (also, won't have a
280 prototype for it, at least on Solaris -- maybe others as well?). */
281#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
282#define USE_CTERMID_R
283#endif
284
285#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
286#define USE_TMPNAM_R
287#endif
288
Fred Drake699f3522000-06-29 21:12:41 +0000289/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000290#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000291#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000292# define STAT win32_stat
293# define FSTAT win32_fstat
294# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000295#else
296# define STAT stat
297# define FSTAT fstat
298# define STRUCT_STAT struct stat
299#endif
300
Tim Peters11b23062003-04-23 02:39:17 +0000301#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000302#include <sys/mkdev.h>
303#else
304#if defined(MAJOR_IN_SYSMACROS)
305#include <sys/sysmacros.h>
306#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000307#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
308#include <sys/mkdev.h>
309#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000310#endif
Fred Drake699f3522000-06-29 21:12:41 +0000311
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000313#ifdef WITH_NEXT_FRAMEWORK
314/* On Darwin/MacOSX a shared library or framework has no access to
315** environ directly, we must obtain it with _NSGetEnviron().
316*/
317#include <crt_externs.h>
318static char **environ;
319#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000321#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322
Barry Warsaw53699e91996-12-10 23:23:01 +0000323static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000324convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325{
Barry Warsaw53699e91996-12-10 23:23:01 +0000326 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000328 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 if (d == NULL)
330 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000331#ifdef WITH_NEXT_FRAMEWORK
332 if (environ == NULL)
333 environ = *_NSGetEnviron();
334#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335 if (environ == NULL)
336 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000337 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000339 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000340 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341 char *p = strchr(*e, '=');
342 if (p == NULL)
343 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000344 k = PyString_FromStringAndSize(*e, (int)(p-*e));
345 if (k == NULL) {
346 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000348 }
349 v = PyString_FromString(p+1);
350 if (v == NULL) {
351 PyErr_Clear();
352 Py_DECREF(k);
353 continue;
354 }
355 if (PyDict_GetItem(d, k) == NULL) {
356 if (PyDict_SetItem(d, k, v) != 0)
357 PyErr_Clear();
358 }
359 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000362#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000363 {
364 APIRET rc;
365 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
366
367 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000368 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000369 PyObject *v = PyString_FromString(buffer);
370 PyDict_SetItemString(d, "BEGINLIBPATH", v);
371 Py_DECREF(v);
372 }
373 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
374 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
375 PyObject *v = PyString_FromString(buffer);
376 PyDict_SetItemString(d, "ENDLIBPATH", v);
377 Py_DECREF(v);
378 }
379 }
380#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 return d;
382}
383
384
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385/* Set a POSIX-specific error from errno, and return NULL */
386
Barry Warsawd58d7641998-07-23 16:14:40 +0000387static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000388posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000389{
Barry Warsawca74da41999-02-09 19:31:45 +0000390 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391}
Barry Warsawd58d7641998-07-23 16:14:40 +0000392static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000393posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000394{
Barry Warsawca74da41999-02-09 19:31:45 +0000395 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000396}
397
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000398#ifdef Py_WIN_WIDE_FILENAMES
399static PyObject *
400posix_error_with_unicode_filename(Py_UNICODE* name)
401{
402 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
403}
404#endif /* Py_WIN_WIDE_FILENAMES */
405
406
Mark Hammondef8b6542001-05-13 08:04:26 +0000407static PyObject *
408posix_error_with_allocated_filename(char* name)
409{
410 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
411 PyMem_Free(name);
412 return rc;
413}
414
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000415#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000416static PyObject *
417win32_error(char* function, char* filename)
418{
Mark Hammond33a6da92000-08-15 00:46:38 +0000419 /* XXX We should pass the function name along in the future.
420 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000421 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000422 Windows error object, which is non-trivial.
423 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000424 errno = GetLastError();
425 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000426 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000427 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000428 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000429}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000430
431#ifdef Py_WIN_WIDE_FILENAMES
432static PyObject *
433win32_error_unicode(char* function, Py_UNICODE* filename)
434{
435 /* XXX - see win32_error for comments on 'function' */
436 errno = GetLastError();
437 if (filename)
438 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
439 else
440 return PyErr_SetFromWindowsErr(errno);
441}
442
443static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
444{
445 /* XXX Perhaps we should make this API an alias of
446 PyObject_Unicode() instead ?! */
447 if (PyUnicode_CheckExact(obj)) {
448 Py_INCREF(obj);
449 return obj;
450 }
451 if (PyUnicode_Check(obj)) {
452 /* For a Unicode subtype that's not a Unicode object,
453 return a true Unicode object with the same data. */
454 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
455 PyUnicode_GET_SIZE(obj));
456 }
Tim Peters11b23062003-04-23 02:39:17 +0000457 return PyUnicode_FromEncodedObject(obj,
458 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000459 "strict");
460}
461
462#endif /* Py_WIN_WIDE_FILENAMES */
463
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000464#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465
Guido van Rossumd48f2521997-12-05 22:19:34 +0000466#if defined(PYOS_OS2)
467/**********************************************************************
468 * Helper Function to Trim and Format OS/2 Messages
469 **********************************************************************/
470 static void
471os2_formatmsg(char *msgbuf, int msglen, char *reason)
472{
473 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
474
475 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
476 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
477
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000478 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000479 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
480 }
481
482 /* Add Optional Reason Text */
483 if (reason) {
484 strcat(msgbuf, " : ");
485 strcat(msgbuf, reason);
486 }
487}
488
489/**********************************************************************
490 * Decode an OS/2 Operating System Error Code
491 *
492 * A convenience function to lookup an OS/2 error code and return a
493 * text message we can use to raise a Python exception.
494 *
495 * Notes:
496 * The messages for errors returned from the OS/2 kernel reside in
497 * the file OSO001.MSG in the \OS2 directory hierarchy.
498 *
499 **********************************************************************/
500 static char *
501os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
502{
503 APIRET rc;
504 ULONG msglen;
505
506 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
507 Py_BEGIN_ALLOW_THREADS
508 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
509 errorcode, "oso001.msg", &msglen);
510 Py_END_ALLOW_THREADS
511
512 if (rc == NO_ERROR)
513 os2_formatmsg(msgbuf, msglen, reason);
514 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000515 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000516 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000517
518 return msgbuf;
519}
520
521/* Set an OS/2-specific error and return NULL. OS/2 kernel
522 errors are not in a global variable e.g. 'errno' nor are
523 they congruent with posix error numbers. */
524
525static PyObject * os2_error(int code)
526{
527 char text[1024];
528 PyObject *v;
529
530 os2_strerror(text, sizeof(text), code, "");
531
532 v = Py_BuildValue("(is)", code, text);
533 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000534 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000535 Py_DECREF(v);
536 }
537 return NULL; /* Signal to Python that an Exception is Pending */
538}
539
540#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541
542/* POSIX generic methods */
543
Barry Warsaw53699e91996-12-10 23:23:01 +0000544static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000545posix_fildes(PyObject *fdobj, int (*func)(int))
546{
547 int fd;
548 int res;
549 fd = PyObject_AsFileDescriptor(fdobj);
550 if (fd < 0)
551 return NULL;
552 Py_BEGIN_ALLOW_THREADS
553 res = (*func)(fd);
554 Py_END_ALLOW_THREADS
555 if (res < 0)
556 return posix_error();
557 Py_INCREF(Py_None);
558 return Py_None;
559}
Guido van Rossum21142a01999-01-08 21:05:37 +0000560
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000561#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000562static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000563unicode_file_names(void)
564{
565 static int canusewide = -1;
566 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000567 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000568 the Windows NT family. */
569 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
570 }
571 return canusewide;
572}
573#endif
Tim Peters11b23062003-04-23 02:39:17 +0000574
Guido van Rossum21142a01999-01-08 21:05:37 +0000575static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576posix_1str(PyObject *args, char *format, int (*func)(const char*),
577 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578{
Mark Hammondef8b6542001-05-13 08:04:26 +0000579 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000580 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000581#ifdef Py_WIN_WIDE_FILENAMES
582 if (unicode_file_names()) {
583 PyUnicodeObject *po;
584 if (PyArg_ParseTuple(args, wformat, &po)) {
585 Py_BEGIN_ALLOW_THREADS
586 /* PyUnicode_AS_UNICODE OK without thread
587 lock as it is a simple dereference. */
588 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
589 Py_END_ALLOW_THREADS
590 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000591 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000592 Py_INCREF(Py_None);
593 return Py_None;
594 }
595 /* Drop the argument parsing error as narrow
596 strings are also valid. */
597 PyErr_Clear();
598 }
599#else
600 /* Platforms that don't support Unicode filenames
601 shouldn't be passing these extra params */
602 assert(wformat==NULL && wfunc == NULL);
603#endif
604
Tim Peters5aa91602002-01-30 05:46:57 +0000605 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000606 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000608 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000609 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000610 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000611 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000612 return posix_error_with_allocated_filename(path1);
613 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000614 Py_INCREF(Py_None);
615 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616}
617
Barry Warsaw53699e91996-12-10 23:23:01 +0000618static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000619posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000620 char *format,
621 int (*func)(const char *, const char *),
622 char *wformat,
623 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624{
Mark Hammondef8b6542001-05-13 08:04:26 +0000625 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000626 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000627#ifdef Py_WIN_WIDE_FILENAMES
628 if (unicode_file_names()) {
629 PyObject *po1;
630 PyObject *po2;
631 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
632 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
633 PyObject *wpath1;
634 PyObject *wpath2;
635 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
636 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
637 if (!wpath1 || !wpath2) {
638 Py_XDECREF(wpath1);
639 Py_XDECREF(wpath2);
640 return NULL;
641 }
642 Py_BEGIN_ALLOW_THREADS
643 /* PyUnicode_AS_UNICODE OK without thread
644 lock as it is a simple dereference. */
645 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
646 PyUnicode_AS_UNICODE(wpath2));
647 Py_END_ALLOW_THREADS
648 Py_XDECREF(wpath1);
649 Py_XDECREF(wpath2);
650 if (res != 0)
651 return posix_error();
652 Py_INCREF(Py_None);
653 return Py_None;
654 }
655 /* Else flow through as neither is Unicode. */
656 }
657 /* Drop the argument parsing error as narrow
658 strings are also valid. */
659 PyErr_Clear();
660 }
661#else
662 /* Platforms that don't support Unicode filenames
663 shouldn't be passing these extra params */
664 assert(wformat==NULL && wfunc == NULL);
665#endif
666
Mark Hammondef8b6542001-05-13 08:04:26 +0000667 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000668 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000669 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000671 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000672 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000673 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000674 PyMem_Free(path1);
675 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000676 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000677 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000679 Py_INCREF(Py_None);
680 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681}
682
Martin v. Löwis14694662006-02-03 12:54:16 +0000683#ifdef MS_WINDOWS
684/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
685 - time stamps are restricted to second resolution
686 - file modification times suffer from forth-and-back conversions between
687 UTC and local time
688 Therefore, we implement our own stat, based on the Win32 API directly.
689*/
690#define HAVE_STAT_NSEC 1
691
692struct win32_stat{
693 int st_dev;
694 __int64 st_ino;
695 unsigned short st_mode;
696 int st_nlink;
697 int st_uid;
698 int st_gid;
699 int st_rdev;
700 __int64 st_size;
701 int st_atime;
702 int st_atime_nsec;
703 int st_mtime;
704 int st_mtime_nsec;
705 int st_ctime;
706 int st_ctime_nsec;
707};
708
709static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
710
711static void
712FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
713{
714 /* XXX endianness */
715 __int64 in = *(__int64*)in_ptr;
716 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
717 /* XXX Win32 supports time stamps past 2038; we currently don't */
718 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
719}
720
721/* Below, we *know* that ugo+r is 0444 */
722#if _S_IREAD != 0400
723#error Unsupported C library
724#endif
725static int
726attributes_to_mode(DWORD attr)
727{
728 int m = 0;
729 if (attr & FILE_ATTRIBUTE_DIRECTORY)
730 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
731 else
732 m |= _S_IFREG;
733 if (attr & FILE_ATTRIBUTE_READONLY)
734 m |= 0444;
735 else
736 m |= 0666;
737 return m;
738}
739
740static int
741attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
742{
743 memset(result, 0, sizeof(*result));
744 result->st_mode = attributes_to_mode(info->dwFileAttributes);
745 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
746 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
747 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
748 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
749
750 return 0;
751}
752
753static int
754win32_stat(const char* path, struct win32_stat *result)
755{
756 WIN32_FILE_ATTRIBUTE_DATA info;
757 int code;
758 char *dot;
759 /* XXX not supported on Win95 and NT 3.x */
760 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
761 /* Protocol violation: we explicitly clear errno, instead of
762 setting it to a POSIX error. Callers should use GetLastError. */
763 errno = 0;
764 return -1;
765 }
766 code = attribute_data_to_stat(&info, result);
767 if (code != 0)
768 return code;
769 /* Set S_IFEXEC if it is an .exe, .bat, ... */
770 dot = strrchr(path, '.');
771 if (dot) {
772 if (stricmp(dot, ".bat") == 0 ||
773 stricmp(dot, ".cmd") == 0 ||
774 stricmp(dot, ".exe") == 0 ||
775 stricmp(dot, ".com") == 0)
776 result->st_mode |= 0111;
777 }
778 return code;
779}
780
781static int
782win32_wstat(const wchar_t* path, struct win32_stat *result)
783{
784 int code;
785 const wchar_t *dot;
786 WIN32_FILE_ATTRIBUTE_DATA info;
787 /* XXX not supported on Win95 and NT 3.x */
788 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
789 /* Protocol violation: we explicitly clear errno, instead of
790 setting it to a POSIX error. Callers should use GetLastError. */
791 errno = 0;
792 return -1;
793 }
794 code = attribute_data_to_stat(&info, result);
795 if (code < 0)
796 return code;
797 /* Set IFEXEC if it is an .exe, .bat, ... */
798 dot = wcsrchr(path, '.');
799 if (dot) {
800 if (_wcsicmp(dot, L".bat") == 0 ||
801 _wcsicmp(dot, L".cmd") == 0 ||
802 _wcsicmp(dot, L".exe") == 0 ||
803 _wcsicmp(dot, L".com") == 0)
804 result->st_mode |= 0111;
805 }
806 return code;
807}
808
809static int
810win32_fstat(int file_number, struct win32_stat *result)
811{
812 BY_HANDLE_FILE_INFORMATION info;
813 HANDLE h;
814 int type;
815
816 h = (HANDLE)_get_osfhandle(file_number);
817
818 /* Protocol violation: we explicitly clear errno, instead of
819 setting it to a POSIX error. Callers should use GetLastError. */
820 errno = 0;
821
822 if (h == INVALID_HANDLE_VALUE) {
823 /* This is really a C library error (invalid file handle).
824 We set the Win32 error to the closes one matching. */
825 SetLastError(ERROR_INVALID_HANDLE);
826 return -1;
827 }
828 memset(result, 0, sizeof(*result));
829
830 type = GetFileType(h);
831 if (type == FILE_TYPE_UNKNOWN) {
832 DWORD error = GetLastError();
833 if (error != 0) {
834 return -1;
835 }
836 /* else: valid but unknown file */
837 }
838
839 if (type != FILE_TYPE_DISK) {
840 if (type == FILE_TYPE_CHAR)
841 result->st_mode = _S_IFCHR;
842 else if (type == FILE_TYPE_PIPE)
843 result->st_mode = _S_IFIFO;
844 return 0;
845 }
846
847 if (!GetFileInformationByHandle(h, &info)) {
848 return -1;
849 }
850
851 /* similar to stat() */
852 result->st_mode = attributes_to_mode(info.dwFileAttributes);
853 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
854 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
855 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
856 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
857 /* specific to fstat() */
858 result->st_nlink = info.nNumberOfLinks;
859 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
860 return 0;
861}
862
863#endif /* MS_WINDOWS */
864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000865PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000866"stat_result: Result from stat or lstat.\n\n\
867This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000868 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000869or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
870\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +0000871Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
872or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000873\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000874See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000875
876static PyStructSequence_Field stat_result_fields[] = {
877 {"st_mode", "protection bits"},
878 {"st_ino", "inode"},
879 {"st_dev", "device"},
880 {"st_nlink", "number of hard links"},
881 {"st_uid", "user ID of owner"},
882 {"st_gid", "group ID of owner"},
883 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000884 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
885 {NULL, "integer time of last access"},
886 {NULL, "integer time of last modification"},
887 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000888 {"st_atime", "time of last access"},
889 {"st_mtime", "time of last modification"},
890 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000891#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000892 {"st_blksize", "blocksize for filesystem I/O"},
893#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000894#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000895 {"st_blocks", "number of blocks allocated"},
896#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000897#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000898 {"st_rdev", "device type (if inode device)"},
899#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +0000900#ifdef HAVE_STRUCT_STAT_ST_FLAGS
901 {"st_flags", "user defined flags for file"},
902#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +0000903#ifdef HAVE_STRUCT_STAT_ST_GEN
904 {"st_gen", "generation number"},
905#endif
906#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
907 {"st_birthtime", "time of creation"},
908#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000909 {0}
910};
911
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000912#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000913#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000914#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000915#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000916#endif
917
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000918#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000919#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
920#else
921#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
922#endif
923
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000924#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000925#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
926#else
927#define ST_RDEV_IDX ST_BLOCKS_IDX
928#endif
929
Hye-Shik Chang5f937a72005-06-02 13:09:30 +0000930#ifdef HAVE_STRUCT_STAT_ST_FLAGS
931#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
932#else
933#define ST_FLAGS_IDX ST_RDEV_IDX
934#endif
935
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +0000936#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +0000937#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +0000938#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +0000939#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +0000940#endif
941
942#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
943#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
944#else
945#define ST_BIRTHTIME_IDX ST_GEN_IDX
946#endif
947
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000948static PyStructSequence_Desc stat_result_desc = {
949 "stat_result", /* name */
950 stat_result__doc__, /* doc */
951 stat_result_fields,
952 10
953};
954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000955PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000956"statvfs_result: Result from statvfs or fstatvfs.\n\n\
957This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000958 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000959or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000960\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000961See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000962
963static PyStructSequence_Field statvfs_result_fields[] = {
964 {"f_bsize", },
965 {"f_frsize", },
966 {"f_blocks", },
967 {"f_bfree", },
968 {"f_bavail", },
969 {"f_files", },
970 {"f_ffree", },
971 {"f_favail", },
972 {"f_flag", },
973 {"f_namemax",},
974 {0}
975};
976
977static PyStructSequence_Desc statvfs_result_desc = {
978 "statvfs_result", /* name */
979 statvfs_result__doc__, /* doc */
980 statvfs_result_fields,
981 10
982};
983
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000984static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000985static PyTypeObject StatResultType;
986static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000987static newfunc structseq_new;
988
989static PyObject *
990statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
991{
992 PyStructSequence *result;
993 int i;
994
995 result = (PyStructSequence*)structseq_new(type, args, kwds);
996 if (!result)
997 return NULL;
998 /* If we have been initialized from a tuple,
999 st_?time might be set to None. Initialize it
1000 from the int slots. */
1001 for (i = 7; i <= 9; i++) {
1002 if (result->ob_item[i+3] == Py_None) {
1003 Py_DECREF(Py_None);
1004 Py_INCREF(result->ob_item[i]);
1005 result->ob_item[i+3] = result->ob_item[i];
1006 }
1007 }
1008 return (PyObject*)result;
1009}
1010
1011
1012
1013/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001014static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001015
1016PyDoc_STRVAR(stat_float_times__doc__,
1017"stat_float_times([newval]) -> oldval\n\n\
1018Determine whether os.[lf]stat represents time stamps as float objects.\n\
1019If newval is True, future calls to stat() return floats, if it is False,\n\
1020future calls return ints. \n\
1021If newval is omitted, return the current setting.\n");
1022
1023static PyObject*
1024stat_float_times(PyObject* self, PyObject *args)
1025{
1026 int newval = -1;
1027 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1028 return NULL;
1029 if (newval == -1)
1030 /* Return old value */
1031 return PyBool_FromLong(_stat_float_times);
1032 _stat_float_times = newval;
1033 Py_INCREF(Py_None);
1034 return Py_None;
1035}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001036
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001037static void
1038fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1039{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001040 PyObject *fval,*ival;
1041#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001042 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001043#else
1044 ival = PyInt_FromLong((long)sec);
1045#endif
1046 if (_stat_float_times) {
1047 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1048 } else {
1049 fval = ival;
1050 Py_INCREF(fval);
1051 }
1052 PyStructSequence_SET_ITEM(v, index, ival);
1053 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001054}
1055
Tim Peters5aa91602002-01-30 05:46:57 +00001056/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001057 (used by posix_stat() and posix_fstat()) */
1058static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001059_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001060{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001061 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001062 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001063 if (v == NULL)
1064 return NULL;
1065
Martin v. Löwis14694662006-02-03 12:54:16 +00001066 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001067#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001068 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001069 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001070#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001071 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001072#endif
1073#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001074 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001075 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001076#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001077 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001078#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001079 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1080 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1081 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001082#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001083 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001084 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001085#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001086 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001087#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001088
Martin v. Löwis14694662006-02-03 12:54:16 +00001089#if defined(HAVE_STAT_TV_NSEC)
1090 ansec = st->st_atim.tv_nsec;
1091 mnsec = st->st_mtim.tv_nsec;
1092 cnsec = st->st_ctim.tv_nsec;
1093#elif defined(HAVE_STAT_TV_NSEC2)
1094 ansec = st->st_atimespec.tv_nsec;
1095 mnsec = st->st_mtimespec.tv_nsec;
1096 cnsec = st->st_ctimespec.tv_nsec;
1097#elif defined(HAVE_STAT_NSEC)
1098 ansec = st->st_atime_nsec;
1099 mnsec = st->st_mtime_nsec;
1100 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001101#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001102 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001103#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001104 fill_time(v, 7, st->st_atime, ansec);
1105 fill_time(v, 8, st->st_mtime, mnsec);
1106 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001107
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001108#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001109 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001110 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001111#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001112#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001113 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001114 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001116#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001117 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001118 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001119#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001120#ifdef HAVE_STRUCT_STAT_ST_GEN
1121 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001122 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001123#endif
1124#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1125 {
1126 PyObject *val;
1127 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001128 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001129#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001130 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001131#else
1132 bnsec = 0;
1133#endif
1134 if (_stat_float_times) {
1135 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1136 } else {
1137 val = PyInt_FromLong((long)bsec);
1138 }
1139 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1140 val);
1141 }
1142#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001143#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1144 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001145 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001146#endif
Fred Drake699f3522000-06-29 21:12:41 +00001147
1148 if (PyErr_Occurred()) {
1149 Py_DECREF(v);
1150 return NULL;
1151 }
1152
1153 return v;
1154}
1155
Martin v. Löwisd8948722004-06-02 09:57:56 +00001156#ifdef MS_WINDOWS
1157
1158/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1159 where / can be used in place of \ and the trailing slash is optional.
1160 Both SERVER and SHARE must have at least one character.
1161*/
1162
1163#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1164#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1165#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1166
Tim Peters4ad82172004-08-30 17:02:04 +00001167static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001168IsUNCRootA(char *path, int pathlen)
1169{
1170 #define ISSLASH ISSLASHA
1171
1172 int i, share;
1173
1174 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1175 /* minimum UNCRoot is \\x\y */
1176 return FALSE;
1177 for (i = 2; i < pathlen ; i++)
1178 if (ISSLASH(path[i])) break;
1179 if (i == 2 || i == pathlen)
1180 /* do not allow \\\SHARE or \\SERVER */
1181 return FALSE;
1182 share = i+1;
1183 for (i = share; i < pathlen; i++)
1184 if (ISSLASH(path[i])) break;
1185 return (i != share && (i == pathlen || i == pathlen-1));
1186
1187 #undef ISSLASH
1188}
1189
1190#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001191static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001192IsUNCRootW(Py_UNICODE *path, int pathlen)
1193{
1194 #define ISSLASH ISSLASHW
1195
1196 int i, share;
1197
1198 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1199 /* minimum UNCRoot is \\x\y */
1200 return FALSE;
1201 for (i = 2; i < pathlen ; i++)
1202 if (ISSLASH(path[i])) break;
1203 if (i == 2 || i == pathlen)
1204 /* do not allow \\\SHARE or \\SERVER */
1205 return FALSE;
1206 share = i+1;
1207 for (i = share; i < pathlen; i++)
1208 if (ISSLASH(path[i])) break;
1209 return (i != share && (i == pathlen || i == pathlen-1));
1210
1211 #undef ISSLASH
1212}
1213#endif /* Py_WIN_WIDE_FILENAMES */
1214#endif /* MS_WINDOWS */
1215
Barry Warsaw53699e91996-12-10 23:23:01 +00001216static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001217posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001218 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001219#ifdef __VMS
1220 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1221#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001222 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001223#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001224 char *wformat,
1225 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001226{
Fred Drake699f3522000-06-29 21:12:41 +00001227 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001228 char *path = NULL; /* pass this to stat; do not free() it */
1229 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001230 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001231 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001232
1233#ifdef Py_WIN_WIDE_FILENAMES
1234 /* If on wide-character-capable OS see if argument
1235 is Unicode and if so use wide API. */
1236 if (unicode_file_names()) {
1237 PyUnicodeObject *po;
1238 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001239 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1240
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001241 Py_BEGIN_ALLOW_THREADS
1242 /* PyUnicode_AS_UNICODE result OK without
1243 thread lock as it is a simple dereference. */
1244 res = wstatfunc(wpath, &st);
1245 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001246
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001247 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001248 return win32_error_unicode("stat", wpath);
1249 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001250 }
1251 /* Drop the argument parsing error as narrow strings
1252 are also valid. */
1253 PyErr_Clear();
1254 }
1255#endif
1256
Tim Peters5aa91602002-01-30 05:46:57 +00001257 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001258 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001260 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001261
Barry Warsaw53699e91996-12-10 23:23:01 +00001262 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001263 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001265
1266 if (res != 0) {
1267#ifdef MS_WINDOWS
1268 result = win32_error("stat", pathfree);
1269#else
1270 result = posix_error_with_filename(pathfree);
1271#endif
1272 }
1273 else
1274 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001275
Tim Peters500bd032001-12-19 19:05:01 +00001276 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001277 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001278}
1279
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001280/* POSIX methods */
1281
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001282PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001283"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001284Use the real uid/gid to test for access to a path. Note that most\n\
1285operations will use the effective uid/gid, therefore this routine can\n\
1286be used in a suid/sgid environment to test if the invoking user has the\n\
1287specified access to the path. The mode argument can be F_OK to test\n\
1288existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001289
1290static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001291posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001292{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001293 char *path;
1294 int mode;
1295 int res;
1296
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001297#ifdef Py_WIN_WIDE_FILENAMES
1298 if (unicode_file_names()) {
1299 PyUnicodeObject *po;
1300 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1301 Py_BEGIN_ALLOW_THREADS
1302 /* PyUnicode_AS_UNICODE OK without thread lock as
1303 it is a simple dereference. */
1304 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1305 Py_END_ALLOW_THREADS
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001306 return PyBool_FromLong(res == 0);
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001307 }
1308 /* Drop the argument parsing error as narrow strings
1309 are also valid. */
1310 PyErr_Clear();
1311 }
1312#endif
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001313 if (!PyArg_ParseTuple(args, "eti:access",
1314 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001315 return NULL;
1316 Py_BEGIN_ALLOW_THREADS
1317 res = access(path, mode);
1318 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001319 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001320 return PyBool_FromLong(res == 0);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001321}
1322
Guido van Rossumd371ff11999-01-25 16:12:23 +00001323#ifndef F_OK
1324#define F_OK 0
1325#endif
1326#ifndef R_OK
1327#define R_OK 4
1328#endif
1329#ifndef W_OK
1330#define W_OK 2
1331#endif
1332#ifndef X_OK
1333#define X_OK 1
1334#endif
1335
1336#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001338"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001340
1341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001342posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001343{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001344 int id;
1345 char *ret;
1346
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001347 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001348 return NULL;
1349
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001350#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001351 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001352 if (id == 0) {
1353 ret = ttyname();
1354 }
1355 else {
1356 ret = NULL;
1357 }
1358#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001359 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001360#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001361 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001362 return posix_error();
1363 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001364}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001365#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001366
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001367#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001368PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001369"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001370Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001371
1372static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001373posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001374{
1375 char *ret;
1376 char buffer[L_ctermid];
1377
Greg Wardb48bc172000-03-01 21:51:56 +00001378#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001379 ret = ctermid_r(buffer);
1380#else
1381 ret = ctermid(buffer);
1382#endif
1383 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001384 return posix_error();
1385 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001386}
1387#endif
1388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001389PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001390"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001391Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001392
Barry Warsaw53699e91996-12-10 23:23:01 +00001393static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001394posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001395{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001396#ifdef MS_WINDOWS
1397 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1398#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1399 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001400#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001401 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001402 NULL, NULL);
1403#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001404 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001405#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001406}
1407
Fred Drake4d1e64b2002-04-15 19:40:07 +00001408#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001409PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001410"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001411Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001412opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001413
1414static PyObject *
1415posix_fchdir(PyObject *self, PyObject *fdobj)
1416{
1417 return posix_fildes(fdobj, fchdir);
1418}
1419#endif /* HAVE_FCHDIR */
1420
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001422PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001423"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001424Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001425
Barry Warsaw53699e91996-12-10 23:23:01 +00001426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001427posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001428{
Mark Hammondef8b6542001-05-13 08:04:26 +00001429 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001430 int i;
1431 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001432#ifdef Py_WIN_WIDE_FILENAMES
1433 if (unicode_file_names()) {
1434 PyUnicodeObject *po;
1435 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1436 Py_BEGIN_ALLOW_THREADS
1437 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1438 Py_END_ALLOW_THREADS
1439 if (res < 0)
1440 return posix_error_with_unicode_filename(
1441 PyUnicode_AS_UNICODE(po));
1442 Py_INCREF(Py_None);
1443 return Py_None;
1444 }
1445 /* Drop the argument parsing error as narrow strings
1446 are also valid. */
1447 PyErr_Clear();
1448 }
1449#endif /* Py_WIN_WIDE_FILENAMES */
1450 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001451 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001452 return NULL;
1453 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001454 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001455 Py_END_ALLOW_THREADS
1456 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001457 return posix_error_with_allocated_filename(path);
1458 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001459 Py_INCREF(Py_None);
1460 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001461}
1462
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001463
Martin v. Löwis244edc82001-10-04 22:44:26 +00001464#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001465PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001466"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001467Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001468
1469static PyObject *
1470posix_chroot(PyObject *self, PyObject *args)
1471{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001472 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001473}
1474#endif
1475
Guido van Rossum21142a01999-01-08 21:05:37 +00001476#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001477PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001478"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001480
1481static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001482posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001483{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001484 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001485}
1486#endif /* HAVE_FSYNC */
1487
1488#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001489
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001490#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001491extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1492#endif
1493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001494PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001495"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001496force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001497 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001498
1499static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001500posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001501{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001502 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001503}
1504#endif /* HAVE_FDATASYNC */
1505
1506
Fredrik Lundh10723342000-07-10 16:38:09 +00001507#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001508PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001509"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001511
Barry Warsaw53699e91996-12-10 23:23:01 +00001512static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001513posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001514{
Mark Hammondef8b6542001-05-13 08:04:26 +00001515 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001516 int uid, gid;
1517 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001518 if (!PyArg_ParseTuple(args, "etii:chown",
1519 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001520 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001521 return NULL;
1522 Py_BEGIN_ALLOW_THREADS
1523 res = chown(path, (uid_t) uid, (gid_t) gid);
1524 Py_END_ALLOW_THREADS
1525 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001526 return posix_error_with_allocated_filename(path);
1527 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001528 Py_INCREF(Py_None);
1529 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001530}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001531#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001532
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001533#ifdef HAVE_LCHOWN
1534PyDoc_STRVAR(posix_lchown__doc__,
1535"lchown(path, uid, gid)\n\n\
1536Change the owner and group id of path to the numeric uid and gid.\n\
1537This function will not follow symbolic links.");
1538
1539static PyObject *
1540posix_lchown(PyObject *self, PyObject *args)
1541{
1542 char *path = NULL;
1543 int uid, gid;
1544 int res;
1545 if (!PyArg_ParseTuple(args, "etii:lchown",
1546 Py_FileSystemDefaultEncoding, &path,
1547 &uid, &gid))
1548 return NULL;
1549 Py_BEGIN_ALLOW_THREADS
1550 res = lchown(path, (uid_t) uid, (gid_t) gid);
1551 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001552 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001553 return posix_error_with_allocated_filename(path);
1554 PyMem_Free(path);
1555 Py_INCREF(Py_None);
1556 return Py_None;
1557}
1558#endif /* HAVE_LCHOWN */
1559
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001560
Guido van Rossum36bc6801995-06-14 22:54:23 +00001561#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001562PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001563"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001565
Barry Warsaw53699e91996-12-10 23:23:01 +00001566static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001567posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001568{
1569 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001570 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001571
Barry Warsaw53699e91996-12-10 23:23:01 +00001572 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001573#if defined(PYOS_OS2) && defined(PYCC_GCC)
1574 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001575#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001576 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001577#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001578 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001579 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001580 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001581 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001582}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001583
Walter Dörwald3b918c32002-11-21 20:18:46 +00001584#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001585PyDoc_STRVAR(posix_getcwdu__doc__,
1586"getcwdu() -> path\n\n\
1587Return a unicode string representing the current working directory.");
1588
1589static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001590posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001591{
1592 char buf[1026];
1593 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001594
1595#ifdef Py_WIN_WIDE_FILENAMES
1596 if (unicode_file_names()) {
1597 wchar_t *wres;
1598 wchar_t wbuf[1026];
1599 Py_BEGIN_ALLOW_THREADS
1600 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1601 Py_END_ALLOW_THREADS
1602 if (wres == NULL)
1603 return posix_error();
1604 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1605 }
1606#endif
1607
1608 Py_BEGIN_ALLOW_THREADS
1609#if defined(PYOS_OS2) && defined(PYCC_GCC)
1610 res = _getcwd2(buf, sizeof buf);
1611#else
1612 res = getcwd(buf, sizeof buf);
1613#endif
1614 Py_END_ALLOW_THREADS
1615 if (res == NULL)
1616 return posix_error();
1617 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1618}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001619#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001620#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001621
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001622
Guido van Rossumb6775db1994-08-01 11:34:53 +00001623#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001625"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001627
Barry Warsaw53699e91996-12-10 23:23:01 +00001628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001629posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001630{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001631 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001632}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001633#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001634
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001636PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001637"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001638Return a list containing the names of the entries in the directory.\n\
1639\n\
1640 path: path of directory to list\n\
1641\n\
1642The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001644
Barry Warsaw53699e91996-12-10 23:23:01 +00001645static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001646posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001647{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001648 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001649 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001650#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001651
Barry Warsaw53699e91996-12-10 23:23:01 +00001652 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001653 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001654 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001655 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001656 /* MAX_PATH characters could mean a bigger encoded string */
1657 char namebuf[MAX_PATH*2+5];
1658 char *bufptr = namebuf;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001659 Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001660
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001661#ifdef Py_WIN_WIDE_FILENAMES
1662 /* If on wide-character-capable OS see if argument
1663 is Unicode and if so use wide API. */
1664 if (unicode_file_names()) {
1665 PyUnicodeObject *po;
1666 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1667 WIN32_FIND_DATAW wFileData;
1668 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1669 Py_UNICODE wch;
1670 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1671 wnamebuf[MAX_PATH] = L'\0';
1672 len = wcslen(wnamebuf);
1673 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1674 if (wch != L'/' && wch != L'\\' && wch != L':')
1675 wnamebuf[len++] = L'/';
1676 wcscpy(wnamebuf + len, L"*.*");
1677 if ((d = PyList_New(0)) == NULL)
1678 return NULL;
1679 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1680 if (hFindFile == INVALID_HANDLE_VALUE) {
1681 errno = GetLastError();
1682 if (errno == ERROR_FILE_NOT_FOUND) {
1683 return d;
1684 }
1685 Py_DECREF(d);
1686 return win32_error_unicode("FindFirstFileW", wnamebuf);
1687 }
1688 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001689 /* Skip over . and .. */
1690 if (wcscmp(wFileData.cFileName, L".") != 0 &&
1691 wcscmp(wFileData.cFileName, L"..") != 0) {
1692 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1693 if (v == NULL) {
1694 Py_DECREF(d);
1695 d = NULL;
1696 break;
1697 }
1698 if (PyList_Append(d, v) != 0) {
1699 Py_DECREF(v);
1700 Py_DECREF(d);
1701 d = NULL;
1702 break;
1703 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001704 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001705 }
Georg Brandl622927b2006-03-07 12:48:03 +00001706 Py_BEGIN_ALLOW_THREADS
1707 result = FindNextFileW(hFindFile, &wFileData);
1708 Py_END_ALLOW_THREADS
1709 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001710
1711 if (FindClose(hFindFile) == FALSE) {
1712 Py_DECREF(d);
1713 return win32_error_unicode("FindClose", wnamebuf);
1714 }
1715 return d;
1716 }
1717 /* Drop the argument parsing error as narrow strings
1718 are also valid. */
1719 PyErr_Clear();
1720 }
1721#endif
1722
Tim Peters5aa91602002-01-30 05:46:57 +00001723 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001724 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001725 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001726 if (len > 0) {
1727 char ch = namebuf[len-1];
1728 if (ch != SEP && ch != ALTSEP && ch != ':')
1729 namebuf[len++] = '/';
1730 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001731 strcpy(namebuf + len, "*.*");
1732
Barry Warsaw53699e91996-12-10 23:23:01 +00001733 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001734 return NULL;
1735
1736 hFindFile = FindFirstFile(namebuf, &FileData);
1737 if (hFindFile == INVALID_HANDLE_VALUE) {
1738 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001739 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001740 return d;
1741 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001742 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001743 }
1744 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001745 /* Skip over . and .. */
1746 if (strcmp(FileData.cFileName, ".") != 0 &&
1747 strcmp(FileData.cFileName, "..") != 0) {
1748 v = PyString_FromString(FileData.cFileName);
1749 if (v == NULL) {
1750 Py_DECREF(d);
1751 d = NULL;
1752 break;
1753 }
1754 if (PyList_Append(d, v) != 0) {
1755 Py_DECREF(v);
1756 Py_DECREF(d);
1757 d = NULL;
1758 break;
1759 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001760 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001761 }
Georg Brandl622927b2006-03-07 12:48:03 +00001762 Py_BEGIN_ALLOW_THREADS
1763 result = FindNextFile(hFindFile, &FileData);
1764 Py_END_ALLOW_THREADS
1765 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001766
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001767 if (FindClose(hFindFile) == FALSE) {
1768 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001769 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001770 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001771
1772 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001773
Tim Peters0bb44a42000-09-15 07:44:49 +00001774#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001775
1776#ifndef MAX_PATH
1777#define MAX_PATH CCHMAXPATH
1778#endif
1779 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00001780 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001781 PyObject *d, *v;
1782 char namebuf[MAX_PATH+5];
1783 HDIR hdir = 1;
1784 ULONG srchcnt = 1;
1785 FILEFINDBUF3 ep;
1786 APIRET rc;
1787
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001788 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001789 return NULL;
1790 if (len >= MAX_PATH) {
1791 PyErr_SetString(PyExc_ValueError, "path too long");
1792 return NULL;
1793 }
1794 strcpy(namebuf, name);
1795 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001796 if (*pt == ALTSEP)
1797 *pt = SEP;
1798 if (namebuf[len-1] != SEP)
1799 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001800 strcpy(namebuf + len, "*.*");
1801
1802 if ((d = PyList_New(0)) == NULL)
1803 return NULL;
1804
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001805 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1806 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001807 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001808 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1809 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1810 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001811
1812 if (rc != NO_ERROR) {
1813 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001814 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001815 }
1816
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001817 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001818 do {
1819 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001820 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001821 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001822
1823 strcpy(namebuf, ep.achName);
1824
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001825 /* Leave Case of Name Alone -- In Native Form */
1826 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001827
1828 v = PyString_FromString(namebuf);
1829 if (v == NULL) {
1830 Py_DECREF(d);
1831 d = NULL;
1832 break;
1833 }
1834 if (PyList_Append(d, v) != 0) {
1835 Py_DECREF(v);
1836 Py_DECREF(d);
1837 d = NULL;
1838 break;
1839 }
1840 Py_DECREF(v);
1841 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1842 }
1843
1844 return d;
1845#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001846
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001847 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001848 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001849 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001850 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001851 int arg_is_unicode = 1;
1852
Georg Brandl05e89b82006-04-11 07:04:06 +00001853 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00001854 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1855 arg_is_unicode = 0;
1856 PyErr_Clear();
1857 }
1858 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001859 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001860 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001861 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001862 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001863 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001864 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001865 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001866 return NULL;
1867 }
Georg Brandl622927b2006-03-07 12:48:03 +00001868 for (;;) {
1869 Py_BEGIN_ALLOW_THREADS
1870 ep = readdir(dirp);
1871 Py_END_ALLOW_THREADS
1872 if (ep == NULL)
1873 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001874 if (ep->d_name[0] == '.' &&
1875 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001876 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001877 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001878 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001879 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001880 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881 d = NULL;
1882 break;
1883 }
Just van Rossum46c97842003-02-25 21:42:15 +00001884#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001885 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001886 PyObject *w;
1887
1888 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001889 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001890 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001891 if (w != NULL) {
1892 Py_DECREF(v);
1893 v = w;
1894 }
1895 else {
1896 /* fall back to the original byte string, as
1897 discussed in patch #683592 */
1898 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001899 }
Just van Rossum46c97842003-02-25 21:42:15 +00001900 }
1901#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001902 if (PyList_Append(d, v) != 0) {
1903 Py_DECREF(v);
1904 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001905 d = NULL;
1906 break;
1907 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001908 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001909 }
Georg Brandlbbfe4fa2006-04-11 06:47:43 +00001910 if (errno != 0 && d != NULL) {
1911 /* readdir() returned NULL and set errno */
1912 closedir(dirp);
1913 Py_DECREF(d);
1914 return posix_error_with_allocated_filename(name);
1915 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001916 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001917 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001918
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001919 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001920
Tim Peters0bb44a42000-09-15 07:44:49 +00001921#endif /* which OS */
1922} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001923
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001924#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001925/* A helper function for abspath on win32 */
1926static PyObject *
1927posix__getfullpathname(PyObject *self, PyObject *args)
1928{
1929 /* assume encoded strings wont more than double no of chars */
1930 char inbuf[MAX_PATH*2];
1931 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00001932 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00001933 char outbuf[MAX_PATH*2];
1934 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001935#ifdef Py_WIN_WIDE_FILENAMES
1936 if (unicode_file_names()) {
1937 PyUnicodeObject *po;
1938 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1939 Py_UNICODE woutbuf[MAX_PATH*2];
1940 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001941 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001942 sizeof(woutbuf)/sizeof(woutbuf[0]),
1943 woutbuf, &wtemp))
1944 return win32_error("GetFullPathName", "");
1945 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1946 }
1947 /* Drop the argument parsing error as narrow strings
1948 are also valid. */
1949 PyErr_Clear();
1950 }
1951#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001952 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1953 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001954 &insize))
1955 return NULL;
1956 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1957 outbuf, &temp))
1958 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00001959 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
1960 return PyUnicode_Decode(outbuf, strlen(outbuf),
1961 Py_FileSystemDefaultEncoding, NULL);
1962 }
Mark Hammondef8b6542001-05-13 08:04:26 +00001963 return PyString_FromString(outbuf);
1964} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001965#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001967PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001968"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001970
Barry Warsaw53699e91996-12-10 23:23:01 +00001971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001972posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001973{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001974 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001975 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001976 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001977
1978#ifdef Py_WIN_WIDE_FILENAMES
1979 if (unicode_file_names()) {
1980 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001981 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001982 Py_BEGIN_ALLOW_THREADS
1983 /* PyUnicode_AS_UNICODE OK without thread lock as
1984 it is a simple dereference. */
1985 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1986 Py_END_ALLOW_THREADS
1987 if (res < 0)
1988 return posix_error();
1989 Py_INCREF(Py_None);
1990 return Py_None;
1991 }
1992 /* Drop the argument parsing error as narrow strings
1993 are also valid. */
1994 PyErr_Clear();
1995 }
1996#endif
1997
Tim Peters5aa91602002-01-30 05:46:57 +00001998 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001999 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002000 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002001 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002002#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002003 res = mkdir(path);
2004#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002005 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002006#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002007 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002008 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002009 return posix_error_with_allocated_filename(path);
2010 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002011 Py_INCREF(Py_None);
2012 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002013}
2014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002015
Neal Norwitz1818ed72006-03-26 00:29:48 +00002016/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2017#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002018#include <sys/resource.h>
2019#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002020
Neal Norwitz1818ed72006-03-26 00:29:48 +00002021
2022#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002023PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002024"nice(inc) -> new_priority\n\n\
2025Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002026
Barry Warsaw53699e91996-12-10 23:23:01 +00002027static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002028posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002029{
2030 int increment, value;
2031
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002032 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002033 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002034
2035 /* There are two flavours of 'nice': one that returns the new
2036 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002037 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2038 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002039
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002040 If we are of the nice family that returns the new priority, we
2041 need to clear errno before the call, and check if errno is filled
2042 before calling posix_error() on a returnvalue of -1, because the
2043 -1 may be the actual new priority! */
2044
2045 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002046 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002047#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002048 if (value == 0)
2049 value = getpriority(PRIO_PROCESS, 0);
2050#endif
2051 if (value == -1 && errno != 0)
2052 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002053 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002054 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002055}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002056#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002057
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002060"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002062
Barry Warsaw53699e91996-12-10 23:23:01 +00002063static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002064posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002065{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002066#ifdef MS_WINDOWS
2067 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
2068#else
2069 return posix_2str(args, "etet:rename", rename, NULL, NULL);
2070#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002071}
2072
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002075"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002076Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002077
Barry Warsaw53699e91996-12-10 23:23:01 +00002078static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002079posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002080{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002081#ifdef MS_WINDOWS
2082 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
2083#else
2084 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
2085#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002086}
2087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002089PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002090"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002091Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002092
Barry Warsaw53699e91996-12-10 23:23:01 +00002093static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002094posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002095{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002096#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002097 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002098#else
2099 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2100#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002101}
2102
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002103
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002104#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002105PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002106"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002107Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002108
Barry Warsaw53699e91996-12-10 23:23:01 +00002109static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002110posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002111{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002112 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002113 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002114 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002115 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002116 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002117 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002118 Py_END_ALLOW_THREADS
2119 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002120}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002121#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002124PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002125"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002126Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002127
Barry Warsaw53699e91996-12-10 23:23:01 +00002128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002129posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002130{
2131 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002132 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002133 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002134 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002135 if (i < 0)
2136 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002137 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002138}
2139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002140
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002141PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002142"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002143Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002145PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002146"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002147Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002148
Barry Warsaw53699e91996-12-10 23:23:01 +00002149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002150posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002151{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002152#ifdef MS_WINDOWS
2153 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
2154#else
2155 return posix_1str(args, "et:remove", unlink, NULL, NULL);
2156#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002157}
2158
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002159
Guido van Rossumb6775db1994-08-01 11:34:53 +00002160#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002161PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002162"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002163Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002164
Barry Warsaw53699e91996-12-10 23:23:01 +00002165static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002166posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002167{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002168 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002169 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002170
Barry Warsaw53699e91996-12-10 23:23:01 +00002171 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002172 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002173 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002174 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002175 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002177 u.sysname,
2178 u.nodename,
2179 u.release,
2180 u.version,
2181 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002182}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002183#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002184
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002185static int
2186extract_time(PyObject *t, long* sec, long* usec)
2187{
2188 long intval;
2189 if (PyFloat_Check(t)) {
2190 double tval = PyFloat_AsDouble(t);
2191 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2192 if (!intobj)
2193 return -1;
2194 intval = PyInt_AsLong(intobj);
2195 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002196 if (intval == -1 && PyErr_Occurred())
2197 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002198 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002199 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002200 if (*usec < 0)
2201 /* If rounding gave us a negative number,
2202 truncate. */
2203 *usec = 0;
2204 return 0;
2205 }
2206 intval = PyInt_AsLong(t);
2207 if (intval == -1 && PyErr_Occurred())
2208 return -1;
2209 *sec = intval;
2210 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002211 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002212}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002214PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002215"utime(path, (atime, utime))\n\
2216utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002217Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002218second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002219
Barry Warsaw53699e91996-12-10 23:23:01 +00002220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002221posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002222{
Neal Norwitz2adf2102004-06-09 01:46:02 +00002223 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002224 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002225 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002226 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002227
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002228#if defined(HAVE_UTIMES)
2229 struct timeval buf[2];
2230#define ATIME buf[0].tv_sec
2231#define MTIME buf[1].tv_sec
2232#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002233/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002234 struct utimbuf buf;
2235#define ATIME buf.actime
2236#define MTIME buf.modtime
2237#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002238#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002239 time_t buf[2];
2240#define ATIME buf[0]
2241#define MTIME buf[1]
2242#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002243#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002244
Mark Hammond817c9292003-12-03 01:22:38 +00002245 int have_unicode_filename = 0;
2246#ifdef Py_WIN_WIDE_FILENAMES
2247 PyUnicodeObject *obwpath;
2248 wchar_t *wpath;
2249 if (unicode_file_names()) {
2250 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2251 wpath = PyUnicode_AS_UNICODE(obwpath);
2252 have_unicode_filename = 1;
2253 } else
2254 /* Drop the argument parsing error as narrow strings
2255 are also valid. */
2256 PyErr_Clear();
2257 }
2258#endif /* Py_WIN_WIDE_FILENAMES */
2259
2260 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002261 !PyArg_ParseTuple(args, "etO:utime",
2262 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002263 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002264 if (arg == Py_None) {
2265 /* optional time values not given */
2266 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002267#ifdef Py_WIN_WIDE_FILENAMES
2268 if (have_unicode_filename)
2269 res = _wutime(wpath, NULL);
2270 else
2271#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002272 res = utime(path, NULL);
2273 Py_END_ALLOW_THREADS
2274 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002275 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002276 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002277 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002278 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002279 return NULL;
2280 }
2281 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002282 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002283 &atime, &ausec) == -1) {
2284 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002285 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002286 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002287 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002288 &mtime, &musec) == -1) {
2289 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002290 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002291 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002292 ATIME = atime;
2293 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002294#ifdef HAVE_UTIMES
2295 buf[0].tv_usec = ausec;
2296 buf[1].tv_usec = musec;
2297 Py_BEGIN_ALLOW_THREADS
2298 res = utimes(path, buf);
2299 Py_END_ALLOW_THREADS
2300#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002301 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002302#ifdef Py_WIN_WIDE_FILENAMES
2303 if (have_unicode_filename)
Tim Peters4ad82172004-08-30 17:02:04 +00002304 /* utime is OK with utimbuf, but _wutime insists
2305 on _utimbuf (the msvc headers assert the
Mark Hammond817c9292003-12-03 01:22:38 +00002306 underscore version is ansi) */
2307 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2308 else
2309#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002310 res = utime(path, UTIME_ARG);
2311 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002312#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002313 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002314 if (res < 0) {
2315#ifdef Py_WIN_WIDE_FILENAMES
Neal Norwitz2adf2102004-06-09 01:46:02 +00002316 if (have_unicode_filename)
Mark Hammond2d5914b2004-05-04 08:10:37 +00002317 return posix_error_with_unicode_filename(wpath);
2318#endif /* Py_WIN_WIDE_FILENAMES */
Neal Norwitz96652712004-06-06 20:40:27 +00002319 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002320 }
Neal Norwitz96652712004-06-06 20:40:27 +00002321 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002322 Py_INCREF(Py_None);
2323 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002324#undef UTIME_ARG
2325#undef ATIME
2326#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002327}
2328
Guido van Rossum85e3b011991-06-03 12:42:10 +00002329
Guido van Rossum3b066191991-06-04 19:40:25 +00002330/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002332PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002333"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002334Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002335
Barry Warsaw53699e91996-12-10 23:23:01 +00002336static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002337posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002338{
2339 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002340 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002341 return NULL;
2342 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002343 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002344}
2345
Martin v. Löwis114619e2002-10-07 06:44:21 +00002346#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2347static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002348free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002349{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002350 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002351 for (i = 0; i < count; i++)
2352 PyMem_Free(array[i]);
2353 PyMem_DEL(array);
2354}
2355#endif
2356
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002357
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002358#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002359PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002360"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002361Execute an executable path with arguments, replacing current process.\n\
2362\n\
2363 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002364 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002365
Barry Warsaw53699e91996-12-10 23:23:01 +00002366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002367posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002368{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002369 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002370 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002371 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002372 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002373 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002374
Guido van Rossum89b33251993-10-22 14:26:06 +00002375 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002376 argv is a list or tuple of strings. */
2377
Martin v. Löwis114619e2002-10-07 06:44:21 +00002378 if (!PyArg_ParseTuple(args, "etO:execv",
2379 Py_FileSystemDefaultEncoding,
2380 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002381 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002382 if (PyList_Check(argv)) {
2383 argc = PyList_Size(argv);
2384 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002385 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002386 else if (PyTuple_Check(argv)) {
2387 argc = PyTuple_Size(argv);
2388 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002389 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002390 else {
Fred Drake661ea262000-10-24 19:57:45 +00002391 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002392 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002393 return NULL;
2394 }
2395
Barry Warsaw53699e91996-12-10 23:23:01 +00002396 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002397 if (argvlist == NULL) {
2398 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002399 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002400 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002401 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002402 if (!PyArg_Parse((*getitem)(argv, i), "et",
2403 Py_FileSystemDefaultEncoding,
2404 &argvlist[i])) {
2405 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002406 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002407 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002408 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002409 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002410
Guido van Rossum85e3b011991-06-03 12:42:10 +00002411 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002412 }
2413 argvlist[argc] = NULL;
2414
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002415 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002416
Guido van Rossum85e3b011991-06-03 12:42:10 +00002417 /* If we get here it's definitely an error */
2418
Martin v. Löwis114619e2002-10-07 06:44:21 +00002419 free_string_array(argvlist, argc);
2420 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002421 return posix_error();
2422}
2423
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002425PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002426"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002427Execute a path with arguments and environment, replacing current process.\n\
2428\n\
2429 path: path of executable file\n\
2430 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002431 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002432
Barry Warsaw53699e91996-12-10 23:23:01 +00002433static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002434posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002435{
2436 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002437 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002438 char **argvlist;
2439 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002440 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002441 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002442 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002443 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002444
2445 /* execve has three arguments: (path, argv, env), where
2446 argv is a list or tuple of strings and env is a dictionary
2447 like posix.environ. */
2448
Martin v. Löwis114619e2002-10-07 06:44:21 +00002449 if (!PyArg_ParseTuple(args, "etOO:execve",
2450 Py_FileSystemDefaultEncoding,
2451 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002452 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002453 if (PyList_Check(argv)) {
2454 argc = PyList_Size(argv);
2455 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002456 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002457 else if (PyTuple_Check(argv)) {
2458 argc = PyTuple_Size(argv);
2459 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002460 }
2461 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002462 PyErr_SetString(PyExc_TypeError,
2463 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002464 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002465 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002466 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002467 PyErr_SetString(PyExc_TypeError,
2468 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002469 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002470 }
2471
Barry Warsaw53699e91996-12-10 23:23:01 +00002472 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002473 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002474 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002475 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002476 }
2477 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002478 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002479 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002480 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002481 &argvlist[i]))
2482 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002483 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002484 goto fail_1;
2485 }
2486 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002487 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002488 argvlist[argc] = NULL;
2489
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002490 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002491 if (i < 0)
2492 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002493 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002494 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002495 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002496 goto fail_1;
2497 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002498 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002499 keys = PyMapping_Keys(env);
2500 vals = PyMapping_Values(env);
2501 if (!keys || !vals)
2502 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002503 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2504 PyErr_SetString(PyExc_TypeError,
2505 "execve(): env.keys() or env.values() is not a list");
2506 goto fail_2;
2507 }
Tim Peters5aa91602002-01-30 05:46:57 +00002508
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002509 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002510 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002511 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002512
2513 key = PyList_GetItem(keys, pos);
2514 val = PyList_GetItem(vals, pos);
2515 if (!key || !val)
2516 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002517
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002518 if (!PyArg_Parse(
2519 key,
2520 "s;execve() arg 3 contains a non-string key",
2521 &k) ||
2522 !PyArg_Parse(
2523 val,
2524 "s;execve() arg 3 contains a non-string value",
2525 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002526 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002527 goto fail_2;
2528 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002529
2530#if defined(PYOS_OS2)
2531 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2532 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2533#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002534 len = PyString_Size(key) + PyString_Size(val) + 2;
2535 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002536 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002537 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002538 goto fail_2;
2539 }
Tim Petersc8996f52001-12-03 20:41:00 +00002540 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002541 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002542#if defined(PYOS_OS2)
2543 }
2544#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002545 }
2546 envlist[envc] = 0;
2547
2548 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002549
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002550 /* If we get here it's definitely an error */
2551
2552 (void) posix_error();
2553
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002554 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002555 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002556 PyMem_DEL(envlist[envc]);
2557 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002558 fail_1:
2559 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002560 Py_XDECREF(vals);
2561 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002562 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002563 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002564 return NULL;
2565}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002566#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002568
Guido van Rossuma1065681999-01-25 23:20:23 +00002569#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002570PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002571"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002572Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002573\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002574 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002575 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002576 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002577
2578static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002579posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002580{
2581 char *path;
2582 PyObject *argv;
2583 char **argvlist;
2584 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002585 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002586 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00002587
2588 /* spawnv has three arguments: (mode, path, argv), where
2589 argv is a list or tuple of strings. */
2590
Martin v. Löwis114619e2002-10-07 06:44:21 +00002591 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2592 Py_FileSystemDefaultEncoding,
2593 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002594 return NULL;
2595 if (PyList_Check(argv)) {
2596 argc = PyList_Size(argv);
2597 getitem = PyList_GetItem;
2598 }
2599 else if (PyTuple_Check(argv)) {
2600 argc = PyTuple_Size(argv);
2601 getitem = PyTuple_GetItem;
2602 }
2603 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002604 PyErr_SetString(PyExc_TypeError,
2605 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002606 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002607 return NULL;
2608 }
2609
2610 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002611 if (argvlist == NULL) {
2612 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002613 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002614 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002615 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002616 if (!PyArg_Parse((*getitem)(argv, i), "et",
2617 Py_FileSystemDefaultEncoding,
2618 &argvlist[i])) {
2619 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002620 PyErr_SetString(
2621 PyExc_TypeError,
2622 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002623 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002624 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002625 }
2626 }
2627 argvlist[argc] = NULL;
2628
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002629#if defined(PYOS_OS2) && defined(PYCC_GCC)
2630 Py_BEGIN_ALLOW_THREADS
2631 spawnval = spawnv(mode, path, argvlist);
2632 Py_END_ALLOW_THREADS
2633#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002634 if (mode == _OLD_P_OVERLAY)
2635 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002636
Tim Peters25059d32001-12-07 20:35:43 +00002637 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002638 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002639 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002640#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002641
Martin v. Löwis114619e2002-10-07 06:44:21 +00002642 free_string_array(argvlist, argc);
2643 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002644
Fred Drake699f3522000-06-29 21:12:41 +00002645 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002646 return posix_error();
2647 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002648#if SIZEOF_LONG == SIZEOF_VOID_P
2649 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002650#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002651 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002652#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002653}
2654
2655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002657"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002658Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002659\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002660 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002661 path: path of executable file\n\
2662 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002664
2665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002666posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002667{
2668 char *path;
2669 PyObject *argv, *env;
2670 char **argvlist;
2671 char **envlist;
2672 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2673 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002674 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002675 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002676 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002677
2678 /* spawnve has four arguments: (mode, path, argv, env), where
2679 argv is a list or tuple of strings and env is a dictionary
2680 like posix.environ. */
2681
Martin v. Löwis114619e2002-10-07 06:44:21 +00002682 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2683 Py_FileSystemDefaultEncoding,
2684 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002685 return NULL;
2686 if (PyList_Check(argv)) {
2687 argc = PyList_Size(argv);
2688 getitem = PyList_GetItem;
2689 }
2690 else if (PyTuple_Check(argv)) {
2691 argc = PyTuple_Size(argv);
2692 getitem = PyTuple_GetItem;
2693 }
2694 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002695 PyErr_SetString(PyExc_TypeError,
2696 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002697 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002698 }
2699 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002700 PyErr_SetString(PyExc_TypeError,
2701 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002702 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002703 }
2704
2705 argvlist = PyMem_NEW(char *, argc+1);
2706 if (argvlist == NULL) {
2707 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002708 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002709 }
2710 for (i = 0; i < argc; i++) {
2711 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002712 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002713 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002714 &argvlist[i]))
2715 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002716 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002717 goto fail_1;
2718 }
2719 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002720 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002721 argvlist[argc] = NULL;
2722
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002723 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002724 if (i < 0)
2725 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002726 envlist = PyMem_NEW(char *, i + 1);
2727 if (envlist == NULL) {
2728 PyErr_NoMemory();
2729 goto fail_1;
2730 }
2731 envc = 0;
2732 keys = PyMapping_Keys(env);
2733 vals = PyMapping_Values(env);
2734 if (!keys || !vals)
2735 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002736 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2737 PyErr_SetString(PyExc_TypeError,
2738 "spawnve(): env.keys() or env.values() is not a list");
2739 goto fail_2;
2740 }
Tim Peters5aa91602002-01-30 05:46:57 +00002741
Guido van Rossuma1065681999-01-25 23:20:23 +00002742 for (pos = 0; pos < i; pos++) {
2743 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002744 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002745
2746 key = PyList_GetItem(keys, pos);
2747 val = PyList_GetItem(vals, pos);
2748 if (!key || !val)
2749 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002750
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002751 if (!PyArg_Parse(
2752 key,
2753 "s;spawnve() arg 3 contains a non-string key",
2754 &k) ||
2755 !PyArg_Parse(
2756 val,
2757 "s;spawnve() arg 3 contains a non-string value",
2758 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002759 {
2760 goto fail_2;
2761 }
Tim Petersc8996f52001-12-03 20:41:00 +00002762 len = PyString_Size(key) + PyString_Size(val) + 2;
2763 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002764 if (p == NULL) {
2765 PyErr_NoMemory();
2766 goto fail_2;
2767 }
Tim Petersc8996f52001-12-03 20:41:00 +00002768 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002769 envlist[envc++] = p;
2770 }
2771 envlist[envc] = 0;
2772
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002773#if defined(PYOS_OS2) && defined(PYCC_GCC)
2774 Py_BEGIN_ALLOW_THREADS
2775 spawnval = spawnve(mode, path, argvlist, envlist);
2776 Py_END_ALLOW_THREADS
2777#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002778 if (mode == _OLD_P_OVERLAY)
2779 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002780
2781 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002782 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002783 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002784#endif
Tim Peters25059d32001-12-07 20:35:43 +00002785
Fred Drake699f3522000-06-29 21:12:41 +00002786 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002787 (void) posix_error();
2788 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002789#if SIZEOF_LONG == SIZEOF_VOID_P
2790 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002791#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002792 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002793#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002794
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002795 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002796 while (--envc >= 0)
2797 PyMem_DEL(envlist[envc]);
2798 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002799 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002800 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002801 Py_XDECREF(vals);
2802 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002803 fail_0:
2804 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002805 return res;
2806}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002807
2808/* OS/2 supports spawnvp & spawnvpe natively */
2809#if defined(PYOS_OS2)
2810PyDoc_STRVAR(posix_spawnvp__doc__,
2811"spawnvp(mode, file, args)\n\n\
2812Execute the program 'file' in a new process, using the environment\n\
2813search path to find the file.\n\
2814\n\
2815 mode: mode of process creation\n\
2816 file: executable file name\n\
2817 args: tuple or list of strings");
2818
2819static PyObject *
2820posix_spawnvp(PyObject *self, PyObject *args)
2821{
2822 char *path;
2823 PyObject *argv;
2824 char **argvlist;
2825 int mode, i, argc;
2826 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002827 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002828
2829 /* spawnvp has three arguments: (mode, path, argv), where
2830 argv is a list or tuple of strings. */
2831
2832 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
2833 Py_FileSystemDefaultEncoding,
2834 &path, &argv))
2835 return NULL;
2836 if (PyList_Check(argv)) {
2837 argc = PyList_Size(argv);
2838 getitem = PyList_GetItem;
2839 }
2840 else if (PyTuple_Check(argv)) {
2841 argc = PyTuple_Size(argv);
2842 getitem = PyTuple_GetItem;
2843 }
2844 else {
2845 PyErr_SetString(PyExc_TypeError,
2846 "spawnvp() arg 2 must be a tuple or list");
2847 PyMem_Free(path);
2848 return NULL;
2849 }
2850
2851 argvlist = PyMem_NEW(char *, argc+1);
2852 if (argvlist == NULL) {
2853 PyMem_Free(path);
2854 return PyErr_NoMemory();
2855 }
2856 for (i = 0; i < argc; i++) {
2857 if (!PyArg_Parse((*getitem)(argv, i), "et",
2858 Py_FileSystemDefaultEncoding,
2859 &argvlist[i])) {
2860 free_string_array(argvlist, i);
2861 PyErr_SetString(
2862 PyExc_TypeError,
2863 "spawnvp() arg 2 must contain only strings");
2864 PyMem_Free(path);
2865 return NULL;
2866 }
2867 }
2868 argvlist[argc] = NULL;
2869
2870 Py_BEGIN_ALLOW_THREADS
2871#if defined(PYCC_GCC)
2872 spawnval = spawnvp(mode, path, argvlist);
2873#else
2874 spawnval = _spawnvp(mode, path, argvlist);
2875#endif
2876 Py_END_ALLOW_THREADS
2877
2878 free_string_array(argvlist, argc);
2879 PyMem_Free(path);
2880
2881 if (spawnval == -1)
2882 return posix_error();
2883 else
2884 return Py_BuildValue("l", (long) spawnval);
2885}
2886
2887
2888PyDoc_STRVAR(posix_spawnvpe__doc__,
2889"spawnvpe(mode, file, args, env)\n\n\
2890Execute the program 'file' in a new process, using the environment\n\
2891search path to find the file.\n\
2892\n\
2893 mode: mode of process creation\n\
2894 file: executable file name\n\
2895 args: tuple or list of arguments\n\
2896 env: dictionary of strings mapping to strings");
2897
2898static PyObject *
2899posix_spawnvpe(PyObject *self, PyObject *args)
2900{
2901 char *path;
2902 PyObject *argv, *env;
2903 char **argvlist;
2904 char **envlist;
2905 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2906 int mode, i, pos, argc, envc;
2907 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002908 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002909 int lastarg = 0;
2910
2911 /* spawnvpe has four arguments: (mode, path, argv, env), where
2912 argv is a list or tuple of strings and env is a dictionary
2913 like posix.environ. */
2914
2915 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
2916 Py_FileSystemDefaultEncoding,
2917 &path, &argv, &env))
2918 return NULL;
2919 if (PyList_Check(argv)) {
2920 argc = PyList_Size(argv);
2921 getitem = PyList_GetItem;
2922 }
2923 else if (PyTuple_Check(argv)) {
2924 argc = PyTuple_Size(argv);
2925 getitem = PyTuple_GetItem;
2926 }
2927 else {
2928 PyErr_SetString(PyExc_TypeError,
2929 "spawnvpe() arg 2 must be a tuple or list");
2930 goto fail_0;
2931 }
2932 if (!PyMapping_Check(env)) {
2933 PyErr_SetString(PyExc_TypeError,
2934 "spawnvpe() arg 3 must be a mapping object");
2935 goto fail_0;
2936 }
2937
2938 argvlist = PyMem_NEW(char *, argc+1);
2939 if (argvlist == NULL) {
2940 PyErr_NoMemory();
2941 goto fail_0;
2942 }
2943 for (i = 0; i < argc; i++) {
2944 if (!PyArg_Parse((*getitem)(argv, i),
2945 "et;spawnvpe() arg 2 must contain only strings",
2946 Py_FileSystemDefaultEncoding,
2947 &argvlist[i]))
2948 {
2949 lastarg = i;
2950 goto fail_1;
2951 }
2952 }
2953 lastarg = argc;
2954 argvlist[argc] = NULL;
2955
2956 i = PyMapping_Size(env);
2957 if (i < 0)
2958 goto fail_1;
2959 envlist = PyMem_NEW(char *, i + 1);
2960 if (envlist == NULL) {
2961 PyErr_NoMemory();
2962 goto fail_1;
2963 }
2964 envc = 0;
2965 keys = PyMapping_Keys(env);
2966 vals = PyMapping_Values(env);
2967 if (!keys || !vals)
2968 goto fail_2;
2969 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2970 PyErr_SetString(PyExc_TypeError,
2971 "spawnvpe(): env.keys() or env.values() is not a list");
2972 goto fail_2;
2973 }
2974
2975 for (pos = 0; pos < i; pos++) {
2976 char *p, *k, *v;
2977 size_t len;
2978
2979 key = PyList_GetItem(keys, pos);
2980 val = PyList_GetItem(vals, pos);
2981 if (!key || !val)
2982 goto fail_2;
2983
2984 if (!PyArg_Parse(
2985 key,
2986 "s;spawnvpe() arg 3 contains a non-string key",
2987 &k) ||
2988 !PyArg_Parse(
2989 val,
2990 "s;spawnvpe() arg 3 contains a non-string value",
2991 &v))
2992 {
2993 goto fail_2;
2994 }
2995 len = PyString_Size(key) + PyString_Size(val) + 2;
2996 p = PyMem_NEW(char, len);
2997 if (p == NULL) {
2998 PyErr_NoMemory();
2999 goto fail_2;
3000 }
3001 PyOS_snprintf(p, len, "%s=%s", k, v);
3002 envlist[envc++] = p;
3003 }
3004 envlist[envc] = 0;
3005
3006 Py_BEGIN_ALLOW_THREADS
3007#if defined(PYCC_GCC)
3008 spawnval = spawnve(mode, path, argvlist, envlist);
3009#else
3010 spawnval = _spawnve(mode, path, argvlist, envlist);
3011#endif
3012 Py_END_ALLOW_THREADS
3013
3014 if (spawnval == -1)
3015 (void) posix_error();
3016 else
3017 res = Py_BuildValue("l", (long) spawnval);
3018
3019 fail_2:
3020 while (--envc >= 0)
3021 PyMem_DEL(envlist[envc]);
3022 PyMem_DEL(envlist);
3023 fail_1:
3024 free_string_array(argvlist, lastarg);
3025 Py_XDECREF(vals);
3026 Py_XDECREF(keys);
3027 fail_0:
3028 PyMem_Free(path);
3029 return res;
3030}
3031#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003032#endif /* HAVE_SPAWNV */
3033
3034
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003035#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003037"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003038Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3039\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003040Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003041
3042static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003043posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003044{
Neal Norwitze241ce82003-02-17 18:17:05 +00003045 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003046 if (pid == -1)
3047 return posix_error();
3048 PyOS_AfterFork();
3049 return PyInt_FromLong((long)pid);
3050}
3051#endif
3052
3053
Guido van Rossumad0ee831995-03-01 10:34:45 +00003054#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003055PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003056"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003057Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003058Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003059
Barry Warsaw53699e91996-12-10 23:23:01 +00003060static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003061posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003062{
Neal Norwitze241ce82003-02-17 18:17:05 +00003063 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003064 if (pid == -1)
3065 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003066 if (pid == 0)
3067 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003068 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003069}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003070#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003071
Neal Norwitzb59798b2003-03-21 01:43:31 +00003072/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003073/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3074#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003075#define DEV_PTY_FILE "/dev/ptc"
3076#define HAVE_DEV_PTMX
3077#else
3078#define DEV_PTY_FILE "/dev/ptmx"
3079#endif
3080
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003081#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003082#ifdef HAVE_PTY_H
3083#include <pty.h>
3084#else
3085#ifdef HAVE_LIBUTIL_H
3086#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003087#endif /* HAVE_LIBUTIL_H */
3088#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003089#ifdef HAVE_STROPTS_H
3090#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003091#endif
3092#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003093
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003094#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003095PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003096"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003097Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003098
3099static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003100posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003101{
3102 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003103#ifndef HAVE_OPENPTY
3104 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003105#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003106#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003107 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003108#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003109 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003110#endif
3111#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003112
Thomas Wouters70c21a12000-07-14 14:28:33 +00003113#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003114 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3115 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003116#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003117 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3118 if (slave_name == NULL)
3119 return posix_error();
3120
3121 slave_fd = open(slave_name, O_RDWR);
3122 if (slave_fd < 0)
3123 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003124#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003125 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003126 if (master_fd < 0)
3127 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003128 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003129 /* change permission of slave */
3130 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003131 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003132 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003133 }
3134 /* unlock slave */
3135 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003136 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003137 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003138 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003139 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003140 slave_name = ptsname(master_fd); /* get name of slave */
3141 if (slave_name == NULL)
3142 return posix_error();
3143 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3144 if (slave_fd < 0)
3145 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003146#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003147 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3148 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003149#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003150 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003151#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003152#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003153#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003154
Fred Drake8cef4cf2000-06-28 16:40:38 +00003155 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003156
Fred Drake8cef4cf2000-06-28 16:40:38 +00003157}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003158#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003159
3160#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003161PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003162"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003163Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3164Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003165To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003166
3167static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003168posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003169{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003170 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003171
Fred Drake8cef4cf2000-06-28 16:40:38 +00003172 pid = forkpty(&master_fd, NULL, NULL, NULL);
3173 if (pid == -1)
3174 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003175 if (pid == 0)
3176 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003177 return Py_BuildValue("(ii)", pid, master_fd);
3178}
3179#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003180
Guido van Rossumad0ee831995-03-01 10:34:45 +00003181#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003182PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003183"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003184Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003185
Barry Warsaw53699e91996-12-10 23:23:01 +00003186static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003187posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003188{
Barry Warsaw53699e91996-12-10 23:23:01 +00003189 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003190}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003191#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003192
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003193
Guido van Rossumad0ee831995-03-01 10:34:45 +00003194#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003195PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003196"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003197Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198
Barry Warsaw53699e91996-12-10 23:23:01 +00003199static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003200posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003201{
Barry Warsaw53699e91996-12-10 23:23:01 +00003202 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003203}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003204#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003206
Guido van Rossumad0ee831995-03-01 10:34:45 +00003207#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003208PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003209"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211
Barry Warsaw53699e91996-12-10 23:23:01 +00003212static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003213posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003214{
Barry Warsaw53699e91996-12-10 23:23:01 +00003215 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003216}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003217#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003218
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003220PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003221"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003222Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003223
Barry Warsaw53699e91996-12-10 23:23:01 +00003224static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003225posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003226{
Barry Warsaw53699e91996-12-10 23:23:01 +00003227 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003228}
3229
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003230
Fred Drakec9680921999-12-13 16:37:25 +00003231#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003232PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003233"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003234Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003235
3236static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003237posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003238{
3239 PyObject *result = NULL;
3240
Fred Drakec9680921999-12-13 16:37:25 +00003241#ifdef NGROUPS_MAX
3242#define MAX_GROUPS NGROUPS_MAX
3243#else
3244 /* defined to be 16 on Solaris7, so this should be a small number */
3245#define MAX_GROUPS 64
3246#endif
3247 gid_t grouplist[MAX_GROUPS];
3248 int n;
3249
3250 n = getgroups(MAX_GROUPS, grouplist);
3251 if (n < 0)
3252 posix_error();
3253 else {
3254 result = PyList_New(n);
3255 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003256 int i;
3257 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003258 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003259 if (o == NULL) {
3260 Py_DECREF(result);
3261 result = NULL;
3262 break;
3263 }
3264 PyList_SET_ITEM(result, i, o);
3265 }
3266 }
3267 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003268
Fred Drakec9680921999-12-13 16:37:25 +00003269 return result;
3270}
3271#endif
3272
Martin v. Löwis606edc12002-06-13 21:09:11 +00003273#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003274PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003275"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003276Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003277
3278static PyObject *
3279posix_getpgid(PyObject *self, PyObject *args)
3280{
3281 int pid, pgid;
3282 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3283 return NULL;
3284 pgid = getpgid(pid);
3285 if (pgid < 0)
3286 return posix_error();
3287 return PyInt_FromLong((long)pgid);
3288}
3289#endif /* HAVE_GETPGID */
3290
3291
Guido van Rossumb6775db1994-08-01 11:34:53 +00003292#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003293PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003294"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003295Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003296
Barry Warsaw53699e91996-12-10 23:23:01 +00003297static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003298posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003299{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003300#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003301 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003302#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003303 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003304#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003305}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003306#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003307
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003308
Guido van Rossumb6775db1994-08-01 11:34:53 +00003309#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003310PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003311"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003312Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003313
Barry Warsaw53699e91996-12-10 23:23:01 +00003314static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003315posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003316{
Guido van Rossum64933891994-10-20 21:56:42 +00003317#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003318 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003319#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003320 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003321#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003322 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003323 Py_INCREF(Py_None);
3324 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003325}
3326
Guido van Rossumb6775db1994-08-01 11:34:53 +00003327#endif /* HAVE_SETPGRP */
3328
Guido van Rossumad0ee831995-03-01 10:34:45 +00003329#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003330PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003331"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003332Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003333
Barry Warsaw53699e91996-12-10 23:23:01 +00003334static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003335posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003336{
Barry Warsaw53699e91996-12-10 23:23:01 +00003337 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003338}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003339#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003340
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003341
Fred Drake12c6e2d1999-12-14 21:25:03 +00003342#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003343PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003344"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003345Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003346
3347static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003348posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003349{
Neal Norwitze241ce82003-02-17 18:17:05 +00003350 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003351 char *name;
3352 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003353
Fred Drakea30680b2000-12-06 21:24:28 +00003354 errno = 0;
3355 name = getlogin();
3356 if (name == NULL) {
3357 if (errno)
3358 posix_error();
3359 else
3360 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003361 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003362 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003363 else
3364 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003365 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003366
Fred Drake12c6e2d1999-12-14 21:25:03 +00003367 return result;
3368}
3369#endif
3370
Guido van Rossumad0ee831995-03-01 10:34:45 +00003371#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003372PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003373"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003374Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003375
Barry Warsaw53699e91996-12-10 23:23:01 +00003376static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003377posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003378{
Barry Warsaw53699e91996-12-10 23:23:01 +00003379 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003380}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003381#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003382
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003383
Guido van Rossumad0ee831995-03-01 10:34:45 +00003384#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003385PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003386"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003387Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003388
Barry Warsaw53699e91996-12-10 23:23:01 +00003389static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003390posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003391{
3392 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003393 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003394 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003395#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003396 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3397 APIRET rc;
3398 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003399 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003400
3401 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3402 APIRET rc;
3403 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003404 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003405
3406 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003407 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003408#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003409 if (kill(pid, sig) == -1)
3410 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003411#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003412 Py_INCREF(Py_None);
3413 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003414}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003415#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003416
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003417#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003418PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003419"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003420Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003421
3422static PyObject *
3423posix_killpg(PyObject *self, PyObject *args)
3424{
3425 int pgid, sig;
3426 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3427 return NULL;
3428 if (killpg(pgid, sig) == -1)
3429 return posix_error();
3430 Py_INCREF(Py_None);
3431 return Py_None;
3432}
3433#endif
3434
Guido van Rossumc0125471996-06-28 18:55:32 +00003435#ifdef HAVE_PLOCK
3436
3437#ifdef HAVE_SYS_LOCK_H
3438#include <sys/lock.h>
3439#endif
3440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003441PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003442"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003443Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003444
Barry Warsaw53699e91996-12-10 23:23:01 +00003445static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003446posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003447{
3448 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003449 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003450 return NULL;
3451 if (plock(op) == -1)
3452 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003453 Py_INCREF(Py_None);
3454 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003455}
3456#endif
3457
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003458
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003459#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003460PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003461"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003462Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003463
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003464#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003465#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003466static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003467async_system(const char *command)
3468{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003469 char errormsg[256], args[1024];
3470 RESULTCODES rcodes;
3471 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003472
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003473 char *shell = getenv("COMSPEC");
3474 if (!shell)
3475 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003476
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003477 /* avoid overflowing the argument buffer */
3478 if (strlen(shell) + 3 + strlen(command) >= 1024)
3479 return ERROR_NOT_ENOUGH_MEMORY
3480
3481 args[0] = '\0';
3482 strcat(args, shell);
3483 strcat(args, "/c ");
3484 strcat(args, command);
3485
3486 /* execute asynchronously, inheriting the environment */
3487 rc = DosExecPgm(errormsg,
3488 sizeof(errormsg),
3489 EXEC_ASYNC,
3490 args,
3491 NULL,
3492 &rcodes,
3493 shell);
3494 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003495}
3496
Guido van Rossumd48f2521997-12-05 22:19:34 +00003497static FILE *
3498popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003499{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003500 int oldfd, tgtfd;
3501 HFILE pipeh[2];
3502 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003503
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003504 /* mode determines which of stdin or stdout is reconnected to
3505 * the pipe to the child
3506 */
3507 if (strchr(mode, 'r') != NULL) {
3508 tgt_fd = 1; /* stdout */
3509 } else if (strchr(mode, 'w')) {
3510 tgt_fd = 0; /* stdin */
3511 } else {
3512 *err = ERROR_INVALID_ACCESS;
3513 return NULL;
3514 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003515
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003516 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003517 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3518 *err = rc;
3519 return NULL;
3520 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003521
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003522 /* prevent other threads accessing stdio */
3523 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003524
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003525 /* reconnect stdio and execute child */
3526 oldfd = dup(tgtfd);
3527 close(tgtfd);
3528 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3529 DosClose(pipeh[tgtfd]);
3530 rc = async_system(command);
3531 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003532
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003533 /* restore stdio */
3534 dup2(oldfd, tgtfd);
3535 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003536
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003537 /* allow other threads access to stdio */
3538 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003539
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003540 /* if execution of child was successful return file stream */
3541 if (rc == NO_ERROR)
3542 return fdopen(pipeh[1 - tgtfd], mode);
3543 else {
3544 DosClose(pipeh[1 - tgtfd]);
3545 *err = rc;
3546 return NULL;
3547 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003548}
3549
3550static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003551posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003552{
3553 char *name;
3554 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003555 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003556 FILE *fp;
3557 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003558 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003559 return NULL;
3560 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003561 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003562 Py_END_ALLOW_THREADS
3563 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003564 return os2_error(err);
3565
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003566 f = PyFile_FromFile(fp, name, mode, fclose);
3567 if (f != NULL)
3568 PyFile_SetBufSize(f, bufsize);
3569 return f;
3570}
3571
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003572#elif defined(PYCC_GCC)
3573
3574/* standard posix version of popen() support */
3575static PyObject *
3576posix_popen(PyObject *self, PyObject *args)
3577{
3578 char *name;
3579 char *mode = "r";
3580 int bufsize = -1;
3581 FILE *fp;
3582 PyObject *f;
3583 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3584 return NULL;
3585 Py_BEGIN_ALLOW_THREADS
3586 fp = popen(name, mode);
3587 Py_END_ALLOW_THREADS
3588 if (fp == NULL)
3589 return posix_error();
3590 f = PyFile_FromFile(fp, name, mode, pclose);
3591 if (f != NULL)
3592 PyFile_SetBufSize(f, bufsize);
3593 return f;
3594}
3595
3596/* fork() under OS/2 has lots'o'warts
3597 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3598 * most of this code is a ripoff of the win32 code, but using the
3599 * capabilities of EMX's C library routines
3600 */
3601
3602/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3603#define POPEN_1 1
3604#define POPEN_2 2
3605#define POPEN_3 3
3606#define POPEN_4 4
3607
3608static PyObject *_PyPopen(char *, int, int, int);
3609static int _PyPclose(FILE *file);
3610
3611/*
3612 * Internal dictionary mapping popen* file pointers to process handles,
3613 * for use when retrieving the process exit code. See _PyPclose() below
3614 * for more information on this dictionary's use.
3615 */
3616static PyObject *_PyPopenProcs = NULL;
3617
3618/* os2emx version of popen2()
3619 *
3620 * The result of this function is a pipe (file) connected to the
3621 * process's stdin, and a pipe connected to the process's stdout.
3622 */
3623
3624static PyObject *
3625os2emx_popen2(PyObject *self, PyObject *args)
3626{
3627 PyObject *f;
3628 int tm=0;
3629
3630 char *cmdstring;
3631 char *mode = "t";
3632 int bufsize = -1;
3633 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3634 return NULL;
3635
3636 if (*mode == 't')
3637 tm = O_TEXT;
3638 else if (*mode != 'b') {
3639 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3640 return NULL;
3641 } else
3642 tm = O_BINARY;
3643
3644 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3645
3646 return f;
3647}
3648
3649/*
3650 * Variation on os2emx.popen2
3651 *
3652 * The result of this function is 3 pipes - the process's stdin,
3653 * stdout and stderr
3654 */
3655
3656static PyObject *
3657os2emx_popen3(PyObject *self, PyObject *args)
3658{
3659 PyObject *f;
3660 int tm = 0;
3661
3662 char *cmdstring;
3663 char *mode = "t";
3664 int bufsize = -1;
3665 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3666 return NULL;
3667
3668 if (*mode == 't')
3669 tm = O_TEXT;
3670 else if (*mode != 'b') {
3671 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3672 return NULL;
3673 } else
3674 tm = O_BINARY;
3675
3676 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3677
3678 return f;
3679}
3680
3681/*
3682 * Variation on os2emx.popen2
3683 *
Tim Peters11b23062003-04-23 02:39:17 +00003684 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003685 * and stdout+stderr combined as a single pipe.
3686 */
3687
3688static PyObject *
3689os2emx_popen4(PyObject *self, PyObject *args)
3690{
3691 PyObject *f;
3692 int tm = 0;
3693
3694 char *cmdstring;
3695 char *mode = "t";
3696 int bufsize = -1;
3697 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3698 return NULL;
3699
3700 if (*mode == 't')
3701 tm = O_TEXT;
3702 else if (*mode != 'b') {
3703 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3704 return NULL;
3705 } else
3706 tm = O_BINARY;
3707
3708 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3709
3710 return f;
3711}
3712
3713/* a couple of structures for convenient handling of multiple
3714 * file handles and pipes
3715 */
3716struct file_ref
3717{
3718 int handle;
3719 int flags;
3720};
3721
3722struct pipe_ref
3723{
3724 int rd;
3725 int wr;
3726};
3727
3728/* The following code is derived from the win32 code */
3729
3730static PyObject *
3731_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3732{
3733 struct file_ref stdio[3];
3734 struct pipe_ref p_fd[3];
3735 FILE *p_s[3];
3736 int file_count, i, pipe_err, pipe_pid;
3737 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3738 PyObject *f, *p_f[3];
3739
3740 /* file modes for subsequent fdopen's on pipe handles */
3741 if (mode == O_TEXT)
3742 {
3743 rd_mode = "rt";
3744 wr_mode = "wt";
3745 }
3746 else
3747 {
3748 rd_mode = "rb";
3749 wr_mode = "wb";
3750 }
3751
3752 /* prepare shell references */
3753 if ((shell = getenv("EMXSHELL")) == NULL)
3754 if ((shell = getenv("COMSPEC")) == NULL)
3755 {
3756 errno = ENOENT;
3757 return posix_error();
3758 }
3759
3760 sh_name = _getname(shell);
3761 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3762 opt = "/c";
3763 else
3764 opt = "-c";
3765
3766 /* save current stdio fds + their flags, and set not inheritable */
3767 i = pipe_err = 0;
3768 while (pipe_err >= 0 && i < 3)
3769 {
3770 pipe_err = stdio[i].handle = dup(i);
3771 stdio[i].flags = fcntl(i, F_GETFD, 0);
3772 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3773 i++;
3774 }
3775 if (pipe_err < 0)
3776 {
3777 /* didn't get them all saved - clean up and bail out */
3778 int saved_err = errno;
3779 while (i-- > 0)
3780 {
3781 close(stdio[i].handle);
3782 }
3783 errno = saved_err;
3784 return posix_error();
3785 }
3786
3787 /* create pipe ends */
3788 file_count = 2;
3789 if (n == POPEN_3)
3790 file_count = 3;
3791 i = pipe_err = 0;
3792 while ((pipe_err == 0) && (i < file_count))
3793 pipe_err = pipe((int *)&p_fd[i++]);
3794 if (pipe_err < 0)
3795 {
3796 /* didn't get them all made - clean up and bail out */
3797 while (i-- > 0)
3798 {
3799 close(p_fd[i].wr);
3800 close(p_fd[i].rd);
3801 }
3802 errno = EPIPE;
3803 return posix_error();
3804 }
3805
3806 /* change the actual standard IO streams over temporarily,
3807 * making the retained pipe ends non-inheritable
3808 */
3809 pipe_err = 0;
3810
3811 /* - stdin */
3812 if (dup2(p_fd[0].rd, 0) == 0)
3813 {
3814 close(p_fd[0].rd);
3815 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3816 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3817 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3818 {
3819 close(p_fd[0].wr);
3820 pipe_err = -1;
3821 }
3822 }
3823 else
3824 {
3825 pipe_err = -1;
3826 }
3827
3828 /* - stdout */
3829 if (pipe_err == 0)
3830 {
3831 if (dup2(p_fd[1].wr, 1) == 1)
3832 {
3833 close(p_fd[1].wr);
3834 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3835 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3836 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3837 {
3838 close(p_fd[1].rd);
3839 pipe_err = -1;
3840 }
3841 }
3842 else
3843 {
3844 pipe_err = -1;
3845 }
3846 }
3847
3848 /* - stderr, as required */
3849 if (pipe_err == 0)
3850 switch (n)
3851 {
3852 case POPEN_3:
3853 {
3854 if (dup2(p_fd[2].wr, 2) == 2)
3855 {
3856 close(p_fd[2].wr);
3857 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3858 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3859 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3860 {
3861 close(p_fd[2].rd);
3862 pipe_err = -1;
3863 }
3864 }
3865 else
3866 {
3867 pipe_err = -1;
3868 }
3869 break;
3870 }
3871
3872 case POPEN_4:
3873 {
3874 if (dup2(1, 2) != 2)
3875 {
3876 pipe_err = -1;
3877 }
3878 break;
3879 }
3880 }
3881
3882 /* spawn the child process */
3883 if (pipe_err == 0)
3884 {
3885 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3886 if (pipe_pid == -1)
3887 {
3888 pipe_err = -1;
3889 }
3890 else
3891 {
3892 /* save the PID into the FILE structure
3893 * NOTE: this implementation doesn't actually
3894 * take advantage of this, but do it for
3895 * completeness - AIM Apr01
3896 */
3897 for (i = 0; i < file_count; i++)
3898 p_s[i]->_pid = pipe_pid;
3899 }
3900 }
3901
3902 /* reset standard IO to normal */
3903 for (i = 0; i < 3; i++)
3904 {
3905 dup2(stdio[i].handle, i);
3906 fcntl(i, F_SETFD, stdio[i].flags);
3907 close(stdio[i].handle);
3908 }
3909
3910 /* if any remnant problems, clean up and bail out */
3911 if (pipe_err < 0)
3912 {
3913 for (i = 0; i < 3; i++)
3914 {
3915 close(p_fd[i].rd);
3916 close(p_fd[i].wr);
3917 }
3918 errno = EPIPE;
3919 return posix_error_with_filename(cmdstring);
3920 }
3921
3922 /* build tuple of file objects to return */
3923 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3924 PyFile_SetBufSize(p_f[0], bufsize);
3925 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3926 PyFile_SetBufSize(p_f[1], bufsize);
3927 if (n == POPEN_3)
3928 {
3929 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3930 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003931 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003932 }
3933 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003934 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003935
3936 /*
3937 * Insert the files we've created into the process dictionary
3938 * all referencing the list with the process handle and the
3939 * initial number of files (see description below in _PyPclose).
3940 * Since if _PyPclose later tried to wait on a process when all
3941 * handles weren't closed, it could create a deadlock with the
3942 * child, we spend some energy here to try to ensure that we
3943 * either insert all file handles into the dictionary or none
3944 * at all. It's a little clumsy with the various popen modes
3945 * and variable number of files involved.
3946 */
3947 if (!_PyPopenProcs)
3948 {
3949 _PyPopenProcs = PyDict_New();
3950 }
3951
3952 if (_PyPopenProcs)
3953 {
3954 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3955 int ins_rc[3];
3956
3957 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3958 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3959
3960 procObj = PyList_New(2);
3961 pidObj = PyInt_FromLong((long) pipe_pid);
3962 intObj = PyInt_FromLong((long) file_count);
3963
3964 if (procObj && pidObj && intObj)
3965 {
3966 PyList_SetItem(procObj, 0, pidObj);
3967 PyList_SetItem(procObj, 1, intObj);
3968
3969 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3970 if (fileObj[0])
3971 {
3972 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3973 fileObj[0],
3974 procObj);
3975 }
3976 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3977 if (fileObj[1])
3978 {
3979 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3980 fileObj[1],
3981 procObj);
3982 }
3983 if (file_count >= 3)
3984 {
3985 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3986 if (fileObj[2])
3987 {
3988 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3989 fileObj[2],
3990 procObj);
3991 }
3992 }
3993
3994 if (ins_rc[0] < 0 || !fileObj[0] ||
3995 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3996 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3997 {
3998 /* Something failed - remove any dictionary
3999 * entries that did make it.
4000 */
4001 if (!ins_rc[0] && fileObj[0])
4002 {
4003 PyDict_DelItem(_PyPopenProcs,
4004 fileObj[0]);
4005 }
4006 if (!ins_rc[1] && fileObj[1])
4007 {
4008 PyDict_DelItem(_PyPopenProcs,
4009 fileObj[1]);
4010 }
4011 if (!ins_rc[2] && fileObj[2])
4012 {
4013 PyDict_DelItem(_PyPopenProcs,
4014 fileObj[2]);
4015 }
4016 }
4017 }
Tim Peters11b23062003-04-23 02:39:17 +00004018
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004019 /*
4020 * Clean up our localized references for the dictionary keys
4021 * and value since PyDict_SetItem will Py_INCREF any copies
4022 * that got placed in the dictionary.
4023 */
4024 Py_XDECREF(procObj);
4025 Py_XDECREF(fileObj[0]);
4026 Py_XDECREF(fileObj[1]);
4027 Py_XDECREF(fileObj[2]);
4028 }
4029
4030 /* Child is launched. */
4031 return f;
4032}
4033
4034/*
4035 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4036 * exit code for the child process and return as a result of the close.
4037 *
4038 * This function uses the _PyPopenProcs dictionary in order to map the
4039 * input file pointer to information about the process that was
4040 * originally created by the popen* call that created the file pointer.
4041 * The dictionary uses the file pointer as a key (with one entry
4042 * inserted for each file returned by the original popen* call) and a
4043 * single list object as the value for all files from a single call.
4044 * The list object contains the Win32 process handle at [0], and a file
4045 * count at [1], which is initialized to the total number of file
4046 * handles using that list.
4047 *
4048 * This function closes whichever handle it is passed, and decrements
4049 * the file count in the dictionary for the process handle pointed to
4050 * by this file. On the last close (when the file count reaches zero),
4051 * this function will wait for the child process and then return its
4052 * exit code as the result of the close() operation. This permits the
4053 * files to be closed in any order - it is always the close() of the
4054 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004055 *
4056 * NOTE: This function is currently called with the GIL released.
4057 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004058 */
4059
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004060static int _PyPclose(FILE *file)
4061{
4062 int result;
4063 int exit_code;
4064 int pipe_pid;
4065 PyObject *procObj, *pidObj, *intObj, *fileObj;
4066 int file_count;
4067#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004068 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004069#endif
4070
4071 /* Close the file handle first, to ensure it can't block the
4072 * child from exiting if it's the last handle.
4073 */
4074 result = fclose(file);
4075
4076#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004077 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004078#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004079 if (_PyPopenProcs)
4080 {
4081 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4082 (procObj = PyDict_GetItem(_PyPopenProcs,
4083 fileObj)) != NULL &&
4084 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4085 (intObj = PyList_GetItem(procObj,1)) != NULL)
4086 {
4087 pipe_pid = (int) PyInt_AsLong(pidObj);
4088 file_count = (int) PyInt_AsLong(intObj);
4089
4090 if (file_count > 1)
4091 {
4092 /* Still other files referencing process */
4093 file_count--;
4094 PyList_SetItem(procObj,1,
4095 PyInt_FromLong((long) file_count));
4096 }
4097 else
4098 {
4099 /* Last file for this process */
4100 if (result != EOF &&
4101 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4102 {
4103 /* extract exit status */
4104 if (WIFEXITED(exit_code))
4105 {
4106 result = WEXITSTATUS(exit_code);
4107 }
4108 else
4109 {
4110 errno = EPIPE;
4111 result = -1;
4112 }
4113 }
4114 else
4115 {
4116 /* Indicate failure - this will cause the file object
4117 * to raise an I/O error and translate the last
4118 * error code from errno. We do have a problem with
4119 * last errors that overlap the normal errno table,
4120 * but that's a consistent problem with the file object.
4121 */
4122 result = -1;
4123 }
4124 }
4125
4126 /* Remove this file pointer from dictionary */
4127 PyDict_DelItem(_PyPopenProcs, fileObj);
4128
4129 if (PyDict_Size(_PyPopenProcs) == 0)
4130 {
4131 Py_DECREF(_PyPopenProcs);
4132 _PyPopenProcs = NULL;
4133 }
4134
4135 } /* if object retrieval ok */
4136
4137 Py_XDECREF(fileObj);
4138 } /* if _PyPopenProcs */
4139
4140#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004141 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004142#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004143 return result;
4144}
4145
4146#endif /* PYCC_??? */
4147
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004148#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004149
4150/*
4151 * Portable 'popen' replacement for Win32.
4152 *
4153 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4154 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004155 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004156 */
4157
4158#include <malloc.h>
4159#include <io.h>
4160#include <fcntl.h>
4161
4162/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4163#define POPEN_1 1
4164#define POPEN_2 2
4165#define POPEN_3 3
4166#define POPEN_4 4
4167
4168static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004169static int _PyPclose(FILE *file);
4170
4171/*
4172 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004173 * for use when retrieving the process exit code. See _PyPclose() below
4174 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004175 */
4176static PyObject *_PyPopenProcs = NULL;
4177
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004178
4179/* popen that works from a GUI.
4180 *
4181 * The result of this function is a pipe (file) connected to the
4182 * processes stdin or stdout, depending on the requested mode.
4183 */
4184
4185static PyObject *
4186posix_popen(PyObject *self, PyObject *args)
4187{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004188 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004189 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004190
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004191 char *cmdstring;
4192 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004193 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004194 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004195 return NULL;
4196
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004197 if (*mode == 'r')
4198 tm = _O_RDONLY;
4199 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004200 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004201 return NULL;
4202 } else
4203 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004204
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004205 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004206 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004207 return NULL;
4208 }
4209
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004210 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004211 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004212 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004213 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004214 else
4215 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4216
4217 return f;
4218}
4219
4220/* Variation on win32pipe.popen
4221 *
4222 * The result of this function is a pipe (file) connected to the
4223 * process's stdin, and a pipe connected to the process's stdout.
4224 */
4225
4226static PyObject *
4227win32_popen2(PyObject *self, PyObject *args)
4228{
4229 PyObject *f;
4230 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004231
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004232 char *cmdstring;
4233 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004234 int bufsize = -1;
4235 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004236 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004237
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004238 if (*mode == 't')
4239 tm = _O_TEXT;
4240 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004241 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004242 return NULL;
4243 } else
4244 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004245
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004246 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004247 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004248 return NULL;
4249 }
4250
4251 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004252
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004253 return f;
4254}
4255
4256/*
4257 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004258 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004259 * The result of this function is 3 pipes - the process's stdin,
4260 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004261 */
4262
4263static PyObject *
4264win32_popen3(PyObject *self, PyObject *args)
4265{
4266 PyObject *f;
4267 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004268
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004269 char *cmdstring;
4270 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004271 int bufsize = -1;
4272 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004273 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004274
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004275 if (*mode == 't')
4276 tm = _O_TEXT;
4277 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004278 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004279 return NULL;
4280 } else
4281 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004282
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004283 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004284 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004285 return NULL;
4286 }
4287
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004288 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004289
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004290 return f;
4291}
4292
4293/*
4294 * Variation on win32pipe.popen
4295 *
Tim Peters5aa91602002-01-30 05:46:57 +00004296 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004297 * and stdout+stderr combined as a single pipe.
4298 */
4299
4300static PyObject *
4301win32_popen4(PyObject *self, PyObject *args)
4302{
4303 PyObject *f;
4304 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004305
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004306 char *cmdstring;
4307 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004308 int bufsize = -1;
4309 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004310 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004311
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004312 if (*mode == 't')
4313 tm = _O_TEXT;
4314 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004315 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004316 return NULL;
4317 } else
4318 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004319
4320 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004321 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004322 return NULL;
4323 }
4324
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004325 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004326
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004327 return f;
4328}
4329
Mark Hammond08501372001-01-31 07:30:29 +00004330static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004331_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004332 HANDLE hStdin,
4333 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004334 HANDLE hStderr,
4335 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004336{
4337 PROCESS_INFORMATION piProcInfo;
4338 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004339 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004340 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004341 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004342 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004343 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004344
4345 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004346 char *comshell;
4347
Tim Peters92e4dd82002-10-05 01:47:34 +00004348 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004349 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004350 /* x < i, so x fits into an integer */
4351 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004352
4353 /* Explicitly check if we are using COMMAND.COM. If we are
4354 * then use the w9xpopen hack.
4355 */
4356 comshell = s1 + x;
4357 while (comshell >= s1 && *comshell != '\\')
4358 --comshell;
4359 ++comshell;
4360
4361 if (GetVersion() < 0x80000000 &&
4362 _stricmp(comshell, "command.com") != 0) {
4363 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004364 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004365 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004366 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004367 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004368 }
4369 else {
4370 /*
Tim Peters402d5982001-08-27 06:37:48 +00004371 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4372 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004373 */
Mark Hammond08501372001-01-31 07:30:29 +00004374 char modulepath[_MAX_PATH];
4375 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004376 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4377 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004378 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004379 x = i+1;
4380 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004381 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004382 strncat(modulepath,
4383 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004384 (sizeof(modulepath)/sizeof(modulepath[0]))
4385 -strlen(modulepath));
4386 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004387 /* Eeek - file-not-found - possibly an embedding
4388 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004389 */
Tim Peters5aa91602002-01-30 05:46:57 +00004390 strncpy(modulepath,
4391 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004392 sizeof(modulepath)/sizeof(modulepath[0]));
4393 if (modulepath[strlen(modulepath)-1] != '\\')
4394 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004395 strncat(modulepath,
4396 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004397 (sizeof(modulepath)/sizeof(modulepath[0]))
4398 -strlen(modulepath));
4399 /* No where else to look - raise an easily identifiable
4400 error, rather than leaving Windows to report
4401 "file not found" - as the user is probably blissfully
4402 unaware this shim EXE is used, and it will confuse them.
4403 (well, it confused me for a while ;-)
4404 */
4405 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004406 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004407 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004408 "for popen to work with your shell "
4409 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004410 szConsoleSpawn);
4411 return FALSE;
4412 }
4413 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004414 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004415 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004416 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004417
Tim Peters92e4dd82002-10-05 01:47:34 +00004418 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004419 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004420 /* To maintain correct argument passing semantics,
4421 we pass the command-line as it stands, and allow
4422 quoting to be applied. w9xpopen.exe will then
4423 use its argv vector, and re-quote the necessary
4424 args for the ultimate child process.
4425 */
Tim Peters75cdad52001-11-28 22:07:30 +00004426 PyOS_snprintf(
4427 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004428 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004429 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004430 s1,
4431 s3,
4432 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004433 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004434 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004435 dialog:
4436 "Your program accessed mem currently in use at xxx"
4437 and a hopeful warning about the stability of your
4438 system.
4439 Cost is Ctrl+C wont kill children, but anyone
4440 who cares can have a go!
4441 */
4442 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004443 }
4444 }
4445
4446 /* Could be an else here to try cmd.exe / command.com in the path
4447 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004448 else {
Tim Peters402d5982001-08-27 06:37:48 +00004449 PyErr_SetString(PyExc_RuntimeError,
4450 "Cannot locate a COMSPEC environment variable to "
4451 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004452 return FALSE;
4453 }
Tim Peters5aa91602002-01-30 05:46:57 +00004454
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004455 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4456 siStartInfo.cb = sizeof(STARTUPINFO);
4457 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4458 siStartInfo.hStdInput = hStdin;
4459 siStartInfo.hStdOutput = hStdout;
4460 siStartInfo.hStdError = hStderr;
4461 siStartInfo.wShowWindow = SW_HIDE;
4462
4463 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004464 s2,
4465 NULL,
4466 NULL,
4467 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004468 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004469 NULL,
4470 NULL,
4471 &siStartInfo,
4472 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004473 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004474 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004475
Mark Hammondb37a3732000-08-14 04:47:33 +00004476 /* Return process handle */
4477 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004478 return TRUE;
4479 }
Tim Peters402d5982001-08-27 06:37:48 +00004480 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004481 return FALSE;
4482}
4483
4484/* The following code is based off of KB: Q190351 */
4485
4486static PyObject *
4487_PyPopen(char *cmdstring, int mode, int n)
4488{
4489 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4490 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004491 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004492
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004493 SECURITY_ATTRIBUTES saAttr;
4494 BOOL fSuccess;
4495 int fd1, fd2, fd3;
4496 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004497 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004498 PyObject *f;
4499
4500 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4501 saAttr.bInheritHandle = TRUE;
4502 saAttr.lpSecurityDescriptor = NULL;
4503
4504 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4505 return win32_error("CreatePipe", NULL);
4506
4507 /* Create new output read handle and the input write handle. Set
4508 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004509 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004510 * being created. */
4511 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004512 GetCurrentProcess(), &hChildStdinWrDup, 0,
4513 FALSE,
4514 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004515 if (!fSuccess)
4516 return win32_error("DuplicateHandle", NULL);
4517
4518 /* Close the inheritable version of ChildStdin
4519 that we're using. */
4520 CloseHandle(hChildStdinWr);
4521
4522 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4523 return win32_error("CreatePipe", NULL);
4524
4525 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004526 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4527 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004528 if (!fSuccess)
4529 return win32_error("DuplicateHandle", NULL);
4530
4531 /* Close the inheritable version of ChildStdout
4532 that we're using. */
4533 CloseHandle(hChildStdoutRd);
4534
4535 if (n != POPEN_4) {
4536 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4537 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004538 fSuccess = DuplicateHandle(GetCurrentProcess(),
4539 hChildStderrRd,
4540 GetCurrentProcess(),
4541 &hChildStderrRdDup, 0,
4542 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004543 if (!fSuccess)
4544 return win32_error("DuplicateHandle", NULL);
4545 /* Close the inheritable version of ChildStdErr that we're using. */
4546 CloseHandle(hChildStderrRd);
4547 }
Tim Peters5aa91602002-01-30 05:46:57 +00004548
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004549 switch (n) {
4550 case POPEN_1:
4551 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4552 case _O_WRONLY | _O_TEXT:
4553 /* Case for writing to child Stdin in text mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004554 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004555 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004556 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004557 PyFile_SetBufSize(f, 0);
4558 /* We don't care about these pipes anymore, so close them. */
4559 CloseHandle(hChildStdoutRdDup);
4560 CloseHandle(hChildStderrRdDup);
4561 break;
4562
4563 case _O_RDONLY | _O_TEXT:
4564 /* Case for reading from child Stdout in text mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004565 fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004566 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004567 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004568 PyFile_SetBufSize(f, 0);
4569 /* We don't care about these pipes anymore, so close them. */
4570 CloseHandle(hChildStdinWrDup);
4571 CloseHandle(hChildStderrRdDup);
4572 break;
4573
4574 case _O_RDONLY | _O_BINARY:
4575 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004576 fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004577 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004578 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004579 PyFile_SetBufSize(f, 0);
4580 /* We don't care about these pipes anymore, so close them. */
4581 CloseHandle(hChildStdinWrDup);
4582 CloseHandle(hChildStderrRdDup);
4583 break;
4584
4585 case _O_WRONLY | _O_BINARY:
4586 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004587 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004588 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004589 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004590 PyFile_SetBufSize(f, 0);
4591 /* We don't care about these pipes anymore, so close them. */
4592 CloseHandle(hChildStdoutRdDup);
4593 CloseHandle(hChildStderrRdDup);
4594 break;
4595 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004596 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004597 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004598
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004599 case POPEN_2:
4600 case POPEN_4:
4601 {
4602 char *m1, *m2;
4603 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004604
Tim Peters7dca21e2002-08-19 00:42:29 +00004605 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004606 m1 = "r";
4607 m2 = "w";
4608 } else {
4609 m1 = "rb";
4610 m2 = "wb";
4611 }
4612
Martin v. Löwis18e16552006-02-15 17:27:45 +00004613 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004614 f1 = _fdopen(fd1, m2);
Martin v. Löwis18e16552006-02-15 17:27:45 +00004615 fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004616 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004617 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004618 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004619 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004620 PyFile_SetBufSize(p2, 0);
4621
4622 if (n != 4)
4623 CloseHandle(hChildStderrRdDup);
4624
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004625 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004626 Py_XDECREF(p1);
4627 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004628 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004629 break;
4630 }
Tim Peters5aa91602002-01-30 05:46:57 +00004631
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004632 case POPEN_3:
4633 {
4634 char *m1, *m2;
4635 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004636
Tim Peters7dca21e2002-08-19 00:42:29 +00004637 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004638 m1 = "r";
4639 m2 = "w";
4640 } else {
4641 m1 = "rb";
4642 m2 = "wb";
4643 }
4644
Martin v. Löwis18e16552006-02-15 17:27:45 +00004645 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004646 f1 = _fdopen(fd1, m2);
Martin v. Löwis18e16552006-02-15 17:27:45 +00004647 fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004648 f2 = _fdopen(fd2, m1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00004649 fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004650 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004651 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004652 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4653 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004654 PyFile_SetBufSize(p1, 0);
4655 PyFile_SetBufSize(p2, 0);
4656 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004657 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004658 Py_XDECREF(p1);
4659 Py_XDECREF(p2);
4660 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004661 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004662 break;
4663 }
4664 }
4665
4666 if (n == POPEN_4) {
4667 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004668 hChildStdinRd,
4669 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004670 hChildStdoutWr,
4671 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004672 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004673 }
4674 else {
4675 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004676 hChildStdinRd,
4677 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004678 hChildStderrWr,
4679 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004680 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004681 }
4682
Mark Hammondb37a3732000-08-14 04:47:33 +00004683 /*
4684 * Insert the files we've created into the process dictionary
4685 * all referencing the list with the process handle and the
4686 * initial number of files (see description below in _PyPclose).
4687 * Since if _PyPclose later tried to wait on a process when all
4688 * handles weren't closed, it could create a deadlock with the
4689 * child, we spend some energy here to try to ensure that we
4690 * either insert all file handles into the dictionary or none
4691 * at all. It's a little clumsy with the various popen modes
4692 * and variable number of files involved.
4693 */
4694 if (!_PyPopenProcs) {
4695 _PyPopenProcs = PyDict_New();
4696 }
4697
4698 if (_PyPopenProcs) {
4699 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4700 int ins_rc[3];
4701
4702 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4703 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4704
4705 procObj = PyList_New(2);
4706 hProcessObj = PyLong_FromVoidPtr(hProcess);
4707 intObj = PyInt_FromLong(file_count);
4708
4709 if (procObj && hProcessObj && intObj) {
4710 PyList_SetItem(procObj,0,hProcessObj);
4711 PyList_SetItem(procObj,1,intObj);
4712
4713 fileObj[0] = PyLong_FromVoidPtr(f1);
4714 if (fileObj[0]) {
4715 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4716 fileObj[0],
4717 procObj);
4718 }
4719 if (file_count >= 2) {
4720 fileObj[1] = PyLong_FromVoidPtr(f2);
4721 if (fileObj[1]) {
4722 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4723 fileObj[1],
4724 procObj);
4725 }
4726 }
4727 if (file_count >= 3) {
4728 fileObj[2] = PyLong_FromVoidPtr(f3);
4729 if (fileObj[2]) {
4730 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4731 fileObj[2],
4732 procObj);
4733 }
4734 }
4735
4736 if (ins_rc[0] < 0 || !fileObj[0] ||
4737 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4738 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4739 /* Something failed - remove any dictionary
4740 * entries that did make it.
4741 */
4742 if (!ins_rc[0] && fileObj[0]) {
4743 PyDict_DelItem(_PyPopenProcs,
4744 fileObj[0]);
4745 }
4746 if (!ins_rc[1] && fileObj[1]) {
4747 PyDict_DelItem(_PyPopenProcs,
4748 fileObj[1]);
4749 }
4750 if (!ins_rc[2] && fileObj[2]) {
4751 PyDict_DelItem(_PyPopenProcs,
4752 fileObj[2]);
4753 }
4754 }
4755 }
Tim Peters5aa91602002-01-30 05:46:57 +00004756
Mark Hammondb37a3732000-08-14 04:47:33 +00004757 /*
4758 * Clean up our localized references for the dictionary keys
4759 * and value since PyDict_SetItem will Py_INCREF any copies
4760 * that got placed in the dictionary.
4761 */
4762 Py_XDECREF(procObj);
4763 Py_XDECREF(fileObj[0]);
4764 Py_XDECREF(fileObj[1]);
4765 Py_XDECREF(fileObj[2]);
4766 }
4767
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004768 /* Child is launched. Close the parents copy of those pipe
4769 * handles that only the child should have open. You need to
4770 * make sure that no handles to the write end of the output pipe
4771 * are maintained in this process or else the pipe will not close
4772 * when the child process exits and the ReadFile will hang. */
4773
4774 if (!CloseHandle(hChildStdinRd))
4775 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004776
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004777 if (!CloseHandle(hChildStdoutWr))
4778 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004779
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004780 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4781 return win32_error("CloseHandle", NULL);
4782
4783 return f;
4784}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004785
4786/*
4787 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4788 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004789 *
4790 * This function uses the _PyPopenProcs dictionary in order to map the
4791 * input file pointer to information about the process that was
4792 * originally created by the popen* call that created the file pointer.
4793 * The dictionary uses the file pointer as a key (with one entry
4794 * inserted for each file returned by the original popen* call) and a
4795 * single list object as the value for all files from a single call.
4796 * The list object contains the Win32 process handle at [0], and a file
4797 * count at [1], which is initialized to the total number of file
4798 * handles using that list.
4799 *
4800 * This function closes whichever handle it is passed, and decrements
4801 * the file count in the dictionary for the process handle pointed to
4802 * by this file. On the last close (when the file count reaches zero),
4803 * this function will wait for the child process and then return its
4804 * exit code as the result of the close() operation. This permits the
4805 * files to be closed in any order - it is always the close() of the
4806 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004807 *
4808 * NOTE: This function is currently called with the GIL released.
4809 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004810 */
Tim Peters736aa322000-09-01 06:51:24 +00004811
Fredrik Lundh56055a42000-07-23 19:47:12 +00004812static int _PyPclose(FILE *file)
4813{
Fredrik Lundh20318932000-07-26 17:29:12 +00004814 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004815 DWORD exit_code;
4816 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004817 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4818 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004819#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004820 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004821#endif
4822
Fredrik Lundh20318932000-07-26 17:29:12 +00004823 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004824 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004825 */
4826 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004827#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004828 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004829#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004830 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004831 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4832 (procObj = PyDict_GetItem(_PyPopenProcs,
4833 fileObj)) != NULL &&
4834 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4835 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4836
4837 hProcess = PyLong_AsVoidPtr(hProcessObj);
4838 file_count = PyInt_AsLong(intObj);
4839
4840 if (file_count > 1) {
4841 /* Still other files referencing process */
4842 file_count--;
4843 PyList_SetItem(procObj,1,
4844 PyInt_FromLong(file_count));
4845 } else {
4846 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004847 if (result != EOF &&
4848 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4849 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004850 /* Possible truncation here in 16-bit environments, but
4851 * real exit codes are just the lower byte in any event.
4852 */
4853 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004854 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004855 /* Indicate failure - this will cause the file object
4856 * to raise an I/O error and translate the last Win32
4857 * error code from errno. We do have a problem with
4858 * last errors that overlap the normal errno table,
4859 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004860 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004861 if (result != EOF) {
4862 /* If the error wasn't from the fclose(), then
4863 * set errno for the file object error handling.
4864 */
4865 errno = GetLastError();
4866 }
4867 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004868 }
4869
4870 /* Free up the native handle at this point */
4871 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004872 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004873
Mark Hammondb37a3732000-08-14 04:47:33 +00004874 /* Remove this file pointer from dictionary */
4875 PyDict_DelItem(_PyPopenProcs, fileObj);
4876
4877 if (PyDict_Size(_PyPopenProcs) == 0) {
4878 Py_DECREF(_PyPopenProcs);
4879 _PyPopenProcs = NULL;
4880 }
4881
4882 } /* if object retrieval ok */
4883
4884 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004885 } /* if _PyPopenProcs */
4886
Tim Peters736aa322000-09-01 06:51:24 +00004887#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004888 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004889#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004890 return result;
4891}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004892
4893#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004894static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004895posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004896{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004897 char *name;
4898 char *mode = "r";
4899 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004900 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004901 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004902 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004903 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004904 /* Strip mode of binary or text modifiers */
4905 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4906 mode = "r";
4907 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4908 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004909 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004910 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004911 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004912 if (fp == NULL)
4913 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004914 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004915 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004916 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004917 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004918}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004919
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004920#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004921#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004923
Guido van Rossumb6775db1994-08-01 11:34:53 +00004924#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004925PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004926"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004927Set the current process's user id.");
4928
Barry Warsaw53699e91996-12-10 23:23:01 +00004929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004930posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004931{
4932 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004933 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004934 return NULL;
4935 if (setuid(uid) < 0)
4936 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004937 Py_INCREF(Py_None);
4938 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004939}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004940#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004941
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004942
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004943#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004944PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004945"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004946Set the current process's effective user id.");
4947
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004948static PyObject *
4949posix_seteuid (PyObject *self, PyObject *args)
4950{
4951 int euid;
4952 if (!PyArg_ParseTuple(args, "i", &euid)) {
4953 return NULL;
4954 } else if (seteuid(euid) < 0) {
4955 return posix_error();
4956 } else {
4957 Py_INCREF(Py_None);
4958 return Py_None;
4959 }
4960}
4961#endif /* HAVE_SETEUID */
4962
4963#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004964PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004965"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004966Set the current process's effective group id.");
4967
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004968static PyObject *
4969posix_setegid (PyObject *self, PyObject *args)
4970{
4971 int egid;
4972 if (!PyArg_ParseTuple(args, "i", &egid)) {
4973 return NULL;
4974 } else if (setegid(egid) < 0) {
4975 return posix_error();
4976 } else {
4977 Py_INCREF(Py_None);
4978 return Py_None;
4979 }
4980}
4981#endif /* HAVE_SETEGID */
4982
4983#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004984PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004985"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004986Set the current process's real and effective user ids.");
4987
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004988static PyObject *
4989posix_setreuid (PyObject *self, PyObject *args)
4990{
4991 int ruid, euid;
4992 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4993 return NULL;
4994 } else if (setreuid(ruid, euid) < 0) {
4995 return posix_error();
4996 } else {
4997 Py_INCREF(Py_None);
4998 return Py_None;
4999 }
5000}
5001#endif /* HAVE_SETREUID */
5002
5003#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005004PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005005"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005006Set the current process's real and effective group ids.");
5007
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005008static PyObject *
5009posix_setregid (PyObject *self, PyObject *args)
5010{
5011 int rgid, egid;
5012 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5013 return NULL;
5014 } else if (setregid(rgid, egid) < 0) {
5015 return posix_error();
5016 } else {
5017 Py_INCREF(Py_None);
5018 return Py_None;
5019 }
5020}
5021#endif /* HAVE_SETREGID */
5022
Guido van Rossumb6775db1994-08-01 11:34:53 +00005023#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005024PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005025"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005026Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005027
Barry Warsaw53699e91996-12-10 23:23:01 +00005028static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005029posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005030{
5031 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005032 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005033 return NULL;
5034 if (setgid(gid) < 0)
5035 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005036 Py_INCREF(Py_None);
5037 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005038}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005039#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005040
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005041#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005042PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005043"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005044Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005045
5046static PyObject *
5047posix_setgroups(PyObject *self, PyObject *args)
5048{
5049 PyObject *groups;
5050 int i, len;
5051 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005052
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005053 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
5054 return NULL;
5055 if (!PySequence_Check(groups)) {
5056 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5057 return NULL;
5058 }
5059 len = PySequence_Size(groups);
5060 if (len > MAX_GROUPS) {
5061 PyErr_SetString(PyExc_ValueError, "too many groups");
5062 return NULL;
5063 }
5064 for(i = 0; i < len; i++) {
5065 PyObject *elem;
5066 elem = PySequence_GetItem(groups, i);
5067 if (!elem)
5068 return NULL;
5069 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005070 if (!PyLong_Check(elem)) {
5071 PyErr_SetString(PyExc_TypeError,
5072 "groups must be integers");
5073 Py_DECREF(elem);
5074 return NULL;
5075 } else {
5076 unsigned long x = PyLong_AsUnsignedLong(elem);
5077 if (PyErr_Occurred()) {
5078 PyErr_SetString(PyExc_TypeError,
5079 "group id too big");
5080 Py_DECREF(elem);
5081 return NULL;
5082 }
5083 grouplist[i] = x;
5084 /* read back the value to see if it fitted in gid_t */
5085 if (grouplist[i] != x) {
5086 PyErr_SetString(PyExc_TypeError,
5087 "group id too big");
5088 Py_DECREF(elem);
5089 return NULL;
5090 }
5091 }
5092 } else {
5093 long x = PyInt_AsLong(elem);
5094 grouplist[i] = x;
5095 if (grouplist[i] != x) {
5096 PyErr_SetString(PyExc_TypeError,
5097 "group id too big");
5098 Py_DECREF(elem);
5099 return NULL;
5100 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005101 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005102 Py_DECREF(elem);
5103 }
5104
5105 if (setgroups(len, grouplist) < 0)
5106 return posix_error();
5107 Py_INCREF(Py_None);
5108 return Py_None;
5109}
5110#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005111
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005112#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005113static PyObject *
5114wait_helper(int pid, int status, struct rusage *ru)
5115{
5116 PyObject *result;
5117 static PyObject *struct_rusage;
5118
5119 if (pid == -1)
5120 return posix_error();
5121
5122 if (struct_rusage == NULL) {
5123 PyObject *m = PyImport_ImportModule("resource");
5124 if (m == NULL)
5125 return NULL;
5126 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5127 Py_DECREF(m);
5128 if (struct_rusage == NULL)
5129 return NULL;
5130 }
5131
5132 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5133 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5134 if (!result)
5135 return NULL;
5136
5137#ifndef doubletime
5138#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5139#endif
5140
5141 PyStructSequence_SET_ITEM(result, 0,
5142 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5143 PyStructSequence_SET_ITEM(result, 1,
5144 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5145#define SET_INT(result, index, value)\
5146 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5147 SET_INT(result, 2, ru->ru_maxrss);
5148 SET_INT(result, 3, ru->ru_ixrss);
5149 SET_INT(result, 4, ru->ru_idrss);
5150 SET_INT(result, 5, ru->ru_isrss);
5151 SET_INT(result, 6, ru->ru_minflt);
5152 SET_INT(result, 7, ru->ru_majflt);
5153 SET_INT(result, 8, ru->ru_nswap);
5154 SET_INT(result, 9, ru->ru_inblock);
5155 SET_INT(result, 10, ru->ru_oublock);
5156 SET_INT(result, 11, ru->ru_msgsnd);
5157 SET_INT(result, 12, ru->ru_msgrcv);
5158 SET_INT(result, 13, ru->ru_nsignals);
5159 SET_INT(result, 14, ru->ru_nvcsw);
5160 SET_INT(result, 15, ru->ru_nivcsw);
5161#undef SET_INT
5162
5163 if (PyErr_Occurred()) {
5164 Py_DECREF(result);
5165 return NULL;
5166 }
5167
Neal Norwitz9b00a562006-03-20 08:47:12 +00005168 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005169}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005170#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005171
5172#ifdef HAVE_WAIT3
5173PyDoc_STRVAR(posix_wait3__doc__,
5174"wait3(options) -> (pid, status, rusage)\n\n\
5175Wait for completion of a child process.");
5176
5177static PyObject *
5178posix_wait3(PyObject *self, PyObject *args)
5179{
5180 int pid, options;
5181 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005182 WAIT_TYPE status;
5183 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005184
5185 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5186 return NULL;
5187
5188 Py_BEGIN_ALLOW_THREADS
5189 pid = wait3(&status, options, &ru);
5190 Py_END_ALLOW_THREADS
5191
Neal Norwitzd5a37542006-03-20 06:48:34 +00005192 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005193}
5194#endif /* HAVE_WAIT3 */
5195
5196#ifdef HAVE_WAIT4
5197PyDoc_STRVAR(posix_wait4__doc__,
5198"wait4(pid, options) -> (pid, status, rusage)\n\n\
5199Wait for completion of a given child process.");
5200
5201static PyObject *
5202posix_wait4(PyObject *self, PyObject *args)
5203{
5204 int pid, options;
5205 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005206 WAIT_TYPE status;
5207 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005208
5209 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5210 return NULL;
5211
5212 Py_BEGIN_ALLOW_THREADS
5213 pid = wait4(pid, &status, options, &ru);
5214 Py_END_ALLOW_THREADS
5215
Neal Norwitzd5a37542006-03-20 06:48:34 +00005216 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005217}
5218#endif /* HAVE_WAIT4 */
5219
Guido van Rossumb6775db1994-08-01 11:34:53 +00005220#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005221PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005222"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005223Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005224
Barry Warsaw53699e91996-12-10 23:23:01 +00005225static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005226posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005227{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005228 int pid, options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005229 WAIT_TYPE status;
5230 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005231
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005232 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005233 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005234 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005235 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005236 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005237 if (pid == -1)
5238 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005239
5240 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005241}
5242
Tim Petersab034fa2002-02-01 11:27:43 +00005243#elif defined(HAVE_CWAIT)
5244
5245/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005246PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005247"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005248"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005249
5250static PyObject *
5251posix_waitpid(PyObject *self, PyObject *args)
5252{
Martin v. Löwis18e16552006-02-15 17:27:45 +00005253 intptr_t pid;
5254 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005255
5256 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5257 return NULL;
5258 Py_BEGIN_ALLOW_THREADS
5259 pid = _cwait(&status, pid, options);
5260 Py_END_ALLOW_THREADS
5261 if (pid == -1)
5262 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005263
5264 /* shift the status left a byte so this is more like the POSIX waitpid */
5265 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005266}
5267#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005268
Guido van Rossumad0ee831995-03-01 10:34:45 +00005269#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005270PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005271"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005273
Barry Warsaw53699e91996-12-10 23:23:01 +00005274static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005275posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005276{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005277 int pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005278 WAIT_TYPE status;
5279 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005280
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005281 Py_BEGIN_ALLOW_THREADS
5282 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005283 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005284 if (pid == -1)
5285 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005286
5287 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005288}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005289#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005290
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005291
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005292PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005293"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005294Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005295
Barry Warsaw53699e91996-12-10 23:23:01 +00005296static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005297posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005298{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005299#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005300 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005301#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005302#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005303 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005304#else
5305 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5306#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005307#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005308}
5309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005310
Guido van Rossumb6775db1994-08-01 11:34:53 +00005311#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005312PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005313"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005314Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005315
Barry Warsaw53699e91996-12-10 23:23:01 +00005316static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005317posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005318{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005319 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005320 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005321 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005322 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005323 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005324 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005325 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005326 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005327 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005328 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00005329 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005330}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005331#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005332
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005333
Guido van Rossumb6775db1994-08-01 11:34:53 +00005334#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005335PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005336"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005337Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005338
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005339static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005340posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005341{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005342 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005343}
5344#endif /* HAVE_SYMLINK */
5345
5346
5347#ifdef HAVE_TIMES
5348#ifndef HZ
5349#define HZ 60 /* Universal constant :-) */
5350#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005351
Guido van Rossumd48f2521997-12-05 22:19:34 +00005352#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5353static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005354system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005355{
5356 ULONG value = 0;
5357
5358 Py_BEGIN_ALLOW_THREADS
5359 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5360 Py_END_ALLOW_THREADS
5361
5362 return value;
5363}
5364
5365static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005366posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005367{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005368 /* Currently Only Uptime is Provided -- Others Later */
5369 return Py_BuildValue("ddddd",
5370 (double)0 /* t.tms_utime / HZ */,
5371 (double)0 /* t.tms_stime / HZ */,
5372 (double)0 /* t.tms_cutime / HZ */,
5373 (double)0 /* t.tms_cstime / HZ */,
5374 (double)system_uptime() / 1000);
5375}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005376#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005377static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005378posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005379{
5380 struct tms t;
5381 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005382 errno = 0;
5383 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005384 if (c == (clock_t) -1)
5385 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005386 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005387 (double)t.tms_utime / HZ,
5388 (double)t.tms_stime / HZ,
5389 (double)t.tms_cutime / HZ,
5390 (double)t.tms_cstime / HZ,
5391 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005392}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005393#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005394#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005395
5396
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005397#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005398#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005399static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005400posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005401{
5402 FILETIME create, exit, kernel, user;
5403 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005404 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005405 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5406 /* The fields of a FILETIME structure are the hi and lo part
5407 of a 64-bit value expressed in 100 nanosecond units.
5408 1e7 is one second in such units; 1e-7 the inverse.
5409 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5410 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005411 return Py_BuildValue(
5412 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005413 (double)(kernel.dwHighDateTime*429.4967296 +
5414 kernel.dwLowDateTime*1e-7),
5415 (double)(user.dwHighDateTime*429.4967296 +
5416 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005417 (double)0,
5418 (double)0,
5419 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005420}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005421#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005422
5423#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005424PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005425"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005427#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005429
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005430#ifdef HAVE_GETSID
5431PyDoc_STRVAR(posix_getsid__doc__,
5432"getsid(pid) -> sid\n\n\
5433Call the system call getsid().");
5434
5435static PyObject *
5436posix_getsid(PyObject *self, PyObject *args)
5437{
5438 int pid, sid;
5439 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5440 return NULL;
5441 sid = getsid(pid);
5442 if (sid < 0)
5443 return posix_error();
5444 return PyInt_FromLong((long)sid);
5445}
5446#endif /* HAVE_GETSID */
5447
5448
Guido van Rossumb6775db1994-08-01 11:34:53 +00005449#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005450PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005451"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005452Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005453
Barry Warsaw53699e91996-12-10 23:23:01 +00005454static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005455posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005456{
Guido van Rossum687dd131993-05-17 08:34:16 +00005457 if (setsid() < 0)
5458 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005459 Py_INCREF(Py_None);
5460 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005461}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005462#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005463
Guido van Rossumb6775db1994-08-01 11:34:53 +00005464#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005465PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005466"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005467Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005468
Barry Warsaw53699e91996-12-10 23:23:01 +00005469static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005470posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005471{
5472 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005473 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005474 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005475 if (setpgid(pid, pgrp) < 0)
5476 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005477 Py_INCREF(Py_None);
5478 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005479}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005480#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005481
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005482
Guido van Rossumb6775db1994-08-01 11:34:53 +00005483#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005484PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005485"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005486Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005487
Barry Warsaw53699e91996-12-10 23:23:01 +00005488static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005489posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005490{
5491 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005492 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005493 return NULL;
5494 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005495 if (pgid < 0)
5496 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005497 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005498}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005499#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005500
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005501
Guido van Rossumb6775db1994-08-01 11:34:53 +00005502#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005504"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005505Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005506
Barry Warsaw53699e91996-12-10 23:23:01 +00005507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005508posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005509{
5510 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005511 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005512 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005513 if (tcsetpgrp(fd, pgid) < 0)
5514 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005515 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005516 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005517}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005518#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005519
Guido van Rossum687dd131993-05-17 08:34:16 +00005520/* Functions acting on file descriptors */
5521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005522PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005523"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005525
Barry Warsaw53699e91996-12-10 23:23:01 +00005526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005527posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005528{
Mark Hammondef8b6542001-05-13 08:04:26 +00005529 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005530 int flag;
5531 int mode = 0777;
5532 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005533
5534#ifdef MS_WINDOWS
5535 if (unicode_file_names()) {
5536 PyUnicodeObject *po;
5537 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5538 Py_BEGIN_ALLOW_THREADS
5539 /* PyUnicode_AS_UNICODE OK without thread
5540 lock as it is a simple dereference. */
5541 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5542 Py_END_ALLOW_THREADS
5543 if (fd < 0)
5544 return posix_error();
5545 return PyInt_FromLong((long)fd);
5546 }
5547 /* Drop the argument parsing error as narrow strings
5548 are also valid. */
5549 PyErr_Clear();
5550 }
5551#endif
5552
Tim Peters5aa91602002-01-30 05:46:57 +00005553 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005554 Py_FileSystemDefaultEncoding, &file,
5555 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005556 return NULL;
5557
Barry Warsaw53699e91996-12-10 23:23:01 +00005558 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005559 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005560 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005561 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005562 return posix_error_with_allocated_filename(file);
5563 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005564 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005565}
5566
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005568PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005569"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005570Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005571
Barry Warsaw53699e91996-12-10 23:23:01 +00005572static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005573posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005574{
5575 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005576 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005577 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005578 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005579 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005580 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005581 if (res < 0)
5582 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005583 Py_INCREF(Py_None);
5584 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005585}
5586
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005588PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005589"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005590Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005591
Barry Warsaw53699e91996-12-10 23:23:01 +00005592static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005593posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005594{
5595 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005596 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005597 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005598 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005599 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005600 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005601 if (fd < 0)
5602 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005603 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005604}
5605
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005607PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005608"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005609Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005610
Barry Warsaw53699e91996-12-10 23:23:01 +00005611static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005612posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005613{
5614 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005615 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005616 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005617 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005618 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005619 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005620 if (res < 0)
5621 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005622 Py_INCREF(Py_None);
5623 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005624}
5625
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005627PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005628"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005629Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005630
Barry Warsaw53699e91996-12-10 23:23:01 +00005631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005632posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005633{
5634 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005635#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005636 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005637#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005638 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005639#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005640 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005641 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005642 return NULL;
5643#ifdef SEEK_SET
5644 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5645 switch (how) {
5646 case 0: how = SEEK_SET; break;
5647 case 1: how = SEEK_CUR; break;
5648 case 2: how = SEEK_END; break;
5649 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005650#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005651
5652#if !defined(HAVE_LARGEFILE_SUPPORT)
5653 pos = PyInt_AsLong(posobj);
5654#else
5655 pos = PyLong_Check(posobj) ?
5656 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5657#endif
5658 if (PyErr_Occurred())
5659 return NULL;
5660
Barry Warsaw53699e91996-12-10 23:23:01 +00005661 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005662#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005663 res = _lseeki64(fd, pos, how);
5664#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005665 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005666#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005667 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005668 if (res < 0)
5669 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005670
5671#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005672 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005673#else
5674 return PyLong_FromLongLong(res);
5675#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005676}
5677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005679PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005680"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005681Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005682
Barry Warsaw53699e91996-12-10 23:23:01 +00005683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005684posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005685{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005686 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005687 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005688 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005689 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005690 if (size < 0) {
5691 errno = EINVAL;
5692 return posix_error();
5693 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005694 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005695 if (buffer == NULL)
5696 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005697 Py_BEGIN_ALLOW_THREADS
5698 n = read(fd, PyString_AsString(buffer), size);
5699 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005700 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005701 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005702 return posix_error();
5703 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005704 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005705 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005706 return buffer;
5707}
5708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005710PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005711"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005712Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005713
Barry Warsaw53699e91996-12-10 23:23:01 +00005714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005715posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005716{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005717 int fd;
5718 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00005719 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005720
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005721 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005722 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005723 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005724 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005725 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005726 if (size < 0)
5727 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005728 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005729}
5730
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005732PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005733"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005734Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005735
Barry Warsaw53699e91996-12-10 23:23:01 +00005736static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005737posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005738{
5739 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005740 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005741 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005742 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005743 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005744#ifdef __VMS
5745 /* on OpenVMS we must ensure that all bytes are written to the file */
5746 fsync(fd);
5747#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005748 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005749 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005750 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005751 if (res != 0) {
5752#ifdef MS_WINDOWS
5753 return win32_error("fstat", NULL);
5754#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005755 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005756#endif
5757 }
Tim Peters5aa91602002-01-30 05:46:57 +00005758
Martin v. Löwis14694662006-02-03 12:54:16 +00005759 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005760}
5761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005763PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005764"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005765Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005766
Barry Warsaw53699e91996-12-10 23:23:01 +00005767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005768posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005769{
Guido van Rossum687dd131993-05-17 08:34:16 +00005770 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005771 char *mode = "r";
5772 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005773 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005774 PyObject *f;
5775 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005776 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005777
Thomas Heller1f043e22002-11-07 16:00:59 +00005778 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5779 PyErr_Format(PyExc_ValueError,
5780 "invalid file mode '%s'", mode);
5781 return NULL;
5782 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005783 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00005784#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00005785 if (mode[0] == 'a') {
5786 /* try to make sure the O_APPEND flag is set */
5787 int flags;
5788 flags = fcntl(fd, F_GETFL);
5789 if (flags != -1)
5790 fcntl(fd, F_SETFL, flags | O_APPEND);
5791 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00005792 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00005793 /* restore old mode if fdopen failed */
5794 fcntl(fd, F_SETFL, flags);
5795 } else {
5796 fp = fdopen(fd, mode);
5797 }
Georg Brandl644b1e72006-03-31 20:27:22 +00005798#else
5799 fp = fdopen(fd, mode);
5800#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005801 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005802 if (fp == NULL)
5803 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005804 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005805 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005806 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005807 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005808}
5809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005810PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005811"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005812Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005813connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005814
5815static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005816posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005817{
5818 int fd;
5819 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5820 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005821 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005822}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005823
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005824#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005825PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005826"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005827Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005828
Barry Warsaw53699e91996-12-10 23:23:01 +00005829static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005830posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005831{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005832#if defined(PYOS_OS2)
5833 HFILE read, write;
5834 APIRET rc;
5835
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005836 Py_BEGIN_ALLOW_THREADS
5837 rc = DosCreatePipe( &read, &write, 4096);
5838 Py_END_ALLOW_THREADS
5839 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005840 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005841
5842 return Py_BuildValue("(ii)", read, write);
5843#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005844#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005845 int fds[2];
5846 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005847 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005848 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005849 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005850 if (res != 0)
5851 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005852 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005853#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005854 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005855 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005856 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005857 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005858 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005859 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005860 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005861 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005862 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5863 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005864 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005865#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005866#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005867}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005868#endif /* HAVE_PIPE */
5869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005870
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005871#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005873"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005874Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005875
Barry Warsaw53699e91996-12-10 23:23:01 +00005876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005877posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005878{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005879 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005880 int mode = 0666;
5881 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005882 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005883 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005884 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005885 res = mkfifo(filename, mode);
5886 Py_END_ALLOW_THREADS
5887 if (res < 0)
5888 return posix_error();
5889 Py_INCREF(Py_None);
5890 return Py_None;
5891}
5892#endif
5893
5894
Neal Norwitz11690112002-07-30 01:08:28 +00005895#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005896PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005897"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005898Create a filesystem node (file, device special file or named pipe)\n\
5899named filename. mode specifies both the permissions to use and the\n\
5900type of node to be created, being combined (bitwise OR) with one of\n\
5901S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005902device defines the newly created device special file (probably using\n\
5903os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005904
5905
5906static PyObject *
5907posix_mknod(PyObject *self, PyObject *args)
5908{
5909 char *filename;
5910 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005911 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005912 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005913 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005914 return NULL;
5915 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005916 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005917 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005918 if (res < 0)
5919 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005920 Py_INCREF(Py_None);
5921 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005922}
5923#endif
5924
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005925#ifdef HAVE_DEVICE_MACROS
5926PyDoc_STRVAR(posix_major__doc__,
5927"major(device) -> major number\n\
5928Extracts a device major number from a raw device number.");
5929
5930static PyObject *
5931posix_major(PyObject *self, PyObject *args)
5932{
5933 int device;
5934 if (!PyArg_ParseTuple(args, "i:major", &device))
5935 return NULL;
5936 return PyInt_FromLong((long)major(device));
5937}
5938
5939PyDoc_STRVAR(posix_minor__doc__,
5940"minor(device) -> minor number\n\
5941Extracts a device minor number from a raw device number.");
5942
5943static PyObject *
5944posix_minor(PyObject *self, PyObject *args)
5945{
5946 int device;
5947 if (!PyArg_ParseTuple(args, "i:minor", &device))
5948 return NULL;
5949 return PyInt_FromLong((long)minor(device));
5950}
5951
5952PyDoc_STRVAR(posix_makedev__doc__,
5953"makedev(major, minor) -> device number\n\
5954Composes a raw device number from the major and minor device numbers.");
5955
5956static PyObject *
5957posix_makedev(PyObject *self, PyObject *args)
5958{
5959 int major, minor;
5960 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5961 return NULL;
5962 return PyInt_FromLong((long)makedev(major, minor));
5963}
5964#endif /* device macros */
5965
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005966
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005967#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005968PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005969"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005970Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005971
Barry Warsaw53699e91996-12-10 23:23:01 +00005972static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005973posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005974{
5975 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005976 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005977 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005978 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005979
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005980 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005981 return NULL;
5982
5983#if !defined(HAVE_LARGEFILE_SUPPORT)
5984 length = PyInt_AsLong(lenobj);
5985#else
5986 length = PyLong_Check(lenobj) ?
5987 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5988#endif
5989 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005990 return NULL;
5991
Barry Warsaw53699e91996-12-10 23:23:01 +00005992 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005993 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005994 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005995 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005996 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005997 return NULL;
5998 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005999 Py_INCREF(Py_None);
6000 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006001}
6002#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006003
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006004#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006005PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006006"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006007Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006008
Fred Drake762e2061999-08-26 17:23:54 +00006009/* Save putenv() parameters as values here, so we can collect them when they
6010 * get re-set with another call for the same key. */
6011static PyObject *posix_putenv_garbage;
6012
Tim Peters5aa91602002-01-30 05:46:57 +00006013static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006014posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006015{
6016 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006017 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006018 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006019 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006020
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006021 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006022 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006023
6024#if defined(PYOS_OS2)
6025 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6026 APIRET rc;
6027
Guido van Rossumd48f2521997-12-05 22:19:34 +00006028 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6029 if (rc != NO_ERROR)
6030 return os2_error(rc);
6031
6032 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6033 APIRET rc;
6034
Guido van Rossumd48f2521997-12-05 22:19:34 +00006035 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6036 if (rc != NO_ERROR)
6037 return os2_error(rc);
6038 } else {
6039#endif
6040
Fred Drake762e2061999-08-26 17:23:54 +00006041 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006042 len = strlen(s1) + strlen(s2) + 2;
6043 /* len includes space for a trailing \0; the size arg to
6044 PyString_FromStringAndSize does not count that */
6045 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006046 if (newstr == NULL)
6047 return PyErr_NoMemory();
Anthony Baxter64182fe2006-04-11 12:14:09 +00006048 newenv = PyString_AS_STRING(newstr);
6049 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6050 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006051 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006052 posix_error();
6053 return NULL;
6054 }
Fred Drake762e2061999-08-26 17:23:54 +00006055 /* Install the first arg and newstr in posix_putenv_garbage;
6056 * this will cause previous value to be collected. This has to
6057 * happen after the real putenv() call because the old value
6058 * was still accessible until then. */
6059 if (PyDict_SetItem(posix_putenv_garbage,
6060 PyTuple_GET_ITEM(args, 0), newstr)) {
6061 /* really not much we can do; just leak */
6062 PyErr_Clear();
6063 }
6064 else {
6065 Py_DECREF(newstr);
6066 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006067
6068#if defined(PYOS_OS2)
6069 }
6070#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006071 Py_INCREF(Py_None);
6072 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006073}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006074#endif /* putenv */
6075
Guido van Rossumc524d952001-10-19 01:31:59 +00006076#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006077PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006078"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006079Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006080
6081static PyObject *
6082posix_unsetenv(PyObject *self, PyObject *args)
6083{
6084 char *s1;
6085
6086 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6087 return NULL;
6088
6089 unsetenv(s1);
6090
6091 /* Remove the key from posix_putenv_garbage;
6092 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006093 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006094 * old value was still accessible until then.
6095 */
6096 if (PyDict_DelItem(posix_putenv_garbage,
6097 PyTuple_GET_ITEM(args, 0))) {
6098 /* really not much we can do; just leak */
6099 PyErr_Clear();
6100 }
6101
6102 Py_INCREF(Py_None);
6103 return Py_None;
6104}
6105#endif /* unsetenv */
6106
Guido van Rossumb6a47161997-09-15 22:54:34 +00006107#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006108PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006109"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006110Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006111
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006112static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006113posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006114{
6115 int code;
6116 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006117 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006118 return NULL;
6119 message = strerror(code);
6120 if (message == NULL) {
6121 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006122 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006123 return NULL;
6124 }
6125 return PyString_FromString(message);
6126}
6127#endif /* strerror */
6128
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006129
Guido van Rossumc9641791998-08-04 15:26:23 +00006130#ifdef HAVE_SYS_WAIT_H
6131
Fred Drake106c1a02002-04-23 15:58:02 +00006132#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006134"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006136
6137static PyObject *
6138posix_WCOREDUMP(PyObject *self, PyObject *args)
6139{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006140 WAIT_TYPE status;
6141 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006142
Neal Norwitzd5a37542006-03-20 06:48:34 +00006143 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006144 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006145
6146 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006147}
6148#endif /* WCOREDUMP */
6149
6150#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006151PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006152"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006153Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006154job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006155
6156static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006157posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006158{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006159 WAIT_TYPE status;
6160 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006161
Neal Norwitzd5a37542006-03-20 06:48:34 +00006162 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006163 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006164
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006165 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006166}
6167#endif /* WIFCONTINUED */
6168
Guido van Rossumc9641791998-08-04 15:26:23 +00006169#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006170PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006171"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006172Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006173
6174static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006175posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006176{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006177 WAIT_TYPE status;
6178 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006179
Neal Norwitzd5a37542006-03-20 06:48:34 +00006180 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006181 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006182
Fred Drake106c1a02002-04-23 15:58:02 +00006183 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006184}
6185#endif /* WIFSTOPPED */
6186
6187#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006188PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006189"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006190Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006191
6192static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006193posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006194{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006195 WAIT_TYPE status;
6196 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006197
Neal Norwitzd5a37542006-03-20 06:48:34 +00006198 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006199 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006200
Fred Drake106c1a02002-04-23 15:58:02 +00006201 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006202}
6203#endif /* WIFSIGNALED */
6204
6205#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006206PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006207"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006208Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006209system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006210
6211static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006212posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006213{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006214 WAIT_TYPE status;
6215 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006216
Neal Norwitzd5a37542006-03-20 06:48:34 +00006217 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006218 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006219
Fred Drake106c1a02002-04-23 15:58:02 +00006220 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006221}
6222#endif /* WIFEXITED */
6223
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006224#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006225PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006226"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006227Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006228
6229static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006230posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006231{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006232 WAIT_TYPE status;
6233 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006234
Neal Norwitzd5a37542006-03-20 06:48:34 +00006235 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006236 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006237
Guido van Rossumc9641791998-08-04 15:26:23 +00006238 return Py_BuildValue("i", WEXITSTATUS(status));
6239}
6240#endif /* WEXITSTATUS */
6241
6242#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006243PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006244"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006245Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006246value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006247
6248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006249posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006250{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006251 WAIT_TYPE status;
6252 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006253
Neal Norwitzd5a37542006-03-20 06:48:34 +00006254 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006255 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006256
Guido van Rossumc9641791998-08-04 15:26:23 +00006257 return Py_BuildValue("i", WTERMSIG(status));
6258}
6259#endif /* WTERMSIG */
6260
6261#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006262PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006263"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006264Return the signal that stopped the process that provided\n\
6265the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006266
6267static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006268posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006269{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006270 WAIT_TYPE status;
6271 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006272
Neal Norwitzd5a37542006-03-20 06:48:34 +00006273 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006274 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006275
Guido van Rossumc9641791998-08-04 15:26:23 +00006276 return Py_BuildValue("i", WSTOPSIG(status));
6277}
6278#endif /* WSTOPSIG */
6279
6280#endif /* HAVE_SYS_WAIT_H */
6281
6282
Guido van Rossum94f6f721999-01-06 18:42:14 +00006283#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006284#ifdef _SCO_DS
6285/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6286 needed definitions in sys/statvfs.h */
6287#define _SVID3
6288#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006289#include <sys/statvfs.h>
6290
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006291static PyObject*
6292_pystatvfs_fromstructstatvfs(struct statvfs st) {
6293 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6294 if (v == NULL)
6295 return NULL;
6296
6297#if !defined(HAVE_LARGEFILE_SUPPORT)
6298 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6299 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6300 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6301 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6302 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6303 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6304 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6305 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6306 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6307 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6308#else
6309 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6310 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006311 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006312 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006313 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006314 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006315 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006316 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006317 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006318 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006319 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006320 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006321 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006322 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006323 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6324 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6325#endif
6326
6327 return v;
6328}
6329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006330PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006331"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006332Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006333
6334static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006335posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006336{
6337 int fd, res;
6338 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006339
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006340 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006341 return NULL;
6342 Py_BEGIN_ALLOW_THREADS
6343 res = fstatvfs(fd, &st);
6344 Py_END_ALLOW_THREADS
6345 if (res != 0)
6346 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006347
6348 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006349}
6350#endif /* HAVE_FSTATVFS */
6351
6352
6353#if defined(HAVE_STATVFS)
6354#include <sys/statvfs.h>
6355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006356PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006357"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006358Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006359
6360static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006361posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006362{
6363 char *path;
6364 int res;
6365 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006366 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006367 return NULL;
6368 Py_BEGIN_ALLOW_THREADS
6369 res = statvfs(path, &st);
6370 Py_END_ALLOW_THREADS
6371 if (res != 0)
6372 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006373
6374 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006375}
6376#endif /* HAVE_STATVFS */
6377
6378
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006379#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006380PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006381"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006382Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006383The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006384or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006385
6386static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006387posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006388{
6389 PyObject *result = NULL;
6390 char *dir = NULL;
6391 char *pfx = NULL;
6392 char *name;
6393
6394 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6395 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006396
6397 if (PyErr_Warn(PyExc_RuntimeWarning,
6398 "tempnam is a potential security risk to your program") < 0)
6399 return NULL;
6400
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006401#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006402 name = _tempnam(dir, pfx);
6403#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006404 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006405#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006406 if (name == NULL)
6407 return PyErr_NoMemory();
6408 result = PyString_FromString(name);
6409 free(name);
6410 return result;
6411}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006412#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006413
6414
6415#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006416PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006417"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006418Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006419
6420static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006421posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006422{
6423 FILE *fp;
6424
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006425 fp = tmpfile();
6426 if (fp == NULL)
6427 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006428 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006429}
6430#endif
6431
6432
6433#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006435"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006436Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006437
6438static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006439posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006440{
6441 char buffer[L_tmpnam];
6442 char *name;
6443
Skip Montanaro95618b52001-08-18 18:52:10 +00006444 if (PyErr_Warn(PyExc_RuntimeWarning,
6445 "tmpnam is a potential security risk to your program") < 0)
6446 return NULL;
6447
Greg Wardb48bc172000-03-01 21:51:56 +00006448#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006449 name = tmpnam_r(buffer);
6450#else
6451 name = tmpnam(buffer);
6452#endif
6453 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006454 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006455#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006456 "unexpected NULL from tmpnam_r"
6457#else
6458 "unexpected NULL from tmpnam"
6459#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006460 );
6461 PyErr_SetObject(PyExc_OSError, err);
6462 Py_XDECREF(err);
6463 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006464 }
6465 return PyString_FromString(buffer);
6466}
6467#endif
6468
6469
Fred Drakec9680921999-12-13 16:37:25 +00006470/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6471 * It maps strings representing configuration variable names to
6472 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006473 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006474 * rarely-used constants. There are three separate tables that use
6475 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006476 *
6477 * This code is always included, even if none of the interfaces that
6478 * need it are included. The #if hackery needed to avoid it would be
6479 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006480 */
6481struct constdef {
6482 char *name;
6483 long value;
6484};
6485
Fred Drake12c6e2d1999-12-14 21:25:03 +00006486static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006487conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6488 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006489{
6490 if (PyInt_Check(arg)) {
6491 *valuep = PyInt_AS_LONG(arg);
6492 return 1;
6493 }
6494 if (PyString_Check(arg)) {
6495 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006496 size_t lo = 0;
6497 size_t mid;
6498 size_t hi = tablesize;
6499 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006500 char *confname = PyString_AS_STRING(arg);
6501 while (lo < hi) {
6502 mid = (lo + hi) / 2;
6503 cmp = strcmp(confname, table[mid].name);
6504 if (cmp < 0)
6505 hi = mid;
6506 else if (cmp > 0)
6507 lo = mid + 1;
6508 else {
6509 *valuep = table[mid].value;
6510 return 1;
6511 }
6512 }
6513 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6514 }
6515 else
6516 PyErr_SetString(PyExc_TypeError,
6517 "configuration names must be strings or integers");
6518 return 0;
6519}
6520
6521
6522#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6523static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006524#ifdef _PC_ABI_AIO_XFER_MAX
6525 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6526#endif
6527#ifdef _PC_ABI_ASYNC_IO
6528 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6529#endif
Fred Drakec9680921999-12-13 16:37:25 +00006530#ifdef _PC_ASYNC_IO
6531 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6532#endif
6533#ifdef _PC_CHOWN_RESTRICTED
6534 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6535#endif
6536#ifdef _PC_FILESIZEBITS
6537 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6538#endif
6539#ifdef _PC_LAST
6540 {"PC_LAST", _PC_LAST},
6541#endif
6542#ifdef _PC_LINK_MAX
6543 {"PC_LINK_MAX", _PC_LINK_MAX},
6544#endif
6545#ifdef _PC_MAX_CANON
6546 {"PC_MAX_CANON", _PC_MAX_CANON},
6547#endif
6548#ifdef _PC_MAX_INPUT
6549 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6550#endif
6551#ifdef _PC_NAME_MAX
6552 {"PC_NAME_MAX", _PC_NAME_MAX},
6553#endif
6554#ifdef _PC_NO_TRUNC
6555 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6556#endif
6557#ifdef _PC_PATH_MAX
6558 {"PC_PATH_MAX", _PC_PATH_MAX},
6559#endif
6560#ifdef _PC_PIPE_BUF
6561 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6562#endif
6563#ifdef _PC_PRIO_IO
6564 {"PC_PRIO_IO", _PC_PRIO_IO},
6565#endif
6566#ifdef _PC_SOCK_MAXBUF
6567 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6568#endif
6569#ifdef _PC_SYNC_IO
6570 {"PC_SYNC_IO", _PC_SYNC_IO},
6571#endif
6572#ifdef _PC_VDISABLE
6573 {"PC_VDISABLE", _PC_VDISABLE},
6574#endif
6575};
6576
Fred Drakec9680921999-12-13 16:37:25 +00006577static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006578conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006579{
6580 return conv_confname(arg, valuep, posix_constants_pathconf,
6581 sizeof(posix_constants_pathconf)
6582 / sizeof(struct constdef));
6583}
6584#endif
6585
6586#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006587PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006588"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006589Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006590If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006591
6592static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006593posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006594{
6595 PyObject *result = NULL;
6596 int name, fd;
6597
Fred Drake12c6e2d1999-12-14 21:25:03 +00006598 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6599 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006600 long limit;
6601
6602 errno = 0;
6603 limit = fpathconf(fd, name);
6604 if (limit == -1 && errno != 0)
6605 posix_error();
6606 else
6607 result = PyInt_FromLong(limit);
6608 }
6609 return result;
6610}
6611#endif
6612
6613
6614#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006615PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006616"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006617Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006618If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006619
6620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006621posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006622{
6623 PyObject *result = NULL;
6624 int name;
6625 char *path;
6626
6627 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6628 conv_path_confname, &name)) {
6629 long limit;
6630
6631 errno = 0;
6632 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006633 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006634 if (errno == EINVAL)
6635 /* could be a path or name problem */
6636 posix_error();
6637 else
6638 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006639 }
Fred Drakec9680921999-12-13 16:37:25 +00006640 else
6641 result = PyInt_FromLong(limit);
6642 }
6643 return result;
6644}
6645#endif
6646
6647#ifdef HAVE_CONFSTR
6648static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006649#ifdef _CS_ARCHITECTURE
6650 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6651#endif
6652#ifdef _CS_HOSTNAME
6653 {"CS_HOSTNAME", _CS_HOSTNAME},
6654#endif
6655#ifdef _CS_HW_PROVIDER
6656 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6657#endif
6658#ifdef _CS_HW_SERIAL
6659 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6660#endif
6661#ifdef _CS_INITTAB_NAME
6662 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6663#endif
Fred Drakec9680921999-12-13 16:37:25 +00006664#ifdef _CS_LFS64_CFLAGS
6665 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6666#endif
6667#ifdef _CS_LFS64_LDFLAGS
6668 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6669#endif
6670#ifdef _CS_LFS64_LIBS
6671 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6672#endif
6673#ifdef _CS_LFS64_LINTFLAGS
6674 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6675#endif
6676#ifdef _CS_LFS_CFLAGS
6677 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6678#endif
6679#ifdef _CS_LFS_LDFLAGS
6680 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6681#endif
6682#ifdef _CS_LFS_LIBS
6683 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6684#endif
6685#ifdef _CS_LFS_LINTFLAGS
6686 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6687#endif
Fred Draked86ed291999-12-15 15:34:33 +00006688#ifdef _CS_MACHINE
6689 {"CS_MACHINE", _CS_MACHINE},
6690#endif
Fred Drakec9680921999-12-13 16:37:25 +00006691#ifdef _CS_PATH
6692 {"CS_PATH", _CS_PATH},
6693#endif
Fred Draked86ed291999-12-15 15:34:33 +00006694#ifdef _CS_RELEASE
6695 {"CS_RELEASE", _CS_RELEASE},
6696#endif
6697#ifdef _CS_SRPC_DOMAIN
6698 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6699#endif
6700#ifdef _CS_SYSNAME
6701 {"CS_SYSNAME", _CS_SYSNAME},
6702#endif
6703#ifdef _CS_VERSION
6704 {"CS_VERSION", _CS_VERSION},
6705#endif
Fred Drakec9680921999-12-13 16:37:25 +00006706#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6707 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6708#endif
6709#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6710 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6711#endif
6712#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6713 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6714#endif
6715#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6716 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6717#endif
6718#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6719 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6720#endif
6721#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6722 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6723#endif
6724#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6725 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6726#endif
6727#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6728 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6729#endif
6730#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6731 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6732#endif
6733#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6734 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6735#endif
6736#ifdef _CS_XBS5_LP64_OFF64_LIBS
6737 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6738#endif
6739#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6740 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6741#endif
6742#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6743 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6744#endif
6745#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6746 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6747#endif
6748#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6749 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6750#endif
6751#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6752 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6753#endif
Fred Draked86ed291999-12-15 15:34:33 +00006754#ifdef _MIPS_CS_AVAIL_PROCESSORS
6755 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6756#endif
6757#ifdef _MIPS_CS_BASE
6758 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6759#endif
6760#ifdef _MIPS_CS_HOSTID
6761 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6762#endif
6763#ifdef _MIPS_CS_HW_NAME
6764 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6765#endif
6766#ifdef _MIPS_CS_NUM_PROCESSORS
6767 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6768#endif
6769#ifdef _MIPS_CS_OSREL_MAJ
6770 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6771#endif
6772#ifdef _MIPS_CS_OSREL_MIN
6773 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6774#endif
6775#ifdef _MIPS_CS_OSREL_PATCH
6776 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6777#endif
6778#ifdef _MIPS_CS_OS_NAME
6779 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6780#endif
6781#ifdef _MIPS_CS_OS_PROVIDER
6782 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6783#endif
6784#ifdef _MIPS_CS_PROCESSORS
6785 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6786#endif
6787#ifdef _MIPS_CS_SERIAL
6788 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6789#endif
6790#ifdef _MIPS_CS_VENDOR
6791 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6792#endif
Fred Drakec9680921999-12-13 16:37:25 +00006793};
6794
6795static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006796conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006797{
6798 return conv_confname(arg, valuep, posix_constants_confstr,
6799 sizeof(posix_constants_confstr)
6800 / sizeof(struct constdef));
6801}
6802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006803PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006804"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006805Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006806
6807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006808posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006809{
6810 PyObject *result = NULL;
6811 int name;
6812 char buffer[64];
6813
6814 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6815 int len = confstr(name, buffer, sizeof(buffer));
6816
Fred Drakec9680921999-12-13 16:37:25 +00006817 errno = 0;
6818 if (len == 0) {
6819 if (errno != 0)
6820 posix_error();
6821 else
6822 result = PyString_FromString("");
6823 }
6824 else {
6825 if (len >= sizeof(buffer)) {
6826 result = PyString_FromStringAndSize(NULL, len);
6827 if (result != NULL)
6828 confstr(name, PyString_AS_STRING(result), len+1);
6829 }
6830 else
6831 result = PyString_FromString(buffer);
6832 }
6833 }
6834 return result;
6835}
6836#endif
6837
6838
6839#ifdef HAVE_SYSCONF
6840static struct constdef posix_constants_sysconf[] = {
6841#ifdef _SC_2_CHAR_TERM
6842 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6843#endif
6844#ifdef _SC_2_C_BIND
6845 {"SC_2_C_BIND", _SC_2_C_BIND},
6846#endif
6847#ifdef _SC_2_C_DEV
6848 {"SC_2_C_DEV", _SC_2_C_DEV},
6849#endif
6850#ifdef _SC_2_C_VERSION
6851 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6852#endif
6853#ifdef _SC_2_FORT_DEV
6854 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6855#endif
6856#ifdef _SC_2_FORT_RUN
6857 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6858#endif
6859#ifdef _SC_2_LOCALEDEF
6860 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6861#endif
6862#ifdef _SC_2_SW_DEV
6863 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6864#endif
6865#ifdef _SC_2_UPE
6866 {"SC_2_UPE", _SC_2_UPE},
6867#endif
6868#ifdef _SC_2_VERSION
6869 {"SC_2_VERSION", _SC_2_VERSION},
6870#endif
Fred Draked86ed291999-12-15 15:34:33 +00006871#ifdef _SC_ABI_ASYNCHRONOUS_IO
6872 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6873#endif
6874#ifdef _SC_ACL
6875 {"SC_ACL", _SC_ACL},
6876#endif
Fred Drakec9680921999-12-13 16:37:25 +00006877#ifdef _SC_AIO_LISTIO_MAX
6878 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6879#endif
Fred Drakec9680921999-12-13 16:37:25 +00006880#ifdef _SC_AIO_MAX
6881 {"SC_AIO_MAX", _SC_AIO_MAX},
6882#endif
6883#ifdef _SC_AIO_PRIO_DELTA_MAX
6884 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6885#endif
6886#ifdef _SC_ARG_MAX
6887 {"SC_ARG_MAX", _SC_ARG_MAX},
6888#endif
6889#ifdef _SC_ASYNCHRONOUS_IO
6890 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6891#endif
6892#ifdef _SC_ATEXIT_MAX
6893 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6894#endif
Fred Draked86ed291999-12-15 15:34:33 +00006895#ifdef _SC_AUDIT
6896 {"SC_AUDIT", _SC_AUDIT},
6897#endif
Fred Drakec9680921999-12-13 16:37:25 +00006898#ifdef _SC_AVPHYS_PAGES
6899 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6900#endif
6901#ifdef _SC_BC_BASE_MAX
6902 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6903#endif
6904#ifdef _SC_BC_DIM_MAX
6905 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6906#endif
6907#ifdef _SC_BC_SCALE_MAX
6908 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6909#endif
6910#ifdef _SC_BC_STRING_MAX
6911 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6912#endif
Fred Draked86ed291999-12-15 15:34:33 +00006913#ifdef _SC_CAP
6914 {"SC_CAP", _SC_CAP},
6915#endif
Fred Drakec9680921999-12-13 16:37:25 +00006916#ifdef _SC_CHARCLASS_NAME_MAX
6917 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6918#endif
6919#ifdef _SC_CHAR_BIT
6920 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6921#endif
6922#ifdef _SC_CHAR_MAX
6923 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6924#endif
6925#ifdef _SC_CHAR_MIN
6926 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6927#endif
6928#ifdef _SC_CHILD_MAX
6929 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6930#endif
6931#ifdef _SC_CLK_TCK
6932 {"SC_CLK_TCK", _SC_CLK_TCK},
6933#endif
6934#ifdef _SC_COHER_BLKSZ
6935 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6936#endif
6937#ifdef _SC_COLL_WEIGHTS_MAX
6938 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6939#endif
6940#ifdef _SC_DCACHE_ASSOC
6941 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6942#endif
6943#ifdef _SC_DCACHE_BLKSZ
6944 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6945#endif
6946#ifdef _SC_DCACHE_LINESZ
6947 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6948#endif
6949#ifdef _SC_DCACHE_SZ
6950 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6951#endif
6952#ifdef _SC_DCACHE_TBLKSZ
6953 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6954#endif
6955#ifdef _SC_DELAYTIMER_MAX
6956 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6957#endif
6958#ifdef _SC_EQUIV_CLASS_MAX
6959 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6960#endif
6961#ifdef _SC_EXPR_NEST_MAX
6962 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6963#endif
6964#ifdef _SC_FSYNC
6965 {"SC_FSYNC", _SC_FSYNC},
6966#endif
6967#ifdef _SC_GETGR_R_SIZE_MAX
6968 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6969#endif
6970#ifdef _SC_GETPW_R_SIZE_MAX
6971 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6972#endif
6973#ifdef _SC_ICACHE_ASSOC
6974 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6975#endif
6976#ifdef _SC_ICACHE_BLKSZ
6977 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6978#endif
6979#ifdef _SC_ICACHE_LINESZ
6980 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6981#endif
6982#ifdef _SC_ICACHE_SZ
6983 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6984#endif
Fred Draked86ed291999-12-15 15:34:33 +00006985#ifdef _SC_INF
6986 {"SC_INF", _SC_INF},
6987#endif
Fred Drakec9680921999-12-13 16:37:25 +00006988#ifdef _SC_INT_MAX
6989 {"SC_INT_MAX", _SC_INT_MAX},
6990#endif
6991#ifdef _SC_INT_MIN
6992 {"SC_INT_MIN", _SC_INT_MIN},
6993#endif
6994#ifdef _SC_IOV_MAX
6995 {"SC_IOV_MAX", _SC_IOV_MAX},
6996#endif
Fred Draked86ed291999-12-15 15:34:33 +00006997#ifdef _SC_IP_SECOPTS
6998 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6999#endif
Fred Drakec9680921999-12-13 16:37:25 +00007000#ifdef _SC_JOB_CONTROL
7001 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7002#endif
Fred Draked86ed291999-12-15 15:34:33 +00007003#ifdef _SC_KERN_POINTERS
7004 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7005#endif
7006#ifdef _SC_KERN_SIM
7007 {"SC_KERN_SIM", _SC_KERN_SIM},
7008#endif
Fred Drakec9680921999-12-13 16:37:25 +00007009#ifdef _SC_LINE_MAX
7010 {"SC_LINE_MAX", _SC_LINE_MAX},
7011#endif
7012#ifdef _SC_LOGIN_NAME_MAX
7013 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7014#endif
7015#ifdef _SC_LOGNAME_MAX
7016 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7017#endif
7018#ifdef _SC_LONG_BIT
7019 {"SC_LONG_BIT", _SC_LONG_BIT},
7020#endif
Fred Draked86ed291999-12-15 15:34:33 +00007021#ifdef _SC_MAC
7022 {"SC_MAC", _SC_MAC},
7023#endif
Fred Drakec9680921999-12-13 16:37:25 +00007024#ifdef _SC_MAPPED_FILES
7025 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7026#endif
7027#ifdef _SC_MAXPID
7028 {"SC_MAXPID", _SC_MAXPID},
7029#endif
7030#ifdef _SC_MB_LEN_MAX
7031 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7032#endif
7033#ifdef _SC_MEMLOCK
7034 {"SC_MEMLOCK", _SC_MEMLOCK},
7035#endif
7036#ifdef _SC_MEMLOCK_RANGE
7037 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7038#endif
7039#ifdef _SC_MEMORY_PROTECTION
7040 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7041#endif
7042#ifdef _SC_MESSAGE_PASSING
7043 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7044#endif
Fred Draked86ed291999-12-15 15:34:33 +00007045#ifdef _SC_MMAP_FIXED_ALIGNMENT
7046 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7047#endif
Fred Drakec9680921999-12-13 16:37:25 +00007048#ifdef _SC_MQ_OPEN_MAX
7049 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7050#endif
7051#ifdef _SC_MQ_PRIO_MAX
7052 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7053#endif
Fred Draked86ed291999-12-15 15:34:33 +00007054#ifdef _SC_NACLS_MAX
7055 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7056#endif
Fred Drakec9680921999-12-13 16:37:25 +00007057#ifdef _SC_NGROUPS_MAX
7058 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7059#endif
7060#ifdef _SC_NL_ARGMAX
7061 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7062#endif
7063#ifdef _SC_NL_LANGMAX
7064 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7065#endif
7066#ifdef _SC_NL_MSGMAX
7067 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7068#endif
7069#ifdef _SC_NL_NMAX
7070 {"SC_NL_NMAX", _SC_NL_NMAX},
7071#endif
7072#ifdef _SC_NL_SETMAX
7073 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7074#endif
7075#ifdef _SC_NL_TEXTMAX
7076 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7077#endif
7078#ifdef _SC_NPROCESSORS_CONF
7079 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7080#endif
7081#ifdef _SC_NPROCESSORS_ONLN
7082 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7083#endif
Fred Draked86ed291999-12-15 15:34:33 +00007084#ifdef _SC_NPROC_CONF
7085 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7086#endif
7087#ifdef _SC_NPROC_ONLN
7088 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7089#endif
Fred Drakec9680921999-12-13 16:37:25 +00007090#ifdef _SC_NZERO
7091 {"SC_NZERO", _SC_NZERO},
7092#endif
7093#ifdef _SC_OPEN_MAX
7094 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7095#endif
7096#ifdef _SC_PAGESIZE
7097 {"SC_PAGESIZE", _SC_PAGESIZE},
7098#endif
7099#ifdef _SC_PAGE_SIZE
7100 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7101#endif
7102#ifdef _SC_PASS_MAX
7103 {"SC_PASS_MAX", _SC_PASS_MAX},
7104#endif
7105#ifdef _SC_PHYS_PAGES
7106 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7107#endif
7108#ifdef _SC_PII
7109 {"SC_PII", _SC_PII},
7110#endif
7111#ifdef _SC_PII_INTERNET
7112 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7113#endif
7114#ifdef _SC_PII_INTERNET_DGRAM
7115 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7116#endif
7117#ifdef _SC_PII_INTERNET_STREAM
7118 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7119#endif
7120#ifdef _SC_PII_OSI
7121 {"SC_PII_OSI", _SC_PII_OSI},
7122#endif
7123#ifdef _SC_PII_OSI_CLTS
7124 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7125#endif
7126#ifdef _SC_PII_OSI_COTS
7127 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7128#endif
7129#ifdef _SC_PII_OSI_M
7130 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7131#endif
7132#ifdef _SC_PII_SOCKET
7133 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7134#endif
7135#ifdef _SC_PII_XTI
7136 {"SC_PII_XTI", _SC_PII_XTI},
7137#endif
7138#ifdef _SC_POLL
7139 {"SC_POLL", _SC_POLL},
7140#endif
7141#ifdef _SC_PRIORITIZED_IO
7142 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7143#endif
7144#ifdef _SC_PRIORITY_SCHEDULING
7145 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7146#endif
7147#ifdef _SC_REALTIME_SIGNALS
7148 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7149#endif
7150#ifdef _SC_RE_DUP_MAX
7151 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7152#endif
7153#ifdef _SC_RTSIG_MAX
7154 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7155#endif
7156#ifdef _SC_SAVED_IDS
7157 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7158#endif
7159#ifdef _SC_SCHAR_MAX
7160 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7161#endif
7162#ifdef _SC_SCHAR_MIN
7163 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7164#endif
7165#ifdef _SC_SELECT
7166 {"SC_SELECT", _SC_SELECT},
7167#endif
7168#ifdef _SC_SEMAPHORES
7169 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7170#endif
7171#ifdef _SC_SEM_NSEMS_MAX
7172 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7173#endif
7174#ifdef _SC_SEM_VALUE_MAX
7175 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7176#endif
7177#ifdef _SC_SHARED_MEMORY_OBJECTS
7178 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7179#endif
7180#ifdef _SC_SHRT_MAX
7181 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7182#endif
7183#ifdef _SC_SHRT_MIN
7184 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7185#endif
7186#ifdef _SC_SIGQUEUE_MAX
7187 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7188#endif
7189#ifdef _SC_SIGRT_MAX
7190 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7191#endif
7192#ifdef _SC_SIGRT_MIN
7193 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7194#endif
Fred Draked86ed291999-12-15 15:34:33 +00007195#ifdef _SC_SOFTPOWER
7196 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7197#endif
Fred Drakec9680921999-12-13 16:37:25 +00007198#ifdef _SC_SPLIT_CACHE
7199 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7200#endif
7201#ifdef _SC_SSIZE_MAX
7202 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7203#endif
7204#ifdef _SC_STACK_PROT
7205 {"SC_STACK_PROT", _SC_STACK_PROT},
7206#endif
7207#ifdef _SC_STREAM_MAX
7208 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7209#endif
7210#ifdef _SC_SYNCHRONIZED_IO
7211 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7212#endif
7213#ifdef _SC_THREADS
7214 {"SC_THREADS", _SC_THREADS},
7215#endif
7216#ifdef _SC_THREAD_ATTR_STACKADDR
7217 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7218#endif
7219#ifdef _SC_THREAD_ATTR_STACKSIZE
7220 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7221#endif
7222#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7223 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7224#endif
7225#ifdef _SC_THREAD_KEYS_MAX
7226 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7227#endif
7228#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7229 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7230#endif
7231#ifdef _SC_THREAD_PRIO_INHERIT
7232 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7233#endif
7234#ifdef _SC_THREAD_PRIO_PROTECT
7235 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7236#endif
7237#ifdef _SC_THREAD_PROCESS_SHARED
7238 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7239#endif
7240#ifdef _SC_THREAD_SAFE_FUNCTIONS
7241 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7242#endif
7243#ifdef _SC_THREAD_STACK_MIN
7244 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7245#endif
7246#ifdef _SC_THREAD_THREADS_MAX
7247 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7248#endif
7249#ifdef _SC_TIMERS
7250 {"SC_TIMERS", _SC_TIMERS},
7251#endif
7252#ifdef _SC_TIMER_MAX
7253 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7254#endif
7255#ifdef _SC_TTY_NAME_MAX
7256 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7257#endif
7258#ifdef _SC_TZNAME_MAX
7259 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7260#endif
7261#ifdef _SC_T_IOV_MAX
7262 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7263#endif
7264#ifdef _SC_UCHAR_MAX
7265 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7266#endif
7267#ifdef _SC_UINT_MAX
7268 {"SC_UINT_MAX", _SC_UINT_MAX},
7269#endif
7270#ifdef _SC_UIO_MAXIOV
7271 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7272#endif
7273#ifdef _SC_ULONG_MAX
7274 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7275#endif
7276#ifdef _SC_USHRT_MAX
7277 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7278#endif
7279#ifdef _SC_VERSION
7280 {"SC_VERSION", _SC_VERSION},
7281#endif
7282#ifdef _SC_WORD_BIT
7283 {"SC_WORD_BIT", _SC_WORD_BIT},
7284#endif
7285#ifdef _SC_XBS5_ILP32_OFF32
7286 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7287#endif
7288#ifdef _SC_XBS5_ILP32_OFFBIG
7289 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7290#endif
7291#ifdef _SC_XBS5_LP64_OFF64
7292 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7293#endif
7294#ifdef _SC_XBS5_LPBIG_OFFBIG
7295 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7296#endif
7297#ifdef _SC_XOPEN_CRYPT
7298 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7299#endif
7300#ifdef _SC_XOPEN_ENH_I18N
7301 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7302#endif
7303#ifdef _SC_XOPEN_LEGACY
7304 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7305#endif
7306#ifdef _SC_XOPEN_REALTIME
7307 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7308#endif
7309#ifdef _SC_XOPEN_REALTIME_THREADS
7310 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7311#endif
7312#ifdef _SC_XOPEN_SHM
7313 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7314#endif
7315#ifdef _SC_XOPEN_UNIX
7316 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7317#endif
7318#ifdef _SC_XOPEN_VERSION
7319 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7320#endif
7321#ifdef _SC_XOPEN_XCU_VERSION
7322 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7323#endif
7324#ifdef _SC_XOPEN_XPG2
7325 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7326#endif
7327#ifdef _SC_XOPEN_XPG3
7328 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7329#endif
7330#ifdef _SC_XOPEN_XPG4
7331 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7332#endif
7333};
7334
7335static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007336conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007337{
7338 return conv_confname(arg, valuep, posix_constants_sysconf,
7339 sizeof(posix_constants_sysconf)
7340 / sizeof(struct constdef));
7341}
7342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007343PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007344"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007345Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007346
7347static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007348posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007349{
7350 PyObject *result = NULL;
7351 int name;
7352
7353 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7354 int value;
7355
7356 errno = 0;
7357 value = sysconf(name);
7358 if (value == -1 && errno != 0)
7359 posix_error();
7360 else
7361 result = PyInt_FromLong(value);
7362 }
7363 return result;
7364}
7365#endif
7366
7367
Fred Drakebec628d1999-12-15 18:31:10 +00007368/* This code is used to ensure that the tables of configuration value names
7369 * are in sorted order as required by conv_confname(), and also to build the
7370 * the exported dictionaries that are used to publish information about the
7371 * names available on the host platform.
7372 *
7373 * Sorting the table at runtime ensures that the table is properly ordered
7374 * when used, even for platforms we're not able to test on. It also makes
7375 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007376 */
Fred Drakebec628d1999-12-15 18:31:10 +00007377
7378static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007379cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007380{
7381 const struct constdef *c1 =
7382 (const struct constdef *) v1;
7383 const struct constdef *c2 =
7384 (const struct constdef *) v2;
7385
7386 return strcmp(c1->name, c2->name);
7387}
7388
7389static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007390setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007391 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007392{
Fred Drakebec628d1999-12-15 18:31:10 +00007393 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007394 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007395
7396 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7397 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007398 if (d == NULL)
7399 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007400
Barry Warsaw3155db32000-04-13 15:20:40 +00007401 for (i=0; i < tablesize; ++i) {
7402 PyObject *o = PyInt_FromLong(table[i].value);
7403 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7404 Py_XDECREF(o);
7405 Py_DECREF(d);
7406 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007407 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007408 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007409 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007410 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007411}
7412
Fred Drakebec628d1999-12-15 18:31:10 +00007413/* Return -1 on failure, 0 on success. */
7414static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007415setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007416{
7417#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007418 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007419 sizeof(posix_constants_pathconf)
7420 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007421 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007422 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007423#endif
7424#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007425 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007426 sizeof(posix_constants_confstr)
7427 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007428 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007429 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007430#endif
7431#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007432 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007433 sizeof(posix_constants_sysconf)
7434 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007435 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007436 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007437#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007438 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007439}
Fred Draked86ed291999-12-15 15:34:33 +00007440
7441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007442PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007443"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007444Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007445in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007446
7447static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007448posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007449{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007450 abort();
7451 /*NOTREACHED*/
7452 Py_FatalError("abort() called from Python code didn't abort!");
7453 return NULL;
7454}
Fred Drakebec628d1999-12-15 18:31:10 +00007455
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007456#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007457PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007458"startfile(filepath [, operation]) - Start a file with its associated\n\
7459application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007460\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007461When \"operation\" is not specified or \"open\", this acts like\n\
7462double-clicking the file in Explorer, or giving the file name as an\n\
7463argument to the DOS \"start\" command: the file is opened with whatever\n\
7464application (if any) its extension is associated.\n\
7465When another \"operation\" is given, it specifies what should be done with\n\
7466the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007467\n\
7468startfile returns as soon as the associated application is launched.\n\
7469There is no option to wait for the application to close, and no way\n\
7470to retrieve the application's exit status.\n\
7471\n\
7472The filepath is relative to the current directory. If you want to use\n\
7473an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007474the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007475
7476static PyObject *
7477win32_startfile(PyObject *self, PyObject *args)
7478{
7479 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007480 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007481 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00007482#ifdef Py_WIN_WIDE_FILENAMES
7483 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007484 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00007485 if (!PyArg_ParseTuple(args, "U|s:startfile",
7486 &unipath, &operation)) {
7487 PyErr_Clear();
7488 goto normal;
7489 }
7490
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007491
7492 if (operation) {
7493 woperation = PyUnicode_DecodeASCII(operation,
7494 strlen(operation), NULL);
7495 if (!woperation) {
7496 PyErr_Clear();
7497 operation = NULL;
7498 goto normal;
7499 }
Georg Brandlad89dc82006-04-03 12:26:26 +00007500 }
7501
7502 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007503 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00007504 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00007505 NULL, NULL, SW_SHOWNORMAL);
7506 Py_END_ALLOW_THREADS
7507
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007508 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00007509 if (rc <= (HINSTANCE)32) {
7510 PyObject *errval = win32_error_unicode("startfile",
7511 PyUnicode_AS_UNICODE(unipath));
7512 return errval;
7513 }
7514 Py_INCREF(Py_None);
7515 return Py_None;
7516 }
7517#endif
7518
7519normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007520 if (!PyArg_ParseTuple(args, "et|s:startfile",
7521 Py_FileSystemDefaultEncoding, &filepath,
7522 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007523 return NULL;
7524 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007525 rc = ShellExecute((HWND)0, operation, filepath,
7526 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007527 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007528 if (rc <= (HINSTANCE)32) {
7529 PyObject *errval = win32_error("startfile", filepath);
7530 PyMem_Free(filepath);
7531 return errval;
7532 }
7533 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007534 Py_INCREF(Py_None);
7535 return Py_None;
7536}
7537#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007538
Martin v. Löwis438b5342002-12-27 10:16:42 +00007539#ifdef HAVE_GETLOADAVG
7540PyDoc_STRVAR(posix_getloadavg__doc__,
7541"getloadavg() -> (float, float, float)\n\n\
7542Return the number of processes in the system run queue averaged over\n\
7543the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7544was unobtainable");
7545
7546static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007547posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007548{
7549 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007550 if (getloadavg(loadavg, 3)!=3) {
7551 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7552 return NULL;
7553 } else
7554 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7555}
7556#endif
7557
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007558#ifdef MS_WINDOWS
7559
7560PyDoc_STRVAR(win32_urandom__doc__,
7561"urandom(n) -> str\n\n\
7562Return a string of n random bytes suitable for cryptographic use.");
7563
7564typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7565 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7566 DWORD dwFlags );
7567typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7568 BYTE *pbBuffer );
7569
7570static CRYPTGENRANDOM pCryptGenRandom = NULL;
7571static HCRYPTPROV hCryptProv = 0;
7572
Tim Peters4ad82172004-08-30 17:02:04 +00007573static PyObject*
7574win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007575{
Tim Petersd3115382004-08-30 17:36:46 +00007576 int howMany;
7577 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007578
Tim Peters4ad82172004-08-30 17:02:04 +00007579 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007580 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007581 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007582 if (howMany < 0)
7583 return PyErr_Format(PyExc_ValueError,
7584 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007585
Tim Peters4ad82172004-08-30 17:02:04 +00007586 if (hCryptProv == 0) {
7587 HINSTANCE hAdvAPI32 = NULL;
7588 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007589
Tim Peters4ad82172004-08-30 17:02:04 +00007590 /* Obtain handle to the DLL containing CryptoAPI
7591 This should not fail */
7592 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7593 if(hAdvAPI32 == NULL)
7594 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007595
Tim Peters4ad82172004-08-30 17:02:04 +00007596 /* Obtain pointers to the CryptoAPI functions
7597 This will fail on some early versions of Win95 */
7598 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7599 hAdvAPI32,
7600 "CryptAcquireContextA");
7601 if (pCryptAcquireContext == NULL)
7602 return PyErr_Format(PyExc_NotImplementedError,
7603 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007604
Tim Peters4ad82172004-08-30 17:02:04 +00007605 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7606 hAdvAPI32, "CryptGenRandom");
7607 if (pCryptAcquireContext == NULL)
7608 return PyErr_Format(PyExc_NotImplementedError,
7609 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007610
Tim Peters4ad82172004-08-30 17:02:04 +00007611 /* Acquire context */
7612 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7613 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7614 return win32_error("CryptAcquireContext", NULL);
7615 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007616
Tim Peters4ad82172004-08-30 17:02:04 +00007617 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00007618 result = PyString_FromStringAndSize(NULL, howMany);
7619 if (result != NULL) {
7620 /* Get random data */
7621 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7622 PyString_AS_STRING(result))) {
7623 Py_DECREF(result);
7624 return win32_error("CryptGenRandom", NULL);
7625 }
Tim Peters4ad82172004-08-30 17:02:04 +00007626 }
Tim Petersd3115382004-08-30 17:36:46 +00007627 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007628}
7629#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007630
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007631static PyMethodDef posix_methods[] = {
7632 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7633#ifdef HAVE_TTYNAME
7634 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7635#endif
7636 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7637 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007638#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007639 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007640#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007641#ifdef HAVE_LCHOWN
7642 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7643#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007644#ifdef HAVE_CHROOT
7645 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7646#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007647#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007648 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007649#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007650#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007651 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007652#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007653 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007654#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007655#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007656#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007657 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007658#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007659 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7660 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7661 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007662#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007663 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007664#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007665#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007666 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007667#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007668 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7669 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7670 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007671 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007672#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007673 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007674#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007675#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007676 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007677#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007678 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007679#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007680 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007681#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007682 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7683 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7684 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007685#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007686 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007687#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007688 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007689#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007690 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7691 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007692#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007693#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007694 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7695 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007696#if defined(PYOS_OS2)
7697 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7698 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7699#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007700#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007701#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007702 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007703#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007704#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007705 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007706#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007707#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007708 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007709#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007710#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007711 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007712#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007713#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007714 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007715#endif /* HAVE_GETEGID */
7716#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007717 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007718#endif /* HAVE_GETEUID */
7719#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007720 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007721#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007722#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007723 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007724#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007725 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007726#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007727 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007728#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007729#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007730 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007731#endif /* HAVE_GETPPID */
7732#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007733 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007734#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007735#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007736 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007737#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007738#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007739 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007740#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007741#ifdef HAVE_KILLPG
7742 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7743#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007744#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007745 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007746#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007747#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007748 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007749#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007750 {"popen2", win32_popen2, METH_VARARGS},
7751 {"popen3", win32_popen3, METH_VARARGS},
7752 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007753 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007754#else
7755#if defined(PYOS_OS2) && defined(PYCC_GCC)
7756 {"popen2", os2emx_popen2, METH_VARARGS},
7757 {"popen3", os2emx_popen3, METH_VARARGS},
7758 {"popen4", os2emx_popen4, METH_VARARGS},
7759#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007760#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007761#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007762#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007763 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007764#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007765#ifdef HAVE_SETEUID
7766 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7767#endif /* HAVE_SETEUID */
7768#ifdef HAVE_SETEGID
7769 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7770#endif /* HAVE_SETEGID */
7771#ifdef HAVE_SETREUID
7772 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7773#endif /* HAVE_SETREUID */
7774#ifdef HAVE_SETREGID
7775 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7776#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007777#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007778 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007779#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007780#ifdef HAVE_SETGROUPS
7781 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7782#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007783#ifdef HAVE_GETPGID
7784 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7785#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007786#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007787 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007788#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007789#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007790 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007791#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00007792#ifdef HAVE_WAIT3
7793 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7794#endif /* HAVE_WAIT3 */
7795#ifdef HAVE_WAIT4
7796 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7797#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007798#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007799 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007800#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007801#ifdef HAVE_GETSID
7802 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7803#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007804#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007805 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007806#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007807#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007808 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007809#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007810#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007811 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007812#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007813#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007814 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007815#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007816 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7817 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7818 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7819 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7820 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7821 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7822 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7823 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7824 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007825 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007826#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007827 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007828#endif
7829#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007830 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007831#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007832#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007833 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7834#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007835#ifdef HAVE_DEVICE_MACROS
7836 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7837 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7838 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7839#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007840#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007841 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007842#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007843#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007844 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007845#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007846#ifdef HAVE_UNSETENV
7847 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7848#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007849#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007850 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007851#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007852#ifdef HAVE_FCHDIR
7853 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7854#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007855#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007856 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007857#endif
7858#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007859 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007860#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007861#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007862#ifdef WCOREDUMP
7863 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7864#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007865#ifdef WIFCONTINUED
7866 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7867#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007868#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007869 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007870#endif /* WIFSTOPPED */
7871#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007872 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007873#endif /* WIFSIGNALED */
7874#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007875 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007876#endif /* WIFEXITED */
7877#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007878 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007879#endif /* WEXITSTATUS */
7880#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007881 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007882#endif /* WTERMSIG */
7883#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007884 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007885#endif /* WSTOPSIG */
7886#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007887#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007888 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007889#endif
7890#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007891 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007892#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007893#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007894 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007895#endif
7896#ifdef HAVE_TEMPNAM
7897 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7898#endif
7899#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007900 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007901#endif
Fred Drakec9680921999-12-13 16:37:25 +00007902#ifdef HAVE_CONFSTR
7903 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7904#endif
7905#ifdef HAVE_SYSCONF
7906 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7907#endif
7908#ifdef HAVE_FPATHCONF
7909 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7910#endif
7911#ifdef HAVE_PATHCONF
7912 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7913#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007914 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007915#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007916 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7917#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007918#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007919 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007920#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007921 #ifdef MS_WINDOWS
7922 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7923 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007924 {NULL, NULL} /* Sentinel */
7925};
7926
7927
Barry Warsaw4a342091996-12-19 23:50:02 +00007928static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007929ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007930{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007931 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007932}
7933
Guido van Rossumd48f2521997-12-05 22:19:34 +00007934#if defined(PYOS_OS2)
7935/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007936static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007937{
7938 APIRET rc;
7939 ULONG values[QSV_MAX+1];
7940 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007941 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007942
7943 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007944 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007945 Py_END_ALLOW_THREADS
7946
7947 if (rc != NO_ERROR) {
7948 os2_error(rc);
7949 return -1;
7950 }
7951
Fred Drake4d1e64b2002-04-15 19:40:07 +00007952 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7953 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7954 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7955 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7956 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7957 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7958 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007959
7960 switch (values[QSV_VERSION_MINOR]) {
7961 case 0: ver = "2.00"; break;
7962 case 10: ver = "2.10"; break;
7963 case 11: ver = "2.11"; break;
7964 case 30: ver = "3.00"; break;
7965 case 40: ver = "4.00"; break;
7966 case 50: ver = "5.00"; break;
7967 default:
Tim Peters885d4572001-11-28 20:27:42 +00007968 PyOS_snprintf(tmp, sizeof(tmp),
7969 "%d-%d", values[QSV_VERSION_MAJOR],
7970 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007971 ver = &tmp[0];
7972 }
7973
7974 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007975 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007976 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007977
7978 /* Add Indicator of Which Drive was Used to Boot the System */
7979 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7980 tmp[1] = ':';
7981 tmp[2] = '\0';
7982
Fred Drake4d1e64b2002-04-15 19:40:07 +00007983 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007984}
7985#endif
7986
Barry Warsaw4a342091996-12-19 23:50:02 +00007987static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007988all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007989{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007990#ifdef F_OK
7991 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007992#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007993#ifdef R_OK
7994 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007995#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007996#ifdef W_OK
7997 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007998#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007999#ifdef X_OK
8000 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008001#endif
Fred Drakec9680921999-12-13 16:37:25 +00008002#ifdef NGROUPS_MAX
8003 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8004#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008005#ifdef TMP_MAX
8006 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8007#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008008#ifdef WCONTINUED
8009 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8010#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008011#ifdef WNOHANG
8012 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008013#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008014#ifdef WUNTRACED
8015 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8016#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008017#ifdef O_RDONLY
8018 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8019#endif
8020#ifdef O_WRONLY
8021 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8022#endif
8023#ifdef O_RDWR
8024 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8025#endif
8026#ifdef O_NDELAY
8027 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8028#endif
8029#ifdef O_NONBLOCK
8030 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8031#endif
8032#ifdef O_APPEND
8033 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8034#endif
8035#ifdef O_DSYNC
8036 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8037#endif
8038#ifdef O_RSYNC
8039 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8040#endif
8041#ifdef O_SYNC
8042 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8043#endif
8044#ifdef O_NOCTTY
8045 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8046#endif
8047#ifdef O_CREAT
8048 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8049#endif
8050#ifdef O_EXCL
8051 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8052#endif
8053#ifdef O_TRUNC
8054 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8055#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008056#ifdef O_BINARY
8057 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8058#endif
8059#ifdef O_TEXT
8060 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8061#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008062#ifdef O_LARGEFILE
8063 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8064#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008065#ifdef O_SHLOCK
8066 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8067#endif
8068#ifdef O_EXLOCK
8069 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8070#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008071
Tim Peters5aa91602002-01-30 05:46:57 +00008072/* MS Windows */
8073#ifdef O_NOINHERIT
8074 /* Don't inherit in child processes. */
8075 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8076#endif
8077#ifdef _O_SHORT_LIVED
8078 /* Optimize for short life (keep in memory). */
8079 /* MS forgot to define this one with a non-underscore form too. */
8080 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8081#endif
8082#ifdef O_TEMPORARY
8083 /* Automatically delete when last handle is closed. */
8084 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8085#endif
8086#ifdef O_RANDOM
8087 /* Optimize for random access. */
8088 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8089#endif
8090#ifdef O_SEQUENTIAL
8091 /* Optimize for sequential access. */
8092 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8093#endif
8094
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008095/* GNU extensions. */
8096#ifdef O_DIRECT
8097 /* Direct disk access. */
8098 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8099#endif
8100#ifdef O_DIRECTORY
8101 /* Must be a directory. */
8102 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8103#endif
8104#ifdef O_NOFOLLOW
8105 /* Do not follow links. */
8106 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8107#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008108
Barry Warsaw5676bd12003-01-07 20:57:09 +00008109 /* These come from sysexits.h */
8110#ifdef EX_OK
8111 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008112#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008113#ifdef EX_USAGE
8114 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008115#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008116#ifdef EX_DATAERR
8117 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008118#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008119#ifdef EX_NOINPUT
8120 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008121#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008122#ifdef EX_NOUSER
8123 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008124#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008125#ifdef EX_NOHOST
8126 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008127#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008128#ifdef EX_UNAVAILABLE
8129 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008130#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008131#ifdef EX_SOFTWARE
8132 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008133#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008134#ifdef EX_OSERR
8135 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008136#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008137#ifdef EX_OSFILE
8138 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008139#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008140#ifdef EX_CANTCREAT
8141 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008142#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008143#ifdef EX_IOERR
8144 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008145#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008146#ifdef EX_TEMPFAIL
8147 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008148#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008149#ifdef EX_PROTOCOL
8150 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008151#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008152#ifdef EX_NOPERM
8153 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008154#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008155#ifdef EX_CONFIG
8156 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008157#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008158#ifdef EX_NOTFOUND
8159 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008160#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008161
Guido van Rossum246bc171999-02-01 23:54:31 +00008162#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008163#if defined(PYOS_OS2) && defined(PYCC_GCC)
8164 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8165 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8166 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8167 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8168 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8169 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8170 if (ins(d, "P_PM", (long)P_PM)) return -1;
8171 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8172 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8173 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8174 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8175 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8176 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8177 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8178 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8179 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8180 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8181 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8182 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8183 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8184#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008185 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8186 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8187 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8188 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8189 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008190#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008191#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008192
Guido van Rossumd48f2521997-12-05 22:19:34 +00008193#if defined(PYOS_OS2)
8194 if (insertvalues(d)) return -1;
8195#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008196 return 0;
8197}
8198
8199
Tim Peters5aa91602002-01-30 05:46:57 +00008200#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008201#define INITFUNC initnt
8202#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008203
8204#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008205#define INITFUNC initos2
8206#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008207
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008208#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008209#define INITFUNC initposix
8210#define MODNAME "posix"
8211#endif
8212
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008213PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008214INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008215{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008216 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008217
Fred Drake4d1e64b2002-04-15 19:40:07 +00008218 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008219 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008220 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008221 if (m == NULL)
8222 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008223
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008224 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008225 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008226 Py_XINCREF(v);
8227 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008228 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008229 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008230
Fred Drake4d1e64b2002-04-15 19:40:07 +00008231 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008232 return;
8233
Fred Drake4d1e64b2002-04-15 19:40:07 +00008234 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008235 return;
8236
Fred Drake4d1e64b2002-04-15 19:40:07 +00008237 Py_INCREF(PyExc_OSError);
8238 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008239
Guido van Rossumb3d39562000-01-31 18:41:26 +00008240#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008241 if (posix_putenv_garbage == NULL)
8242 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008243#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008244
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008245 if (!initialized) {
8246 stat_result_desc.name = MODNAME ".stat_result";
8247 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8248 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8249 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8250 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8251 structseq_new = StatResultType.tp_new;
8252 StatResultType.tp_new = statresult_new;
8253
8254 statvfs_result_desc.name = MODNAME ".statvfs_result";
8255 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8256 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008257 Py_INCREF((PyObject*) &StatResultType);
8258 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008259 Py_INCREF((PyObject*) &StatVFSResultType);
8260 PyModule_AddObject(m, "statvfs_result",
8261 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008262 initialized = 1;
Guido van Rossumb6775db1994-08-01 11:34:53 +00008263}
Anthony Baxterac6bd462006-04-13 02:06:09 +00008264
8265#ifdef __cplusplus
8266}
8267#endif
8268