blob: 13f943f57e4b8d2f5fb1b80865a9e15b308d4124 [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. Kuchling8d2f2b2d2000-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. Kuchling8d2f2b2d2000-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. Kuchling8d2f2b2d2000-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. Kuchling8d2f2b2d2000-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. Kuchling8d2f2b2d2000-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
Guido van Rossumd48f2521997-12-05 22:19:34 +00005751#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5752static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005753system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005754{
5755 ULONG value = 0;
5756
5757 Py_BEGIN_ALLOW_THREADS
5758 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5759 Py_END_ALLOW_THREADS
5760
5761 return value;
5762}
5763
5764static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005765posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005766{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005767 /* Currently Only Uptime is Provided -- Others Later */
5768 return Py_BuildValue("ddddd",
5769 (double)0 /* t.tms_utime / HZ */,
5770 (double)0 /* t.tms_stime / HZ */,
5771 (double)0 /* t.tms_cutime / HZ */,
5772 (double)0 /* t.tms_cstime / HZ */,
5773 (double)system_uptime() / 1000);
5774}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005775#else /* not OS2 */
Martin v. Löwis710fb8b2008-12-13 15:14:30 +00005776#define NEED_TICKS_PER_SECOND
5777static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005778static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005779posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005780{
5781 struct tms t;
5782 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005783 errno = 0;
5784 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005785 if (c == (clock_t) -1)
5786 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005787 return Py_BuildValue("ddddd",
Martin v. Löwis710fb8b2008-12-13 15:14:30 +00005788 (double)t.tms_utime / ticks_per_second,
5789 (double)t.tms_stime / ticks_per_second,
5790 (double)t.tms_cutime / ticks_per_second,
5791 (double)t.tms_cstime / ticks_per_second,
5792 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005793}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005794#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005795#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005796
5797
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005798#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005799#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005800static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005801posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005802{
5803 FILETIME create, exit, kernel, user;
5804 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005805 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005806 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5807 /* The fields of a FILETIME structure are the hi and lo part
5808 of a 64-bit value expressed in 100 nanosecond units.
5809 1e7 is one second in such units; 1e-7 the inverse.
5810 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5811 */
Barry Warsaw53699e91996-12-10 23:23:01 +00005812 return Py_BuildValue(
5813 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00005814 (double)(kernel.dwHighDateTime*429.4967296 +
5815 kernel.dwLowDateTime*1e-7),
5816 (double)(user.dwHighDateTime*429.4967296 +
5817 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00005818 (double)0,
5819 (double)0,
5820 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005821}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005822#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005823
5824#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005825PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005826"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005827Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005828#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005829
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005830
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005831#ifdef HAVE_GETSID
5832PyDoc_STRVAR(posix_getsid__doc__,
5833"getsid(pid) -> sid\n\n\
5834Call the system call getsid().");
5835
5836static PyObject *
5837posix_getsid(PyObject *self, PyObject *args)
5838{
5839 int pid, sid;
5840 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
5841 return NULL;
5842 sid = getsid(pid);
5843 if (sid < 0)
5844 return posix_error();
5845 return PyInt_FromLong((long)sid);
5846}
5847#endif /* HAVE_GETSID */
5848
5849
Guido van Rossumb6775db1994-08-01 11:34:53 +00005850#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005851PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005852"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005853Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005854
Barry Warsaw53699e91996-12-10 23:23:01 +00005855static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005856posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005857{
Guido van Rossum687dd131993-05-17 08:34:16 +00005858 if (setsid() < 0)
5859 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005860 Py_INCREF(Py_None);
5861 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005862}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005863#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005864
Guido van Rossumb6775db1994-08-01 11:34:53 +00005865#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005866PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005867"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005868Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005869
Barry Warsaw53699e91996-12-10 23:23:01 +00005870static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005871posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005872{
5873 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005874 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00005875 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005876 if (setpgid(pid, pgrp) < 0)
5877 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005878 Py_INCREF(Py_None);
5879 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005880}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005881#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005882
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005883
Guido van Rossumb6775db1994-08-01 11:34:53 +00005884#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005885PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005886"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005887Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005888
Barry Warsaw53699e91996-12-10 23:23:01 +00005889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005890posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005891{
5892 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005893 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005894 return NULL;
5895 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005896 if (pgid < 0)
5897 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005898 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005899}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005900#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005902
Guido van Rossumb6775db1994-08-01 11:34:53 +00005903#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005904PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005905"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005906Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005907
Barry Warsaw53699e91996-12-10 23:23:01 +00005908static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005909posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005910{
5911 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005912 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00005913 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005914 if (tcsetpgrp(fd, pgid) < 0)
5915 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00005916 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00005917 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005918}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005919#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005920
Guido van Rossum687dd131993-05-17 08:34:16 +00005921/* Functions acting on file descriptors */
5922
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005923PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005924"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005925Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005926
Barry Warsaw53699e91996-12-10 23:23:01 +00005927static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005928posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005929{
Mark Hammondef8b6542001-05-13 08:04:26 +00005930 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005931 int flag;
5932 int mode = 0777;
5933 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005934
5935#ifdef MS_WINDOWS
5936 if (unicode_file_names()) {
5937 PyUnicodeObject *po;
5938 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5939 Py_BEGIN_ALLOW_THREADS
5940 /* PyUnicode_AS_UNICODE OK without thread
5941 lock as it is a simple dereference. */
5942 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5943 Py_END_ALLOW_THREADS
5944 if (fd < 0)
5945 return posix_error();
5946 return PyInt_FromLong((long)fd);
5947 }
5948 /* Drop the argument parsing error as narrow strings
5949 are also valid. */
5950 PyErr_Clear();
5951 }
5952#endif
5953
Tim Peters5aa91602002-01-30 05:46:57 +00005954 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00005955 Py_FileSystemDefaultEncoding, &file,
5956 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00005957 return NULL;
5958
Barry Warsaw53699e91996-12-10 23:23:01 +00005959 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005960 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005961 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005962 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00005963 return posix_error_with_allocated_filename(file);
5964 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00005965 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005966}
5967
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005969PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005970"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005971Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005972
Barry Warsaw53699e91996-12-10 23:23:01 +00005973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005974posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005975{
5976 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005977 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005978 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005979 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005980 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00005981 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005982 if (res < 0)
5983 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005984 Py_INCREF(Py_None);
5985 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005986}
5987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005988
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005989PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005990"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005991Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005992
Barry Warsaw53699e91996-12-10 23:23:01 +00005993static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005994posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005995{
5996 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005997 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005998 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005999 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006000 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006001 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006002 if (fd < 0)
6003 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006004 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006005}
6006
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006008PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006009"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006010Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006011
Barry Warsaw53699e91996-12-10 23:23:01 +00006012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006013posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006014{
6015 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006016 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006017 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006018 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006019 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006020 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006021 if (res < 0)
6022 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006023 Py_INCREF(Py_None);
6024 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006025}
6026
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006027
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006028PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006029"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006030Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006031
Barry Warsaw53699e91996-12-10 23:23:01 +00006032static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006033posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006034{
6035 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006036#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006037 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006038#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006039 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006040#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006041 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006042 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006043 return NULL;
6044#ifdef SEEK_SET
6045 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6046 switch (how) {
6047 case 0: how = SEEK_SET; break;
6048 case 1: how = SEEK_CUR; break;
6049 case 2: how = SEEK_END; break;
6050 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006051#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006052
6053#if !defined(HAVE_LARGEFILE_SUPPORT)
6054 pos = PyInt_AsLong(posobj);
6055#else
6056 pos = PyLong_Check(posobj) ?
6057 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6058#endif
6059 if (PyErr_Occurred())
6060 return NULL;
6061
Barry Warsaw53699e91996-12-10 23:23:01 +00006062 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006063#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006064 res = _lseeki64(fd, pos, how);
6065#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006066 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006067#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006068 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006069 if (res < 0)
6070 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006071
6072#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006073 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006074#else
6075 return PyLong_FromLongLong(res);
6076#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006077}
6078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006080PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006081"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006082Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006083
Barry Warsaw53699e91996-12-10 23:23:01 +00006084static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006085posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006086{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006087 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006088 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006089 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006090 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006091 if (size < 0) {
6092 errno = EINVAL;
6093 return posix_error();
6094 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006095 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006096 if (buffer == NULL)
6097 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006098 Py_BEGIN_ALLOW_THREADS
6099 n = read(fd, PyString_AsString(buffer), size);
6100 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006101 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006102 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006103 return posix_error();
6104 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006105 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00006106 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006107 return buffer;
6108}
6109
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006111PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006112"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006113Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006114
Barry Warsaw53699e91996-12-10 23:23:01 +00006115static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006116posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006117{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006118 int fd;
6119 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00006120 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006121
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006122 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006123 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006124 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006125 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006126 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006127 if (size < 0)
6128 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006129 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006130}
6131
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006134"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006136
Barry Warsaw53699e91996-12-10 23:23:01 +00006137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006138posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006139{
6140 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006141 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006142 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006143 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006144 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006145#ifdef __VMS
6146 /* on OpenVMS we must ensure that all bytes are written to the file */
6147 fsync(fd);
6148#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006149 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006150 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006151 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006152 if (res != 0) {
6153#ifdef MS_WINDOWS
6154 return win32_error("fstat", NULL);
6155#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006156 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006157#endif
6158 }
Tim Peters5aa91602002-01-30 05:46:57 +00006159
Martin v. Löwis14694662006-02-03 12:54:16 +00006160 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006161}
6162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006164PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006165"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006166Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006167
Barry Warsaw53699e91996-12-10 23:23:01 +00006168static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006169posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006170{
Guido van Rossum687dd131993-05-17 08:34:16 +00006171 int fd;
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006172 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006173 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006174 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006175 PyObject *f;
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006176 char *mode;
6177 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006178 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006179
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006180 /* Sanitize mode. See fileobject.c */
6181 mode = PyMem_MALLOC(strlen(orgmode)+3);
6182 if (!mode) {
6183 PyErr_NoMemory();
6184 return NULL;
6185 }
6186 strcpy(mode, orgmode);
6187 if (_PyFile_SanitizeMode(mode)) {
6188 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006189 return NULL;
6190 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006191 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006192#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006193 if (mode[0] == 'a') {
6194 /* try to make sure the O_APPEND flag is set */
6195 int flags;
6196 flags = fcntl(fd, F_GETFL);
6197 if (flags != -1)
6198 fcntl(fd, F_SETFL, flags | O_APPEND);
6199 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006200 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006201 /* restore old mode if fdopen failed */
6202 fcntl(fd, F_SETFL, flags);
6203 } else {
6204 fp = fdopen(fd, mode);
6205 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006206#else
6207 fp = fdopen(fd, mode);
6208#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006209 Py_END_ALLOW_THREADS
Neal Norwitz6ca6f142007-05-09 06:45:53 +00006210 PyMem_FREE(mode);
Guido van Rossum687dd131993-05-17 08:34:16 +00006211 if (fp == NULL)
6212 return posix_error();
Kristján Valur Jónssona1392d52007-05-07 19:25:38 +00006213 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006214 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006215 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006216 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006217}
6218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006219PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006220"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006221Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006222connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006223
6224static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006225posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006226{
6227 int fd;
6228 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6229 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006230 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006231}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006232
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006233#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006235"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006237
Barry Warsaw53699e91996-12-10 23:23:01 +00006238static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006239posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006240{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006241#if defined(PYOS_OS2)
6242 HFILE read, write;
6243 APIRET rc;
6244
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006245 Py_BEGIN_ALLOW_THREADS
6246 rc = DosCreatePipe( &read, &write, 4096);
6247 Py_END_ALLOW_THREADS
6248 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006249 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006250
6251 return Py_BuildValue("(ii)", read, write);
6252#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006253#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006254 int fds[2];
6255 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006256 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006257 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006258 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006259 if (res != 0)
6260 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006261 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006262#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006263 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006264 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006265 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006266 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006267 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006268 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006269 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006270 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006271 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6272 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006273 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006274#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006275#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006276}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006277#endif /* HAVE_PIPE */
6278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006279
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006280#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006281PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006282"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006283Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006284
Barry Warsaw53699e91996-12-10 23:23:01 +00006285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006286posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006287{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006288 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006289 int mode = 0666;
6290 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006291 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006292 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006293 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006294 res = mkfifo(filename, mode);
6295 Py_END_ALLOW_THREADS
6296 if (res < 0)
6297 return posix_error();
6298 Py_INCREF(Py_None);
6299 return Py_None;
6300}
6301#endif
6302
6303
Neal Norwitz11690112002-07-30 01:08:28 +00006304#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006305PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006306"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006307Create a filesystem node (file, device special file or named pipe)\n\
6308named filename. mode specifies both the permissions to use and the\n\
6309type of node to be created, being combined (bitwise OR) with one of\n\
6310S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006311device defines the newly created device special file (probably using\n\
6312os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006313
6314
6315static PyObject *
6316posix_mknod(PyObject *self, PyObject *args)
6317{
6318 char *filename;
6319 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006320 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006321 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006322 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006323 return NULL;
6324 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006325 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006326 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006327 if (res < 0)
6328 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006329 Py_INCREF(Py_None);
6330 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006331}
6332#endif
6333
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006334#ifdef HAVE_DEVICE_MACROS
6335PyDoc_STRVAR(posix_major__doc__,
6336"major(device) -> major number\n\
6337Extracts a device major number from a raw device number.");
6338
6339static PyObject *
6340posix_major(PyObject *self, PyObject *args)
6341{
6342 int device;
6343 if (!PyArg_ParseTuple(args, "i:major", &device))
6344 return NULL;
6345 return PyInt_FromLong((long)major(device));
6346}
6347
6348PyDoc_STRVAR(posix_minor__doc__,
6349"minor(device) -> minor number\n\
6350Extracts a device minor number from a raw device number.");
6351
6352static PyObject *
6353posix_minor(PyObject *self, PyObject *args)
6354{
6355 int device;
6356 if (!PyArg_ParseTuple(args, "i:minor", &device))
6357 return NULL;
6358 return PyInt_FromLong((long)minor(device));
6359}
6360
6361PyDoc_STRVAR(posix_makedev__doc__,
6362"makedev(major, minor) -> device number\n\
6363Composes a raw device number from the major and minor device numbers.");
6364
6365static PyObject *
6366posix_makedev(PyObject *self, PyObject *args)
6367{
6368 int major, minor;
6369 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6370 return NULL;
6371 return PyInt_FromLong((long)makedev(major, minor));
6372}
6373#endif /* device macros */
6374
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006375
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006376#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006377PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006378"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006379Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006380
Barry Warsaw53699e91996-12-10 23:23:01 +00006381static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006382posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006383{
6384 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006385 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006386 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006387 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006388
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006389 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006390 return NULL;
6391
6392#if !defined(HAVE_LARGEFILE_SUPPORT)
6393 length = PyInt_AsLong(lenobj);
6394#else
6395 length = PyLong_Check(lenobj) ?
6396 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6397#endif
6398 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006399 return NULL;
6400
Barry Warsaw53699e91996-12-10 23:23:01 +00006401 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006402 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006403 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006404 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006405 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006406 return NULL;
6407 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006408 Py_INCREF(Py_None);
6409 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006410}
6411#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006412
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006413#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006414PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006415"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006416Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006417
Fred Drake762e2061999-08-26 17:23:54 +00006418/* Save putenv() parameters as values here, so we can collect them when they
6419 * get re-set with another call for the same key. */
6420static PyObject *posix_putenv_garbage;
6421
Tim Peters5aa91602002-01-30 05:46:57 +00006422static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006423posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006424{
6425 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006426 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006427 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006428 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006429
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006430 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006431 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006432
6433#if defined(PYOS_OS2)
6434 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6435 APIRET rc;
6436
Guido van Rossumd48f2521997-12-05 22:19:34 +00006437 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6438 if (rc != NO_ERROR)
6439 return os2_error(rc);
6440
6441 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6442 APIRET rc;
6443
Guido van Rossumd48f2521997-12-05 22:19:34 +00006444 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6445 if (rc != NO_ERROR)
6446 return os2_error(rc);
6447 } else {
6448#endif
6449
Fred Drake762e2061999-08-26 17:23:54 +00006450 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006451 len = strlen(s1) + strlen(s2) + 2;
6452 /* len includes space for a trailing \0; the size arg to
6453 PyString_FromStringAndSize does not count that */
6454 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006455 if (newstr == NULL)
6456 return PyErr_NoMemory();
Anthony Baxter64182fe2006-04-11 12:14:09 +00006457 newenv = PyString_AS_STRING(newstr);
6458 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6459 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006460 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006461 posix_error();
6462 return NULL;
6463 }
Fred Drake762e2061999-08-26 17:23:54 +00006464 /* Install the first arg and newstr in posix_putenv_garbage;
6465 * this will cause previous value to be collected. This has to
6466 * happen after the real putenv() call because the old value
6467 * was still accessible until then. */
6468 if (PyDict_SetItem(posix_putenv_garbage,
6469 PyTuple_GET_ITEM(args, 0), newstr)) {
6470 /* really not much we can do; just leak */
6471 PyErr_Clear();
6472 }
6473 else {
6474 Py_DECREF(newstr);
6475 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006476
6477#if defined(PYOS_OS2)
6478 }
6479#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006480 Py_INCREF(Py_None);
6481 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006482}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006483#endif /* putenv */
6484
Guido van Rossumc524d952001-10-19 01:31:59 +00006485#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006486PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006487"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006488Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006489
6490static PyObject *
6491posix_unsetenv(PyObject *self, PyObject *args)
6492{
6493 char *s1;
6494
6495 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6496 return NULL;
6497
6498 unsetenv(s1);
6499
6500 /* Remove the key from posix_putenv_garbage;
6501 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006502 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006503 * old value was still accessible until then.
6504 */
6505 if (PyDict_DelItem(posix_putenv_garbage,
6506 PyTuple_GET_ITEM(args, 0))) {
6507 /* really not much we can do; just leak */
6508 PyErr_Clear();
6509 }
6510
6511 Py_INCREF(Py_None);
6512 return Py_None;
6513}
6514#endif /* unsetenv */
6515
Guido van Rossumb6a47161997-09-15 22:54:34 +00006516#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006517PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006518"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006519Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006520
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006521static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006522posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006523{
6524 int code;
6525 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006526 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006527 return NULL;
6528 message = strerror(code);
6529 if (message == NULL) {
6530 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006531 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006532 return NULL;
6533 }
6534 return PyString_FromString(message);
6535}
6536#endif /* strerror */
6537
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006538
Guido van Rossumc9641791998-08-04 15:26:23 +00006539#ifdef HAVE_SYS_WAIT_H
6540
Fred Drake106c1a02002-04-23 15:58:02 +00006541#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006542PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006543"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006544Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006545
6546static PyObject *
6547posix_WCOREDUMP(PyObject *self, PyObject *args)
6548{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006549 WAIT_TYPE status;
6550 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006551
Neal Norwitzd5a37542006-03-20 06:48:34 +00006552 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006553 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006554
6555 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006556}
6557#endif /* WCOREDUMP */
6558
6559#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006560PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006561"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006562Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006563job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006564
6565static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006566posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006567{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006568 WAIT_TYPE status;
6569 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006570
Neal Norwitzd5a37542006-03-20 06:48:34 +00006571 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006572 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006573
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006574 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006575}
6576#endif /* WIFCONTINUED */
6577
Guido van Rossumc9641791998-08-04 15:26:23 +00006578#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006579PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006580"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006581Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006582
6583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006584posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006585{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006586 WAIT_TYPE status;
6587 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006588
Neal Norwitzd5a37542006-03-20 06:48:34 +00006589 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006590 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006591
Fred Drake106c1a02002-04-23 15:58:02 +00006592 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006593}
6594#endif /* WIFSTOPPED */
6595
6596#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006597PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006598"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006599Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006600
6601static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006602posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006603{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006604 WAIT_TYPE status;
6605 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006606
Neal Norwitzd5a37542006-03-20 06:48:34 +00006607 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006608 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006609
Fred Drake106c1a02002-04-23 15:58:02 +00006610 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006611}
6612#endif /* WIFSIGNALED */
6613
6614#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006615PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006616"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006617Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006618system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006619
6620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006621posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006622{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006623 WAIT_TYPE status;
6624 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006625
Neal Norwitzd5a37542006-03-20 06:48:34 +00006626 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006627 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006628
Fred Drake106c1a02002-04-23 15:58:02 +00006629 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006630}
6631#endif /* WIFEXITED */
6632
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006633#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006634PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006635"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006636Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006637
6638static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006639posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006640{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006641 WAIT_TYPE status;
6642 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006643
Neal Norwitzd5a37542006-03-20 06:48:34 +00006644 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006645 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006646
Guido van Rossumc9641791998-08-04 15:26:23 +00006647 return Py_BuildValue("i", WEXITSTATUS(status));
6648}
6649#endif /* WEXITSTATUS */
6650
6651#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006652PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006653"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006654Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006655value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006656
6657static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006658posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006659{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006660 WAIT_TYPE status;
6661 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006662
Neal Norwitzd5a37542006-03-20 06:48:34 +00006663 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006664 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006665
Guido van Rossumc9641791998-08-04 15:26:23 +00006666 return Py_BuildValue("i", WTERMSIG(status));
6667}
6668#endif /* WTERMSIG */
6669
6670#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006671PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006672"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006673Return the signal that stopped the process that provided\n\
6674the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006675
6676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006677posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006678{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006679 WAIT_TYPE status;
6680 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006681
Neal Norwitzd5a37542006-03-20 06:48:34 +00006682 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006683 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006684
Guido van Rossumc9641791998-08-04 15:26:23 +00006685 return Py_BuildValue("i", WSTOPSIG(status));
6686}
6687#endif /* WSTOPSIG */
6688
6689#endif /* HAVE_SYS_WAIT_H */
6690
6691
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006692#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006693#ifdef _SCO_DS
6694/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6695 needed definitions in sys/statvfs.h */
6696#define _SVID3
6697#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006698#include <sys/statvfs.h>
6699
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006700static PyObject*
6701_pystatvfs_fromstructstatvfs(struct statvfs st) {
6702 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6703 if (v == NULL)
6704 return NULL;
6705
6706#if !defined(HAVE_LARGEFILE_SUPPORT)
6707 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6708 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6709 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6710 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6711 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6712 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6713 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6714 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6715 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6716 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6717#else
6718 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6719 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006720 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006721 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006722 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006723 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006724 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006725 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006726 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006727 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006728 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006729 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006730 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006731 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006732 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6733 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6734#endif
6735
6736 return v;
6737}
6738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006739PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006740"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006741Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006742
6743static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006744posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006745{
6746 int fd, res;
6747 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006748
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006749 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006750 return NULL;
6751 Py_BEGIN_ALLOW_THREADS
6752 res = fstatvfs(fd, &st);
6753 Py_END_ALLOW_THREADS
6754 if (res != 0)
6755 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006756
6757 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006758}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006759#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006760
6761
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006762#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006763#include <sys/statvfs.h>
6764
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006765PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006766"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006767Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006768
6769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006770posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006771{
6772 char *path;
6773 int res;
6774 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006775 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006776 return NULL;
6777 Py_BEGIN_ALLOW_THREADS
6778 res = statvfs(path, &st);
6779 Py_END_ALLOW_THREADS
6780 if (res != 0)
6781 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006782
6783 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006784}
6785#endif /* HAVE_STATVFS */
6786
6787
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006788#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006789PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006790"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006791Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00006792The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006793or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006794
6795static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006796posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006797{
6798 PyObject *result = NULL;
6799 char *dir = NULL;
6800 char *pfx = NULL;
6801 char *name;
6802
6803 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
6804 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00006805
6806 if (PyErr_Warn(PyExc_RuntimeWarning,
6807 "tempnam is a potential security risk to your program") < 0)
6808 return NULL;
6809
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006810#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00006811 name = _tempnam(dir, pfx);
6812#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006813 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00006814#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006815 if (name == NULL)
6816 return PyErr_NoMemory();
6817 result = PyString_FromString(name);
6818 free(name);
6819 return result;
6820}
Guido van Rossumd371ff11999-01-25 16:12:23 +00006821#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006822
6823
6824#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006825PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006826"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006827Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006828
6829static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006830posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006831{
6832 FILE *fp;
6833
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006834 fp = tmpfile();
6835 if (fp == NULL)
6836 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00006837 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006838}
6839#endif
6840
6841
6842#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006843PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006844"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006845Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006846
6847static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006848posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006849{
6850 char buffer[L_tmpnam];
6851 char *name;
6852
Skip Montanaro95618b52001-08-18 18:52:10 +00006853 if (PyErr_Warn(PyExc_RuntimeWarning,
6854 "tmpnam is a potential security risk to your program") < 0)
6855 return NULL;
6856
Greg Wardb48bc172000-03-01 21:51:56 +00006857#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006858 name = tmpnam_r(buffer);
6859#else
6860 name = tmpnam(buffer);
6861#endif
6862 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006863 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00006864#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006865 "unexpected NULL from tmpnam_r"
6866#else
6867 "unexpected NULL from tmpnam"
6868#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00006869 );
6870 PyErr_SetObject(PyExc_OSError, err);
6871 Py_XDECREF(err);
6872 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006873 }
6874 return PyString_FromString(buffer);
6875}
6876#endif
6877
6878
Fred Drakec9680921999-12-13 16:37:25 +00006879/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6880 * It maps strings representing configuration variable names to
6881 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006882 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006883 * rarely-used constants. There are three separate tables that use
6884 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006885 *
6886 * This code is always included, even if none of the interfaces that
6887 * need it are included. The #if hackery needed to avoid it would be
6888 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006889 */
6890struct constdef {
6891 char *name;
6892 long value;
6893};
6894
Fred Drake12c6e2d1999-12-14 21:25:03 +00006895static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006896conv_confname(PyObject *arg, int *valuep, struct constdef *table,
6897 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006898{
6899 if (PyInt_Check(arg)) {
6900 *valuep = PyInt_AS_LONG(arg);
6901 return 1;
6902 }
6903 if (PyString_Check(arg)) {
6904 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00006905 size_t lo = 0;
6906 size_t mid;
6907 size_t hi = tablesize;
6908 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006909 char *confname = PyString_AS_STRING(arg);
6910 while (lo < hi) {
6911 mid = (lo + hi) / 2;
6912 cmp = strcmp(confname, table[mid].name);
6913 if (cmp < 0)
6914 hi = mid;
6915 else if (cmp > 0)
6916 lo = mid + 1;
6917 else {
6918 *valuep = table[mid].value;
6919 return 1;
6920 }
6921 }
6922 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6923 }
6924 else
6925 PyErr_SetString(PyExc_TypeError,
6926 "configuration names must be strings or integers");
6927 return 0;
6928}
6929
6930
6931#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6932static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006933#ifdef _PC_ABI_AIO_XFER_MAX
6934 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
6935#endif
6936#ifdef _PC_ABI_ASYNC_IO
6937 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
6938#endif
Fred Drakec9680921999-12-13 16:37:25 +00006939#ifdef _PC_ASYNC_IO
6940 {"PC_ASYNC_IO", _PC_ASYNC_IO},
6941#endif
6942#ifdef _PC_CHOWN_RESTRICTED
6943 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
6944#endif
6945#ifdef _PC_FILESIZEBITS
6946 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
6947#endif
6948#ifdef _PC_LAST
6949 {"PC_LAST", _PC_LAST},
6950#endif
6951#ifdef _PC_LINK_MAX
6952 {"PC_LINK_MAX", _PC_LINK_MAX},
6953#endif
6954#ifdef _PC_MAX_CANON
6955 {"PC_MAX_CANON", _PC_MAX_CANON},
6956#endif
6957#ifdef _PC_MAX_INPUT
6958 {"PC_MAX_INPUT", _PC_MAX_INPUT},
6959#endif
6960#ifdef _PC_NAME_MAX
6961 {"PC_NAME_MAX", _PC_NAME_MAX},
6962#endif
6963#ifdef _PC_NO_TRUNC
6964 {"PC_NO_TRUNC", _PC_NO_TRUNC},
6965#endif
6966#ifdef _PC_PATH_MAX
6967 {"PC_PATH_MAX", _PC_PATH_MAX},
6968#endif
6969#ifdef _PC_PIPE_BUF
6970 {"PC_PIPE_BUF", _PC_PIPE_BUF},
6971#endif
6972#ifdef _PC_PRIO_IO
6973 {"PC_PRIO_IO", _PC_PRIO_IO},
6974#endif
6975#ifdef _PC_SOCK_MAXBUF
6976 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
6977#endif
6978#ifdef _PC_SYNC_IO
6979 {"PC_SYNC_IO", _PC_SYNC_IO},
6980#endif
6981#ifdef _PC_VDISABLE
6982 {"PC_VDISABLE", _PC_VDISABLE},
6983#endif
6984};
6985
Fred Drakec9680921999-12-13 16:37:25 +00006986static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006987conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006988{
6989 return conv_confname(arg, valuep, posix_constants_pathconf,
6990 sizeof(posix_constants_pathconf)
6991 / sizeof(struct constdef));
6992}
6993#endif
6994
6995#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006996PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006997"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006998Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006999If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007000
7001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007002posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007003{
7004 PyObject *result = NULL;
7005 int name, fd;
7006
Fred Drake12c6e2d1999-12-14 21:25:03 +00007007 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7008 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007009 long limit;
7010
7011 errno = 0;
7012 limit = fpathconf(fd, name);
7013 if (limit == -1 && errno != 0)
7014 posix_error();
7015 else
7016 result = PyInt_FromLong(limit);
7017 }
7018 return result;
7019}
7020#endif
7021
7022
7023#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007024PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007025"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007026Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007027If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007028
7029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007030posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007031{
7032 PyObject *result = NULL;
7033 int name;
7034 char *path;
7035
7036 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7037 conv_path_confname, &name)) {
7038 long limit;
7039
7040 errno = 0;
7041 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007042 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007043 if (errno == EINVAL)
7044 /* could be a path or name problem */
7045 posix_error();
7046 else
7047 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007048 }
Fred Drakec9680921999-12-13 16:37:25 +00007049 else
7050 result = PyInt_FromLong(limit);
7051 }
7052 return result;
7053}
7054#endif
7055
7056#ifdef HAVE_CONFSTR
7057static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007058#ifdef _CS_ARCHITECTURE
7059 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7060#endif
7061#ifdef _CS_HOSTNAME
7062 {"CS_HOSTNAME", _CS_HOSTNAME},
7063#endif
7064#ifdef _CS_HW_PROVIDER
7065 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7066#endif
7067#ifdef _CS_HW_SERIAL
7068 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7069#endif
7070#ifdef _CS_INITTAB_NAME
7071 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7072#endif
Fred Drakec9680921999-12-13 16:37:25 +00007073#ifdef _CS_LFS64_CFLAGS
7074 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7075#endif
7076#ifdef _CS_LFS64_LDFLAGS
7077 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7078#endif
7079#ifdef _CS_LFS64_LIBS
7080 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7081#endif
7082#ifdef _CS_LFS64_LINTFLAGS
7083 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7084#endif
7085#ifdef _CS_LFS_CFLAGS
7086 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7087#endif
7088#ifdef _CS_LFS_LDFLAGS
7089 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7090#endif
7091#ifdef _CS_LFS_LIBS
7092 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7093#endif
7094#ifdef _CS_LFS_LINTFLAGS
7095 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7096#endif
Fred Draked86ed291999-12-15 15:34:33 +00007097#ifdef _CS_MACHINE
7098 {"CS_MACHINE", _CS_MACHINE},
7099#endif
Fred Drakec9680921999-12-13 16:37:25 +00007100#ifdef _CS_PATH
7101 {"CS_PATH", _CS_PATH},
7102#endif
Fred Draked86ed291999-12-15 15:34:33 +00007103#ifdef _CS_RELEASE
7104 {"CS_RELEASE", _CS_RELEASE},
7105#endif
7106#ifdef _CS_SRPC_DOMAIN
7107 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7108#endif
7109#ifdef _CS_SYSNAME
7110 {"CS_SYSNAME", _CS_SYSNAME},
7111#endif
7112#ifdef _CS_VERSION
7113 {"CS_VERSION", _CS_VERSION},
7114#endif
Fred Drakec9680921999-12-13 16:37:25 +00007115#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7116 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7117#endif
7118#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7119 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7120#endif
7121#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7122 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7123#endif
7124#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7125 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7126#endif
7127#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7128 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7129#endif
7130#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7131 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7132#endif
7133#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7134 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7135#endif
7136#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7137 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7138#endif
7139#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7140 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7141#endif
7142#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7143 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7144#endif
7145#ifdef _CS_XBS5_LP64_OFF64_LIBS
7146 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7147#endif
7148#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7149 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7150#endif
7151#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7152 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7153#endif
7154#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7155 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7156#endif
7157#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7158 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7159#endif
7160#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7161 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7162#endif
Fred Draked86ed291999-12-15 15:34:33 +00007163#ifdef _MIPS_CS_AVAIL_PROCESSORS
7164 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7165#endif
7166#ifdef _MIPS_CS_BASE
7167 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7168#endif
7169#ifdef _MIPS_CS_HOSTID
7170 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7171#endif
7172#ifdef _MIPS_CS_HW_NAME
7173 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7174#endif
7175#ifdef _MIPS_CS_NUM_PROCESSORS
7176 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7177#endif
7178#ifdef _MIPS_CS_OSREL_MAJ
7179 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7180#endif
7181#ifdef _MIPS_CS_OSREL_MIN
7182 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7183#endif
7184#ifdef _MIPS_CS_OSREL_PATCH
7185 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7186#endif
7187#ifdef _MIPS_CS_OS_NAME
7188 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7189#endif
7190#ifdef _MIPS_CS_OS_PROVIDER
7191 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7192#endif
7193#ifdef _MIPS_CS_PROCESSORS
7194 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7195#endif
7196#ifdef _MIPS_CS_SERIAL
7197 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7198#endif
7199#ifdef _MIPS_CS_VENDOR
7200 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7201#endif
Fred Drakec9680921999-12-13 16:37:25 +00007202};
7203
7204static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007205conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007206{
7207 return conv_confname(arg, valuep, posix_constants_confstr,
7208 sizeof(posix_constants_confstr)
7209 / sizeof(struct constdef));
7210}
7211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007212PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007213"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007214Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007215
7216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007217posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007218{
7219 PyObject *result = NULL;
7220 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007221 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007222
7223 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007224 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007225
Fred Drakec9680921999-12-13 16:37:25 +00007226 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007227 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007228 if (len == 0) {
7229 if (errno) {
7230 posix_error();
7231 }
7232 else {
7233 result = Py_None;
7234 Py_INCREF(Py_None);
7235 }
Fred Drakec9680921999-12-13 16:37:25 +00007236 }
7237 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007238 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz449b24e2006-04-20 06:56:05 +00007239 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007240 if (result != NULL)
Neal Norwitz449b24e2006-04-20 06:56:05 +00007241 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007242 }
7243 else
Neal Norwitz449b24e2006-04-20 06:56:05 +00007244 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007245 }
7246 }
7247 return result;
7248}
7249#endif
7250
7251
7252#ifdef HAVE_SYSCONF
7253static struct constdef posix_constants_sysconf[] = {
7254#ifdef _SC_2_CHAR_TERM
7255 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7256#endif
7257#ifdef _SC_2_C_BIND
7258 {"SC_2_C_BIND", _SC_2_C_BIND},
7259#endif
7260#ifdef _SC_2_C_DEV
7261 {"SC_2_C_DEV", _SC_2_C_DEV},
7262#endif
7263#ifdef _SC_2_C_VERSION
7264 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7265#endif
7266#ifdef _SC_2_FORT_DEV
7267 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7268#endif
7269#ifdef _SC_2_FORT_RUN
7270 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7271#endif
7272#ifdef _SC_2_LOCALEDEF
7273 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7274#endif
7275#ifdef _SC_2_SW_DEV
7276 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7277#endif
7278#ifdef _SC_2_UPE
7279 {"SC_2_UPE", _SC_2_UPE},
7280#endif
7281#ifdef _SC_2_VERSION
7282 {"SC_2_VERSION", _SC_2_VERSION},
7283#endif
Fred Draked86ed291999-12-15 15:34:33 +00007284#ifdef _SC_ABI_ASYNCHRONOUS_IO
7285 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7286#endif
7287#ifdef _SC_ACL
7288 {"SC_ACL", _SC_ACL},
7289#endif
Fred Drakec9680921999-12-13 16:37:25 +00007290#ifdef _SC_AIO_LISTIO_MAX
7291 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7292#endif
Fred Drakec9680921999-12-13 16:37:25 +00007293#ifdef _SC_AIO_MAX
7294 {"SC_AIO_MAX", _SC_AIO_MAX},
7295#endif
7296#ifdef _SC_AIO_PRIO_DELTA_MAX
7297 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7298#endif
7299#ifdef _SC_ARG_MAX
7300 {"SC_ARG_MAX", _SC_ARG_MAX},
7301#endif
7302#ifdef _SC_ASYNCHRONOUS_IO
7303 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7304#endif
7305#ifdef _SC_ATEXIT_MAX
7306 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7307#endif
Fred Draked86ed291999-12-15 15:34:33 +00007308#ifdef _SC_AUDIT
7309 {"SC_AUDIT", _SC_AUDIT},
7310#endif
Fred Drakec9680921999-12-13 16:37:25 +00007311#ifdef _SC_AVPHYS_PAGES
7312 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7313#endif
7314#ifdef _SC_BC_BASE_MAX
7315 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7316#endif
7317#ifdef _SC_BC_DIM_MAX
7318 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7319#endif
7320#ifdef _SC_BC_SCALE_MAX
7321 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7322#endif
7323#ifdef _SC_BC_STRING_MAX
7324 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7325#endif
Fred Draked86ed291999-12-15 15:34:33 +00007326#ifdef _SC_CAP
7327 {"SC_CAP", _SC_CAP},
7328#endif
Fred Drakec9680921999-12-13 16:37:25 +00007329#ifdef _SC_CHARCLASS_NAME_MAX
7330 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7331#endif
7332#ifdef _SC_CHAR_BIT
7333 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7334#endif
7335#ifdef _SC_CHAR_MAX
7336 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7337#endif
7338#ifdef _SC_CHAR_MIN
7339 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7340#endif
7341#ifdef _SC_CHILD_MAX
7342 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7343#endif
7344#ifdef _SC_CLK_TCK
7345 {"SC_CLK_TCK", _SC_CLK_TCK},
7346#endif
7347#ifdef _SC_COHER_BLKSZ
7348 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7349#endif
7350#ifdef _SC_COLL_WEIGHTS_MAX
7351 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7352#endif
7353#ifdef _SC_DCACHE_ASSOC
7354 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7355#endif
7356#ifdef _SC_DCACHE_BLKSZ
7357 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7358#endif
7359#ifdef _SC_DCACHE_LINESZ
7360 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7361#endif
7362#ifdef _SC_DCACHE_SZ
7363 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7364#endif
7365#ifdef _SC_DCACHE_TBLKSZ
7366 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7367#endif
7368#ifdef _SC_DELAYTIMER_MAX
7369 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7370#endif
7371#ifdef _SC_EQUIV_CLASS_MAX
7372 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7373#endif
7374#ifdef _SC_EXPR_NEST_MAX
7375 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7376#endif
7377#ifdef _SC_FSYNC
7378 {"SC_FSYNC", _SC_FSYNC},
7379#endif
7380#ifdef _SC_GETGR_R_SIZE_MAX
7381 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7382#endif
7383#ifdef _SC_GETPW_R_SIZE_MAX
7384 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7385#endif
7386#ifdef _SC_ICACHE_ASSOC
7387 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7388#endif
7389#ifdef _SC_ICACHE_BLKSZ
7390 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7391#endif
7392#ifdef _SC_ICACHE_LINESZ
7393 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7394#endif
7395#ifdef _SC_ICACHE_SZ
7396 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7397#endif
Fred Draked86ed291999-12-15 15:34:33 +00007398#ifdef _SC_INF
7399 {"SC_INF", _SC_INF},
7400#endif
Fred Drakec9680921999-12-13 16:37:25 +00007401#ifdef _SC_INT_MAX
7402 {"SC_INT_MAX", _SC_INT_MAX},
7403#endif
7404#ifdef _SC_INT_MIN
7405 {"SC_INT_MIN", _SC_INT_MIN},
7406#endif
7407#ifdef _SC_IOV_MAX
7408 {"SC_IOV_MAX", _SC_IOV_MAX},
7409#endif
Fred Draked86ed291999-12-15 15:34:33 +00007410#ifdef _SC_IP_SECOPTS
7411 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7412#endif
Fred Drakec9680921999-12-13 16:37:25 +00007413#ifdef _SC_JOB_CONTROL
7414 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7415#endif
Fred Draked86ed291999-12-15 15:34:33 +00007416#ifdef _SC_KERN_POINTERS
7417 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7418#endif
7419#ifdef _SC_KERN_SIM
7420 {"SC_KERN_SIM", _SC_KERN_SIM},
7421#endif
Fred Drakec9680921999-12-13 16:37:25 +00007422#ifdef _SC_LINE_MAX
7423 {"SC_LINE_MAX", _SC_LINE_MAX},
7424#endif
7425#ifdef _SC_LOGIN_NAME_MAX
7426 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7427#endif
7428#ifdef _SC_LOGNAME_MAX
7429 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7430#endif
7431#ifdef _SC_LONG_BIT
7432 {"SC_LONG_BIT", _SC_LONG_BIT},
7433#endif
Fred Draked86ed291999-12-15 15:34:33 +00007434#ifdef _SC_MAC
7435 {"SC_MAC", _SC_MAC},
7436#endif
Fred Drakec9680921999-12-13 16:37:25 +00007437#ifdef _SC_MAPPED_FILES
7438 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7439#endif
7440#ifdef _SC_MAXPID
7441 {"SC_MAXPID", _SC_MAXPID},
7442#endif
7443#ifdef _SC_MB_LEN_MAX
7444 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7445#endif
7446#ifdef _SC_MEMLOCK
7447 {"SC_MEMLOCK", _SC_MEMLOCK},
7448#endif
7449#ifdef _SC_MEMLOCK_RANGE
7450 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7451#endif
7452#ifdef _SC_MEMORY_PROTECTION
7453 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7454#endif
7455#ifdef _SC_MESSAGE_PASSING
7456 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7457#endif
Fred Draked86ed291999-12-15 15:34:33 +00007458#ifdef _SC_MMAP_FIXED_ALIGNMENT
7459 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7460#endif
Fred Drakec9680921999-12-13 16:37:25 +00007461#ifdef _SC_MQ_OPEN_MAX
7462 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7463#endif
7464#ifdef _SC_MQ_PRIO_MAX
7465 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7466#endif
Fred Draked86ed291999-12-15 15:34:33 +00007467#ifdef _SC_NACLS_MAX
7468 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7469#endif
Fred Drakec9680921999-12-13 16:37:25 +00007470#ifdef _SC_NGROUPS_MAX
7471 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7472#endif
7473#ifdef _SC_NL_ARGMAX
7474 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7475#endif
7476#ifdef _SC_NL_LANGMAX
7477 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7478#endif
7479#ifdef _SC_NL_MSGMAX
7480 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7481#endif
7482#ifdef _SC_NL_NMAX
7483 {"SC_NL_NMAX", _SC_NL_NMAX},
7484#endif
7485#ifdef _SC_NL_SETMAX
7486 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7487#endif
7488#ifdef _SC_NL_TEXTMAX
7489 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7490#endif
7491#ifdef _SC_NPROCESSORS_CONF
7492 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7493#endif
7494#ifdef _SC_NPROCESSORS_ONLN
7495 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7496#endif
Fred Draked86ed291999-12-15 15:34:33 +00007497#ifdef _SC_NPROC_CONF
7498 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7499#endif
7500#ifdef _SC_NPROC_ONLN
7501 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7502#endif
Fred Drakec9680921999-12-13 16:37:25 +00007503#ifdef _SC_NZERO
7504 {"SC_NZERO", _SC_NZERO},
7505#endif
7506#ifdef _SC_OPEN_MAX
7507 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7508#endif
7509#ifdef _SC_PAGESIZE
7510 {"SC_PAGESIZE", _SC_PAGESIZE},
7511#endif
7512#ifdef _SC_PAGE_SIZE
7513 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7514#endif
7515#ifdef _SC_PASS_MAX
7516 {"SC_PASS_MAX", _SC_PASS_MAX},
7517#endif
7518#ifdef _SC_PHYS_PAGES
7519 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7520#endif
7521#ifdef _SC_PII
7522 {"SC_PII", _SC_PII},
7523#endif
7524#ifdef _SC_PII_INTERNET
7525 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7526#endif
7527#ifdef _SC_PII_INTERNET_DGRAM
7528 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7529#endif
7530#ifdef _SC_PII_INTERNET_STREAM
7531 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7532#endif
7533#ifdef _SC_PII_OSI
7534 {"SC_PII_OSI", _SC_PII_OSI},
7535#endif
7536#ifdef _SC_PII_OSI_CLTS
7537 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7538#endif
7539#ifdef _SC_PII_OSI_COTS
7540 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7541#endif
7542#ifdef _SC_PII_OSI_M
7543 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7544#endif
7545#ifdef _SC_PII_SOCKET
7546 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7547#endif
7548#ifdef _SC_PII_XTI
7549 {"SC_PII_XTI", _SC_PII_XTI},
7550#endif
7551#ifdef _SC_POLL
7552 {"SC_POLL", _SC_POLL},
7553#endif
7554#ifdef _SC_PRIORITIZED_IO
7555 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7556#endif
7557#ifdef _SC_PRIORITY_SCHEDULING
7558 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7559#endif
7560#ifdef _SC_REALTIME_SIGNALS
7561 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7562#endif
7563#ifdef _SC_RE_DUP_MAX
7564 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7565#endif
7566#ifdef _SC_RTSIG_MAX
7567 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7568#endif
7569#ifdef _SC_SAVED_IDS
7570 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7571#endif
7572#ifdef _SC_SCHAR_MAX
7573 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7574#endif
7575#ifdef _SC_SCHAR_MIN
7576 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7577#endif
7578#ifdef _SC_SELECT
7579 {"SC_SELECT", _SC_SELECT},
7580#endif
7581#ifdef _SC_SEMAPHORES
7582 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7583#endif
7584#ifdef _SC_SEM_NSEMS_MAX
7585 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7586#endif
7587#ifdef _SC_SEM_VALUE_MAX
7588 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7589#endif
7590#ifdef _SC_SHARED_MEMORY_OBJECTS
7591 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7592#endif
7593#ifdef _SC_SHRT_MAX
7594 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7595#endif
7596#ifdef _SC_SHRT_MIN
7597 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7598#endif
7599#ifdef _SC_SIGQUEUE_MAX
7600 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7601#endif
7602#ifdef _SC_SIGRT_MAX
7603 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7604#endif
7605#ifdef _SC_SIGRT_MIN
7606 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7607#endif
Fred Draked86ed291999-12-15 15:34:33 +00007608#ifdef _SC_SOFTPOWER
7609 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7610#endif
Fred Drakec9680921999-12-13 16:37:25 +00007611#ifdef _SC_SPLIT_CACHE
7612 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7613#endif
7614#ifdef _SC_SSIZE_MAX
7615 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7616#endif
7617#ifdef _SC_STACK_PROT
7618 {"SC_STACK_PROT", _SC_STACK_PROT},
7619#endif
7620#ifdef _SC_STREAM_MAX
7621 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7622#endif
7623#ifdef _SC_SYNCHRONIZED_IO
7624 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7625#endif
7626#ifdef _SC_THREADS
7627 {"SC_THREADS", _SC_THREADS},
7628#endif
7629#ifdef _SC_THREAD_ATTR_STACKADDR
7630 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7631#endif
7632#ifdef _SC_THREAD_ATTR_STACKSIZE
7633 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7634#endif
7635#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7636 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7637#endif
7638#ifdef _SC_THREAD_KEYS_MAX
7639 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7640#endif
7641#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7642 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7643#endif
7644#ifdef _SC_THREAD_PRIO_INHERIT
7645 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7646#endif
7647#ifdef _SC_THREAD_PRIO_PROTECT
7648 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7649#endif
7650#ifdef _SC_THREAD_PROCESS_SHARED
7651 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7652#endif
7653#ifdef _SC_THREAD_SAFE_FUNCTIONS
7654 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7655#endif
7656#ifdef _SC_THREAD_STACK_MIN
7657 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7658#endif
7659#ifdef _SC_THREAD_THREADS_MAX
7660 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7661#endif
7662#ifdef _SC_TIMERS
7663 {"SC_TIMERS", _SC_TIMERS},
7664#endif
7665#ifdef _SC_TIMER_MAX
7666 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7667#endif
7668#ifdef _SC_TTY_NAME_MAX
7669 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7670#endif
7671#ifdef _SC_TZNAME_MAX
7672 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7673#endif
7674#ifdef _SC_T_IOV_MAX
7675 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7676#endif
7677#ifdef _SC_UCHAR_MAX
7678 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7679#endif
7680#ifdef _SC_UINT_MAX
7681 {"SC_UINT_MAX", _SC_UINT_MAX},
7682#endif
7683#ifdef _SC_UIO_MAXIOV
7684 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7685#endif
7686#ifdef _SC_ULONG_MAX
7687 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7688#endif
7689#ifdef _SC_USHRT_MAX
7690 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7691#endif
7692#ifdef _SC_VERSION
7693 {"SC_VERSION", _SC_VERSION},
7694#endif
7695#ifdef _SC_WORD_BIT
7696 {"SC_WORD_BIT", _SC_WORD_BIT},
7697#endif
7698#ifdef _SC_XBS5_ILP32_OFF32
7699 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7700#endif
7701#ifdef _SC_XBS5_ILP32_OFFBIG
7702 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7703#endif
7704#ifdef _SC_XBS5_LP64_OFF64
7705 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7706#endif
7707#ifdef _SC_XBS5_LPBIG_OFFBIG
7708 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7709#endif
7710#ifdef _SC_XOPEN_CRYPT
7711 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7712#endif
7713#ifdef _SC_XOPEN_ENH_I18N
7714 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7715#endif
7716#ifdef _SC_XOPEN_LEGACY
7717 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7718#endif
7719#ifdef _SC_XOPEN_REALTIME
7720 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7721#endif
7722#ifdef _SC_XOPEN_REALTIME_THREADS
7723 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7724#endif
7725#ifdef _SC_XOPEN_SHM
7726 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7727#endif
7728#ifdef _SC_XOPEN_UNIX
7729 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7730#endif
7731#ifdef _SC_XOPEN_VERSION
7732 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7733#endif
7734#ifdef _SC_XOPEN_XCU_VERSION
7735 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7736#endif
7737#ifdef _SC_XOPEN_XPG2
7738 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7739#endif
7740#ifdef _SC_XOPEN_XPG3
7741 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7742#endif
7743#ifdef _SC_XOPEN_XPG4
7744 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7745#endif
7746};
7747
7748static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007749conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007750{
7751 return conv_confname(arg, valuep, posix_constants_sysconf,
7752 sizeof(posix_constants_sysconf)
7753 / sizeof(struct constdef));
7754}
7755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007756PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007757"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007758Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007759
7760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007761posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007762{
7763 PyObject *result = NULL;
7764 int name;
7765
7766 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7767 int value;
7768
7769 errno = 0;
7770 value = sysconf(name);
7771 if (value == -1 && errno != 0)
7772 posix_error();
7773 else
7774 result = PyInt_FromLong(value);
7775 }
7776 return result;
7777}
7778#endif
7779
7780
Fred Drakebec628d1999-12-15 18:31:10 +00007781/* This code is used to ensure that the tables of configuration value names
7782 * are in sorted order as required by conv_confname(), and also to build the
7783 * the exported dictionaries that are used to publish information about the
7784 * names available on the host platform.
7785 *
7786 * Sorting the table at runtime ensures that the table is properly ordered
7787 * when used, even for platforms we're not able to test on. It also makes
7788 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007789 */
Fred Drakebec628d1999-12-15 18:31:10 +00007790
7791static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007792cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007793{
7794 const struct constdef *c1 =
7795 (const struct constdef *) v1;
7796 const struct constdef *c2 =
7797 (const struct constdef *) v2;
7798
7799 return strcmp(c1->name, c2->name);
7800}
7801
7802static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007803setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007804 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007805{
Fred Drakebec628d1999-12-15 18:31:10 +00007806 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007807 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007808
7809 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7810 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007811 if (d == NULL)
7812 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007813
Barry Warsaw3155db32000-04-13 15:20:40 +00007814 for (i=0; i < tablesize; ++i) {
7815 PyObject *o = PyInt_FromLong(table[i].value);
7816 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7817 Py_XDECREF(o);
7818 Py_DECREF(d);
7819 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007820 }
Barry Warsaw3155db32000-04-13 15:20:40 +00007821 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007822 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007823 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007824}
7825
Fred Drakebec628d1999-12-15 18:31:10 +00007826/* Return -1 on failure, 0 on success. */
7827static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007828setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007829{
7830#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007831 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007832 sizeof(posix_constants_pathconf)
7833 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007834 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007835 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007836#endif
7837#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007838 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007839 sizeof(posix_constants_confstr)
7840 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007841 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007842 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007843#endif
7844#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007845 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007846 sizeof(posix_constants_sysconf)
7847 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007848 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00007849 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007850#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007851 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007852}
Fred Draked86ed291999-12-15 15:34:33 +00007853
7854
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007855PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007856"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007857Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007858in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007859
7860static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007861posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007862{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007863 abort();
7864 /*NOTREACHED*/
7865 Py_FatalError("abort() called from Python code didn't abort!");
7866 return NULL;
7867}
Fred Drakebec628d1999-12-15 18:31:10 +00007868
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007869#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007870PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007871"startfile(filepath [, operation]) - Start a file with its associated\n\
7872application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007873\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007874When \"operation\" is not specified or \"open\", this acts like\n\
7875double-clicking the file in Explorer, or giving the file name as an\n\
7876argument to the DOS \"start\" command: the file is opened with whatever\n\
7877application (if any) its extension is associated.\n\
7878When another \"operation\" is given, it specifies what should be done with\n\
7879the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007880\n\
7881startfile returns as soon as the associated application is launched.\n\
7882There is no option to wait for the application to close, and no way\n\
7883to retrieve the application's exit status.\n\
7884\n\
7885The filepath is relative to the current directory. If you want to use\n\
7886an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007887the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007888
7889static PyObject *
7890win32_startfile(PyObject *self, PyObject *args)
7891{
7892 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00007893 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007894 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00007895#ifdef Py_WIN_WIDE_FILENAMES
7896 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007897 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00007898 if (!PyArg_ParseTuple(args, "U|s:startfile",
7899 &unipath, &operation)) {
7900 PyErr_Clear();
7901 goto normal;
7902 }
7903
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007904
7905 if (operation) {
7906 woperation = PyUnicode_DecodeASCII(operation,
7907 strlen(operation), NULL);
7908 if (!woperation) {
7909 PyErr_Clear();
7910 operation = NULL;
7911 goto normal;
7912 }
Georg Brandlad89dc82006-04-03 12:26:26 +00007913 }
7914
7915 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007916 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00007917 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00007918 NULL, NULL, SW_SHOWNORMAL);
7919 Py_END_ALLOW_THREADS
7920
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00007921 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00007922 if (rc <= (HINSTANCE)32) {
7923 PyObject *errval = win32_error_unicode("startfile",
7924 PyUnicode_AS_UNICODE(unipath));
7925 return errval;
7926 }
7927 Py_INCREF(Py_None);
7928 return Py_None;
7929 }
7930#endif
7931
7932normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00007933 if (!PyArg_ParseTuple(args, "et|s:startfile",
7934 Py_FileSystemDefaultEncoding, &filepath,
7935 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00007936 return NULL;
7937 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00007938 rc = ShellExecute((HWND)0, operation, filepath,
7939 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007940 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00007941 if (rc <= (HINSTANCE)32) {
7942 PyObject *errval = win32_error("startfile", filepath);
7943 PyMem_Free(filepath);
7944 return errval;
7945 }
7946 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00007947 Py_INCREF(Py_None);
7948 return Py_None;
7949}
7950#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007951
Martin v. Löwis438b5342002-12-27 10:16:42 +00007952#ifdef HAVE_GETLOADAVG
7953PyDoc_STRVAR(posix_getloadavg__doc__,
7954"getloadavg() -> (float, float, float)\n\n\
7955Return the number of processes in the system run queue averaged over\n\
7956the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7957was unobtainable");
7958
7959static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007960posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007961{
7962 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007963 if (getloadavg(loadavg, 3)!=3) {
7964 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7965 return NULL;
7966 } else
7967 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
7968}
7969#endif
7970
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007971#ifdef MS_WINDOWS
7972
7973PyDoc_STRVAR(win32_urandom__doc__,
7974"urandom(n) -> str\n\n\
7975Return a string of n random bytes suitable for cryptographic use.");
7976
7977typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7978 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7979 DWORD dwFlags );
7980typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7981 BYTE *pbBuffer );
7982
7983static CRYPTGENRANDOM pCryptGenRandom = NULL;
7984static HCRYPTPROV hCryptProv = 0;
7985
Tim Peters4ad82172004-08-30 17:02:04 +00007986static PyObject*
7987win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007988{
Tim Petersd3115382004-08-30 17:36:46 +00007989 int howMany;
7990 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007991
Tim Peters4ad82172004-08-30 17:02:04 +00007992 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00007993 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00007994 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00007995 if (howMany < 0)
7996 return PyErr_Format(PyExc_ValueError,
7997 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007998
Tim Peters4ad82172004-08-30 17:02:04 +00007999 if (hCryptProv == 0) {
8000 HINSTANCE hAdvAPI32 = NULL;
8001 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008002
Tim Peters4ad82172004-08-30 17:02:04 +00008003 /* Obtain handle to the DLL containing CryptoAPI
8004 This should not fail */
8005 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8006 if(hAdvAPI32 == NULL)
8007 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008008
Tim Peters4ad82172004-08-30 17:02:04 +00008009 /* Obtain pointers to the CryptoAPI functions
8010 This will fail on some early versions of Win95 */
8011 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8012 hAdvAPI32,
8013 "CryptAcquireContextA");
8014 if (pCryptAcquireContext == NULL)
8015 return PyErr_Format(PyExc_NotImplementedError,
8016 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008017
Tim Peters4ad82172004-08-30 17:02:04 +00008018 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8019 hAdvAPI32, "CryptGenRandom");
Georg Brandlb20cb332006-09-06 06:04:06 +00008020 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008021 return PyErr_Format(PyExc_NotImplementedError,
8022 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008023
Tim Peters4ad82172004-08-30 17:02:04 +00008024 /* Acquire context */
8025 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8026 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8027 return win32_error("CryptAcquireContext", NULL);
8028 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008029
Tim Peters4ad82172004-08-30 17:02:04 +00008030 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00008031 result = PyString_FromStringAndSize(NULL, howMany);
8032 if (result != NULL) {
8033 /* Get random data */
8034 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8035 PyString_AS_STRING(result))) {
8036 Py_DECREF(result);
8037 return win32_error("CryptGenRandom", NULL);
8038 }
Tim Peters4ad82172004-08-30 17:02:04 +00008039 }
Tim Petersd3115382004-08-30 17:36:46 +00008040 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008041}
8042#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008043
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008044#ifdef __VMS
8045/* Use openssl random routine */
8046#include <openssl/rand.h>
8047PyDoc_STRVAR(vms_urandom__doc__,
8048"urandom(n) -> str\n\n\
8049Return a string of n random bytes suitable for cryptographic use.");
8050
8051static PyObject*
8052vms_urandom(PyObject *self, PyObject *args)
8053{
8054 int howMany;
8055 PyObject* result;
8056
8057 /* Read arguments */
8058 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8059 return NULL;
8060 if (howMany < 0)
8061 return PyErr_Format(PyExc_ValueError,
8062 "negative argument not allowed");
8063
8064 /* Allocate bytes */
8065 result = PyString_FromStringAndSize(NULL, howMany);
8066 if (result != NULL) {
8067 /* Get random data */
8068 if (RAND_pseudo_bytes((unsigned char*)
8069 PyString_AS_STRING(result),
8070 howMany) < 0) {
8071 Py_DECREF(result);
8072 return PyErr_Format(PyExc_ValueError,
8073 "RAND_pseudo_bytes");
8074 }
8075 }
8076 return result;
8077}
8078#endif
8079
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008080static PyMethodDef posix_methods[] = {
8081 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8082#ifdef HAVE_TTYNAME
8083 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8084#endif
8085 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
8086 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008087#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008088 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008089#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008090#ifdef HAVE_LCHOWN
8091 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8092#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008093#ifdef HAVE_CHROOT
8094 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8095#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008096#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008097 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008098#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008099#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008100 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008101#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008102 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008103#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008104#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008105#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008106 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008107#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008108 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8109 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8110 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008111#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008112 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008113#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008114#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008115 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008116#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008117 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8118 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8119 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008120 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008121#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008122 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008123#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008124#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008125 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008126#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008127 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008128#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008129 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008130#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008131 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8132 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8133 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008134#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008135 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008136#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008137 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008138#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008139 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8140 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008141#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008142#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008143 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8144 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008145#if defined(PYOS_OS2)
8146 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8147 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8148#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008149#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008150#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008151 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008152#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008153#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008154 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008155#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008156#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008157 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008158#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008159#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008160 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008161#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008162#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008163 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008164#endif /* HAVE_GETEGID */
8165#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008166 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008167#endif /* HAVE_GETEUID */
8168#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008169 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008170#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008171#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008172 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008173#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008174 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008175#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008176 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008177#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008178#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008179 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008180#endif /* HAVE_GETPPID */
8181#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008182 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008183#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008184#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008185 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008186#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008187#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008188 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008189#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008190#ifdef HAVE_KILLPG
8191 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8192#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008193#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008194 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008195#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008196#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008197 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008198#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008199 {"popen2", win32_popen2, METH_VARARGS},
8200 {"popen3", win32_popen3, METH_VARARGS},
8201 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008202 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008203#else
8204#if defined(PYOS_OS2) && defined(PYCC_GCC)
8205 {"popen2", os2emx_popen2, METH_VARARGS},
8206 {"popen3", os2emx_popen3, METH_VARARGS},
8207 {"popen4", os2emx_popen4, METH_VARARGS},
8208#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008209#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008210#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008211#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008212 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008213#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00008214#ifdef HAVE_SETEUID
8215 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8216#endif /* HAVE_SETEUID */
8217#ifdef HAVE_SETEGID
8218 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8219#endif /* HAVE_SETEGID */
8220#ifdef HAVE_SETREUID
8221 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8222#endif /* HAVE_SETREUID */
8223#ifdef HAVE_SETREGID
8224 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8225#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008226#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008227 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008228#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008229#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008230 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008231#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008232#ifdef HAVE_GETPGID
8233 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8234#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008235#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008236 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008237#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008238#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008239 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008240#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008241#ifdef HAVE_WAIT3
8242 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8243#endif /* HAVE_WAIT3 */
8244#ifdef HAVE_WAIT4
8245 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8246#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008247#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008248 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008249#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008250#ifdef HAVE_GETSID
8251 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8252#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008253#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008254 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008255#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008256#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008257 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008258#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008259#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008260 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008261#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008262#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008263 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008264#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008265 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8266 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8267 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8268 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8269 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8270 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8271 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8272 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8273 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008274 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008275#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008276 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008277#endif
8278#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008279 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008280#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008281#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008282 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8283#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008284#ifdef HAVE_DEVICE_MACROS
8285 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8286 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8287 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8288#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008289#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008290 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008291#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008292#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008293 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008294#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008295#ifdef HAVE_UNSETENV
8296 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8297#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00008298#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008299 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00008300#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00008301#ifdef HAVE_FCHDIR
8302 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8303#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008304#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008305 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008306#endif
8307#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008308 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008309#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008310#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008311#ifdef WCOREDUMP
8312 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8313#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008314#ifdef WIFCONTINUED
8315 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8316#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008317#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008318 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008319#endif /* WIFSTOPPED */
8320#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008321 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008322#endif /* WIFSIGNALED */
8323#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008324 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008325#endif /* WIFEXITED */
8326#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008327 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008328#endif /* WEXITSTATUS */
8329#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008330 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008331#endif /* WTERMSIG */
8332#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008333 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008334#endif /* WSTOPSIG */
8335#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008336#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008337 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008338#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008339#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008340 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008341#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008342#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008343 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008344#endif
8345#ifdef HAVE_TEMPNAM
8346 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8347#endif
8348#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008349 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008350#endif
Fred Drakec9680921999-12-13 16:37:25 +00008351#ifdef HAVE_CONFSTR
8352 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8353#endif
8354#ifdef HAVE_SYSCONF
8355 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8356#endif
8357#ifdef HAVE_FPATHCONF
8358 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8359#endif
8360#ifdef HAVE_PATHCONF
8361 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8362#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008363 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008364#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008365 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8366#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008367#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008368 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008369#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008370 #ifdef MS_WINDOWS
8371 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8372 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008373 #ifdef __VMS
8374 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8375 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008376 {NULL, NULL} /* Sentinel */
8377};
8378
8379
Barry Warsaw4a342091996-12-19 23:50:02 +00008380static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008381ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008382{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008383 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008384}
8385
Guido van Rossumd48f2521997-12-05 22:19:34 +00008386#if defined(PYOS_OS2)
8387/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008388static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008389{
8390 APIRET rc;
8391 ULONG values[QSV_MAX+1];
8392 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008393 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008394
8395 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008396 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008397 Py_END_ALLOW_THREADS
8398
8399 if (rc != NO_ERROR) {
8400 os2_error(rc);
8401 return -1;
8402 }
8403
Fred Drake4d1e64b2002-04-15 19:40:07 +00008404 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8405 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8406 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8407 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8408 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8409 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8410 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008411
8412 switch (values[QSV_VERSION_MINOR]) {
8413 case 0: ver = "2.00"; break;
8414 case 10: ver = "2.10"; break;
8415 case 11: ver = "2.11"; break;
8416 case 30: ver = "3.00"; break;
8417 case 40: ver = "4.00"; break;
8418 case 50: ver = "5.00"; break;
8419 default:
Tim Peters885d4572001-11-28 20:27:42 +00008420 PyOS_snprintf(tmp, sizeof(tmp),
8421 "%d-%d", values[QSV_VERSION_MAJOR],
8422 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008423 ver = &tmp[0];
8424 }
8425
8426 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008427 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008428 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008429
8430 /* Add Indicator of Which Drive was Used to Boot the System */
8431 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8432 tmp[1] = ':';
8433 tmp[2] = '\0';
8434
Fred Drake4d1e64b2002-04-15 19:40:07 +00008435 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008436}
8437#endif
8438
Barry Warsaw4a342091996-12-19 23:50:02 +00008439static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008440all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008441{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008442#ifdef F_OK
8443 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008444#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008445#ifdef R_OK
8446 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008447#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008448#ifdef W_OK
8449 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008450#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008451#ifdef X_OK
8452 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008453#endif
Fred Drakec9680921999-12-13 16:37:25 +00008454#ifdef NGROUPS_MAX
8455 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8456#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008457#ifdef TMP_MAX
8458 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8459#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008460#ifdef WCONTINUED
8461 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8462#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008463#ifdef WNOHANG
8464 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008465#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008466#ifdef WUNTRACED
8467 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8468#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008469#ifdef O_RDONLY
8470 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8471#endif
8472#ifdef O_WRONLY
8473 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8474#endif
8475#ifdef O_RDWR
8476 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8477#endif
8478#ifdef O_NDELAY
8479 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8480#endif
8481#ifdef O_NONBLOCK
8482 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8483#endif
8484#ifdef O_APPEND
8485 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8486#endif
8487#ifdef O_DSYNC
8488 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8489#endif
8490#ifdef O_RSYNC
8491 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8492#endif
8493#ifdef O_SYNC
8494 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8495#endif
8496#ifdef O_NOCTTY
8497 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8498#endif
8499#ifdef O_CREAT
8500 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8501#endif
8502#ifdef O_EXCL
8503 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8504#endif
8505#ifdef O_TRUNC
8506 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8507#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008508#ifdef O_BINARY
8509 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8510#endif
8511#ifdef O_TEXT
8512 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8513#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008514#ifdef O_LARGEFILE
8515 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8516#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008517#ifdef O_SHLOCK
8518 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8519#endif
8520#ifdef O_EXLOCK
8521 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8522#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008523
Tim Peters5aa91602002-01-30 05:46:57 +00008524/* MS Windows */
8525#ifdef O_NOINHERIT
8526 /* Don't inherit in child processes. */
8527 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8528#endif
8529#ifdef _O_SHORT_LIVED
8530 /* Optimize for short life (keep in memory). */
8531 /* MS forgot to define this one with a non-underscore form too. */
8532 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8533#endif
8534#ifdef O_TEMPORARY
8535 /* Automatically delete when last handle is closed. */
8536 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8537#endif
8538#ifdef O_RANDOM
8539 /* Optimize for random access. */
8540 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8541#endif
8542#ifdef O_SEQUENTIAL
8543 /* Optimize for sequential access. */
8544 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8545#endif
8546
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008547/* GNU extensions. */
8548#ifdef O_DIRECT
8549 /* Direct disk access. */
8550 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8551#endif
8552#ifdef O_DIRECTORY
8553 /* Must be a directory. */
8554 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8555#endif
8556#ifdef O_NOFOLLOW
8557 /* Do not follow links. */
8558 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8559#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008560
Barry Warsaw5676bd12003-01-07 20:57:09 +00008561 /* These come from sysexits.h */
8562#ifdef EX_OK
8563 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008564#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008565#ifdef EX_USAGE
8566 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008567#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008568#ifdef EX_DATAERR
8569 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008570#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008571#ifdef EX_NOINPUT
8572 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008573#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008574#ifdef EX_NOUSER
8575 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008576#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008577#ifdef EX_NOHOST
8578 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008579#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008580#ifdef EX_UNAVAILABLE
8581 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008582#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008583#ifdef EX_SOFTWARE
8584 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008585#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008586#ifdef EX_OSERR
8587 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008588#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008589#ifdef EX_OSFILE
8590 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008591#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008592#ifdef EX_CANTCREAT
8593 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008594#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008595#ifdef EX_IOERR
8596 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008597#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008598#ifdef EX_TEMPFAIL
8599 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008600#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008601#ifdef EX_PROTOCOL
8602 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008603#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008604#ifdef EX_NOPERM
8605 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008606#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008607#ifdef EX_CONFIG
8608 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008609#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008610#ifdef EX_NOTFOUND
8611 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008612#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008613
Guido van Rossum246bc171999-02-01 23:54:31 +00008614#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008615#if defined(PYOS_OS2) && defined(PYCC_GCC)
8616 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8617 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8618 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8619 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8620 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8621 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8622 if (ins(d, "P_PM", (long)P_PM)) return -1;
8623 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8624 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8625 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8626 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8627 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8628 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8629 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8630 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8631 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8632 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8633 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8634 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8635 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8636#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008637 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8638 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8639 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8640 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8641 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008642#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008643#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008644
Guido van Rossumd48f2521997-12-05 22:19:34 +00008645#if defined(PYOS_OS2)
8646 if (insertvalues(d)) return -1;
8647#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008648 return 0;
8649}
8650
8651
Tim Peters5aa91602002-01-30 05:46:57 +00008652#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008653#define INITFUNC initnt
8654#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008655
8656#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008657#define INITFUNC initos2
8658#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008659
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008660#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008661#define INITFUNC initposix
8662#define MODNAME "posix"
8663#endif
8664
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008665PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008666INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008667{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008668 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008669
Fred Drake4d1e64b2002-04-15 19:40:07 +00008670 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008671 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008672 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008673 if (m == NULL)
8674 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008675
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008676 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008677 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008678 Py_XINCREF(v);
8679 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008680 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008681 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008682
Fred Drake4d1e64b2002-04-15 19:40:07 +00008683 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008684 return;
8685
Fred Drake4d1e64b2002-04-15 19:40:07 +00008686 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008687 return;
8688
Fred Drake4d1e64b2002-04-15 19:40:07 +00008689 Py_INCREF(PyExc_OSError);
8690 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008691
Guido van Rossumb3d39562000-01-31 18:41:26 +00008692#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008693 if (posix_putenv_garbage == NULL)
8694 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008695#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008696
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008697 if (!initialized) {
8698 stat_result_desc.name = MODNAME ".stat_result";
8699 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8700 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8701 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8702 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8703 structseq_new = StatResultType.tp_new;
8704 StatResultType.tp_new = statresult_new;
8705
8706 statvfs_result_desc.name = MODNAME ".statvfs_result";
8707 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis710fb8b2008-12-13 15:14:30 +00008708#ifdef NEED_TICKS_PER_SECOND
8709# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
8710 ticks_per_second = sysconf(_SC_CLK_TCK);
8711# elif defined(HZ)
8712 ticks_per_second = HZ;
8713# else
8714 ticks_per_second = 60; /* magic fallback value; may be bogus */
8715# endif
8716#endif
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008717 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008718 Py_INCREF((PyObject*) &StatResultType);
8719 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008720 Py_INCREF((PyObject*) &StatVFSResultType);
8721 PyModule_AddObject(m, "statvfs_result",
8722 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008723 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008724
8725#ifdef __APPLE__
8726 /*
8727 * Step 2 of weak-linking support on Mac OS X.
8728 *
8729 * The code below removes functions that are not available on the
8730 * currently active platform.
8731 *
8732 * This block allow one to use a python binary that was build on
8733 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8734 * OSX 10.4.
8735 */
8736#ifdef HAVE_FSTATVFS
8737 if (fstatvfs == NULL) {
8738 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8739 return;
8740 }
8741 }
8742#endif /* HAVE_FSTATVFS */
8743
8744#ifdef HAVE_STATVFS
8745 if (statvfs == NULL) {
8746 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8747 return;
8748 }
8749 }
8750#endif /* HAVE_STATVFS */
8751
8752# ifdef HAVE_LCHOWN
8753 if (lchown == NULL) {
8754 if (PyObject_DelAttrString(m, "lchown") == -1) {
8755 return;
8756 }
8757 }
8758#endif /* HAVE_LCHOWN */
8759
8760
8761#endif /* __APPLE__ */
8762
Guido van Rossumb6775db1994-08-01 11:34:53 +00008763}
Anthony Baxterac6bd462006-04-13 02:06:09 +00008764
8765#ifdef __cplusplus
8766}
8767#endif
8768
Martin v. Löwis2c7aa632006-10-15 09:44:02 +00008769