blob: e07021a8f3d91e3830ed58aecff9ea2108d9393a [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
984static PyTypeObject StatResultType;
985static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000986static newfunc structseq_new;
987
988static PyObject *
989statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
990{
991 PyStructSequence *result;
992 int i;
993
994 result = (PyStructSequence*)structseq_new(type, args, kwds);
995 if (!result)
996 return NULL;
997 /* If we have been initialized from a tuple,
998 st_?time might be set to None. Initialize it
999 from the int slots. */
1000 for (i = 7; i <= 9; i++) {
1001 if (result->ob_item[i+3] == Py_None) {
1002 Py_DECREF(Py_None);
1003 Py_INCREF(result->ob_item[i]);
1004 result->ob_item[i+3] = result->ob_item[i];
1005 }
1006 }
1007 return (PyObject*)result;
1008}
1009
1010
1011
1012/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001013static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001014
1015PyDoc_STRVAR(stat_float_times__doc__,
1016"stat_float_times([newval]) -> oldval\n\n\
1017Determine whether os.[lf]stat represents time stamps as float objects.\n\
1018If newval is True, future calls to stat() return floats, if it is False,\n\
1019future calls return ints. \n\
1020If newval is omitted, return the current setting.\n");
1021
1022static PyObject*
1023stat_float_times(PyObject* self, PyObject *args)
1024{
1025 int newval = -1;
1026 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1027 return NULL;
1028 if (newval == -1)
1029 /* Return old value */
1030 return PyBool_FromLong(_stat_float_times);
1031 _stat_float_times = newval;
1032 Py_INCREF(Py_None);
1033 return Py_None;
1034}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001035
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001036static void
1037fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1038{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001039 PyObject *fval,*ival;
1040#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001041 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001042#else
1043 ival = PyInt_FromLong((long)sec);
1044#endif
1045 if (_stat_float_times) {
1046 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1047 } else {
1048 fval = ival;
1049 Py_INCREF(fval);
1050 }
1051 PyStructSequence_SET_ITEM(v, index, ival);
1052 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001053}
1054
Tim Peters5aa91602002-01-30 05:46:57 +00001055/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001056 (used by posix_stat() and posix_fstat()) */
1057static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001058_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001059{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001060 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001061 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001062 if (v == NULL)
1063 return NULL;
1064
Martin v. Löwis14694662006-02-03 12:54:16 +00001065 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001066#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001067 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001068 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001069#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001070 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001071#endif
1072#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001073 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001074 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001075#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001076 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001077#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001078 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1079 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1080 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001081#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001082 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001083 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001084#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001085 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001086#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001087
Martin v. Löwis14694662006-02-03 12:54:16 +00001088#if defined(HAVE_STAT_TV_NSEC)
1089 ansec = st->st_atim.tv_nsec;
1090 mnsec = st->st_mtim.tv_nsec;
1091 cnsec = st->st_ctim.tv_nsec;
1092#elif defined(HAVE_STAT_TV_NSEC2)
1093 ansec = st->st_atimespec.tv_nsec;
1094 mnsec = st->st_mtimespec.tv_nsec;
1095 cnsec = st->st_ctimespec.tv_nsec;
1096#elif defined(HAVE_STAT_NSEC)
1097 ansec = st->st_atime_nsec;
1098 mnsec = st->st_mtime_nsec;
1099 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001100#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001101 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001102#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001103 fill_time(v, 7, st->st_atime, ansec);
1104 fill_time(v, 8, st->st_mtime, mnsec);
1105 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001106
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001107#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001108 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001109 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001111#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001112 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001113 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001114#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001115#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001116 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001117 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001118#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001119#ifdef HAVE_STRUCT_STAT_ST_GEN
1120 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001121 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001122#endif
1123#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1124 {
1125 PyObject *val;
1126 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001127 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001128#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001129 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001130#else
1131 bnsec = 0;
1132#endif
1133 if (_stat_float_times) {
1134 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1135 } else {
1136 val = PyInt_FromLong((long)bsec);
1137 }
1138 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1139 val);
1140 }
1141#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001142#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1143 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001144 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001145#endif
Fred Drake699f3522000-06-29 21:12:41 +00001146
1147 if (PyErr_Occurred()) {
1148 Py_DECREF(v);
1149 return NULL;
1150 }
1151
1152 return v;
1153}
1154
Martin v. Löwisd8948722004-06-02 09:57:56 +00001155#ifdef MS_WINDOWS
1156
1157/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1158 where / can be used in place of \ and the trailing slash is optional.
1159 Both SERVER and SHARE must have at least one character.
1160*/
1161
1162#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1163#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1164#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1165
Tim Peters4ad82172004-08-30 17:02:04 +00001166static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001167IsUNCRootA(char *path, int pathlen)
1168{
1169 #define ISSLASH ISSLASHA
1170
1171 int i, share;
1172
1173 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1174 /* minimum UNCRoot is \\x\y */
1175 return FALSE;
1176 for (i = 2; i < pathlen ; i++)
1177 if (ISSLASH(path[i])) break;
1178 if (i == 2 || i == pathlen)
1179 /* do not allow \\\SHARE or \\SERVER */
1180 return FALSE;
1181 share = i+1;
1182 for (i = share; i < pathlen; i++)
1183 if (ISSLASH(path[i])) break;
1184 return (i != share && (i == pathlen || i == pathlen-1));
1185
1186 #undef ISSLASH
1187}
1188
1189#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001190static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001191IsUNCRootW(Py_UNICODE *path, int pathlen)
1192{
1193 #define ISSLASH ISSLASHW
1194
1195 int i, share;
1196
1197 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1198 /* minimum UNCRoot is \\x\y */
1199 return FALSE;
1200 for (i = 2; i < pathlen ; i++)
1201 if (ISSLASH(path[i])) break;
1202 if (i == 2 || i == pathlen)
1203 /* do not allow \\\SHARE or \\SERVER */
1204 return FALSE;
1205 share = i+1;
1206 for (i = share; i < pathlen; i++)
1207 if (ISSLASH(path[i])) break;
1208 return (i != share && (i == pathlen || i == pathlen-1));
1209
1210 #undef ISSLASH
1211}
1212#endif /* Py_WIN_WIDE_FILENAMES */
1213#endif /* MS_WINDOWS */
1214
Barry Warsaw53699e91996-12-10 23:23:01 +00001215static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001216posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001217 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001218#ifdef __VMS
1219 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1220#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001221 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001222#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001223 char *wformat,
1224 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001225{
Fred Drake699f3522000-06-29 21:12:41 +00001226 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001227 char *path = NULL; /* pass this to stat; do not free() it */
1228 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001229 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001230 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001231
1232#ifdef Py_WIN_WIDE_FILENAMES
1233 /* If on wide-character-capable OS see if argument
1234 is Unicode and if so use wide API. */
1235 if (unicode_file_names()) {
1236 PyUnicodeObject *po;
1237 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001238 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1239
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001240 Py_BEGIN_ALLOW_THREADS
1241 /* PyUnicode_AS_UNICODE result OK without
1242 thread lock as it is a simple dereference. */
1243 res = wstatfunc(wpath, &st);
1244 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001245
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001246 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001247 return win32_error_unicode("stat", wpath);
1248 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001249 }
1250 /* Drop the argument parsing error as narrow strings
1251 are also valid. */
1252 PyErr_Clear();
1253 }
1254#endif
1255
Tim Peters5aa91602002-01-30 05:46:57 +00001256 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001257 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001258 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001259 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001260
Barry Warsaw53699e91996-12-10 23:23:01 +00001261 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001262 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001263 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001264
1265 if (res != 0) {
1266#ifdef MS_WINDOWS
1267 result = win32_error("stat", pathfree);
1268#else
1269 result = posix_error_with_filename(pathfree);
1270#endif
1271 }
1272 else
1273 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001274
Tim Peters500bd032001-12-19 19:05:01 +00001275 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001276 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001277}
1278
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001279/* POSIX methods */
1280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001281PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001282"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001283Use the real uid/gid to test for access to a path. Note that most\n\
1284operations will use the effective uid/gid, therefore this routine can\n\
1285be used in a suid/sgid environment to test if the invoking user has the\n\
1286specified access to the path. The mode argument can be F_OK to test\n\
1287existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001288
1289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001290posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001291{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001292 char *path;
1293 int mode;
1294 int res;
1295
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001296#ifdef Py_WIN_WIDE_FILENAMES
1297 if (unicode_file_names()) {
1298 PyUnicodeObject *po;
1299 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1300 Py_BEGIN_ALLOW_THREADS
1301 /* PyUnicode_AS_UNICODE OK without thread lock as
1302 it is a simple dereference. */
1303 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1304 Py_END_ALLOW_THREADS
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001305 return PyBool_FromLong(res == 0);
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001306 }
1307 /* Drop the argument parsing error as narrow strings
1308 are also valid. */
1309 PyErr_Clear();
1310 }
1311#endif
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001312 if (!PyArg_ParseTuple(args, "eti:access",
1313 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001314 return NULL;
1315 Py_BEGIN_ALLOW_THREADS
1316 res = access(path, mode);
1317 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001318 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001319 return PyBool_FromLong(res == 0);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001320}
1321
Guido van Rossumd371ff11999-01-25 16:12:23 +00001322#ifndef F_OK
1323#define F_OK 0
1324#endif
1325#ifndef R_OK
1326#define R_OK 4
1327#endif
1328#ifndef W_OK
1329#define W_OK 2
1330#endif
1331#ifndef X_OK
1332#define X_OK 1
1333#endif
1334
1335#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001336PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001337"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001338Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001339
1340static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001341posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001342{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001343 int id;
1344 char *ret;
1345
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001346 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001347 return NULL;
1348
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001349#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001350 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001351 if (id == 0) {
1352 ret = ttyname();
1353 }
1354 else {
1355 ret = NULL;
1356 }
1357#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001358 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001359#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001360 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001361 return posix_error();
1362 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001363}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001364#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001365
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001366#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001367PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001368"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001370
1371static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001372posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001373{
1374 char *ret;
1375 char buffer[L_ctermid];
1376
Greg Wardb48bc172000-03-01 21:51:56 +00001377#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001378 ret = ctermid_r(buffer);
1379#else
1380 ret = ctermid(buffer);
1381#endif
1382 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001383 return posix_error();
1384 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001385}
1386#endif
1387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001388PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001389"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001390Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001391
Barry Warsaw53699e91996-12-10 23:23:01 +00001392static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001393posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001394{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001395#ifdef MS_WINDOWS
1396 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1397#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1398 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001399#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001400 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001401 NULL, NULL);
1402#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001403 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001404#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001405}
1406
Fred Drake4d1e64b2002-04-15 19:40:07 +00001407#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001408PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001409"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001410Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001411opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001412
1413static PyObject *
1414posix_fchdir(PyObject *self, PyObject *fdobj)
1415{
1416 return posix_fildes(fdobj, fchdir);
1417}
1418#endif /* HAVE_FCHDIR */
1419
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001420
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001421PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001422"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001423Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001424
Barry Warsaw53699e91996-12-10 23:23:01 +00001425static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001426posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001427{
Mark Hammondef8b6542001-05-13 08:04:26 +00001428 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001429 int i;
1430 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001431#ifdef Py_WIN_WIDE_FILENAMES
1432 if (unicode_file_names()) {
1433 PyUnicodeObject *po;
1434 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1435 Py_BEGIN_ALLOW_THREADS
1436 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1437 Py_END_ALLOW_THREADS
1438 if (res < 0)
1439 return posix_error_with_unicode_filename(
1440 PyUnicode_AS_UNICODE(po));
1441 Py_INCREF(Py_None);
1442 return Py_None;
1443 }
1444 /* Drop the argument parsing error as narrow strings
1445 are also valid. */
1446 PyErr_Clear();
1447 }
1448#endif /* Py_WIN_WIDE_FILENAMES */
1449 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001450 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001451 return NULL;
1452 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001453 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001454 Py_END_ALLOW_THREADS
1455 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001456 return posix_error_with_allocated_filename(path);
1457 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001458 Py_INCREF(Py_None);
1459 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460}
1461
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001462
Martin v. Löwis244edc82001-10-04 22:44:26 +00001463#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001464PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001465"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001466Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001467
1468static PyObject *
1469posix_chroot(PyObject *self, PyObject *args)
1470{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001471 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001472}
1473#endif
1474
Guido van Rossum21142a01999-01-08 21:05:37 +00001475#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001476PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001477"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001478force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001479
1480static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001481posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001482{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001483 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001484}
1485#endif /* HAVE_FSYNC */
1486
1487#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001488
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001489#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001490extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1491#endif
1492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001493PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001494"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001495force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001496 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001497
1498static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001499posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001500{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001501 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001502}
1503#endif /* HAVE_FDATASYNC */
1504
1505
Fredrik Lundh10723342000-07-10 16:38:09 +00001506#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001507PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001508"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001509Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001510
Barry Warsaw53699e91996-12-10 23:23:01 +00001511static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001512posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001513{
Mark Hammondef8b6542001-05-13 08:04:26 +00001514 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001515 int uid, gid;
1516 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001517 if (!PyArg_ParseTuple(args, "etii:chown",
1518 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001519 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001520 return NULL;
1521 Py_BEGIN_ALLOW_THREADS
1522 res = chown(path, (uid_t) uid, (gid_t) gid);
1523 Py_END_ALLOW_THREADS
1524 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001525 return posix_error_with_allocated_filename(path);
1526 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001527 Py_INCREF(Py_None);
1528 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001529}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001530#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001531
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001532#ifdef HAVE_LCHOWN
1533PyDoc_STRVAR(posix_lchown__doc__,
1534"lchown(path, uid, gid)\n\n\
1535Change the owner and group id of path to the numeric uid and gid.\n\
1536This function will not follow symbolic links.");
1537
1538static PyObject *
1539posix_lchown(PyObject *self, PyObject *args)
1540{
1541 char *path = NULL;
1542 int uid, gid;
1543 int res;
1544 if (!PyArg_ParseTuple(args, "etii:lchown",
1545 Py_FileSystemDefaultEncoding, &path,
1546 &uid, &gid))
1547 return NULL;
1548 Py_BEGIN_ALLOW_THREADS
1549 res = lchown(path, (uid_t) uid, (gid_t) gid);
1550 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001551 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001552 return posix_error_with_allocated_filename(path);
1553 PyMem_Free(path);
1554 Py_INCREF(Py_None);
1555 return Py_None;
1556}
1557#endif /* HAVE_LCHOWN */
1558
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001559
Guido van Rossum36bc6801995-06-14 22:54:23 +00001560#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001562"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001563Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001564
Barry Warsaw53699e91996-12-10 23:23:01 +00001565static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001566posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001567{
1568 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001569 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001570
Barry Warsaw53699e91996-12-10 23:23:01 +00001571 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001572#if defined(PYOS_OS2) && defined(PYCC_GCC)
1573 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001574#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001575 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001576#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001577 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001578 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001579 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001580 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001581}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001582
Walter Dörwald3b918c32002-11-21 20:18:46 +00001583#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001584PyDoc_STRVAR(posix_getcwdu__doc__,
1585"getcwdu() -> path\n\n\
1586Return a unicode string representing the current working directory.");
1587
1588static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001589posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001590{
1591 char buf[1026];
1592 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001593
1594#ifdef Py_WIN_WIDE_FILENAMES
1595 if (unicode_file_names()) {
1596 wchar_t *wres;
1597 wchar_t wbuf[1026];
1598 Py_BEGIN_ALLOW_THREADS
1599 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1600 Py_END_ALLOW_THREADS
1601 if (wres == NULL)
1602 return posix_error();
1603 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1604 }
1605#endif
1606
1607 Py_BEGIN_ALLOW_THREADS
1608#if defined(PYOS_OS2) && defined(PYCC_GCC)
1609 res = _getcwd2(buf, sizeof buf);
1610#else
1611 res = getcwd(buf, sizeof buf);
1612#endif
1613 Py_END_ALLOW_THREADS
1614 if (res == NULL)
1615 return posix_error();
1616 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1617}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001618#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001619#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001621
Guido van Rossumb6775db1994-08-01 11:34:53 +00001622#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001623PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001624"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001625Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001626
Barry Warsaw53699e91996-12-10 23:23:01 +00001627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001628posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001629{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001630 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001631}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001632#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001633
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001635PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001636"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001637Return a list containing the names of the entries in the directory.\n\
1638\n\
1639 path: path of directory to list\n\
1640\n\
1641The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001642entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001643
Barry Warsaw53699e91996-12-10 23:23:01 +00001644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001645posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001646{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001647 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001648 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001649#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001650
Barry Warsaw53699e91996-12-10 23:23:01 +00001651 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001652 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001653 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001654 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001655 /* MAX_PATH characters could mean a bigger encoded string */
1656 char namebuf[MAX_PATH*2+5];
1657 char *bufptr = namebuf;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001658 Py_ssize_t len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001659
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001660#ifdef Py_WIN_WIDE_FILENAMES
1661 /* If on wide-character-capable OS see if argument
1662 is Unicode and if so use wide API. */
1663 if (unicode_file_names()) {
1664 PyUnicodeObject *po;
1665 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1666 WIN32_FIND_DATAW wFileData;
1667 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1668 Py_UNICODE wch;
1669 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1670 wnamebuf[MAX_PATH] = L'\0';
1671 len = wcslen(wnamebuf);
1672 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1673 if (wch != L'/' && wch != L'\\' && wch != L':')
1674 wnamebuf[len++] = L'/';
1675 wcscpy(wnamebuf + len, L"*.*");
1676 if ((d = PyList_New(0)) == NULL)
1677 return NULL;
1678 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1679 if (hFindFile == INVALID_HANDLE_VALUE) {
1680 errno = GetLastError();
1681 if (errno == ERROR_FILE_NOT_FOUND) {
1682 return d;
1683 }
1684 Py_DECREF(d);
1685 return win32_error_unicode("FindFirstFileW", wnamebuf);
1686 }
1687 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001688 /* Skip over . and .. */
1689 if (wcscmp(wFileData.cFileName, L".") != 0 &&
1690 wcscmp(wFileData.cFileName, L"..") != 0) {
1691 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1692 if (v == NULL) {
1693 Py_DECREF(d);
1694 d = NULL;
1695 break;
1696 }
1697 if (PyList_Append(d, v) != 0) {
1698 Py_DECREF(v);
1699 Py_DECREF(d);
1700 d = NULL;
1701 break;
1702 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001703 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001704 }
Georg Brandl622927b2006-03-07 12:48:03 +00001705 Py_BEGIN_ALLOW_THREADS
1706 result = FindNextFileW(hFindFile, &wFileData);
1707 Py_END_ALLOW_THREADS
1708 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001709
1710 if (FindClose(hFindFile) == FALSE) {
1711 Py_DECREF(d);
1712 return win32_error_unicode("FindClose", wnamebuf);
1713 }
1714 return d;
1715 }
1716 /* Drop the argument parsing error as narrow strings
1717 are also valid. */
1718 PyErr_Clear();
1719 }
1720#endif
1721
Tim Peters5aa91602002-01-30 05:46:57 +00001722 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001723 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001724 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001725 if (len > 0) {
1726 char ch = namebuf[len-1];
1727 if (ch != SEP && ch != ALTSEP && ch != ':')
1728 namebuf[len++] = '/';
1729 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001730 strcpy(namebuf + len, "*.*");
1731
Barry Warsaw53699e91996-12-10 23:23:01 +00001732 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001733 return NULL;
1734
1735 hFindFile = FindFirstFile(namebuf, &FileData);
1736 if (hFindFile == INVALID_HANDLE_VALUE) {
1737 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001738 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001739 return d;
1740 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001741 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001742 }
1743 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001744 /* Skip over . and .. */
1745 if (strcmp(FileData.cFileName, ".") != 0 &&
1746 strcmp(FileData.cFileName, "..") != 0) {
1747 v = PyString_FromString(FileData.cFileName);
1748 if (v == NULL) {
1749 Py_DECREF(d);
1750 d = NULL;
1751 break;
1752 }
1753 if (PyList_Append(d, v) != 0) {
1754 Py_DECREF(v);
1755 Py_DECREF(d);
1756 d = NULL;
1757 break;
1758 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001759 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001760 }
Georg Brandl622927b2006-03-07 12:48:03 +00001761 Py_BEGIN_ALLOW_THREADS
1762 result = FindNextFile(hFindFile, &FileData);
1763 Py_END_ALLOW_THREADS
1764 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001765
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001766 if (FindClose(hFindFile) == FALSE) {
1767 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001768 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001769 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001770
1771 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001772
Tim Peters0bb44a42000-09-15 07:44:49 +00001773#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001774
1775#ifndef MAX_PATH
1776#define MAX_PATH CCHMAXPATH
1777#endif
1778 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00001779 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001780 PyObject *d, *v;
1781 char namebuf[MAX_PATH+5];
1782 HDIR hdir = 1;
1783 ULONG srchcnt = 1;
1784 FILEFINDBUF3 ep;
1785 APIRET rc;
1786
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001787 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001788 return NULL;
1789 if (len >= MAX_PATH) {
1790 PyErr_SetString(PyExc_ValueError, "path too long");
1791 return NULL;
1792 }
1793 strcpy(namebuf, name);
1794 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001795 if (*pt == ALTSEP)
1796 *pt = SEP;
1797 if (namebuf[len-1] != SEP)
1798 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001799 strcpy(namebuf + len, "*.*");
1800
1801 if ((d = PyList_New(0)) == NULL)
1802 return NULL;
1803
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001804 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1805 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001806 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001807 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1808 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1809 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001810
1811 if (rc != NO_ERROR) {
1812 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001813 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001814 }
1815
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001816 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001817 do {
1818 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001819 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001820 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001821
1822 strcpy(namebuf, ep.achName);
1823
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001824 /* Leave Case of Name Alone -- In Native Form */
1825 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001826
1827 v = PyString_FromString(namebuf);
1828 if (v == NULL) {
1829 Py_DECREF(d);
1830 d = NULL;
1831 break;
1832 }
1833 if (PyList_Append(d, v) != 0) {
1834 Py_DECREF(v);
1835 Py_DECREF(d);
1836 d = NULL;
1837 break;
1838 }
1839 Py_DECREF(v);
1840 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1841 }
1842
1843 return d;
1844#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001845
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001846 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001847 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001848 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001849 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001850 int arg_is_unicode = 1;
1851
Georg Brandl05e89b82006-04-11 07:04:06 +00001852 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00001853 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1854 arg_is_unicode = 0;
1855 PyErr_Clear();
1856 }
1857 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001858 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001859 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001860 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001861 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001862 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001863 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001864 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001865 return NULL;
1866 }
Georg Brandl622927b2006-03-07 12:48:03 +00001867 for (;;) {
1868 Py_BEGIN_ALLOW_THREADS
1869 ep = readdir(dirp);
1870 Py_END_ALLOW_THREADS
1871 if (ep == NULL)
1872 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001873 if (ep->d_name[0] == '.' &&
1874 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001875 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001876 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001878 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001879 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001880 d = NULL;
1881 break;
1882 }
Just van Rossum46c97842003-02-25 21:42:15 +00001883#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001884 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001885 PyObject *w;
1886
1887 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001888 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001889 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001890 if (w != NULL) {
1891 Py_DECREF(v);
1892 v = w;
1893 }
1894 else {
1895 /* fall back to the original byte string, as
1896 discussed in patch #683592 */
1897 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001898 }
Just van Rossum46c97842003-02-25 21:42:15 +00001899 }
1900#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001901 if (PyList_Append(d, v) != 0) {
1902 Py_DECREF(v);
1903 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001904 d = NULL;
1905 break;
1906 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001907 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001908 }
Georg Brandlbbfe4fa2006-04-11 06:47:43 +00001909 if (errno != 0 && d != NULL) {
1910 /* readdir() returned NULL and set errno */
1911 closedir(dirp);
1912 Py_DECREF(d);
1913 return posix_error_with_allocated_filename(name);
1914 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001915 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001916 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001917
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001918 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001919
Tim Peters0bb44a42000-09-15 07:44:49 +00001920#endif /* which OS */
1921} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001922
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001923#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001924/* A helper function for abspath on win32 */
1925static PyObject *
1926posix__getfullpathname(PyObject *self, PyObject *args)
1927{
1928 /* assume encoded strings wont more than double no of chars */
1929 char inbuf[MAX_PATH*2];
1930 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00001931 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00001932 char outbuf[MAX_PATH*2];
1933 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001934#ifdef Py_WIN_WIDE_FILENAMES
1935 if (unicode_file_names()) {
1936 PyUnicodeObject *po;
1937 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1938 Py_UNICODE woutbuf[MAX_PATH*2];
1939 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001940 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001941 sizeof(woutbuf)/sizeof(woutbuf[0]),
1942 woutbuf, &wtemp))
1943 return win32_error("GetFullPathName", "");
1944 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1945 }
1946 /* Drop the argument parsing error as narrow strings
1947 are also valid. */
1948 PyErr_Clear();
1949 }
1950#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001951 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1952 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001953 &insize))
1954 return NULL;
1955 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1956 outbuf, &temp))
1957 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00001958 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
1959 return PyUnicode_Decode(outbuf, strlen(outbuf),
1960 Py_FileSystemDefaultEncoding, NULL);
1961 }
Mark Hammondef8b6542001-05-13 08:04:26 +00001962 return PyString_FromString(outbuf);
1963} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001964#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001966PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001967"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001968Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001969
Barry Warsaw53699e91996-12-10 23:23:01 +00001970static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001971posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001972{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001973 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001974 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001975 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001976
1977#ifdef Py_WIN_WIDE_FILENAMES
1978 if (unicode_file_names()) {
1979 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001980 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981 Py_BEGIN_ALLOW_THREADS
1982 /* PyUnicode_AS_UNICODE OK without thread lock as
1983 it is a simple dereference. */
1984 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1985 Py_END_ALLOW_THREADS
1986 if (res < 0)
1987 return posix_error();
1988 Py_INCREF(Py_None);
1989 return Py_None;
1990 }
1991 /* Drop the argument parsing error as narrow strings
1992 are also valid. */
1993 PyErr_Clear();
1994 }
1995#endif
1996
Tim Peters5aa91602002-01-30 05:46:57 +00001997 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001998 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001999 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002000 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002001#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002002 res = mkdir(path);
2003#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002004 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002005#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002006 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002007 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002008 return posix_error_with_allocated_filename(path);
2009 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002010 Py_INCREF(Py_None);
2011 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002012}
2013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002014
Neal Norwitz1818ed72006-03-26 00:29:48 +00002015/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2016#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002017#include <sys/resource.h>
2018#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002019
Neal Norwitz1818ed72006-03-26 00:29:48 +00002020
2021#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002022PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002023"nice(inc) -> new_priority\n\n\
2024Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002025
Barry Warsaw53699e91996-12-10 23:23:01 +00002026static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002027posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002028{
2029 int increment, value;
2030
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002031 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002032 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002033
2034 /* There are two flavours of 'nice': one that returns the new
2035 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002036 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2037 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002038
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002039 If we are of the nice family that returns the new priority, we
2040 need to clear errno before the call, and check if errno is filled
2041 before calling posix_error() on a returnvalue of -1, because the
2042 -1 may be the actual new priority! */
2043
2044 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002045 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002046#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002047 if (value == 0)
2048 value = getpriority(PRIO_PROCESS, 0);
2049#endif
2050 if (value == -1 && errno != 0)
2051 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002052 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002053 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002054}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002055#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002059"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002060Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002061
Barry Warsaw53699e91996-12-10 23:23:01 +00002062static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002063posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002064{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002065#ifdef MS_WINDOWS
2066 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
2067#else
2068 return posix_2str(args, "etet:rename", rename, NULL, NULL);
2069#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070}
2071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002073PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002074"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002075Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002076
Barry Warsaw53699e91996-12-10 23:23:01 +00002077static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002078posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002079{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002080#ifdef MS_WINDOWS
2081 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
2082#else
2083 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
2084#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002085}
2086
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002087
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002088PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002089"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002090Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002091
Barry Warsaw53699e91996-12-10 23:23:01 +00002092static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002093posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002094{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002095#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002096 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002097#else
2098 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2099#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002100}
2101
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002102
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002103#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002104PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002105"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002107
Barry Warsaw53699e91996-12-10 23:23:01 +00002108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002109posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002110{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002111 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002112 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002113 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002114 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002115 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002116 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002117 Py_END_ALLOW_THREADS
2118 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002119}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002120#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002121
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002123PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002124"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002125Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002126
Barry Warsaw53699e91996-12-10 23:23:01 +00002127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002128posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002129{
2130 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002131 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002132 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002133 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002134 if (i < 0)
2135 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002136 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002137}
2138
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002140PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002141"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002143
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002144PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002145"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002146Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002147
Barry Warsaw53699e91996-12-10 23:23:01 +00002148static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002149posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002150{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002151#ifdef MS_WINDOWS
2152 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
2153#else
2154 return posix_1str(args, "et:remove", unlink, NULL, NULL);
2155#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002156}
2157
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002158
Guido van Rossumb6775db1994-08-01 11:34:53 +00002159#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002160PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002161"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002162Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002163
Barry Warsaw53699e91996-12-10 23:23:01 +00002164static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002165posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002166{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002167 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002168 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002169
Barry Warsaw53699e91996-12-10 23:23:01 +00002170 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002171 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002172 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002173 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002174 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002175 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002176 u.sysname,
2177 u.nodename,
2178 u.release,
2179 u.version,
2180 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002181}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002182#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002183
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002184static int
2185extract_time(PyObject *t, long* sec, long* usec)
2186{
2187 long intval;
2188 if (PyFloat_Check(t)) {
2189 double tval = PyFloat_AsDouble(t);
2190 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2191 if (!intobj)
2192 return -1;
2193 intval = PyInt_AsLong(intobj);
2194 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002195 if (intval == -1 && PyErr_Occurred())
2196 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002197 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002198 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002199 if (*usec < 0)
2200 /* If rounding gave us a negative number,
2201 truncate. */
2202 *usec = 0;
2203 return 0;
2204 }
2205 intval = PyInt_AsLong(t);
2206 if (intval == -1 && PyErr_Occurred())
2207 return -1;
2208 *sec = intval;
2209 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002210 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002211}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002214"utime(path, (atime, utime))\n\
2215utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002216Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002217second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002218
Barry Warsaw53699e91996-12-10 23:23:01 +00002219static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002220posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221{
Neal Norwitz2adf2102004-06-09 01:46:02 +00002222 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002223 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002224 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002225 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002226
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002227#if defined(HAVE_UTIMES)
2228 struct timeval buf[2];
2229#define ATIME buf[0].tv_sec
2230#define MTIME buf[1].tv_sec
2231#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002232/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002233 struct utimbuf buf;
2234#define ATIME buf.actime
2235#define MTIME buf.modtime
2236#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002237#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002238 time_t buf[2];
2239#define ATIME buf[0]
2240#define MTIME buf[1]
2241#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002242#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002243
Mark Hammond817c9292003-12-03 01:22:38 +00002244 int have_unicode_filename = 0;
2245#ifdef Py_WIN_WIDE_FILENAMES
2246 PyUnicodeObject *obwpath;
2247 wchar_t *wpath;
2248 if (unicode_file_names()) {
2249 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2250 wpath = PyUnicode_AS_UNICODE(obwpath);
2251 have_unicode_filename = 1;
2252 } else
2253 /* Drop the argument parsing error as narrow strings
2254 are also valid. */
2255 PyErr_Clear();
2256 }
2257#endif /* Py_WIN_WIDE_FILENAMES */
2258
2259 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002260 !PyArg_ParseTuple(args, "etO:utime",
2261 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002262 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002263 if (arg == Py_None) {
2264 /* optional time values not given */
2265 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002266#ifdef Py_WIN_WIDE_FILENAMES
2267 if (have_unicode_filename)
2268 res = _wutime(wpath, NULL);
2269 else
2270#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002271 res = utime(path, NULL);
2272 Py_END_ALLOW_THREADS
2273 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002274 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002275 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002276 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002277 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002278 return NULL;
2279 }
2280 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002281 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002282 &atime, &ausec) == -1) {
2283 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002284 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002285 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002286 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002287 &mtime, &musec) == -1) {
2288 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002289 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002290 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002291 ATIME = atime;
2292 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002293#ifdef HAVE_UTIMES
2294 buf[0].tv_usec = ausec;
2295 buf[1].tv_usec = musec;
2296 Py_BEGIN_ALLOW_THREADS
2297 res = utimes(path, buf);
2298 Py_END_ALLOW_THREADS
2299#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002300 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002301#ifdef Py_WIN_WIDE_FILENAMES
2302 if (have_unicode_filename)
Tim Peters4ad82172004-08-30 17:02:04 +00002303 /* utime is OK with utimbuf, but _wutime insists
2304 on _utimbuf (the msvc headers assert the
Mark Hammond817c9292003-12-03 01:22:38 +00002305 underscore version is ansi) */
2306 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2307 else
2308#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002309 res = utime(path, UTIME_ARG);
2310 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002311#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002312 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002313 if (res < 0) {
2314#ifdef Py_WIN_WIDE_FILENAMES
Neal Norwitz2adf2102004-06-09 01:46:02 +00002315 if (have_unicode_filename)
Mark Hammond2d5914b2004-05-04 08:10:37 +00002316 return posix_error_with_unicode_filename(wpath);
2317#endif /* Py_WIN_WIDE_FILENAMES */
Neal Norwitz96652712004-06-06 20:40:27 +00002318 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002319 }
Neal Norwitz96652712004-06-06 20:40:27 +00002320 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002321 Py_INCREF(Py_None);
2322 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002323#undef UTIME_ARG
2324#undef ATIME
2325#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002326}
2327
Guido van Rossum85e3b011991-06-03 12:42:10 +00002328
Guido van Rossum3b066191991-06-04 19:40:25 +00002329/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002330
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002331PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002332"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002333Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002334
Barry Warsaw53699e91996-12-10 23:23:01 +00002335static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002336posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002337{
2338 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002339 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002340 return NULL;
2341 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002342 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002343}
2344
Martin v. Löwis114619e2002-10-07 06:44:21 +00002345#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2346static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002347free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002348{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002349 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002350 for (i = 0; i < count; i++)
2351 PyMem_Free(array[i]);
2352 PyMem_DEL(array);
2353}
2354#endif
2355
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002356
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002357#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002358PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002359"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002360Execute an executable path with arguments, replacing current process.\n\
2361\n\
2362 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002363 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002364
Barry Warsaw53699e91996-12-10 23:23:01 +00002365static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002366posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002367{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002368 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002369 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002370 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002371 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002372 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002373
Guido van Rossum89b33251993-10-22 14:26:06 +00002374 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002375 argv is a list or tuple of strings. */
2376
Martin v. Löwis114619e2002-10-07 06:44:21 +00002377 if (!PyArg_ParseTuple(args, "etO:execv",
2378 Py_FileSystemDefaultEncoding,
2379 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002380 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002381 if (PyList_Check(argv)) {
2382 argc = PyList_Size(argv);
2383 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002384 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002385 else if (PyTuple_Check(argv)) {
2386 argc = PyTuple_Size(argv);
2387 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002388 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002389 else {
Fred Drake661ea262000-10-24 19:57:45 +00002390 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002391 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002392 return NULL;
2393 }
2394
Barry Warsaw53699e91996-12-10 23:23:01 +00002395 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002396 if (argvlist == NULL) {
2397 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002398 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002399 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002400 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002401 if (!PyArg_Parse((*getitem)(argv, i), "et",
2402 Py_FileSystemDefaultEncoding,
2403 &argvlist[i])) {
2404 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002405 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002406 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002407 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002408 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002409
Guido van Rossum85e3b011991-06-03 12:42:10 +00002410 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002411 }
2412 argvlist[argc] = NULL;
2413
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002414 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002415
Guido van Rossum85e3b011991-06-03 12:42:10 +00002416 /* If we get here it's definitely an error */
2417
Martin v. Löwis114619e2002-10-07 06:44:21 +00002418 free_string_array(argvlist, argc);
2419 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002420 return posix_error();
2421}
2422
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002423
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002424PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002425"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002426Execute a path with arguments and environment, replacing current process.\n\
2427\n\
2428 path: path of executable file\n\
2429 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002430 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002431
Barry Warsaw53699e91996-12-10 23:23:01 +00002432static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002433posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002434{
2435 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002436 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002437 char **argvlist;
2438 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002439 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002440 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002441 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002442 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002443
2444 /* execve has three arguments: (path, argv, env), where
2445 argv is a list or tuple of strings and env is a dictionary
2446 like posix.environ. */
2447
Martin v. Löwis114619e2002-10-07 06:44:21 +00002448 if (!PyArg_ParseTuple(args, "etOO:execve",
2449 Py_FileSystemDefaultEncoding,
2450 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002451 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002452 if (PyList_Check(argv)) {
2453 argc = PyList_Size(argv);
2454 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002455 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002456 else if (PyTuple_Check(argv)) {
2457 argc = PyTuple_Size(argv);
2458 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002459 }
2460 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002461 PyErr_SetString(PyExc_TypeError,
2462 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002463 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002464 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002465 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002466 PyErr_SetString(PyExc_TypeError,
2467 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002468 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002469 }
2470
Barry Warsaw53699e91996-12-10 23:23:01 +00002471 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002472 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002473 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002474 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002475 }
2476 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002477 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002478 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002479 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002480 &argvlist[i]))
2481 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002482 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002483 goto fail_1;
2484 }
2485 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002486 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002487 argvlist[argc] = NULL;
2488
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002489 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002490 if (i < 0)
2491 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002492 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002493 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002494 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002495 goto fail_1;
2496 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002497 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002498 keys = PyMapping_Keys(env);
2499 vals = PyMapping_Values(env);
2500 if (!keys || !vals)
2501 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002502 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2503 PyErr_SetString(PyExc_TypeError,
2504 "execve(): env.keys() or env.values() is not a list");
2505 goto fail_2;
2506 }
Tim Peters5aa91602002-01-30 05:46:57 +00002507
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002508 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002509 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002510 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002511
2512 key = PyList_GetItem(keys, pos);
2513 val = PyList_GetItem(vals, pos);
2514 if (!key || !val)
2515 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002516
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002517 if (!PyArg_Parse(
2518 key,
2519 "s;execve() arg 3 contains a non-string key",
2520 &k) ||
2521 !PyArg_Parse(
2522 val,
2523 "s;execve() arg 3 contains a non-string value",
2524 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002525 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002526 goto fail_2;
2527 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002528
2529#if defined(PYOS_OS2)
2530 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2531 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2532#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002533 len = PyString_Size(key) + PyString_Size(val) + 2;
2534 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002535 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002536 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002537 goto fail_2;
2538 }
Tim Petersc8996f52001-12-03 20:41:00 +00002539 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002540 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002541#if defined(PYOS_OS2)
2542 }
2543#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002544 }
2545 envlist[envc] = 0;
2546
2547 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002548
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002549 /* If we get here it's definitely an error */
2550
2551 (void) posix_error();
2552
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002553 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002554 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002555 PyMem_DEL(envlist[envc]);
2556 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002557 fail_1:
2558 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002559 Py_XDECREF(vals);
2560 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002561 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002562 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002563 return NULL;
2564}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002565#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002566
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002567
Guido van Rossuma1065681999-01-25 23:20:23 +00002568#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002569PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002570"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002571Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002572\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002573 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002574 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002575 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002576
2577static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002578posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002579{
2580 char *path;
2581 PyObject *argv;
2582 char **argvlist;
2583 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002584 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002585 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00002586
2587 /* spawnv has three arguments: (mode, path, argv), where
2588 argv is a list or tuple of strings. */
2589
Martin v. Löwis114619e2002-10-07 06:44:21 +00002590 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2591 Py_FileSystemDefaultEncoding,
2592 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002593 return NULL;
2594 if (PyList_Check(argv)) {
2595 argc = PyList_Size(argv);
2596 getitem = PyList_GetItem;
2597 }
2598 else if (PyTuple_Check(argv)) {
2599 argc = PyTuple_Size(argv);
2600 getitem = PyTuple_GetItem;
2601 }
2602 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002603 PyErr_SetString(PyExc_TypeError,
2604 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002605 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002606 return NULL;
2607 }
2608
2609 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002610 if (argvlist == NULL) {
2611 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002612 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002613 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002614 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002615 if (!PyArg_Parse((*getitem)(argv, i), "et",
2616 Py_FileSystemDefaultEncoding,
2617 &argvlist[i])) {
2618 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002619 PyErr_SetString(
2620 PyExc_TypeError,
2621 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002622 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002623 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002624 }
2625 }
2626 argvlist[argc] = NULL;
2627
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002628#if defined(PYOS_OS2) && defined(PYCC_GCC)
2629 Py_BEGIN_ALLOW_THREADS
2630 spawnval = spawnv(mode, path, argvlist);
2631 Py_END_ALLOW_THREADS
2632#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002633 if (mode == _OLD_P_OVERLAY)
2634 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002635
Tim Peters25059d32001-12-07 20:35:43 +00002636 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002637 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002638 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002639#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002640
Martin v. Löwis114619e2002-10-07 06:44:21 +00002641 free_string_array(argvlist, argc);
2642 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002643
Fred Drake699f3522000-06-29 21:12:41 +00002644 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002645 return posix_error();
2646 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002647#if SIZEOF_LONG == SIZEOF_VOID_P
2648 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002649#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002650 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002651#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002652}
2653
2654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002655PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002656"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002657Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002658\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002659 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002660 path: path of executable file\n\
2661 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002662 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002663
2664static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002665posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002666{
2667 char *path;
2668 PyObject *argv, *env;
2669 char **argvlist;
2670 char **envlist;
2671 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2672 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002673 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002674 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002675 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002676
2677 /* spawnve has four arguments: (mode, path, argv, env), where
2678 argv is a list or tuple of strings and env is a dictionary
2679 like posix.environ. */
2680
Martin v. Löwis114619e2002-10-07 06:44:21 +00002681 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2682 Py_FileSystemDefaultEncoding,
2683 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002684 return NULL;
2685 if (PyList_Check(argv)) {
2686 argc = PyList_Size(argv);
2687 getitem = PyList_GetItem;
2688 }
2689 else if (PyTuple_Check(argv)) {
2690 argc = PyTuple_Size(argv);
2691 getitem = PyTuple_GetItem;
2692 }
2693 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002694 PyErr_SetString(PyExc_TypeError,
2695 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002696 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002697 }
2698 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002699 PyErr_SetString(PyExc_TypeError,
2700 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002701 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002702 }
2703
2704 argvlist = PyMem_NEW(char *, argc+1);
2705 if (argvlist == NULL) {
2706 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002707 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002708 }
2709 for (i = 0; i < argc; i++) {
2710 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002711 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002712 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002713 &argvlist[i]))
2714 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002715 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002716 goto fail_1;
2717 }
2718 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002719 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002720 argvlist[argc] = NULL;
2721
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002722 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002723 if (i < 0)
2724 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002725 envlist = PyMem_NEW(char *, i + 1);
2726 if (envlist == NULL) {
2727 PyErr_NoMemory();
2728 goto fail_1;
2729 }
2730 envc = 0;
2731 keys = PyMapping_Keys(env);
2732 vals = PyMapping_Values(env);
2733 if (!keys || !vals)
2734 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002735 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2736 PyErr_SetString(PyExc_TypeError,
2737 "spawnve(): env.keys() or env.values() is not a list");
2738 goto fail_2;
2739 }
Tim Peters5aa91602002-01-30 05:46:57 +00002740
Guido van Rossuma1065681999-01-25 23:20:23 +00002741 for (pos = 0; pos < i; pos++) {
2742 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002743 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002744
2745 key = PyList_GetItem(keys, pos);
2746 val = PyList_GetItem(vals, pos);
2747 if (!key || !val)
2748 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002749
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002750 if (!PyArg_Parse(
2751 key,
2752 "s;spawnve() arg 3 contains a non-string key",
2753 &k) ||
2754 !PyArg_Parse(
2755 val,
2756 "s;spawnve() arg 3 contains a non-string value",
2757 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002758 {
2759 goto fail_2;
2760 }
Tim Petersc8996f52001-12-03 20:41:00 +00002761 len = PyString_Size(key) + PyString_Size(val) + 2;
2762 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002763 if (p == NULL) {
2764 PyErr_NoMemory();
2765 goto fail_2;
2766 }
Tim Petersc8996f52001-12-03 20:41:00 +00002767 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002768 envlist[envc++] = p;
2769 }
2770 envlist[envc] = 0;
2771
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002772#if defined(PYOS_OS2) && defined(PYCC_GCC)
2773 Py_BEGIN_ALLOW_THREADS
2774 spawnval = spawnve(mode, path, argvlist, envlist);
2775 Py_END_ALLOW_THREADS
2776#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002777 if (mode == _OLD_P_OVERLAY)
2778 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002779
2780 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002781 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002782 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002783#endif
Tim Peters25059d32001-12-07 20:35:43 +00002784
Fred Drake699f3522000-06-29 21:12:41 +00002785 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002786 (void) posix_error();
2787 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002788#if SIZEOF_LONG == SIZEOF_VOID_P
2789 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002790#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002791 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002792#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002793
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002794 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002795 while (--envc >= 0)
2796 PyMem_DEL(envlist[envc]);
2797 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002798 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002799 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002800 Py_XDECREF(vals);
2801 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002802 fail_0:
2803 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002804 return res;
2805}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002806
2807/* OS/2 supports spawnvp & spawnvpe natively */
2808#if defined(PYOS_OS2)
2809PyDoc_STRVAR(posix_spawnvp__doc__,
2810"spawnvp(mode, file, args)\n\n\
2811Execute the program 'file' in a new process, using the environment\n\
2812search path to find the file.\n\
2813\n\
2814 mode: mode of process creation\n\
2815 file: executable file name\n\
2816 args: tuple or list of strings");
2817
2818static PyObject *
2819posix_spawnvp(PyObject *self, PyObject *args)
2820{
2821 char *path;
2822 PyObject *argv;
2823 char **argvlist;
2824 int mode, i, argc;
2825 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002826 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002827
2828 /* spawnvp has three arguments: (mode, path, argv), where
2829 argv is a list or tuple of strings. */
2830
2831 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
2832 Py_FileSystemDefaultEncoding,
2833 &path, &argv))
2834 return NULL;
2835 if (PyList_Check(argv)) {
2836 argc = PyList_Size(argv);
2837 getitem = PyList_GetItem;
2838 }
2839 else if (PyTuple_Check(argv)) {
2840 argc = PyTuple_Size(argv);
2841 getitem = PyTuple_GetItem;
2842 }
2843 else {
2844 PyErr_SetString(PyExc_TypeError,
2845 "spawnvp() arg 2 must be a tuple or list");
2846 PyMem_Free(path);
2847 return NULL;
2848 }
2849
2850 argvlist = PyMem_NEW(char *, argc+1);
2851 if (argvlist == NULL) {
2852 PyMem_Free(path);
2853 return PyErr_NoMemory();
2854 }
2855 for (i = 0; i < argc; i++) {
2856 if (!PyArg_Parse((*getitem)(argv, i), "et",
2857 Py_FileSystemDefaultEncoding,
2858 &argvlist[i])) {
2859 free_string_array(argvlist, i);
2860 PyErr_SetString(
2861 PyExc_TypeError,
2862 "spawnvp() arg 2 must contain only strings");
2863 PyMem_Free(path);
2864 return NULL;
2865 }
2866 }
2867 argvlist[argc] = NULL;
2868
2869 Py_BEGIN_ALLOW_THREADS
2870#if defined(PYCC_GCC)
2871 spawnval = spawnvp(mode, path, argvlist);
2872#else
2873 spawnval = _spawnvp(mode, path, argvlist);
2874#endif
2875 Py_END_ALLOW_THREADS
2876
2877 free_string_array(argvlist, argc);
2878 PyMem_Free(path);
2879
2880 if (spawnval == -1)
2881 return posix_error();
2882 else
2883 return Py_BuildValue("l", (long) spawnval);
2884}
2885
2886
2887PyDoc_STRVAR(posix_spawnvpe__doc__,
2888"spawnvpe(mode, file, args, env)\n\n\
2889Execute the program 'file' in a new process, using the environment\n\
2890search path to find the file.\n\
2891\n\
2892 mode: mode of process creation\n\
2893 file: executable file name\n\
2894 args: tuple or list of arguments\n\
2895 env: dictionary of strings mapping to strings");
2896
2897static PyObject *
2898posix_spawnvpe(PyObject *self, PyObject *args)
2899{
2900 char *path;
2901 PyObject *argv, *env;
2902 char **argvlist;
2903 char **envlist;
2904 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2905 int mode, i, pos, argc, envc;
2906 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002907 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00002908 int lastarg = 0;
2909
2910 /* spawnvpe has four arguments: (mode, path, argv, env), where
2911 argv is a list or tuple of strings and env is a dictionary
2912 like posix.environ. */
2913
2914 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
2915 Py_FileSystemDefaultEncoding,
2916 &path, &argv, &env))
2917 return NULL;
2918 if (PyList_Check(argv)) {
2919 argc = PyList_Size(argv);
2920 getitem = PyList_GetItem;
2921 }
2922 else if (PyTuple_Check(argv)) {
2923 argc = PyTuple_Size(argv);
2924 getitem = PyTuple_GetItem;
2925 }
2926 else {
2927 PyErr_SetString(PyExc_TypeError,
2928 "spawnvpe() arg 2 must be a tuple or list");
2929 goto fail_0;
2930 }
2931 if (!PyMapping_Check(env)) {
2932 PyErr_SetString(PyExc_TypeError,
2933 "spawnvpe() arg 3 must be a mapping object");
2934 goto fail_0;
2935 }
2936
2937 argvlist = PyMem_NEW(char *, argc+1);
2938 if (argvlist == NULL) {
2939 PyErr_NoMemory();
2940 goto fail_0;
2941 }
2942 for (i = 0; i < argc; i++) {
2943 if (!PyArg_Parse((*getitem)(argv, i),
2944 "et;spawnvpe() arg 2 must contain only strings",
2945 Py_FileSystemDefaultEncoding,
2946 &argvlist[i]))
2947 {
2948 lastarg = i;
2949 goto fail_1;
2950 }
2951 }
2952 lastarg = argc;
2953 argvlist[argc] = NULL;
2954
2955 i = PyMapping_Size(env);
2956 if (i < 0)
2957 goto fail_1;
2958 envlist = PyMem_NEW(char *, i + 1);
2959 if (envlist == NULL) {
2960 PyErr_NoMemory();
2961 goto fail_1;
2962 }
2963 envc = 0;
2964 keys = PyMapping_Keys(env);
2965 vals = PyMapping_Values(env);
2966 if (!keys || !vals)
2967 goto fail_2;
2968 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2969 PyErr_SetString(PyExc_TypeError,
2970 "spawnvpe(): env.keys() or env.values() is not a list");
2971 goto fail_2;
2972 }
2973
2974 for (pos = 0; pos < i; pos++) {
2975 char *p, *k, *v;
2976 size_t len;
2977
2978 key = PyList_GetItem(keys, pos);
2979 val = PyList_GetItem(vals, pos);
2980 if (!key || !val)
2981 goto fail_2;
2982
2983 if (!PyArg_Parse(
2984 key,
2985 "s;spawnvpe() arg 3 contains a non-string key",
2986 &k) ||
2987 !PyArg_Parse(
2988 val,
2989 "s;spawnvpe() arg 3 contains a non-string value",
2990 &v))
2991 {
2992 goto fail_2;
2993 }
2994 len = PyString_Size(key) + PyString_Size(val) + 2;
2995 p = PyMem_NEW(char, len);
2996 if (p == NULL) {
2997 PyErr_NoMemory();
2998 goto fail_2;
2999 }
3000 PyOS_snprintf(p, len, "%s=%s", k, v);
3001 envlist[envc++] = p;
3002 }
3003 envlist[envc] = 0;
3004
3005 Py_BEGIN_ALLOW_THREADS
3006#if defined(PYCC_GCC)
3007 spawnval = spawnve(mode, path, argvlist, envlist);
3008#else
3009 spawnval = _spawnve(mode, path, argvlist, envlist);
3010#endif
3011 Py_END_ALLOW_THREADS
3012
3013 if (spawnval == -1)
3014 (void) posix_error();
3015 else
3016 res = Py_BuildValue("l", (long) spawnval);
3017
3018 fail_2:
3019 while (--envc >= 0)
3020 PyMem_DEL(envlist[envc]);
3021 PyMem_DEL(envlist);
3022 fail_1:
3023 free_string_array(argvlist, lastarg);
3024 Py_XDECREF(vals);
3025 Py_XDECREF(keys);
3026 fail_0:
3027 PyMem_Free(path);
3028 return res;
3029}
3030#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003031#endif /* HAVE_SPAWNV */
3032
3033
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003034#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003035PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003036"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003037Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3038\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003039Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003040
3041static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003042posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003043{
Neal Norwitze241ce82003-02-17 18:17:05 +00003044 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003045 if (pid == -1)
3046 return posix_error();
3047 PyOS_AfterFork();
3048 return PyInt_FromLong((long)pid);
3049}
3050#endif
3051
3052
Guido van Rossumad0ee831995-03-01 10:34:45 +00003053#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003054PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003055"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003056Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003057Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003058
Barry Warsaw53699e91996-12-10 23:23:01 +00003059static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003060posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003061{
Neal Norwitze241ce82003-02-17 18:17:05 +00003062 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003063 if (pid == -1)
3064 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003065 if (pid == 0)
3066 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003067 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003068}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003069#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003070
Neal Norwitzb59798b2003-03-21 01:43:31 +00003071/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003072/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3073#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003074#define DEV_PTY_FILE "/dev/ptc"
3075#define HAVE_DEV_PTMX
3076#else
3077#define DEV_PTY_FILE "/dev/ptmx"
3078#endif
3079
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003080#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003081#ifdef HAVE_PTY_H
3082#include <pty.h>
3083#else
3084#ifdef HAVE_LIBUTIL_H
3085#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003086#endif /* HAVE_LIBUTIL_H */
3087#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003088#ifdef HAVE_STROPTS_H
3089#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003090#endif
3091#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003092
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003093#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003094PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003095"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003097
3098static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003099posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003100{
3101 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003102#ifndef HAVE_OPENPTY
3103 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003104#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003105#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003106 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003107#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003108 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003109#endif
3110#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003111
Thomas Wouters70c21a12000-07-14 14:28:33 +00003112#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003113 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3114 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003115#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003116 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3117 if (slave_name == NULL)
3118 return posix_error();
3119
3120 slave_fd = open(slave_name, O_RDWR);
3121 if (slave_fd < 0)
3122 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003123#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003124 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003125 if (master_fd < 0)
3126 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003127 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003128 /* change permission of slave */
3129 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003130 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003131 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003132 }
3133 /* unlock slave */
3134 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003135 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003136 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003137 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003138 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003139 slave_name = ptsname(master_fd); /* get name of slave */
3140 if (slave_name == NULL)
3141 return posix_error();
3142 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3143 if (slave_fd < 0)
3144 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003145#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003146 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3147 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003148#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003149 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003150#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003151#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003152#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003153
Fred Drake8cef4cf2000-06-28 16:40:38 +00003154 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003155
Fred Drake8cef4cf2000-06-28 16:40:38 +00003156}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003157#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003158
3159#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003160PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003161"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003162Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3163Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003165
3166static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003167posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003168{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003169 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003170
Fred Drake8cef4cf2000-06-28 16:40:38 +00003171 pid = forkpty(&master_fd, NULL, NULL, NULL);
3172 if (pid == -1)
3173 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003174 if (pid == 0)
3175 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003176 return Py_BuildValue("(ii)", pid, master_fd);
3177}
3178#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003179
Guido van Rossumad0ee831995-03-01 10:34:45 +00003180#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003181PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003182"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003183Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003184
Barry Warsaw53699e91996-12-10 23:23:01 +00003185static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003186posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003187{
Barry Warsaw53699e91996-12-10 23:23:01 +00003188 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003189}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003190#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003191
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003192
Guido van Rossumad0ee831995-03-01 10:34:45 +00003193#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003194PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003195"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003196Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003197
Barry Warsaw53699e91996-12-10 23:23:01 +00003198static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003199posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003200{
Barry Warsaw53699e91996-12-10 23:23:01 +00003201 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003202}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003203#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003204
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003205
Guido van Rossumad0ee831995-03-01 10:34:45 +00003206#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003207PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003208"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003209Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210
Barry Warsaw53699e91996-12-10 23:23:01 +00003211static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003212posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003213{
Barry Warsaw53699e91996-12-10 23:23:01 +00003214 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003215}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003216#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003217
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003219PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003220"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222
Barry Warsaw53699e91996-12-10 23:23:01 +00003223static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003224posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003225{
Barry Warsaw53699e91996-12-10 23:23:01 +00003226 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003227}
3228
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003229
Fred Drakec9680921999-12-13 16:37:25 +00003230#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003231PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003232"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003233Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003234
3235static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003236posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003237{
3238 PyObject *result = NULL;
3239
Fred Drakec9680921999-12-13 16:37:25 +00003240#ifdef NGROUPS_MAX
3241#define MAX_GROUPS NGROUPS_MAX
3242#else
3243 /* defined to be 16 on Solaris7, so this should be a small number */
3244#define MAX_GROUPS 64
3245#endif
3246 gid_t grouplist[MAX_GROUPS];
3247 int n;
3248
3249 n = getgroups(MAX_GROUPS, grouplist);
3250 if (n < 0)
3251 posix_error();
3252 else {
3253 result = PyList_New(n);
3254 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003255 int i;
3256 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003257 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003258 if (o == NULL) {
3259 Py_DECREF(result);
3260 result = NULL;
3261 break;
3262 }
3263 PyList_SET_ITEM(result, i, o);
3264 }
3265 }
3266 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003267
Fred Drakec9680921999-12-13 16:37:25 +00003268 return result;
3269}
3270#endif
3271
Martin v. Löwis606edc12002-06-13 21:09:11 +00003272#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003273PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003274"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003275Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003276
3277static PyObject *
3278posix_getpgid(PyObject *self, PyObject *args)
3279{
3280 int pid, pgid;
3281 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3282 return NULL;
3283 pgid = getpgid(pid);
3284 if (pgid < 0)
3285 return posix_error();
3286 return PyInt_FromLong((long)pgid);
3287}
3288#endif /* HAVE_GETPGID */
3289
3290
Guido van Rossumb6775db1994-08-01 11:34:53 +00003291#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003292PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003293"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003294Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003295
Barry Warsaw53699e91996-12-10 23:23:01 +00003296static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003297posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003298{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003299#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003300 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003301#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003302 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003303#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003304}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003305#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003306
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003307
Guido van Rossumb6775db1994-08-01 11:34:53 +00003308#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003309PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003310"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003311Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003312
Barry Warsaw53699e91996-12-10 23:23:01 +00003313static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003314posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003315{
Guido van Rossum64933891994-10-20 21:56:42 +00003316#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003317 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003318#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003319 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003320#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003321 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003322 Py_INCREF(Py_None);
3323 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003324}
3325
Guido van Rossumb6775db1994-08-01 11:34:53 +00003326#endif /* HAVE_SETPGRP */
3327
Guido van Rossumad0ee831995-03-01 10:34:45 +00003328#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003329PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003330"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003331Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003332
Barry Warsaw53699e91996-12-10 23:23:01 +00003333static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003334posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003335{
Barry Warsaw53699e91996-12-10 23:23:01 +00003336 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003337}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003338#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003340
Fred Drake12c6e2d1999-12-14 21:25:03 +00003341#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003342PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003343"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003344Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003345
3346static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003347posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003348{
Neal Norwitze241ce82003-02-17 18:17:05 +00003349 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003350 char *name;
3351 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003352
Fred Drakea30680b2000-12-06 21:24:28 +00003353 errno = 0;
3354 name = getlogin();
3355 if (name == NULL) {
3356 if (errno)
3357 posix_error();
3358 else
3359 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003360 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003361 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003362 else
3363 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003364 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003365
Fred Drake12c6e2d1999-12-14 21:25:03 +00003366 return result;
3367}
3368#endif
3369
Guido van Rossumad0ee831995-03-01 10:34:45 +00003370#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003371PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003372"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003373Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Barry Warsaw53699e91996-12-10 23:23:01 +00003375static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003376posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003377{
Barry Warsaw53699e91996-12-10 23:23:01 +00003378 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003379}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003380#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003381
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003382
Guido van Rossumad0ee831995-03-01 10:34:45 +00003383#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003384PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003385"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003386Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003387
Barry Warsaw53699e91996-12-10 23:23:01 +00003388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003389posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003390{
3391 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003392 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003393 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003394#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003395 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3396 APIRET rc;
3397 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003398 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003399
3400 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3401 APIRET rc;
3402 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003403 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003404
3405 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003406 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003407#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003408 if (kill(pid, sig) == -1)
3409 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003410#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003411 Py_INCREF(Py_None);
3412 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003413}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003414#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003415
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003416#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003417PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003418"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003419Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003420
3421static PyObject *
3422posix_killpg(PyObject *self, PyObject *args)
3423{
3424 int pgid, sig;
3425 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3426 return NULL;
3427 if (killpg(pgid, sig) == -1)
3428 return posix_error();
3429 Py_INCREF(Py_None);
3430 return Py_None;
3431}
3432#endif
3433
Guido van Rossumc0125471996-06-28 18:55:32 +00003434#ifdef HAVE_PLOCK
3435
3436#ifdef HAVE_SYS_LOCK_H
3437#include <sys/lock.h>
3438#endif
3439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003440PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003441"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003442Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003443
Barry Warsaw53699e91996-12-10 23:23:01 +00003444static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003445posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003446{
3447 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003448 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003449 return NULL;
3450 if (plock(op) == -1)
3451 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003452 Py_INCREF(Py_None);
3453 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003454}
3455#endif
3456
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003457
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003458#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003459PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003460"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003461Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003462
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003463#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003464#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003465static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003466async_system(const char *command)
3467{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003468 char errormsg[256], args[1024];
3469 RESULTCODES rcodes;
3470 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003471
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003472 char *shell = getenv("COMSPEC");
3473 if (!shell)
3474 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003475
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003476 /* avoid overflowing the argument buffer */
3477 if (strlen(shell) + 3 + strlen(command) >= 1024)
3478 return ERROR_NOT_ENOUGH_MEMORY
3479
3480 args[0] = '\0';
3481 strcat(args, shell);
3482 strcat(args, "/c ");
3483 strcat(args, command);
3484
3485 /* execute asynchronously, inheriting the environment */
3486 rc = DosExecPgm(errormsg,
3487 sizeof(errormsg),
3488 EXEC_ASYNC,
3489 args,
3490 NULL,
3491 &rcodes,
3492 shell);
3493 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003494}
3495
Guido van Rossumd48f2521997-12-05 22:19:34 +00003496static FILE *
3497popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003498{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003499 int oldfd, tgtfd;
3500 HFILE pipeh[2];
3501 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003502
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003503 /* mode determines which of stdin or stdout is reconnected to
3504 * the pipe to the child
3505 */
3506 if (strchr(mode, 'r') != NULL) {
3507 tgt_fd = 1; /* stdout */
3508 } else if (strchr(mode, 'w')) {
3509 tgt_fd = 0; /* stdin */
3510 } else {
3511 *err = ERROR_INVALID_ACCESS;
3512 return NULL;
3513 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003514
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003515 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003516 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3517 *err = rc;
3518 return NULL;
3519 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003520
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003521 /* prevent other threads accessing stdio */
3522 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003523
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003524 /* reconnect stdio and execute child */
3525 oldfd = dup(tgtfd);
3526 close(tgtfd);
3527 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3528 DosClose(pipeh[tgtfd]);
3529 rc = async_system(command);
3530 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003531
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003532 /* restore stdio */
3533 dup2(oldfd, tgtfd);
3534 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003535
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003536 /* allow other threads access to stdio */
3537 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003538
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003539 /* if execution of child was successful return file stream */
3540 if (rc == NO_ERROR)
3541 return fdopen(pipeh[1 - tgtfd], mode);
3542 else {
3543 DosClose(pipeh[1 - tgtfd]);
3544 *err = rc;
3545 return NULL;
3546 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003547}
3548
3549static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003550posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003551{
3552 char *name;
3553 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003554 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003555 FILE *fp;
3556 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003557 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003558 return NULL;
3559 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003560 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003561 Py_END_ALLOW_THREADS
3562 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003563 return os2_error(err);
3564
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003565 f = PyFile_FromFile(fp, name, mode, fclose);
3566 if (f != NULL)
3567 PyFile_SetBufSize(f, bufsize);
3568 return f;
3569}
3570
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003571#elif defined(PYCC_GCC)
3572
3573/* standard posix version of popen() support */
3574static PyObject *
3575posix_popen(PyObject *self, PyObject *args)
3576{
3577 char *name;
3578 char *mode = "r";
3579 int bufsize = -1;
3580 FILE *fp;
3581 PyObject *f;
3582 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3583 return NULL;
3584 Py_BEGIN_ALLOW_THREADS
3585 fp = popen(name, mode);
3586 Py_END_ALLOW_THREADS
3587 if (fp == NULL)
3588 return posix_error();
3589 f = PyFile_FromFile(fp, name, mode, pclose);
3590 if (f != NULL)
3591 PyFile_SetBufSize(f, bufsize);
3592 return f;
3593}
3594
3595/* fork() under OS/2 has lots'o'warts
3596 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3597 * most of this code is a ripoff of the win32 code, but using the
3598 * capabilities of EMX's C library routines
3599 */
3600
3601/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3602#define POPEN_1 1
3603#define POPEN_2 2
3604#define POPEN_3 3
3605#define POPEN_4 4
3606
3607static PyObject *_PyPopen(char *, int, int, int);
3608static int _PyPclose(FILE *file);
3609
3610/*
3611 * Internal dictionary mapping popen* file pointers to process handles,
3612 * for use when retrieving the process exit code. See _PyPclose() below
3613 * for more information on this dictionary's use.
3614 */
3615static PyObject *_PyPopenProcs = NULL;
3616
3617/* os2emx version of popen2()
3618 *
3619 * The result of this function is a pipe (file) connected to the
3620 * process's stdin, and a pipe connected to the process's stdout.
3621 */
3622
3623static PyObject *
3624os2emx_popen2(PyObject *self, PyObject *args)
3625{
3626 PyObject *f;
3627 int tm=0;
3628
3629 char *cmdstring;
3630 char *mode = "t";
3631 int bufsize = -1;
3632 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3633 return NULL;
3634
3635 if (*mode == 't')
3636 tm = O_TEXT;
3637 else if (*mode != 'b') {
3638 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3639 return NULL;
3640 } else
3641 tm = O_BINARY;
3642
3643 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3644
3645 return f;
3646}
3647
3648/*
3649 * Variation on os2emx.popen2
3650 *
3651 * The result of this function is 3 pipes - the process's stdin,
3652 * stdout and stderr
3653 */
3654
3655static PyObject *
3656os2emx_popen3(PyObject *self, PyObject *args)
3657{
3658 PyObject *f;
3659 int tm = 0;
3660
3661 char *cmdstring;
3662 char *mode = "t";
3663 int bufsize = -1;
3664 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3665 return NULL;
3666
3667 if (*mode == 't')
3668 tm = O_TEXT;
3669 else if (*mode != 'b') {
3670 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3671 return NULL;
3672 } else
3673 tm = O_BINARY;
3674
3675 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3676
3677 return f;
3678}
3679
3680/*
3681 * Variation on os2emx.popen2
3682 *
Tim Peters11b23062003-04-23 02:39:17 +00003683 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003684 * and stdout+stderr combined as a single pipe.
3685 */
3686
3687static PyObject *
3688os2emx_popen4(PyObject *self, PyObject *args)
3689{
3690 PyObject *f;
3691 int tm = 0;
3692
3693 char *cmdstring;
3694 char *mode = "t";
3695 int bufsize = -1;
3696 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3697 return NULL;
3698
3699 if (*mode == 't')
3700 tm = O_TEXT;
3701 else if (*mode != 'b') {
3702 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3703 return NULL;
3704 } else
3705 tm = O_BINARY;
3706
3707 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3708
3709 return f;
3710}
3711
3712/* a couple of structures for convenient handling of multiple
3713 * file handles and pipes
3714 */
3715struct file_ref
3716{
3717 int handle;
3718 int flags;
3719};
3720
3721struct pipe_ref
3722{
3723 int rd;
3724 int wr;
3725};
3726
3727/* The following code is derived from the win32 code */
3728
3729static PyObject *
3730_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3731{
3732 struct file_ref stdio[3];
3733 struct pipe_ref p_fd[3];
3734 FILE *p_s[3];
3735 int file_count, i, pipe_err, pipe_pid;
3736 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3737 PyObject *f, *p_f[3];
3738
3739 /* file modes for subsequent fdopen's on pipe handles */
3740 if (mode == O_TEXT)
3741 {
3742 rd_mode = "rt";
3743 wr_mode = "wt";
3744 }
3745 else
3746 {
3747 rd_mode = "rb";
3748 wr_mode = "wb";
3749 }
3750
3751 /* prepare shell references */
3752 if ((shell = getenv("EMXSHELL")) == NULL)
3753 if ((shell = getenv("COMSPEC")) == NULL)
3754 {
3755 errno = ENOENT;
3756 return posix_error();
3757 }
3758
3759 sh_name = _getname(shell);
3760 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3761 opt = "/c";
3762 else
3763 opt = "-c";
3764
3765 /* save current stdio fds + their flags, and set not inheritable */
3766 i = pipe_err = 0;
3767 while (pipe_err >= 0 && i < 3)
3768 {
3769 pipe_err = stdio[i].handle = dup(i);
3770 stdio[i].flags = fcntl(i, F_GETFD, 0);
3771 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3772 i++;
3773 }
3774 if (pipe_err < 0)
3775 {
3776 /* didn't get them all saved - clean up and bail out */
3777 int saved_err = errno;
3778 while (i-- > 0)
3779 {
3780 close(stdio[i].handle);
3781 }
3782 errno = saved_err;
3783 return posix_error();
3784 }
3785
3786 /* create pipe ends */
3787 file_count = 2;
3788 if (n == POPEN_3)
3789 file_count = 3;
3790 i = pipe_err = 0;
3791 while ((pipe_err == 0) && (i < file_count))
3792 pipe_err = pipe((int *)&p_fd[i++]);
3793 if (pipe_err < 0)
3794 {
3795 /* didn't get them all made - clean up and bail out */
3796 while (i-- > 0)
3797 {
3798 close(p_fd[i].wr);
3799 close(p_fd[i].rd);
3800 }
3801 errno = EPIPE;
3802 return posix_error();
3803 }
3804
3805 /* change the actual standard IO streams over temporarily,
3806 * making the retained pipe ends non-inheritable
3807 */
3808 pipe_err = 0;
3809
3810 /* - stdin */
3811 if (dup2(p_fd[0].rd, 0) == 0)
3812 {
3813 close(p_fd[0].rd);
3814 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3815 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3816 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3817 {
3818 close(p_fd[0].wr);
3819 pipe_err = -1;
3820 }
3821 }
3822 else
3823 {
3824 pipe_err = -1;
3825 }
3826
3827 /* - stdout */
3828 if (pipe_err == 0)
3829 {
3830 if (dup2(p_fd[1].wr, 1) == 1)
3831 {
3832 close(p_fd[1].wr);
3833 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3834 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3835 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3836 {
3837 close(p_fd[1].rd);
3838 pipe_err = -1;
3839 }
3840 }
3841 else
3842 {
3843 pipe_err = -1;
3844 }
3845 }
3846
3847 /* - stderr, as required */
3848 if (pipe_err == 0)
3849 switch (n)
3850 {
3851 case POPEN_3:
3852 {
3853 if (dup2(p_fd[2].wr, 2) == 2)
3854 {
3855 close(p_fd[2].wr);
3856 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3857 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3858 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3859 {
3860 close(p_fd[2].rd);
3861 pipe_err = -1;
3862 }
3863 }
3864 else
3865 {
3866 pipe_err = -1;
3867 }
3868 break;
3869 }
3870
3871 case POPEN_4:
3872 {
3873 if (dup2(1, 2) != 2)
3874 {
3875 pipe_err = -1;
3876 }
3877 break;
3878 }
3879 }
3880
3881 /* spawn the child process */
3882 if (pipe_err == 0)
3883 {
3884 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3885 if (pipe_pid == -1)
3886 {
3887 pipe_err = -1;
3888 }
3889 else
3890 {
3891 /* save the PID into the FILE structure
3892 * NOTE: this implementation doesn't actually
3893 * take advantage of this, but do it for
3894 * completeness - AIM Apr01
3895 */
3896 for (i = 0; i < file_count; i++)
3897 p_s[i]->_pid = pipe_pid;
3898 }
3899 }
3900
3901 /* reset standard IO to normal */
3902 for (i = 0; i < 3; i++)
3903 {
3904 dup2(stdio[i].handle, i);
3905 fcntl(i, F_SETFD, stdio[i].flags);
3906 close(stdio[i].handle);
3907 }
3908
3909 /* if any remnant problems, clean up and bail out */
3910 if (pipe_err < 0)
3911 {
3912 for (i = 0; i < 3; i++)
3913 {
3914 close(p_fd[i].rd);
3915 close(p_fd[i].wr);
3916 }
3917 errno = EPIPE;
3918 return posix_error_with_filename(cmdstring);
3919 }
3920
3921 /* build tuple of file objects to return */
3922 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3923 PyFile_SetBufSize(p_f[0], bufsize);
3924 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3925 PyFile_SetBufSize(p_f[1], bufsize);
3926 if (n == POPEN_3)
3927 {
3928 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3929 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003930 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003931 }
3932 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003933 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003934
3935 /*
3936 * Insert the files we've created into the process dictionary
3937 * all referencing the list with the process handle and the
3938 * initial number of files (see description below in _PyPclose).
3939 * Since if _PyPclose later tried to wait on a process when all
3940 * handles weren't closed, it could create a deadlock with the
3941 * child, we spend some energy here to try to ensure that we
3942 * either insert all file handles into the dictionary or none
3943 * at all. It's a little clumsy with the various popen modes
3944 * and variable number of files involved.
3945 */
3946 if (!_PyPopenProcs)
3947 {
3948 _PyPopenProcs = PyDict_New();
3949 }
3950
3951 if (_PyPopenProcs)
3952 {
3953 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3954 int ins_rc[3];
3955
3956 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3957 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3958
3959 procObj = PyList_New(2);
3960 pidObj = PyInt_FromLong((long) pipe_pid);
3961 intObj = PyInt_FromLong((long) file_count);
3962
3963 if (procObj && pidObj && intObj)
3964 {
3965 PyList_SetItem(procObj, 0, pidObj);
3966 PyList_SetItem(procObj, 1, intObj);
3967
3968 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3969 if (fileObj[0])
3970 {
3971 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3972 fileObj[0],
3973 procObj);
3974 }
3975 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3976 if (fileObj[1])
3977 {
3978 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3979 fileObj[1],
3980 procObj);
3981 }
3982 if (file_count >= 3)
3983 {
3984 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3985 if (fileObj[2])
3986 {
3987 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3988 fileObj[2],
3989 procObj);
3990 }
3991 }
3992
3993 if (ins_rc[0] < 0 || !fileObj[0] ||
3994 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3995 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3996 {
3997 /* Something failed - remove any dictionary
3998 * entries that did make it.
3999 */
4000 if (!ins_rc[0] && fileObj[0])
4001 {
4002 PyDict_DelItem(_PyPopenProcs,
4003 fileObj[0]);
4004 }
4005 if (!ins_rc[1] && fileObj[1])
4006 {
4007 PyDict_DelItem(_PyPopenProcs,
4008 fileObj[1]);
4009 }
4010 if (!ins_rc[2] && fileObj[2])
4011 {
4012 PyDict_DelItem(_PyPopenProcs,
4013 fileObj[2]);
4014 }
4015 }
4016 }
Tim Peters11b23062003-04-23 02:39:17 +00004017
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004018 /*
4019 * Clean up our localized references for the dictionary keys
4020 * and value since PyDict_SetItem will Py_INCREF any copies
4021 * that got placed in the dictionary.
4022 */
4023 Py_XDECREF(procObj);
4024 Py_XDECREF(fileObj[0]);
4025 Py_XDECREF(fileObj[1]);
4026 Py_XDECREF(fileObj[2]);
4027 }
4028
4029 /* Child is launched. */
4030 return f;
4031}
4032
4033/*
4034 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4035 * exit code for the child process and return as a result of the close.
4036 *
4037 * This function uses the _PyPopenProcs dictionary in order to map the
4038 * input file pointer to information about the process that was
4039 * originally created by the popen* call that created the file pointer.
4040 * The dictionary uses the file pointer as a key (with one entry
4041 * inserted for each file returned by the original popen* call) and a
4042 * single list object as the value for all files from a single call.
4043 * The list object contains the Win32 process handle at [0], and a file
4044 * count at [1], which is initialized to the total number of file
4045 * handles using that list.
4046 *
4047 * This function closes whichever handle it is passed, and decrements
4048 * the file count in the dictionary for the process handle pointed to
4049 * by this file. On the last close (when the file count reaches zero),
4050 * this function will wait for the child process and then return its
4051 * exit code as the result of the close() operation. This permits the
4052 * files to be closed in any order - it is always the close() of the
4053 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004054 *
4055 * NOTE: This function is currently called with the GIL released.
4056 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004057 */
4058
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004059static int _PyPclose(FILE *file)
4060{
4061 int result;
4062 int exit_code;
4063 int pipe_pid;
4064 PyObject *procObj, *pidObj, *intObj, *fileObj;
4065 int file_count;
4066#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004067 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004068#endif
4069
4070 /* Close the file handle first, to ensure it can't block the
4071 * child from exiting if it's the last handle.
4072 */
4073 result = fclose(file);
4074
4075#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004076 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004077#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004078 if (_PyPopenProcs)
4079 {
4080 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4081 (procObj = PyDict_GetItem(_PyPopenProcs,
4082 fileObj)) != NULL &&
4083 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4084 (intObj = PyList_GetItem(procObj,1)) != NULL)
4085 {
4086 pipe_pid = (int) PyInt_AsLong(pidObj);
4087 file_count = (int) PyInt_AsLong(intObj);
4088
4089 if (file_count > 1)
4090 {
4091 /* Still other files referencing process */
4092 file_count--;
4093 PyList_SetItem(procObj,1,
4094 PyInt_FromLong((long) file_count));
4095 }
4096 else
4097 {
4098 /* Last file for this process */
4099 if (result != EOF &&
4100 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4101 {
4102 /* extract exit status */
4103 if (WIFEXITED(exit_code))
4104 {
4105 result = WEXITSTATUS(exit_code);
4106 }
4107 else
4108 {
4109 errno = EPIPE;
4110 result = -1;
4111 }
4112 }
4113 else
4114 {
4115 /* Indicate failure - this will cause the file object
4116 * to raise an I/O error and translate the last
4117 * error code from errno. We do have a problem with
4118 * last errors that overlap the normal errno table,
4119 * but that's a consistent problem with the file object.
4120 */
4121 result = -1;
4122 }
4123 }
4124
4125 /* Remove this file pointer from dictionary */
4126 PyDict_DelItem(_PyPopenProcs, fileObj);
4127
4128 if (PyDict_Size(_PyPopenProcs) == 0)
4129 {
4130 Py_DECREF(_PyPopenProcs);
4131 _PyPopenProcs = NULL;
4132 }
4133
4134 } /* if object retrieval ok */
4135
4136 Py_XDECREF(fileObj);
4137 } /* if _PyPopenProcs */
4138
4139#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004140 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004141#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004142 return result;
4143}
4144
4145#endif /* PYCC_??? */
4146
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004147#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004148
4149/*
4150 * Portable 'popen' replacement for Win32.
4151 *
4152 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4153 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004154 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004155 */
4156
4157#include <malloc.h>
4158#include <io.h>
4159#include <fcntl.h>
4160
4161/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4162#define POPEN_1 1
4163#define POPEN_2 2
4164#define POPEN_3 3
4165#define POPEN_4 4
4166
4167static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004168static int _PyPclose(FILE *file);
4169
4170/*
4171 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004172 * for use when retrieving the process exit code. See _PyPclose() below
4173 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004174 */
4175static PyObject *_PyPopenProcs = NULL;
4176
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004177
4178/* popen that works from a GUI.
4179 *
4180 * The result of this function is a pipe (file) connected to the
4181 * processes stdin or stdout, depending on the requested mode.
4182 */
4183
4184static PyObject *
4185posix_popen(PyObject *self, PyObject *args)
4186{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004187 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004188 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004189
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004190 char *cmdstring;
4191 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004192 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004193 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004194 return NULL;
4195
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004196 if (*mode == 'r')
4197 tm = _O_RDONLY;
4198 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004199 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004200 return NULL;
4201 } else
4202 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004203
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004204 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004205 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004206 return NULL;
4207 }
4208
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004209 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004210 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004211 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004212 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004213 else
4214 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4215
4216 return f;
4217}
4218
4219/* Variation on win32pipe.popen
4220 *
4221 * The result of this function is a pipe (file) connected to the
4222 * process's stdin, and a pipe connected to the process's stdout.
4223 */
4224
4225static PyObject *
4226win32_popen2(PyObject *self, PyObject *args)
4227{
4228 PyObject *f;
4229 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004230
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004231 char *cmdstring;
4232 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004233 int bufsize = -1;
4234 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004235 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004236
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004237 if (*mode == 't')
4238 tm = _O_TEXT;
4239 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004240 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004241 return NULL;
4242 } else
4243 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004244
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004245 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004246 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004247 return NULL;
4248 }
4249
4250 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004251
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004252 return f;
4253}
4254
4255/*
4256 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004257 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004258 * The result of this function is 3 pipes - the process's stdin,
4259 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004260 */
4261
4262static PyObject *
4263win32_popen3(PyObject *self, PyObject *args)
4264{
4265 PyObject *f;
4266 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004267
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004268 char *cmdstring;
4269 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004270 int bufsize = -1;
4271 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004272 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004273
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004274 if (*mode == 't')
4275 tm = _O_TEXT;
4276 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004277 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004278 return NULL;
4279 } else
4280 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004281
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004282 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004283 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004284 return NULL;
4285 }
4286
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004287 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004288
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004289 return f;
4290}
4291
4292/*
4293 * Variation on win32pipe.popen
4294 *
Tim Peters5aa91602002-01-30 05:46:57 +00004295 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004296 * and stdout+stderr combined as a single pipe.
4297 */
4298
4299static PyObject *
4300win32_popen4(PyObject *self, PyObject *args)
4301{
4302 PyObject *f;
4303 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004304
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004305 char *cmdstring;
4306 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004307 int bufsize = -1;
4308 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004309 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004310
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004311 if (*mode == 't')
4312 tm = _O_TEXT;
4313 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004314 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004315 return NULL;
4316 } else
4317 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004318
4319 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004320 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004321 return NULL;
4322 }
4323
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004324 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004325
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004326 return f;
4327}
4328
Mark Hammond08501372001-01-31 07:30:29 +00004329static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004330_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004331 HANDLE hStdin,
4332 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004333 HANDLE hStderr,
4334 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004335{
4336 PROCESS_INFORMATION piProcInfo;
4337 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004338 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004339 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004340 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004341 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004342 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004343
4344 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004345 char *comshell;
4346
Tim Peters92e4dd82002-10-05 01:47:34 +00004347 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004348 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004349 /* x < i, so x fits into an integer */
4350 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004351
4352 /* Explicitly check if we are using COMMAND.COM. If we are
4353 * then use the w9xpopen hack.
4354 */
4355 comshell = s1 + x;
4356 while (comshell >= s1 && *comshell != '\\')
4357 --comshell;
4358 ++comshell;
4359
4360 if (GetVersion() < 0x80000000 &&
4361 _stricmp(comshell, "command.com") != 0) {
4362 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004363 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004364 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004365 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004366 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004367 }
4368 else {
4369 /*
Tim Peters402d5982001-08-27 06:37:48 +00004370 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4371 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004372 */
Mark Hammond08501372001-01-31 07:30:29 +00004373 char modulepath[_MAX_PATH];
4374 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004375 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
4376 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004377 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004378 x = i+1;
4379 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004380 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004381 strncat(modulepath,
4382 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004383 (sizeof(modulepath)/sizeof(modulepath[0]))
4384 -strlen(modulepath));
4385 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004386 /* Eeek - file-not-found - possibly an embedding
4387 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004388 */
Tim Peters5aa91602002-01-30 05:46:57 +00004389 strncpy(modulepath,
4390 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00004391 sizeof(modulepath)/sizeof(modulepath[0]));
4392 if (modulepath[strlen(modulepath)-1] != '\\')
4393 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004394 strncat(modulepath,
4395 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004396 (sizeof(modulepath)/sizeof(modulepath[0]))
4397 -strlen(modulepath));
4398 /* No where else to look - raise an easily identifiable
4399 error, rather than leaving Windows to report
4400 "file not found" - as the user is probably blissfully
4401 unaware this shim EXE is used, and it will confuse them.
4402 (well, it confused me for a while ;-)
4403 */
4404 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004405 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004406 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004407 "for popen to work with your shell "
4408 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004409 szConsoleSpawn);
4410 return FALSE;
4411 }
4412 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004413 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004414 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004415 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004416
Tim Peters92e4dd82002-10-05 01:47:34 +00004417 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004418 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004419 /* To maintain correct argument passing semantics,
4420 we pass the command-line as it stands, and allow
4421 quoting to be applied. w9xpopen.exe will then
4422 use its argv vector, and re-quote the necessary
4423 args for the ultimate child process.
4424 */
Tim Peters75cdad52001-11-28 22:07:30 +00004425 PyOS_snprintf(
4426 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004427 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004428 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004429 s1,
4430 s3,
4431 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004432 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004433 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004434 dialog:
4435 "Your program accessed mem currently in use at xxx"
4436 and a hopeful warning about the stability of your
4437 system.
4438 Cost is Ctrl+C wont kill children, but anyone
4439 who cares can have a go!
4440 */
4441 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004442 }
4443 }
4444
4445 /* Could be an else here to try cmd.exe / command.com in the path
4446 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004447 else {
Tim Peters402d5982001-08-27 06:37:48 +00004448 PyErr_SetString(PyExc_RuntimeError,
4449 "Cannot locate a COMSPEC environment variable to "
4450 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004451 return FALSE;
4452 }
Tim Peters5aa91602002-01-30 05:46:57 +00004453
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004454 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4455 siStartInfo.cb = sizeof(STARTUPINFO);
4456 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4457 siStartInfo.hStdInput = hStdin;
4458 siStartInfo.hStdOutput = hStdout;
4459 siStartInfo.hStdError = hStderr;
4460 siStartInfo.wShowWindow = SW_HIDE;
4461
4462 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004463 s2,
4464 NULL,
4465 NULL,
4466 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004467 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004468 NULL,
4469 NULL,
4470 &siStartInfo,
4471 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004472 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004473 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004474
Mark Hammondb37a3732000-08-14 04:47:33 +00004475 /* Return process handle */
4476 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004477 return TRUE;
4478 }
Tim Peters402d5982001-08-27 06:37:48 +00004479 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004480 return FALSE;
4481}
4482
4483/* The following code is based off of KB: Q190351 */
4484
4485static PyObject *
4486_PyPopen(char *cmdstring, int mode, int n)
4487{
4488 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4489 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004490 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004491
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004492 SECURITY_ATTRIBUTES saAttr;
4493 BOOL fSuccess;
4494 int fd1, fd2, fd3;
4495 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004496 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004497 PyObject *f;
4498
4499 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4500 saAttr.bInheritHandle = TRUE;
4501 saAttr.lpSecurityDescriptor = NULL;
4502
4503 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4504 return win32_error("CreatePipe", NULL);
4505
4506 /* Create new output read handle and the input write handle. Set
4507 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004508 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004509 * being created. */
4510 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004511 GetCurrentProcess(), &hChildStdinWrDup, 0,
4512 FALSE,
4513 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004514 if (!fSuccess)
4515 return win32_error("DuplicateHandle", NULL);
4516
4517 /* Close the inheritable version of ChildStdin
4518 that we're using. */
4519 CloseHandle(hChildStdinWr);
4520
4521 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4522 return win32_error("CreatePipe", NULL);
4523
4524 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004525 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4526 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004527 if (!fSuccess)
4528 return win32_error("DuplicateHandle", NULL);
4529
4530 /* Close the inheritable version of ChildStdout
4531 that we're using. */
4532 CloseHandle(hChildStdoutRd);
4533
4534 if (n != POPEN_4) {
4535 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4536 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004537 fSuccess = DuplicateHandle(GetCurrentProcess(),
4538 hChildStderrRd,
4539 GetCurrentProcess(),
4540 &hChildStderrRdDup, 0,
4541 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004542 if (!fSuccess)
4543 return win32_error("DuplicateHandle", NULL);
4544 /* Close the inheritable version of ChildStdErr that we're using. */
4545 CloseHandle(hChildStderrRd);
4546 }
Tim Peters5aa91602002-01-30 05:46:57 +00004547
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004548 switch (n) {
4549 case POPEN_1:
4550 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4551 case _O_WRONLY | _O_TEXT:
4552 /* Case for writing to child Stdin in text mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004553 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004554 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004555 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004556 PyFile_SetBufSize(f, 0);
4557 /* We don't care about these pipes anymore, so close them. */
4558 CloseHandle(hChildStdoutRdDup);
4559 CloseHandle(hChildStderrRdDup);
4560 break;
4561
4562 case _O_RDONLY | _O_TEXT:
4563 /* Case for reading from child Stdout in text mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004564 fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004565 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004566 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004567 PyFile_SetBufSize(f, 0);
4568 /* We don't care about these pipes anymore, so close them. */
4569 CloseHandle(hChildStdinWrDup);
4570 CloseHandle(hChildStderrRdDup);
4571 break;
4572
4573 case _O_RDONLY | _O_BINARY:
4574 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004575 fd1 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004576 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004577 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004578 PyFile_SetBufSize(f, 0);
4579 /* We don't care about these pipes anymore, so close them. */
4580 CloseHandle(hChildStdinWrDup);
4581 CloseHandle(hChildStderrRdDup);
4582 break;
4583
4584 case _O_WRONLY | _O_BINARY:
4585 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00004586 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004587 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004588 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004589 PyFile_SetBufSize(f, 0);
4590 /* We don't care about these pipes anymore, so close them. */
4591 CloseHandle(hChildStdoutRdDup);
4592 CloseHandle(hChildStderrRdDup);
4593 break;
4594 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004595 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004596 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004597
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004598 case POPEN_2:
4599 case POPEN_4:
4600 {
4601 char *m1, *m2;
4602 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004603
Tim Peters7dca21e2002-08-19 00:42:29 +00004604 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004605 m1 = "r";
4606 m2 = "w";
4607 } else {
4608 m1 = "rb";
4609 m2 = "wb";
4610 }
4611
Martin v. Löwis18e16552006-02-15 17:27:45 +00004612 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004613 f1 = _fdopen(fd1, m2);
Martin v. Löwis18e16552006-02-15 17:27:45 +00004614 fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004615 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004616 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004617 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004618 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004619 PyFile_SetBufSize(p2, 0);
4620
4621 if (n != 4)
4622 CloseHandle(hChildStderrRdDup);
4623
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004624 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004625 Py_XDECREF(p1);
4626 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004627 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004628 break;
4629 }
Tim Peters5aa91602002-01-30 05:46:57 +00004630
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004631 case POPEN_3:
4632 {
4633 char *m1, *m2;
4634 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004635
Tim Peters7dca21e2002-08-19 00:42:29 +00004636 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004637 m1 = "r";
4638 m2 = "w";
4639 } else {
4640 m1 = "rb";
4641 m2 = "wb";
4642 }
4643
Martin v. Löwis18e16552006-02-15 17:27:45 +00004644 fd1 = _open_osfhandle((intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004645 f1 = _fdopen(fd1, m2);
Martin v. Löwis18e16552006-02-15 17:27:45 +00004646 fd2 = _open_osfhandle((intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004647 f2 = _fdopen(fd2, m1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00004648 fd3 = _open_osfhandle((intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004649 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004650 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004651 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4652 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004653 PyFile_SetBufSize(p1, 0);
4654 PyFile_SetBufSize(p2, 0);
4655 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004656 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004657 Py_XDECREF(p1);
4658 Py_XDECREF(p2);
4659 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004660 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004661 break;
4662 }
4663 }
4664
4665 if (n == POPEN_4) {
4666 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004667 hChildStdinRd,
4668 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004669 hChildStdoutWr,
4670 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004671 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004672 }
4673 else {
4674 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004675 hChildStdinRd,
4676 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004677 hChildStderrWr,
4678 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004679 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004680 }
4681
Mark Hammondb37a3732000-08-14 04:47:33 +00004682 /*
4683 * Insert the files we've created into the process dictionary
4684 * all referencing the list with the process handle and the
4685 * initial number of files (see description below in _PyPclose).
4686 * Since if _PyPclose later tried to wait on a process when all
4687 * handles weren't closed, it could create a deadlock with the
4688 * child, we spend some energy here to try to ensure that we
4689 * either insert all file handles into the dictionary or none
4690 * at all. It's a little clumsy with the various popen modes
4691 * and variable number of files involved.
4692 */
4693 if (!_PyPopenProcs) {
4694 _PyPopenProcs = PyDict_New();
4695 }
4696
4697 if (_PyPopenProcs) {
4698 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4699 int ins_rc[3];
4700
4701 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4702 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4703
4704 procObj = PyList_New(2);
4705 hProcessObj = PyLong_FromVoidPtr(hProcess);
4706 intObj = PyInt_FromLong(file_count);
4707
4708 if (procObj && hProcessObj && intObj) {
4709 PyList_SetItem(procObj,0,hProcessObj);
4710 PyList_SetItem(procObj,1,intObj);
4711
4712 fileObj[0] = PyLong_FromVoidPtr(f1);
4713 if (fileObj[0]) {
4714 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4715 fileObj[0],
4716 procObj);
4717 }
4718 if (file_count >= 2) {
4719 fileObj[1] = PyLong_FromVoidPtr(f2);
4720 if (fileObj[1]) {
4721 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4722 fileObj[1],
4723 procObj);
4724 }
4725 }
4726 if (file_count >= 3) {
4727 fileObj[2] = PyLong_FromVoidPtr(f3);
4728 if (fileObj[2]) {
4729 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4730 fileObj[2],
4731 procObj);
4732 }
4733 }
4734
4735 if (ins_rc[0] < 0 || !fileObj[0] ||
4736 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4737 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4738 /* Something failed - remove any dictionary
4739 * entries that did make it.
4740 */
4741 if (!ins_rc[0] && fileObj[0]) {
4742 PyDict_DelItem(_PyPopenProcs,
4743 fileObj[0]);
4744 }
4745 if (!ins_rc[1] && fileObj[1]) {
4746 PyDict_DelItem(_PyPopenProcs,
4747 fileObj[1]);
4748 }
4749 if (!ins_rc[2] && fileObj[2]) {
4750 PyDict_DelItem(_PyPopenProcs,
4751 fileObj[2]);
4752 }
4753 }
4754 }
Tim Peters5aa91602002-01-30 05:46:57 +00004755
Mark Hammondb37a3732000-08-14 04:47:33 +00004756 /*
4757 * Clean up our localized references for the dictionary keys
4758 * and value since PyDict_SetItem will Py_INCREF any copies
4759 * that got placed in the dictionary.
4760 */
4761 Py_XDECREF(procObj);
4762 Py_XDECREF(fileObj[0]);
4763 Py_XDECREF(fileObj[1]);
4764 Py_XDECREF(fileObj[2]);
4765 }
4766
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004767 /* Child is launched. Close the parents copy of those pipe
4768 * handles that only the child should have open. You need to
4769 * make sure that no handles to the write end of the output pipe
4770 * are maintained in this process or else the pipe will not close
4771 * when the child process exits and the ReadFile will hang. */
4772
4773 if (!CloseHandle(hChildStdinRd))
4774 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004775
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004776 if (!CloseHandle(hChildStdoutWr))
4777 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004778
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004779 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4780 return win32_error("CloseHandle", NULL);
4781
4782 return f;
4783}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004784
4785/*
4786 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4787 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004788 *
4789 * This function uses the _PyPopenProcs dictionary in order to map the
4790 * input file pointer to information about the process that was
4791 * originally created by the popen* call that created the file pointer.
4792 * The dictionary uses the file pointer as a key (with one entry
4793 * inserted for each file returned by the original popen* call) and a
4794 * single list object as the value for all files from a single call.
4795 * The list object contains the Win32 process handle at [0], and a file
4796 * count at [1], which is initialized to the total number of file
4797 * handles using that list.
4798 *
4799 * This function closes whichever handle it is passed, and decrements
4800 * the file count in the dictionary for the process handle pointed to
4801 * by this file. On the last close (when the file count reaches zero),
4802 * this function will wait for the child process and then return its
4803 * exit code as the result of the close() operation. This permits the
4804 * files to be closed in any order - it is always the close() of the
4805 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004806 *
4807 * NOTE: This function is currently called with the GIL released.
4808 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004809 */
Tim Peters736aa322000-09-01 06:51:24 +00004810
Fredrik Lundh56055a42000-07-23 19:47:12 +00004811static int _PyPclose(FILE *file)
4812{
Fredrik Lundh20318932000-07-26 17:29:12 +00004813 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004814 DWORD exit_code;
4815 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004816 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4817 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004818#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004819 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004820#endif
4821
Fredrik Lundh20318932000-07-26 17:29:12 +00004822 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004823 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004824 */
4825 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004826#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004827 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004828#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004829 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004830 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4831 (procObj = PyDict_GetItem(_PyPopenProcs,
4832 fileObj)) != NULL &&
4833 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4834 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4835
4836 hProcess = PyLong_AsVoidPtr(hProcessObj);
4837 file_count = PyInt_AsLong(intObj);
4838
4839 if (file_count > 1) {
4840 /* Still other files referencing process */
4841 file_count--;
4842 PyList_SetItem(procObj,1,
4843 PyInt_FromLong(file_count));
4844 } else {
4845 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004846 if (result != EOF &&
4847 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4848 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004849 /* Possible truncation here in 16-bit environments, but
4850 * real exit codes are just the lower byte in any event.
4851 */
4852 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004853 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004854 /* Indicate failure - this will cause the file object
4855 * to raise an I/O error and translate the last Win32
4856 * error code from errno. We do have a problem with
4857 * last errors that overlap the normal errno table,
4858 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004859 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004860 if (result != EOF) {
4861 /* If the error wasn't from the fclose(), then
4862 * set errno for the file object error handling.
4863 */
4864 errno = GetLastError();
4865 }
4866 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004867 }
4868
4869 /* Free up the native handle at this point */
4870 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004871 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004872
Mark Hammondb37a3732000-08-14 04:47:33 +00004873 /* Remove this file pointer from dictionary */
4874 PyDict_DelItem(_PyPopenProcs, fileObj);
4875
4876 if (PyDict_Size(_PyPopenProcs) == 0) {
4877 Py_DECREF(_PyPopenProcs);
4878 _PyPopenProcs = NULL;
4879 }
4880
4881 } /* if object retrieval ok */
4882
4883 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004884 } /* if _PyPopenProcs */
4885
Tim Peters736aa322000-09-01 06:51:24 +00004886#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004887 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004888#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004889 return result;
4890}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004891
4892#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004893static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004894posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004895{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004896 char *name;
4897 char *mode = "r";
4898 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004899 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004900 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004901 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004902 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004903 /* Strip mode of binary or text modifiers */
4904 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4905 mode = "r";
4906 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4907 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004908 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004909 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004910 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004911 if (fp == NULL)
4912 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004913 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004914 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004915 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004916 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004917}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004918
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004919#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004920#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004921
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004922
Guido van Rossumb6775db1994-08-01 11:34:53 +00004923#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004924PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004925"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004926Set the current process's user id.");
4927
Barry Warsaw53699e91996-12-10 23:23:01 +00004928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004929posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004930{
4931 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004932 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004933 return NULL;
4934 if (setuid(uid) < 0)
4935 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004936 Py_INCREF(Py_None);
4937 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004938}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004939#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004940
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004941
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004942#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004943PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004944"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004945Set the current process's effective user id.");
4946
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004947static PyObject *
4948posix_seteuid (PyObject *self, PyObject *args)
4949{
4950 int euid;
4951 if (!PyArg_ParseTuple(args, "i", &euid)) {
4952 return NULL;
4953 } else if (seteuid(euid) < 0) {
4954 return posix_error();
4955 } else {
4956 Py_INCREF(Py_None);
4957 return Py_None;
4958 }
4959}
4960#endif /* HAVE_SETEUID */
4961
4962#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004963PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004964"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004965Set the current process's effective group id.");
4966
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004967static PyObject *
4968posix_setegid (PyObject *self, PyObject *args)
4969{
4970 int egid;
4971 if (!PyArg_ParseTuple(args, "i", &egid)) {
4972 return NULL;
4973 } else if (setegid(egid) < 0) {
4974 return posix_error();
4975 } else {
4976 Py_INCREF(Py_None);
4977 return Py_None;
4978 }
4979}
4980#endif /* HAVE_SETEGID */
4981
4982#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004983PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004984"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004985Set the current process's real and effective user ids.");
4986
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004987static PyObject *
4988posix_setreuid (PyObject *self, PyObject *args)
4989{
4990 int ruid, euid;
4991 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4992 return NULL;
4993 } else if (setreuid(ruid, euid) < 0) {
4994 return posix_error();
4995 } else {
4996 Py_INCREF(Py_None);
4997 return Py_None;
4998 }
4999}
5000#endif /* HAVE_SETREUID */
5001
5002#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005003PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005004"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005005Set the current process's real and effective group ids.");
5006
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005007static PyObject *
5008posix_setregid (PyObject *self, PyObject *args)
5009{
5010 int rgid, egid;
5011 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5012 return NULL;
5013 } else if (setregid(rgid, egid) < 0) {
5014 return posix_error();
5015 } else {
5016 Py_INCREF(Py_None);
5017 return Py_None;
5018 }
5019}
5020#endif /* HAVE_SETREGID */
5021
Guido van Rossumb6775db1994-08-01 11:34:53 +00005022#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005023PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005024"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005026
Barry Warsaw53699e91996-12-10 23:23:01 +00005027static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005028posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005029{
5030 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005031 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005032 return NULL;
5033 if (setgid(gid) < 0)
5034 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005035 Py_INCREF(Py_None);
5036 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005037}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005038#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005039
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005040#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005041PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005042"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005043Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005044
5045static PyObject *
5046posix_setgroups(PyObject *self, PyObject *args)
5047{
5048 PyObject *groups;
5049 int i, len;
5050 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005051
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005052 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
5053 return NULL;
5054 if (!PySequence_Check(groups)) {
5055 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5056 return NULL;
5057 }
5058 len = PySequence_Size(groups);
5059 if (len > MAX_GROUPS) {
5060 PyErr_SetString(PyExc_ValueError, "too many groups");
5061 return NULL;
5062 }
5063 for(i = 0; i < len; i++) {
5064 PyObject *elem;
5065 elem = PySequence_GetItem(groups, i);
5066 if (!elem)
5067 return NULL;
5068 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005069 if (!PyLong_Check(elem)) {
5070 PyErr_SetString(PyExc_TypeError,
5071 "groups must be integers");
5072 Py_DECREF(elem);
5073 return NULL;
5074 } else {
5075 unsigned long x = PyLong_AsUnsignedLong(elem);
5076 if (PyErr_Occurred()) {
5077 PyErr_SetString(PyExc_TypeError,
5078 "group id too big");
5079 Py_DECREF(elem);
5080 return NULL;
5081 }
5082 grouplist[i] = x;
5083 /* read back the value to see if it fitted in gid_t */
5084 if (grouplist[i] != x) {
5085 PyErr_SetString(PyExc_TypeError,
5086 "group id too big");
5087 Py_DECREF(elem);
5088 return NULL;
5089 }
5090 }
5091 } else {
5092 long x = PyInt_AsLong(elem);
5093 grouplist[i] = x;
5094 if (grouplist[i] != x) {
5095 PyErr_SetString(PyExc_TypeError,
5096 "group id too big");
5097 Py_DECREF(elem);
5098 return NULL;
5099 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005100 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005101 Py_DECREF(elem);
5102 }
5103
5104 if (setgroups(len, grouplist) < 0)
5105 return posix_error();
5106 Py_INCREF(Py_None);
5107 return Py_None;
5108}
5109#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005110
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005111#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005112static PyObject *
5113wait_helper(int pid, int status, struct rusage *ru)
5114{
5115 PyObject *result;
5116 static PyObject *struct_rusage;
5117
5118 if (pid == -1)
5119 return posix_error();
5120
5121 if (struct_rusage == NULL) {
5122 PyObject *m = PyImport_ImportModule("resource");
5123 if (m == NULL)
5124 return NULL;
5125 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5126 Py_DECREF(m);
5127 if (struct_rusage == NULL)
5128 return NULL;
5129 }
5130
5131 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5132 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5133 if (!result)
5134 return NULL;
5135
5136#ifndef doubletime
5137#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5138#endif
5139
5140 PyStructSequence_SET_ITEM(result, 0,
5141 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5142 PyStructSequence_SET_ITEM(result, 1,
5143 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5144#define SET_INT(result, index, value)\
5145 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5146 SET_INT(result, 2, ru->ru_maxrss);
5147 SET_INT(result, 3, ru->ru_ixrss);
5148 SET_INT(result, 4, ru->ru_idrss);
5149 SET_INT(result, 5, ru->ru_isrss);
5150 SET_INT(result, 6, ru->ru_minflt);
5151 SET_INT(result, 7, ru->ru_majflt);
5152 SET_INT(result, 8, ru->ru_nswap);
5153 SET_INT(result, 9, ru->ru_inblock);
5154 SET_INT(result, 10, ru->ru_oublock);
5155 SET_INT(result, 11, ru->ru_msgsnd);
5156 SET_INT(result, 12, ru->ru_msgrcv);
5157 SET_INT(result, 13, ru->ru_nsignals);
5158 SET_INT(result, 14, ru->ru_nvcsw);
5159 SET_INT(result, 15, ru->ru_nivcsw);
5160#undef SET_INT
5161
5162 if (PyErr_Occurred()) {
5163 Py_DECREF(result);
5164 return NULL;
5165 }
5166
Neal Norwitz9b00a562006-03-20 08:47:12 +00005167 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005168}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005169#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005170
5171#ifdef HAVE_WAIT3
5172PyDoc_STRVAR(posix_wait3__doc__,
5173"wait3(options) -> (pid, status, rusage)\n\n\
5174Wait for completion of a child process.");
5175
5176static PyObject *
5177posix_wait3(PyObject *self, PyObject *args)
5178{
5179 int pid, options;
5180 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005181 WAIT_TYPE status;
5182 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005183
5184 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5185 return NULL;
5186
5187 Py_BEGIN_ALLOW_THREADS
5188 pid = wait3(&status, options, &ru);
5189 Py_END_ALLOW_THREADS
5190
Neal Norwitzd5a37542006-03-20 06:48:34 +00005191 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005192}
5193#endif /* HAVE_WAIT3 */
5194
5195#ifdef HAVE_WAIT4
5196PyDoc_STRVAR(posix_wait4__doc__,
5197"wait4(pid, options) -> (pid, status, rusage)\n\n\
5198Wait for completion of a given child process.");
5199
5200static PyObject *
5201posix_wait4(PyObject *self, PyObject *args)
5202{
5203 int pid, options;
5204 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005205 WAIT_TYPE status;
5206 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005207
5208 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5209 return NULL;
5210
5211 Py_BEGIN_ALLOW_THREADS
5212 pid = wait4(pid, &status, options, &ru);
5213 Py_END_ALLOW_THREADS
5214
Neal Norwitzd5a37542006-03-20 06:48:34 +00005215 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005216}
5217#endif /* HAVE_WAIT4 */
5218
Guido van Rossumb6775db1994-08-01 11:34:53 +00005219#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005220PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005221"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005222Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005223
Barry Warsaw53699e91996-12-10 23:23:01 +00005224static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005225posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005226{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005227 int pid, options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005228 WAIT_TYPE status;
5229 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005230
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005231 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005232 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005233 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005234 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005235 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005236 if (pid == -1)
5237 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005238
5239 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005240}
5241
Tim Petersab034fa2002-02-01 11:27:43 +00005242#elif defined(HAVE_CWAIT)
5243
5244/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005245PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005246"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005247"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005248
5249static PyObject *
5250posix_waitpid(PyObject *self, PyObject *args)
5251{
Martin v. Löwis18e16552006-02-15 17:27:45 +00005252 intptr_t pid;
5253 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005254
5255 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5256 return NULL;
5257 Py_BEGIN_ALLOW_THREADS
5258 pid = _cwait(&status, pid, options);
5259 Py_END_ALLOW_THREADS
5260 if (pid == -1)
5261 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005262
5263 /* shift the status left a byte so this is more like the POSIX waitpid */
5264 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005265}
5266#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005267
Guido van Rossumad0ee831995-03-01 10:34:45 +00005268#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005269PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005270"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005271Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005272
Barry Warsaw53699e91996-12-10 23:23:01 +00005273static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005274posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005275{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005276 int pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005277 WAIT_TYPE status;
5278 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005279
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005280 Py_BEGIN_ALLOW_THREADS
5281 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005282 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005283 if (pid == -1)
5284 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005285
5286 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005287}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005288#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005289
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005291PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005292"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005293Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005294
Barry Warsaw53699e91996-12-10 23:23:01 +00005295static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005296posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005297{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005298#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005299 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005300#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005301#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005302 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005303#else
5304 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5305#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005306#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005307}
5308
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005309
Guido van Rossumb6775db1994-08-01 11:34:53 +00005310#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005311PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005312"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005313Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005314
Barry Warsaw53699e91996-12-10 23:23:01 +00005315static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005316posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005317{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005318 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005319 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005320 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005321 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005322 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005323 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005324 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005325 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005326 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005327 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00005328 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005329}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005330#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005331
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005332
Guido van Rossumb6775db1994-08-01 11:34:53 +00005333#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005334PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005335"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005336Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005337
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005338static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005339posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005340{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005341 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005342}
5343#endif /* HAVE_SYMLINK */
5344
5345
5346#ifdef HAVE_TIMES
5347#ifndef HZ
5348#define HZ 60 /* Universal constant :-) */
5349#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005350
Guido van Rossumd48f2521997-12-05 22:19:34 +00005351#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5352static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005353system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005354{
5355 ULONG value = 0;
5356
5357 Py_BEGIN_ALLOW_THREADS
5358 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5359 Py_END_ALLOW_THREADS
5360
5361 return value;
5362}
5363
5364static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005365posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005366{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005367 /* Currently Only Uptime is Provided -- Others Later */
5368 return Py_BuildValue("ddddd",
5369 (double)0 /* t.tms_utime / HZ */,
5370 (double)0 /* t.tms_stime / HZ */,
5371 (double)0 /* t.tms_cutime / HZ */,
5372 (double)0 /* t.tms_cstime / HZ */,
5373 (double)system_uptime() / 1000);
5374}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005375#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005376static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005377posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005378{
5379 struct tms t;
5380 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005381 errno = 0;
5382 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005383 if (c == (clock_t) -1)
5384 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005385 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005386 (double)t.tms_utime / HZ,
5387 (double)t.tms_stime / HZ,
5388 (double)t.tms_cutime / HZ,
5389 (double)t.tms_cstime / HZ,
5390 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005391}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005392#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005393#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005394
5395
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005396#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005397#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005398static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005399posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005400{
5401 FILETIME create, exit, kernel, user;
5402 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005403 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005404 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5405 /* The fields of a FILETIME structure are the hi and lo part
5406 of a 64-bit value expressed in 100 nanosecond units.
5407 1e7 is one second in such units; 1e-7 the inverse.
5408 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5409 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005410 return Py_BuildValue(
5411 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005412 (double)(kernel.dwHighDateTime*429.4967296 +
5413 kernel.dwLowDateTime*1e-7),
5414 (double)(user.dwHighDateTime*429.4967296 +
5415 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005416 (double)0,
5417 (double)0,
5418 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005419}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005420#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005421
5422#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005423PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005424"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005425Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005426#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005428
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005429#ifdef HAVE_GETSID
5430PyDoc_STRVAR(posix_getsid__doc__,
5431"getsid(pid) -> sid\n\n\
5432Call the system call getsid().");
5433
5434static PyObject *
5435posix_getsid(PyObject *self, PyObject *args)
5436{
5437 int pid, sid;
5438 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5439 return NULL;
5440 sid = getsid(pid);
5441 if (sid < 0)
5442 return posix_error();
5443 return PyInt_FromLong((long)sid);
5444}
5445#endif /* HAVE_GETSID */
5446
5447
Guido van Rossumb6775db1994-08-01 11:34:53 +00005448#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005449PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005450"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005451Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005452
Barry Warsaw53699e91996-12-10 23:23:01 +00005453static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005454posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005455{
Guido van Rossum687dd131993-05-17 08:34:16 +00005456 if (setsid() < 0)
5457 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005458 Py_INCREF(Py_None);
5459 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005460}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005461#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005462
Guido van Rossumb6775db1994-08-01 11:34:53 +00005463#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005464PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005465"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005466Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005467
Barry Warsaw53699e91996-12-10 23:23:01 +00005468static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005469posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005470{
5471 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005472 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005473 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005474 if (setpgid(pid, pgrp) < 0)
5475 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005476 Py_INCREF(Py_None);
5477 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005478}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005479#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005480
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005481
Guido van Rossumb6775db1994-08-01 11:34:53 +00005482#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005483PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005484"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005486
Barry Warsaw53699e91996-12-10 23:23:01 +00005487static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005488posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005489{
5490 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005491 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005492 return NULL;
5493 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005494 if (pgid < 0)
5495 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005496 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005497}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005498#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005499
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005500
Guido van Rossumb6775db1994-08-01 11:34:53 +00005501#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005502PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005503"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005505
Barry Warsaw53699e91996-12-10 23:23:01 +00005506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005507posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005508{
5509 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005510 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005511 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005512 if (tcsetpgrp(fd, pgid) < 0)
5513 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005514 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005515 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005516}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005517#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005518
Guido van Rossum687dd131993-05-17 08:34:16 +00005519/* Functions acting on file descriptors */
5520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005521PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005522"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005523Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005524
Barry Warsaw53699e91996-12-10 23:23:01 +00005525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005526posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005527{
Mark Hammondef8b6542001-05-13 08:04:26 +00005528 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005529 int flag;
5530 int mode = 0777;
5531 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005532
5533#ifdef MS_WINDOWS
5534 if (unicode_file_names()) {
5535 PyUnicodeObject *po;
5536 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5537 Py_BEGIN_ALLOW_THREADS
5538 /* PyUnicode_AS_UNICODE OK without thread
5539 lock as it is a simple dereference. */
5540 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5541 Py_END_ALLOW_THREADS
5542 if (fd < 0)
5543 return posix_error();
5544 return PyInt_FromLong((long)fd);
5545 }
5546 /* Drop the argument parsing error as narrow strings
5547 are also valid. */
5548 PyErr_Clear();
5549 }
5550#endif
5551
Tim Peters5aa91602002-01-30 05:46:57 +00005552 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005553 Py_FileSystemDefaultEncoding, &file,
5554 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005555 return NULL;
5556
Barry Warsaw53699e91996-12-10 23:23:01 +00005557 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005558 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005559 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005560 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005561 return posix_error_with_allocated_filename(file);
5562 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005563 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005564}
5565
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005567PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005568"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005569Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005570
Barry Warsaw53699e91996-12-10 23:23:01 +00005571static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005572posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005573{
5574 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005575 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005576 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005577 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005578 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005579 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005580 if (res < 0)
5581 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005582 Py_INCREF(Py_None);
5583 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005584}
5585
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005587PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005588"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005589Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005590
Barry Warsaw53699e91996-12-10 23:23:01 +00005591static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005592posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005593{
5594 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005595 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005596 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005597 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005598 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005599 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005600 if (fd < 0)
5601 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005602 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005603}
5604
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005606PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005607"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005608Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005609
Barry Warsaw53699e91996-12-10 23:23:01 +00005610static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005611posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005612{
5613 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005614 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00005615 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005616 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005617 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00005618 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005619 if (res < 0)
5620 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005621 Py_INCREF(Py_None);
5622 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005623}
5624
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005627"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005628Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005629
Barry Warsaw53699e91996-12-10 23:23:01 +00005630static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005631posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005632{
5633 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005634#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005635 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005636#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005637 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005638#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005639 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005640 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005641 return NULL;
5642#ifdef SEEK_SET
5643 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5644 switch (how) {
5645 case 0: how = SEEK_SET; break;
5646 case 1: how = SEEK_CUR; break;
5647 case 2: how = SEEK_END; break;
5648 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005649#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005650
5651#if !defined(HAVE_LARGEFILE_SUPPORT)
5652 pos = PyInt_AsLong(posobj);
5653#else
5654 pos = PyLong_Check(posobj) ?
5655 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5656#endif
5657 if (PyErr_Occurred())
5658 return NULL;
5659
Barry Warsaw53699e91996-12-10 23:23:01 +00005660 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005661#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005662 res = _lseeki64(fd, pos, how);
5663#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005664 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005665#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005666 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005667 if (res < 0)
5668 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005669
5670#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005671 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005672#else
5673 return PyLong_FromLongLong(res);
5674#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005675}
5676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005678PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005679"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005680Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005681
Barry Warsaw53699e91996-12-10 23:23:01 +00005682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005683posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005684{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005685 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005686 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005687 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005688 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00005689 if (size < 0) {
5690 errno = EINVAL;
5691 return posix_error();
5692 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005693 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005694 if (buffer == NULL)
5695 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005696 Py_BEGIN_ALLOW_THREADS
5697 n = read(fd, PyString_AsString(buffer), size);
5698 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005699 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005700 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005701 return posix_error();
5702 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005703 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005704 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005705 return buffer;
5706}
5707
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005709PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005710"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005711Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005712
Barry Warsaw53699e91996-12-10 23:23:01 +00005713static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005714posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005715{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005716 int fd;
5717 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00005718 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005719
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005720 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005721 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005722 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005723 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005724 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005725 if (size < 0)
5726 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005727 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005728}
5729
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005731PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005732"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005733Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005734
Barry Warsaw53699e91996-12-10 23:23:01 +00005735static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005736posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005737{
5738 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005739 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005740 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005741 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005742 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005743#ifdef __VMS
5744 /* on OpenVMS we must ensure that all bytes are written to the file */
5745 fsync(fd);
5746#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005747 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005748 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005749 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00005750 if (res != 0) {
5751#ifdef MS_WINDOWS
5752 return win32_error("fstat", NULL);
5753#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005754 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005755#endif
5756 }
Tim Peters5aa91602002-01-30 05:46:57 +00005757
Martin v. Löwis14694662006-02-03 12:54:16 +00005758 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005759}
5760
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005762PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005763"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005764Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005765
Barry Warsaw53699e91996-12-10 23:23:01 +00005766static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005767posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005768{
Guido van Rossum687dd131993-05-17 08:34:16 +00005769 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005770 char *mode = "r";
5771 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005772 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005773 PyObject *f;
5774 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005775 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005776
Thomas Heller1f043e22002-11-07 16:00:59 +00005777 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5778 PyErr_Format(PyExc_ValueError,
5779 "invalid file mode '%s'", mode);
5780 return NULL;
5781 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005782 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00005783#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00005784 if (mode[0] == 'a') {
5785 /* try to make sure the O_APPEND flag is set */
5786 int flags;
5787 flags = fcntl(fd, F_GETFL);
5788 if (flags != -1)
5789 fcntl(fd, F_SETFL, flags | O_APPEND);
5790 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00005791 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00005792 /* restore old mode if fdopen failed */
5793 fcntl(fd, F_SETFL, flags);
5794 } else {
5795 fp = fdopen(fd, mode);
5796 }
Georg Brandl644b1e72006-03-31 20:27:22 +00005797#else
5798 fp = fdopen(fd, mode);
5799#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005800 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005801 if (fp == NULL)
5802 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005803 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005804 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005805 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005806 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005807}
5808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005809PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005810"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005811Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005812connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005813
5814static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005815posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005816{
5817 int fd;
5818 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5819 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005820 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005821}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005822
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005823#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005824PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005825"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005826Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005827
Barry Warsaw53699e91996-12-10 23:23:01 +00005828static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005829posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005830{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005831#if defined(PYOS_OS2)
5832 HFILE read, write;
5833 APIRET rc;
5834
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005835 Py_BEGIN_ALLOW_THREADS
5836 rc = DosCreatePipe( &read, &write, 4096);
5837 Py_END_ALLOW_THREADS
5838 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005839 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005840
5841 return Py_BuildValue("(ii)", read, write);
5842#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005843#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005844 int fds[2];
5845 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005846 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005847 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005848 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005849 if (res != 0)
5850 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005851 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005852#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005853 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005854 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005855 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005856 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005857 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005858 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005859 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005860 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005861 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5862 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005863 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005864#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005865#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005866}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005867#endif /* HAVE_PIPE */
5868
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005869
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005870#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005871PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005872"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005873Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005874
Barry Warsaw53699e91996-12-10 23:23:01 +00005875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005876posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005877{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005878 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005879 int mode = 0666;
5880 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005881 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005882 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005883 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005884 res = mkfifo(filename, mode);
5885 Py_END_ALLOW_THREADS
5886 if (res < 0)
5887 return posix_error();
5888 Py_INCREF(Py_None);
5889 return Py_None;
5890}
5891#endif
5892
5893
Neal Norwitz11690112002-07-30 01:08:28 +00005894#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005895PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005896"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005897Create a filesystem node (file, device special file or named pipe)\n\
5898named filename. mode specifies both the permissions to use and the\n\
5899type of node to be created, being combined (bitwise OR) with one of\n\
5900S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005901device defines the newly created device special file (probably using\n\
5902os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005903
5904
5905static PyObject *
5906posix_mknod(PyObject *self, PyObject *args)
5907{
5908 char *filename;
5909 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005910 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005911 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005912 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005913 return NULL;
5914 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005915 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005916 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005917 if (res < 0)
5918 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005919 Py_INCREF(Py_None);
5920 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005921}
5922#endif
5923
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005924#ifdef HAVE_DEVICE_MACROS
5925PyDoc_STRVAR(posix_major__doc__,
5926"major(device) -> major number\n\
5927Extracts a device major number from a raw device number.");
5928
5929static PyObject *
5930posix_major(PyObject *self, PyObject *args)
5931{
5932 int device;
5933 if (!PyArg_ParseTuple(args, "i:major", &device))
5934 return NULL;
5935 return PyInt_FromLong((long)major(device));
5936}
5937
5938PyDoc_STRVAR(posix_minor__doc__,
5939"minor(device) -> minor number\n\
5940Extracts a device minor number from a raw device number.");
5941
5942static PyObject *
5943posix_minor(PyObject *self, PyObject *args)
5944{
5945 int device;
5946 if (!PyArg_ParseTuple(args, "i:minor", &device))
5947 return NULL;
5948 return PyInt_FromLong((long)minor(device));
5949}
5950
5951PyDoc_STRVAR(posix_makedev__doc__,
5952"makedev(major, minor) -> device number\n\
5953Composes a raw device number from the major and minor device numbers.");
5954
5955static PyObject *
5956posix_makedev(PyObject *self, PyObject *args)
5957{
5958 int major, minor;
5959 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5960 return NULL;
5961 return PyInt_FromLong((long)makedev(major, minor));
5962}
5963#endif /* device macros */
5964
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005965
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005966#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005967PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005968"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005969Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005970
Barry Warsaw53699e91996-12-10 23:23:01 +00005971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005972posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005973{
5974 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005975 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005976 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005977 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005978
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005979 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005980 return NULL;
5981
5982#if !defined(HAVE_LARGEFILE_SUPPORT)
5983 length = PyInt_AsLong(lenobj);
5984#else
5985 length = PyLong_Check(lenobj) ?
5986 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5987#endif
5988 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005989 return NULL;
5990
Barry Warsaw53699e91996-12-10 23:23:01 +00005991 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005992 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005993 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005994 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005995 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005996 return NULL;
5997 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005998 Py_INCREF(Py_None);
5999 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006000}
6001#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006002
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006003#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006004PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006005"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006006Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006007
Fred Drake762e2061999-08-26 17:23:54 +00006008/* Save putenv() parameters as values here, so we can collect them when they
6009 * get re-set with another call for the same key. */
6010static PyObject *posix_putenv_garbage;
6011
Tim Peters5aa91602002-01-30 05:46:57 +00006012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006013posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006014{
6015 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006016 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006017 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006018 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006019
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006020 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006021 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006022
6023#if defined(PYOS_OS2)
6024 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6025 APIRET rc;
6026
Guido van Rossumd48f2521997-12-05 22:19:34 +00006027 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6028 if (rc != NO_ERROR)
6029 return os2_error(rc);
6030
6031 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6032 APIRET rc;
6033
Guido van Rossumd48f2521997-12-05 22:19:34 +00006034 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6035 if (rc != NO_ERROR)
6036 return os2_error(rc);
6037 } else {
6038#endif
6039
Fred Drake762e2061999-08-26 17:23:54 +00006040 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006041 len = strlen(s1) + strlen(s2) + 2;
6042 /* len includes space for a trailing \0; the size arg to
6043 PyString_FromStringAndSize does not count that */
6044 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006045 if (newstr == NULL)
6046 return PyErr_NoMemory();
Anthony Baxter64182fe2006-04-11 12:14:09 +00006047 newenv = PyString_AS_STRING(newstr);
6048 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6049 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006050 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006051 posix_error();
6052 return NULL;
6053 }
Fred Drake762e2061999-08-26 17:23:54 +00006054 /* Install the first arg and newstr in posix_putenv_garbage;
6055 * this will cause previous value to be collected. This has to
6056 * happen after the real putenv() call because the old value
6057 * was still accessible until then. */
6058 if (PyDict_SetItem(posix_putenv_garbage,
6059 PyTuple_GET_ITEM(args, 0), newstr)) {
6060 /* really not much we can do; just leak */
6061 PyErr_Clear();
6062 }
6063 else {
6064 Py_DECREF(newstr);
6065 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006066
6067#if defined(PYOS_OS2)
6068 }
6069#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006070 Py_INCREF(Py_None);
6071 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006072}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006073#endif /* putenv */
6074
Guido van Rossumc524d952001-10-19 01:31:59 +00006075#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006076PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006077"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006078Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006079
6080static PyObject *
6081posix_unsetenv(PyObject *self, PyObject *args)
6082{
6083 char *s1;
6084
6085 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6086 return NULL;
6087
6088 unsetenv(s1);
6089
6090 /* Remove the key from posix_putenv_garbage;
6091 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006092 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006093 * old value was still accessible until then.
6094 */
6095 if (PyDict_DelItem(posix_putenv_garbage,
6096 PyTuple_GET_ITEM(args, 0))) {
6097 /* really not much we can do; just leak */
6098 PyErr_Clear();
6099 }
6100
6101 Py_INCREF(Py_None);
6102 return Py_None;
6103}
6104#endif /* unsetenv */
6105
Guido van Rossumb6a47161997-09-15 22:54:34 +00006106#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006107PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006108"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006109Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006110
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006111static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006112posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006113{
6114 int code;
6115 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006116 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006117 return NULL;
6118 message = strerror(code);
6119 if (message == NULL) {
6120 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006121 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006122 return NULL;
6123 }
6124 return PyString_FromString(message);
6125}
6126#endif /* strerror */
6127
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006128
Guido van Rossumc9641791998-08-04 15:26:23 +00006129#ifdef HAVE_SYS_WAIT_H
6130
Fred Drake106c1a02002-04-23 15:58:02 +00006131#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006132PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006133"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006134Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006135
6136static PyObject *
6137posix_WCOREDUMP(PyObject *self, PyObject *args)
6138{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006139 WAIT_TYPE status;
6140 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006141
Neal Norwitzd5a37542006-03-20 06:48:34 +00006142 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006143 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006144
6145 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006146}
6147#endif /* WCOREDUMP */
6148
6149#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006150PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006151"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006152Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006153job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006154
6155static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006156posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006157{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006158 WAIT_TYPE status;
6159 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006160
Neal Norwitzd5a37542006-03-20 06:48:34 +00006161 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006162 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006163
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006164 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006165}
6166#endif /* WIFCONTINUED */
6167
Guido van Rossumc9641791998-08-04 15:26:23 +00006168#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006169PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006170"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006172
6173static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006174posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006175{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006176 WAIT_TYPE status;
6177 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006178
Neal Norwitzd5a37542006-03-20 06:48:34 +00006179 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006180 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006181
Fred Drake106c1a02002-04-23 15:58:02 +00006182 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006183}
6184#endif /* WIFSTOPPED */
6185
6186#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006187PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006188"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006189Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006190
6191static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006192posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006193{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006194 WAIT_TYPE status;
6195 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006196
Neal Norwitzd5a37542006-03-20 06:48:34 +00006197 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006198 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006199
Fred Drake106c1a02002-04-23 15:58:02 +00006200 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006201}
6202#endif /* WIFSIGNALED */
6203
6204#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006205PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006206"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006207Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006208system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006209
6210static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006211posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006212{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006213 WAIT_TYPE status;
6214 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006215
Neal Norwitzd5a37542006-03-20 06:48:34 +00006216 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006217 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006218
Fred Drake106c1a02002-04-23 15:58:02 +00006219 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006220}
6221#endif /* WIFEXITED */
6222
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006223#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006224PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006225"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006226Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006227
6228static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006229posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006230{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006231 WAIT_TYPE status;
6232 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006233
Neal Norwitzd5a37542006-03-20 06:48:34 +00006234 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006235 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006236
Guido van Rossumc9641791998-08-04 15:26:23 +00006237 return Py_BuildValue("i", WEXITSTATUS(status));
6238}
6239#endif /* WEXITSTATUS */
6240
6241#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006242PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006243"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006244Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006245value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006246
6247static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006248posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006249{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006250 WAIT_TYPE status;
6251 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006252
Neal Norwitzd5a37542006-03-20 06:48:34 +00006253 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006254 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006255
Guido van Rossumc9641791998-08-04 15:26:23 +00006256 return Py_BuildValue("i", WTERMSIG(status));
6257}
6258#endif /* WTERMSIG */
6259
6260#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006261PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006262"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006263Return the signal that stopped the process that provided\n\
6264the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006265
6266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006267posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006268{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006269 WAIT_TYPE status;
6270 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006271
Neal Norwitzd5a37542006-03-20 06:48:34 +00006272 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006273 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006274
Guido van Rossumc9641791998-08-04 15:26:23 +00006275 return Py_BuildValue("i", WSTOPSIG(status));
6276}
6277#endif /* WSTOPSIG */
6278
6279#endif /* HAVE_SYS_WAIT_H */
6280
6281
Guido van Rossum94f6f721999-01-06 18:42:14 +00006282#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006283#ifdef _SCO_DS
6284/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6285 needed definitions in sys/statvfs.h */
6286#define _SVID3
6287#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006288#include <sys/statvfs.h>
6289
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006290static PyObject*
6291_pystatvfs_fromstructstatvfs(struct statvfs st) {
6292 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6293 if (v == NULL)
6294 return NULL;
6295
6296#if !defined(HAVE_LARGEFILE_SUPPORT)
6297 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6298 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6299 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6300 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6301 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6302 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6303 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6304 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6305 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6306 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6307#else
6308 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6309 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006310 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006311 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006312 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006313 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006314 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006315 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006316 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006317 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006318 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006319 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006320 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006321 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006322 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6323 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6324#endif
6325
6326 return v;
6327}
6328
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006329PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006330"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006331Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006332
6333static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006334posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006335{
6336 int fd, res;
6337 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006338
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006339 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006340 return NULL;
6341 Py_BEGIN_ALLOW_THREADS
6342 res = fstatvfs(fd, &st);
6343 Py_END_ALLOW_THREADS
6344 if (res != 0)
6345 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006346
6347 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006348}
6349#endif /* HAVE_FSTATVFS */
6350
6351
6352#if defined(HAVE_STATVFS)
6353#include <sys/statvfs.h>
6354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006355PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006356"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006357Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006358
6359static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006360posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006361{
6362 char *path;
6363 int res;
6364 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006365 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006366 return NULL;
6367 Py_BEGIN_ALLOW_THREADS
6368 res = statvfs(path, &st);
6369 Py_END_ALLOW_THREADS
6370 if (res != 0)
6371 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006372
6373 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006374}
6375#endif /* HAVE_STATVFS */
6376
6377
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006378#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006379PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006380"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006381Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006382The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006383or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006384
6385static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006386posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006387{
6388 PyObject *result = NULL;
6389 char *dir = NULL;
6390 char *pfx = NULL;
6391 char *name;
6392
6393 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6394 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006395
6396 if (PyErr_Warn(PyExc_RuntimeWarning,
6397 "tempnam is a potential security risk to your program") < 0)
6398 return NULL;
6399
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006400#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006401 name = _tempnam(dir, pfx);
6402#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006403 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006404#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006405 if (name == NULL)
6406 return PyErr_NoMemory();
6407 result = PyString_FromString(name);
6408 free(name);
6409 return result;
6410}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006411#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006412
6413
6414#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006415PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006416"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006417Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006418
6419static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006420posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006421{
6422 FILE *fp;
6423
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006424 fp = tmpfile();
6425 if (fp == NULL)
6426 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006427 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006428}
6429#endif
6430
6431
6432#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006433PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006434"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006435Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006436
6437static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006438posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006439{
6440 char buffer[L_tmpnam];
6441 char *name;
6442
Skip Montanaro95618b52001-08-18 18:52:10 +00006443 if (PyErr_Warn(PyExc_RuntimeWarning,
6444 "tmpnam is a potential security risk to your program") < 0)
6445 return NULL;
6446
Greg Wardb48bc172000-03-01 21:51:56 +00006447#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006448 name = tmpnam_r(buffer);
6449#else
6450 name = tmpnam(buffer);
6451#endif
6452 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006453 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006454#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006455 "unexpected NULL from tmpnam_r"
6456#else
6457 "unexpected NULL from tmpnam"
6458#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006459 );
6460 PyErr_SetObject(PyExc_OSError, err);
6461 Py_XDECREF(err);
6462 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006463 }
6464 return PyString_FromString(buffer);
6465}
6466#endif
6467
6468
Fred Drakec9680921999-12-13 16:37:25 +00006469/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6470 * It maps strings representing configuration variable names to
6471 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006472 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006473 * rarely-used constants. There are three separate tables that use
6474 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006475 *
6476 * This code is always included, even if none of the interfaces that
6477 * need it are included. The #if hackery needed to avoid it would be
6478 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006479 */
6480struct constdef {
6481 char *name;
6482 long value;
6483};
6484
Fred Drake12c6e2d1999-12-14 21:25:03 +00006485static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006486conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6487 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006488{
6489 if (PyInt_Check(arg)) {
6490 *valuep = PyInt_AS_LONG(arg);
6491 return 1;
6492 }
6493 if (PyString_Check(arg)) {
6494 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006495 size_t lo = 0;
6496 size_t mid;
6497 size_t hi = tablesize;
6498 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006499 char *confname = PyString_AS_STRING(arg);
6500 while (lo < hi) {
6501 mid = (lo + hi) / 2;
6502 cmp = strcmp(confname, table[mid].name);
6503 if (cmp < 0)
6504 hi = mid;
6505 else if (cmp > 0)
6506 lo = mid + 1;
6507 else {
6508 *valuep = table[mid].value;
6509 return 1;
6510 }
6511 }
6512 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6513 }
6514 else
6515 PyErr_SetString(PyExc_TypeError,
6516 "configuration names must be strings or integers");
6517 return 0;
6518}
6519
6520
6521#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6522static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006523#ifdef _PC_ABI_AIO_XFER_MAX
6524 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6525#endif
6526#ifdef _PC_ABI_ASYNC_IO
6527 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6528#endif
Fred Drakec9680921999-12-13 16:37:25 +00006529#ifdef _PC_ASYNC_IO
6530 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6531#endif
6532#ifdef _PC_CHOWN_RESTRICTED
6533 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6534#endif
6535#ifdef _PC_FILESIZEBITS
6536 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6537#endif
6538#ifdef _PC_LAST
6539 {"PC_LAST", _PC_LAST},
6540#endif
6541#ifdef _PC_LINK_MAX
6542 {"PC_LINK_MAX", _PC_LINK_MAX},
6543#endif
6544#ifdef _PC_MAX_CANON
6545 {"PC_MAX_CANON", _PC_MAX_CANON},
6546#endif
6547#ifdef _PC_MAX_INPUT
6548 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6549#endif
6550#ifdef _PC_NAME_MAX
6551 {"PC_NAME_MAX", _PC_NAME_MAX},
6552#endif
6553#ifdef _PC_NO_TRUNC
6554 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6555#endif
6556#ifdef _PC_PATH_MAX
6557 {"PC_PATH_MAX", _PC_PATH_MAX},
6558#endif
6559#ifdef _PC_PIPE_BUF
6560 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6561#endif
6562#ifdef _PC_PRIO_IO
6563 {"PC_PRIO_IO", _PC_PRIO_IO},
6564#endif
6565#ifdef _PC_SOCK_MAXBUF
6566 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6567#endif
6568#ifdef _PC_SYNC_IO
6569 {"PC_SYNC_IO", _PC_SYNC_IO},
6570#endif
6571#ifdef _PC_VDISABLE
6572 {"PC_VDISABLE", _PC_VDISABLE},
6573#endif
6574};
6575
Fred Drakec9680921999-12-13 16:37:25 +00006576static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006577conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006578{
6579 return conv_confname(arg, valuep, posix_constants_pathconf,
6580 sizeof(posix_constants_pathconf)
6581 / sizeof(struct constdef));
6582}
6583#endif
6584
6585#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006586PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006587"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006588Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006589If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006590
6591static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006592posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006593{
6594 PyObject *result = NULL;
6595 int name, fd;
6596
Fred Drake12c6e2d1999-12-14 21:25:03 +00006597 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6598 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006599 long limit;
6600
6601 errno = 0;
6602 limit = fpathconf(fd, name);
6603 if (limit == -1 && errno != 0)
6604 posix_error();
6605 else
6606 result = PyInt_FromLong(limit);
6607 }
6608 return result;
6609}
6610#endif
6611
6612
6613#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006614PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006615"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006616Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006617If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006618
6619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006620posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006621{
6622 PyObject *result = NULL;
6623 int name;
6624 char *path;
6625
6626 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6627 conv_path_confname, &name)) {
6628 long limit;
6629
6630 errno = 0;
6631 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006632 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006633 if (errno == EINVAL)
6634 /* could be a path or name problem */
6635 posix_error();
6636 else
6637 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006638 }
Fred Drakec9680921999-12-13 16:37:25 +00006639 else
6640 result = PyInt_FromLong(limit);
6641 }
6642 return result;
6643}
6644#endif
6645
6646#ifdef HAVE_CONFSTR
6647static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006648#ifdef _CS_ARCHITECTURE
6649 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6650#endif
6651#ifdef _CS_HOSTNAME
6652 {"CS_HOSTNAME", _CS_HOSTNAME},
6653#endif
6654#ifdef _CS_HW_PROVIDER
6655 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6656#endif
6657#ifdef _CS_HW_SERIAL
6658 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6659#endif
6660#ifdef _CS_INITTAB_NAME
6661 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6662#endif
Fred Drakec9680921999-12-13 16:37:25 +00006663#ifdef _CS_LFS64_CFLAGS
6664 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6665#endif
6666#ifdef _CS_LFS64_LDFLAGS
6667 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6668#endif
6669#ifdef _CS_LFS64_LIBS
6670 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6671#endif
6672#ifdef _CS_LFS64_LINTFLAGS
6673 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6674#endif
6675#ifdef _CS_LFS_CFLAGS
6676 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6677#endif
6678#ifdef _CS_LFS_LDFLAGS
6679 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6680#endif
6681#ifdef _CS_LFS_LIBS
6682 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6683#endif
6684#ifdef _CS_LFS_LINTFLAGS
6685 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6686#endif
Fred Draked86ed291999-12-15 15:34:33 +00006687#ifdef _CS_MACHINE
6688 {"CS_MACHINE", _CS_MACHINE},
6689#endif
Fred Drakec9680921999-12-13 16:37:25 +00006690#ifdef _CS_PATH
6691 {"CS_PATH", _CS_PATH},
6692#endif
Fred Draked86ed291999-12-15 15:34:33 +00006693#ifdef _CS_RELEASE
6694 {"CS_RELEASE", _CS_RELEASE},
6695#endif
6696#ifdef _CS_SRPC_DOMAIN
6697 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6698#endif
6699#ifdef _CS_SYSNAME
6700 {"CS_SYSNAME", _CS_SYSNAME},
6701#endif
6702#ifdef _CS_VERSION
6703 {"CS_VERSION", _CS_VERSION},
6704#endif
Fred Drakec9680921999-12-13 16:37:25 +00006705#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6706 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6707#endif
6708#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6709 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6710#endif
6711#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6712 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6713#endif
6714#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6715 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6716#endif
6717#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6718 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6719#endif
6720#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6721 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6722#endif
6723#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6724 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6725#endif
6726#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6727 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6728#endif
6729#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6730 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6731#endif
6732#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6733 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6734#endif
6735#ifdef _CS_XBS5_LP64_OFF64_LIBS
6736 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6737#endif
6738#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6739 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6740#endif
6741#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6742 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6743#endif
6744#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6745 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6746#endif
6747#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6748 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6749#endif
6750#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6751 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6752#endif
Fred Draked86ed291999-12-15 15:34:33 +00006753#ifdef _MIPS_CS_AVAIL_PROCESSORS
6754 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6755#endif
6756#ifdef _MIPS_CS_BASE
6757 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6758#endif
6759#ifdef _MIPS_CS_HOSTID
6760 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6761#endif
6762#ifdef _MIPS_CS_HW_NAME
6763 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6764#endif
6765#ifdef _MIPS_CS_NUM_PROCESSORS
6766 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6767#endif
6768#ifdef _MIPS_CS_OSREL_MAJ
6769 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6770#endif
6771#ifdef _MIPS_CS_OSREL_MIN
6772 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6773#endif
6774#ifdef _MIPS_CS_OSREL_PATCH
6775 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6776#endif
6777#ifdef _MIPS_CS_OS_NAME
6778 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6779#endif
6780#ifdef _MIPS_CS_OS_PROVIDER
6781 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6782#endif
6783#ifdef _MIPS_CS_PROCESSORS
6784 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6785#endif
6786#ifdef _MIPS_CS_SERIAL
6787 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6788#endif
6789#ifdef _MIPS_CS_VENDOR
6790 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6791#endif
Fred Drakec9680921999-12-13 16:37:25 +00006792};
6793
6794static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006795conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006796{
6797 return conv_confname(arg, valuep, posix_constants_confstr,
6798 sizeof(posix_constants_confstr)
6799 / sizeof(struct constdef));
6800}
6801
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006802PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006803"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006804Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006805
6806static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006807posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006808{
6809 PyObject *result = NULL;
6810 int name;
6811 char buffer[64];
6812
6813 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6814 int len = confstr(name, buffer, sizeof(buffer));
6815
Fred Drakec9680921999-12-13 16:37:25 +00006816 errno = 0;
6817 if (len == 0) {
6818 if (errno != 0)
6819 posix_error();
6820 else
6821 result = PyString_FromString("");
6822 }
6823 else {
6824 if (len >= sizeof(buffer)) {
6825 result = PyString_FromStringAndSize(NULL, len);
6826 if (result != NULL)
6827 confstr(name, PyString_AS_STRING(result), len+1);
6828 }
6829 else
6830 result = PyString_FromString(buffer);
6831 }
6832 }
6833 return result;
6834}
6835#endif
6836
6837
6838#ifdef HAVE_SYSCONF
6839static struct constdef posix_constants_sysconf[] = {
6840#ifdef _SC_2_CHAR_TERM
6841 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6842#endif
6843#ifdef _SC_2_C_BIND
6844 {"SC_2_C_BIND", _SC_2_C_BIND},
6845#endif
6846#ifdef _SC_2_C_DEV
6847 {"SC_2_C_DEV", _SC_2_C_DEV},
6848#endif
6849#ifdef _SC_2_C_VERSION
6850 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6851#endif
6852#ifdef _SC_2_FORT_DEV
6853 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6854#endif
6855#ifdef _SC_2_FORT_RUN
6856 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6857#endif
6858#ifdef _SC_2_LOCALEDEF
6859 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6860#endif
6861#ifdef _SC_2_SW_DEV
6862 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6863#endif
6864#ifdef _SC_2_UPE
6865 {"SC_2_UPE", _SC_2_UPE},
6866#endif
6867#ifdef _SC_2_VERSION
6868 {"SC_2_VERSION", _SC_2_VERSION},
6869#endif
Fred Draked86ed291999-12-15 15:34:33 +00006870#ifdef _SC_ABI_ASYNCHRONOUS_IO
6871 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6872#endif
6873#ifdef _SC_ACL
6874 {"SC_ACL", _SC_ACL},
6875#endif
Fred Drakec9680921999-12-13 16:37:25 +00006876#ifdef _SC_AIO_LISTIO_MAX
6877 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6878#endif
Fred Drakec9680921999-12-13 16:37:25 +00006879#ifdef _SC_AIO_MAX
6880 {"SC_AIO_MAX", _SC_AIO_MAX},
6881#endif
6882#ifdef _SC_AIO_PRIO_DELTA_MAX
6883 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6884#endif
6885#ifdef _SC_ARG_MAX
6886 {"SC_ARG_MAX", _SC_ARG_MAX},
6887#endif
6888#ifdef _SC_ASYNCHRONOUS_IO
6889 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6890#endif
6891#ifdef _SC_ATEXIT_MAX
6892 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6893#endif
Fred Draked86ed291999-12-15 15:34:33 +00006894#ifdef _SC_AUDIT
6895 {"SC_AUDIT", _SC_AUDIT},
6896#endif
Fred Drakec9680921999-12-13 16:37:25 +00006897#ifdef _SC_AVPHYS_PAGES
6898 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6899#endif
6900#ifdef _SC_BC_BASE_MAX
6901 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6902#endif
6903#ifdef _SC_BC_DIM_MAX
6904 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6905#endif
6906#ifdef _SC_BC_SCALE_MAX
6907 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6908#endif
6909#ifdef _SC_BC_STRING_MAX
6910 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6911#endif
Fred Draked86ed291999-12-15 15:34:33 +00006912#ifdef _SC_CAP
6913 {"SC_CAP", _SC_CAP},
6914#endif
Fred Drakec9680921999-12-13 16:37:25 +00006915#ifdef _SC_CHARCLASS_NAME_MAX
6916 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6917#endif
6918#ifdef _SC_CHAR_BIT
6919 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6920#endif
6921#ifdef _SC_CHAR_MAX
6922 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6923#endif
6924#ifdef _SC_CHAR_MIN
6925 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6926#endif
6927#ifdef _SC_CHILD_MAX
6928 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6929#endif
6930#ifdef _SC_CLK_TCK
6931 {"SC_CLK_TCK", _SC_CLK_TCK},
6932#endif
6933#ifdef _SC_COHER_BLKSZ
6934 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6935#endif
6936#ifdef _SC_COLL_WEIGHTS_MAX
6937 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6938#endif
6939#ifdef _SC_DCACHE_ASSOC
6940 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6941#endif
6942#ifdef _SC_DCACHE_BLKSZ
6943 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6944#endif
6945#ifdef _SC_DCACHE_LINESZ
6946 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6947#endif
6948#ifdef _SC_DCACHE_SZ
6949 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6950#endif
6951#ifdef _SC_DCACHE_TBLKSZ
6952 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6953#endif
6954#ifdef _SC_DELAYTIMER_MAX
6955 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6956#endif
6957#ifdef _SC_EQUIV_CLASS_MAX
6958 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6959#endif
6960#ifdef _SC_EXPR_NEST_MAX
6961 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6962#endif
6963#ifdef _SC_FSYNC
6964 {"SC_FSYNC", _SC_FSYNC},
6965#endif
6966#ifdef _SC_GETGR_R_SIZE_MAX
6967 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6968#endif
6969#ifdef _SC_GETPW_R_SIZE_MAX
6970 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6971#endif
6972#ifdef _SC_ICACHE_ASSOC
6973 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6974#endif
6975#ifdef _SC_ICACHE_BLKSZ
6976 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6977#endif
6978#ifdef _SC_ICACHE_LINESZ
6979 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6980#endif
6981#ifdef _SC_ICACHE_SZ
6982 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6983#endif
Fred Draked86ed291999-12-15 15:34:33 +00006984#ifdef _SC_INF
6985 {"SC_INF", _SC_INF},
6986#endif
Fred Drakec9680921999-12-13 16:37:25 +00006987#ifdef _SC_INT_MAX
6988 {"SC_INT_MAX", _SC_INT_MAX},
6989#endif
6990#ifdef _SC_INT_MIN
6991 {"SC_INT_MIN", _SC_INT_MIN},
6992#endif
6993#ifdef _SC_IOV_MAX
6994 {"SC_IOV_MAX", _SC_IOV_MAX},
6995#endif
Fred Draked86ed291999-12-15 15:34:33 +00006996#ifdef _SC_IP_SECOPTS
6997 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6998#endif
Fred Drakec9680921999-12-13 16:37:25 +00006999#ifdef _SC_JOB_CONTROL
7000 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7001#endif
Fred Draked86ed291999-12-15 15:34:33 +00007002#ifdef _SC_KERN_POINTERS
7003 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7004#endif
7005#ifdef _SC_KERN_SIM
7006 {"SC_KERN_SIM", _SC_KERN_SIM},
7007#endif
Fred Drakec9680921999-12-13 16:37:25 +00007008#ifdef _SC_LINE_MAX
7009 {"SC_LINE_MAX", _SC_LINE_MAX},
7010#endif
7011#ifdef _SC_LOGIN_NAME_MAX
7012 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7013#endif
7014#ifdef _SC_LOGNAME_MAX
7015 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7016#endif
7017#ifdef _SC_LONG_BIT
7018 {"SC_LONG_BIT", _SC_LONG_BIT},
7019#endif
Fred Draked86ed291999-12-15 15:34:33 +00007020#ifdef _SC_MAC
7021 {"SC_MAC", _SC_MAC},
7022#endif
Fred Drakec9680921999-12-13 16:37:25 +00007023#ifdef _SC_MAPPED_FILES
7024 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7025#endif
7026#ifdef _SC_MAXPID
7027 {"SC_MAXPID", _SC_MAXPID},
7028#endif
7029#ifdef _SC_MB_LEN_MAX
7030 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7031#endif
7032#ifdef _SC_MEMLOCK
7033 {"SC_MEMLOCK", _SC_MEMLOCK},
7034#endif
7035#ifdef _SC_MEMLOCK_RANGE
7036 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7037#endif
7038#ifdef _SC_MEMORY_PROTECTION
7039 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7040#endif
7041#ifdef _SC_MESSAGE_PASSING
7042 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7043#endif
Fred Draked86ed291999-12-15 15:34:33 +00007044#ifdef _SC_MMAP_FIXED_ALIGNMENT
7045 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7046#endif
Fred Drakec9680921999-12-13 16:37:25 +00007047#ifdef _SC_MQ_OPEN_MAX
7048 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7049#endif
7050#ifdef _SC_MQ_PRIO_MAX
7051 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7052#endif
Fred Draked86ed291999-12-15 15:34:33 +00007053#ifdef _SC_NACLS_MAX
7054 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7055#endif
Fred Drakec9680921999-12-13 16:37:25 +00007056#ifdef _SC_NGROUPS_MAX
7057 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7058#endif
7059#ifdef _SC_NL_ARGMAX
7060 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7061#endif
7062#ifdef _SC_NL_LANGMAX
7063 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7064#endif
7065#ifdef _SC_NL_MSGMAX
7066 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7067#endif
7068#ifdef _SC_NL_NMAX
7069 {"SC_NL_NMAX", _SC_NL_NMAX},
7070#endif
7071#ifdef _SC_NL_SETMAX
7072 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7073#endif
7074#ifdef _SC_NL_TEXTMAX
7075 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7076#endif
7077#ifdef _SC_NPROCESSORS_CONF
7078 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7079#endif
7080#ifdef _SC_NPROCESSORS_ONLN
7081 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7082#endif
Fred Draked86ed291999-12-15 15:34:33 +00007083#ifdef _SC_NPROC_CONF
7084 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7085#endif
7086#ifdef _SC_NPROC_ONLN
7087 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7088#endif
Fred Drakec9680921999-12-13 16:37:25 +00007089#ifdef _SC_NZERO
7090 {"SC_NZERO", _SC_NZERO},
7091#endif
7092#ifdef _SC_OPEN_MAX
7093 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7094#endif
7095#ifdef _SC_PAGESIZE
7096 {"SC_PAGESIZE", _SC_PAGESIZE},
7097#endif
7098#ifdef _SC_PAGE_SIZE
7099 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7100#endif
7101#ifdef _SC_PASS_MAX
7102 {"SC_PASS_MAX", _SC_PASS_MAX},
7103#endif
7104#ifdef _SC_PHYS_PAGES
7105 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7106#endif
7107#ifdef _SC_PII
7108 {"SC_PII", _SC_PII},
7109#endif
7110#ifdef _SC_PII_INTERNET
7111 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7112#endif
7113#ifdef _SC_PII_INTERNET_DGRAM
7114 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7115#endif
7116#ifdef _SC_PII_INTERNET_STREAM
7117 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7118#endif
7119#ifdef _SC_PII_OSI
7120 {"SC_PII_OSI", _SC_PII_OSI},
7121#endif
7122#ifdef _SC_PII_OSI_CLTS
7123 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7124#endif
7125#ifdef _SC_PII_OSI_COTS
7126 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7127#endif
7128#ifdef _SC_PII_OSI_M
7129 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7130#endif
7131#ifdef _SC_PII_SOCKET
7132 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7133#endif
7134#ifdef _SC_PII_XTI
7135 {"SC_PII_XTI", _SC_PII_XTI},
7136#endif
7137#ifdef _SC_POLL
7138 {"SC_POLL", _SC_POLL},
7139#endif
7140#ifdef _SC_PRIORITIZED_IO
7141 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7142#endif
7143#ifdef _SC_PRIORITY_SCHEDULING
7144 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7145#endif
7146#ifdef _SC_REALTIME_SIGNALS
7147 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7148#endif
7149#ifdef _SC_RE_DUP_MAX
7150 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7151#endif
7152#ifdef _SC_RTSIG_MAX
7153 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7154#endif
7155#ifdef _SC_SAVED_IDS
7156 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7157#endif
7158#ifdef _SC_SCHAR_MAX
7159 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7160#endif
7161#ifdef _SC_SCHAR_MIN
7162 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7163#endif
7164#ifdef _SC_SELECT
7165 {"SC_SELECT", _SC_SELECT},
7166#endif
7167#ifdef _SC_SEMAPHORES
7168 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7169#endif
7170#ifdef _SC_SEM_NSEMS_MAX
7171 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7172#endif
7173#ifdef _SC_SEM_VALUE_MAX
7174 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7175#endif
7176#ifdef _SC_SHARED_MEMORY_OBJECTS
7177 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7178#endif
7179#ifdef _SC_SHRT_MAX
7180 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7181#endif
7182#ifdef _SC_SHRT_MIN
7183 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7184#endif
7185#ifdef _SC_SIGQUEUE_MAX
7186 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7187#endif
7188#ifdef _SC_SIGRT_MAX
7189 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7190#endif
7191#ifdef _SC_SIGRT_MIN
7192 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7193#endif
Fred Draked86ed291999-12-15 15:34:33 +00007194#ifdef _SC_SOFTPOWER
7195 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7196#endif
Fred Drakec9680921999-12-13 16:37:25 +00007197#ifdef _SC_SPLIT_CACHE
7198 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7199#endif
7200#ifdef _SC_SSIZE_MAX
7201 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7202#endif
7203#ifdef _SC_STACK_PROT
7204 {"SC_STACK_PROT", _SC_STACK_PROT},
7205#endif
7206#ifdef _SC_STREAM_MAX
7207 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7208#endif
7209#ifdef _SC_SYNCHRONIZED_IO
7210 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7211#endif
7212#ifdef _SC_THREADS
7213 {"SC_THREADS", _SC_THREADS},
7214#endif
7215#ifdef _SC_THREAD_ATTR_STACKADDR
7216 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7217#endif
7218#ifdef _SC_THREAD_ATTR_STACKSIZE
7219 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7220#endif
7221#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7222 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7223#endif
7224#ifdef _SC_THREAD_KEYS_MAX
7225 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7226#endif
7227#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7228 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7229#endif
7230#ifdef _SC_THREAD_PRIO_INHERIT
7231 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7232#endif
7233#ifdef _SC_THREAD_PRIO_PROTECT
7234 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7235#endif
7236#ifdef _SC_THREAD_PROCESS_SHARED
7237 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7238#endif
7239#ifdef _SC_THREAD_SAFE_FUNCTIONS
7240 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7241#endif
7242#ifdef _SC_THREAD_STACK_MIN
7243 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7244#endif
7245#ifdef _SC_THREAD_THREADS_MAX
7246 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7247#endif
7248#ifdef _SC_TIMERS
7249 {"SC_TIMERS", _SC_TIMERS},
7250#endif
7251#ifdef _SC_TIMER_MAX
7252 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7253#endif
7254#ifdef _SC_TTY_NAME_MAX
7255 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7256#endif
7257#ifdef _SC_TZNAME_MAX
7258 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7259#endif
7260#ifdef _SC_T_IOV_MAX
7261 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7262#endif
7263#ifdef _SC_UCHAR_MAX
7264 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7265#endif
7266#ifdef _SC_UINT_MAX
7267 {"SC_UINT_MAX", _SC_UINT_MAX},
7268#endif
7269#ifdef _SC_UIO_MAXIOV
7270 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7271#endif
7272#ifdef _SC_ULONG_MAX
7273 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7274#endif
7275#ifdef _SC_USHRT_MAX
7276 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7277#endif
7278#ifdef _SC_VERSION
7279 {"SC_VERSION", _SC_VERSION},
7280#endif
7281#ifdef _SC_WORD_BIT
7282 {"SC_WORD_BIT", _SC_WORD_BIT},
7283#endif
7284#ifdef _SC_XBS5_ILP32_OFF32
7285 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7286#endif
7287#ifdef _SC_XBS5_ILP32_OFFBIG
7288 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7289#endif
7290#ifdef _SC_XBS5_LP64_OFF64
7291 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7292#endif
7293#ifdef _SC_XBS5_LPBIG_OFFBIG
7294 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7295#endif
7296#ifdef _SC_XOPEN_CRYPT
7297 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7298#endif
7299#ifdef _SC_XOPEN_ENH_I18N
7300 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7301#endif
7302#ifdef _SC_XOPEN_LEGACY
7303 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7304#endif
7305#ifdef _SC_XOPEN_REALTIME
7306 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7307#endif
7308#ifdef _SC_XOPEN_REALTIME_THREADS
7309 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7310#endif
7311#ifdef _SC_XOPEN_SHM
7312 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7313#endif
7314#ifdef _SC_XOPEN_UNIX
7315 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7316#endif
7317#ifdef _SC_XOPEN_VERSION
7318 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7319#endif
7320#ifdef _SC_XOPEN_XCU_VERSION
7321 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7322#endif
7323#ifdef _SC_XOPEN_XPG2
7324 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7325#endif
7326#ifdef _SC_XOPEN_XPG3
7327 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7328#endif
7329#ifdef _SC_XOPEN_XPG4
7330 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7331#endif
7332};
7333
7334static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007335conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007336{
7337 return conv_confname(arg, valuep, posix_constants_sysconf,
7338 sizeof(posix_constants_sysconf)
7339 / sizeof(struct constdef));
7340}
7341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007342PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007343"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007344Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007345
7346static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007347posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007348{
7349 PyObject *result = NULL;
7350 int name;
7351
7352 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7353 int value;
7354
7355 errno = 0;
7356 value = sysconf(name);
7357 if (value == -1 && errno != 0)
7358 posix_error();
7359 else
7360 result = PyInt_FromLong(value);
7361 }
7362 return result;
7363}
7364#endif
7365
7366
Fred Drakebec628d1999-12-15 18:31:10 +00007367/* This code is used to ensure that the tables of configuration value names
7368 * are in sorted order as required by conv_confname(), and also to build the
7369 * the exported dictionaries that are used to publish information about the
7370 * names available on the host platform.
7371 *
7372 * Sorting the table at runtime ensures that the table is properly ordered
7373 * when used, even for platforms we're not able to test on. It also makes
7374 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007375 */
Fred Drakebec628d1999-12-15 18:31:10 +00007376
7377static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007378cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007379{
7380 const struct constdef *c1 =
7381 (const struct constdef *) v1;
7382 const struct constdef *c2 =
7383 (const struct constdef *) v2;
7384
7385 return strcmp(c1->name, c2->name);
7386}
7387
7388static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007389setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007390 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007391{
Fred Drakebec628d1999-12-15 18:31:10 +00007392 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007393 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007394
7395 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7396 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007397 if (d == NULL)
7398 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007399
Barry Warsaw3155db32000-04-13 15:20:40 +00007400 for (i=0; i < tablesize; ++i) {
7401 PyObject *o = PyInt_FromLong(table[i].value);
7402 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7403 Py_XDECREF(o);
7404 Py_DECREF(d);
7405 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007406 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007407 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007408 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007409 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007410}
7411
Fred Drakebec628d1999-12-15 18:31:10 +00007412/* Return -1 on failure, 0 on success. */
7413static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007414setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007415{
7416#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007417 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007418 sizeof(posix_constants_pathconf)
7419 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007420 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007421 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007422#endif
7423#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007424 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007425 sizeof(posix_constants_confstr)
7426 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007427 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007428 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007429#endif
7430#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007431 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007432 sizeof(posix_constants_sysconf)
7433 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007434 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007435 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007436#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007437 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007438}
Fred Draked86ed291999-12-15 15:34:33 +00007439
7440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007441PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007442"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007443Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007444in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007445
7446static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007447posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007448{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007449 abort();
7450 /*NOTREACHED*/
7451 Py_FatalError("abort() called from Python code didn't abort!");
7452 return NULL;
7453}
Fred Drakebec628d1999-12-15 18:31:10 +00007454
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007455#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007456PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007457"startfile(filepath [, operation]) - Start a file with its associated\n\
7458application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007459\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007460When \"operation\" is not specified or \"open\", this acts like\n\
7461double-clicking the file in Explorer, or giving the file name as an\n\
7462argument to the DOS \"start\" command: the file is opened with whatever\n\
7463application (if any) its extension is associated.\n\
7464When another \"operation\" is given, it specifies what should be done with\n\
7465the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007466\n\
7467startfile returns as soon as the associated application is launched.\n\
7468There is no option to wait for the application to close, and no way\n\
7469to retrieve the application's exit status.\n\
7470\n\
7471The filepath is relative to the current directory. If you want to use\n\
7472an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007473the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007474
7475static PyObject *
7476win32_startfile(PyObject *self, PyObject *args)
7477{
7478 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007479 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007480 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00007481#ifdef Py_WIN_WIDE_FILENAMES
7482 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007483 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00007484 if (!PyArg_ParseTuple(args, "U|s:startfile",
7485 &unipath, &operation)) {
7486 PyErr_Clear();
7487 goto normal;
7488 }
7489
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007490
7491 if (operation) {
7492 woperation = PyUnicode_DecodeASCII(operation,
7493 strlen(operation), NULL);
7494 if (!woperation) {
7495 PyErr_Clear();
7496 operation = NULL;
7497 goto normal;
7498 }
Georg Brandlad89dc82006-04-03 12:26:26 +00007499 }
7500
7501 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007502 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00007503 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00007504 NULL, NULL, SW_SHOWNORMAL);
7505 Py_END_ALLOW_THREADS
7506
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007507 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00007508 if (rc <= (HINSTANCE)32) {
7509 PyObject *errval = win32_error_unicode("startfile",
7510 PyUnicode_AS_UNICODE(unipath));
7511 return errval;
7512 }
7513 Py_INCREF(Py_None);
7514 return Py_None;
7515 }
7516#endif
7517
7518normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007519 if (!PyArg_ParseTuple(args, "et|s:startfile",
7520 Py_FileSystemDefaultEncoding, &filepath,
7521 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007522 return NULL;
7523 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007524 rc = ShellExecute((HWND)0, operation, filepath,
7525 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007526 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007527 if (rc <= (HINSTANCE)32) {
7528 PyObject *errval = win32_error("startfile", filepath);
7529 PyMem_Free(filepath);
7530 return errval;
7531 }
7532 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007533 Py_INCREF(Py_None);
7534 return Py_None;
7535}
7536#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007537
Martin v. Löwis438b5342002-12-27 10:16:42 +00007538#ifdef HAVE_GETLOADAVG
7539PyDoc_STRVAR(posix_getloadavg__doc__,
7540"getloadavg() -> (float, float, float)\n\n\
7541Return the number of processes in the system run queue averaged over\n\
7542the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7543was unobtainable");
7544
7545static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007546posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007547{
7548 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007549 if (getloadavg(loadavg, 3)!=3) {
7550 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7551 return NULL;
7552 } else
7553 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7554}
7555#endif
7556
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007557#ifdef MS_WINDOWS
7558
7559PyDoc_STRVAR(win32_urandom__doc__,
7560"urandom(n) -> str\n\n\
7561Return a string of n random bytes suitable for cryptographic use.");
7562
7563typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7564 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7565 DWORD dwFlags );
7566typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7567 BYTE *pbBuffer );
7568
7569static CRYPTGENRANDOM pCryptGenRandom = NULL;
7570static HCRYPTPROV hCryptProv = 0;
7571
Tim Peters4ad82172004-08-30 17:02:04 +00007572static PyObject*
7573win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007574{
Tim Petersd3115382004-08-30 17:36:46 +00007575 int howMany;
7576 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007577
Tim Peters4ad82172004-08-30 17:02:04 +00007578 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007579 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007580 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007581 if (howMany < 0)
7582 return PyErr_Format(PyExc_ValueError,
7583 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007584
Tim Peters4ad82172004-08-30 17:02:04 +00007585 if (hCryptProv == 0) {
7586 HINSTANCE hAdvAPI32 = NULL;
7587 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007588
Tim Peters4ad82172004-08-30 17:02:04 +00007589 /* Obtain handle to the DLL containing CryptoAPI
7590 This should not fail */
7591 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7592 if(hAdvAPI32 == NULL)
7593 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007594
Tim Peters4ad82172004-08-30 17:02:04 +00007595 /* Obtain pointers to the CryptoAPI functions
7596 This will fail on some early versions of Win95 */
7597 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7598 hAdvAPI32,
7599 "CryptAcquireContextA");
7600 if (pCryptAcquireContext == NULL)
7601 return PyErr_Format(PyExc_NotImplementedError,
7602 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007603
Tim Peters4ad82172004-08-30 17:02:04 +00007604 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7605 hAdvAPI32, "CryptGenRandom");
7606 if (pCryptAcquireContext == NULL)
7607 return PyErr_Format(PyExc_NotImplementedError,
7608 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007609
Tim Peters4ad82172004-08-30 17:02:04 +00007610 /* Acquire context */
7611 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7612 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7613 return win32_error("CryptAcquireContext", NULL);
7614 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007615
Tim Peters4ad82172004-08-30 17:02:04 +00007616 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00007617 result = PyString_FromStringAndSize(NULL, howMany);
7618 if (result != NULL) {
7619 /* Get random data */
7620 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7621 PyString_AS_STRING(result))) {
7622 Py_DECREF(result);
7623 return win32_error("CryptGenRandom", NULL);
7624 }
Tim Peters4ad82172004-08-30 17:02:04 +00007625 }
Tim Petersd3115382004-08-30 17:36:46 +00007626 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007627}
7628#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007629
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007630static PyMethodDef posix_methods[] = {
7631 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7632#ifdef HAVE_TTYNAME
7633 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7634#endif
7635 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7636 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007637#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007638 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007639#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007640#ifdef HAVE_LCHOWN
7641 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
7642#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007643#ifdef HAVE_CHROOT
7644 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
7645#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007646#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00007647 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007648#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007649#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00007650 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00007651#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00007652 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007653#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00007654#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007655#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007656 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007657#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007658 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7659 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7660 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007661#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007662 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007663#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007664#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007665 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007666#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007667 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7668 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7669 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007670 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007671#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007672 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007673#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007674#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007675 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007676#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007677 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007678#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00007679 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007680#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007681 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7682 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7683 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007684#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00007685 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007686#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007687 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007688#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007689 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7690 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007691#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007692#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007693 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7694 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007695#if defined(PYOS_OS2)
7696 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7697 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
7698#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007699#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007700#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00007701 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007702#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007703#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007704 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007705#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007706#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007707 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007708#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007709#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007710 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007711#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007712#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007713 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007714#endif /* HAVE_GETEGID */
7715#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007716 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007717#endif /* HAVE_GETEUID */
7718#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007719 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007720#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007721#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007722 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007723#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007724 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007725#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007726 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007727#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007728#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007729 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007730#endif /* HAVE_GETPPID */
7731#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007732 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007733#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007734#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007735 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007736#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007737#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007738 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007739#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007740#ifdef HAVE_KILLPG
7741 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7742#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007743#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007744 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007745#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007746#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007747 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007748#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007749 {"popen2", win32_popen2, METH_VARARGS},
7750 {"popen3", win32_popen3, METH_VARARGS},
7751 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007752 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007753#else
7754#if defined(PYOS_OS2) && defined(PYCC_GCC)
7755 {"popen2", os2emx_popen2, METH_VARARGS},
7756 {"popen3", os2emx_popen3, METH_VARARGS},
7757 {"popen4", os2emx_popen4, METH_VARARGS},
7758#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007759#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007760#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007761#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007762 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007763#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007764#ifdef HAVE_SETEUID
7765 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7766#endif /* HAVE_SETEUID */
7767#ifdef HAVE_SETEGID
7768 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7769#endif /* HAVE_SETEGID */
7770#ifdef HAVE_SETREUID
7771 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7772#endif /* HAVE_SETREUID */
7773#ifdef HAVE_SETREGID
7774 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7775#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007776#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007777 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007778#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007779#ifdef HAVE_SETGROUPS
7780 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7781#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007782#ifdef HAVE_GETPGID
7783 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7784#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007785#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007786 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007787#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007788#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007789 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007790#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00007791#ifdef HAVE_WAIT3
7792 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
7793#endif /* HAVE_WAIT3 */
7794#ifdef HAVE_WAIT4
7795 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
7796#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007797#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007798 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007799#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007800#ifdef HAVE_GETSID
7801 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7802#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007803#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007804 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007805#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007806#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007807 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007808#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007809#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007810 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007811#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007812#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007813 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007814#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007815 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7816 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7817 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7818 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7819 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7820 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7821 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7822 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7823 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007824 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007825#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007826 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007827#endif
7828#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007829 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007830#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007831#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007832 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7833#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007834#ifdef HAVE_DEVICE_MACROS
7835 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7836 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7837 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7838#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007839#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007840 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007841#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007842#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007843 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007844#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007845#ifdef HAVE_UNSETENV
7846 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7847#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007848#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007849 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007850#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007851#ifdef HAVE_FCHDIR
7852 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7853#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007854#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007855 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007856#endif
7857#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007858 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007859#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007860#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007861#ifdef WCOREDUMP
7862 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7863#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007864#ifdef WIFCONTINUED
7865 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7866#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007867#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007868 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007869#endif /* WIFSTOPPED */
7870#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007871 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007872#endif /* WIFSIGNALED */
7873#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007874 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007875#endif /* WIFEXITED */
7876#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007877 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007878#endif /* WEXITSTATUS */
7879#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007880 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007881#endif /* WTERMSIG */
7882#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007883 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007884#endif /* WSTOPSIG */
7885#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007886#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007887 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007888#endif
7889#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007890 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007891#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007892#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007893 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007894#endif
7895#ifdef HAVE_TEMPNAM
7896 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7897#endif
7898#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007899 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007900#endif
Fred Drakec9680921999-12-13 16:37:25 +00007901#ifdef HAVE_CONFSTR
7902 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7903#endif
7904#ifdef HAVE_SYSCONF
7905 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7906#endif
7907#ifdef HAVE_FPATHCONF
7908 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7909#endif
7910#ifdef HAVE_PATHCONF
7911 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7912#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007913 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007914#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007915 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7916#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007917#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007918 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007919#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007920 #ifdef MS_WINDOWS
7921 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7922 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007923 {NULL, NULL} /* Sentinel */
7924};
7925
7926
Barry Warsaw4a342091996-12-19 23:50:02 +00007927static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007928ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007929{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007930 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007931}
7932
Guido van Rossumd48f2521997-12-05 22:19:34 +00007933#if defined(PYOS_OS2)
7934/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007935static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007936{
7937 APIRET rc;
7938 ULONG values[QSV_MAX+1];
7939 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007940 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007941
7942 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007943 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007944 Py_END_ALLOW_THREADS
7945
7946 if (rc != NO_ERROR) {
7947 os2_error(rc);
7948 return -1;
7949 }
7950
Fred Drake4d1e64b2002-04-15 19:40:07 +00007951 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7952 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7953 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7954 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7955 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7956 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7957 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007958
7959 switch (values[QSV_VERSION_MINOR]) {
7960 case 0: ver = "2.00"; break;
7961 case 10: ver = "2.10"; break;
7962 case 11: ver = "2.11"; break;
7963 case 30: ver = "3.00"; break;
7964 case 40: ver = "4.00"; break;
7965 case 50: ver = "5.00"; break;
7966 default:
Tim Peters885d4572001-11-28 20:27:42 +00007967 PyOS_snprintf(tmp, sizeof(tmp),
7968 "%d-%d", values[QSV_VERSION_MAJOR],
7969 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007970 ver = &tmp[0];
7971 }
7972
7973 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007974 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007975 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007976
7977 /* Add Indicator of Which Drive was Used to Boot the System */
7978 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7979 tmp[1] = ':';
7980 tmp[2] = '\0';
7981
Fred Drake4d1e64b2002-04-15 19:40:07 +00007982 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007983}
7984#endif
7985
Barry Warsaw4a342091996-12-19 23:50:02 +00007986static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007987all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007988{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007989#ifdef F_OK
7990 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007991#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007992#ifdef R_OK
7993 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007994#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007995#ifdef W_OK
7996 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007997#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007998#ifdef X_OK
7999 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008000#endif
Fred Drakec9680921999-12-13 16:37:25 +00008001#ifdef NGROUPS_MAX
8002 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8003#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008004#ifdef TMP_MAX
8005 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8006#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008007#ifdef WCONTINUED
8008 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8009#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008010#ifdef WNOHANG
8011 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008012#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008013#ifdef WUNTRACED
8014 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8015#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008016#ifdef O_RDONLY
8017 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8018#endif
8019#ifdef O_WRONLY
8020 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8021#endif
8022#ifdef O_RDWR
8023 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8024#endif
8025#ifdef O_NDELAY
8026 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8027#endif
8028#ifdef O_NONBLOCK
8029 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8030#endif
8031#ifdef O_APPEND
8032 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8033#endif
8034#ifdef O_DSYNC
8035 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8036#endif
8037#ifdef O_RSYNC
8038 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8039#endif
8040#ifdef O_SYNC
8041 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8042#endif
8043#ifdef O_NOCTTY
8044 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8045#endif
8046#ifdef O_CREAT
8047 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8048#endif
8049#ifdef O_EXCL
8050 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8051#endif
8052#ifdef O_TRUNC
8053 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8054#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008055#ifdef O_BINARY
8056 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8057#endif
8058#ifdef O_TEXT
8059 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8060#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008061#ifdef O_LARGEFILE
8062 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8063#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008064#ifdef O_SHLOCK
8065 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8066#endif
8067#ifdef O_EXLOCK
8068 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8069#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008070
Tim Peters5aa91602002-01-30 05:46:57 +00008071/* MS Windows */
8072#ifdef O_NOINHERIT
8073 /* Don't inherit in child processes. */
8074 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8075#endif
8076#ifdef _O_SHORT_LIVED
8077 /* Optimize for short life (keep in memory). */
8078 /* MS forgot to define this one with a non-underscore form too. */
8079 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8080#endif
8081#ifdef O_TEMPORARY
8082 /* Automatically delete when last handle is closed. */
8083 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8084#endif
8085#ifdef O_RANDOM
8086 /* Optimize for random access. */
8087 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8088#endif
8089#ifdef O_SEQUENTIAL
8090 /* Optimize for sequential access. */
8091 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8092#endif
8093
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008094/* GNU extensions. */
8095#ifdef O_DIRECT
8096 /* Direct disk access. */
8097 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8098#endif
8099#ifdef O_DIRECTORY
8100 /* Must be a directory. */
8101 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8102#endif
8103#ifdef O_NOFOLLOW
8104 /* Do not follow links. */
8105 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8106#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008107
Barry Warsaw5676bd12003-01-07 20:57:09 +00008108 /* These come from sysexits.h */
8109#ifdef EX_OK
8110 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008111#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008112#ifdef EX_USAGE
8113 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008114#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008115#ifdef EX_DATAERR
8116 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008117#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008118#ifdef EX_NOINPUT
8119 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008120#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008121#ifdef EX_NOUSER
8122 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008123#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008124#ifdef EX_NOHOST
8125 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008126#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008127#ifdef EX_UNAVAILABLE
8128 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008129#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008130#ifdef EX_SOFTWARE
8131 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008132#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008133#ifdef EX_OSERR
8134 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008135#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008136#ifdef EX_OSFILE
8137 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008138#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008139#ifdef EX_CANTCREAT
8140 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008141#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008142#ifdef EX_IOERR
8143 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008144#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008145#ifdef EX_TEMPFAIL
8146 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008147#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008148#ifdef EX_PROTOCOL
8149 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008150#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008151#ifdef EX_NOPERM
8152 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008153#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008154#ifdef EX_CONFIG
8155 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008156#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008157#ifdef EX_NOTFOUND
8158 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008159#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008160
Guido van Rossum246bc171999-02-01 23:54:31 +00008161#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008162#if defined(PYOS_OS2) && defined(PYCC_GCC)
8163 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8164 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8165 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8166 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8167 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8168 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8169 if (ins(d, "P_PM", (long)P_PM)) return -1;
8170 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8171 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8172 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8173 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8174 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8175 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8176 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8177 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8178 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8179 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8180 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8181 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8182 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8183#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008184 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8185 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8186 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8187 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8188 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008189#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008190#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008191
Guido van Rossumd48f2521997-12-05 22:19:34 +00008192#if defined(PYOS_OS2)
8193 if (insertvalues(d)) return -1;
8194#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008195 return 0;
8196}
8197
8198
Tim Peters5aa91602002-01-30 05:46:57 +00008199#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008200#define INITFUNC initnt
8201#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008202
8203#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008204#define INITFUNC initos2
8205#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008206
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008207#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008208#define INITFUNC initposix
8209#define MODNAME "posix"
8210#endif
8211
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008212PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008213INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008214{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008215 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008216
Fred Drake4d1e64b2002-04-15 19:40:07 +00008217 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008218 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008219 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008220 if (m == NULL)
8221 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008222
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008223 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008224 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008225 Py_XINCREF(v);
8226 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008227 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008228 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008229
Fred Drake4d1e64b2002-04-15 19:40:07 +00008230 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008231 return;
8232
Fred Drake4d1e64b2002-04-15 19:40:07 +00008233 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008234 return;
8235
Fred Drake4d1e64b2002-04-15 19:40:07 +00008236 Py_INCREF(PyExc_OSError);
8237 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008238
Guido van Rossumb3d39562000-01-31 18:41:26 +00008239#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008240 if (posix_putenv_garbage == NULL)
8241 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008242#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008243
Guido van Rossum14648392001-12-08 18:02:58 +00008244 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008245 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8246 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8247 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008248 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008249 structseq_new = StatResultType.tp_new;
8250 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00008251 Py_INCREF((PyObject*) &StatResultType);
8252 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008253
Guido van Rossum14648392001-12-08 18:02:58 +00008254 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008255 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008256 Py_INCREF((PyObject*) &StatVFSResultType);
8257 PyModule_AddObject(m, "statvfs_result",
8258 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00008259}
Anthony Baxterac6bd462006-04-13 02:06:09 +00008260
8261#ifdef __cplusplus
8262}
8263#endif
8264