blob: 3e97605e5ef495daebe14cf6be2c58e3a5cb5b01 [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
Ronald Oussorend06b6f22006-04-23 11:59:25 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Anthony Baxterac6bd462006-04-13 02:06:09 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047#ifndef Py_USING_UNICODE
48/* This is used in signatures of functions. */
49#define Py_UNICODE void
50#endif
51
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000052#if defined(PYOS_OS2)
53#define INCL_DOS
54#define INCL_DOSERRORS
55#define INCL_DOSPROCESS
56#define INCL_NOPMAPI
57#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#if defined(PYCC_GCC)
59#include <ctype.h>
60#include <io.h>
61#include <stdio.h>
62#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000063#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000064#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000067#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000069#endif /* HAVE_SYS_TYPES_H */
70
71#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000072#include <sys/stat.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000073#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000074
Guido van Rossum36bc6801995-06-14 22:54:23 +000075#ifdef HAVE_SYS_WAIT_H
76#include <sys/wait.h> /* For WNOHANG */
77#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000078
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000079#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000080#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000081#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000082
Guido van Rossumb6775db1994-08-01 11:34:53 +000083#ifdef HAVE_FCNTL_H
84#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000085#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000086
Guido van Rossuma6535fd2001-10-18 19:44:10 +000087#ifdef HAVE_GRP_H
88#include <grp.h>
89#endif
90
Barry Warsaw5676bd12003-01-07 20:57:09 +000091#ifdef HAVE_SYSEXITS_H
92#include <sysexits.h>
93#endif /* HAVE_SYSEXITS_H */
94
Anthony Baxter8a560de2004-10-13 15:30:56 +000095#ifdef HAVE_SYS_LOADAVG_H
96#include <sys/loadavg.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
119#define HAVE_POPEN 1
120#define HAVE_SYSTEM 1
121#define HAVE_WAIT 1
122#else
123#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000124#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
129#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000130#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define HAVE_FSYNC 1
132#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000133#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000134#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#else /* all other compilers */
137/* Unix functions that the configure script doesn't check for */
138#define HAVE_EXECV 1
139#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
141#define HAVE_FORK1 1
142#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_GETEGID 1
145#define HAVE_GETEUID 1
146#define HAVE_GETGID 1
147#define HAVE_GETPPID 1
148#define HAVE_GETUID 1
149#define HAVE_KILL 1
150#define HAVE_OPENDIR 1
151#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000152#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000154#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#define HAVE_SYSTEM 1
156#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000157#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000158#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#endif /* _MSC_VER */
160#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000161#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000162#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000163
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000165
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000166#if defined(__sgi)&&_COMPILER_VERSION>=700
167/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
168 (default) */
169extern char *ctermid_r(char *);
170#endif
171
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000172#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000173#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000176#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#endif
182#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000183extern int chdir(char *);
184extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int chdir(const char *);
187extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#ifdef __BORLANDC__
190extern int chmod(const char *, int);
191#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000193#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000194extern int chown(const char *, uid_t, gid_t);
195extern char *getcwd(char *, int);
196extern char *strerror(int);
197extern int link(const char *, const char *);
198extern int rename(const char *, const char *);
199extern int stat(const char *, struct stat *);
200extern int unlink(const char *);
201extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000266#define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000268#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000270#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000271#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272
Guido van Rossumd48f2521997-12-05 22:19:34 +0000273#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000275#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276
Tim Petersbc2e10e2002-03-03 23:17:02 +0000277#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000278#if defined(PATH_MAX) && PATH_MAX > 1024
279#define MAXPATHLEN PATH_MAX
280#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000281#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000282#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000283#endif /* MAXPATHLEN */
284
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000285#ifdef UNION_WAIT
286/* Emulate some macros on systems that have a union instead of macros */
287
288#ifndef WIFEXITED
289#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
290#endif
291
292#ifndef WEXITSTATUS
293#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
294#endif
295
296#ifndef WTERMSIG
297#define WTERMSIG(u_wait) ((u_wait).w_termsig)
298#endif
299
Neal Norwitzd5a37542006-03-20 06:48:34 +0000300#define WAIT_TYPE union wait
301#define WAIT_STATUS_INT(s) (s.w_status)
302
303#else /* !UNION_WAIT */
304#define WAIT_TYPE int
305#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000306#endif /* UNION_WAIT */
307
Greg Wardb48bc172000-03-01 21:51:56 +0000308/* Don't use the "_r" form if we don't need it (also, won't have a
309 prototype for it, at least on Solaris -- maybe others as well?). */
310#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
311#define USE_CTERMID_R
312#endif
313
314#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
315#define USE_TMPNAM_R
316#endif
317
Fred Drake699f3522000-06-29 21:12:41 +0000318/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000319#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000320#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000321# define STAT win32_stat
322# define FSTAT win32_fstat
323# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000324#else
325# define STAT stat
326# define FSTAT fstat
327# define STRUCT_STAT struct stat
328#endif
329
Tim Peters11b23062003-04-23 02:39:17 +0000330#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000331#include <sys/mkdev.h>
332#else
333#if defined(MAJOR_IN_SYSMACROS)
334#include <sys/sysmacros.h>
335#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000336#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
337#include <sys/mkdev.h>
338#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000339#endif
Fred Drake699f3522000-06-29 21:12:41 +0000340
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000342#ifdef WITH_NEXT_FRAMEWORK
343/* On Darwin/MacOSX a shared library or framework has no access to
344** environ directly, we must obtain it with _NSGetEnviron().
345*/
346#include <crt_externs.h>
347static char **environ;
348#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000350#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351
Barry Warsaw53699e91996-12-10 23:23:01 +0000352static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000353convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354{
Barry Warsaw53699e91996-12-10 23:23:01 +0000355 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000357 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 if (d == NULL)
359 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000360#ifdef WITH_NEXT_FRAMEWORK
361 if (environ == NULL)
362 environ = *_NSGetEnviron();
363#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364 if (environ == NULL)
365 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000366 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000368 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000369 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370 char *p = strchr(*e, '=');
371 if (p == NULL)
372 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000373 k = PyString_FromStringAndSize(*e, (int)(p-*e));
374 if (k == NULL) {
375 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000377 }
378 v = PyString_FromString(p+1);
379 if (v == NULL) {
380 PyErr_Clear();
381 Py_DECREF(k);
382 continue;
383 }
384 if (PyDict_GetItem(d, k) == NULL) {
385 if (PyDict_SetItem(d, k, v) != 0)
386 PyErr_Clear();
387 }
388 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000389 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000391#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000392 {
393 APIRET rc;
394 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
395
396 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000397 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000398 PyObject *v = PyString_FromString(buffer);
399 PyDict_SetItemString(d, "BEGINLIBPATH", v);
400 Py_DECREF(v);
401 }
402 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
403 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
404 PyObject *v = PyString_FromString(buffer);
405 PyDict_SetItemString(d, "ENDLIBPATH", v);
406 Py_DECREF(v);
407 }
408 }
409#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410 return d;
411}
412
413
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414/* Set a POSIX-specific error from errno, and return NULL */
415
Barry Warsawd58d7641998-07-23 16:14:40 +0000416static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000417posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000418{
Barry Warsawca74da41999-02-09 19:31:45 +0000419 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420}
Barry Warsawd58d7641998-07-23 16:14:40 +0000421static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000422posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000423{
Barry Warsawca74da41999-02-09 19:31:45 +0000424 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000425}
426
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000427#ifdef Py_WIN_WIDE_FILENAMES
428static PyObject *
429posix_error_with_unicode_filename(Py_UNICODE* name)
430{
431 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
432}
433#endif /* Py_WIN_WIDE_FILENAMES */
434
435
Mark Hammondef8b6542001-05-13 08:04:26 +0000436static PyObject *
437posix_error_with_allocated_filename(char* name)
438{
439 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
440 PyMem_Free(name);
441 return rc;
442}
443
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000444#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000445static PyObject *
446win32_error(char* function, char* filename)
447{
Mark Hammond33a6da92000-08-15 00:46:38 +0000448 /* XXX We should pass the function name along in the future.
449 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000450 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000451 Windows error object, which is non-trivial.
452 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000453 errno = GetLastError();
454 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000455 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000456 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000457 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000458}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000459
460#ifdef Py_WIN_WIDE_FILENAMES
461static PyObject *
462win32_error_unicode(char* function, Py_UNICODE* filename)
463{
464 /* XXX - see win32_error for comments on 'function' */
465 errno = GetLastError();
466 if (filename)
467 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
468 else
469 return PyErr_SetFromWindowsErr(errno);
470}
471
472static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
473{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000474}
475
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000476static int
Hirokazu Yamamoto50c60722008-08-17 09:39:06 +0000477convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000478{
Hirokazu Yamamoto50c60722008-08-17 09:39:06 +0000479 if (PyUnicode_CheckExact(*param))
480 Py_INCREF(*param);
481 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000482 /* For a Unicode subtype that's not a Unicode object,
483 return a true Unicode object with the same data. */
Hirokazu Yamamoto50c60722008-08-17 09:39:06 +0000484 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
485 PyUnicode_GET_SIZE(*param));
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000486 else
Hirokazu Yamamoto50c60722008-08-17 09:39:06 +0000487 *param = PyUnicode_FromEncodedObject(*param,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000488 Py_FileSystemDefaultEncoding,
489 "strict");
490 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000491}
492
493#endif /* Py_WIN_WIDE_FILENAMES */
494
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000495#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496
Guido van Rossumd48f2521997-12-05 22:19:34 +0000497#if defined(PYOS_OS2)
498/**********************************************************************
499 * Helper Function to Trim and Format OS/2 Messages
500 **********************************************************************/
501 static void
502os2_formatmsg(char *msgbuf, int msglen, char *reason)
503{
504 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
505
506 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
507 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
508
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000509 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000510 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
511 }
512
513 /* Add Optional Reason Text */
514 if (reason) {
515 strcat(msgbuf, " : ");
516 strcat(msgbuf, reason);
517 }
518}
519
520/**********************************************************************
521 * Decode an OS/2 Operating System Error Code
522 *
523 * A convenience function to lookup an OS/2 error code and return a
524 * text message we can use to raise a Python exception.
525 *
526 * Notes:
527 * The messages for errors returned from the OS/2 kernel reside in
528 * the file OSO001.MSG in the \OS2 directory hierarchy.
529 *
530 **********************************************************************/
531 static char *
532os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
533{
534 APIRET rc;
535 ULONG msglen;
536
537 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
538 Py_BEGIN_ALLOW_THREADS
539 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
540 errorcode, "oso001.msg", &msglen);
541 Py_END_ALLOW_THREADS
542
543 if (rc == NO_ERROR)
544 os2_formatmsg(msgbuf, msglen, reason);
545 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000546 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000547 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000548
549 return msgbuf;
550}
551
552/* Set an OS/2-specific error and return NULL. OS/2 kernel
553 errors are not in a global variable e.g. 'errno' nor are
554 they congruent with posix error numbers. */
555
556static PyObject * os2_error(int code)
557{
558 char text[1024];
559 PyObject *v;
560
561 os2_strerror(text, sizeof(text), code, "");
562
563 v = Py_BuildValue("(is)", code, text);
564 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000565 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000566 Py_DECREF(v);
567 }
568 return NULL; /* Signal to Python that an Exception is Pending */
569}
570
571#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572
573/* POSIX generic methods */
574
Barry Warsaw53699e91996-12-10 23:23:01 +0000575static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000576posix_fildes(PyObject *fdobj, int (*func)(int))
577{
578 int fd;
579 int res;
580 fd = PyObject_AsFileDescriptor(fdobj);
581 if (fd < 0)
582 return NULL;
583 Py_BEGIN_ALLOW_THREADS
584 res = (*func)(fd);
585 Py_END_ALLOW_THREADS
586 if (res < 0)
587 return posix_error();
588 Py_INCREF(Py_None);
589 return Py_None;
590}
Guido van Rossum21142a01999-01-08 21:05:37 +0000591
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000592#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000593static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000594unicode_file_names(void)
595{
596 static int canusewide = -1;
597 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000598 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000599 the Windows NT family. */
600 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
601 }
602 return canusewide;
603}
604#endif
Tim Peters11b23062003-04-23 02:39:17 +0000605
Guido van Rossum21142a01999-01-08 21:05:37 +0000606static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000607posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608{
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000610 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000611 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000612 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000614 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000615 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000616 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000617 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000618 return posix_error_with_allocated_filename(path1);
619 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000620 Py_INCREF(Py_None);
621 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000622}
623
Barry Warsaw53699e91996-12-10 23:23:01 +0000624static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000625posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000626 char *format,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000627 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628{
Mark Hammondef8b6542001-05-13 08:04:26 +0000629 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000630 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000631 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000632 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000633 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000634 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000635 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000636 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000637 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000638 PyMem_Free(path1);
639 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000640 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000641 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000643 Py_INCREF(Py_None);
644 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645}
646
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000647#ifdef Py_WIN_WIDE_FILENAMES
648static PyObject*
649win32_1str(PyObject* args, char* func,
650 char* format, BOOL (__stdcall *funcA)(LPCSTR),
651 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
652{
653 PyObject *uni;
654 char *ansi;
655 BOOL result;
656 if (unicode_file_names()) {
657 if (!PyArg_ParseTuple(args, wformat, &uni))
658 PyErr_Clear();
659 else {
660 Py_BEGIN_ALLOW_THREADS
661 result = funcW(PyUnicode_AsUnicode(uni));
662 Py_END_ALLOW_THREADS
663 if (!result)
664 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
665 Py_INCREF(Py_None);
666 return Py_None;
667 }
668 }
669 if (!PyArg_ParseTuple(args, format, &ansi))
670 return NULL;
671 Py_BEGIN_ALLOW_THREADS
672 result = funcA(ansi);
673 Py_END_ALLOW_THREADS
674 if (!result)
675 return win32_error(func, ansi);
676 Py_INCREF(Py_None);
677 return Py_None;
678
679}
680
681/* This is a reimplementation of the C library's chdir function,
682 but one that produces Win32 errors instead of DOS error codes.
683 chdir is essentially a wrapper around SetCurrentDirectory; however,
684 it also needs to set "magic" environment variables indicating
685 the per-drive current directory, which are of the form =<drive>: */
686BOOL __stdcall
687win32_chdir(LPCSTR path)
688{
689 char new_path[MAX_PATH+1];
690 int result;
691 char env[4] = "=x:";
692
693 if(!SetCurrentDirectoryA(path))
694 return FALSE;
695 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
696 if (!result)
697 return FALSE;
698 /* In the ANSI API, there should not be any paths longer
699 than MAX_PATH. */
700 assert(result <= MAX_PATH+1);
701 if (strncmp(new_path, "\\\\", 2) == 0 ||
702 strncmp(new_path, "//", 2) == 0)
703 /* UNC path, nothing to do. */
704 return TRUE;
705 env[1] = new_path[0];
706 return SetEnvironmentVariableA(env, new_path);
707}
708
709/* The Unicode version differs from the ANSI version
710 since the current directory might exceed MAX_PATH characters */
711BOOL __stdcall
712win32_wchdir(LPCWSTR path)
713{
714 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
715 int result;
716 wchar_t env[4] = L"=x:";
717
718 if(!SetCurrentDirectoryW(path))
719 return FALSE;
720 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
721 if (!result)
722 return FALSE;
723 if (result > MAX_PATH+1) {
724 new_path = malloc(result);
725 if (!new_path) {
726 SetLastError(ERROR_OUTOFMEMORY);
727 return FALSE;
728 }
729 }
730 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
731 wcsncmp(new_path, L"//", 2) == 0)
732 /* UNC path, nothing to do. */
733 return TRUE;
734 env[1] = new_path[0];
735 result = SetEnvironmentVariableW(env, new_path);
736 if (new_path != _new_path)
737 free(new_path);
738 return result;
739}
740#endif
741
Martin v. Löwis14694662006-02-03 12:54:16 +0000742#ifdef MS_WINDOWS
743/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
744 - time stamps are restricted to second resolution
745 - file modification times suffer from forth-and-back conversions between
746 UTC and local time
747 Therefore, we implement our own stat, based on the Win32 API directly.
748*/
749#define HAVE_STAT_NSEC 1
750
751struct win32_stat{
752 int st_dev;
753 __int64 st_ino;
754 unsigned short st_mode;
755 int st_nlink;
756 int st_uid;
757 int st_gid;
758 int st_rdev;
759 __int64 st_size;
760 int st_atime;
761 int st_atime_nsec;
762 int st_mtime;
763 int st_mtime_nsec;
764 int st_ctime;
765 int st_ctime_nsec;
766};
767
768static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
769
770static void
771FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
772{
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000773 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
774 /* Cannot simply cast and dereference in_ptr,
775 since it might not be aligned properly */
776 __int64 in;
777 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000778 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
779 /* XXX Win32 supports time stamps past 2038; we currently don't */
780 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
781}
782
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000783static void
784time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
785{
786 /* XXX endianness */
787 __int64 out;
788 out = time_in + secs_between_epochs;
Martin v. Löwis463a42b2006-10-09 20:44:50 +0000789 out = out * 10000000 + nsec_in / 100;
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000790 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000791}
792
Martin v. Löwis14694662006-02-03 12:54:16 +0000793/* Below, we *know* that ugo+r is 0444 */
794#if _S_IREAD != 0400
795#error Unsupported C library
796#endif
797static int
798attributes_to_mode(DWORD attr)
799{
800 int m = 0;
801 if (attr & FILE_ATTRIBUTE_DIRECTORY)
802 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
803 else
804 m |= _S_IFREG;
805 if (attr & FILE_ATTRIBUTE_READONLY)
806 m |= 0444;
807 else
808 m |= 0666;
809 return m;
810}
811
812static int
813attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
814{
815 memset(result, 0, sizeof(*result));
816 result->st_mode = attributes_to_mode(info->dwFileAttributes);
817 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
818 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
819 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
820 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
821
822 return 0;
823}
824
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000825/* Emulate GetFileAttributesEx[AW] on Windows 95 */
826static int checked = 0;
827static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
828static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
829static void
830check_gfax()
831{
832 HINSTANCE hKernel32;
833 if (checked)
834 return;
835 checked = 1;
836 hKernel32 = GetModuleHandle("KERNEL32");
837 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
838 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
839}
840
Martin v. Löwis88635442007-04-04 18:30:56 +0000841static BOOL
842attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
843{
844 HANDLE hFindFile;
845 WIN32_FIND_DATAA FileData;
846 hFindFile = FindFirstFileA(pszFile, &FileData);
847 if (hFindFile == INVALID_HANDLE_VALUE)
848 return FALSE;
849 FindClose(hFindFile);
850 pfad->dwFileAttributes = FileData.dwFileAttributes;
851 pfad->ftCreationTime = FileData.ftCreationTime;
852 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
853 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
854 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
855 pfad->nFileSizeLow = FileData.nFileSizeLow;
856 return TRUE;
857}
858
859static BOOL
860attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
861{
862 HANDLE hFindFile;
863 WIN32_FIND_DATAW FileData;
864 hFindFile = FindFirstFileW(pszFile, &FileData);
865 if (hFindFile == INVALID_HANDLE_VALUE)
866 return FALSE;
867 FindClose(hFindFile);
868 pfad->dwFileAttributes = FileData.dwFileAttributes;
869 pfad->ftCreationTime = FileData.ftCreationTime;
870 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
871 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
872 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
873 pfad->nFileSizeLow = FileData.nFileSizeLow;
874 return TRUE;
875}
876
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000877static BOOL WINAPI
878Py_GetFileAttributesExA(LPCSTR pszFile,
879 GET_FILEEX_INFO_LEVELS level,
880 LPVOID pv)
881{
882 BOOL result;
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000883 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
884 /* First try to use the system's implementation, if that is
885 available and either succeeds to gives an error other than
886 that it isn't implemented. */
887 check_gfax();
888 if (gfaxa) {
889 result = gfaxa(pszFile, level, pv);
890 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
891 return result;
892 }
893 /* It's either not present, or not implemented.
894 Emulate using FindFirstFile. */
895 if (level != GetFileExInfoStandard) {
896 SetLastError(ERROR_INVALID_PARAMETER);
897 return FALSE;
898 }
899 /* Use GetFileAttributes to validate that the file name
900 does not contain wildcards (which FindFirstFile would
901 accept). */
902 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
903 return FALSE;
Martin v. Löwis88635442007-04-04 18:30:56 +0000904 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000905}
906
907static BOOL WINAPI
908Py_GetFileAttributesExW(LPCWSTR pszFile,
909 GET_FILEEX_INFO_LEVELS level,
910 LPVOID pv)
911{
912 BOOL result;
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000913 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
914 /* First try to use the system's implementation, if that is
915 available and either succeeds to gives an error other than
916 that it isn't implemented. */
917 check_gfax();
918 if (gfaxa) {
919 result = gfaxw(pszFile, level, pv);
920 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
921 return result;
922 }
923 /* It's either not present, or not implemented.
924 Emulate using FindFirstFile. */
925 if (level != GetFileExInfoStandard) {
926 SetLastError(ERROR_INVALID_PARAMETER);
927 return FALSE;
928 }
929 /* Use GetFileAttributes to validate that the file name
930 does not contain wildcards (which FindFirstFile would
931 accept). */
932 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
933 return FALSE;
Martin v. Löwis88635442007-04-04 18:30:56 +0000934 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000935}
936
Martin v. Löwis14694662006-02-03 12:54:16 +0000937static int
938win32_stat(const char* path, struct win32_stat *result)
939{
940 WIN32_FILE_ATTRIBUTE_DATA info;
941 int code;
942 char *dot;
943 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000944 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis88635442007-04-04 18:30:56 +0000945 if (GetLastError() != ERROR_SHARING_VIOLATION) {
946 /* Protocol violation: we explicitly clear errno, instead of
947 setting it to a POSIX error. Callers should use GetLastError. */
948 errno = 0;
949 return -1;
950 } else {
951 /* Could not get attributes on open file. Fall back to
952 reading the directory. */
953 if (!attributes_from_dir(path, &info)) {
954 /* Very strange. This should not fail now */
955 errno = 0;
956 return -1;
957 }
958 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000959 }
960 code = attribute_data_to_stat(&info, result);
961 if (code != 0)
962 return code;
963 /* Set S_IFEXEC if it is an .exe, .bat, ... */
964 dot = strrchr(path, '.');
965 if (dot) {
966 if (stricmp(dot, ".bat") == 0 ||
967 stricmp(dot, ".cmd") == 0 ||
968 stricmp(dot, ".exe") == 0 ||
969 stricmp(dot, ".com") == 0)
970 result->st_mode |= 0111;
971 }
972 return code;
973}
974
975static int
976win32_wstat(const wchar_t* path, struct win32_stat *result)
977{
978 int code;
979 const wchar_t *dot;
980 WIN32_FILE_ATTRIBUTE_DATA info;
981 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis2c7aa632006-10-15 09:44:02 +0000982 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis88635442007-04-04 18:30:56 +0000983 if (GetLastError() != ERROR_SHARING_VIOLATION) {
984 /* Protocol violation: we explicitly clear errno, instead of
985 setting it to a POSIX error. Callers should use GetLastError. */
986 errno = 0;
987 return -1;
988 } else {
989 /* Could not get attributes on open file. Fall back to
990 reading the directory. */
991 if (!attributes_from_dir_w(path, &info)) {
992 /* Very strange. This should not fail now */
993 errno = 0;
994 return -1;
995 }
996 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000997 }
998 code = attribute_data_to_stat(&info, result);
999 if (code < 0)
1000 return code;
1001 /* Set IFEXEC if it is an .exe, .bat, ... */
1002 dot = wcsrchr(path, '.');
1003 if (dot) {
1004 if (_wcsicmp(dot, L".bat") == 0 ||
1005 _wcsicmp(dot, L".cmd") == 0 ||
1006 _wcsicmp(dot, L".exe") == 0 ||
1007 _wcsicmp(dot, L".com") == 0)
1008 result->st_mode |= 0111;
1009 }
1010 return code;
1011}
1012
1013static int
1014win32_fstat(int file_number, struct win32_stat *result)
1015{
1016 BY_HANDLE_FILE_INFORMATION info;
1017 HANDLE h;
1018 int type;
1019
1020 h = (HANDLE)_get_osfhandle(file_number);
1021
1022 /* Protocol violation: we explicitly clear errno, instead of
1023 setting it to a POSIX error. Callers should use GetLastError. */
1024 errno = 0;
1025
1026 if (h == INVALID_HANDLE_VALUE) {
1027 /* This is really a C library error (invalid file handle).
1028 We set the Win32 error to the closes one matching. */
1029 SetLastError(ERROR_INVALID_HANDLE);
1030 return -1;
1031 }
1032 memset(result, 0, sizeof(*result));
1033
1034 type = GetFileType(h);
1035 if (type == FILE_TYPE_UNKNOWN) {
1036 DWORD error = GetLastError();
1037 if (error != 0) {
1038 return -1;
1039 }
1040 /* else: valid but unknown file */
1041 }
1042
1043 if (type != FILE_TYPE_DISK) {
1044 if (type == FILE_TYPE_CHAR)
1045 result->st_mode = _S_IFCHR;
1046 else if (type == FILE_TYPE_PIPE)
1047 result->st_mode = _S_IFIFO;
1048 return 0;
1049 }
1050
1051 if (!GetFileInformationByHandle(h, &info)) {
1052 return -1;
1053 }
1054
1055 /* similar to stat() */
1056 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1057 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1058 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1059 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1060 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1061 /* specific to fstat() */
1062 result->st_nlink = info.nNumberOfLinks;
1063 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1064 return 0;
1065}
1066
1067#endif /* MS_WINDOWS */
1068
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001069PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070"stat_result: Result from stat or lstat.\n\n\
1071This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001072 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001073or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1074\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001075Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1076or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001077\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001078See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001079
1080static PyStructSequence_Field stat_result_fields[] = {
1081 {"st_mode", "protection bits"},
1082 {"st_ino", "inode"},
1083 {"st_dev", "device"},
1084 {"st_nlink", "number of hard links"},
1085 {"st_uid", "user ID of owner"},
1086 {"st_gid", "group ID of owner"},
1087 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001088 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1089 {NULL, "integer time of last access"},
1090 {NULL, "integer time of last modification"},
1091 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001092 {"st_atime", "time of last access"},
1093 {"st_mtime", "time of last modification"},
1094 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001095#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001096 {"st_blksize", "blocksize for filesystem I/O"},
1097#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001098#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001099 {"st_blocks", "number of blocks allocated"},
1100#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001101#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001102 {"st_rdev", "device type (if inode device)"},
1103#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001104#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1105 {"st_flags", "user defined flags for file"},
1106#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001107#ifdef HAVE_STRUCT_STAT_ST_GEN
1108 {"st_gen", "generation number"},
1109#endif
1110#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1111 {"st_birthtime", "time of creation"},
1112#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001113 {0}
1114};
1115
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001116#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001117#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001118#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001119#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001120#endif
1121
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001122#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001123#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1124#else
1125#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1126#endif
1127
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001128#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001129#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1130#else
1131#define ST_RDEV_IDX ST_BLOCKS_IDX
1132#endif
1133
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001134#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1135#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1136#else
1137#define ST_FLAGS_IDX ST_RDEV_IDX
1138#endif
1139
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001140#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001141#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001142#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001143#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001144#endif
1145
1146#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1147#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1148#else
1149#define ST_BIRTHTIME_IDX ST_GEN_IDX
1150#endif
1151
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001152static PyStructSequence_Desc stat_result_desc = {
1153 "stat_result", /* name */
1154 stat_result__doc__, /* doc */
1155 stat_result_fields,
1156 10
1157};
1158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001159PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001160"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1161This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001162 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001163or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001164\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001166
1167static PyStructSequence_Field statvfs_result_fields[] = {
1168 {"f_bsize", },
1169 {"f_frsize", },
1170 {"f_blocks", },
1171 {"f_bfree", },
1172 {"f_bavail", },
1173 {"f_files", },
1174 {"f_ffree", },
1175 {"f_favail", },
1176 {"f_flag", },
1177 {"f_namemax",},
1178 {0}
1179};
1180
1181static PyStructSequence_Desc statvfs_result_desc = {
1182 "statvfs_result", /* name */
1183 statvfs_result__doc__, /* doc */
1184 statvfs_result_fields,
1185 10
1186};
1187
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001188static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001189static PyTypeObject StatResultType;
1190static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001191static newfunc structseq_new;
1192
1193static PyObject *
1194statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1195{
1196 PyStructSequence *result;
1197 int i;
1198
1199 result = (PyStructSequence*)structseq_new(type, args, kwds);
1200 if (!result)
1201 return NULL;
1202 /* If we have been initialized from a tuple,
1203 st_?time might be set to None. Initialize it
1204 from the int slots. */
1205 for (i = 7; i <= 9; i++) {
1206 if (result->ob_item[i+3] == Py_None) {
1207 Py_DECREF(Py_None);
1208 Py_INCREF(result->ob_item[i]);
1209 result->ob_item[i+3] = result->ob_item[i];
1210 }
1211 }
1212 return (PyObject*)result;
1213}
1214
1215
1216
1217/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001218static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001219
1220PyDoc_STRVAR(stat_float_times__doc__,
1221"stat_float_times([newval]) -> oldval\n\n\
1222Determine whether os.[lf]stat represents time stamps as float objects.\n\
1223If newval is True, future calls to stat() return floats, if it is False,\n\
1224future calls return ints. \n\
1225If newval is omitted, return the current setting.\n");
1226
1227static PyObject*
1228stat_float_times(PyObject* self, PyObject *args)
1229{
1230 int newval = -1;
1231 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1232 return NULL;
1233 if (newval == -1)
1234 /* Return old value */
1235 return PyBool_FromLong(_stat_float_times);
1236 _stat_float_times = newval;
1237 Py_INCREF(Py_None);
1238 return Py_None;
1239}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001240
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001241static void
1242fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1243{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001244 PyObject *fval,*ival;
1245#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001246 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001247#else
1248 ival = PyInt_FromLong((long)sec);
1249#endif
Neal Norwitze0a81af2006-08-12 01:50:38 +00001250 if (!ival)
1251 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001252 if (_stat_float_times) {
1253 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1254 } else {
1255 fval = ival;
1256 Py_INCREF(fval);
1257 }
1258 PyStructSequence_SET_ITEM(v, index, ival);
1259 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001260}
1261
Tim Peters5aa91602002-01-30 05:46:57 +00001262/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001263 (used by posix_stat() and posix_fstat()) */
1264static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001265_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001266{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001267 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001268 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001269 if (v == NULL)
1270 return NULL;
1271
Martin v. Löwis14694662006-02-03 12:54:16 +00001272 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001273#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001274 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001275 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001276#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001277 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001278#endif
1279#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001280 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001281 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001282#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001283 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001284#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001285 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1286 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1287 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001288#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001289 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001290 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001291#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001292 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001293#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001294
Martin v. Löwis14694662006-02-03 12:54:16 +00001295#if defined(HAVE_STAT_TV_NSEC)
1296 ansec = st->st_atim.tv_nsec;
1297 mnsec = st->st_mtim.tv_nsec;
1298 cnsec = st->st_ctim.tv_nsec;
1299#elif defined(HAVE_STAT_TV_NSEC2)
1300 ansec = st->st_atimespec.tv_nsec;
1301 mnsec = st->st_mtimespec.tv_nsec;
1302 cnsec = st->st_ctimespec.tv_nsec;
1303#elif defined(HAVE_STAT_NSEC)
1304 ansec = st->st_atime_nsec;
1305 mnsec = st->st_mtime_nsec;
1306 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001307#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001308 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001309#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001310 fill_time(v, 7, st->st_atime, ansec);
1311 fill_time(v, 8, st->st_mtime, mnsec);
1312 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001313
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001314#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001315 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001316 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001317#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001318#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001319 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001320 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001321#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001322#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001323 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001324 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001325#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001326#ifdef HAVE_STRUCT_STAT_ST_GEN
1327 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001328 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001329#endif
1330#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1331 {
1332 PyObject *val;
1333 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001334 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001335#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001336 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001337#else
1338 bnsec = 0;
1339#endif
1340 if (_stat_float_times) {
1341 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1342 } else {
1343 val = PyInt_FromLong((long)bsec);
1344 }
1345 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1346 val);
1347 }
1348#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001349#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1350 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001351 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001352#endif
Fred Drake699f3522000-06-29 21:12:41 +00001353
1354 if (PyErr_Occurred()) {
1355 Py_DECREF(v);
1356 return NULL;
1357 }
1358
1359 return v;
1360}
1361
Martin v. Löwisd8948722004-06-02 09:57:56 +00001362#ifdef MS_WINDOWS
1363
1364/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1365 where / can be used in place of \ and the trailing slash is optional.
1366 Both SERVER and SHARE must have at least one character.
1367*/
1368
1369#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1370#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001371#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001372#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001373#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001374
Tim Peters4ad82172004-08-30 17:02:04 +00001375static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001376IsUNCRootA(char *path, int pathlen)
1377{
1378 #define ISSLASH ISSLASHA
1379
1380 int i, share;
1381
1382 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1383 /* minimum UNCRoot is \\x\y */
1384 return FALSE;
1385 for (i = 2; i < pathlen ; i++)
1386 if (ISSLASH(path[i])) break;
1387 if (i == 2 || i == pathlen)
1388 /* do not allow \\\SHARE or \\SERVER */
1389 return FALSE;
1390 share = i+1;
1391 for (i = share; i < pathlen; i++)
1392 if (ISSLASH(path[i])) break;
1393 return (i != share && (i == pathlen || i == pathlen-1));
1394
1395 #undef ISSLASH
1396}
1397
1398#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001399static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001400IsUNCRootW(Py_UNICODE *path, int pathlen)
1401{
1402 #define ISSLASH ISSLASHW
1403
1404 int i, share;
1405
1406 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1407 /* minimum UNCRoot is \\x\y */
1408 return FALSE;
1409 for (i = 2; i < pathlen ; i++)
1410 if (ISSLASH(path[i])) break;
1411 if (i == 2 || i == pathlen)
1412 /* do not allow \\\SHARE or \\SERVER */
1413 return FALSE;
1414 share = i+1;
1415 for (i = share; i < pathlen; i++)
1416 if (ISSLASH(path[i])) break;
1417 return (i != share && (i == pathlen || i == pathlen-1));
1418
1419 #undef ISSLASH
1420}
1421#endif /* Py_WIN_WIDE_FILENAMES */
1422#endif /* MS_WINDOWS */
1423
Barry Warsaw53699e91996-12-10 23:23:01 +00001424static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001425posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001426 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001427#ifdef __VMS
1428 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1429#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001430 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001431#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001432 char *wformat,
1433 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001434{
Fred Drake699f3522000-06-29 21:12:41 +00001435 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001436 char *path = NULL; /* pass this to stat; do not free() it */
1437 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001438 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001439 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001440
1441#ifdef Py_WIN_WIDE_FILENAMES
1442 /* If on wide-character-capable OS see if argument
1443 is Unicode and if so use wide API. */
1444 if (unicode_file_names()) {
1445 PyUnicodeObject *po;
1446 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001447 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1448
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001449 Py_BEGIN_ALLOW_THREADS
1450 /* PyUnicode_AS_UNICODE result OK without
1451 thread lock as it is a simple dereference. */
1452 res = wstatfunc(wpath, &st);
1453 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001454
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001455 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001456 return win32_error_unicode("stat", wpath);
1457 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001458 }
1459 /* Drop the argument parsing error as narrow strings
1460 are also valid. */
1461 PyErr_Clear();
1462 }
1463#endif
1464
Tim Peters5aa91602002-01-30 05:46:57 +00001465 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001466 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001467 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001468 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001469
Barry Warsaw53699e91996-12-10 23:23:01 +00001470 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001471 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001472 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001473
1474 if (res != 0) {
1475#ifdef MS_WINDOWS
1476 result = win32_error("stat", pathfree);
1477#else
1478 result = posix_error_with_filename(pathfree);
1479#endif
1480 }
1481 else
1482 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001483
Tim Peters500bd032001-12-19 19:05:01 +00001484 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001485 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001486}
1487
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001488/* POSIX methods */
1489
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001490PyDoc_STRVAR(posix_access__doc__,
Georg Brandl3d26ef12007-01-27 19:38:56 +00001491"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001492Use the real uid/gid to test for access to a path. Note that most\n\
1493operations will use the effective uid/gid, therefore this routine can\n\
1494be used in a suid/sgid environment to test if the invoking user has the\n\
1495specified access to the path. The mode argument can be F_OK to test\n\
1496existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001497
1498static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001499posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001500{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001501 char *path;
1502 int mode;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001503
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001504#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001505 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001506 if (unicode_file_names()) {
1507 PyUnicodeObject *po;
1508 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1509 Py_BEGIN_ALLOW_THREADS
1510 /* PyUnicode_AS_UNICODE OK without thread lock as
1511 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001512 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001513 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001514 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001515 }
1516 /* Drop the argument parsing error as narrow strings
1517 are also valid. */
1518 PyErr_Clear();
1519 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001520 if (!PyArg_ParseTuple(args, "eti:access",
1521 Py_FileSystemDefaultEncoding, &path, &mode))
1522 return 0;
1523 Py_BEGIN_ALLOW_THREADS
1524 attr = GetFileAttributesA(path);
1525 Py_END_ALLOW_THREADS
1526 PyMem_Free(path);
1527finish:
1528 if (attr == 0xFFFFFFFF)
1529 /* File does not exist, or cannot read attributes */
1530 return PyBool_FromLong(0);
1531 /* Access is possible if either write access wasn't requested, or
Martin v. Löwisc8dbc922007-12-03 22:39:10 +00001532 the file isn't read-only, or if it's a directory, as there are
1533 no read-only directories on Windows. */
1534 return PyBool_FromLong(!(mode & 2)
1535 || !(attr & FILE_ATTRIBUTE_READONLY)
1536 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001537#else
1538 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001539 if (!PyArg_ParseTuple(args, "eti:access",
1540 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001541 return NULL;
1542 Py_BEGIN_ALLOW_THREADS
1543 res = access(path, mode);
1544 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001545 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001546 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001547#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001548}
1549
Guido van Rossumd371ff11999-01-25 16:12:23 +00001550#ifndef F_OK
1551#define F_OK 0
1552#endif
1553#ifndef R_OK
1554#define R_OK 4
1555#endif
1556#ifndef W_OK
1557#define W_OK 2
1558#endif
1559#ifndef X_OK
1560#define X_OK 1
1561#endif
1562
1563#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001565"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001567
1568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001569posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001570{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001571 int id;
1572 char *ret;
1573
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001574 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001575 return NULL;
1576
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001577#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001578 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001579 if (id == 0) {
1580 ret = ttyname();
1581 }
1582 else {
1583 ret = NULL;
1584 }
1585#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001586 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001587#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001588 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001589 return posix_error();
1590 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001591}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001592#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001593
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001594#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001596"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001598
1599static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001600posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001601{
1602 char *ret;
1603 char buffer[L_ctermid];
1604
Greg Wardb48bc172000-03-01 21:51:56 +00001605#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001606 ret = ctermid_r(buffer);
1607#else
1608 ret = ctermid(buffer);
1609#endif
1610 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001611 return posix_error();
1612 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001613}
1614#endif
1615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001616PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001617"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001618Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001619
Barry Warsaw53699e91996-12-10 23:23:01 +00001620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001621posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001622{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001623#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001624 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001625#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001626 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001627#elif defined(__VMS)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001628 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001629#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001630 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001631#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001632}
1633
Fred Drake4d1e64b2002-04-15 19:40:07 +00001634#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001635PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001636"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001637Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001638opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001639
1640static PyObject *
1641posix_fchdir(PyObject *self, PyObject *fdobj)
1642{
1643 return posix_fildes(fdobj, fchdir);
1644}
1645#endif /* HAVE_FCHDIR */
1646
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001648PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001649"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001651
Barry Warsaw53699e91996-12-10 23:23:01 +00001652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001653posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001654{
Mark Hammondef8b6542001-05-13 08:04:26 +00001655 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001656 int i;
1657 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001658#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001659 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001660 if (unicode_file_names()) {
1661 PyUnicodeObject *po;
1662 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1663 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001664 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1665 if (attr != 0xFFFFFFFF) {
1666 if (i & _S_IWRITE)
1667 attr &= ~FILE_ATTRIBUTE_READONLY;
1668 else
1669 attr |= FILE_ATTRIBUTE_READONLY;
1670 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1671 }
1672 else
1673 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001674 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001675 if (!res)
1676 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001677 PyUnicode_AS_UNICODE(po));
1678 Py_INCREF(Py_None);
1679 return Py_None;
1680 }
1681 /* Drop the argument parsing error as narrow strings
1682 are also valid. */
1683 PyErr_Clear();
1684 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001685 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1686 &path, &i))
1687 return NULL;
1688 Py_BEGIN_ALLOW_THREADS
1689 attr = GetFileAttributesA(path);
1690 if (attr != 0xFFFFFFFF) {
1691 if (i & _S_IWRITE)
1692 attr &= ~FILE_ATTRIBUTE_READONLY;
1693 else
1694 attr |= FILE_ATTRIBUTE_READONLY;
1695 res = SetFileAttributesA(path, attr);
1696 }
1697 else
1698 res = 0;
1699 Py_END_ALLOW_THREADS
1700 if (!res) {
1701 win32_error("chmod", path);
1702 PyMem_Free(path);
1703 return NULL;
1704 }
Martin v. Löwis9f485bc2006-05-08 05:25:56 +00001705 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001706 Py_INCREF(Py_None);
1707 return Py_None;
1708#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001709 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001710 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001711 return NULL;
1712 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001713 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001714 Py_END_ALLOW_THREADS
1715 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001716 return posix_error_with_allocated_filename(path);
1717 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001718 Py_INCREF(Py_None);
1719 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001720#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001721}
1722
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001723
Martin v. Löwis244edc82001-10-04 22:44:26 +00001724#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001725PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001726"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001727Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001728
1729static PyObject *
1730posix_chroot(PyObject *self, PyObject *args)
1731{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001732 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001733}
1734#endif
1735
Guido van Rossum21142a01999-01-08 21:05:37 +00001736#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001737PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001738"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001739force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001740
1741static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001742posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001743{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001744 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001745}
1746#endif /* HAVE_FSYNC */
1747
1748#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001749
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001750#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001751extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1752#endif
1753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001754PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001755"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001756force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001757 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001758
1759static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001760posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001761{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001762 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001763}
1764#endif /* HAVE_FDATASYNC */
1765
1766
Fredrik Lundh10723342000-07-10 16:38:09 +00001767#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001769"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001771
Barry Warsaw53699e91996-12-10 23:23:01 +00001772static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001773posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001774{
Mark Hammondef8b6542001-05-13 08:04:26 +00001775 char *path = NULL;
Gregory P. Smith631df752008-03-18 19:21:40 +00001776 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001777 int res;
Gregory P. Smith631df752008-03-18 19:21:40 +00001778 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001779 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001780 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001781 return NULL;
1782 Py_BEGIN_ALLOW_THREADS
1783 res = chown(path, (uid_t) uid, (gid_t) gid);
1784 Py_END_ALLOW_THREADS
1785 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001786 return posix_error_with_allocated_filename(path);
1787 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001788 Py_INCREF(Py_None);
1789 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001790}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001791#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001792
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001793#ifdef HAVE_LCHOWN
1794PyDoc_STRVAR(posix_lchown__doc__,
1795"lchown(path, uid, gid)\n\n\
1796Change the owner and group id of path to the numeric uid and gid.\n\
1797This function will not follow symbolic links.");
1798
1799static PyObject *
1800posix_lchown(PyObject *self, PyObject *args)
1801{
1802 char *path = NULL;
1803 int uid, gid;
1804 int res;
1805 if (!PyArg_ParseTuple(args, "etii:lchown",
1806 Py_FileSystemDefaultEncoding, &path,
1807 &uid, &gid))
1808 return NULL;
1809 Py_BEGIN_ALLOW_THREADS
1810 res = lchown(path, (uid_t) uid, (gid_t) gid);
1811 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001812 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001813 return posix_error_with_allocated_filename(path);
1814 PyMem_Free(path);
1815 Py_INCREF(Py_None);
1816 return Py_None;
1817}
1818#endif /* HAVE_LCHOWN */
1819
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001820
Guido van Rossum36bc6801995-06-14 22:54:23 +00001821#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001822PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001823"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001824Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001825
Barry Warsaw53699e91996-12-10 23:23:01 +00001826static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001827posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001828{
1829 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001830 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001831
Barry Warsaw53699e91996-12-10 23:23:01 +00001832 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001833#if defined(PYOS_OS2) && defined(PYCC_GCC)
1834 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001835#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001836 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001837#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001838 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001839 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001840 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001841 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001842}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001843
Walter Dörwald3b918c32002-11-21 20:18:46 +00001844#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001845PyDoc_STRVAR(posix_getcwdu__doc__,
1846"getcwdu() -> path\n\n\
1847Return a unicode string representing the current working directory.");
1848
1849static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001850posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001851{
1852 char buf[1026];
1853 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001854
1855#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001856 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001857 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001858 wchar_t wbuf[1026];
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001859 wchar_t *wbuf2 = wbuf;
1860 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001861 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001862 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1863 /* If the buffer is large enough, len does not include the
1864 terminating \0. If the buffer is too small, len includes
1865 the space needed for the terminator. */
1866 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1867 wbuf2 = malloc(len * sizeof(wchar_t));
1868 if (wbuf2)
1869 len = GetCurrentDirectoryW(len, wbuf2);
1870 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001871 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001872 if (!wbuf2) {
1873 PyErr_NoMemory();
1874 return NULL;
1875 }
1876 if (!len) {
1877 if (wbuf2 != wbuf) free(wbuf2);
1878 return win32_error("getcwdu", NULL);
1879 }
1880 resobj = PyUnicode_FromWideChar(wbuf2, len);
1881 if (wbuf2 != wbuf) free(wbuf2);
1882 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001883 }
1884#endif
1885
1886 Py_BEGIN_ALLOW_THREADS
1887#if defined(PYOS_OS2) && defined(PYCC_GCC)
1888 res = _getcwd2(buf, sizeof buf);
1889#else
1890 res = getcwd(buf, sizeof buf);
1891#endif
1892 Py_END_ALLOW_THREADS
1893 if (res == NULL)
1894 return posix_error();
1895 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1896}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001897#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001898#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001900
Guido van Rossumb6775db1994-08-01 11:34:53 +00001901#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001903"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001904Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001905
Barry Warsaw53699e91996-12-10 23:23:01 +00001906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001907posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001908{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00001909 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001910}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001911#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001912
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001915"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001916Return a list containing the names of the entries in the directory.\n\
1917\n\
1918 path: path of directory to list\n\
1919\n\
1920The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001921entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001922
Barry Warsaw53699e91996-12-10 23:23:01 +00001923static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001924posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001925{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001926 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001927 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001928#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001929
Barry Warsaw53699e91996-12-10 23:23:01 +00001930 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001931 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001932 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001933 WIN32_FIND_DATA FileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001934 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001935 char *bufptr = namebuf;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001936 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001937
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001938#ifdef Py_WIN_WIDE_FILENAMES
1939 /* If on wide-character-capable OS see if argument
1940 is Unicode and if so use wide API. */
1941 if (unicode_file_names()) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001942 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001943 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1944 WIN32_FIND_DATAW wFileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001945 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001946 Py_UNICODE wch;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001947 /* Overallocate for \\*.*\0 */
1948 len = PyUnicode_GET_SIZE(po);
1949 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1950 if (!wnamebuf) {
1951 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001952 return NULL;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001953 }
1954 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1955 wch = len > 0 ? wnamebuf[len-1] : '\0';
1956 if (wch != L'/' && wch != L'\\' && wch != L':')
1957 wnamebuf[len++] = L'\\';
1958 wcscpy(wnamebuf + len, L"*.*");
1959 if ((d = PyList_New(0)) == NULL) {
1960 free(wnamebuf);
1961 return NULL;
1962 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001963 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1964 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001965 int error = GetLastError();
1966 if (error == ERROR_FILE_NOT_FOUND) {
1967 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001968 return d;
1969 }
1970 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00001971 win32_error_unicode("FindFirstFileW", wnamebuf);
1972 free(wnamebuf);
1973 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001974 }
1975 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001976 /* Skip over . and .. */
1977 if (wcscmp(wFileData.cFileName, L".") != 0 &&
1978 wcscmp(wFileData.cFileName, L"..") != 0) {
1979 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1980 if (v == NULL) {
1981 Py_DECREF(d);
1982 d = NULL;
1983 break;
1984 }
1985 if (PyList_Append(d, v) != 0) {
1986 Py_DECREF(v);
1987 Py_DECREF(d);
1988 d = NULL;
1989 break;
1990 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001991 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001992 }
Georg Brandl622927b2006-03-07 12:48:03 +00001993 Py_BEGIN_ALLOW_THREADS
1994 result = FindNextFileW(hFindFile, &wFileData);
1995 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00001996 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
1997 it got to the end of the directory. */
1998 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
1999 Py_DECREF(d);
2000 win32_error_unicode("FindNextFileW", wnamebuf);
2001 FindClose(hFindFile);
2002 free(wnamebuf);
2003 return NULL;
2004 }
Georg Brandl622927b2006-03-07 12:48:03 +00002005 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006
2007 if (FindClose(hFindFile) == FALSE) {
2008 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002009 win32_error_unicode("FindClose", wnamebuf);
2010 free(wnamebuf);
2011 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002012 }
Martin v. Löwise3edaea2006-05-15 05:51:36 +00002013 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002014 return d;
2015 }
2016 /* Drop the argument parsing error as narrow strings
2017 are also valid. */
2018 PyErr_Clear();
2019 }
2020#endif
2021
Tim Peters5aa91602002-01-30 05:46:57 +00002022 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002023 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002024 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002025 if (len > 0) {
2026 char ch = namebuf[len-1];
2027 if (ch != SEP && ch != ALTSEP && ch != ':')
2028 namebuf[len++] = '/';
2029 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002030 strcpy(namebuf + len, "*.*");
2031
Barry Warsaw53699e91996-12-10 23:23:01 +00002032 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002033 return NULL;
2034
2035 hFindFile = FindFirstFile(namebuf, &FileData);
2036 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002037 int error = GetLastError();
2038 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002039 return d;
2040 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002041 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002042 }
2043 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002044 /* Skip over . and .. */
2045 if (strcmp(FileData.cFileName, ".") != 0 &&
2046 strcmp(FileData.cFileName, "..") != 0) {
2047 v = PyString_FromString(FileData.cFileName);
2048 if (v == NULL) {
2049 Py_DECREF(d);
2050 d = NULL;
2051 break;
2052 }
2053 if (PyList_Append(d, v) != 0) {
2054 Py_DECREF(v);
2055 Py_DECREF(d);
2056 d = NULL;
2057 break;
2058 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002059 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002060 }
Georg Brandl622927b2006-03-07 12:48:03 +00002061 Py_BEGIN_ALLOW_THREADS
2062 result = FindNextFile(hFindFile, &FileData);
2063 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002064 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2065 it got to the end of the directory. */
2066 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2067 Py_DECREF(d);
2068 win32_error("FindNextFile", namebuf);
2069 FindClose(hFindFile);
2070 return NULL;
2071 }
Georg Brandl622927b2006-03-07 12:48:03 +00002072 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002073
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002074 if (FindClose(hFindFile) == FALSE) {
2075 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002076 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002077 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002078
2079 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002080
Tim Peters0bb44a42000-09-15 07:44:49 +00002081#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002082
2083#ifndef MAX_PATH
2084#define MAX_PATH CCHMAXPATH
2085#endif
2086 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002087 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002088 PyObject *d, *v;
2089 char namebuf[MAX_PATH+5];
2090 HDIR hdir = 1;
2091 ULONG srchcnt = 1;
2092 FILEFINDBUF3 ep;
2093 APIRET rc;
2094
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002095 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002096 return NULL;
2097 if (len >= MAX_PATH) {
2098 PyErr_SetString(PyExc_ValueError, "path too long");
2099 return NULL;
2100 }
2101 strcpy(namebuf, name);
2102 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002103 if (*pt == ALTSEP)
2104 *pt = SEP;
2105 if (namebuf[len-1] != SEP)
2106 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002107 strcpy(namebuf + len, "*.*");
2108
2109 if ((d = PyList_New(0)) == NULL)
2110 return NULL;
2111
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002112 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2113 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002114 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002115 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2116 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2117 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002118
2119 if (rc != NO_ERROR) {
2120 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002121 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002122 }
2123
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002124 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002125 do {
2126 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002127 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002128 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002129
2130 strcpy(namebuf, ep.achName);
2131
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002132 /* Leave Case of Name Alone -- In Native Form */
2133 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002134
2135 v = PyString_FromString(namebuf);
2136 if (v == NULL) {
2137 Py_DECREF(d);
2138 d = NULL;
2139 break;
2140 }
2141 if (PyList_Append(d, v) != 0) {
2142 Py_DECREF(v);
2143 Py_DECREF(d);
2144 d = NULL;
2145 break;
2146 }
2147 Py_DECREF(v);
2148 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2149 }
2150
2151 return d;
2152#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002153
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002154 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002155 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002156 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002157 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002158 int arg_is_unicode = 1;
2159
Georg Brandl05e89b82006-04-11 07:04:06 +00002160 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002161 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2162 arg_is_unicode = 0;
2163 PyErr_Clear();
2164 }
2165 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002166 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002167 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002168 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002169 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002170 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002171 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002172 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002173 return NULL;
2174 }
Georg Brandl622927b2006-03-07 12:48:03 +00002175 for (;;) {
2176 Py_BEGIN_ALLOW_THREADS
2177 ep = readdir(dirp);
2178 Py_END_ALLOW_THREADS
2179 if (ep == NULL)
2180 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002181 if (ep->d_name[0] == '.' &&
2182 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002183 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002184 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002185 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002186 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002187 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002188 d = NULL;
2189 break;
2190 }
Just van Rossum46c97842003-02-25 21:42:15 +00002191#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002192 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002193 PyObject *w;
2194
2195 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002196 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002197 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002198 if (w != NULL) {
2199 Py_DECREF(v);
2200 v = w;
2201 }
2202 else {
2203 /* fall back to the original byte string, as
2204 discussed in patch #683592 */
2205 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002206 }
Just van Rossum46c97842003-02-25 21:42:15 +00002207 }
2208#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002209 if (PyList_Append(d, v) != 0) {
2210 Py_DECREF(v);
2211 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002212 d = NULL;
2213 break;
2214 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002215 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002216 }
Georg Brandlbbfe4fa2006-04-11 06:47:43 +00002217 if (errno != 0 && d != NULL) {
2218 /* readdir() returned NULL and set errno */
2219 closedir(dirp);
2220 Py_DECREF(d);
2221 return posix_error_with_allocated_filename(name);
2222 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002223 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002224 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002225
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002226 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002227
Tim Peters0bb44a42000-09-15 07:44:49 +00002228#endif /* which OS */
2229} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002230
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002231#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002232/* A helper function for abspath on win32 */
2233static PyObject *
2234posix__getfullpathname(PyObject *self, PyObject *args)
2235{
2236 /* assume encoded strings wont more than double no of chars */
2237 char inbuf[MAX_PATH*2];
2238 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002239 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002240 char outbuf[MAX_PATH*2];
2241 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002242#ifdef Py_WIN_WIDE_FILENAMES
2243 if (unicode_file_names()) {
2244 PyUnicodeObject *po;
2245 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2246 Py_UNICODE woutbuf[MAX_PATH*2];
2247 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002248 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002249 sizeof(woutbuf)/sizeof(woutbuf[0]),
2250 woutbuf, &wtemp))
2251 return win32_error("GetFullPathName", "");
2252 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2253 }
2254 /* Drop the argument parsing error as narrow strings
2255 are also valid. */
2256 PyErr_Clear();
2257 }
2258#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002259 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2260 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002261 &insize))
2262 return NULL;
2263 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2264 outbuf, &temp))
2265 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002266 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2267 return PyUnicode_Decode(outbuf, strlen(outbuf),
2268 Py_FileSystemDefaultEncoding, NULL);
2269 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002270 return PyString_FromString(outbuf);
2271} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002272#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002274PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002275"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002276Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002277
Barry Warsaw53699e91996-12-10 23:23:01 +00002278static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002279posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002280{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002281 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002282 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002283 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002284
2285#ifdef Py_WIN_WIDE_FILENAMES
2286 if (unicode_file_names()) {
2287 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002288 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002289 Py_BEGIN_ALLOW_THREADS
2290 /* PyUnicode_AS_UNICODE OK without thread lock as
2291 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002292 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002293 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002294 if (!res)
2295 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002296 Py_INCREF(Py_None);
2297 return Py_None;
2298 }
2299 /* Drop the argument parsing error as narrow strings
2300 are also valid. */
2301 PyErr_Clear();
2302 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002303 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2304 Py_FileSystemDefaultEncoding, &path, &mode))
2305 return NULL;
2306 Py_BEGIN_ALLOW_THREADS
2307 /* PyUnicode_AS_UNICODE OK without thread lock as
2308 it is a simple dereference. */
2309 res = CreateDirectoryA(path, NULL);
2310 Py_END_ALLOW_THREADS
2311 if (!res) {
2312 win32_error("mkdir", path);
2313 PyMem_Free(path);
2314 return NULL;
2315 }
2316 PyMem_Free(path);
2317 Py_INCREF(Py_None);
2318 return Py_None;
2319#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002320
Tim Peters5aa91602002-01-30 05:46:57 +00002321 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002322 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002323 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002324 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002325#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002326 res = mkdir(path);
2327#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002328 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002329#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002330 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002331 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002332 return posix_error_with_allocated_filename(path);
2333 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002334 Py_INCREF(Py_None);
2335 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002336#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002337}
2338
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002339
Neal Norwitz1818ed72006-03-26 00:29:48 +00002340/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2341#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002342#include <sys/resource.h>
2343#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002344
Neal Norwitz1818ed72006-03-26 00:29:48 +00002345
2346#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002347PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002348"nice(inc) -> new_priority\n\n\
2349Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002350
Barry Warsaw53699e91996-12-10 23:23:01 +00002351static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002352posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002353{
2354 int increment, value;
2355
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002356 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002357 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002358
2359 /* There are two flavours of 'nice': one that returns the new
2360 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002361 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2362 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002363
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002364 If we are of the nice family that returns the new priority, we
2365 need to clear errno before the call, and check if errno is filled
2366 before calling posix_error() on a returnvalue of -1, because the
2367 -1 may be the actual new priority! */
2368
2369 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002370 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002371#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002372 if (value == 0)
2373 value = getpriority(PRIO_PROCESS, 0);
2374#endif
2375 if (value == -1 && errno != 0)
2376 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002377 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002378 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002379}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002380#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002382PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002383"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002384Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002385
Barry Warsaw53699e91996-12-10 23:23:01 +00002386static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002387posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002388{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002389#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002390 PyObject *o1, *o2;
2391 char *p1, *p2;
2392 BOOL result;
2393 if (unicode_file_names()) {
Hirokazu Yamamoto50c60722008-08-17 09:39:06 +00002394 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2395 goto error;
2396 if (!convert_to_unicode(&o1))
2397 goto error;
2398 if (!convert_to_unicode(&o2)) {
2399 Py_DECREF(o1);
2400 goto error;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002401 }
Hirokazu Yamamoto50c60722008-08-17 09:39:06 +00002402 Py_BEGIN_ALLOW_THREADS
2403 result = MoveFileW(PyUnicode_AsUnicode(o1),
2404 PyUnicode_AsUnicode(o2));
2405 Py_END_ALLOW_THREADS
2406 Py_DECREF(o1);
2407 Py_DECREF(o2);
2408 if (!result)
2409 return win32_error("rename", NULL);
2410 Py_INCREF(Py_None);
2411 return Py_None;
2412error:
2413 PyErr_Clear();
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002414 }
2415 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2416 return NULL;
2417 Py_BEGIN_ALLOW_THREADS
2418 result = MoveFileA(p1, p2);
2419 Py_END_ALLOW_THREADS
2420 if (!result)
2421 return win32_error("rename", NULL);
2422 Py_INCREF(Py_None);
2423 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002424#else
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002425 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002426#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002427}
2428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002429
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002430PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002431"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002432Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002433
Barry Warsaw53699e91996-12-10 23:23:01 +00002434static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002435posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002436{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002437#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002438 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002439#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002440 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002441#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002442}
2443
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002445PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002446"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002447Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002448
Barry Warsaw53699e91996-12-10 23:23:01 +00002449static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002450posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002451{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002452#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002453 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002454#else
2455 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2456#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002457}
2458
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002459
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002460#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002461PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002462"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002464
Barry Warsaw53699e91996-12-10 23:23:01 +00002465static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002466posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002467{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002468 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002469 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002470 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002471 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002472 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002473 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002474 Py_END_ALLOW_THREADS
2475 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002476}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002477#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002478
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002480PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002481"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002482Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002483
Barry Warsaw53699e91996-12-10 23:23:01 +00002484static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002485posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002486{
2487 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002488 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002489 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002490 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002491 if (i < 0)
2492 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002493 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002494}
2495
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002496
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002497PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002498"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002499Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002501PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002502"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002503Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002504
Barry Warsaw53699e91996-12-10 23:23:01 +00002505static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002506posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002507{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002508#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002509 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002510#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002511 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002512#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002513}
2514
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002515
Guido van Rossumb6775db1994-08-01 11:34:53 +00002516#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002517PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002518"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002519Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002520
Barry Warsaw53699e91996-12-10 23:23:01 +00002521static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002522posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002523{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002524 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002525 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002526
Barry Warsaw53699e91996-12-10 23:23:01 +00002527 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002528 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002529 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002530 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002531 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002532 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002533 u.sysname,
2534 u.nodename,
2535 u.release,
2536 u.version,
2537 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002538}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002539#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002540
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002541static int
2542extract_time(PyObject *t, long* sec, long* usec)
2543{
2544 long intval;
2545 if (PyFloat_Check(t)) {
2546 double tval = PyFloat_AsDouble(t);
2547 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
2548 if (!intobj)
2549 return -1;
2550 intval = PyInt_AsLong(intobj);
2551 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002552 if (intval == -1 && PyErr_Occurred())
2553 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002554 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002555 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002556 if (*usec < 0)
2557 /* If rounding gave us a negative number,
2558 truncate. */
2559 *usec = 0;
2560 return 0;
2561 }
2562 intval = PyInt_AsLong(t);
2563 if (intval == -1 && PyErr_Occurred())
2564 return -1;
2565 *sec = intval;
2566 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002567 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002568}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002570PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002571"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002572utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002573Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002574second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002575
Barry Warsaw53699e91996-12-10 23:23:01 +00002576static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002577posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002578{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002579#ifdef Py_WIN_WIDE_FILENAMES
2580 PyObject *arg;
2581 PyUnicodeObject *obwpath;
2582 wchar_t *wpath = NULL;
2583 char *apath = NULL;
2584 HANDLE hFile;
2585 long atimesec, mtimesec, ausec, musec;
2586 FILETIME atime, mtime;
2587 PyObject *result = NULL;
2588
2589 if (unicode_file_names()) {
2590 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2591 wpath = PyUnicode_AS_UNICODE(obwpath);
2592 Py_BEGIN_ALLOW_THREADS
2593 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2594 NULL, OPEN_EXISTING, 0, NULL);
2595 Py_END_ALLOW_THREADS
2596 if (hFile == INVALID_HANDLE_VALUE)
2597 return win32_error_unicode("utime", wpath);
2598 } else
2599 /* Drop the argument parsing error as narrow strings
2600 are also valid. */
2601 PyErr_Clear();
2602 }
2603 if (!wpath) {
2604 if (!PyArg_ParseTuple(args, "etO:utime",
2605 Py_FileSystemDefaultEncoding, &apath, &arg))
2606 return NULL;
2607 Py_BEGIN_ALLOW_THREADS
2608 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2609 NULL, OPEN_EXISTING, 0, NULL);
2610 Py_END_ALLOW_THREADS
2611 if (hFile == INVALID_HANDLE_VALUE) {
2612 win32_error("utime", apath);
2613 PyMem_Free(apath);
2614 return NULL;
2615 }
2616 PyMem_Free(apath);
2617 }
2618
2619 if (arg == Py_None) {
2620 SYSTEMTIME now;
2621 GetSystemTime(&now);
2622 if (!SystemTimeToFileTime(&now, &mtime) ||
2623 !SystemTimeToFileTime(&now, &atime)) {
2624 win32_error("utime", NULL);
2625 goto done;
2626 }
2627 }
2628 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2629 PyErr_SetString(PyExc_TypeError,
2630 "utime() arg 2 must be a tuple (atime, mtime)");
2631 goto done;
2632 }
2633 else {
2634 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2635 &atimesec, &ausec) == -1)
2636 goto done;
Martin v. Löwis463a42b2006-10-09 20:44:50 +00002637 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002638 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2639 &mtimesec, &musec) == -1)
2640 goto done;
Martin v. Löwis463a42b2006-10-09 20:44:50 +00002641 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002642 }
2643 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2644 /* Avoid putting the file name into the error here,
2645 as that may confuse the user into believing that
2646 something is wrong with the file, when it also
2647 could be the time stamp that gives a problem. */
2648 win32_error("utime", NULL);
2649 }
2650 Py_INCREF(Py_None);
2651 result = Py_None;
2652done:
2653 CloseHandle(hFile);
2654 return result;
2655#else /* Py_WIN_WIDE_FILENAMES */
2656
Neal Norwitz2adf2102004-06-09 01:46:02 +00002657 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002658 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002659 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002660 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002661
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002662#if defined(HAVE_UTIMES)
2663 struct timeval buf[2];
2664#define ATIME buf[0].tv_sec
2665#define MTIME buf[1].tv_sec
2666#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002667/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002668 struct utimbuf buf;
2669#define ATIME buf.actime
2670#define MTIME buf.modtime
2671#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002672#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002673 time_t buf[2];
2674#define ATIME buf[0]
2675#define MTIME buf[1]
2676#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002677#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002678
Mark Hammond817c9292003-12-03 01:22:38 +00002679
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002680 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002681 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002682 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002683 if (arg == Py_None) {
2684 /* optional time values not given */
2685 Py_BEGIN_ALLOW_THREADS
2686 res = utime(path, NULL);
2687 Py_END_ALLOW_THREADS
2688 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002689 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002690 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002691 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002692 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002693 return NULL;
2694 }
2695 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002696 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002697 &atime, &ausec) == -1) {
2698 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002699 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002700 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002701 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002702 &mtime, &musec) == -1) {
2703 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002704 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002705 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002706 ATIME = atime;
2707 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002708#ifdef HAVE_UTIMES
2709 buf[0].tv_usec = ausec;
2710 buf[1].tv_usec = musec;
2711 Py_BEGIN_ALLOW_THREADS
2712 res = utimes(path, buf);
2713 Py_END_ALLOW_THREADS
2714#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002715 Py_BEGIN_ALLOW_THREADS
2716 res = utime(path, UTIME_ARG);
2717 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002718#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002719 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002720 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002721 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002722 }
Neal Norwitz96652712004-06-06 20:40:27 +00002723 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002724 Py_INCREF(Py_None);
2725 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002726#undef UTIME_ARG
2727#undef ATIME
2728#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002729#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002730}
2731
Guido van Rossum85e3b011991-06-03 12:42:10 +00002732
Guido van Rossum3b066191991-06-04 19:40:25 +00002733/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002735PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002736"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002737Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002738
Barry Warsaw53699e91996-12-10 23:23:01 +00002739static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002740posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002741{
2742 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002743 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002744 return NULL;
2745 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002746 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002747}
2748
Martin v. Löwis114619e2002-10-07 06:44:21 +00002749#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2750static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002751free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002752{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002753 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002754 for (i = 0; i < count; i++)
2755 PyMem_Free(array[i]);
2756 PyMem_DEL(array);
2757}
2758#endif
2759
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002760
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002761#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002762PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002763"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002764Execute an executable path with arguments, replacing current process.\n\
2765\n\
2766 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002767 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002768
Barry Warsaw53699e91996-12-10 23:23:01 +00002769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002770posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002771{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002772 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002773 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002774 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002775 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002776 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002777
Guido van Rossum89b33251993-10-22 14:26:06 +00002778 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002779 argv is a list or tuple of strings. */
2780
Martin v. Löwis114619e2002-10-07 06:44:21 +00002781 if (!PyArg_ParseTuple(args, "etO:execv",
2782 Py_FileSystemDefaultEncoding,
2783 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002784 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002785 if (PyList_Check(argv)) {
2786 argc = PyList_Size(argv);
2787 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002788 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002789 else if (PyTuple_Check(argv)) {
2790 argc = PyTuple_Size(argv);
2791 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002792 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002793 else {
Fred Drake661ea262000-10-24 19:57:45 +00002794 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002795 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002796 return NULL;
2797 }
2798
Barry Warsaw53699e91996-12-10 23:23:01 +00002799 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002800 if (argvlist == NULL) {
2801 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002802 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002803 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002804 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002805 if (!PyArg_Parse((*getitem)(argv, i), "et",
2806 Py_FileSystemDefaultEncoding,
2807 &argvlist[i])) {
2808 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002809 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002810 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002811 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002812 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002813
Guido van Rossum85e3b011991-06-03 12:42:10 +00002814 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002815 }
2816 argvlist[argc] = NULL;
2817
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002818 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002819
Guido van Rossum85e3b011991-06-03 12:42:10 +00002820 /* If we get here it's definitely an error */
2821
Martin v. Löwis114619e2002-10-07 06:44:21 +00002822 free_string_array(argvlist, argc);
2823 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002824 return posix_error();
2825}
2826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002828PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002829"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002830Execute a path with arguments and environment, replacing current process.\n\
2831\n\
2832 path: path of executable file\n\
2833 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002834 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002835
Barry Warsaw53699e91996-12-10 23:23:01 +00002836static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002837posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002838{
2839 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002840 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002841 char **argvlist;
2842 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002843 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002844 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002845 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002846 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002847
2848 /* execve has three arguments: (path, argv, env), where
2849 argv is a list or tuple of strings and env is a dictionary
2850 like posix.environ. */
2851
Martin v. Löwis114619e2002-10-07 06:44:21 +00002852 if (!PyArg_ParseTuple(args, "etOO:execve",
2853 Py_FileSystemDefaultEncoding,
2854 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002855 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002856 if (PyList_Check(argv)) {
2857 argc = PyList_Size(argv);
2858 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002859 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002860 else if (PyTuple_Check(argv)) {
2861 argc = PyTuple_Size(argv);
2862 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002863 }
2864 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002865 PyErr_SetString(PyExc_TypeError,
2866 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002867 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002868 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002869 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002870 PyErr_SetString(PyExc_TypeError,
2871 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002872 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002873 }
2874
Barry Warsaw53699e91996-12-10 23:23:01 +00002875 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002876 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002877 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002878 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002879 }
2880 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002881 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002882 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002883 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002884 &argvlist[i]))
2885 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002886 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002887 goto fail_1;
2888 }
2889 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002890 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002891 argvlist[argc] = NULL;
2892
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002893 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002894 if (i < 0)
2895 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002896 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002897 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002898 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002899 goto fail_1;
2900 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002901 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002902 keys = PyMapping_Keys(env);
2903 vals = PyMapping_Values(env);
2904 if (!keys || !vals)
2905 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002906 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2907 PyErr_SetString(PyExc_TypeError,
2908 "execve(): env.keys() or env.values() is not a list");
2909 goto fail_2;
2910 }
Tim Peters5aa91602002-01-30 05:46:57 +00002911
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002912 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002913 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002914 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002915
2916 key = PyList_GetItem(keys, pos);
2917 val = PyList_GetItem(vals, pos);
2918 if (!key || !val)
2919 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002920
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002921 if (!PyArg_Parse(
2922 key,
2923 "s;execve() arg 3 contains a non-string key",
2924 &k) ||
2925 !PyArg_Parse(
2926 val,
2927 "s;execve() arg 3 contains a non-string value",
2928 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002929 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002930 goto fail_2;
2931 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002932
2933#if defined(PYOS_OS2)
2934 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2935 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2936#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002937 len = PyString_Size(key) + PyString_Size(val) + 2;
2938 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002939 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002940 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002941 goto fail_2;
2942 }
Tim Petersc8996f52001-12-03 20:41:00 +00002943 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002944 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002945#if defined(PYOS_OS2)
2946 }
2947#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002948 }
2949 envlist[envc] = 0;
2950
2951 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002952
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002953 /* If we get here it's definitely an error */
2954
2955 (void) posix_error();
2956
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002957 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002958 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002959 PyMem_DEL(envlist[envc]);
2960 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002961 fail_1:
2962 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002963 Py_XDECREF(vals);
2964 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002965 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002966 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002967 return NULL;
2968}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002969#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002970
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002971
Guido van Rossuma1065681999-01-25 23:20:23 +00002972#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002973PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002974"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002975Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002976\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002977 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002978 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002979 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002980
2981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002982posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002983{
2984 char *path;
2985 PyObject *argv;
2986 char **argvlist;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00002987 int mode, i;
2988 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002989 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002990 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00002991
2992 /* spawnv has three arguments: (mode, path, argv), where
2993 argv is a list or tuple of strings. */
2994
Martin v. Löwis114619e2002-10-07 06:44:21 +00002995 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2996 Py_FileSystemDefaultEncoding,
2997 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002998 return NULL;
2999 if (PyList_Check(argv)) {
3000 argc = PyList_Size(argv);
3001 getitem = PyList_GetItem;
3002 }
3003 else if (PyTuple_Check(argv)) {
3004 argc = PyTuple_Size(argv);
3005 getitem = PyTuple_GetItem;
3006 }
3007 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003008 PyErr_SetString(PyExc_TypeError,
3009 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003010 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003011 return NULL;
3012 }
3013
3014 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003015 if (argvlist == NULL) {
3016 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003017 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003018 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003019 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003020 if (!PyArg_Parse((*getitem)(argv, i), "et",
3021 Py_FileSystemDefaultEncoding,
3022 &argvlist[i])) {
3023 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003024 PyErr_SetString(
3025 PyExc_TypeError,
3026 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003027 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003028 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003029 }
3030 }
3031 argvlist[argc] = NULL;
3032
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003033#if defined(PYOS_OS2) && defined(PYCC_GCC)
3034 Py_BEGIN_ALLOW_THREADS
3035 spawnval = spawnv(mode, path, argvlist);
3036 Py_END_ALLOW_THREADS
3037#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003038 if (mode == _OLD_P_OVERLAY)
3039 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003040
Tim Peters25059d32001-12-07 20:35:43 +00003041 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003042 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003043 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003044#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003045
Martin v. Löwis114619e2002-10-07 06:44:21 +00003046 free_string_array(argvlist, argc);
3047 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003048
Fred Drake699f3522000-06-29 21:12:41 +00003049 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003050 return posix_error();
3051 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003052#if SIZEOF_LONG == SIZEOF_VOID_P
3053 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003054#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003055 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003056#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003057}
3058
3059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003060PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003061"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003062Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003063\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003064 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003065 path: path of executable file\n\
3066 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003067 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003068
3069static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003070posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003071{
3072 char *path;
3073 PyObject *argv, *env;
3074 char **argvlist;
3075 char **envlist;
3076 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003077 int mode, pos, envc;
3078 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003079 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003080 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003081 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003082
3083 /* spawnve has four arguments: (mode, path, argv, env), where
3084 argv is a list or tuple of strings and env is a dictionary
3085 like posix.environ. */
3086
Martin v. Löwis114619e2002-10-07 06:44:21 +00003087 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3088 Py_FileSystemDefaultEncoding,
3089 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003090 return NULL;
3091 if (PyList_Check(argv)) {
3092 argc = PyList_Size(argv);
3093 getitem = PyList_GetItem;
3094 }
3095 else if (PyTuple_Check(argv)) {
3096 argc = PyTuple_Size(argv);
3097 getitem = PyTuple_GetItem;
3098 }
3099 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003100 PyErr_SetString(PyExc_TypeError,
3101 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003102 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003103 }
3104 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003105 PyErr_SetString(PyExc_TypeError,
3106 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003107 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003108 }
3109
3110 argvlist = PyMem_NEW(char *, argc+1);
3111 if (argvlist == NULL) {
3112 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003113 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003114 }
3115 for (i = 0; i < argc; i++) {
3116 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003117 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003118 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003119 &argvlist[i]))
3120 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003121 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003122 goto fail_1;
3123 }
3124 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003125 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003126 argvlist[argc] = NULL;
3127
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003128 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003129 if (i < 0)
3130 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003131 envlist = PyMem_NEW(char *, i + 1);
3132 if (envlist == NULL) {
3133 PyErr_NoMemory();
3134 goto fail_1;
3135 }
3136 envc = 0;
3137 keys = PyMapping_Keys(env);
3138 vals = PyMapping_Values(env);
3139 if (!keys || !vals)
3140 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003141 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3142 PyErr_SetString(PyExc_TypeError,
3143 "spawnve(): env.keys() or env.values() is not a list");
3144 goto fail_2;
3145 }
Tim Peters5aa91602002-01-30 05:46:57 +00003146
Guido van Rossuma1065681999-01-25 23:20:23 +00003147 for (pos = 0; pos < i; pos++) {
3148 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003149 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003150
3151 key = PyList_GetItem(keys, pos);
3152 val = PyList_GetItem(vals, pos);
3153 if (!key || !val)
3154 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003155
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003156 if (!PyArg_Parse(
3157 key,
3158 "s;spawnve() arg 3 contains a non-string key",
3159 &k) ||
3160 !PyArg_Parse(
3161 val,
3162 "s;spawnve() arg 3 contains a non-string value",
3163 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003164 {
3165 goto fail_2;
3166 }
Tim Petersc8996f52001-12-03 20:41:00 +00003167 len = PyString_Size(key) + PyString_Size(val) + 2;
3168 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003169 if (p == NULL) {
3170 PyErr_NoMemory();
3171 goto fail_2;
3172 }
Tim Petersc8996f52001-12-03 20:41:00 +00003173 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003174 envlist[envc++] = p;
3175 }
3176 envlist[envc] = 0;
3177
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003178#if defined(PYOS_OS2) && defined(PYCC_GCC)
3179 Py_BEGIN_ALLOW_THREADS
3180 spawnval = spawnve(mode, path, argvlist, envlist);
3181 Py_END_ALLOW_THREADS
3182#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003183 if (mode == _OLD_P_OVERLAY)
3184 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003185
3186 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003187 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003188 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003189#endif
Tim Peters25059d32001-12-07 20:35:43 +00003190
Fred Drake699f3522000-06-29 21:12:41 +00003191 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003192 (void) posix_error();
3193 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003194#if SIZEOF_LONG == SIZEOF_VOID_P
3195 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003196#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003197 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003198#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003199
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003200 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003201 while (--envc >= 0)
3202 PyMem_DEL(envlist[envc]);
3203 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003204 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003205 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003206 Py_XDECREF(vals);
3207 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003208 fail_0:
3209 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003210 return res;
3211}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003212
3213/* OS/2 supports spawnvp & spawnvpe natively */
3214#if defined(PYOS_OS2)
3215PyDoc_STRVAR(posix_spawnvp__doc__,
3216"spawnvp(mode, file, args)\n\n\
3217Execute the program 'file' in a new process, using the environment\n\
3218search path to find the file.\n\
3219\n\
3220 mode: mode of process creation\n\
3221 file: executable file name\n\
3222 args: tuple or list of strings");
3223
3224static PyObject *
3225posix_spawnvp(PyObject *self, PyObject *args)
3226{
3227 char *path;
3228 PyObject *argv;
3229 char **argvlist;
3230 int mode, i, argc;
3231 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003232 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003233
3234 /* spawnvp has three arguments: (mode, path, argv), where
3235 argv is a list or tuple of strings. */
3236
3237 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3238 Py_FileSystemDefaultEncoding,
3239 &path, &argv))
3240 return NULL;
3241 if (PyList_Check(argv)) {
3242 argc = PyList_Size(argv);
3243 getitem = PyList_GetItem;
3244 }
3245 else if (PyTuple_Check(argv)) {
3246 argc = PyTuple_Size(argv);
3247 getitem = PyTuple_GetItem;
3248 }
3249 else {
3250 PyErr_SetString(PyExc_TypeError,
3251 "spawnvp() arg 2 must be a tuple or list");
3252 PyMem_Free(path);
3253 return NULL;
3254 }
3255
3256 argvlist = PyMem_NEW(char *, argc+1);
3257 if (argvlist == NULL) {
3258 PyMem_Free(path);
3259 return PyErr_NoMemory();
3260 }
3261 for (i = 0; i < argc; i++) {
3262 if (!PyArg_Parse((*getitem)(argv, i), "et",
3263 Py_FileSystemDefaultEncoding,
3264 &argvlist[i])) {
3265 free_string_array(argvlist, i);
3266 PyErr_SetString(
3267 PyExc_TypeError,
3268 "spawnvp() arg 2 must contain only strings");
3269 PyMem_Free(path);
3270 return NULL;
3271 }
3272 }
3273 argvlist[argc] = NULL;
3274
3275 Py_BEGIN_ALLOW_THREADS
3276#if defined(PYCC_GCC)
3277 spawnval = spawnvp(mode, path, argvlist);
3278#else
3279 spawnval = _spawnvp(mode, path, argvlist);
3280#endif
3281 Py_END_ALLOW_THREADS
3282
3283 free_string_array(argvlist, argc);
3284 PyMem_Free(path);
3285
3286 if (spawnval == -1)
3287 return posix_error();
3288 else
3289 return Py_BuildValue("l", (long) spawnval);
3290}
3291
3292
3293PyDoc_STRVAR(posix_spawnvpe__doc__,
3294"spawnvpe(mode, file, args, env)\n\n\
3295Execute the program 'file' in a new process, using the environment\n\
3296search path to find the file.\n\
3297\n\
3298 mode: mode of process creation\n\
3299 file: executable file name\n\
3300 args: tuple or list of arguments\n\
3301 env: dictionary of strings mapping to strings");
3302
3303static PyObject *
3304posix_spawnvpe(PyObject *self, PyObject *args)
3305{
3306 char *path;
3307 PyObject *argv, *env;
3308 char **argvlist;
3309 char **envlist;
3310 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3311 int mode, i, pos, argc, envc;
3312 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003313 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003314 int lastarg = 0;
3315
3316 /* spawnvpe has four arguments: (mode, path, argv, env), where
3317 argv is a list or tuple of strings and env is a dictionary
3318 like posix.environ. */
3319
3320 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3321 Py_FileSystemDefaultEncoding,
3322 &path, &argv, &env))
3323 return NULL;
3324 if (PyList_Check(argv)) {
3325 argc = PyList_Size(argv);
3326 getitem = PyList_GetItem;
3327 }
3328 else if (PyTuple_Check(argv)) {
3329 argc = PyTuple_Size(argv);
3330 getitem = PyTuple_GetItem;
3331 }
3332 else {
3333 PyErr_SetString(PyExc_TypeError,
3334 "spawnvpe() arg 2 must be a tuple or list");
3335 goto fail_0;
3336 }
3337 if (!PyMapping_Check(env)) {
3338 PyErr_SetString(PyExc_TypeError,
3339 "spawnvpe() arg 3 must be a mapping object");
3340 goto fail_0;
3341 }
3342
3343 argvlist = PyMem_NEW(char *, argc+1);
3344 if (argvlist == NULL) {
3345 PyErr_NoMemory();
3346 goto fail_0;
3347 }
3348 for (i = 0; i < argc; i++) {
3349 if (!PyArg_Parse((*getitem)(argv, i),
3350 "et;spawnvpe() arg 2 must contain only strings",
3351 Py_FileSystemDefaultEncoding,
3352 &argvlist[i]))
3353 {
3354 lastarg = i;
3355 goto fail_1;
3356 }
3357 }
3358 lastarg = argc;
3359 argvlist[argc] = NULL;
3360
3361 i = PyMapping_Size(env);
3362 if (i < 0)
3363 goto fail_1;
3364 envlist = PyMem_NEW(char *, i + 1);
3365 if (envlist == NULL) {
3366 PyErr_NoMemory();
3367 goto fail_1;
3368 }
3369 envc = 0;
3370 keys = PyMapping_Keys(env);
3371 vals = PyMapping_Values(env);
3372 if (!keys || !vals)
3373 goto fail_2;
3374 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3375 PyErr_SetString(PyExc_TypeError,
3376 "spawnvpe(): env.keys() or env.values() is not a list");
3377 goto fail_2;
3378 }
3379
3380 for (pos = 0; pos < i; pos++) {
3381 char *p, *k, *v;
3382 size_t len;
3383
3384 key = PyList_GetItem(keys, pos);
3385 val = PyList_GetItem(vals, pos);
3386 if (!key || !val)
3387 goto fail_2;
3388
3389 if (!PyArg_Parse(
3390 key,
3391 "s;spawnvpe() arg 3 contains a non-string key",
3392 &k) ||
3393 !PyArg_Parse(
3394 val,
3395 "s;spawnvpe() arg 3 contains a non-string value",
3396 &v))
3397 {
3398 goto fail_2;
3399 }
3400 len = PyString_Size(key) + PyString_Size(val) + 2;
3401 p = PyMem_NEW(char, len);
3402 if (p == NULL) {
3403 PyErr_NoMemory();
3404 goto fail_2;
3405 }
3406 PyOS_snprintf(p, len, "%s=%s", k, v);
3407 envlist[envc++] = p;
3408 }
3409 envlist[envc] = 0;
3410
3411 Py_BEGIN_ALLOW_THREADS
3412#if defined(PYCC_GCC)
Andrew MacIntyre8af70672008-02-03 07:20:39 +00003413 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003414#else
Andrew MacIntyre8af70672008-02-03 07:20:39 +00003415 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003416#endif
3417 Py_END_ALLOW_THREADS
3418
3419 if (spawnval == -1)
3420 (void) posix_error();
3421 else
3422 res = Py_BuildValue("l", (long) spawnval);
3423
3424 fail_2:
3425 while (--envc >= 0)
3426 PyMem_DEL(envlist[envc]);
3427 PyMem_DEL(envlist);
3428 fail_1:
3429 free_string_array(argvlist, lastarg);
3430 Py_XDECREF(vals);
3431 Py_XDECREF(keys);
3432 fail_0:
3433 PyMem_Free(path);
3434 return res;
3435}
3436#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003437#endif /* HAVE_SPAWNV */
3438
3439
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003440#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003441PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003442"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003443Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3444\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003445Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003446
3447static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003448posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003449{
Neal Norwitze241ce82003-02-17 18:17:05 +00003450 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003451 if (pid == -1)
3452 return posix_error();
3453 PyOS_AfterFork();
3454 return PyInt_FromLong((long)pid);
3455}
3456#endif
3457
3458
Guido van Rossumad0ee831995-03-01 10:34:45 +00003459#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003460PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003461"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003462Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003463Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003464
Barry Warsaw53699e91996-12-10 23:23:01 +00003465static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003466posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003467{
Neal Norwitze241ce82003-02-17 18:17:05 +00003468 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003469 if (pid == -1)
3470 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003471 if (pid == 0)
3472 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003473 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003474}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003475#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003476
Neal Norwitzb59798b2003-03-21 01:43:31 +00003477/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003478/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3479#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003480#define DEV_PTY_FILE "/dev/ptc"
3481#define HAVE_DEV_PTMX
3482#else
3483#define DEV_PTY_FILE "/dev/ptmx"
3484#endif
3485
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003486#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003487#ifdef HAVE_PTY_H
3488#include <pty.h>
3489#else
3490#ifdef HAVE_LIBUTIL_H
3491#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003492#endif /* HAVE_LIBUTIL_H */
3493#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003494#ifdef HAVE_STROPTS_H
3495#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003496#endif
3497#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003498
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003499#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003500PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003501"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003502Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003503
3504static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003505posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003506{
3507 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003508#ifndef HAVE_OPENPTY
3509 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003510#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003511#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003512 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003513#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003514 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003515#endif
3516#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003517
Thomas Wouters70c21a12000-07-14 14:28:33 +00003518#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003519 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3520 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003521#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003522 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3523 if (slave_name == NULL)
3524 return posix_error();
3525
3526 slave_fd = open(slave_name, O_RDWR);
3527 if (slave_fd < 0)
3528 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003529#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003530 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003531 if (master_fd < 0)
3532 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003533 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003534 /* change permission of slave */
3535 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003536 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003537 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003538 }
3539 /* unlock slave */
3540 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003541 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003542 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003543 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003544 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003545 slave_name = ptsname(master_fd); /* get name of slave */
3546 if (slave_name == NULL)
3547 return posix_error();
3548 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3549 if (slave_fd < 0)
3550 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003551#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003552 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3553 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003554#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003555 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003556#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003557#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003558#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003559
Fred Drake8cef4cf2000-06-28 16:40:38 +00003560 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003561
Fred Drake8cef4cf2000-06-28 16:40:38 +00003562}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003563#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003564
3565#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003566PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003567"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003568Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3569Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003570To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003571
3572static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003573posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003574{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003575 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003576
Fred Drake8cef4cf2000-06-28 16:40:38 +00003577 pid = forkpty(&master_fd, NULL, NULL, NULL);
3578 if (pid == -1)
3579 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003580 if (pid == 0)
3581 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003582 return Py_BuildValue("(ii)", pid, master_fd);
3583}
3584#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003585
Guido van Rossumad0ee831995-03-01 10:34:45 +00003586#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003587PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003588"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003589Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003590
Barry Warsaw53699e91996-12-10 23:23:01 +00003591static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003592posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003593{
Barry Warsaw53699e91996-12-10 23:23:01 +00003594 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003595}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003596#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003597
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003598
Guido van Rossumad0ee831995-03-01 10:34:45 +00003599#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003600PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003601"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003602Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003603
Barry Warsaw53699e91996-12-10 23:23:01 +00003604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003605posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003606{
Barry Warsaw53699e91996-12-10 23:23:01 +00003607 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003608}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003609#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003611
Guido van Rossumad0ee831995-03-01 10:34:45 +00003612#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003613PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003614"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003615Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003616
Barry Warsaw53699e91996-12-10 23:23:01 +00003617static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003618posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003619{
Barry Warsaw53699e91996-12-10 23:23:01 +00003620 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003621}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003622#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003625PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003626"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003627Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003628
Barry Warsaw53699e91996-12-10 23:23:01 +00003629static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003630posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003631{
Barry Warsaw53699e91996-12-10 23:23:01 +00003632 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003633}
3634
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003635
Fred Drakec9680921999-12-13 16:37:25 +00003636#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003637PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003638"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003639Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003640
3641static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003642posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003643{
3644 PyObject *result = NULL;
3645
Fred Drakec9680921999-12-13 16:37:25 +00003646#ifdef NGROUPS_MAX
3647#define MAX_GROUPS NGROUPS_MAX
3648#else
3649 /* defined to be 16 on Solaris7, so this should be a small number */
3650#define MAX_GROUPS 64
3651#endif
3652 gid_t grouplist[MAX_GROUPS];
3653 int n;
3654
3655 n = getgroups(MAX_GROUPS, grouplist);
3656 if (n < 0)
3657 posix_error();
3658 else {
3659 result = PyList_New(n);
3660 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003661 int i;
3662 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003663 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003664 if (o == NULL) {
3665 Py_DECREF(result);
3666 result = NULL;
3667 break;
3668 }
3669 PyList_SET_ITEM(result, i, o);
3670 }
3671 }
3672 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003673
Fred Drakec9680921999-12-13 16:37:25 +00003674 return result;
3675}
3676#endif
3677
Martin v. Löwis606edc12002-06-13 21:09:11 +00003678#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003679PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003680"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003681Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003682
3683static PyObject *
3684posix_getpgid(PyObject *self, PyObject *args)
3685{
3686 int pid, pgid;
3687 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3688 return NULL;
3689 pgid = getpgid(pid);
3690 if (pgid < 0)
3691 return posix_error();
3692 return PyInt_FromLong((long)pgid);
3693}
3694#endif /* HAVE_GETPGID */
3695
3696
Guido van Rossumb6775db1994-08-01 11:34:53 +00003697#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003698PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003699"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003700Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003701
Barry Warsaw53699e91996-12-10 23:23:01 +00003702static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003703posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003704{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003705#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003706 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003707#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003708 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003709#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003710}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003711#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003713
Guido van Rossumb6775db1994-08-01 11:34:53 +00003714#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003715PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003716"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003717Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003718
Barry Warsaw53699e91996-12-10 23:23:01 +00003719static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003720posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003721{
Guido van Rossum64933891994-10-20 21:56:42 +00003722#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003723 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003724#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003725 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003726#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003727 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003728 Py_INCREF(Py_None);
3729 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003730}
3731
Guido van Rossumb6775db1994-08-01 11:34:53 +00003732#endif /* HAVE_SETPGRP */
3733
Guido van Rossumad0ee831995-03-01 10:34:45 +00003734#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003735PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003736"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003737Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003738
Barry Warsaw53699e91996-12-10 23:23:01 +00003739static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003740posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003741{
Barry Warsaw53699e91996-12-10 23:23:01 +00003742 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003743}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003744#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003745
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003746
Fred Drake12c6e2d1999-12-14 21:25:03 +00003747#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003748PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003749"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003750Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003751
3752static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003753posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003754{
Neal Norwitze241ce82003-02-17 18:17:05 +00003755 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003756 char *name;
3757 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003758
Fred Drakea30680b2000-12-06 21:24:28 +00003759 errno = 0;
3760 name = getlogin();
3761 if (name == NULL) {
3762 if (errno)
3763 posix_error();
3764 else
3765 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003766 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003767 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003768 else
3769 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003770 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003771
Fred Drake12c6e2d1999-12-14 21:25:03 +00003772 return result;
3773}
3774#endif
3775
Guido van Rossumad0ee831995-03-01 10:34:45 +00003776#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003777PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003778"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003780
Barry Warsaw53699e91996-12-10 23:23:01 +00003781static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003782posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003783{
Barry Warsaw53699e91996-12-10 23:23:01 +00003784 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003785}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003786#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003788
Guido van Rossumad0ee831995-03-01 10:34:45 +00003789#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003790PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003791"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003792Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003793
Barry Warsaw53699e91996-12-10 23:23:01 +00003794static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003795posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003796{
3797 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003798 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003799 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003800#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003801 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3802 APIRET rc;
3803 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003804 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003805
3806 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3807 APIRET rc;
3808 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003809 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003810
3811 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003812 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003813#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003814 if (kill(pid, sig) == -1)
3815 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003816#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003817 Py_INCREF(Py_None);
3818 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003819}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003820#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003821
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003822#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003823PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003824"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003825Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003826
3827static PyObject *
3828posix_killpg(PyObject *self, PyObject *args)
3829{
3830 int pgid, sig;
3831 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3832 return NULL;
3833 if (killpg(pgid, sig) == -1)
3834 return posix_error();
3835 Py_INCREF(Py_None);
3836 return Py_None;
3837}
3838#endif
3839
Guido van Rossumc0125471996-06-28 18:55:32 +00003840#ifdef HAVE_PLOCK
3841
3842#ifdef HAVE_SYS_LOCK_H
3843#include <sys/lock.h>
3844#endif
3845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003846PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003847"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003848Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003849
Barry Warsaw53699e91996-12-10 23:23:01 +00003850static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003851posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003852{
3853 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003854 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003855 return NULL;
3856 if (plock(op) == -1)
3857 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003858 Py_INCREF(Py_None);
3859 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003860}
3861#endif
3862
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003863
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003864#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003865PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003866"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003867Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003868
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003869#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003870#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003871static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003872async_system(const char *command)
3873{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003874 char errormsg[256], args[1024];
3875 RESULTCODES rcodes;
3876 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003877
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003878 char *shell = getenv("COMSPEC");
3879 if (!shell)
3880 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003881
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003882 /* avoid overflowing the argument buffer */
3883 if (strlen(shell) + 3 + strlen(command) >= 1024)
3884 return ERROR_NOT_ENOUGH_MEMORY
3885
3886 args[0] = '\0';
3887 strcat(args, shell);
3888 strcat(args, "/c ");
3889 strcat(args, command);
3890
3891 /* execute asynchronously, inheriting the environment */
3892 rc = DosExecPgm(errormsg,
3893 sizeof(errormsg),
3894 EXEC_ASYNC,
3895 args,
3896 NULL,
3897 &rcodes,
3898 shell);
3899 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003900}
3901
Guido van Rossumd48f2521997-12-05 22:19:34 +00003902static FILE *
3903popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003904{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003905 int oldfd, tgtfd;
3906 HFILE pipeh[2];
3907 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003908
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003909 /* mode determines which of stdin or stdout is reconnected to
3910 * the pipe to the child
3911 */
3912 if (strchr(mode, 'r') != NULL) {
3913 tgt_fd = 1; /* stdout */
3914 } else if (strchr(mode, 'w')) {
3915 tgt_fd = 0; /* stdin */
3916 } else {
3917 *err = ERROR_INVALID_ACCESS;
3918 return NULL;
3919 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003920
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00003921 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003922 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
3923 *err = rc;
3924 return NULL;
3925 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003926
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003927 /* prevent other threads accessing stdio */
3928 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003929
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003930 /* reconnect stdio and execute child */
3931 oldfd = dup(tgtfd);
3932 close(tgtfd);
3933 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
3934 DosClose(pipeh[tgtfd]);
3935 rc = async_system(command);
3936 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003937
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003938 /* restore stdio */
3939 dup2(oldfd, tgtfd);
3940 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003941
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003942 /* allow other threads access to stdio */
3943 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003944
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00003945 /* if execution of child was successful return file stream */
3946 if (rc == NO_ERROR)
3947 return fdopen(pipeh[1 - tgtfd], mode);
3948 else {
3949 DosClose(pipeh[1 - tgtfd]);
3950 *err = rc;
3951 return NULL;
3952 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003953}
3954
3955static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003956posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003957{
3958 char *name;
3959 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003960 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003961 FILE *fp;
3962 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003963 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003964 return NULL;
3965 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003966 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003967 Py_END_ALLOW_THREADS
3968 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003969 return os2_error(err);
3970
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003971 f = PyFile_FromFile(fp, name, mode, fclose);
3972 if (f != NULL)
3973 PyFile_SetBufSize(f, bufsize);
3974 return f;
3975}
3976
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003977#elif defined(PYCC_GCC)
3978
3979/* standard posix version of popen() support */
3980static PyObject *
3981posix_popen(PyObject *self, PyObject *args)
3982{
3983 char *name;
3984 char *mode = "r";
3985 int bufsize = -1;
3986 FILE *fp;
3987 PyObject *f;
3988 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3989 return NULL;
3990 Py_BEGIN_ALLOW_THREADS
3991 fp = popen(name, mode);
3992 Py_END_ALLOW_THREADS
3993 if (fp == NULL)
3994 return posix_error();
3995 f = PyFile_FromFile(fp, name, mode, pclose);
3996 if (f != NULL)
3997 PyFile_SetBufSize(f, bufsize);
3998 return f;
3999}
4000
4001/* fork() under OS/2 has lots'o'warts
4002 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4003 * most of this code is a ripoff of the win32 code, but using the
4004 * capabilities of EMX's C library routines
4005 */
4006
4007/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4008#define POPEN_1 1
4009#define POPEN_2 2
4010#define POPEN_3 3
4011#define POPEN_4 4
4012
4013static PyObject *_PyPopen(char *, int, int, int);
4014static int _PyPclose(FILE *file);
4015
4016/*
4017 * Internal dictionary mapping popen* file pointers to process handles,
4018 * for use when retrieving the process exit code. See _PyPclose() below
4019 * for more information on this dictionary's use.
4020 */
4021static PyObject *_PyPopenProcs = NULL;
4022
4023/* os2emx version of popen2()
4024 *
4025 * The result of this function is a pipe (file) connected to the
4026 * process's stdin, and a pipe connected to the process's stdout.
4027 */
4028
4029static PyObject *
4030os2emx_popen2(PyObject *self, PyObject *args)
4031{
4032 PyObject *f;
4033 int tm=0;
4034
4035 char *cmdstring;
4036 char *mode = "t";
4037 int bufsize = -1;
4038 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4039 return NULL;
4040
4041 if (*mode == 't')
4042 tm = O_TEXT;
4043 else if (*mode != 'b') {
4044 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4045 return NULL;
4046 } else
4047 tm = O_BINARY;
4048
4049 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4050
4051 return f;
4052}
4053
4054/*
4055 * Variation on os2emx.popen2
4056 *
4057 * The result of this function is 3 pipes - the process's stdin,
4058 * stdout and stderr
4059 */
4060
4061static PyObject *
4062os2emx_popen3(PyObject *self, PyObject *args)
4063{
4064 PyObject *f;
4065 int tm = 0;
4066
4067 char *cmdstring;
4068 char *mode = "t";
4069 int bufsize = -1;
4070 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4071 return NULL;
4072
4073 if (*mode == 't')
4074 tm = O_TEXT;
4075 else if (*mode != 'b') {
4076 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4077 return NULL;
4078 } else
4079 tm = O_BINARY;
4080
4081 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4082
4083 return f;
4084}
4085
4086/*
4087 * Variation on os2emx.popen2
4088 *
Tim Peters11b23062003-04-23 02:39:17 +00004089 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004090 * and stdout+stderr combined as a single pipe.
4091 */
4092
4093static PyObject *
4094os2emx_popen4(PyObject *self, PyObject *args)
4095{
4096 PyObject *f;
4097 int tm = 0;
4098
4099 char *cmdstring;
4100 char *mode = "t";
4101 int bufsize = -1;
4102 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4103 return NULL;
4104
4105 if (*mode == 't')
4106 tm = O_TEXT;
4107 else if (*mode != 'b') {
4108 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4109 return NULL;
4110 } else
4111 tm = O_BINARY;
4112
4113 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4114
4115 return f;
4116}
4117
4118/* a couple of structures for convenient handling of multiple
4119 * file handles and pipes
4120 */
4121struct file_ref
4122{
4123 int handle;
4124 int flags;
4125};
4126
4127struct pipe_ref
4128{
4129 int rd;
4130 int wr;
4131};
4132
4133/* The following code is derived from the win32 code */
4134
4135static PyObject *
4136_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4137{
4138 struct file_ref stdio[3];
4139 struct pipe_ref p_fd[3];
4140 FILE *p_s[3];
4141 int file_count, i, pipe_err, pipe_pid;
4142 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4143 PyObject *f, *p_f[3];
4144
4145 /* file modes for subsequent fdopen's on pipe handles */
4146 if (mode == O_TEXT)
4147 {
4148 rd_mode = "rt";
4149 wr_mode = "wt";
4150 }
4151 else
4152 {
4153 rd_mode = "rb";
4154 wr_mode = "wb";
4155 }
4156
4157 /* prepare shell references */
4158 if ((shell = getenv("EMXSHELL")) == NULL)
4159 if ((shell = getenv("COMSPEC")) == NULL)
4160 {
4161 errno = ENOENT;
4162 return posix_error();
4163 }
4164
4165 sh_name = _getname(shell);
4166 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4167 opt = "/c";
4168 else
4169 opt = "-c";
4170
4171 /* save current stdio fds + their flags, and set not inheritable */
4172 i = pipe_err = 0;
4173 while (pipe_err >= 0 && i < 3)
4174 {
4175 pipe_err = stdio[i].handle = dup(i);
4176 stdio[i].flags = fcntl(i, F_GETFD, 0);
4177 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4178 i++;
4179 }
4180 if (pipe_err < 0)
4181 {
4182 /* didn't get them all saved - clean up and bail out */
4183 int saved_err = errno;
4184 while (i-- > 0)
4185 {
4186 close(stdio[i].handle);
4187 }
4188 errno = saved_err;
4189 return posix_error();
4190 }
4191
4192 /* create pipe ends */
4193 file_count = 2;
4194 if (n == POPEN_3)
4195 file_count = 3;
4196 i = pipe_err = 0;
4197 while ((pipe_err == 0) && (i < file_count))
4198 pipe_err = pipe((int *)&p_fd[i++]);
4199 if (pipe_err < 0)
4200 {
4201 /* didn't get them all made - clean up and bail out */
4202 while (i-- > 0)
4203 {
4204 close(p_fd[i].wr);
4205 close(p_fd[i].rd);
4206 }
4207 errno = EPIPE;
4208 return posix_error();
4209 }
4210
4211 /* change the actual standard IO streams over temporarily,
4212 * making the retained pipe ends non-inheritable
4213 */
4214 pipe_err = 0;
4215
4216 /* - stdin */
4217 if (dup2(p_fd[0].rd, 0) == 0)
4218 {
4219 close(p_fd[0].rd);
4220 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4221 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4222 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4223 {
4224 close(p_fd[0].wr);
4225 pipe_err = -1;
4226 }
4227 }
4228 else
4229 {
4230 pipe_err = -1;
4231 }
4232
4233 /* - stdout */
4234 if (pipe_err == 0)
4235 {
4236 if (dup2(p_fd[1].wr, 1) == 1)
4237 {
4238 close(p_fd[1].wr);
4239 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4240 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4241 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4242 {
4243 close(p_fd[1].rd);
4244 pipe_err = -1;
4245 }
4246 }
4247 else
4248 {
4249 pipe_err = -1;
4250 }
4251 }
4252
4253 /* - stderr, as required */
4254 if (pipe_err == 0)
4255 switch (n)
4256 {
4257 case POPEN_3:
4258 {
4259 if (dup2(p_fd[2].wr, 2) == 2)
4260 {
4261 close(p_fd[2].wr);
4262 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4263 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4264 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4265 {
4266 close(p_fd[2].rd);
4267 pipe_err = -1;
4268 }
4269 }
4270 else
4271 {
4272 pipe_err = -1;
4273 }
4274 break;
4275 }
4276
4277 case POPEN_4:
4278 {
4279 if (dup2(1, 2) != 2)
4280 {
4281 pipe_err = -1;
4282 }
4283 break;
4284 }
4285 }
4286
4287 /* spawn the child process */
4288 if (pipe_err == 0)
4289 {
4290 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4291 if (pipe_pid == -1)
4292 {
4293 pipe_err = -1;
4294 }
4295 else
4296 {
4297 /* save the PID into the FILE structure
4298 * NOTE: this implementation doesn't actually
4299 * take advantage of this, but do it for
4300 * completeness - AIM Apr01
4301 */
4302 for (i = 0; i < file_count; i++)
4303 p_s[i]->_pid = pipe_pid;
4304 }
4305 }
4306
4307 /* reset standard IO to normal */
4308 for (i = 0; i < 3; i++)
4309 {
4310 dup2(stdio[i].handle, i);
4311 fcntl(i, F_SETFD, stdio[i].flags);
4312 close(stdio[i].handle);
4313 }
4314
4315 /* if any remnant problems, clean up and bail out */
4316 if (pipe_err < 0)
4317 {
4318 for (i = 0; i < 3; i++)
4319 {
4320 close(p_fd[i].rd);
4321 close(p_fd[i].wr);
4322 }
4323 errno = EPIPE;
4324 return posix_error_with_filename(cmdstring);
4325 }
4326
4327 /* build tuple of file objects to return */
4328 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4329 PyFile_SetBufSize(p_f[0], bufsize);
4330 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4331 PyFile_SetBufSize(p_f[1], bufsize);
4332 if (n == POPEN_3)
4333 {
4334 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4335 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004336 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004337 }
4338 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004339 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004340
4341 /*
4342 * Insert the files we've created into the process dictionary
4343 * all referencing the list with the process handle and the
4344 * initial number of files (see description below in _PyPclose).
4345 * Since if _PyPclose later tried to wait on a process when all
4346 * handles weren't closed, it could create a deadlock with the
4347 * child, we spend some energy here to try to ensure that we
4348 * either insert all file handles into the dictionary or none
4349 * at all. It's a little clumsy with the various popen modes
4350 * and variable number of files involved.
4351 */
4352 if (!_PyPopenProcs)
4353 {
4354 _PyPopenProcs = PyDict_New();
4355 }
4356
4357 if (_PyPopenProcs)
4358 {
4359 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4360 int ins_rc[3];
4361
4362 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4363 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4364
4365 procObj = PyList_New(2);
4366 pidObj = PyInt_FromLong((long) pipe_pid);
4367 intObj = PyInt_FromLong((long) file_count);
4368
4369 if (procObj && pidObj && intObj)
4370 {
4371 PyList_SetItem(procObj, 0, pidObj);
4372 PyList_SetItem(procObj, 1, intObj);
4373
4374 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4375 if (fileObj[0])
4376 {
4377 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4378 fileObj[0],
4379 procObj);
4380 }
4381 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4382 if (fileObj[1])
4383 {
4384 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4385 fileObj[1],
4386 procObj);
4387 }
4388 if (file_count >= 3)
4389 {
4390 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4391 if (fileObj[2])
4392 {
4393 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4394 fileObj[2],
4395 procObj);
4396 }
4397 }
4398
4399 if (ins_rc[0] < 0 || !fileObj[0] ||
4400 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4401 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4402 {
4403 /* Something failed - remove any dictionary
4404 * entries that did make it.
4405 */
4406 if (!ins_rc[0] && fileObj[0])
4407 {
4408 PyDict_DelItem(_PyPopenProcs,
4409 fileObj[0]);
4410 }
4411 if (!ins_rc[1] && fileObj[1])
4412 {
4413 PyDict_DelItem(_PyPopenProcs,
4414 fileObj[1]);
4415 }
4416 if (!ins_rc[2] && fileObj[2])
4417 {
4418 PyDict_DelItem(_PyPopenProcs,
4419 fileObj[2]);
4420 }
4421 }
4422 }
Tim Peters11b23062003-04-23 02:39:17 +00004423
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004424 /*
4425 * Clean up our localized references for the dictionary keys
4426 * and value since PyDict_SetItem will Py_INCREF any copies
4427 * that got placed in the dictionary.
4428 */
4429 Py_XDECREF(procObj);
4430 Py_XDECREF(fileObj[0]);
4431 Py_XDECREF(fileObj[1]);
4432 Py_XDECREF(fileObj[2]);
4433 }
4434
4435 /* Child is launched. */
4436 return f;
4437}
4438
4439/*
4440 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4441 * exit code for the child process and return as a result of the close.
4442 *
4443 * This function uses the _PyPopenProcs dictionary in order to map the
4444 * input file pointer to information about the process that was
4445 * originally created by the popen* call that created the file pointer.
4446 * The dictionary uses the file pointer as a key (with one entry
4447 * inserted for each file returned by the original popen* call) and a
4448 * single list object as the value for all files from a single call.
4449 * The list object contains the Win32 process handle at [0], and a file
4450 * count at [1], which is initialized to the total number of file
4451 * handles using that list.
4452 *
4453 * This function closes whichever handle it is passed, and decrements
4454 * the file count in the dictionary for the process handle pointed to
4455 * by this file. On the last close (when the file count reaches zero),
4456 * this function will wait for the child process and then return its
4457 * exit code as the result of the close() operation. This permits the
4458 * files to be closed in any order - it is always the close() of the
4459 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004460 *
4461 * NOTE: This function is currently called with the GIL released.
4462 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004463 */
4464
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004465static int _PyPclose(FILE *file)
4466{
4467 int result;
4468 int exit_code;
4469 int pipe_pid;
4470 PyObject *procObj, *pidObj, *intObj, *fileObj;
4471 int file_count;
4472#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004473 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004474#endif
4475
4476 /* Close the file handle first, to ensure it can't block the
4477 * child from exiting if it's the last handle.
4478 */
4479 result = fclose(file);
4480
4481#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004482 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004483#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004484 if (_PyPopenProcs)
4485 {
4486 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4487 (procObj = PyDict_GetItem(_PyPopenProcs,
4488 fileObj)) != NULL &&
4489 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4490 (intObj = PyList_GetItem(procObj,1)) != NULL)
4491 {
4492 pipe_pid = (int) PyInt_AsLong(pidObj);
4493 file_count = (int) PyInt_AsLong(intObj);
4494
4495 if (file_count > 1)
4496 {
4497 /* Still other files referencing process */
4498 file_count--;
4499 PyList_SetItem(procObj,1,
4500 PyInt_FromLong((long) file_count));
4501 }
4502 else
4503 {
4504 /* Last file for this process */
4505 if (result != EOF &&
4506 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4507 {
4508 /* extract exit status */
4509 if (WIFEXITED(exit_code))
4510 {
4511 result = WEXITSTATUS(exit_code);
4512 }
4513 else
4514 {
4515 errno = EPIPE;
4516 result = -1;
4517 }
4518 }
4519 else
4520 {
4521 /* Indicate failure - this will cause the file object
4522 * to raise an I/O error and translate the last
4523 * error code from errno. We do have a problem with
4524 * last errors that overlap the normal errno table,
4525 * but that's a consistent problem with the file object.
4526 */
4527 result = -1;
4528 }
4529 }
4530
4531 /* Remove this file pointer from dictionary */
4532 PyDict_DelItem(_PyPopenProcs, fileObj);
4533
4534 if (PyDict_Size(_PyPopenProcs) == 0)
4535 {
4536 Py_DECREF(_PyPopenProcs);
4537 _PyPopenProcs = NULL;
4538 }
4539
4540 } /* if object retrieval ok */
4541
4542 Py_XDECREF(fileObj);
4543 } /* if _PyPopenProcs */
4544
4545#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004546 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004547#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004548 return result;
4549}
4550
4551#endif /* PYCC_??? */
4552
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004553#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004554
4555/*
4556 * Portable 'popen' replacement for Win32.
4557 *
4558 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4559 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004560 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004561 */
4562
4563#include <malloc.h>
4564#include <io.h>
4565#include <fcntl.h>
4566
4567/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4568#define POPEN_1 1
4569#define POPEN_2 2
4570#define POPEN_3 3
4571#define POPEN_4 4
4572
4573static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004574static int _PyPclose(FILE *file);
4575
4576/*
4577 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004578 * for use when retrieving the process exit code. See _PyPclose() below
4579 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004580 */
4581static PyObject *_PyPopenProcs = NULL;
4582
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004583
4584/* popen that works from a GUI.
4585 *
4586 * The result of this function is a pipe (file) connected to the
4587 * processes stdin or stdout, depending on the requested mode.
4588 */
4589
4590static PyObject *
4591posix_popen(PyObject *self, PyObject *args)
4592{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004593 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004594 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004595
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004596 char *cmdstring;
4597 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004598 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004599 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004600 return NULL;
4601
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004602 if (*mode == 'r')
4603 tm = _O_RDONLY;
4604 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004605 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004606 return NULL;
4607 } else
4608 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004609
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004610 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004611 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004612 return NULL;
4613 }
4614
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004615 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004616 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004617 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004618 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004619 else
4620 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4621
4622 return f;
4623}
4624
4625/* Variation on win32pipe.popen
4626 *
4627 * The result of this function is a pipe (file) connected to the
4628 * process's stdin, and a pipe connected to the process's stdout.
4629 */
4630
4631static PyObject *
4632win32_popen2(PyObject *self, PyObject *args)
4633{
4634 PyObject *f;
4635 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004636
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004637 char *cmdstring;
4638 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004639 int bufsize = -1;
4640 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004641 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004642
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004643 if (*mode == 't')
4644 tm = _O_TEXT;
4645 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004646 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004647 return NULL;
4648 } else
4649 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004650
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004651 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004652 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004653 return NULL;
4654 }
4655
4656 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004657
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004658 return f;
4659}
4660
4661/*
4662 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004663 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004664 * The result of this function is 3 pipes - the process's stdin,
4665 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004666 */
4667
4668static PyObject *
4669win32_popen3(PyObject *self, PyObject *args)
4670{
4671 PyObject *f;
4672 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004673
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004674 char *cmdstring;
4675 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004676 int bufsize = -1;
4677 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004678 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004679
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004680 if (*mode == 't')
4681 tm = _O_TEXT;
4682 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004683 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004684 return NULL;
4685 } else
4686 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004687
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004688 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004689 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004690 return NULL;
4691 }
4692
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004693 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004694
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004695 return f;
4696}
4697
4698/*
4699 * Variation on win32pipe.popen
4700 *
Tim Peters5aa91602002-01-30 05:46:57 +00004701 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004702 * and stdout+stderr combined as a single pipe.
4703 */
4704
4705static PyObject *
4706win32_popen4(PyObject *self, PyObject *args)
4707{
4708 PyObject *f;
4709 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004710
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004711 char *cmdstring;
4712 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004713 int bufsize = -1;
4714 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004715 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004716
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004717 if (*mode == 't')
4718 tm = _O_TEXT;
4719 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004720 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004721 return NULL;
4722 } else
4723 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004724
4725 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004726 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004727 return NULL;
4728 }
4729
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004730 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004731
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004732 return f;
4733}
4734
Mark Hammond08501372001-01-31 07:30:29 +00004735static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004736_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004737 HANDLE hStdin,
4738 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004739 HANDLE hStderr,
4740 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004741{
4742 PROCESS_INFORMATION piProcInfo;
4743 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004744 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004745 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004746 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004747 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004748 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004749
4750 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004751 char *comshell;
4752
Tim Peters92e4dd82002-10-05 01:47:34 +00004753 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004754 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004755 /* x < i, so x fits into an integer */
4756 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004757
4758 /* Explicitly check if we are using COMMAND.COM. If we are
4759 * then use the w9xpopen hack.
4760 */
4761 comshell = s1 + x;
4762 while (comshell >= s1 && *comshell != '\\')
4763 --comshell;
4764 ++comshell;
4765
4766 if (GetVersion() < 0x80000000 &&
4767 _stricmp(comshell, "command.com") != 0) {
4768 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004769 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004770 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004771 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004772 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004773 }
4774 else {
4775 /*
Tim Peters402d5982001-08-27 06:37:48 +00004776 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4777 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004778 */
Mark Hammond08501372001-01-31 07:30:29 +00004779 char modulepath[_MAX_PATH];
4780 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004781 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Martin v. Löwis26fd9602006-04-22 11:15:41 +00004782 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004783 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004784 x = i+1;
4785 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004786 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004787 strncat(modulepath,
4788 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004789 (sizeof(modulepath)/sizeof(modulepath[0]))
4790 -strlen(modulepath));
4791 if (stat(modulepath, &statinfo) != 0) {
Kristján Valur Jónsson5e4e31f2007-04-21 12:46:49 +00004792 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
Tim Peters5aa91602002-01-30 05:46:57 +00004793 /* Eeek - file-not-found - possibly an embedding
4794 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004795 */
Tim Peters5aa91602002-01-30 05:46:57 +00004796 strncpy(modulepath,
4797 Py_GetExecPrefix(),
Kristján Valur Jónsson5e4e31f2007-04-21 12:46:49 +00004798 mplen);
4799 modulepath[mplen-1] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004800 if (modulepath[strlen(modulepath)-1] != '\\')
4801 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004802 strncat(modulepath,
4803 szConsoleSpawn,
Kristján Valur Jónsson5e4e31f2007-04-21 12:46:49 +00004804 mplen-strlen(modulepath));
Mark Hammond08501372001-01-31 07:30:29 +00004805 /* No where else to look - raise an easily identifiable
4806 error, rather than leaving Windows to report
4807 "file not found" - as the user is probably blissfully
4808 unaware this shim EXE is used, and it will confuse them.
4809 (well, it confused me for a while ;-)
4810 */
4811 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004812 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004813 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004814 "for popen to work with your shell "
4815 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004816 szConsoleSpawn);
4817 return FALSE;
4818 }
4819 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004820 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004821 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004822 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004823
Tim Peters92e4dd82002-10-05 01:47:34 +00004824 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004825 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004826 /* To maintain correct argument passing semantics,
4827 we pass the command-line as it stands, and allow
4828 quoting to be applied. w9xpopen.exe will then
4829 use its argv vector, and re-quote the necessary
4830 args for the ultimate child process.
4831 */
Tim Peters75cdad52001-11-28 22:07:30 +00004832 PyOS_snprintf(
4833 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004834 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004835 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004836 s1,
4837 s3,
4838 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004839 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00004840 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004841 dialog:
4842 "Your program accessed mem currently in use at xxx"
4843 and a hopeful warning about the stability of your
4844 system.
4845 Cost is Ctrl+C wont kill children, but anyone
4846 who cares can have a go!
4847 */
4848 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004849 }
4850 }
4851
4852 /* Could be an else here to try cmd.exe / command.com in the path
4853 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00004854 else {
Tim Peters402d5982001-08-27 06:37:48 +00004855 PyErr_SetString(PyExc_RuntimeError,
4856 "Cannot locate a COMSPEC environment variable to "
4857 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00004858 return FALSE;
4859 }
Tim Peters5aa91602002-01-30 05:46:57 +00004860
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004861 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
4862 siStartInfo.cb = sizeof(STARTUPINFO);
4863 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
4864 siStartInfo.hStdInput = hStdin;
4865 siStartInfo.hStdOutput = hStdout;
4866 siStartInfo.hStdError = hStderr;
4867 siStartInfo.wShowWindow = SW_HIDE;
4868
4869 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004870 s2,
4871 NULL,
4872 NULL,
4873 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004874 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004875 NULL,
4876 NULL,
4877 &siStartInfo,
4878 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004879 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004880 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004881
Mark Hammondb37a3732000-08-14 04:47:33 +00004882 /* Return process handle */
4883 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004884 return TRUE;
4885 }
Tim Peters402d5982001-08-27 06:37:48 +00004886 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004887 return FALSE;
4888}
4889
4890/* The following code is based off of KB: Q190351 */
4891
4892static PyObject *
4893_PyPopen(char *cmdstring, int mode, int n)
4894{
4895 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
4896 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00004897 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00004898
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004899 SECURITY_ATTRIBUTES saAttr;
4900 BOOL fSuccess;
4901 int fd1, fd2, fd3;
4902 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00004903 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004904 PyObject *f;
4905
4906 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
4907 saAttr.bInheritHandle = TRUE;
4908 saAttr.lpSecurityDescriptor = NULL;
4909
4910 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4911 return win32_error("CreatePipe", NULL);
4912
4913 /* Create new output read handle and the input write handle. Set
4914 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004915 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004916 * being created. */
4917 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004918 GetCurrentProcess(), &hChildStdinWrDup, 0,
4919 FALSE,
4920 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004921 if (!fSuccess)
4922 return win32_error("DuplicateHandle", NULL);
4923
4924 /* Close the inheritable version of ChildStdin
4925 that we're using. */
4926 CloseHandle(hChildStdinWr);
4927
4928 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4929 return win32_error("CreatePipe", NULL);
4930
4931 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004932 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4933 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004934 if (!fSuccess)
4935 return win32_error("DuplicateHandle", NULL);
4936
4937 /* Close the inheritable version of ChildStdout
4938 that we're using. */
4939 CloseHandle(hChildStdoutRd);
4940
4941 if (n != POPEN_4) {
4942 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4943 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004944 fSuccess = DuplicateHandle(GetCurrentProcess(),
4945 hChildStderrRd,
4946 GetCurrentProcess(),
4947 &hChildStderrRdDup, 0,
4948 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004949 if (!fSuccess)
4950 return win32_error("DuplicateHandle", NULL);
4951 /* Close the inheritable version of ChildStdErr that we're using. */
4952 CloseHandle(hChildStderrRd);
4953 }
Tim Peters5aa91602002-01-30 05:46:57 +00004954
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004955 switch (n) {
4956 case POPEN_1:
4957 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4958 case _O_WRONLY | _O_TEXT:
4959 /* Case for writing to child Stdin in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004960 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004961 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004962 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004963 PyFile_SetBufSize(f, 0);
4964 /* We don't care about these pipes anymore, so close them. */
4965 CloseHandle(hChildStdoutRdDup);
4966 CloseHandle(hChildStderrRdDup);
4967 break;
4968
4969 case _O_RDONLY | _O_TEXT:
4970 /* Case for reading from child Stdout in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004971 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004972 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004973 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004974 PyFile_SetBufSize(f, 0);
4975 /* We don't care about these pipes anymore, so close them. */
4976 CloseHandle(hChildStdinWrDup);
4977 CloseHandle(hChildStderrRdDup);
4978 break;
4979
4980 case _O_RDONLY | _O_BINARY:
4981 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004982 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004983 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004984 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004985 PyFile_SetBufSize(f, 0);
4986 /* We don't care about these pipes anymore, so close them. */
4987 CloseHandle(hChildStdinWrDup);
4988 CloseHandle(hChildStderrRdDup);
4989 break;
4990
4991 case _O_WRONLY | _O_BINARY:
4992 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00004993 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004994 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004995 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004996 PyFile_SetBufSize(f, 0);
4997 /* We don't care about these pipes anymore, so close them. */
4998 CloseHandle(hChildStdoutRdDup);
4999 CloseHandle(hChildStderrRdDup);
5000 break;
5001 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005002 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005003 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005004
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005005 case POPEN_2:
5006 case POPEN_4:
5007 {
5008 char *m1, *m2;
5009 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005010
Tim Peters7dca21e2002-08-19 00:42:29 +00005011 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005012 m1 = "r";
5013 m2 = "w";
5014 } else {
5015 m1 = "rb";
5016 m2 = "wb";
5017 }
5018
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005019 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005020 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005021 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005022 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005023 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005024 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005025 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005026 PyFile_SetBufSize(p2, 0);
5027
5028 if (n != 4)
5029 CloseHandle(hChildStderrRdDup);
5030
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005031 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005032 Py_XDECREF(p1);
5033 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005034 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005035 break;
5036 }
Tim Peters5aa91602002-01-30 05:46:57 +00005037
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005038 case POPEN_3:
5039 {
5040 char *m1, *m2;
5041 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005042
Tim Peters7dca21e2002-08-19 00:42:29 +00005043 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005044 m1 = "r";
5045 m2 = "w";
5046 } else {
5047 m1 = "rb";
5048 m2 = "wb";
5049 }
5050
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005051 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005052 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005053 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005054 f2 = _fdopen(fd2, m1);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005055 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005056 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005057 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005058 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5059 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005060 PyFile_SetBufSize(p1, 0);
5061 PyFile_SetBufSize(p2, 0);
5062 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005063 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005064 Py_XDECREF(p1);
5065 Py_XDECREF(p2);
5066 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005067 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005068 break;
5069 }
5070 }
5071
5072 if (n == POPEN_4) {
5073 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005074 hChildStdinRd,
5075 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005076 hChildStdoutWr,
5077 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005078 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005079 }
5080 else {
5081 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005082 hChildStdinRd,
5083 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005084 hChildStderrWr,
5085 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005086 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005087 }
5088
Mark Hammondb37a3732000-08-14 04:47:33 +00005089 /*
5090 * Insert the files we've created into the process dictionary
5091 * all referencing the list with the process handle and the
5092 * initial number of files (see description below in _PyPclose).
5093 * Since if _PyPclose later tried to wait on a process when all
5094 * handles weren't closed, it could create a deadlock with the
5095 * child, we spend some energy here to try to ensure that we
5096 * either insert all file handles into the dictionary or none
5097 * at all. It's a little clumsy with the various popen modes
5098 * and variable number of files involved.
5099 */
5100 if (!_PyPopenProcs) {
5101 _PyPopenProcs = PyDict_New();
5102 }
5103
5104 if (_PyPopenProcs) {
5105 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5106 int ins_rc[3];
5107
5108 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5109 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5110
5111 procObj = PyList_New(2);
5112 hProcessObj = PyLong_FromVoidPtr(hProcess);
5113 intObj = PyInt_FromLong(file_count);
5114
5115 if (procObj && hProcessObj && intObj) {
5116 PyList_SetItem(procObj,0,hProcessObj);
5117 PyList_SetItem(procObj,1,intObj);
5118
5119 fileObj[0] = PyLong_FromVoidPtr(f1);
5120 if (fileObj[0]) {
5121 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5122 fileObj[0],
5123 procObj);
5124 }
5125 if (file_count >= 2) {
5126 fileObj[1] = PyLong_FromVoidPtr(f2);
5127 if (fileObj[1]) {
5128 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5129 fileObj[1],
5130 procObj);
5131 }
5132 }
5133 if (file_count >= 3) {
5134 fileObj[2] = PyLong_FromVoidPtr(f3);
5135 if (fileObj[2]) {
5136 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5137 fileObj[2],
5138 procObj);
5139 }
5140 }
5141
5142 if (ins_rc[0] < 0 || !fileObj[0] ||
5143 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5144 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5145 /* Something failed - remove any dictionary
5146 * entries that did make it.
5147 */
5148 if (!ins_rc[0] && fileObj[0]) {
5149 PyDict_DelItem(_PyPopenProcs,
5150 fileObj[0]);
5151 }
5152 if (!ins_rc[1] && fileObj[1]) {
5153 PyDict_DelItem(_PyPopenProcs,
5154 fileObj[1]);
5155 }
5156 if (!ins_rc[2] && fileObj[2]) {
5157 PyDict_DelItem(_PyPopenProcs,
5158 fileObj[2]);
5159 }
5160 }
5161 }
Tim Peters5aa91602002-01-30 05:46:57 +00005162
Mark Hammondb37a3732000-08-14 04:47:33 +00005163 /*
5164 * Clean up our localized references for the dictionary keys
5165 * and value since PyDict_SetItem will Py_INCREF any copies
5166 * that got placed in the dictionary.
5167 */
5168 Py_XDECREF(procObj);
5169 Py_XDECREF(fileObj[0]);
5170 Py_XDECREF(fileObj[1]);
5171 Py_XDECREF(fileObj[2]);
5172 }
5173
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005174 /* Child is launched. Close the parents copy of those pipe
5175 * handles that only the child should have open. You need to
5176 * make sure that no handles to the write end of the output pipe
5177 * are maintained in this process or else the pipe will not close
5178 * when the child process exits and the ReadFile will hang. */
5179
5180 if (!CloseHandle(hChildStdinRd))
5181 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005182
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005183 if (!CloseHandle(hChildStdoutWr))
5184 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005185
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005186 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5187 return win32_error("CloseHandle", NULL);
5188
5189 return f;
5190}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005191
5192/*
5193 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5194 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005195 *
5196 * This function uses the _PyPopenProcs dictionary in order to map the
5197 * input file pointer to information about the process that was
5198 * originally created by the popen* call that created the file pointer.
5199 * The dictionary uses the file pointer as a key (with one entry
5200 * inserted for each file returned by the original popen* call) and a
5201 * single list object as the value for all files from a single call.
5202 * The list object contains the Win32 process handle at [0], and a file
5203 * count at [1], which is initialized to the total number of file
5204 * handles using that list.
5205 *
5206 * This function closes whichever handle it is passed, and decrements
5207 * the file count in the dictionary for the process handle pointed to
5208 * by this file. On the last close (when the file count reaches zero),
5209 * this function will wait for the child process and then return its
5210 * exit code as the result of the close() operation. This permits the
5211 * files to be closed in any order - it is always the close() of the
5212 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005213 *
5214 * NOTE: This function is currently called with the GIL released.
5215 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005216 */
Tim Peters736aa322000-09-01 06:51:24 +00005217
Fredrik Lundh56055a42000-07-23 19:47:12 +00005218static int _PyPclose(FILE *file)
5219{
Fredrik Lundh20318932000-07-26 17:29:12 +00005220 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005221 DWORD exit_code;
5222 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005223 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5224 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005225#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005226 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005227#endif
5228
Fredrik Lundh20318932000-07-26 17:29:12 +00005229 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005230 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005231 */
5232 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005233#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005234 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005235#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005236 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005237 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5238 (procObj = PyDict_GetItem(_PyPopenProcs,
5239 fileObj)) != NULL &&
5240 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5241 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5242
5243 hProcess = PyLong_AsVoidPtr(hProcessObj);
5244 file_count = PyInt_AsLong(intObj);
5245
5246 if (file_count > 1) {
5247 /* Still other files referencing process */
5248 file_count--;
5249 PyList_SetItem(procObj,1,
5250 PyInt_FromLong(file_count));
5251 } else {
5252 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005253 if (result != EOF &&
5254 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5255 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005256 /* Possible truncation here in 16-bit environments, but
5257 * real exit codes are just the lower byte in any event.
5258 */
5259 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005260 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005261 /* Indicate failure - this will cause the file object
5262 * to raise an I/O error and translate the last Win32
5263 * error code from errno. We do have a problem with
5264 * last errors that overlap the normal errno table,
5265 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005266 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005267 if (result != EOF) {
5268 /* If the error wasn't from the fclose(), then
5269 * set errno for the file object error handling.
5270 */
5271 errno = GetLastError();
5272 }
5273 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005274 }
5275
5276 /* Free up the native handle at this point */
5277 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005278 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005279
Mark Hammondb37a3732000-08-14 04:47:33 +00005280 /* Remove this file pointer from dictionary */
5281 PyDict_DelItem(_PyPopenProcs, fileObj);
5282
5283 if (PyDict_Size(_PyPopenProcs) == 0) {
5284 Py_DECREF(_PyPopenProcs);
5285 _PyPopenProcs = NULL;
5286 }
5287
5288 } /* if object retrieval ok */
5289
5290 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005291 } /* if _PyPopenProcs */
5292
Tim Peters736aa322000-09-01 06:51:24 +00005293#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005294 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005295#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005296 return result;
5297}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005298
5299#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005300static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005301posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005302{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005303 char *name;
5304 char *mode = "r";
5305 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005306 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005307 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005308 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005309 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005310 /* Strip mode of binary or text modifiers */
5311 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5312 mode = "r";
5313 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5314 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005315 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005316 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005317 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005318 if (fp == NULL)
5319 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005320 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005321 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005322 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005323 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005324}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005325
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005326#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005327#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005328
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005329
Guido van Rossumb6775db1994-08-01 11:34:53 +00005330#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005331PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005332"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005333Set the current process's user id.");
5334
Barry Warsaw53699e91996-12-10 23:23:01 +00005335static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005336posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005337{
5338 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005339 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005340 return NULL;
5341 if (setuid(uid) < 0)
5342 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005343 Py_INCREF(Py_None);
5344 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005345}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005346#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005347
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005348
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005349#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005350PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005351"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005352Set the current process's effective user id.");
5353
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005354static PyObject *
5355posix_seteuid (PyObject *self, PyObject *args)
5356{
5357 int euid;
5358 if (!PyArg_ParseTuple(args, "i", &euid)) {
5359 return NULL;
5360 } else if (seteuid(euid) < 0) {
5361 return posix_error();
5362 } else {
5363 Py_INCREF(Py_None);
5364 return Py_None;
5365 }
5366}
5367#endif /* HAVE_SETEUID */
5368
5369#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005370PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005371"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005372Set the current process's effective group id.");
5373
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005374static PyObject *
5375posix_setegid (PyObject *self, PyObject *args)
5376{
5377 int egid;
5378 if (!PyArg_ParseTuple(args, "i", &egid)) {
5379 return NULL;
5380 } else if (setegid(egid) < 0) {
5381 return posix_error();
5382 } else {
5383 Py_INCREF(Py_None);
5384 return Py_None;
5385 }
5386}
5387#endif /* HAVE_SETEGID */
5388
5389#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005390PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005391"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005392Set the current process's real and effective user ids.");
5393
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005394static PyObject *
5395posix_setreuid (PyObject *self, PyObject *args)
5396{
5397 int ruid, euid;
5398 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5399 return NULL;
5400 } else if (setreuid(ruid, euid) < 0) {
5401 return posix_error();
5402 } else {
5403 Py_INCREF(Py_None);
5404 return Py_None;
5405 }
5406}
5407#endif /* HAVE_SETREUID */
5408
5409#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005410PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005411"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005412Set the current process's real and effective group ids.");
5413
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005414static PyObject *
5415posix_setregid (PyObject *self, PyObject *args)
5416{
5417 int rgid, egid;
5418 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5419 return NULL;
5420 } else if (setregid(rgid, egid) < 0) {
5421 return posix_error();
5422 } else {
5423 Py_INCREF(Py_None);
5424 return Py_None;
5425 }
5426}
5427#endif /* HAVE_SETREGID */
5428
Guido van Rossumb6775db1994-08-01 11:34:53 +00005429#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005430PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005431"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005432Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005433
Barry Warsaw53699e91996-12-10 23:23:01 +00005434static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005435posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005436{
5437 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005438 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005439 return NULL;
5440 if (setgid(gid) < 0)
5441 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005442 Py_INCREF(Py_None);
5443 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005444}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005445#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005446
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005447#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005448PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005449"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005450Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005451
5452static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005453posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005454{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005455 int i, len;
5456 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005457
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005458 if (!PySequence_Check(groups)) {
5459 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5460 return NULL;
5461 }
5462 len = PySequence_Size(groups);
5463 if (len > MAX_GROUPS) {
5464 PyErr_SetString(PyExc_ValueError, "too many groups");
5465 return NULL;
5466 }
5467 for(i = 0; i < len; i++) {
5468 PyObject *elem;
5469 elem = PySequence_GetItem(groups, i);
5470 if (!elem)
5471 return NULL;
5472 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005473 if (!PyLong_Check(elem)) {
5474 PyErr_SetString(PyExc_TypeError,
5475 "groups must be integers");
5476 Py_DECREF(elem);
5477 return NULL;
5478 } else {
5479 unsigned long x = PyLong_AsUnsignedLong(elem);
5480 if (PyErr_Occurred()) {
5481 PyErr_SetString(PyExc_TypeError,
5482 "group id too big");
5483 Py_DECREF(elem);
5484 return NULL;
5485 }
5486 grouplist[i] = x;
5487 /* read back the value to see if it fitted in gid_t */
5488 if (grouplist[i] != x) {
5489 PyErr_SetString(PyExc_TypeError,
5490 "group id too big");
5491 Py_DECREF(elem);
5492 return NULL;
5493 }
5494 }
5495 } else {
5496 long x = PyInt_AsLong(elem);
5497 grouplist[i] = x;
5498 if (grouplist[i] != x) {
5499 PyErr_SetString(PyExc_TypeError,
5500 "group id too big");
5501 Py_DECREF(elem);
5502 return NULL;
5503 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005504 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005505 Py_DECREF(elem);
5506 }
5507
5508 if (setgroups(len, grouplist) < 0)
5509 return posix_error();
5510 Py_INCREF(Py_None);
5511 return Py_None;
5512}
5513#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005514
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005515#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005516static PyObject *
5517wait_helper(int pid, int status, struct rusage *ru)
5518{
5519 PyObject *result;
5520 static PyObject *struct_rusage;
5521
5522 if (pid == -1)
5523 return posix_error();
5524
5525 if (struct_rusage == NULL) {
5526 PyObject *m = PyImport_ImportModule("resource");
5527 if (m == NULL)
5528 return NULL;
5529 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5530 Py_DECREF(m);
5531 if (struct_rusage == NULL)
5532 return NULL;
5533 }
5534
5535 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5536 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5537 if (!result)
5538 return NULL;
5539
5540#ifndef doubletime
5541#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5542#endif
5543
5544 PyStructSequence_SET_ITEM(result, 0,
5545 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5546 PyStructSequence_SET_ITEM(result, 1,
5547 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5548#define SET_INT(result, index, value)\
5549 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5550 SET_INT(result, 2, ru->ru_maxrss);
5551 SET_INT(result, 3, ru->ru_ixrss);
5552 SET_INT(result, 4, ru->ru_idrss);
5553 SET_INT(result, 5, ru->ru_isrss);
5554 SET_INT(result, 6, ru->ru_minflt);
5555 SET_INT(result, 7, ru->ru_majflt);
5556 SET_INT(result, 8, ru->ru_nswap);
5557 SET_INT(result, 9, ru->ru_inblock);
5558 SET_INT(result, 10, ru->ru_oublock);
5559 SET_INT(result, 11, ru->ru_msgsnd);
5560 SET_INT(result, 12, ru->ru_msgrcv);
5561 SET_INT(result, 13, ru->ru_nsignals);
5562 SET_INT(result, 14, ru->ru_nvcsw);
5563 SET_INT(result, 15, ru->ru_nivcsw);
5564#undef SET_INT
5565
5566 if (PyErr_Occurred()) {
5567 Py_DECREF(result);
5568 return NULL;
5569 }
5570
Neal Norwitz9b00a562006-03-20 08:47:12 +00005571 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005572}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005573#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005574
5575#ifdef HAVE_WAIT3
5576PyDoc_STRVAR(posix_wait3__doc__,
5577"wait3(options) -> (pid, status, rusage)\n\n\
5578Wait for completion of a child process.");
5579
5580static PyObject *
5581posix_wait3(PyObject *self, PyObject *args)
5582{
5583 int pid, options;
5584 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005585 WAIT_TYPE status;
5586 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005587
5588 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5589 return NULL;
5590
5591 Py_BEGIN_ALLOW_THREADS
5592 pid = wait3(&status, options, &ru);
5593 Py_END_ALLOW_THREADS
5594
Neal Norwitzd5a37542006-03-20 06:48:34 +00005595 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005596}
5597#endif /* HAVE_WAIT3 */
5598
5599#ifdef HAVE_WAIT4
5600PyDoc_STRVAR(posix_wait4__doc__,
5601"wait4(pid, options) -> (pid, status, rusage)\n\n\
5602Wait for completion of a given child process.");
5603
5604static PyObject *
5605posix_wait4(PyObject *self, PyObject *args)
5606{
5607 int pid, options;
5608 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005609 WAIT_TYPE status;
5610 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005611
5612 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5613 return NULL;
5614
5615 Py_BEGIN_ALLOW_THREADS
5616 pid = wait4(pid, &status, options, &ru);
5617 Py_END_ALLOW_THREADS
5618
Neal Norwitzd5a37542006-03-20 06:48:34 +00005619 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005620}
5621#endif /* HAVE_WAIT4 */
5622
Guido van Rossumb6775db1994-08-01 11:34:53 +00005623#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005624PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005625"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005627
Barry Warsaw53699e91996-12-10 23:23:01 +00005628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005629posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005630{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005631 int pid, options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005632 WAIT_TYPE status;
5633 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005634
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005635 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005636 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005637 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005638 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005639 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005640 if (pid == -1)
5641 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005642
5643 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005644}
5645
Tim Petersab034fa2002-02-01 11:27:43 +00005646#elif defined(HAVE_CWAIT)
5647
5648/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005649PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005650"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005651"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005652
5653static PyObject *
5654posix_waitpid(PyObject *self, PyObject *args)
5655{
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005656 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005657 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005658
5659 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5660 return NULL;
5661 Py_BEGIN_ALLOW_THREADS
5662 pid = _cwait(&status, pid, options);
5663 Py_END_ALLOW_THREADS
5664 if (pid == -1)
5665 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005666
5667 /* shift the status left a byte so this is more like the POSIX waitpid */
5668 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005669}
5670#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005671
Guido van Rossumad0ee831995-03-01 10:34:45 +00005672#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005673PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005674"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005676
Barry Warsaw53699e91996-12-10 23:23:01 +00005677static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005678posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005679{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005680 int pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005681 WAIT_TYPE status;
5682 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005683
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005684 Py_BEGIN_ALLOW_THREADS
5685 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005686 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005687 if (pid == -1)
5688 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005689
5690 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005691}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005692#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005693
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005695PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005696"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005697Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005698
Barry Warsaw53699e91996-12-10 23:23:01 +00005699static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005700posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005701{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005702#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005703 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005704#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005705#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005706 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005707#else
5708 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5709#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005710#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005711}
5712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005713
Guido van Rossumb6775db1994-08-01 11:34:53 +00005714#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005715PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005716"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005717Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005718
Barry Warsaw53699e91996-12-10 23:23:01 +00005719static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005720posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005721{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005722 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005723 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005724 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005725 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005726 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005727 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005728 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005729 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005730 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00005731 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00005732 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005733}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005734#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005735
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005736
Guido van Rossumb6775db1994-08-01 11:34:53 +00005737#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005738PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005739"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005740Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005741
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005743posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005744{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00005745 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005746}
5747#endif /* HAVE_SYMLINK */
5748
5749
5750#ifdef HAVE_TIMES
5751#ifndef HZ
5752#define HZ 60 /* Universal constant :-) */
5753#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00005754
Guido van Rossumd48f2521997-12-05 22:19:34 +00005755#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5756static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005757system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005758{
5759 ULONG value = 0;
5760
5761 Py_BEGIN_ALLOW_THREADS
5762 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5763 Py_END_ALLOW_THREADS
5764
5765 return value;
5766}
5767
5768static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005769posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005770{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005771 /* Currently Only Uptime is Provided -- Others Later */
5772 return Py_BuildValue("ddddd",
5773 (double)0 /* t.tms_utime / HZ */,
5774 (double)0 /* t.tms_stime / HZ */,
5775 (double)0 /* t.tms_cutime / HZ */,
5776 (double)0 /* t.tms_cstime / HZ */,
5777 (double)system_uptime() / 1000);
5778}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005779#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005780static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005781posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005782{
5783 struct tms t;
5784 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005785 errno = 0;
5786 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005787 if (c == (clock_t) -1)
5788 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005789 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00005790 (double)t.tms_utime / HZ,
5791 (double)t.tms_stime / HZ,
5792 (double)t.tms_cutime / HZ,
5793 (double)t.tms_cstime / HZ,
5794 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005795}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005796#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005797#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005798
5799
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005800#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005801#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005802static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005803posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005804{
5805 FILETIME create, exit, kernel, user;
5806 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005807 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005808 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5809 /* The fields of a FILETIME structure are the hi and lo part
5810 of a 64-bit value expressed in 100 nanosecond units.
5811 1e7 is one second in such units; 1e-7 the inverse.
5812 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5813 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005814 return Py_BuildValue(
5815 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005816 (double)(kernel.dwHighDateTime*429.4967296 +
5817 kernel.dwLowDateTime*1e-7),
5818 (double)(user.dwHighDateTime*429.4967296 +
5819 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005820 (double)0,
5821 (double)0,
5822 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005823}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005824#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005825
5826#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005827PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005828"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005829Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005830#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005832
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005833#ifdef HAVE_GETSID
5834PyDoc_STRVAR(posix_getsid__doc__,
5835"getsid(pid) -> sid\n\n\
5836Call the system call getsid().");
5837
5838static PyObject *
5839posix_getsid(PyObject *self, PyObject *args)
5840{
5841 int pid, sid;
5842 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5843 return NULL;
5844 sid = getsid(pid);
5845 if (sid < 0)
5846 return posix_error();
5847 return PyInt_FromLong((long)sid);
5848}
5849#endif /* HAVE_GETSID */
5850
5851
Guido van Rossumb6775db1994-08-01 11:34:53 +00005852#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005853PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005854"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005855Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005856
Barry Warsaw53699e91996-12-10 23:23:01 +00005857static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005858posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005859{
Guido van Rossum687dd131993-05-17 08:34:16 +00005860 if (setsid() < 0)
5861 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005862 Py_INCREF(Py_None);
5863 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005864}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005865#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005866
Guido van Rossumb6775db1994-08-01 11:34:53 +00005867#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005868PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005869"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005870Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005871
Barry Warsaw53699e91996-12-10 23:23:01 +00005872static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005873posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005874{
5875 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005876 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005877 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005878 if (setpgid(pid, pgrp) < 0)
5879 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005880 Py_INCREF(Py_None);
5881 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005882}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005883#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005884
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005885
Guido van Rossumb6775db1994-08-01 11:34:53 +00005886#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005887PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005888"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005889Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005890
Barry Warsaw53699e91996-12-10 23:23:01 +00005891static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005892posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005893{
5894 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005895 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005896 return NULL;
5897 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005898 if (pgid < 0)
5899 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005900 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005901}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005902#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005903
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005904
Guido van Rossumb6775db1994-08-01 11:34:53 +00005905#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005906PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005907"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005908Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005909
Barry Warsaw53699e91996-12-10 23:23:01 +00005910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005911posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005912{
5913 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005914 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005915 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005916 if (tcsetpgrp(fd, pgid) < 0)
5917 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005918 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005919 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005920}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005921#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005922
Guido van Rossum687dd131993-05-17 08:34:16 +00005923/* Functions acting on file descriptors */
5924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005925PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005926"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005928
Barry Warsaw53699e91996-12-10 23:23:01 +00005929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005930posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005931{
Mark Hammondef8b6542001-05-13 08:04:26 +00005932 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005933 int flag;
5934 int mode = 0777;
5935 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005936
5937#ifdef MS_WINDOWS
5938 if (unicode_file_names()) {
5939 PyUnicodeObject *po;
5940 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5941 Py_BEGIN_ALLOW_THREADS
5942 /* PyUnicode_AS_UNICODE OK without thread
5943 lock as it is a simple dereference. */
5944 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5945 Py_END_ALLOW_THREADS
5946 if (fd < 0)
5947 return posix_error();
5948 return PyInt_FromLong((long)fd);
5949 }
5950 /* Drop the argument parsing error as narrow strings
5951 are also valid. */
5952 PyErr_Clear();
5953 }
5954#endif
5955
Tim Peters5aa91602002-01-30 05:46:57 +00005956 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005957 Py_FileSystemDefaultEncoding, &file,
5958 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005959 return NULL;
5960
Barry Warsaw53699e91996-12-10 23:23:01 +00005961 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005962 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005963 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005964 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005965 return posix_error_with_allocated_filename(file);
5966 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005967 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005968}
5969
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005970
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005971PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005972"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005973Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005974
Barry Warsaw53699e91996-12-10 23:23:01 +00005975static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005976posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005977{
5978 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005979 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005980 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005981 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005982 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005983 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005984 if (res < 0)
5985 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005986 Py_INCREF(Py_None);
5987 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005988}
5989
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005991PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005992"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005993Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005994
Barry Warsaw53699e91996-12-10 23:23:01 +00005995static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005996posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005997{
5998 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005999 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006000 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006001 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006002 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006003 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006004 if (fd < 0)
6005 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006006 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006007}
6008
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006010PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006011"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006012Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006013
Barry Warsaw53699e91996-12-10 23:23:01 +00006014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006015posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006016{
6017 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006018 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006019 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006020 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006021 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006022 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006023 if (res < 0)
6024 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006025 Py_INCREF(Py_None);
6026 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006027}
6028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006030PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006031"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006032Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006033
Barry Warsaw53699e91996-12-10 23:23:01 +00006034static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006035posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006036{
6037 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006038#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006039 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006040#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006041 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006042#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006043 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006044 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006045 return NULL;
6046#ifdef SEEK_SET
6047 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6048 switch (how) {
6049 case 0: how = SEEK_SET; break;
6050 case 1: how = SEEK_CUR; break;
6051 case 2: how = SEEK_END; break;
6052 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006053#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006054
6055#if !defined(HAVE_LARGEFILE_SUPPORT)
6056 pos = PyInt_AsLong(posobj);
6057#else
6058 pos = PyLong_Check(posobj) ?
6059 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6060#endif
6061 if (PyErr_Occurred())
6062 return NULL;
6063
Barry Warsaw53699e91996-12-10 23:23:01 +00006064 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006065#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006066 res = _lseeki64(fd, pos, how);
6067#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006068 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006069#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006070 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006071 if (res < 0)
6072 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006073
6074#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006075 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006076#else
6077 return PyLong_FromLongLong(res);
6078#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006079}
6080
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006082PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006083"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006084Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006085
Barry Warsaw53699e91996-12-10 23:23:01 +00006086static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006087posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006088{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006089 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006090 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006091 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006092 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006093 if (size < 0) {
6094 errno = EINVAL;
6095 return posix_error();
6096 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006097 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006098 if (buffer == NULL)
6099 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006100 Py_BEGIN_ALLOW_THREADS
6101 n = read(fd, PyString_AsString(buffer), size);
6102 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006103 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006104 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006105 return posix_error();
6106 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006107 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00006108 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006109 return buffer;
6110}
6111
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006112
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006113PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006114"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006115Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006116
Barry Warsaw53699e91996-12-10 23:23:01 +00006117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006118posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006119{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006120 int fd;
6121 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00006122 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006123
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006124 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006125 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006126 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006127 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006128 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006129 if (size < 0)
6130 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006131 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006132}
6133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006136"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006137Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006138
Barry Warsaw53699e91996-12-10 23:23:01 +00006139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006140posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006141{
6142 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006143 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006144 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006145 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006146 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006147#ifdef __VMS
6148 /* on OpenVMS we must ensure that all bytes are written to the file */
6149 fsync(fd);
6150#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006151 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006152 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006153 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006154 if (res != 0) {
6155#ifdef MS_WINDOWS
6156 return win32_error("fstat", NULL);
6157#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006158 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006159#endif
6160 }
Tim Peters5aa91602002-01-30 05:46:57 +00006161
Martin v. Löwis14694662006-02-03 12:54:16 +00006162 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006163}
6164
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006166PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006167"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006168Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006169
Barry Warsaw53699e91996-12-10 23:23:01 +00006170static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006171posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006172{
Guido van Rossum687dd131993-05-17 08:34:16 +00006173 int fd;
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006174 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006175 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006176 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006177 PyObject *f;
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006178 char *mode;
6179 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006180 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006181
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006182 /* Sanitize mode. See fileobject.c */
6183 mode = PyMem_MALLOC(strlen(orgmode)+3);
6184 if (!mode) {
6185 PyErr_NoMemory();
6186 return NULL;
6187 }
6188 strcpy(mode, orgmode);
6189 if (_PyFile_SanitizeMode(mode)) {
6190 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006191 return NULL;
6192 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006193 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006194#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006195 if (mode[0] == 'a') {
6196 /* try to make sure the O_APPEND flag is set */
6197 int flags;
6198 flags = fcntl(fd, F_GETFL);
6199 if (flags != -1)
6200 fcntl(fd, F_SETFL, flags | O_APPEND);
6201 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006202 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006203 /* restore old mode if fdopen failed */
6204 fcntl(fd, F_SETFL, flags);
6205 } else {
6206 fp = fdopen(fd, mode);
6207 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006208#else
6209 fp = fdopen(fd, mode);
6210#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006211 Py_END_ALLOW_THREADS
Neal Norwitz6ca6f142007-05-09 06:45:53 +00006212 PyMem_FREE(mode);
Guido van Rossum687dd131993-05-17 08:34:16 +00006213 if (fp == NULL)
6214 return posix_error();
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006215 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006216 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006217 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006218 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006219}
6220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006221PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006222"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006223Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006224connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006225
6226static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006227posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006228{
6229 int fd;
6230 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6231 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006232 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006233}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006234
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006235#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006237"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006239
Barry Warsaw53699e91996-12-10 23:23:01 +00006240static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006241posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006242{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006243#if defined(PYOS_OS2)
6244 HFILE read, write;
6245 APIRET rc;
6246
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006247 Py_BEGIN_ALLOW_THREADS
6248 rc = DosCreatePipe( &read, &write, 4096);
6249 Py_END_ALLOW_THREADS
6250 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006251 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006252
6253 return Py_BuildValue("(ii)", read, write);
6254#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006255#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006256 int fds[2];
6257 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006258 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006259 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006260 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006261 if (res != 0)
6262 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006263 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006264#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006265 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006266 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006267 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006268 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006269 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006270 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006271 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006272 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006273 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6274 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006275 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006276#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006277#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006278}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006279#endif /* HAVE_PIPE */
6280
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006281
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006282#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006283PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006284"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006285Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006286
Barry Warsaw53699e91996-12-10 23:23:01 +00006287static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006288posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006289{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006290 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006291 int mode = 0666;
6292 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006293 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006294 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006295 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006296 res = mkfifo(filename, mode);
6297 Py_END_ALLOW_THREADS
6298 if (res < 0)
6299 return posix_error();
6300 Py_INCREF(Py_None);
6301 return Py_None;
6302}
6303#endif
6304
6305
Neal Norwitz11690112002-07-30 01:08:28 +00006306#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006307PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006308"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006309Create a filesystem node (file, device special file or named pipe)\n\
6310named filename. mode specifies both the permissions to use and the\n\
6311type of node to be created, being combined (bitwise OR) with one of\n\
6312S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006313device defines the newly created device special file (probably using\n\
6314os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006315
6316
6317static PyObject *
6318posix_mknod(PyObject *self, PyObject *args)
6319{
6320 char *filename;
6321 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006322 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006323 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006324 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006325 return NULL;
6326 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006327 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006328 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006329 if (res < 0)
6330 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006331 Py_INCREF(Py_None);
6332 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006333}
6334#endif
6335
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006336#ifdef HAVE_DEVICE_MACROS
6337PyDoc_STRVAR(posix_major__doc__,
6338"major(device) -> major number\n\
6339Extracts a device major number from a raw device number.");
6340
6341static PyObject *
6342posix_major(PyObject *self, PyObject *args)
6343{
6344 int device;
6345 if (!PyArg_ParseTuple(args, "i:major", &device))
6346 return NULL;
6347 return PyInt_FromLong((long)major(device));
6348}
6349
6350PyDoc_STRVAR(posix_minor__doc__,
6351"minor(device) -> minor number\n\
6352Extracts a device minor number from a raw device number.");
6353
6354static PyObject *
6355posix_minor(PyObject *self, PyObject *args)
6356{
6357 int device;
6358 if (!PyArg_ParseTuple(args, "i:minor", &device))
6359 return NULL;
6360 return PyInt_FromLong((long)minor(device));
6361}
6362
6363PyDoc_STRVAR(posix_makedev__doc__,
6364"makedev(major, minor) -> device number\n\
6365Composes a raw device number from the major and minor device numbers.");
6366
6367static PyObject *
6368posix_makedev(PyObject *self, PyObject *args)
6369{
6370 int major, minor;
6371 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6372 return NULL;
6373 return PyInt_FromLong((long)makedev(major, minor));
6374}
6375#endif /* device macros */
6376
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006377
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006378#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006379PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006380"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006381Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006382
Barry Warsaw53699e91996-12-10 23:23:01 +00006383static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006384posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006385{
6386 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006387 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006388 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006389 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006390
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006391 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006392 return NULL;
6393
6394#if !defined(HAVE_LARGEFILE_SUPPORT)
6395 length = PyInt_AsLong(lenobj);
6396#else
6397 length = PyLong_Check(lenobj) ?
6398 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6399#endif
6400 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006401 return NULL;
6402
Barry Warsaw53699e91996-12-10 23:23:01 +00006403 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006404 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006405 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006406 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006407 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006408 return NULL;
6409 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006410 Py_INCREF(Py_None);
6411 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006412}
6413#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006414
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006415#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006416PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006417"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006418Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006419
Fred Drake762e2061999-08-26 17:23:54 +00006420/* Save putenv() parameters as values here, so we can collect them when they
6421 * get re-set with another call for the same key. */
6422static PyObject *posix_putenv_garbage;
6423
Tim Peters5aa91602002-01-30 05:46:57 +00006424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006425posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006426{
6427 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006428 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006429 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006430 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006431
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006432 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006433 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006434
6435#if defined(PYOS_OS2)
6436 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6437 APIRET rc;
6438
Guido van Rossumd48f2521997-12-05 22:19:34 +00006439 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6440 if (rc != NO_ERROR)
6441 return os2_error(rc);
6442
6443 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6444 APIRET rc;
6445
Guido van Rossumd48f2521997-12-05 22:19:34 +00006446 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6447 if (rc != NO_ERROR)
6448 return os2_error(rc);
6449 } else {
6450#endif
6451
Fred Drake762e2061999-08-26 17:23:54 +00006452 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006453 len = strlen(s1) + strlen(s2) + 2;
6454 /* len includes space for a trailing \0; the size arg to
6455 PyString_FromStringAndSize does not count that */
6456 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006457 if (newstr == NULL)
6458 return PyErr_NoMemory();
Anthony Baxter64182fe2006-04-11 12:14:09 +00006459 newenv = PyString_AS_STRING(newstr);
6460 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6461 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006462 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006463 posix_error();
6464 return NULL;
6465 }
Fred Drake762e2061999-08-26 17:23:54 +00006466 /* Install the first arg and newstr in posix_putenv_garbage;
6467 * this will cause previous value to be collected. This has to
6468 * happen after the real putenv() call because the old value
6469 * was still accessible until then. */
6470 if (PyDict_SetItem(posix_putenv_garbage,
6471 PyTuple_GET_ITEM(args, 0), newstr)) {
6472 /* really not much we can do; just leak */
6473 PyErr_Clear();
6474 }
6475 else {
6476 Py_DECREF(newstr);
6477 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006478
6479#if defined(PYOS_OS2)
6480 }
6481#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006482 Py_INCREF(Py_None);
6483 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006484}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006485#endif /* putenv */
6486
Guido van Rossumc524d952001-10-19 01:31:59 +00006487#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006488PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006489"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006490Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006491
6492static PyObject *
6493posix_unsetenv(PyObject *self, PyObject *args)
6494{
6495 char *s1;
6496
6497 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6498 return NULL;
6499
6500 unsetenv(s1);
6501
6502 /* Remove the key from posix_putenv_garbage;
6503 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006504 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006505 * old value was still accessible until then.
6506 */
6507 if (PyDict_DelItem(posix_putenv_garbage,
6508 PyTuple_GET_ITEM(args, 0))) {
6509 /* really not much we can do; just leak */
6510 PyErr_Clear();
6511 }
6512
6513 Py_INCREF(Py_None);
6514 return Py_None;
6515}
6516#endif /* unsetenv */
6517
Guido van Rossumb6a47161997-09-15 22:54:34 +00006518#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006519PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006520"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006521Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006522
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006524posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006525{
6526 int code;
6527 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006528 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006529 return NULL;
6530 message = strerror(code);
6531 if (message == NULL) {
6532 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006533 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006534 return NULL;
6535 }
6536 return PyString_FromString(message);
6537}
6538#endif /* strerror */
6539
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006540
Guido van Rossumc9641791998-08-04 15:26:23 +00006541#ifdef HAVE_SYS_WAIT_H
6542
Fred Drake106c1a02002-04-23 15:58:02 +00006543#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006544PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006545"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006546Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006547
6548static PyObject *
6549posix_WCOREDUMP(PyObject *self, PyObject *args)
6550{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006551 WAIT_TYPE status;
6552 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006553
Neal Norwitzd5a37542006-03-20 06:48:34 +00006554 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006555 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006556
6557 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006558}
6559#endif /* WCOREDUMP */
6560
6561#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006562PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006563"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006564Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006565job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006566
6567static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006568posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006569{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006570 WAIT_TYPE status;
6571 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006572
Neal Norwitzd5a37542006-03-20 06:48:34 +00006573 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006574 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006575
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006576 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006577}
6578#endif /* WIFCONTINUED */
6579
Guido van Rossumc9641791998-08-04 15:26:23 +00006580#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006581PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006582"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006583Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006584
6585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006586posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006587{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006588 WAIT_TYPE status;
6589 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006590
Neal Norwitzd5a37542006-03-20 06:48:34 +00006591 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006592 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006593
Fred Drake106c1a02002-04-23 15:58:02 +00006594 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006595}
6596#endif /* WIFSTOPPED */
6597
6598#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006599PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006600"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006601Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006602
6603static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006604posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006605{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006606 WAIT_TYPE status;
6607 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006608
Neal Norwitzd5a37542006-03-20 06:48:34 +00006609 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006610 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006611
Fred Drake106c1a02002-04-23 15:58:02 +00006612 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006613}
6614#endif /* WIFSIGNALED */
6615
6616#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006617PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006618"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006619Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006620system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006621
6622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006623posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006624{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006625 WAIT_TYPE status;
6626 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006627
Neal Norwitzd5a37542006-03-20 06:48:34 +00006628 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006629 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006630
Fred Drake106c1a02002-04-23 15:58:02 +00006631 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006632}
6633#endif /* WIFEXITED */
6634
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006635#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006636PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006637"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006638Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006639
6640static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006641posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006642{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006643 WAIT_TYPE status;
6644 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006645
Neal Norwitzd5a37542006-03-20 06:48:34 +00006646 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006647 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006648
Guido van Rossumc9641791998-08-04 15:26:23 +00006649 return Py_BuildValue("i", WEXITSTATUS(status));
6650}
6651#endif /* WEXITSTATUS */
6652
6653#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006654PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006655"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006656Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006657value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006658
6659static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006660posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006661{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006662 WAIT_TYPE status;
6663 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006664
Neal Norwitzd5a37542006-03-20 06:48:34 +00006665 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006666 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006667
Guido van Rossumc9641791998-08-04 15:26:23 +00006668 return Py_BuildValue("i", WTERMSIG(status));
6669}
6670#endif /* WTERMSIG */
6671
6672#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006673PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006674"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006675Return the signal that stopped the process that provided\n\
6676the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006677
6678static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006679posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006680{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006681 WAIT_TYPE status;
6682 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006683
Neal Norwitzd5a37542006-03-20 06:48:34 +00006684 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006685 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006686
Guido van Rossumc9641791998-08-04 15:26:23 +00006687 return Py_BuildValue("i", WSTOPSIG(status));
6688}
6689#endif /* WSTOPSIG */
6690
6691#endif /* HAVE_SYS_WAIT_H */
6692
6693
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006694#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006695#ifdef _SCO_DS
6696/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6697 needed definitions in sys/statvfs.h */
6698#define _SVID3
6699#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006700#include <sys/statvfs.h>
6701
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006702static PyObject*
6703_pystatvfs_fromstructstatvfs(struct statvfs st) {
6704 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6705 if (v == NULL)
6706 return NULL;
6707
6708#if !defined(HAVE_LARGEFILE_SUPPORT)
6709 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6710 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6711 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6712 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6713 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6714 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6715 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6716 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6717 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6718 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6719#else
6720 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6721 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006722 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006723 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006724 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006725 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006726 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006727 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006728 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006729 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006730 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006731 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006732 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006733 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006734 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6735 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6736#endif
6737
6738 return v;
6739}
6740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006741PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006742"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006743Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006744
6745static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006746posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006747{
6748 int fd, res;
6749 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006750
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006751 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006752 return NULL;
6753 Py_BEGIN_ALLOW_THREADS
6754 res = fstatvfs(fd, &st);
6755 Py_END_ALLOW_THREADS
6756 if (res != 0)
6757 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006758
6759 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006760}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006761#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006762
6763
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006764#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006765#include <sys/statvfs.h>
6766
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006767PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006768"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006769Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006770
6771static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006772posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006773{
6774 char *path;
6775 int res;
6776 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006777 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006778 return NULL;
6779 Py_BEGIN_ALLOW_THREADS
6780 res = statvfs(path, &st);
6781 Py_END_ALLOW_THREADS
6782 if (res != 0)
6783 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006784
6785 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006786}
6787#endif /* HAVE_STATVFS */
6788
6789
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006790#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006791PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006792"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006793Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006794The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006795or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006796
6797static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006798posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006799{
6800 PyObject *result = NULL;
6801 char *dir = NULL;
6802 char *pfx = NULL;
6803 char *name;
6804
6805 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6806 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006807
6808 if (PyErr_Warn(PyExc_RuntimeWarning,
6809 "tempnam is a potential security risk to your program") < 0)
6810 return NULL;
6811
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006812#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006813 name = _tempnam(dir, pfx);
6814#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006815 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006816#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006817 if (name == NULL)
6818 return PyErr_NoMemory();
6819 result = PyString_FromString(name);
6820 free(name);
6821 return result;
6822}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006823#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006824
6825
6826#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006827PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006828"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006829Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006830
6831static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006832posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006833{
6834 FILE *fp;
6835
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006836 fp = tmpfile();
6837 if (fp == NULL)
6838 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006839 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006840}
6841#endif
6842
6843
6844#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006845PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006846"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006847Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006848
6849static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006850posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006851{
6852 char buffer[L_tmpnam];
6853 char *name;
6854
Skip Montanaro95618b52001-08-18 18:52:10 +00006855 if (PyErr_Warn(PyExc_RuntimeWarning,
6856 "tmpnam is a potential security risk to your program") < 0)
6857 return NULL;
6858
Greg Wardb48bc172000-03-01 21:51:56 +00006859#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006860 name = tmpnam_r(buffer);
6861#else
6862 name = tmpnam(buffer);
6863#endif
6864 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006865 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006866#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006867 "unexpected NULL from tmpnam_r"
6868#else
6869 "unexpected NULL from tmpnam"
6870#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006871 );
6872 PyErr_SetObject(PyExc_OSError, err);
6873 Py_XDECREF(err);
6874 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006875 }
6876 return PyString_FromString(buffer);
6877}
6878#endif
6879
6880
Fred Drakec9680921999-12-13 16:37:25 +00006881/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6882 * It maps strings representing configuration variable names to
6883 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006884 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006885 * rarely-used constants. There are three separate tables that use
6886 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006887 *
6888 * This code is always included, even if none of the interfaces that
6889 * need it are included. The #if hackery needed to avoid it would be
6890 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006891 */
6892struct constdef {
6893 char *name;
6894 long value;
6895};
6896
Fred Drake12c6e2d1999-12-14 21:25:03 +00006897static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006898conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6899 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006900{
6901 if (PyInt_Check(arg)) {
6902 *valuep = PyInt_AS_LONG(arg);
6903 return 1;
6904 }
6905 if (PyString_Check(arg)) {
6906 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006907 size_t lo = 0;
6908 size_t mid;
6909 size_t hi = tablesize;
6910 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006911 char *confname = PyString_AS_STRING(arg);
6912 while (lo < hi) {
6913 mid = (lo + hi) / 2;
6914 cmp = strcmp(confname, table[mid].name);
6915 if (cmp < 0)
6916 hi = mid;
6917 else if (cmp > 0)
6918 lo = mid + 1;
6919 else {
6920 *valuep = table[mid].value;
6921 return 1;
6922 }
6923 }
6924 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6925 }
6926 else
6927 PyErr_SetString(PyExc_TypeError,
6928 "configuration names must be strings or integers");
6929 return 0;
6930}
6931
6932
6933#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6934static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006935#ifdef _PC_ABI_AIO_XFER_MAX
6936 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6937#endif
6938#ifdef _PC_ABI_ASYNC_IO
6939 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6940#endif
Fred Drakec9680921999-12-13 16:37:25 +00006941#ifdef _PC_ASYNC_IO
6942 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6943#endif
6944#ifdef _PC_CHOWN_RESTRICTED
6945 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6946#endif
6947#ifdef _PC_FILESIZEBITS
6948 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6949#endif
6950#ifdef _PC_LAST
6951 {"PC_LAST", _PC_LAST},
6952#endif
6953#ifdef _PC_LINK_MAX
6954 {"PC_LINK_MAX", _PC_LINK_MAX},
6955#endif
6956#ifdef _PC_MAX_CANON
6957 {"PC_MAX_CANON", _PC_MAX_CANON},
6958#endif
6959#ifdef _PC_MAX_INPUT
6960 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6961#endif
6962#ifdef _PC_NAME_MAX
6963 {"PC_NAME_MAX", _PC_NAME_MAX},
6964#endif
6965#ifdef _PC_NO_TRUNC
6966 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6967#endif
6968#ifdef _PC_PATH_MAX
6969 {"PC_PATH_MAX", _PC_PATH_MAX},
6970#endif
6971#ifdef _PC_PIPE_BUF
6972 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6973#endif
6974#ifdef _PC_PRIO_IO
6975 {"PC_PRIO_IO", _PC_PRIO_IO},
6976#endif
6977#ifdef _PC_SOCK_MAXBUF
6978 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6979#endif
6980#ifdef _PC_SYNC_IO
6981 {"PC_SYNC_IO", _PC_SYNC_IO},
6982#endif
6983#ifdef _PC_VDISABLE
6984 {"PC_VDISABLE", _PC_VDISABLE},
6985#endif
6986};
6987
Fred Drakec9680921999-12-13 16:37:25 +00006988static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006989conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006990{
6991 return conv_confname(arg, valuep, posix_constants_pathconf,
6992 sizeof(posix_constants_pathconf)
6993 / sizeof(struct constdef));
6994}
6995#endif
6996
6997#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006998PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006999"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007000Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007001If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007002
7003static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007004posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007005{
7006 PyObject *result = NULL;
7007 int name, fd;
7008
Fred Drake12c6e2d1999-12-14 21:25:03 +00007009 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7010 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007011 long limit;
7012
7013 errno = 0;
7014 limit = fpathconf(fd, name);
7015 if (limit == -1 && errno != 0)
7016 posix_error();
7017 else
7018 result = PyInt_FromLong(limit);
7019 }
7020 return result;
7021}
7022#endif
7023
7024
7025#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007026PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007027"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007028Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007029If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007030
7031static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007032posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007033{
7034 PyObject *result = NULL;
7035 int name;
7036 char *path;
7037
7038 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7039 conv_path_confname, &name)) {
7040 long limit;
7041
7042 errno = 0;
7043 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007044 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007045 if (errno == EINVAL)
7046 /* could be a path or name problem */
7047 posix_error();
7048 else
7049 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007050 }
Fred Drakec9680921999-12-13 16:37:25 +00007051 else
7052 result = PyInt_FromLong(limit);
7053 }
7054 return result;
7055}
7056#endif
7057
7058#ifdef HAVE_CONFSTR
7059static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007060#ifdef _CS_ARCHITECTURE
7061 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7062#endif
7063#ifdef _CS_HOSTNAME
7064 {"CS_HOSTNAME", _CS_HOSTNAME},
7065#endif
7066#ifdef _CS_HW_PROVIDER
7067 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7068#endif
7069#ifdef _CS_HW_SERIAL
7070 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7071#endif
7072#ifdef _CS_INITTAB_NAME
7073 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7074#endif
Fred Drakec9680921999-12-13 16:37:25 +00007075#ifdef _CS_LFS64_CFLAGS
7076 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7077#endif
7078#ifdef _CS_LFS64_LDFLAGS
7079 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7080#endif
7081#ifdef _CS_LFS64_LIBS
7082 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7083#endif
7084#ifdef _CS_LFS64_LINTFLAGS
7085 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7086#endif
7087#ifdef _CS_LFS_CFLAGS
7088 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7089#endif
7090#ifdef _CS_LFS_LDFLAGS
7091 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7092#endif
7093#ifdef _CS_LFS_LIBS
7094 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7095#endif
7096#ifdef _CS_LFS_LINTFLAGS
7097 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7098#endif
Fred Draked86ed291999-12-15 15:34:33 +00007099#ifdef _CS_MACHINE
7100 {"CS_MACHINE", _CS_MACHINE},
7101#endif
Fred Drakec9680921999-12-13 16:37:25 +00007102#ifdef _CS_PATH
7103 {"CS_PATH", _CS_PATH},
7104#endif
Fred Draked86ed291999-12-15 15:34:33 +00007105#ifdef _CS_RELEASE
7106 {"CS_RELEASE", _CS_RELEASE},
7107#endif
7108#ifdef _CS_SRPC_DOMAIN
7109 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7110#endif
7111#ifdef _CS_SYSNAME
7112 {"CS_SYSNAME", _CS_SYSNAME},
7113#endif
7114#ifdef _CS_VERSION
7115 {"CS_VERSION", _CS_VERSION},
7116#endif
Fred Drakec9680921999-12-13 16:37:25 +00007117#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7118 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7119#endif
7120#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7121 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7122#endif
7123#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7124 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7125#endif
7126#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7127 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7128#endif
7129#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7130 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7131#endif
7132#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7133 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7134#endif
7135#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7136 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7137#endif
7138#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7139 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7140#endif
7141#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7142 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7143#endif
7144#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7145 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7146#endif
7147#ifdef _CS_XBS5_LP64_OFF64_LIBS
7148 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7149#endif
7150#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7151 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7152#endif
7153#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7154 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7155#endif
7156#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7157 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7158#endif
7159#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7160 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7161#endif
7162#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7163 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7164#endif
Fred Draked86ed291999-12-15 15:34:33 +00007165#ifdef _MIPS_CS_AVAIL_PROCESSORS
7166 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7167#endif
7168#ifdef _MIPS_CS_BASE
7169 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7170#endif
7171#ifdef _MIPS_CS_HOSTID
7172 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7173#endif
7174#ifdef _MIPS_CS_HW_NAME
7175 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7176#endif
7177#ifdef _MIPS_CS_NUM_PROCESSORS
7178 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7179#endif
7180#ifdef _MIPS_CS_OSREL_MAJ
7181 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7182#endif
7183#ifdef _MIPS_CS_OSREL_MIN
7184 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7185#endif
7186#ifdef _MIPS_CS_OSREL_PATCH
7187 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7188#endif
7189#ifdef _MIPS_CS_OS_NAME
7190 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7191#endif
7192#ifdef _MIPS_CS_OS_PROVIDER
7193 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7194#endif
7195#ifdef _MIPS_CS_PROCESSORS
7196 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7197#endif
7198#ifdef _MIPS_CS_SERIAL
7199 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7200#endif
7201#ifdef _MIPS_CS_VENDOR
7202 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7203#endif
Fred Drakec9680921999-12-13 16:37:25 +00007204};
7205
7206static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007207conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007208{
7209 return conv_confname(arg, valuep, posix_constants_confstr,
7210 sizeof(posix_constants_confstr)
7211 / sizeof(struct constdef));
7212}
7213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007214PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007215"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007216Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007217
7218static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007219posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007220{
7221 PyObject *result = NULL;
7222 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007223 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007224
7225 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007226 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007227
Fred Drakec9680921999-12-13 16:37:25 +00007228 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007229 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007230 if (len == 0) {
7231 if (errno) {
7232 posix_error();
7233 }
7234 else {
7235 result = Py_None;
7236 Py_INCREF(Py_None);
7237 }
Fred Drakec9680921999-12-13 16:37:25 +00007238 }
7239 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007240 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz449b24e2006-04-20 06:56:05 +00007241 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007242 if (result != NULL)
Neal Norwitz449b24e2006-04-20 06:56:05 +00007243 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007244 }
7245 else
Neal Norwitz449b24e2006-04-20 06:56:05 +00007246 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007247 }
7248 }
7249 return result;
7250}
7251#endif
7252
7253
7254#ifdef HAVE_SYSCONF
7255static struct constdef posix_constants_sysconf[] = {
7256#ifdef _SC_2_CHAR_TERM
7257 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7258#endif
7259#ifdef _SC_2_C_BIND
7260 {"SC_2_C_BIND", _SC_2_C_BIND},
7261#endif
7262#ifdef _SC_2_C_DEV
7263 {"SC_2_C_DEV", _SC_2_C_DEV},
7264#endif
7265#ifdef _SC_2_C_VERSION
7266 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7267#endif
7268#ifdef _SC_2_FORT_DEV
7269 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7270#endif
7271#ifdef _SC_2_FORT_RUN
7272 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7273#endif
7274#ifdef _SC_2_LOCALEDEF
7275 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7276#endif
7277#ifdef _SC_2_SW_DEV
7278 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7279#endif
7280#ifdef _SC_2_UPE
7281 {"SC_2_UPE", _SC_2_UPE},
7282#endif
7283#ifdef _SC_2_VERSION
7284 {"SC_2_VERSION", _SC_2_VERSION},
7285#endif
Fred Draked86ed291999-12-15 15:34:33 +00007286#ifdef _SC_ABI_ASYNCHRONOUS_IO
7287 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7288#endif
7289#ifdef _SC_ACL
7290 {"SC_ACL", _SC_ACL},
7291#endif
Fred Drakec9680921999-12-13 16:37:25 +00007292#ifdef _SC_AIO_LISTIO_MAX
7293 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7294#endif
Fred Drakec9680921999-12-13 16:37:25 +00007295#ifdef _SC_AIO_MAX
7296 {"SC_AIO_MAX", _SC_AIO_MAX},
7297#endif
7298#ifdef _SC_AIO_PRIO_DELTA_MAX
7299 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7300#endif
7301#ifdef _SC_ARG_MAX
7302 {"SC_ARG_MAX", _SC_ARG_MAX},
7303#endif
7304#ifdef _SC_ASYNCHRONOUS_IO
7305 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7306#endif
7307#ifdef _SC_ATEXIT_MAX
7308 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7309#endif
Fred Draked86ed291999-12-15 15:34:33 +00007310#ifdef _SC_AUDIT
7311 {"SC_AUDIT", _SC_AUDIT},
7312#endif
Fred Drakec9680921999-12-13 16:37:25 +00007313#ifdef _SC_AVPHYS_PAGES
7314 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7315#endif
7316#ifdef _SC_BC_BASE_MAX
7317 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7318#endif
7319#ifdef _SC_BC_DIM_MAX
7320 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7321#endif
7322#ifdef _SC_BC_SCALE_MAX
7323 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7324#endif
7325#ifdef _SC_BC_STRING_MAX
7326 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7327#endif
Fred Draked86ed291999-12-15 15:34:33 +00007328#ifdef _SC_CAP
7329 {"SC_CAP", _SC_CAP},
7330#endif
Fred Drakec9680921999-12-13 16:37:25 +00007331#ifdef _SC_CHARCLASS_NAME_MAX
7332 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7333#endif
7334#ifdef _SC_CHAR_BIT
7335 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7336#endif
7337#ifdef _SC_CHAR_MAX
7338 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7339#endif
7340#ifdef _SC_CHAR_MIN
7341 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7342#endif
7343#ifdef _SC_CHILD_MAX
7344 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7345#endif
7346#ifdef _SC_CLK_TCK
7347 {"SC_CLK_TCK", _SC_CLK_TCK},
7348#endif
7349#ifdef _SC_COHER_BLKSZ
7350 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7351#endif
7352#ifdef _SC_COLL_WEIGHTS_MAX
7353 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7354#endif
7355#ifdef _SC_DCACHE_ASSOC
7356 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7357#endif
7358#ifdef _SC_DCACHE_BLKSZ
7359 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7360#endif
7361#ifdef _SC_DCACHE_LINESZ
7362 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7363#endif
7364#ifdef _SC_DCACHE_SZ
7365 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7366#endif
7367#ifdef _SC_DCACHE_TBLKSZ
7368 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7369#endif
7370#ifdef _SC_DELAYTIMER_MAX
7371 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7372#endif
7373#ifdef _SC_EQUIV_CLASS_MAX
7374 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7375#endif
7376#ifdef _SC_EXPR_NEST_MAX
7377 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7378#endif
7379#ifdef _SC_FSYNC
7380 {"SC_FSYNC", _SC_FSYNC},
7381#endif
7382#ifdef _SC_GETGR_R_SIZE_MAX
7383 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7384#endif
7385#ifdef _SC_GETPW_R_SIZE_MAX
7386 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7387#endif
7388#ifdef _SC_ICACHE_ASSOC
7389 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7390#endif
7391#ifdef _SC_ICACHE_BLKSZ
7392 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7393#endif
7394#ifdef _SC_ICACHE_LINESZ
7395 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7396#endif
7397#ifdef _SC_ICACHE_SZ
7398 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7399#endif
Fred Draked86ed291999-12-15 15:34:33 +00007400#ifdef _SC_INF
7401 {"SC_INF", _SC_INF},
7402#endif
Fred Drakec9680921999-12-13 16:37:25 +00007403#ifdef _SC_INT_MAX
7404 {"SC_INT_MAX", _SC_INT_MAX},
7405#endif
7406#ifdef _SC_INT_MIN
7407 {"SC_INT_MIN", _SC_INT_MIN},
7408#endif
7409#ifdef _SC_IOV_MAX
7410 {"SC_IOV_MAX", _SC_IOV_MAX},
7411#endif
Fred Draked86ed291999-12-15 15:34:33 +00007412#ifdef _SC_IP_SECOPTS
7413 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7414#endif
Fred Drakec9680921999-12-13 16:37:25 +00007415#ifdef _SC_JOB_CONTROL
7416 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7417#endif
Fred Draked86ed291999-12-15 15:34:33 +00007418#ifdef _SC_KERN_POINTERS
7419 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7420#endif
7421#ifdef _SC_KERN_SIM
7422 {"SC_KERN_SIM", _SC_KERN_SIM},
7423#endif
Fred Drakec9680921999-12-13 16:37:25 +00007424#ifdef _SC_LINE_MAX
7425 {"SC_LINE_MAX", _SC_LINE_MAX},
7426#endif
7427#ifdef _SC_LOGIN_NAME_MAX
7428 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7429#endif
7430#ifdef _SC_LOGNAME_MAX
7431 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7432#endif
7433#ifdef _SC_LONG_BIT
7434 {"SC_LONG_BIT", _SC_LONG_BIT},
7435#endif
Fred Draked86ed291999-12-15 15:34:33 +00007436#ifdef _SC_MAC
7437 {"SC_MAC", _SC_MAC},
7438#endif
Fred Drakec9680921999-12-13 16:37:25 +00007439#ifdef _SC_MAPPED_FILES
7440 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7441#endif
7442#ifdef _SC_MAXPID
7443 {"SC_MAXPID", _SC_MAXPID},
7444#endif
7445#ifdef _SC_MB_LEN_MAX
7446 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7447#endif
7448#ifdef _SC_MEMLOCK
7449 {"SC_MEMLOCK", _SC_MEMLOCK},
7450#endif
7451#ifdef _SC_MEMLOCK_RANGE
7452 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7453#endif
7454#ifdef _SC_MEMORY_PROTECTION
7455 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7456#endif
7457#ifdef _SC_MESSAGE_PASSING
7458 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7459#endif
Fred Draked86ed291999-12-15 15:34:33 +00007460#ifdef _SC_MMAP_FIXED_ALIGNMENT
7461 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7462#endif
Fred Drakec9680921999-12-13 16:37:25 +00007463#ifdef _SC_MQ_OPEN_MAX
7464 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7465#endif
7466#ifdef _SC_MQ_PRIO_MAX
7467 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7468#endif
Fred Draked86ed291999-12-15 15:34:33 +00007469#ifdef _SC_NACLS_MAX
7470 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7471#endif
Fred Drakec9680921999-12-13 16:37:25 +00007472#ifdef _SC_NGROUPS_MAX
7473 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7474#endif
7475#ifdef _SC_NL_ARGMAX
7476 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7477#endif
7478#ifdef _SC_NL_LANGMAX
7479 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7480#endif
7481#ifdef _SC_NL_MSGMAX
7482 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7483#endif
7484#ifdef _SC_NL_NMAX
7485 {"SC_NL_NMAX", _SC_NL_NMAX},
7486#endif
7487#ifdef _SC_NL_SETMAX
7488 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7489#endif
7490#ifdef _SC_NL_TEXTMAX
7491 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7492#endif
7493#ifdef _SC_NPROCESSORS_CONF
7494 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7495#endif
7496#ifdef _SC_NPROCESSORS_ONLN
7497 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7498#endif
Fred Draked86ed291999-12-15 15:34:33 +00007499#ifdef _SC_NPROC_CONF
7500 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7501#endif
7502#ifdef _SC_NPROC_ONLN
7503 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7504#endif
Fred Drakec9680921999-12-13 16:37:25 +00007505#ifdef _SC_NZERO
7506 {"SC_NZERO", _SC_NZERO},
7507#endif
7508#ifdef _SC_OPEN_MAX
7509 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7510#endif
7511#ifdef _SC_PAGESIZE
7512 {"SC_PAGESIZE", _SC_PAGESIZE},
7513#endif
7514#ifdef _SC_PAGE_SIZE
7515 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7516#endif
7517#ifdef _SC_PASS_MAX
7518 {"SC_PASS_MAX", _SC_PASS_MAX},
7519#endif
7520#ifdef _SC_PHYS_PAGES
7521 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7522#endif
7523#ifdef _SC_PII
7524 {"SC_PII", _SC_PII},
7525#endif
7526#ifdef _SC_PII_INTERNET
7527 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7528#endif
7529#ifdef _SC_PII_INTERNET_DGRAM
7530 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7531#endif
7532#ifdef _SC_PII_INTERNET_STREAM
7533 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7534#endif
7535#ifdef _SC_PII_OSI
7536 {"SC_PII_OSI", _SC_PII_OSI},
7537#endif
7538#ifdef _SC_PII_OSI_CLTS
7539 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7540#endif
7541#ifdef _SC_PII_OSI_COTS
7542 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7543#endif
7544#ifdef _SC_PII_OSI_M
7545 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7546#endif
7547#ifdef _SC_PII_SOCKET
7548 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7549#endif
7550#ifdef _SC_PII_XTI
7551 {"SC_PII_XTI", _SC_PII_XTI},
7552#endif
7553#ifdef _SC_POLL
7554 {"SC_POLL", _SC_POLL},
7555#endif
7556#ifdef _SC_PRIORITIZED_IO
7557 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7558#endif
7559#ifdef _SC_PRIORITY_SCHEDULING
7560 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7561#endif
7562#ifdef _SC_REALTIME_SIGNALS
7563 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7564#endif
7565#ifdef _SC_RE_DUP_MAX
7566 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7567#endif
7568#ifdef _SC_RTSIG_MAX
7569 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7570#endif
7571#ifdef _SC_SAVED_IDS
7572 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7573#endif
7574#ifdef _SC_SCHAR_MAX
7575 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7576#endif
7577#ifdef _SC_SCHAR_MIN
7578 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7579#endif
7580#ifdef _SC_SELECT
7581 {"SC_SELECT", _SC_SELECT},
7582#endif
7583#ifdef _SC_SEMAPHORES
7584 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7585#endif
7586#ifdef _SC_SEM_NSEMS_MAX
7587 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7588#endif
7589#ifdef _SC_SEM_VALUE_MAX
7590 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7591#endif
7592#ifdef _SC_SHARED_MEMORY_OBJECTS
7593 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7594#endif
7595#ifdef _SC_SHRT_MAX
7596 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7597#endif
7598#ifdef _SC_SHRT_MIN
7599 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7600#endif
7601#ifdef _SC_SIGQUEUE_MAX
7602 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7603#endif
7604#ifdef _SC_SIGRT_MAX
7605 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7606#endif
7607#ifdef _SC_SIGRT_MIN
7608 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7609#endif
Fred Draked86ed291999-12-15 15:34:33 +00007610#ifdef _SC_SOFTPOWER
7611 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7612#endif
Fred Drakec9680921999-12-13 16:37:25 +00007613#ifdef _SC_SPLIT_CACHE
7614 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7615#endif
7616#ifdef _SC_SSIZE_MAX
7617 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7618#endif
7619#ifdef _SC_STACK_PROT
7620 {"SC_STACK_PROT", _SC_STACK_PROT},
7621#endif
7622#ifdef _SC_STREAM_MAX
7623 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7624#endif
7625#ifdef _SC_SYNCHRONIZED_IO
7626 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7627#endif
7628#ifdef _SC_THREADS
7629 {"SC_THREADS", _SC_THREADS},
7630#endif
7631#ifdef _SC_THREAD_ATTR_STACKADDR
7632 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7633#endif
7634#ifdef _SC_THREAD_ATTR_STACKSIZE
7635 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7636#endif
7637#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7638 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7639#endif
7640#ifdef _SC_THREAD_KEYS_MAX
7641 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7642#endif
7643#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7644 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7645#endif
7646#ifdef _SC_THREAD_PRIO_INHERIT
7647 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7648#endif
7649#ifdef _SC_THREAD_PRIO_PROTECT
7650 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7651#endif
7652#ifdef _SC_THREAD_PROCESS_SHARED
7653 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7654#endif
7655#ifdef _SC_THREAD_SAFE_FUNCTIONS
7656 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7657#endif
7658#ifdef _SC_THREAD_STACK_MIN
7659 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7660#endif
7661#ifdef _SC_THREAD_THREADS_MAX
7662 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7663#endif
7664#ifdef _SC_TIMERS
7665 {"SC_TIMERS", _SC_TIMERS},
7666#endif
7667#ifdef _SC_TIMER_MAX
7668 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7669#endif
7670#ifdef _SC_TTY_NAME_MAX
7671 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7672#endif
7673#ifdef _SC_TZNAME_MAX
7674 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7675#endif
7676#ifdef _SC_T_IOV_MAX
7677 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7678#endif
7679#ifdef _SC_UCHAR_MAX
7680 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7681#endif
7682#ifdef _SC_UINT_MAX
7683 {"SC_UINT_MAX", _SC_UINT_MAX},
7684#endif
7685#ifdef _SC_UIO_MAXIOV
7686 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7687#endif
7688#ifdef _SC_ULONG_MAX
7689 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7690#endif
7691#ifdef _SC_USHRT_MAX
7692 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7693#endif
7694#ifdef _SC_VERSION
7695 {"SC_VERSION", _SC_VERSION},
7696#endif
7697#ifdef _SC_WORD_BIT
7698 {"SC_WORD_BIT", _SC_WORD_BIT},
7699#endif
7700#ifdef _SC_XBS5_ILP32_OFF32
7701 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7702#endif
7703#ifdef _SC_XBS5_ILP32_OFFBIG
7704 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7705#endif
7706#ifdef _SC_XBS5_LP64_OFF64
7707 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7708#endif
7709#ifdef _SC_XBS5_LPBIG_OFFBIG
7710 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7711#endif
7712#ifdef _SC_XOPEN_CRYPT
7713 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7714#endif
7715#ifdef _SC_XOPEN_ENH_I18N
7716 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7717#endif
7718#ifdef _SC_XOPEN_LEGACY
7719 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7720#endif
7721#ifdef _SC_XOPEN_REALTIME
7722 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7723#endif
7724#ifdef _SC_XOPEN_REALTIME_THREADS
7725 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7726#endif
7727#ifdef _SC_XOPEN_SHM
7728 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7729#endif
7730#ifdef _SC_XOPEN_UNIX
7731 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7732#endif
7733#ifdef _SC_XOPEN_VERSION
7734 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7735#endif
7736#ifdef _SC_XOPEN_XCU_VERSION
7737 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7738#endif
7739#ifdef _SC_XOPEN_XPG2
7740 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7741#endif
7742#ifdef _SC_XOPEN_XPG3
7743 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7744#endif
7745#ifdef _SC_XOPEN_XPG4
7746 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7747#endif
7748};
7749
7750static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007751conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007752{
7753 return conv_confname(arg, valuep, posix_constants_sysconf,
7754 sizeof(posix_constants_sysconf)
7755 / sizeof(struct constdef));
7756}
7757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007758PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007759"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007760Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007761
7762static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007763posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007764{
7765 PyObject *result = NULL;
7766 int name;
7767
7768 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7769 int value;
7770
7771 errno = 0;
7772 value = sysconf(name);
7773 if (value == -1 && errno != 0)
7774 posix_error();
7775 else
7776 result = PyInt_FromLong(value);
7777 }
7778 return result;
7779}
7780#endif
7781
7782
Fred Drakebec628d1999-12-15 18:31:10 +00007783/* This code is used to ensure that the tables of configuration value names
7784 * are in sorted order as required by conv_confname(), and also to build the
7785 * the exported dictionaries that are used to publish information about the
7786 * names available on the host platform.
7787 *
7788 * Sorting the table at runtime ensures that the table is properly ordered
7789 * when used, even for platforms we're not able to test on. It also makes
7790 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007791 */
Fred Drakebec628d1999-12-15 18:31:10 +00007792
7793static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007794cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007795{
7796 const struct constdef *c1 =
7797 (const struct constdef *) v1;
7798 const struct constdef *c2 =
7799 (const struct constdef *) v2;
7800
7801 return strcmp(c1->name, c2->name);
7802}
7803
7804static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007805setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007806 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007807{
Fred Drakebec628d1999-12-15 18:31:10 +00007808 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007809 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007810
7811 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7812 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007813 if (d == NULL)
7814 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007815
Barry Warsaw3155db32000-04-13 15:20:40 +00007816 for (i=0; i < tablesize; ++i) {
7817 PyObject *o = PyInt_FromLong(table[i].value);
7818 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7819 Py_XDECREF(o);
7820 Py_DECREF(d);
7821 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007822 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007823 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007824 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007825 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007826}
7827
Fred Drakebec628d1999-12-15 18:31:10 +00007828/* Return -1 on failure, 0 on success. */
7829static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007830setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007831{
7832#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007833 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007834 sizeof(posix_constants_pathconf)
7835 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007836 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007837 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007838#endif
7839#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007840 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007841 sizeof(posix_constants_confstr)
7842 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007843 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007844 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007845#endif
7846#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007847 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007848 sizeof(posix_constants_sysconf)
7849 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007850 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007851 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007852#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007853 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007854}
Fred Draked86ed291999-12-15 15:34:33 +00007855
7856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007857PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007858"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007859Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007860in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007861
7862static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007863posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007864{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007865 abort();
7866 /*NOTREACHED*/
7867 Py_FatalError("abort() called from Python code didn't abort!");
7868 return NULL;
7869}
Fred Drakebec628d1999-12-15 18:31:10 +00007870
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007871#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007872PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007873"startfile(filepath [, operation]) - Start a file with its associated\n\
7874application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007875\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007876When \"operation\" is not specified or \"open\", this acts like\n\
7877double-clicking the file in Explorer, or giving the file name as an\n\
7878argument to the DOS \"start\" command: the file is opened with whatever\n\
7879application (if any) its extension is associated.\n\
7880When another \"operation\" is given, it specifies what should be done with\n\
7881the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007882\n\
7883startfile returns as soon as the associated application is launched.\n\
7884There is no option to wait for the application to close, and no way\n\
7885to retrieve the application's exit status.\n\
7886\n\
7887The filepath is relative to the current directory. If you want to use\n\
7888an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007889the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007890
7891static PyObject *
7892win32_startfile(PyObject *self, PyObject *args)
7893{
7894 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007895 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007896 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00007897#ifdef Py_WIN_WIDE_FILENAMES
7898 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007899 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00007900 if (!PyArg_ParseTuple(args, "U|s:startfile",
7901 &unipath, &operation)) {
7902 PyErr_Clear();
7903 goto normal;
7904 }
7905
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007906
7907 if (operation) {
7908 woperation = PyUnicode_DecodeASCII(operation,
7909 strlen(operation), NULL);
7910 if (!woperation) {
7911 PyErr_Clear();
7912 operation = NULL;
7913 goto normal;
7914 }
Georg Brandlad89dc82006-04-03 12:26:26 +00007915 }
7916
7917 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007918 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00007919 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00007920 NULL, NULL, SW_SHOWNORMAL);
7921 Py_END_ALLOW_THREADS
7922
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007923 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00007924 if (rc <= (HINSTANCE)32) {
7925 PyObject *errval = win32_error_unicode("startfile",
7926 PyUnicode_AS_UNICODE(unipath));
7927 return errval;
7928 }
7929 Py_INCREF(Py_None);
7930 return Py_None;
7931 }
7932#endif
7933
7934normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007935 if (!PyArg_ParseTuple(args, "et|s:startfile",
7936 Py_FileSystemDefaultEncoding, &filepath,
7937 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007938 return NULL;
7939 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007940 rc = ShellExecute((HWND)0, operation, filepath,
7941 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007942 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007943 if (rc <= (HINSTANCE)32) {
7944 PyObject *errval = win32_error("startfile", filepath);
7945 PyMem_Free(filepath);
7946 return errval;
7947 }
7948 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007949 Py_INCREF(Py_None);
7950 return Py_None;
7951}
7952#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007953
Martin v. Löwis438b5342002-12-27 10:16:42 +00007954#ifdef HAVE_GETLOADAVG
7955PyDoc_STRVAR(posix_getloadavg__doc__,
7956"getloadavg() -> (float, float, float)\n\n\
7957Return the number of processes in the system run queue averaged over\n\
7958the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7959was unobtainable");
7960
7961static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007962posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007963{
7964 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007965 if (getloadavg(loadavg, 3)!=3) {
7966 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7967 return NULL;
7968 } else
7969 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7970}
7971#endif
7972
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007973#ifdef MS_WINDOWS
7974
7975PyDoc_STRVAR(win32_urandom__doc__,
7976"urandom(n) -> str\n\n\
7977Return a string of n random bytes suitable for cryptographic use.");
7978
7979typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7980 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7981 DWORD dwFlags );
7982typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7983 BYTE *pbBuffer );
7984
7985static CRYPTGENRANDOM pCryptGenRandom = NULL;
7986static HCRYPTPROV hCryptProv = 0;
7987
Tim Peters4ad82172004-08-30 17:02:04 +00007988static PyObject*
7989win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007990{
Tim Petersd3115382004-08-30 17:36:46 +00007991 int howMany;
7992 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007993
Tim Peters4ad82172004-08-30 17:02:04 +00007994 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007995 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007996 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007997 if (howMany < 0)
7998 return PyErr_Format(PyExc_ValueError,
7999 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008000
Tim Peters4ad82172004-08-30 17:02:04 +00008001 if (hCryptProv == 0) {
8002 HINSTANCE hAdvAPI32 = NULL;
8003 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008004
Tim Peters4ad82172004-08-30 17:02:04 +00008005 /* Obtain handle to the DLL containing CryptoAPI
8006 This should not fail */
8007 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8008 if(hAdvAPI32 == NULL)
8009 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008010
Tim Peters4ad82172004-08-30 17:02:04 +00008011 /* Obtain pointers to the CryptoAPI functions
8012 This will fail on some early versions of Win95 */
8013 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8014 hAdvAPI32,
8015 "CryptAcquireContextA");
8016 if (pCryptAcquireContext == NULL)
8017 return PyErr_Format(PyExc_NotImplementedError,
8018 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008019
Tim Peters4ad82172004-08-30 17:02:04 +00008020 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8021 hAdvAPI32, "CryptGenRandom");
Georg Brandlb20cb332006-09-06 06:04:06 +00008022 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008023 return PyErr_Format(PyExc_NotImplementedError,
8024 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008025
Tim Peters4ad82172004-08-30 17:02:04 +00008026 /* Acquire context */
8027 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8028 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8029 return win32_error("CryptAcquireContext", NULL);
8030 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008031
Tim Peters4ad82172004-08-30 17:02:04 +00008032 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00008033 result = PyString_FromStringAndSize(NULL, howMany);
8034 if (result != NULL) {
8035 /* Get random data */
8036 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8037 PyString_AS_STRING(result))) {
8038 Py_DECREF(result);
8039 return win32_error("CryptGenRandom", NULL);
8040 }
Tim Peters4ad82172004-08-30 17:02:04 +00008041 }
Tim Petersd3115382004-08-30 17:36:46 +00008042 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008043}
8044#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008045
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008046#ifdef __VMS
8047/* Use openssl random routine */
8048#include <openssl/rand.h>
8049PyDoc_STRVAR(vms_urandom__doc__,
8050"urandom(n) -> str\n\n\
8051Return a string of n random bytes suitable for cryptographic use.");
8052
8053static PyObject*
8054vms_urandom(PyObject *self, PyObject *args)
8055{
8056 int howMany;
8057 PyObject* result;
8058
8059 /* Read arguments */
8060 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8061 return NULL;
8062 if (howMany < 0)
8063 return PyErr_Format(PyExc_ValueError,
8064 "negative argument not allowed");
8065
8066 /* Allocate bytes */
8067 result = PyString_FromStringAndSize(NULL, howMany);
8068 if (result != NULL) {
8069 /* Get random data */
8070 if (RAND_pseudo_bytes((unsigned char*)
8071 PyString_AS_STRING(result),
8072 howMany) < 0) {
8073 Py_DECREF(result);
8074 return PyErr_Format(PyExc_ValueError,
8075 "RAND_pseudo_bytes");
8076 }
8077 }
8078 return result;
8079}
8080#endif
8081
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008082static PyMethodDef posix_methods[] = {
8083 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8084#ifdef HAVE_TTYNAME
8085 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8086#endif
8087 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
8088 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008089#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008090 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008091#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008092#ifdef HAVE_LCHOWN
8093 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8094#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008095#ifdef HAVE_CHROOT
8096 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8097#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008098#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008099 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008100#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008101#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008102 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008103#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008104 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008105#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008106#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008107#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008108 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008109#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008110 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8111 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8112 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008113#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008114 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008115#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008116#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008117 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008118#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008119 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8120 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8121 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008122 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008123#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008124 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008125#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008126#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008127 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008128#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008129 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008130#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008131 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008132#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008133 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8134 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8135 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008136#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008137 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008138#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008139 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008140#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008141 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8142 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008143#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008144#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008145 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8146 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008147#if defined(PYOS_OS2)
8148 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8149 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8150#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008151#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008152#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008153 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008154#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008155#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008156 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008157#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008158#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008159 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008160#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008161#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008162 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008163#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008164#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008165 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008166#endif /* HAVE_GETEGID */
8167#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008168 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008169#endif /* HAVE_GETEUID */
8170#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008171 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008172#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008173#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008174 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008175#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008176 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008177#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008178 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008179#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008180#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008181 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008182#endif /* HAVE_GETPPID */
8183#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008184 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008185#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008186#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008187 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008188#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008189#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008190 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008191#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008192#ifdef HAVE_KILLPG
8193 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8194#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008195#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008196 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008197#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008198#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008199 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008200#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008201 {"popen2", win32_popen2, METH_VARARGS},
8202 {"popen3", win32_popen3, METH_VARARGS},
8203 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008204 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008205#else
8206#if defined(PYOS_OS2) && defined(PYCC_GCC)
8207 {"popen2", os2emx_popen2, METH_VARARGS},
8208 {"popen3", os2emx_popen3, METH_VARARGS},
8209 {"popen4", os2emx_popen4, METH_VARARGS},
8210#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008211#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008212#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008213#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008214 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008215#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008216#ifdef HAVE_SETEUID
8217 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8218#endif /* HAVE_SETEUID */
8219#ifdef HAVE_SETEGID
8220 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8221#endif /* HAVE_SETEGID */
8222#ifdef HAVE_SETREUID
8223 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8224#endif /* HAVE_SETREUID */
8225#ifdef HAVE_SETREGID
8226 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8227#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008228#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008229 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008230#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008231#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008232 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008233#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008234#ifdef HAVE_GETPGID
8235 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8236#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008237#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008238 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008239#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008240#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008241 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008242#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008243#ifdef HAVE_WAIT3
8244 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8245#endif /* HAVE_WAIT3 */
8246#ifdef HAVE_WAIT4
8247 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8248#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008249#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008250 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008251#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008252#ifdef HAVE_GETSID
8253 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8254#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008255#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008256 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008257#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008258#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008259 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008260#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008261#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008262 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008263#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008264#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008265 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008266#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008267 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8268 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8269 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8270 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8271 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8272 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8273 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8274 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8275 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008276 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008277#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008278 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008279#endif
8280#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008281 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008282#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008283#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008284 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8285#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008286#ifdef HAVE_DEVICE_MACROS
8287 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8288 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8289 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8290#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008291#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008292 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008293#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008294#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008295 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008296#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008297#ifdef HAVE_UNSETENV
8298 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8299#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008300#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008301 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008302#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008303#ifdef HAVE_FCHDIR
8304 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8305#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008306#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008307 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008308#endif
8309#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008310 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008311#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008312#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008313#ifdef WCOREDUMP
8314 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8315#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008316#ifdef WIFCONTINUED
8317 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8318#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008319#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008320 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008321#endif /* WIFSTOPPED */
8322#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008323 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008324#endif /* WIFSIGNALED */
8325#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008326 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008327#endif /* WIFEXITED */
8328#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008329 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008330#endif /* WEXITSTATUS */
8331#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008332 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008333#endif /* WTERMSIG */
8334#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008335 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008336#endif /* WSTOPSIG */
8337#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008338#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008339 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008340#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008341#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008342 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008343#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008344#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008345 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008346#endif
8347#ifdef HAVE_TEMPNAM
8348 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8349#endif
8350#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008351 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008352#endif
Fred Drakec9680921999-12-13 16:37:25 +00008353#ifdef HAVE_CONFSTR
8354 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8355#endif
8356#ifdef HAVE_SYSCONF
8357 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8358#endif
8359#ifdef HAVE_FPATHCONF
8360 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8361#endif
8362#ifdef HAVE_PATHCONF
8363 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8364#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008365 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008366#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008367 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8368#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008369#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008370 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008371#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008372 #ifdef MS_WINDOWS
8373 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8374 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008375 #ifdef __VMS
8376 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8377 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008378 {NULL, NULL} /* Sentinel */
8379};
8380
8381
Barry Warsaw4a342091996-12-19 23:50:02 +00008382static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008383ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008384{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008385 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008386}
8387
Guido van Rossumd48f2521997-12-05 22:19:34 +00008388#if defined(PYOS_OS2)
8389/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008390static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008391{
8392 APIRET rc;
8393 ULONG values[QSV_MAX+1];
8394 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008395 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008396
8397 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008398 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008399 Py_END_ALLOW_THREADS
8400
8401 if (rc != NO_ERROR) {
8402 os2_error(rc);
8403 return -1;
8404 }
8405
Fred Drake4d1e64b2002-04-15 19:40:07 +00008406 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8407 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8408 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8409 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8410 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8411 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8412 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008413
8414 switch (values[QSV_VERSION_MINOR]) {
8415 case 0: ver = "2.00"; break;
8416 case 10: ver = "2.10"; break;
8417 case 11: ver = "2.11"; break;
8418 case 30: ver = "3.00"; break;
8419 case 40: ver = "4.00"; break;
8420 case 50: ver = "5.00"; break;
8421 default:
Tim Peters885d4572001-11-28 20:27:42 +00008422 PyOS_snprintf(tmp, sizeof(tmp),
8423 "%d-%d", values[QSV_VERSION_MAJOR],
8424 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008425 ver = &tmp[0];
8426 }
8427
8428 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008429 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008430 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008431
8432 /* Add Indicator of Which Drive was Used to Boot the System */
8433 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8434 tmp[1] = ':';
8435 tmp[2] = '\0';
8436
Fred Drake4d1e64b2002-04-15 19:40:07 +00008437 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008438}
8439#endif
8440
Barry Warsaw4a342091996-12-19 23:50:02 +00008441static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008442all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008443{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008444#ifdef F_OK
8445 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008446#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008447#ifdef R_OK
8448 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008449#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008450#ifdef W_OK
8451 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008452#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008453#ifdef X_OK
8454 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008455#endif
Fred Drakec9680921999-12-13 16:37:25 +00008456#ifdef NGROUPS_MAX
8457 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8458#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008459#ifdef TMP_MAX
8460 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8461#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008462#ifdef WCONTINUED
8463 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8464#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008465#ifdef WNOHANG
8466 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008467#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008468#ifdef WUNTRACED
8469 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8470#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008471#ifdef O_RDONLY
8472 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8473#endif
8474#ifdef O_WRONLY
8475 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8476#endif
8477#ifdef O_RDWR
8478 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8479#endif
8480#ifdef O_NDELAY
8481 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8482#endif
8483#ifdef O_NONBLOCK
8484 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8485#endif
8486#ifdef O_APPEND
8487 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8488#endif
8489#ifdef O_DSYNC
8490 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8491#endif
8492#ifdef O_RSYNC
8493 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8494#endif
8495#ifdef O_SYNC
8496 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8497#endif
8498#ifdef O_NOCTTY
8499 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8500#endif
8501#ifdef O_CREAT
8502 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8503#endif
8504#ifdef O_EXCL
8505 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8506#endif
8507#ifdef O_TRUNC
8508 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8509#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008510#ifdef O_BINARY
8511 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8512#endif
8513#ifdef O_TEXT
8514 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8515#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008516#ifdef O_LARGEFILE
8517 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8518#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008519#ifdef O_SHLOCK
8520 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8521#endif
8522#ifdef O_EXLOCK
8523 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8524#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008525
Tim Peters5aa91602002-01-30 05:46:57 +00008526/* MS Windows */
8527#ifdef O_NOINHERIT
8528 /* Don't inherit in child processes. */
8529 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8530#endif
8531#ifdef _O_SHORT_LIVED
8532 /* Optimize for short life (keep in memory). */
8533 /* MS forgot to define this one with a non-underscore form too. */
8534 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8535#endif
8536#ifdef O_TEMPORARY
8537 /* Automatically delete when last handle is closed. */
8538 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8539#endif
8540#ifdef O_RANDOM
8541 /* Optimize for random access. */
8542 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8543#endif
8544#ifdef O_SEQUENTIAL
8545 /* Optimize for sequential access. */
8546 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8547#endif
8548
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008549/* GNU extensions. */
8550#ifdef O_DIRECT
8551 /* Direct disk access. */
8552 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8553#endif
8554#ifdef O_DIRECTORY
8555 /* Must be a directory. */
8556 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8557#endif
8558#ifdef O_NOFOLLOW
8559 /* Do not follow links. */
8560 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8561#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008562
Barry Warsaw5676bd12003-01-07 20:57:09 +00008563 /* These come from sysexits.h */
8564#ifdef EX_OK
8565 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008566#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008567#ifdef EX_USAGE
8568 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008569#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008570#ifdef EX_DATAERR
8571 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008572#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008573#ifdef EX_NOINPUT
8574 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008575#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008576#ifdef EX_NOUSER
8577 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008578#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008579#ifdef EX_NOHOST
8580 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008581#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008582#ifdef EX_UNAVAILABLE
8583 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008584#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008585#ifdef EX_SOFTWARE
8586 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008587#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008588#ifdef EX_OSERR
8589 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008590#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008591#ifdef EX_OSFILE
8592 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008593#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008594#ifdef EX_CANTCREAT
8595 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008596#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008597#ifdef EX_IOERR
8598 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008599#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008600#ifdef EX_TEMPFAIL
8601 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008602#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008603#ifdef EX_PROTOCOL
8604 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008605#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008606#ifdef EX_NOPERM
8607 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008608#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008609#ifdef EX_CONFIG
8610 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008611#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008612#ifdef EX_NOTFOUND
8613 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008614#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008615
Guido van Rossum246bc171999-02-01 23:54:31 +00008616#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008617#if defined(PYOS_OS2) && defined(PYCC_GCC)
8618 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8619 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8620 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8621 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8622 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8623 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8624 if (ins(d, "P_PM", (long)P_PM)) return -1;
8625 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8626 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8627 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8628 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8629 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8630 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8631 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8632 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8633 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8634 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8635 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8636 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8637 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8638#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008639 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8640 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8641 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8642 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8643 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008644#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008645#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008646
Guido van Rossumd48f2521997-12-05 22:19:34 +00008647#if defined(PYOS_OS2)
8648 if (insertvalues(d)) return -1;
8649#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008650 return 0;
8651}
8652
8653
Tim Peters5aa91602002-01-30 05:46:57 +00008654#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008655#define INITFUNC initnt
8656#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008657
8658#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008659#define INITFUNC initos2
8660#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008661
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008662#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008663#define INITFUNC initposix
8664#define MODNAME "posix"
8665#endif
8666
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008667PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008668INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008669{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008670 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008671
Fred Drake4d1e64b2002-04-15 19:40:07 +00008672 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008673 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008674 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008675 if (m == NULL)
8676 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008677
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008678 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008679 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008680 Py_XINCREF(v);
8681 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008682 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008683 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008684
Fred Drake4d1e64b2002-04-15 19:40:07 +00008685 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008686 return;
8687
Fred Drake4d1e64b2002-04-15 19:40:07 +00008688 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008689 return;
8690
Fred Drake4d1e64b2002-04-15 19:40:07 +00008691 Py_INCREF(PyExc_OSError);
8692 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008693
Guido van Rossumb3d39562000-01-31 18:41:26 +00008694#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008695 if (posix_putenv_garbage == NULL)
8696 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008697#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008698
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008699 if (!initialized) {
8700 stat_result_desc.name = MODNAME ".stat_result";
8701 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8702 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8703 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8704 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8705 structseq_new = StatResultType.tp_new;
8706 StatResultType.tp_new = statresult_new;
8707
8708 statvfs_result_desc.name = MODNAME ".statvfs_result";
8709 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
8710 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008711 Py_INCREF((PyObject*) &StatResultType);
8712 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008713 Py_INCREF((PyObject*) &StatVFSResultType);
8714 PyModule_AddObject(m, "statvfs_result",
8715 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008716 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008717
8718#ifdef __APPLE__
8719 /*
8720 * Step 2 of weak-linking support on Mac OS X.
8721 *
8722 * The code below removes functions that are not available on the
8723 * currently active platform.
8724 *
8725 * This block allow one to use a python binary that was build on
8726 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8727 * OSX 10.4.
8728 */
8729#ifdef HAVE_FSTATVFS
8730 if (fstatvfs == NULL) {
8731 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8732 return;
8733 }
8734 }
8735#endif /* HAVE_FSTATVFS */
8736
8737#ifdef HAVE_STATVFS
8738 if (statvfs == NULL) {
8739 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8740 return;
8741 }
8742 }
8743#endif /* HAVE_STATVFS */
8744
8745# ifdef HAVE_LCHOWN
8746 if (lchown == NULL) {
8747 if (PyObject_DelAttrString(m, "lchown") == -1) {
8748 return;
8749 }
8750 }
8751#endif /* HAVE_LCHOWN */
8752
8753
8754#endif /* __APPLE__ */
8755
Guido van Rossumb6775db1994-08-01 11:34:53 +00008756}
Anthony Baxterac6bd462006-04-13 02:06:09 +00008757
8758#ifdef __cplusplus
8759}
8760#endif
8761
Martin v. Löwis2c7aa632006-10-15 09:44:02 +00008762