blob: a1c344465d0711fc5f339ae11b7ae41d4435cfdc [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
Christian Heimes36281872007-11-30 21:11:28 +0000194/*#ifdef HAVE_FCHMOD
195extern int fchmod(int, mode_t);
196#endif*/
197/*#ifdef HAVE_LCHMOD
198extern int lchmod(const char *, mode_t);
199#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int chown(const char *, uid_t, gid_t);
201extern char *getcwd(char *, int);
202extern char *strerror(int);
203extern int link(const char *, const char *);
204extern int rename(const char *, const char *);
205extern int stat(const char *, struct stat *);
206extern int unlink(const char *);
207extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000210#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000215
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218#ifdef HAVE_UTIME_H
219#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000220#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000222#ifdef HAVE_SYS_UTIME_H
223#include <sys/utime.h>
224#define HAVE_UTIME_H /* pretend we do for the rest of this file */
225#endif /* HAVE_SYS_UTIME_H */
226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#ifdef HAVE_SYS_TIMES_H
228#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
231#ifdef HAVE_SYS_PARAM_H
232#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000233#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
235#ifdef HAVE_SYS_UTSNAME_H
236#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000241#define NAMLEN(dirent) strlen((dirent)->d_name)
242#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000243#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#include <direct.h>
245#define NAMLEN(dirent) strlen((dirent)->d_name)
246#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000255#endif
256#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258#endif
259#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#ifdef _MSC_VER
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000262#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <direct.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000264#endif
265#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <io.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000267#endif
268#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <process.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000270#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000271#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000273#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000274#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000275#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossumd48f2521997-12-05 22:19:34 +0000278#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000280#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281
Tim Petersbc2e10e2002-03-03 23:17:02 +0000282#ifndef MAXPATHLEN
Thomas Wouters1ddba602006-04-25 15:29:46 +0000283#if defined(PATH_MAX) && PATH_MAX > 1024
284#define MAXPATHLEN PATH_MAX
285#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000286#define MAXPATHLEN 1024
Thomas Wouters1ddba602006-04-25 15:29:46 +0000287#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#endif /* MAXPATHLEN */
289
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000290#ifdef UNION_WAIT
291/* Emulate some macros on systems that have a union instead of macros */
292
293#ifndef WIFEXITED
294#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
295#endif
296
297#ifndef WEXITSTATUS
298#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
299#endif
300
301#ifndef WTERMSIG
302#define WTERMSIG(u_wait) ((u_wait).w_termsig)
303#endif
304
Neal Norwitzd5a37542006-03-20 06:48:34 +0000305#define WAIT_TYPE union wait
306#define WAIT_STATUS_INT(s) (s.w_status)
307
308#else /* !UNION_WAIT */
309#define WAIT_TYPE int
310#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000311#endif /* UNION_WAIT */
312
Greg Wardb48bc172000-03-01 21:51:56 +0000313/* Don't use the "_r" form if we don't need it (also, won't have a
314 prototype for it, at least on Solaris -- maybe others as well?). */
315#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
316#define USE_CTERMID_R
317#endif
318
319#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
320#define USE_TMPNAM_R
321#endif
322
Fred Drake699f3522000-06-29 21:12:41 +0000323/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000324#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000325#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000326# define STAT win32_stat
327# define FSTAT win32_fstat
328# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000329#else
330# define STAT stat
331# define FSTAT fstat
332# define STRUCT_STAT struct stat
333#endif
334
Tim Peters11b23062003-04-23 02:39:17 +0000335#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000336#include <sys/mkdev.h>
337#else
338#if defined(MAJOR_IN_SYSMACROS)
339#include <sys/sysmacros.h>
340#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000341#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
342#include <sys/mkdev.h>
343#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000344#endif
Fred Drake699f3522000-06-29 21:12:41 +0000345
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000347#ifdef WITH_NEXT_FRAMEWORK
348/* On Darwin/MacOSX a shared library or framework has no access to
349** environ directly, we must obtain it with _NSGetEnviron().
350*/
351#include <crt_externs.h>
352static char **environ;
353#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000355#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356
Barry Warsaw53699e91996-12-10 23:23:01 +0000357static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000358convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359{
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000362 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 if (d == NULL)
364 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000365#ifdef WITH_NEXT_FRAMEWORK
366 if (environ == NULL)
367 environ = *_NSGetEnviron();
368#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 if (environ == NULL)
370 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000371 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000373 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000374 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 char *p = strchr(*e, '=');
376 if (p == NULL)
377 continue;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000378 k = PyString_FromStringAndSize(*e, (int)(p-*e));
Guido van Rossum6a619f41999-08-03 19:41:10 +0000379 if (k == NULL) {
380 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000382 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000383 v = PyString_FromString(p+1);
Guido van Rossum6a619f41999-08-03 19:41:10 +0000384 if (v == NULL) {
385 PyErr_Clear();
386 Py_DECREF(k);
387 continue;
388 }
389 if (PyDict_GetItem(d, k) == NULL) {
390 if (PyDict_SetItem(d, k, v) != 0)
391 PyErr_Clear();
392 }
393 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000394 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000396#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000397 {
398 APIRET rc;
399 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
400
401 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000402 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000403 PyObject *v = PyString_FromString(buffer);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000404 PyDict_SetItemString(d, "BEGINLIBPATH", v);
405 Py_DECREF(v);
406 }
407 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
408 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000409 PyObject *v = PyString_FromString(buffer);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000410 PyDict_SetItemString(d, "ENDLIBPATH", v);
411 Py_DECREF(v);
412 }
413 }
414#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415 return d;
416}
417
418
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419/* Set a POSIX-specific error from errno, and return NULL */
420
Barry Warsawd58d7641998-07-23 16:14:40 +0000421static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000422posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000423{
Barry Warsawca74da41999-02-09 19:31:45 +0000424 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425}
Barry Warsawd58d7641998-07-23 16:14:40 +0000426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000427posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000428{
Barry Warsawca74da41999-02-09 19:31:45 +0000429 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000430}
431
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000432#ifdef Py_WIN_WIDE_FILENAMES
433static PyObject *
434posix_error_with_unicode_filename(Py_UNICODE* name)
435{
436 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
437}
438#endif /* Py_WIN_WIDE_FILENAMES */
439
440
Mark Hammondef8b6542001-05-13 08:04:26 +0000441static PyObject *
442posix_error_with_allocated_filename(char* name)
443{
444 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
445 PyMem_Free(name);
446 return rc;
447}
448
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000449#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000450static PyObject *
451win32_error(char* function, char* filename)
452{
Mark Hammond33a6da92000-08-15 00:46:38 +0000453 /* XXX We should pass the function name along in the future.
454 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000455 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000456 Windows error object, which is non-trivial.
457 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000458 errno = GetLastError();
459 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000460 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000461 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000462 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000463}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000464
465#ifdef Py_WIN_WIDE_FILENAMES
466static PyObject *
467win32_error_unicode(char* function, Py_UNICODE* filename)
468{
469 /* XXX - see win32_error for comments on 'function' */
470 errno = GetLastError();
471 if (filename)
472 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
473 else
474 return PyErr_SetFromWindowsErr(errno);
475}
476
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000477static int
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000478convert_to_unicode(PyObject **param)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000479{
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000480 if (PyUnicode_CheckExact(*param))
481 Py_INCREF(*param);
482 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000483 /* For a Unicode subtype that's not a Unicode object,
484 return a true Unicode object with the same data. */
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000485 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
486 PyUnicode_GET_SIZE(*param));
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000487 else
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +0000488 *param = PyUnicode_FromEncodedObject(*param,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000489 Py_FileSystemDefaultEncoding,
490 "strict");
491 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000492}
493
494#endif /* Py_WIN_WIDE_FILENAMES */
495
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000496#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497
Guido van Rossumd48f2521997-12-05 22:19:34 +0000498#if defined(PYOS_OS2)
499/**********************************************************************
500 * Helper Function to Trim and Format OS/2 Messages
501 **********************************************************************/
502 static void
503os2_formatmsg(char *msgbuf, int msglen, char *reason)
504{
505 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
506
507 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
508 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
509
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000510 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000511 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
512 }
513
514 /* Add Optional Reason Text */
515 if (reason) {
516 strcat(msgbuf, " : ");
517 strcat(msgbuf, reason);
518 }
519}
520
521/**********************************************************************
522 * Decode an OS/2 Operating System Error Code
523 *
524 * A convenience function to lookup an OS/2 error code and return a
525 * text message we can use to raise a Python exception.
526 *
527 * Notes:
528 * The messages for errors returned from the OS/2 kernel reside in
529 * the file OSO001.MSG in the \OS2 directory hierarchy.
530 *
531 **********************************************************************/
532 static char *
533os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
534{
535 APIRET rc;
536 ULONG msglen;
537
538 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
539 Py_BEGIN_ALLOW_THREADS
540 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
541 errorcode, "oso001.msg", &msglen);
542 Py_END_ALLOW_THREADS
543
544 if (rc == NO_ERROR)
545 os2_formatmsg(msgbuf, msglen, reason);
546 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000547 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000548 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000549
550 return msgbuf;
551}
552
553/* Set an OS/2-specific error and return NULL. OS/2 kernel
554 errors are not in a global variable e.g. 'errno' nor are
555 they congruent with posix error numbers. */
556
557static PyObject * os2_error(int code)
558{
559 char text[1024];
560 PyObject *v;
561
562 os2_strerror(text, sizeof(text), code, "");
563
564 v = Py_BuildValue("(is)", code, text);
565 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000566 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000567 Py_DECREF(v);
568 }
569 return NULL; /* Signal to Python that an Exception is Pending */
570}
571
572#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573
574/* POSIX generic methods */
575
Barry Warsaw53699e91996-12-10 23:23:01 +0000576static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000577posix_fildes(PyObject *fdobj, int (*func)(int))
578{
579 int fd;
580 int res;
581 fd = PyObject_AsFileDescriptor(fdobj);
582 if (fd < 0)
583 return NULL;
584 Py_BEGIN_ALLOW_THREADS
585 res = (*func)(fd);
586 Py_END_ALLOW_THREADS
587 if (res < 0)
588 return posix_error();
589 Py_INCREF(Py_None);
590 return Py_None;
591}
Guido van Rossum21142a01999-01-08 21:05:37 +0000592
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000593#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000594static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000595unicode_file_names(void)
596{
597 static int canusewide = -1;
598 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000599 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000600 the Windows NT family. */
601 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
602 }
603 return canusewide;
604}
605#endif
Tim Peters11b23062003-04-23 02:39:17 +0000606
Guido van Rossum21142a01999-01-08 21:05:37 +0000607static PyObject *
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000608posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609{
Mark Hammondef8b6542001-05-13 08:04:26 +0000610 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000611 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000612 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000613 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000615 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000616 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000617 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000618 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000619 return posix_error_with_allocated_filename(path1);
620 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000621 Py_INCREF(Py_None);
622 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623}
624
Barry Warsaw53699e91996-12-10 23:23:01 +0000625static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000626posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000627 char *format,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000628 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629{
Mark Hammondef8b6542001-05-13 08:04:26 +0000630 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000631 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000632 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000633 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000634 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000636 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000637 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000638 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000639 PyMem_Free(path1);
640 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000641 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000642 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000644 Py_INCREF(Py_None);
645 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646}
647
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000648#ifdef Py_WIN_WIDE_FILENAMES
649static PyObject*
650win32_1str(PyObject* args, char* func,
651 char* format, BOOL (__stdcall *funcA)(LPCSTR),
652 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
653{
654 PyObject *uni;
655 char *ansi;
656 BOOL result;
657 if (unicode_file_names()) {
658 if (!PyArg_ParseTuple(args, wformat, &uni))
659 PyErr_Clear();
660 else {
661 Py_BEGIN_ALLOW_THREADS
662 result = funcW(PyUnicode_AsUnicode(uni));
663 Py_END_ALLOW_THREADS
664 if (!result)
665 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
666 Py_INCREF(Py_None);
667 return Py_None;
668 }
669 }
670 if (!PyArg_ParseTuple(args, format, &ansi))
671 return NULL;
672 Py_BEGIN_ALLOW_THREADS
673 result = funcA(ansi);
674 Py_END_ALLOW_THREADS
675 if (!result)
676 return win32_error(func, ansi);
677 Py_INCREF(Py_None);
678 return Py_None;
679
680}
681
682/* This is a reimplementation of the C library's chdir function,
683 but one that produces Win32 errors instead of DOS error codes.
684 chdir is essentially a wrapper around SetCurrentDirectory; however,
685 it also needs to set "magic" environment variables indicating
686 the per-drive current directory, which are of the form =<drive>: */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000687static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000688win32_chdir(LPCSTR path)
689{
690 char new_path[MAX_PATH+1];
691 int result;
692 char env[4] = "=x:";
693
694 if(!SetCurrentDirectoryA(path))
695 return FALSE;
696 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
697 if (!result)
698 return FALSE;
699 /* In the ANSI API, there should not be any paths longer
700 than MAX_PATH. */
701 assert(result <= MAX_PATH+1);
702 if (strncmp(new_path, "\\\\", 2) == 0 ||
703 strncmp(new_path, "//", 2) == 0)
704 /* UNC path, nothing to do. */
705 return TRUE;
706 env[1] = new_path[0];
707 return SetEnvironmentVariableA(env, new_path);
708}
709
710/* The Unicode version differs from the ANSI version
711 since the current directory might exceed MAX_PATH characters */
Hirokazu Yamamoto61376402008-10-16 06:25:25 +0000712static BOOL __stdcall
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000713win32_wchdir(LPCWSTR path)
714{
715 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
716 int result;
717 wchar_t env[4] = L"=x:";
718
719 if(!SetCurrentDirectoryW(path))
720 return FALSE;
721 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
722 if (!result)
723 return FALSE;
724 if (result > MAX_PATH+1) {
Hirokazu Yamamoto10a018c2008-10-09 10:00:30 +0000725 new_path = malloc(result * sizeof(wchar_t));
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000726 if (!new_path) {
727 SetLastError(ERROR_OUTOFMEMORY);
728 return FALSE;
729 }
Hirokazu Yamamoto10a018c2008-10-09 10:00:30 +0000730 result = GetCurrentDirectoryW(result, new_path);
Hirokazu Yamamoto6c75a302008-10-09 10:11:21 +0000731 if (!result) {
732 free(new_path);
Hirokazu Yamamoto10a018c2008-10-09 10:00:30 +0000733 return FALSE;
Hirokazu Yamamoto6c75a302008-10-09 10:11:21 +0000734 }
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000735 }
736 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
737 wcsncmp(new_path, L"//", 2) == 0)
738 /* UNC path, nothing to do. */
739 return TRUE;
740 env[1] = new_path[0];
741 result = SetEnvironmentVariableW(env, new_path);
742 if (new_path != _new_path)
743 free(new_path);
744 return result;
745}
746#endif
747
Martin v. Löwis14694662006-02-03 12:54:16 +0000748#ifdef MS_WINDOWS
749/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
750 - time stamps are restricted to second resolution
751 - file modification times suffer from forth-and-back conversions between
752 UTC and local time
753 Therefore, we implement our own stat, based on the Win32 API directly.
754*/
755#define HAVE_STAT_NSEC 1
756
757struct win32_stat{
758 int st_dev;
759 __int64 st_ino;
760 unsigned short st_mode;
761 int st_nlink;
762 int st_uid;
763 int st_gid;
764 int st_rdev;
765 __int64 st_size;
766 int st_atime;
767 int st_atime_nsec;
768 int st_mtime;
769 int st_mtime_nsec;
770 int st_ctime;
771 int st_ctime_nsec;
772};
773
774static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
775
776static void
777FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
778{
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000779 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
780 /* Cannot simply cast and dereference in_ptr,
781 since it might not be aligned properly */
782 __int64 in;
783 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000784 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
785 /* XXX Win32 supports time stamps past 2038; we currently don't */
786 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
787}
788
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000789static void
790time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
791{
792 /* XXX endianness */
793 __int64 out;
794 out = time_in + secs_between_epochs;
Martin v. Löwisf43893a2006-10-09 20:44:25 +0000795 out = out * 10000000 + nsec_in / 100;
Martin v. Löwis77c176d2006-05-12 17:22:04 +0000796 memcpy(out_ptr, &out, sizeof(out));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000797}
798
Martin v. Löwis14694662006-02-03 12:54:16 +0000799/* Below, we *know* that ugo+r is 0444 */
800#if _S_IREAD != 0400
801#error Unsupported C library
802#endif
803static int
804attributes_to_mode(DWORD attr)
805{
806 int m = 0;
807 if (attr & FILE_ATTRIBUTE_DIRECTORY)
808 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
809 else
810 m |= _S_IFREG;
811 if (attr & FILE_ATTRIBUTE_READONLY)
812 m |= 0444;
813 else
814 m |= 0666;
815 return m;
816}
817
818static int
819attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
820{
821 memset(result, 0, sizeof(*result));
822 result->st_mode = attributes_to_mode(info->dwFileAttributes);
823 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
824 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
825 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
826 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
827
828 return 0;
829}
830
Martin v. Löwis012bc722006-10-15 09:43:39 +0000831/* Emulate GetFileAttributesEx[AW] on Windows 95 */
832static int checked = 0;
833static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
834static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
835static void
836check_gfax()
837{
838 HINSTANCE hKernel32;
839 if (checked)
840 return;
841 checked = 1;
842 hKernel32 = GetModuleHandle("KERNEL32");
843 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
844 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
845}
846
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000847static BOOL
848attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
849{
850 HANDLE hFindFile;
851 WIN32_FIND_DATAA FileData;
852 hFindFile = FindFirstFileA(pszFile, &FileData);
853 if (hFindFile == INVALID_HANDLE_VALUE)
854 return FALSE;
855 FindClose(hFindFile);
856 pfad->dwFileAttributes = FileData.dwFileAttributes;
857 pfad->ftCreationTime = FileData.ftCreationTime;
858 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
859 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
860 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
861 pfad->nFileSizeLow = FileData.nFileSizeLow;
862 return TRUE;
863}
864
865static BOOL
866attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
867{
868 HANDLE hFindFile;
869 WIN32_FIND_DATAW FileData;
870 hFindFile = FindFirstFileW(pszFile, &FileData);
871 if (hFindFile == INVALID_HANDLE_VALUE)
872 return FALSE;
873 FindClose(hFindFile);
874 pfad->dwFileAttributes = FileData.dwFileAttributes;
875 pfad->ftCreationTime = FileData.ftCreationTime;
876 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
877 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
878 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
879 pfad->nFileSizeLow = FileData.nFileSizeLow;
880 return TRUE;
881}
882
Martin v. Löwis012bc722006-10-15 09:43:39 +0000883static BOOL WINAPI
884Py_GetFileAttributesExA(LPCSTR pszFile,
885 GET_FILEEX_INFO_LEVELS level,
886 LPVOID pv)
887{
888 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000889 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
890 /* First try to use the system's implementation, if that is
891 available and either succeeds to gives an error other than
892 that it isn't implemented. */
893 check_gfax();
894 if (gfaxa) {
895 result = gfaxa(pszFile, level, pv);
896 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
897 return result;
898 }
899 /* It's either not present, or not implemented.
900 Emulate using FindFirstFile. */
901 if (level != GetFileExInfoStandard) {
902 SetLastError(ERROR_INVALID_PARAMETER);
903 return FALSE;
904 }
905 /* Use GetFileAttributes to validate that the file name
906 does not contain wildcards (which FindFirstFile would
907 accept). */
908 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
909 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000910 return attributes_from_dir(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000911}
912
913static BOOL WINAPI
914Py_GetFileAttributesExW(LPCWSTR pszFile,
915 GET_FILEEX_INFO_LEVELS level,
916 LPVOID pv)
917{
918 BOOL result;
Martin v. Löwis012bc722006-10-15 09:43:39 +0000919 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
920 /* First try to use the system's implementation, if that is
921 available and either succeeds to gives an error other than
922 that it isn't implemented. */
923 check_gfax();
924 if (gfaxa) {
925 result = gfaxw(pszFile, level, pv);
926 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
927 return result;
928 }
929 /* It's either not present, or not implemented.
930 Emulate using FindFirstFile. */
931 if (level != GetFileExInfoStandard) {
932 SetLastError(ERROR_INVALID_PARAMETER);
933 return FALSE;
934 }
935 /* Use GetFileAttributes to validate that the file name
936 does not contain wildcards (which FindFirstFile would
937 accept). */
938 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
939 return FALSE;
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000940 return attributes_from_dir_w(pszFile, pfad);
Martin v. Löwis012bc722006-10-15 09:43:39 +0000941}
942
Martin v. Löwis14694662006-02-03 12:54:16 +0000943static int
944win32_stat(const char* path, struct win32_stat *result)
945{
946 WIN32_FILE_ATTRIBUTE_DATA info;
947 int code;
948 char *dot;
949 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +0000950 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000951 if (GetLastError() != ERROR_SHARING_VIOLATION) {
952 /* Protocol violation: we explicitly clear errno, instead of
953 setting it to a POSIX error. Callers should use GetLastError. */
954 errno = 0;
955 return -1;
956 } else {
957 /* Could not get attributes on open file. Fall back to
958 reading the directory. */
959 if (!attributes_from_dir(path, &info)) {
960 /* Very strange. This should not fail now */
961 errno = 0;
962 return -1;
963 }
964 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000965 }
966 code = attribute_data_to_stat(&info, result);
967 if (code != 0)
968 return code;
969 /* Set S_IFEXEC if it is an .exe, .bat, ... */
970 dot = strrchr(path, '.');
971 if (dot) {
972 if (stricmp(dot, ".bat") == 0 ||
973 stricmp(dot, ".cmd") == 0 ||
974 stricmp(dot, ".exe") == 0 ||
975 stricmp(dot, ".com") == 0)
976 result->st_mode |= 0111;
977 }
978 return code;
979}
980
981static int
982win32_wstat(const wchar_t* path, struct win32_stat *result)
983{
984 int code;
985 const wchar_t *dot;
986 WIN32_FILE_ATTRIBUTE_DATA info;
987 /* XXX not supported on Win95 and NT 3.x */
Martin v. Löwis012bc722006-10-15 09:43:39 +0000988 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000989 if (GetLastError() != ERROR_SHARING_VIOLATION) {
990 /* Protocol violation: we explicitly clear errno, instead of
991 setting it to a POSIX error. Callers should use GetLastError. */
992 errno = 0;
993 return -1;
994 } else {
995 /* Could not get attributes on open file. Fall back to
996 reading the directory. */
997 if (!attributes_from_dir_w(path, &info)) {
998 /* Very strange. This should not fail now */
999 errno = 0;
1000 return -1;
1001 }
1002 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001003 }
1004 code = attribute_data_to_stat(&info, result);
1005 if (code < 0)
1006 return code;
1007 /* Set IFEXEC if it is an .exe, .bat, ... */
1008 dot = wcsrchr(path, '.');
1009 if (dot) {
1010 if (_wcsicmp(dot, L".bat") == 0 ||
1011 _wcsicmp(dot, L".cmd") == 0 ||
1012 _wcsicmp(dot, L".exe") == 0 ||
1013 _wcsicmp(dot, L".com") == 0)
1014 result->st_mode |= 0111;
1015 }
1016 return code;
1017}
1018
1019static int
1020win32_fstat(int file_number, struct win32_stat *result)
1021{
1022 BY_HANDLE_FILE_INFORMATION info;
1023 HANDLE h;
1024 int type;
1025
1026 h = (HANDLE)_get_osfhandle(file_number);
1027
1028 /* Protocol violation: we explicitly clear errno, instead of
1029 setting it to a POSIX error. Callers should use GetLastError. */
1030 errno = 0;
1031
1032 if (h == INVALID_HANDLE_VALUE) {
1033 /* This is really a C library error (invalid file handle).
1034 We set the Win32 error to the closes one matching. */
1035 SetLastError(ERROR_INVALID_HANDLE);
1036 return -1;
1037 }
1038 memset(result, 0, sizeof(*result));
1039
1040 type = GetFileType(h);
1041 if (type == FILE_TYPE_UNKNOWN) {
1042 DWORD error = GetLastError();
1043 if (error != 0) {
1044 return -1;
1045 }
1046 /* else: valid but unknown file */
1047 }
1048
1049 if (type != FILE_TYPE_DISK) {
1050 if (type == FILE_TYPE_CHAR)
1051 result->st_mode = _S_IFCHR;
1052 else if (type == FILE_TYPE_PIPE)
1053 result->st_mode = _S_IFIFO;
1054 return 0;
1055 }
1056
1057 if (!GetFileInformationByHandle(h, &info)) {
1058 return -1;
1059 }
1060
1061 /* similar to stat() */
1062 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1063 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1064 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1065 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1066 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1067 /* specific to fstat() */
1068 result->st_nlink = info.nNumberOfLinks;
1069 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1070 return 0;
1071}
1072
1073#endif /* MS_WINDOWS */
1074
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076"stat_result: Result from stat or lstat.\n\n\
1077This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001078 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001079or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1080\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001081Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1082or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001083\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001084See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001085
1086static PyStructSequence_Field stat_result_fields[] = {
1087 {"st_mode", "protection bits"},
1088 {"st_ino", "inode"},
1089 {"st_dev", "device"},
1090 {"st_nlink", "number of hard links"},
1091 {"st_uid", "user ID of owner"},
1092 {"st_gid", "group ID of owner"},
1093 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001094 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1095 {NULL, "integer time of last access"},
1096 {NULL, "integer time of last modification"},
1097 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001098 {"st_atime", "time of last access"},
1099 {"st_mtime", "time of last modification"},
1100 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001101#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001102 {"st_blksize", "blocksize for filesystem I/O"},
1103#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001104#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001105 {"st_blocks", "number of blocks allocated"},
1106#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001107#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001108 {"st_rdev", "device type (if inode device)"},
1109#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001110#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1111 {"st_flags", "user defined flags for file"},
1112#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001113#ifdef HAVE_STRUCT_STAT_ST_GEN
1114 {"st_gen", "generation number"},
1115#endif
1116#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1117 {"st_birthtime", "time of creation"},
1118#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001119 {0}
1120};
1121
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001122#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001123#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001124#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001125#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#endif
1127
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001128#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001129#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1130#else
1131#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1132#endif
1133
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001134#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001135#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1136#else
1137#define ST_RDEV_IDX ST_BLOCKS_IDX
1138#endif
1139
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001140#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1141#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1142#else
1143#define ST_FLAGS_IDX ST_RDEV_IDX
1144#endif
1145
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001146#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001147#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001148#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001149#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001150#endif
1151
1152#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1153#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1154#else
1155#define ST_BIRTHTIME_IDX ST_GEN_IDX
1156#endif
1157
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001158static PyStructSequence_Desc stat_result_desc = {
1159 "stat_result", /* name */
1160 stat_result__doc__, /* doc */
1161 stat_result_fields,
1162 10
1163};
1164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001166"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1167This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001168 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001169or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001170\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001171See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001172
1173static PyStructSequence_Field statvfs_result_fields[] = {
1174 {"f_bsize", },
1175 {"f_frsize", },
1176 {"f_blocks", },
1177 {"f_bfree", },
1178 {"f_bavail", },
1179 {"f_files", },
1180 {"f_ffree", },
1181 {"f_favail", },
1182 {"f_flag", },
1183 {"f_namemax",},
1184 {0}
1185};
1186
1187static PyStructSequence_Desc statvfs_result_desc = {
1188 "statvfs_result", /* name */
1189 statvfs_result__doc__, /* doc */
1190 statvfs_result_fields,
1191 10
1192};
1193
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00001194static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001195static PyTypeObject StatResultType;
1196static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001197static newfunc structseq_new;
1198
1199static PyObject *
1200statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1201{
1202 PyStructSequence *result;
1203 int i;
1204
1205 result = (PyStructSequence*)structseq_new(type, args, kwds);
1206 if (!result)
1207 return NULL;
1208 /* If we have been initialized from a tuple,
1209 st_?time might be set to None. Initialize it
1210 from the int slots. */
1211 for (i = 7; i <= 9; i++) {
1212 if (result->ob_item[i+3] == Py_None) {
1213 Py_DECREF(Py_None);
1214 Py_INCREF(result->ob_item[i]);
1215 result->ob_item[i+3] = result->ob_item[i];
1216 }
1217 }
1218 return (PyObject*)result;
1219}
1220
1221
1222
1223/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001224static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001225
1226PyDoc_STRVAR(stat_float_times__doc__,
1227"stat_float_times([newval]) -> oldval\n\n\
1228Determine whether os.[lf]stat represents time stamps as float objects.\n\
1229If newval is True, future calls to stat() return floats, if it is False,\n\
1230future calls return ints. \n\
1231If newval is omitted, return the current setting.\n");
1232
1233static PyObject*
1234stat_float_times(PyObject* self, PyObject *args)
1235{
1236 int newval = -1;
1237 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1238 return NULL;
1239 if (newval == -1)
1240 /* Return old value */
1241 return PyBool_FromLong(_stat_float_times);
1242 _stat_float_times = newval;
1243 Py_INCREF(Py_None);
1244 return Py_None;
1245}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001246
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001247static void
1248fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1249{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001250 PyObject *fval,*ival;
1251#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001252 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001253#else
1254 ival = PyInt_FromLong((long)sec);
1255#endif
Neal Norwitze0a81af2006-08-12 01:50:38 +00001256 if (!ival)
1257 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001258 if (_stat_float_times) {
1259 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1260 } else {
1261 fval = ival;
1262 Py_INCREF(fval);
1263 }
1264 PyStructSequence_SET_ITEM(v, index, ival);
1265 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001266}
1267
Tim Peters5aa91602002-01-30 05:46:57 +00001268/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001269 (used by posix_stat() and posix_fstat()) */
1270static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001271_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001272{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001273 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001274 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001275 if (v == NULL)
1276 return NULL;
1277
Martin v. Löwis14694662006-02-03 12:54:16 +00001278 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001279#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001280 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001281 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001282#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001283 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001284#endif
1285#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001286 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001287 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001288#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001289 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001290#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001291 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1292 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1293 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001294#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001295 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001296 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001297#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001298 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001299#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001300
Martin v. Löwis14694662006-02-03 12:54:16 +00001301#if defined(HAVE_STAT_TV_NSEC)
1302 ansec = st->st_atim.tv_nsec;
1303 mnsec = st->st_mtim.tv_nsec;
1304 cnsec = st->st_ctim.tv_nsec;
1305#elif defined(HAVE_STAT_TV_NSEC2)
1306 ansec = st->st_atimespec.tv_nsec;
1307 mnsec = st->st_mtimespec.tv_nsec;
1308 cnsec = st->st_ctimespec.tv_nsec;
1309#elif defined(HAVE_STAT_NSEC)
1310 ansec = st->st_atime_nsec;
1311 mnsec = st->st_mtime_nsec;
1312 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001313#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001314 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001315#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001316 fill_time(v, 7, st->st_atime, ansec);
1317 fill_time(v, 8, st->st_mtime, mnsec);
1318 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001319
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001320#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001321 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001322 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001323#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001324#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001325 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001326 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001327#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001328#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001329 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001330 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001331#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001332#ifdef HAVE_STRUCT_STAT_ST_GEN
1333 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001334 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001335#endif
1336#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1337 {
1338 PyObject *val;
1339 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001340 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001341#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001342 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001343#else
1344 bnsec = 0;
1345#endif
1346 if (_stat_float_times) {
1347 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1348 } else {
1349 val = PyInt_FromLong((long)bsec);
1350 }
1351 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1352 val);
1353 }
1354#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001355#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1356 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001357 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001358#endif
Fred Drake699f3522000-06-29 21:12:41 +00001359
1360 if (PyErr_Occurred()) {
1361 Py_DECREF(v);
1362 return NULL;
1363 }
1364
1365 return v;
1366}
1367
Martin v. Löwisd8948722004-06-02 09:57:56 +00001368#ifdef MS_WINDOWS
1369
1370/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1371 where / can be used in place of \ and the trailing slash is optional.
1372 Both SERVER and SHARE must have at least one character.
1373*/
1374
1375#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1376#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001377#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001378#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Kristján Valur Jónssondbeaa692006-06-09 16:28:01 +00001379#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001380
Tim Peters4ad82172004-08-30 17:02:04 +00001381static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001382IsUNCRootA(char *path, int pathlen)
1383{
1384 #define ISSLASH ISSLASHA
1385
1386 int i, share;
1387
1388 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1389 /* minimum UNCRoot is \\x\y */
1390 return FALSE;
1391 for (i = 2; i < pathlen ; i++)
1392 if (ISSLASH(path[i])) break;
1393 if (i == 2 || i == pathlen)
1394 /* do not allow \\\SHARE or \\SERVER */
1395 return FALSE;
1396 share = i+1;
1397 for (i = share; i < pathlen; i++)
1398 if (ISSLASH(path[i])) break;
1399 return (i != share && (i == pathlen || i == pathlen-1));
1400
1401 #undef ISSLASH
1402}
1403
1404#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001405static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001406IsUNCRootW(Py_UNICODE *path, int pathlen)
1407{
1408 #define ISSLASH ISSLASHW
1409
1410 int i, share;
1411
1412 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1413 /* minimum UNCRoot is \\x\y */
1414 return FALSE;
1415 for (i = 2; i < pathlen ; i++)
1416 if (ISSLASH(path[i])) break;
1417 if (i == 2 || i == pathlen)
1418 /* do not allow \\\SHARE or \\SERVER */
1419 return FALSE;
1420 share = i+1;
1421 for (i = share; i < pathlen; i++)
1422 if (ISSLASH(path[i])) break;
1423 return (i != share && (i == pathlen || i == pathlen-1));
1424
1425 #undef ISSLASH
1426}
1427#endif /* Py_WIN_WIDE_FILENAMES */
1428#endif /* MS_WINDOWS */
1429
Barry Warsaw53699e91996-12-10 23:23:01 +00001430static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001431posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001432 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001433#ifdef __VMS
1434 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1435#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001436 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001437#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001438 char *wformat,
1439 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001440{
Fred Drake699f3522000-06-29 21:12:41 +00001441 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001442 char *path = NULL; /* pass this to stat; do not free() it */
1443 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001444 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001445 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001446
1447#ifdef Py_WIN_WIDE_FILENAMES
1448 /* If on wide-character-capable OS see if argument
1449 is Unicode and if so use wide API. */
1450 if (unicode_file_names()) {
1451 PyUnicodeObject *po;
1452 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001453 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1454
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001455 Py_BEGIN_ALLOW_THREADS
1456 /* PyUnicode_AS_UNICODE result OK without
1457 thread lock as it is a simple dereference. */
1458 res = wstatfunc(wpath, &st);
1459 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001460
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001461 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001462 return win32_error_unicode("stat", wpath);
1463 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001464 }
1465 /* Drop the argument parsing error as narrow strings
1466 are also valid. */
1467 PyErr_Clear();
1468 }
1469#endif
1470
Tim Peters5aa91602002-01-30 05:46:57 +00001471 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001472 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001473 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001474 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001475
Barry Warsaw53699e91996-12-10 23:23:01 +00001476 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001477 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001478 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001479
1480 if (res != 0) {
1481#ifdef MS_WINDOWS
1482 result = win32_error("stat", pathfree);
1483#else
1484 result = posix_error_with_filename(pathfree);
1485#endif
1486 }
1487 else
1488 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001489
Tim Peters500bd032001-12-19 19:05:01 +00001490 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001491 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492}
1493
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494/* POSIX methods */
1495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001496PyDoc_STRVAR(posix_access__doc__,
Georg Brandl7a284472007-01-27 19:38:50 +00001497"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001498Use the real uid/gid to test for access to a path. Note that most\n\
1499operations will use the effective uid/gid, therefore this routine can\n\
1500be used in a suid/sgid environment to test if the invoking user has the\n\
1501specified access to the path. The mode argument can be F_OK to test\n\
1502existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001503
1504static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001505posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001506{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001507 char *path;
1508 int mode;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001509
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001510#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001511 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001512 if (unicode_file_names()) {
1513 PyUnicodeObject *po;
1514 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1515 Py_BEGIN_ALLOW_THREADS
1516 /* PyUnicode_AS_UNICODE OK without thread lock as
1517 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001518 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001519 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001520 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001521 }
1522 /* Drop the argument parsing error as narrow strings
1523 are also valid. */
1524 PyErr_Clear();
1525 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001526 if (!PyArg_ParseTuple(args, "eti:access",
1527 Py_FileSystemDefaultEncoding, &path, &mode))
1528 return 0;
1529 Py_BEGIN_ALLOW_THREADS
1530 attr = GetFileAttributesA(path);
1531 Py_END_ALLOW_THREADS
1532 PyMem_Free(path);
1533finish:
1534 if (attr == 0xFFFFFFFF)
1535 /* File does not exist, or cannot read attributes */
1536 return PyBool_FromLong(0);
1537 /* Access is possible if either write access wasn't requested, or
Martin v. Löwis7b3cc062007-12-03 23:09:04 +00001538 the file isn't read-only, or if it's a directory, as there are
1539 no read-only directories on Windows. */
1540 return PyBool_FromLong(!(mode & 2)
1541 || !(attr & FILE_ATTRIBUTE_READONLY)
1542 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001543#else
1544 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001545 if (!PyArg_ParseTuple(args, "eti:access",
1546 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001547 return NULL;
1548 Py_BEGIN_ALLOW_THREADS
1549 res = access(path, mode);
1550 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001551 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001552 return PyBool_FromLong(res == 0);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001553#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001554}
1555
Guido van Rossumd371ff11999-01-25 16:12:23 +00001556#ifndef F_OK
1557#define F_OK 0
1558#endif
1559#ifndef R_OK
1560#define R_OK 4
1561#endif
1562#ifndef W_OK
1563#define W_OK 2
1564#endif
1565#ifndef X_OK
1566#define X_OK 1
1567#endif
1568
1569#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001571"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001573
1574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001575posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001576{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001577 int id;
1578 char *ret;
1579
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001580 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001581 return NULL;
1582
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001583#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001584 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001585 if (id == 0) {
1586 ret = ttyname();
1587 }
1588 else {
1589 ret = NULL;
1590 }
1591#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001592 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001593#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001594 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001595 return posix_error();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001596 return PyString_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001597}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001598#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001599
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001601PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001602"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001603Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001604
1605static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001606posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607{
1608 char *ret;
1609 char buffer[L_ctermid];
1610
Greg Wardb48bc172000-03-01 21:51:56 +00001611#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001612 ret = ctermid_r(buffer);
1613#else
1614 ret = ctermid(buffer);
1615#endif
1616 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001617 return posix_error();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001618 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001619}
1620#endif
1621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001623"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001625
Barry Warsaw53699e91996-12-10 23:23:01 +00001626static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001627posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001628{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001629#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001630 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001631#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001632 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001633#elif defined(__VMS)
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001634 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001635#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001636 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001637#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001638}
1639
Fred Drake4d1e64b2002-04-15 19:40:07 +00001640#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001641PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001642"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001643Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001644opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001645
1646static PyObject *
1647posix_fchdir(PyObject *self, PyObject *fdobj)
1648{
1649 return posix_fildes(fdobj, fchdir);
1650}
1651#endif /* HAVE_FCHDIR */
1652
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001655"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001657
Barry Warsaw53699e91996-12-10 23:23:01 +00001658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001659posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001660{
Mark Hammondef8b6542001-05-13 08:04:26 +00001661 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001662 int i;
1663 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001664#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001665 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001666 if (unicode_file_names()) {
1667 PyUnicodeObject *po;
1668 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1669 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001670 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1671 if (attr != 0xFFFFFFFF) {
1672 if (i & _S_IWRITE)
1673 attr &= ~FILE_ATTRIBUTE_READONLY;
1674 else
1675 attr |= FILE_ATTRIBUTE_READONLY;
1676 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1677 }
1678 else
1679 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001680 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001681 if (!res)
1682 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001683 PyUnicode_AS_UNICODE(po));
1684 Py_INCREF(Py_None);
1685 return Py_None;
1686 }
1687 /* Drop the argument parsing error as narrow strings
1688 are also valid. */
1689 PyErr_Clear();
1690 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001691 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1692 &path, &i))
1693 return NULL;
1694 Py_BEGIN_ALLOW_THREADS
1695 attr = GetFileAttributesA(path);
1696 if (attr != 0xFFFFFFFF) {
1697 if (i & _S_IWRITE)
1698 attr &= ~FILE_ATTRIBUTE_READONLY;
1699 else
1700 attr |= FILE_ATTRIBUTE_READONLY;
1701 res = SetFileAttributesA(path, attr);
1702 }
1703 else
1704 res = 0;
1705 Py_END_ALLOW_THREADS
1706 if (!res) {
1707 win32_error("chmod", path);
1708 PyMem_Free(path);
1709 return NULL;
1710 }
Martin v. Löwis9f485bc2006-05-08 05:25:56 +00001711 PyMem_Free(path);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001712 Py_INCREF(Py_None);
1713 return Py_None;
1714#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001715 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001716 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001717 return NULL;
1718 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001719 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001720 Py_END_ALLOW_THREADS
1721 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001722 return posix_error_with_allocated_filename(path);
1723 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001724 Py_INCREF(Py_None);
1725 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00001726#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001727}
1728
Christian Heimes36281872007-11-30 21:11:28 +00001729#ifdef HAVE_FCHMOD
1730PyDoc_STRVAR(posix_fchmod__doc__,
1731"fchmod(fd, mode)\n\n\
1732Change the access permissions of the file given by file\n\
1733descriptor fd.");
1734
1735static PyObject *
1736posix_fchmod(PyObject *self, PyObject *args)
1737{
1738 int fd, mode, res;
1739 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1740 return NULL;
1741 Py_BEGIN_ALLOW_THREADS
1742 res = fchmod(fd, mode);
1743 Py_END_ALLOW_THREADS
1744 if (res < 0)
1745 return posix_error();
1746 Py_RETURN_NONE;
1747}
1748#endif /* HAVE_FCHMOD */
1749
1750#ifdef HAVE_LCHMOD
1751PyDoc_STRVAR(posix_lchmod__doc__,
1752"lchmod(path, mode)\n\n\
1753Change the access permissions of a file. If path is a symlink, this\n\
1754affects the link itself rather than the target.");
1755
1756static PyObject *
1757posix_lchmod(PyObject *self, PyObject *args)
1758{
1759 char *path = NULL;
1760 int i;
1761 int res;
1762 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1763 &path, &i))
1764 return NULL;
1765 Py_BEGIN_ALLOW_THREADS
1766 res = lchmod(path, i);
1767 Py_END_ALLOW_THREADS
1768 if (res < 0)
1769 return posix_error_with_allocated_filename(path);
1770 PyMem_Free(path);
1771 Py_RETURN_NONE;
1772}
1773#endif /* HAVE_LCHMOD */
1774
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001775
Martin v. Löwis382abef2007-02-19 10:55:19 +00001776#ifdef HAVE_CHFLAGS
1777PyDoc_STRVAR(posix_chflags__doc__,
1778"chflags(path, flags)\n\n\
1779Set file flags.");
1780
1781static PyObject *
1782posix_chflags(PyObject *self, PyObject *args)
1783{
1784 char *path;
1785 unsigned long flags;
1786 int res;
1787 if (!PyArg_ParseTuple(args, "etk:chflags",
1788 Py_FileSystemDefaultEncoding, &path, &flags))
1789 return NULL;
1790 Py_BEGIN_ALLOW_THREADS
1791 res = chflags(path, flags);
1792 Py_END_ALLOW_THREADS
1793 if (res < 0)
1794 return posix_error_with_allocated_filename(path);
1795 PyMem_Free(path);
1796 Py_INCREF(Py_None);
1797 return Py_None;
1798}
1799#endif /* HAVE_CHFLAGS */
1800
1801#ifdef HAVE_LCHFLAGS
1802PyDoc_STRVAR(posix_lchflags__doc__,
1803"lchflags(path, flags)\n\n\
1804Set file flags.\n\
1805This function will not follow symbolic links.");
1806
1807static PyObject *
1808posix_lchflags(PyObject *self, PyObject *args)
1809{
1810 char *path;
1811 unsigned long flags;
1812 int res;
1813 if (!PyArg_ParseTuple(args, "etk:lchflags",
1814 Py_FileSystemDefaultEncoding, &path, &flags))
1815 return NULL;
1816 Py_BEGIN_ALLOW_THREADS
1817 res = lchflags(path, flags);
1818 Py_END_ALLOW_THREADS
1819 if (res < 0)
1820 return posix_error_with_allocated_filename(path);
1821 PyMem_Free(path);
1822 Py_INCREF(Py_None);
1823 return Py_None;
1824}
1825#endif /* HAVE_LCHFLAGS */
1826
Martin v. Löwis244edc82001-10-04 22:44:26 +00001827#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001828PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001829"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001830Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001831
1832static PyObject *
1833posix_chroot(PyObject *self, PyObject *args)
1834{
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00001835 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001836}
1837#endif
1838
Guido van Rossum21142a01999-01-08 21:05:37 +00001839#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001840PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001841"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001842force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001843
1844static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001845posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001846{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001847 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001848}
1849#endif /* HAVE_FSYNC */
1850
1851#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001852
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001853#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001854extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1855#endif
1856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001858"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001859force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001860 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001861
1862static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001863posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001864{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001865 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001866}
1867#endif /* HAVE_FDATASYNC */
1868
1869
Fredrik Lundh10723342000-07-10 16:38:09 +00001870#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001872"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001874
Barry Warsaw53699e91996-12-10 23:23:01 +00001875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001876posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001877{
Mark Hammondef8b6542001-05-13 08:04:26 +00001878 char *path = NULL;
Gregory P. Smithf48da8f2008-03-18 19:05:32 +00001879 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001880 int res;
Gregory P. Smithf48da8f2008-03-18 19:05:32 +00001881 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001882 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001883 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001884 return NULL;
1885 Py_BEGIN_ALLOW_THREADS
1886 res = chown(path, (uid_t) uid, (gid_t) gid);
1887 Py_END_ALLOW_THREADS
1888 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001889 return posix_error_with_allocated_filename(path);
1890 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001891 Py_INCREF(Py_None);
1892 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001893}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001894#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001895
Christian Heimes36281872007-11-30 21:11:28 +00001896#ifdef HAVE_FCHOWN
1897PyDoc_STRVAR(posix_fchown__doc__,
1898"fchown(fd, uid, gid)\n\n\
1899Change the owner and group id of the file given by file descriptor\n\
1900fd to the numeric uid and gid.");
1901
1902static PyObject *
1903posix_fchown(PyObject *self, PyObject *args)
1904{
1905 int fd, uid, gid;
1906 int res;
1907 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1908 return NULL;
1909 Py_BEGIN_ALLOW_THREADS
1910 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1911 Py_END_ALLOW_THREADS
1912 if (res < 0)
1913 return posix_error();
1914 Py_RETURN_NONE;
1915}
1916#endif /* HAVE_FCHOWN */
1917
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001918#ifdef HAVE_LCHOWN
1919PyDoc_STRVAR(posix_lchown__doc__,
1920"lchown(path, uid, gid)\n\n\
1921Change the owner and group id of path to the numeric uid and gid.\n\
1922This function will not follow symbolic links.");
1923
1924static PyObject *
1925posix_lchown(PyObject *self, PyObject *args)
1926{
1927 char *path = NULL;
1928 int uid, gid;
1929 int res;
1930 if (!PyArg_ParseTuple(args, "etii:lchown",
1931 Py_FileSystemDefaultEncoding, &path,
1932 &uid, &gid))
1933 return NULL;
1934 Py_BEGIN_ALLOW_THREADS
1935 res = lchown(path, (uid_t) uid, (gid_t) gid);
1936 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001937 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001938 return posix_error_with_allocated_filename(path);
1939 PyMem_Free(path);
1940 Py_INCREF(Py_None);
1941 return Py_None;
1942}
1943#endif /* HAVE_LCHOWN */
1944
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001945
Guido van Rossum36bc6801995-06-14 22:54:23 +00001946#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001948"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001949Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001950
Barry Warsaw53699e91996-12-10 23:23:01 +00001951static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001952posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001953{
Facundo Batista5596b0c2008-06-22 13:36:20 +00001954 int bufsize_incr = 1024;
1955 int bufsize = 0;
1956 char *tmpbuf = NULL;
1957 char *res = NULL;
1958 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001959
Barry Warsaw53699e91996-12-10 23:23:01 +00001960 Py_BEGIN_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00001961 do {
1962 bufsize = bufsize + bufsize_incr;
1963 tmpbuf = malloc(bufsize);
1964 if (tmpbuf == NULL) {
1965 break;
1966 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001967#if defined(PYOS_OS2) && defined(PYCC_GCC)
Facundo Batista5596b0c2008-06-22 13:36:20 +00001968 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001969#else
Facundo Batista5596b0c2008-06-22 13:36:20 +00001970 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001971#endif
Facundo Batista5596b0c2008-06-22 13:36:20 +00001972
1973 if (res == NULL) {
1974 free(tmpbuf);
1975 }
1976 } while ((res == NULL) && (errno == ERANGE));
Barry Warsaw53699e91996-12-10 23:23:01 +00001977 Py_END_ALLOW_THREADS
Facundo Batista5596b0c2008-06-22 13:36:20 +00001978
Guido van Rossumff4949e1992-08-05 19:58:53 +00001979 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001980 return posix_error();
Facundo Batista5596b0c2008-06-22 13:36:20 +00001981
1982 dynamic_return = PyString_FromString(tmpbuf);
1983 free(tmpbuf);
1984
1985 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001986}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001987
Walter Dörwald3b918c32002-11-21 20:18:46 +00001988#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989PyDoc_STRVAR(posix_getcwdu__doc__,
1990"getcwdu() -> path\n\n\
1991Return a unicode string representing the current working directory.");
1992
1993static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001994posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995{
1996 char buf[1026];
1997 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001998
1999#ifdef Py_WIN_WIDE_FILENAMES
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002000 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002001 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002002 wchar_t wbuf[1026];
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002003 wchar_t *wbuf2 = wbuf;
2004 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002005 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002006 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2007 /* If the buffer is large enough, len does not include the
2008 terminating \0. If the buffer is too small, len includes
2009 the space needed for the terminator. */
2010 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2011 wbuf2 = malloc(len * sizeof(wchar_t));
2012 if (wbuf2)
2013 len = GetCurrentDirectoryW(len, wbuf2);
2014 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002015 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002016 if (!wbuf2) {
2017 PyErr_NoMemory();
2018 return NULL;
2019 }
2020 if (!len) {
2021 if (wbuf2 != wbuf) free(wbuf2);
2022 return win32_error("getcwdu", NULL);
2023 }
2024 resobj = PyUnicode_FromWideChar(wbuf2, len);
2025 if (wbuf2 != wbuf) free(wbuf2);
2026 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002027 }
2028#endif
2029
2030 Py_BEGIN_ALLOW_THREADS
2031#if defined(PYOS_OS2) && defined(PYCC_GCC)
2032 res = _getcwd2(buf, sizeof buf);
2033#else
2034 res = getcwd(buf, sizeof buf);
2035#endif
2036 Py_END_ALLOW_THREADS
2037 if (res == NULL)
2038 return posix_error();
2039 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2040}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002041#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00002042#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002043
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002044
Guido van Rossumb6775db1994-08-01 11:34:53 +00002045#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002046PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002047"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002049
Barry Warsaw53699e91996-12-10 23:23:01 +00002050static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002051posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002052{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002053 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002054}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002055#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002059"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060Return a list containing the names of the entries in the directory.\n\
2061\n\
2062 path: path of directory to list\n\
2063\n\
2064The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066
Barry Warsaw53699e91996-12-10 23:23:01 +00002067static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002068posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002069{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002070 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002071 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002072#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002073
Barry Warsaw53699e91996-12-10 23:23:01 +00002074 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002075 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002076 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077 WIN32_FIND_DATA FileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002078 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002079 char *bufptr = namebuf;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002080 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002082#ifdef Py_WIN_WIDE_FILENAMES
2083 /* If on wide-character-capable OS see if argument
2084 is Unicode and if so use wide API. */
2085 if (unicode_file_names()) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002086 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002087 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2088 WIN32_FIND_DATAW wFileData;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002089 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002090 Py_UNICODE wch;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002091 /* Overallocate for \\*.*\0 */
2092 len = PyUnicode_GET_SIZE(po);
2093 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2094 if (!wnamebuf) {
2095 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002096 return NULL;
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002097 }
2098 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2099 wch = len > 0 ? wnamebuf[len-1] : '\0';
2100 if (wch != L'/' && wch != L'\\' && wch != L':')
2101 wnamebuf[len++] = L'\\';
2102 wcscpy(wnamebuf + len, L"*.*");
2103 if ((d = PyList_New(0)) == NULL) {
2104 free(wnamebuf);
2105 return NULL;
2106 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002107 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2108 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002109 int error = GetLastError();
2110 if (error == ERROR_FILE_NOT_FOUND) {
2111 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002112 return d;
2113 }
2114 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002115 win32_error_unicode("FindFirstFileW", wnamebuf);
2116 free(wnamebuf);
2117 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002118 }
2119 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002120 /* Skip over . and .. */
2121 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2122 wcscmp(wFileData.cFileName, L"..") != 0) {
2123 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2124 if (v == NULL) {
2125 Py_DECREF(d);
2126 d = NULL;
2127 break;
2128 }
2129 if (PyList_Append(d, v) != 0) {
2130 Py_DECREF(v);
2131 Py_DECREF(d);
2132 d = NULL;
2133 break;
2134 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002135 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002136 }
Georg Brandl622927b2006-03-07 12:48:03 +00002137 Py_BEGIN_ALLOW_THREADS
2138 result = FindNextFileW(hFindFile, &wFileData);
2139 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002140 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2141 it got to the end of the directory. */
2142 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2143 Py_DECREF(d);
2144 win32_error_unicode("FindNextFileW", wnamebuf);
2145 FindClose(hFindFile);
2146 free(wnamebuf);
2147 return NULL;
2148 }
Georg Brandl622927b2006-03-07 12:48:03 +00002149 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002150
2151 if (FindClose(hFindFile) == FALSE) {
2152 Py_DECREF(d);
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002153 win32_error_unicode("FindClose", wnamebuf);
2154 free(wnamebuf);
2155 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156 }
Martin v. Löwise3edaea2006-05-15 05:51:36 +00002157 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002158 return d;
2159 }
2160 /* Drop the argument parsing error as narrow strings
2161 are also valid. */
2162 PyErr_Clear();
2163 }
2164#endif
2165
Tim Peters5aa91602002-01-30 05:46:57 +00002166 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002167 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002168 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002169 if (len > 0) {
2170 char ch = namebuf[len-1];
2171 if (ch != SEP && ch != ALTSEP && ch != ':')
2172 namebuf[len++] = '/';
2173 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002174 strcpy(namebuf + len, "*.*");
2175
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002177 return NULL;
2178
2179 hFindFile = FindFirstFile(namebuf, &FileData);
2180 if (hFindFile == INVALID_HANDLE_VALUE) {
Martin v. Löwis682b1bb2006-05-12 12:27:28 +00002181 int error = GetLastError();
2182 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002183 return d;
2184 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002185 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002186 }
2187 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002188 /* Skip over . and .. */
2189 if (strcmp(FileData.cFileName, ".") != 0 &&
2190 strcmp(FileData.cFileName, "..") != 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002191 v = PyString_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002192 if (v == NULL) {
2193 Py_DECREF(d);
2194 d = NULL;
2195 break;
2196 }
2197 if (PyList_Append(d, v) != 0) {
2198 Py_DECREF(v);
2199 Py_DECREF(d);
2200 d = NULL;
2201 break;
2202 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002204 }
Georg Brandl622927b2006-03-07 12:48:03 +00002205 Py_BEGIN_ALLOW_THREADS
2206 result = FindNextFile(hFindFile, &FileData);
2207 Py_END_ALLOW_THREADS
Martin v. Löwis982e9fe2006-07-24 12:54:17 +00002208 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2209 it got to the end of the directory. */
2210 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2211 Py_DECREF(d);
2212 win32_error("FindNextFile", namebuf);
2213 FindClose(hFindFile);
2214 return NULL;
2215 }
Georg Brandl622927b2006-03-07 12:48:03 +00002216 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002217
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002218 if (FindClose(hFindFile) == FALSE) {
2219 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002220 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002221 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002222
2223 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002224
Tim Peters0bb44a42000-09-15 07:44:49 +00002225#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002226
2227#ifndef MAX_PATH
2228#define MAX_PATH CCHMAXPATH
2229#endif
2230 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002231 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002232 PyObject *d, *v;
2233 char namebuf[MAX_PATH+5];
2234 HDIR hdir = 1;
2235 ULONG srchcnt = 1;
2236 FILEFINDBUF3 ep;
2237 APIRET rc;
2238
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002239 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002240 return NULL;
2241 if (len >= MAX_PATH) {
2242 PyErr_SetString(PyExc_ValueError, "path too long");
2243 return NULL;
2244 }
2245 strcpy(namebuf, name);
2246 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002247 if (*pt == ALTSEP)
2248 *pt = SEP;
2249 if (namebuf[len-1] != SEP)
2250 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002251 strcpy(namebuf + len, "*.*");
2252
2253 if ((d = PyList_New(0)) == NULL)
2254 return NULL;
2255
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002256 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2257 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002258 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002259 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2260 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2261 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002262
2263 if (rc != NO_ERROR) {
2264 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002265 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266 }
2267
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002268 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002269 do {
2270 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002271 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002272 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273
2274 strcpy(namebuf, ep.achName);
2275
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002276 /* Leave Case of Name Alone -- In Native Form */
2277 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002278
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002279 v = PyString_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002280 if (v == NULL) {
2281 Py_DECREF(d);
2282 d = NULL;
2283 break;
2284 }
2285 if (PyList_Append(d, v) != 0) {
2286 Py_DECREF(v);
2287 Py_DECREF(d);
2288 d = NULL;
2289 break;
2290 }
2291 Py_DECREF(v);
2292 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2293 }
2294
2295 return d;
2296#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002297
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002298 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002299 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002300 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002301 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002302 int arg_is_unicode = 1;
2303
Georg Brandl05e89b82006-04-11 07:04:06 +00002304 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002305 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2306 arg_is_unicode = 0;
2307 PyErr_Clear();
2308 }
2309 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002310 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002311 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002312 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002313 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002314 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002315 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002316 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002317 return NULL;
2318 }
Georg Brandl622927b2006-03-07 12:48:03 +00002319 for (;;) {
Georg Brandl86cbf812008-07-16 21:31:41 +00002320 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002321 Py_BEGIN_ALLOW_THREADS
2322 ep = readdir(dirp);
2323 Py_END_ALLOW_THREADS
Georg Brandl86cbf812008-07-16 21:31:41 +00002324 if (ep == NULL) {
2325 if (errno == 0) {
2326 break;
2327 } else {
2328 closedir(dirp);
2329 Py_DECREF(d);
2330 return posix_error_with_allocated_filename(name);
2331 }
2332 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002333 if (ep->d_name[0] == '.' &&
2334 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002335 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002336 continue;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002337 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002338 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002339 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002340 d = NULL;
2341 break;
2342 }
Just van Rossum46c97842003-02-25 21:42:15 +00002343#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00002344 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002345 PyObject *w;
2346
2347 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002348 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002349 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002350 if (w != NULL) {
2351 Py_DECREF(v);
2352 v = w;
2353 }
2354 else {
2355 /* fall back to the original byte string, as
2356 discussed in patch #683592 */
2357 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002358 }
Just van Rossum46c97842003-02-25 21:42:15 +00002359 }
2360#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002361 if (PyList_Append(d, v) != 0) {
2362 Py_DECREF(v);
2363 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002364 d = NULL;
2365 break;
2366 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002367 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002368 }
2369 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002370 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002371
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002372 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002373
Tim Peters0bb44a42000-09-15 07:44:49 +00002374#endif /* which OS */
2375} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002376
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002377#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002378/* A helper function for abspath on win32 */
2379static PyObject *
2380posix__getfullpathname(PyObject *self, PyObject *args)
2381{
2382 /* assume encoded strings wont more than double no of chars */
2383 char inbuf[MAX_PATH*2];
2384 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002385 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002386 char outbuf[MAX_PATH*2];
2387 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002388#ifdef Py_WIN_WIDE_FILENAMES
2389 if (unicode_file_names()) {
2390 PyUnicodeObject *po;
2391 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Hirokazu Yamamotoed29bb42008-11-08 03:46:17 +00002392 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2393 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002394 Py_UNICODE *wtemp;
Hirokazu Yamamotoed29bb42008-11-08 03:46:17 +00002395 DWORD result;
2396 PyObject *v;
2397 result = GetFullPathNameW(wpath,
2398 sizeof(woutbuf)/sizeof(woutbuf[0]),
2399 woutbuf, &wtemp);
2400 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2401 woutbufp = malloc(result * sizeof(Py_UNICODE));
2402 if (!woutbufp)
2403 return PyErr_NoMemory();
2404 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2405 }
2406 if (result)
2407 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2408 else
2409 v = win32_error_unicode("GetFullPathNameW", wpath);
2410 if (woutbufp != woutbuf)
2411 free(woutbufp);
2412 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002413 }
2414 /* Drop the argument parsing error as narrow strings
2415 are also valid. */
2416 PyErr_Clear();
2417 }
2418#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002419 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2420 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002421 &insize))
2422 return NULL;
2423 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2424 outbuf, &temp))
2425 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002426 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2427 return PyUnicode_Decode(outbuf, strlen(outbuf),
2428 Py_FileSystemDefaultEncoding, NULL);
2429 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002430 return PyString_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002431} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002432#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002434PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002435"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002436Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002437
Barry Warsaw53699e91996-12-10 23:23:01 +00002438static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002439posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002440{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002441 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002442 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002443 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002444
2445#ifdef Py_WIN_WIDE_FILENAMES
2446 if (unicode_file_names()) {
2447 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002448 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002449 Py_BEGIN_ALLOW_THREADS
2450 /* PyUnicode_AS_UNICODE OK without thread lock as
2451 it is a simple dereference. */
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002452 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002453 Py_END_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002454 if (!res)
2455 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002456 Py_INCREF(Py_None);
2457 return Py_None;
2458 }
2459 /* Drop the argument parsing error as narrow strings
2460 are also valid. */
2461 PyErr_Clear();
2462 }
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002463 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2464 Py_FileSystemDefaultEncoding, &path, &mode))
2465 return NULL;
2466 Py_BEGIN_ALLOW_THREADS
2467 /* PyUnicode_AS_UNICODE OK without thread lock as
2468 it is a simple dereference. */
2469 res = CreateDirectoryA(path, NULL);
2470 Py_END_ALLOW_THREADS
2471 if (!res) {
2472 win32_error("mkdir", path);
2473 PyMem_Free(path);
2474 return NULL;
2475 }
2476 PyMem_Free(path);
2477 Py_INCREF(Py_None);
2478 return Py_None;
2479#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002480
Tim Peters5aa91602002-01-30 05:46:57 +00002481 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002482 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002483 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002484 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002485#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002486 res = mkdir(path);
2487#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002488 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002489#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002490 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002491 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002492 return posix_error_with_allocated_filename(path);
2493 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002494 Py_INCREF(Py_None);
2495 return Py_None;
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002496#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002497}
2498
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002499
Neal Norwitz1818ed72006-03-26 00:29:48 +00002500/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2501#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002502#include <sys/resource.h>
2503#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002504
Neal Norwitz1818ed72006-03-26 00:29:48 +00002505
2506#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002507PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002508"nice(inc) -> new_priority\n\n\
2509Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002510
Barry Warsaw53699e91996-12-10 23:23:01 +00002511static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002512posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002513{
2514 int increment, value;
2515
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002516 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002517 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002518
2519 /* There are two flavours of 'nice': one that returns the new
2520 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002521 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2522 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002523
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002524 If we are of the nice family that returns the new priority, we
2525 need to clear errno before the call, and check if errno is filled
2526 before calling posix_error() on a returnvalue of -1, because the
2527 -1 may be the actual new priority! */
2528
2529 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002530 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002531#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002532 if (value == 0)
2533 value = getpriority(PRIO_PROCESS, 0);
2534#endif
2535 if (value == -1 && errno != 0)
2536 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002537 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002538 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002539}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002540#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002543"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002544Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002545
Barry Warsaw53699e91996-12-10 23:23:01 +00002546static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002547posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002548{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002549#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002550 PyObject *o1, *o2;
2551 char *p1, *p2;
2552 BOOL result;
2553 if (unicode_file_names()) {
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002554 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2555 goto error;
2556 if (!convert_to_unicode(&o1))
2557 goto error;
2558 if (!convert_to_unicode(&o2)) {
2559 Py_DECREF(o1);
2560 goto error;
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002561 }
Hirokazu Yamamotoa0fdd722008-08-17 09:19:52 +00002562 Py_BEGIN_ALLOW_THREADS
2563 result = MoveFileW(PyUnicode_AsUnicode(o1),
2564 PyUnicode_AsUnicode(o2));
2565 Py_END_ALLOW_THREADS
2566 Py_DECREF(o1);
2567 Py_DECREF(o2);
2568 if (!result)
2569 return win32_error("rename", NULL);
2570 Py_INCREF(Py_None);
2571 return Py_None;
2572error:
2573 PyErr_Clear();
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002574 }
2575 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2576 return NULL;
2577 Py_BEGIN_ALLOW_THREADS
2578 result = MoveFileA(p1, p2);
2579 Py_END_ALLOW_THREADS
2580 if (!result)
2581 return win32_error("rename", NULL);
2582 Py_INCREF(Py_None);
2583 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002584#else
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00002585 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002586#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002587}
2588
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002589
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002590PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002591"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002592Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002593
Barry Warsaw53699e91996-12-10 23:23:01 +00002594static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002595posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002596{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002597#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002598 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002599#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002600 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002601#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002602}
2603
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002605PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002606"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002607Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002608
Barry Warsaw53699e91996-12-10 23:23:01 +00002609static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002610posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002611{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002612#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002613 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002614#else
2615 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2616#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002617}
2618
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002619
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002620#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002621PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002622"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002623Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002624
Barry Warsaw53699e91996-12-10 23:23:01 +00002625static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002626posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002627{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002628 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002629 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002630 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002631 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002632 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002633 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002634 Py_END_ALLOW_THREADS
2635 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002636}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002637#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002638
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002640PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002641"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002642Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002643
Barry Warsaw53699e91996-12-10 23:23:01 +00002644static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002645posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002646{
2647 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002648 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002649 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002650 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002651 if (i < 0)
2652 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002653 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002654}
2655
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002656
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002657PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002658"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002659Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002662"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002664
Barry Warsaw53699e91996-12-10 23:23:01 +00002665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002666posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002667{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002668#ifdef MS_WINDOWS
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002669 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002670#else
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00002671 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002672#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002673}
2674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002675
Guido van Rossumb6775db1994-08-01 11:34:53 +00002676#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002677PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002678"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Barry Warsaw53699e91996-12-10 23:23:01 +00002681static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002682posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002683{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002684 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002685 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002686
Barry Warsaw53699e91996-12-10 23:23:01 +00002687 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002688 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002689 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002690 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002691 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002692 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002693 u.sysname,
2694 u.nodename,
2695 u.release,
2696 u.version,
2697 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002698}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002699#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002700
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002701static int
2702extract_time(PyObject *t, long* sec, long* usec)
2703{
2704 long intval;
2705 if (PyFloat_Check(t)) {
2706 double tval = PyFloat_AsDouble(t);
Christian Heimese93237d2007-12-19 02:37:44 +00002707 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002708 if (!intobj)
2709 return -1;
2710 intval = PyInt_AsLong(intobj);
2711 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002712 if (intval == -1 && PyErr_Occurred())
2713 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002714 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002715 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716 if (*usec < 0)
2717 /* If rounding gave us a negative number,
2718 truncate. */
2719 *usec = 0;
2720 return 0;
2721 }
2722 intval = PyInt_AsLong(t);
2723 if (intval == -1 && PyErr_Occurred())
2724 return -1;
2725 *sec = intval;
2726 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002727 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002728}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002730PyDoc_STRVAR(posix_utime__doc__,
Georg Brandl9e5b5e42006-05-17 14:18:20 +00002731"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002732utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002733Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002734second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002735
Barry Warsaw53699e91996-12-10 23:23:01 +00002736static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002737posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002738{
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002739#ifdef Py_WIN_WIDE_FILENAMES
2740 PyObject *arg;
2741 PyUnicodeObject *obwpath;
2742 wchar_t *wpath = NULL;
2743 char *apath = NULL;
2744 HANDLE hFile;
2745 long atimesec, mtimesec, ausec, musec;
2746 FILETIME atime, mtime;
2747 PyObject *result = NULL;
2748
2749 if (unicode_file_names()) {
2750 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2751 wpath = PyUnicode_AS_UNICODE(obwpath);
2752 Py_BEGIN_ALLOW_THREADS
2753 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002754 NULL, OPEN_EXISTING,
2755 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002756 Py_END_ALLOW_THREADS
2757 if (hFile == INVALID_HANDLE_VALUE)
2758 return win32_error_unicode("utime", wpath);
2759 } else
2760 /* Drop the argument parsing error as narrow strings
2761 are also valid. */
2762 PyErr_Clear();
2763 }
2764 if (!wpath) {
2765 if (!PyArg_ParseTuple(args, "etO:utime",
2766 Py_FileSystemDefaultEncoding, &apath, &arg))
2767 return NULL;
2768 Py_BEGIN_ALLOW_THREADS
2769 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Martin v. Löwis18aaa562006-10-15 08:43:33 +00002770 NULL, OPEN_EXISTING,
2771 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002772 Py_END_ALLOW_THREADS
2773 if (hFile == INVALID_HANDLE_VALUE) {
2774 win32_error("utime", apath);
2775 PyMem_Free(apath);
2776 return NULL;
2777 }
2778 PyMem_Free(apath);
2779 }
2780
2781 if (arg == Py_None) {
2782 SYSTEMTIME now;
2783 GetSystemTime(&now);
2784 if (!SystemTimeToFileTime(&now, &mtime) ||
2785 !SystemTimeToFileTime(&now, &atime)) {
2786 win32_error("utime", NULL);
2787 goto done;
2788 }
2789 }
2790 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2791 PyErr_SetString(PyExc_TypeError,
2792 "utime() arg 2 must be a tuple (atime, mtime)");
2793 goto done;
2794 }
2795 else {
2796 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2797 &atimesec, &ausec) == -1)
2798 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002799 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002800 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2801 &mtimesec, &musec) == -1)
2802 goto done;
Martin v. Löwisf43893a2006-10-09 20:44:25 +00002803 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002804 }
2805 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2806 /* Avoid putting the file name into the error here,
2807 as that may confuse the user into believing that
2808 something is wrong with the file, when it also
2809 could be the time stamp that gives a problem. */
2810 win32_error("utime", NULL);
2811 }
2812 Py_INCREF(Py_None);
2813 result = Py_None;
2814done:
2815 CloseHandle(hFile);
2816 return result;
2817#else /* Py_WIN_WIDE_FILENAMES */
2818
Neal Norwitz2adf2102004-06-09 01:46:02 +00002819 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002820 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002821 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002822 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002823
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002824#if defined(HAVE_UTIMES)
2825 struct timeval buf[2];
2826#define ATIME buf[0].tv_sec
2827#define MTIME buf[1].tv_sec
2828#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002829/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002830 struct utimbuf buf;
2831#define ATIME buf.actime
2832#define MTIME buf.modtime
2833#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002834#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002835 time_t buf[2];
2836#define ATIME buf[0]
2837#define MTIME buf[1]
2838#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002839#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002840
Mark Hammond817c9292003-12-03 01:22:38 +00002841
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002842 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002843 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002844 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002845 if (arg == Py_None) {
2846 /* optional time values not given */
2847 Py_BEGIN_ALLOW_THREADS
2848 res = utime(path, NULL);
2849 Py_END_ALLOW_THREADS
2850 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002851 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002852 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002853 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002854 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002855 return NULL;
2856 }
2857 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002858 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002859 &atime, &ausec) == -1) {
2860 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002861 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002862 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002863 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002864 &mtime, &musec) == -1) {
2865 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002866 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002867 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002868 ATIME = atime;
2869 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002870#ifdef HAVE_UTIMES
2871 buf[0].tv_usec = ausec;
2872 buf[1].tv_usec = musec;
2873 Py_BEGIN_ALLOW_THREADS
2874 res = utimes(path, buf);
2875 Py_END_ALLOW_THREADS
2876#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002877 Py_BEGIN_ALLOW_THREADS
2878 res = utime(path, UTIME_ARG);
2879 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002880#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002881 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002882 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002883 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002884 }
Neal Norwitz96652712004-06-06 20:40:27 +00002885 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002886 Py_INCREF(Py_None);
2887 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002888#undef UTIME_ARG
2889#undef ATIME
2890#undef MTIME
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +00002891#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002892}
2893
Guido van Rossum85e3b011991-06-03 12:42:10 +00002894
Guido van Rossum3b066191991-06-04 19:40:25 +00002895/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002897PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002898"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002899Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002900
Barry Warsaw53699e91996-12-10 23:23:01 +00002901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002902posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002903{
2904 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002905 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002906 return NULL;
2907 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002908 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002909}
2910
Martin v. Löwis114619e2002-10-07 06:44:21 +00002911#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2912static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002913free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002914{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002915 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002916 for (i = 0; i < count; i++)
2917 PyMem_Free(array[i]);
2918 PyMem_DEL(array);
2919}
2920#endif
2921
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002922
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002923#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002924PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002925"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002926Execute an executable path with arguments, replacing current process.\n\
2927\n\
2928 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002929 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002930
Barry Warsaw53699e91996-12-10 23:23:01 +00002931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002932posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002933{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002934 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002935 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002936 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002937 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002938 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002939
Guido van Rossum89b33251993-10-22 14:26:06 +00002940 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002941 argv is a list or tuple of strings. */
2942
Martin v. Löwis114619e2002-10-07 06:44:21 +00002943 if (!PyArg_ParseTuple(args, "etO:execv",
2944 Py_FileSystemDefaultEncoding,
2945 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002946 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002947 if (PyList_Check(argv)) {
2948 argc = PyList_Size(argv);
2949 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002950 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002951 else if (PyTuple_Check(argv)) {
2952 argc = PyTuple_Size(argv);
2953 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002954 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002955 else {
Fred Drake661ea262000-10-24 19:57:45 +00002956 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002957 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002958 return NULL;
2959 }
2960
Barry Warsaw53699e91996-12-10 23:23:01 +00002961 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002962 if (argvlist == NULL) {
2963 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002964 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002965 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002966 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002967 if (!PyArg_Parse((*getitem)(argv, i), "et",
2968 Py_FileSystemDefaultEncoding,
2969 &argvlist[i])) {
2970 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002971 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002972 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002973 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002974 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002975
Guido van Rossum85e3b011991-06-03 12:42:10 +00002976 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002977 }
2978 argvlist[argc] = NULL;
2979
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002980 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002981
Guido van Rossum85e3b011991-06-03 12:42:10 +00002982 /* If we get here it's definitely an error */
2983
Martin v. Löwis114619e2002-10-07 06:44:21 +00002984 free_string_array(argvlist, argc);
2985 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002986 return posix_error();
2987}
2988
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002990PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002991"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002992Execute a path with arguments and environment, replacing current process.\n\
2993\n\
2994 path: path of executable file\n\
2995 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002996 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002997
Barry Warsaw53699e91996-12-10 23:23:01 +00002998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002999posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003000{
3001 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003002 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003003 char **argvlist;
3004 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003005 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003006 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003007 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003008 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003009
3010 /* execve has three arguments: (path, argv, env), where
3011 argv is a list or tuple of strings and env is a dictionary
3012 like posix.environ. */
3013
Martin v. Löwis114619e2002-10-07 06:44:21 +00003014 if (!PyArg_ParseTuple(args, "etOO:execve",
3015 Py_FileSystemDefaultEncoding,
3016 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003017 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003018 if (PyList_Check(argv)) {
3019 argc = PyList_Size(argv);
3020 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003021 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003022 else if (PyTuple_Check(argv)) {
3023 argc = PyTuple_Size(argv);
3024 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003025 }
3026 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003027 PyErr_SetString(PyExc_TypeError,
3028 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003029 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003030 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003031 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003032 PyErr_SetString(PyExc_TypeError,
3033 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003034 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003035 }
3036
Barry Warsaw53699e91996-12-10 23:23:01 +00003037 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003038 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003039 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003040 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003041 }
3042 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003043 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003044 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003045 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003046 &argvlist[i]))
3047 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003048 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003049 goto fail_1;
3050 }
3051 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003052 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003053 argvlist[argc] = NULL;
3054
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003055 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003056 if (i < 0)
3057 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003058 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003059 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003060 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003061 goto fail_1;
3062 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003063 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003064 keys = PyMapping_Keys(env);
3065 vals = PyMapping_Values(env);
3066 if (!keys || !vals)
3067 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003068 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3069 PyErr_SetString(PyExc_TypeError,
3070 "execve(): env.keys() or env.values() is not a list");
3071 goto fail_2;
3072 }
Tim Peters5aa91602002-01-30 05:46:57 +00003073
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003074 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003075 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003076 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003077
3078 key = PyList_GetItem(keys, pos);
3079 val = PyList_GetItem(vals, pos);
3080 if (!key || !val)
3081 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003082
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003083 if (!PyArg_Parse(
3084 key,
3085 "s;execve() arg 3 contains a non-string key",
3086 &k) ||
3087 !PyArg_Parse(
3088 val,
3089 "s;execve() arg 3 contains a non-string value",
3090 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003091 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003092 goto fail_2;
3093 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003094
3095#if defined(PYOS_OS2)
3096 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3097 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3098#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003099 len = PyString_Size(key) + PyString_Size(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003100 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003101 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003102 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003103 goto fail_2;
3104 }
Tim Petersc8996f52001-12-03 20:41:00 +00003105 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003106 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003107#if defined(PYOS_OS2)
3108 }
3109#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003110 }
3111 envlist[envc] = 0;
3112
3113 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003114
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003115 /* If we get here it's definitely an error */
3116
3117 (void) posix_error();
3118
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003119 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003120 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003121 PyMem_DEL(envlist[envc]);
3122 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003123 fail_1:
3124 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003125 Py_XDECREF(vals);
3126 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003127 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003128 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003129 return NULL;
3130}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003131#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003132
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003133
Guido van Rossuma1065681999-01-25 23:20:23 +00003134#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003135PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003136"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003137Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003138\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003139 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003140 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003141 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003142
3143static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003144posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003145{
3146 char *path;
3147 PyObject *argv;
3148 char **argvlist;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003149 int mode, i;
3150 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003151 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003152 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003153
3154 /* spawnv has three arguments: (mode, path, argv), where
3155 argv is a list or tuple of strings. */
3156
Martin v. Löwis114619e2002-10-07 06:44:21 +00003157 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3158 Py_FileSystemDefaultEncoding,
3159 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003160 return NULL;
3161 if (PyList_Check(argv)) {
3162 argc = PyList_Size(argv);
3163 getitem = PyList_GetItem;
3164 }
3165 else if (PyTuple_Check(argv)) {
3166 argc = PyTuple_Size(argv);
3167 getitem = PyTuple_GetItem;
3168 }
3169 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003170 PyErr_SetString(PyExc_TypeError,
3171 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003172 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003173 return NULL;
3174 }
3175
3176 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003177 if (argvlist == NULL) {
3178 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003179 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003180 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003181 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003182 if (!PyArg_Parse((*getitem)(argv, i), "et",
3183 Py_FileSystemDefaultEncoding,
3184 &argvlist[i])) {
3185 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003186 PyErr_SetString(
3187 PyExc_TypeError,
3188 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003189 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003190 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003191 }
3192 }
3193 argvlist[argc] = NULL;
3194
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003195#if defined(PYOS_OS2) && defined(PYCC_GCC)
3196 Py_BEGIN_ALLOW_THREADS
3197 spawnval = spawnv(mode, path, argvlist);
3198 Py_END_ALLOW_THREADS
3199#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003200 if (mode == _OLD_P_OVERLAY)
3201 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003202
Tim Peters25059d32001-12-07 20:35:43 +00003203 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003204 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003205 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003206#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003207
Martin v. Löwis114619e2002-10-07 06:44:21 +00003208 free_string_array(argvlist, argc);
3209 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003210
Fred Drake699f3522000-06-29 21:12:41 +00003211 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003212 return posix_error();
3213 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003214#if SIZEOF_LONG == SIZEOF_VOID_P
3215 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003216#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003217 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003218#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003219}
3220
3221
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003222PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003223"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003224Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003225\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003226 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003227 path: path of executable file\n\
3228 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003229 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003230
3231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003232posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003233{
3234 char *path;
3235 PyObject *argv, *env;
3236 char **argvlist;
3237 char **envlist;
3238 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003239 int mode, pos, envc;
3240 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003241 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003242 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Martin v. Löwis26fd9602006-04-22 11:15:41 +00003243 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003244
3245 /* spawnve has four arguments: (mode, path, argv, env), where
3246 argv is a list or tuple of strings and env is a dictionary
3247 like posix.environ. */
3248
Martin v. Löwis114619e2002-10-07 06:44:21 +00003249 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3250 Py_FileSystemDefaultEncoding,
3251 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003252 return NULL;
3253 if (PyList_Check(argv)) {
3254 argc = PyList_Size(argv);
3255 getitem = PyList_GetItem;
3256 }
3257 else if (PyTuple_Check(argv)) {
3258 argc = PyTuple_Size(argv);
3259 getitem = PyTuple_GetItem;
3260 }
3261 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003262 PyErr_SetString(PyExc_TypeError,
3263 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003264 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003265 }
3266 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003267 PyErr_SetString(PyExc_TypeError,
3268 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003269 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003270 }
3271
3272 argvlist = PyMem_NEW(char *, argc+1);
3273 if (argvlist == NULL) {
3274 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003275 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003276 }
3277 for (i = 0; i < argc; i++) {
3278 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003279 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003280 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003281 &argvlist[i]))
3282 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003283 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003284 goto fail_1;
3285 }
3286 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003287 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003288 argvlist[argc] = NULL;
3289
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003290 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003291 if (i < 0)
3292 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003293 envlist = PyMem_NEW(char *, i + 1);
3294 if (envlist == NULL) {
3295 PyErr_NoMemory();
3296 goto fail_1;
3297 }
3298 envc = 0;
3299 keys = PyMapping_Keys(env);
3300 vals = PyMapping_Values(env);
3301 if (!keys || !vals)
3302 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003303 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3304 PyErr_SetString(PyExc_TypeError,
3305 "spawnve(): env.keys() or env.values() is not a list");
3306 goto fail_2;
3307 }
Tim Peters5aa91602002-01-30 05:46:57 +00003308
Guido van Rossuma1065681999-01-25 23:20:23 +00003309 for (pos = 0; pos < i; pos++) {
3310 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003311 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003312
3313 key = PyList_GetItem(keys, pos);
3314 val = PyList_GetItem(vals, pos);
3315 if (!key || !val)
3316 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003317
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003318 if (!PyArg_Parse(
3319 key,
3320 "s;spawnve() arg 3 contains a non-string key",
3321 &k) ||
3322 !PyArg_Parse(
3323 val,
3324 "s;spawnve() arg 3 contains a non-string value",
3325 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003326 {
3327 goto fail_2;
3328 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003329 len = PyString_Size(key) + PyString_Size(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003330 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003331 if (p == NULL) {
3332 PyErr_NoMemory();
3333 goto fail_2;
3334 }
Tim Petersc8996f52001-12-03 20:41:00 +00003335 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003336 envlist[envc++] = p;
3337 }
3338 envlist[envc] = 0;
3339
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003340#if defined(PYOS_OS2) && defined(PYCC_GCC)
3341 Py_BEGIN_ALLOW_THREADS
3342 spawnval = spawnve(mode, path, argvlist, envlist);
3343 Py_END_ALLOW_THREADS
3344#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003345 if (mode == _OLD_P_OVERLAY)
3346 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003347
3348 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003349 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003350 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003351#endif
Tim Peters25059d32001-12-07 20:35:43 +00003352
Fred Drake699f3522000-06-29 21:12:41 +00003353 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003354 (void) posix_error();
3355 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003356#if SIZEOF_LONG == SIZEOF_VOID_P
3357 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003358#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003359 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003360#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003361
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003362 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003363 while (--envc >= 0)
3364 PyMem_DEL(envlist[envc]);
3365 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003366 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003367 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003368 Py_XDECREF(vals);
3369 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003370 fail_0:
3371 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003372 return res;
3373}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003374
3375/* OS/2 supports spawnvp & spawnvpe natively */
3376#if defined(PYOS_OS2)
3377PyDoc_STRVAR(posix_spawnvp__doc__,
3378"spawnvp(mode, file, args)\n\n\
3379Execute the program 'file' in a new process, using the environment\n\
3380search path to find the file.\n\
3381\n\
3382 mode: mode of process creation\n\
3383 file: executable file name\n\
3384 args: tuple or list of strings");
3385
3386static PyObject *
3387posix_spawnvp(PyObject *self, PyObject *args)
3388{
3389 char *path;
3390 PyObject *argv;
3391 char **argvlist;
3392 int mode, i, argc;
3393 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003394 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003395
3396 /* spawnvp has three arguments: (mode, path, argv), where
3397 argv is a list or tuple of strings. */
3398
3399 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3400 Py_FileSystemDefaultEncoding,
3401 &path, &argv))
3402 return NULL;
3403 if (PyList_Check(argv)) {
3404 argc = PyList_Size(argv);
3405 getitem = PyList_GetItem;
3406 }
3407 else if (PyTuple_Check(argv)) {
3408 argc = PyTuple_Size(argv);
3409 getitem = PyTuple_GetItem;
3410 }
3411 else {
3412 PyErr_SetString(PyExc_TypeError,
3413 "spawnvp() arg 2 must be a tuple or list");
3414 PyMem_Free(path);
3415 return NULL;
3416 }
3417
3418 argvlist = PyMem_NEW(char *, argc+1);
3419 if (argvlist == NULL) {
3420 PyMem_Free(path);
3421 return PyErr_NoMemory();
3422 }
3423 for (i = 0; i < argc; i++) {
3424 if (!PyArg_Parse((*getitem)(argv, i), "et",
3425 Py_FileSystemDefaultEncoding,
3426 &argvlist[i])) {
3427 free_string_array(argvlist, i);
3428 PyErr_SetString(
3429 PyExc_TypeError,
3430 "spawnvp() arg 2 must contain only strings");
3431 PyMem_Free(path);
3432 return NULL;
3433 }
3434 }
3435 argvlist[argc] = NULL;
3436
3437 Py_BEGIN_ALLOW_THREADS
3438#if defined(PYCC_GCC)
3439 spawnval = spawnvp(mode, path, argvlist);
3440#else
3441 spawnval = _spawnvp(mode, path, argvlist);
3442#endif
3443 Py_END_ALLOW_THREADS
3444
3445 free_string_array(argvlist, argc);
3446 PyMem_Free(path);
3447
3448 if (spawnval == -1)
3449 return posix_error();
3450 else
3451 return Py_BuildValue("l", (long) spawnval);
3452}
3453
3454
3455PyDoc_STRVAR(posix_spawnvpe__doc__,
3456"spawnvpe(mode, file, args, env)\n\n\
3457Execute the program 'file' in a new process, using the environment\n\
3458search path to find the file.\n\
3459\n\
3460 mode: mode of process creation\n\
3461 file: executable file name\n\
3462 args: tuple or list of arguments\n\
3463 env: dictionary of strings mapping to strings");
3464
3465static PyObject *
3466posix_spawnvpe(PyObject *self, PyObject *args)
3467{
3468 char *path;
3469 PyObject *argv, *env;
3470 char **argvlist;
3471 char **envlist;
3472 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3473 int mode, i, pos, argc, envc;
3474 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003475 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003476 int lastarg = 0;
3477
3478 /* spawnvpe has four arguments: (mode, path, argv, env), where
3479 argv is a list or tuple of strings and env is a dictionary
3480 like posix.environ. */
3481
3482 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3483 Py_FileSystemDefaultEncoding,
3484 &path, &argv, &env))
3485 return NULL;
3486 if (PyList_Check(argv)) {
3487 argc = PyList_Size(argv);
3488 getitem = PyList_GetItem;
3489 }
3490 else if (PyTuple_Check(argv)) {
3491 argc = PyTuple_Size(argv);
3492 getitem = PyTuple_GetItem;
3493 }
3494 else {
3495 PyErr_SetString(PyExc_TypeError,
3496 "spawnvpe() arg 2 must be a tuple or list");
3497 goto fail_0;
3498 }
3499 if (!PyMapping_Check(env)) {
3500 PyErr_SetString(PyExc_TypeError,
3501 "spawnvpe() arg 3 must be a mapping object");
3502 goto fail_0;
3503 }
3504
3505 argvlist = PyMem_NEW(char *, argc+1);
3506 if (argvlist == NULL) {
3507 PyErr_NoMemory();
3508 goto fail_0;
3509 }
3510 for (i = 0; i < argc; i++) {
3511 if (!PyArg_Parse((*getitem)(argv, i),
3512 "et;spawnvpe() arg 2 must contain only strings",
3513 Py_FileSystemDefaultEncoding,
3514 &argvlist[i]))
3515 {
3516 lastarg = i;
3517 goto fail_1;
3518 }
3519 }
3520 lastarg = argc;
3521 argvlist[argc] = NULL;
3522
3523 i = PyMapping_Size(env);
3524 if (i < 0)
3525 goto fail_1;
3526 envlist = PyMem_NEW(char *, i + 1);
3527 if (envlist == NULL) {
3528 PyErr_NoMemory();
3529 goto fail_1;
3530 }
3531 envc = 0;
3532 keys = PyMapping_Keys(env);
3533 vals = PyMapping_Values(env);
3534 if (!keys || !vals)
3535 goto fail_2;
3536 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3537 PyErr_SetString(PyExc_TypeError,
3538 "spawnvpe(): env.keys() or env.values() is not a list");
3539 goto fail_2;
3540 }
3541
3542 for (pos = 0; pos < i; pos++) {
3543 char *p, *k, *v;
3544 size_t len;
3545
3546 key = PyList_GetItem(keys, pos);
3547 val = PyList_GetItem(vals, pos);
3548 if (!key || !val)
3549 goto fail_2;
3550
3551 if (!PyArg_Parse(
3552 key,
3553 "s;spawnvpe() arg 3 contains a non-string key",
3554 &k) ||
3555 !PyArg_Parse(
3556 val,
3557 "s;spawnvpe() arg 3 contains a non-string value",
3558 &v))
3559 {
3560 goto fail_2;
3561 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003562 len = PyString_Size(key) + PyString_Size(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003563 p = PyMem_NEW(char, len);
3564 if (p == NULL) {
3565 PyErr_NoMemory();
3566 goto fail_2;
3567 }
3568 PyOS_snprintf(p, len, "%s=%s", k, v);
3569 envlist[envc++] = p;
3570 }
3571 envlist[envc] = 0;
3572
3573 Py_BEGIN_ALLOW_THREADS
3574#if defined(PYCC_GCC)
Andrew MacIntyre94dcf0d2008-02-03 07:07:31 +00003575 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003576#else
Andrew MacIntyre94dcf0d2008-02-03 07:07:31 +00003577 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003578#endif
3579 Py_END_ALLOW_THREADS
3580
3581 if (spawnval == -1)
3582 (void) posix_error();
3583 else
3584 res = Py_BuildValue("l", (long) spawnval);
3585
3586 fail_2:
3587 while (--envc >= 0)
3588 PyMem_DEL(envlist[envc]);
3589 PyMem_DEL(envlist);
3590 fail_1:
3591 free_string_array(argvlist, lastarg);
3592 Py_XDECREF(vals);
3593 Py_XDECREF(keys);
3594 fail_0:
3595 PyMem_Free(path);
3596 return res;
3597}
3598#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003599#endif /* HAVE_SPAWNV */
3600
3601
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003602#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003603PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003604"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003605Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3606\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003607Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003608
3609static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003610posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003611{
Christian Heimes951cc0f2008-01-31 23:08:23 +00003612 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003613 if (pid == -1)
3614 return posix_error();
Gregory P. Smithfb7a50f2008-07-14 06:06:48 +00003615 if (pid == 0)
3616 PyOS_AfterFork();
Christian Heimes951cc0f2008-01-31 23:08:23 +00003617 return PyInt_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003618}
3619#endif
3620
3621
Guido van Rossumad0ee831995-03-01 10:34:45 +00003622#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003623PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003624"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003625Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003626Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003627
Barry Warsaw53699e91996-12-10 23:23:01 +00003628static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003629posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003630{
Christian Heimes951cc0f2008-01-31 23:08:23 +00003631 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003632 if (pid == -1)
3633 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003634 if (pid == 0)
3635 PyOS_AfterFork();
Christian Heimes951cc0f2008-01-31 23:08:23 +00003636 return PyInt_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003637}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003638#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003639
Neal Norwitzb59798b2003-03-21 01:43:31 +00003640/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003641/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3642#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003643#define DEV_PTY_FILE "/dev/ptc"
3644#define HAVE_DEV_PTMX
3645#else
3646#define DEV_PTY_FILE "/dev/ptmx"
3647#endif
3648
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003649#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003650#ifdef HAVE_PTY_H
3651#include <pty.h>
3652#else
3653#ifdef HAVE_LIBUTIL_H
3654#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003655#endif /* HAVE_LIBUTIL_H */
3656#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003657#ifdef HAVE_STROPTS_H
3658#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003659#endif
3660#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003661
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003662#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003663PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003664"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003665Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003666
3667static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003668posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003669{
3670 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003671#ifndef HAVE_OPENPTY
3672 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003673#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003674#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003675 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003676#ifdef sun
Neal Norwitz84a98e02006-04-10 07:44:23 +00003677 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003678#endif
3679#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003680
Thomas Wouters70c21a12000-07-14 14:28:33 +00003681#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003682 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3683 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003684#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003685 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3686 if (slave_name == NULL)
3687 return posix_error();
3688
3689 slave_fd = open(slave_name, O_RDWR);
3690 if (slave_fd < 0)
3691 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003692#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003693 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003694 if (master_fd < 0)
3695 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003696 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003697 /* change permission of slave */
3698 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003699 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003700 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003701 }
3702 /* unlock slave */
3703 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003704 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003705 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003706 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003707 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003708 slave_name = ptsname(master_fd); /* get name of slave */
3709 if (slave_name == NULL)
3710 return posix_error();
3711 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3712 if (slave_fd < 0)
3713 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003714#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003715 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3716 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003717#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003718 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003719#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003720#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003721#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003722
Fred Drake8cef4cf2000-06-28 16:40:38 +00003723 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003724
Fred Drake8cef4cf2000-06-28 16:40:38 +00003725}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003726#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003727
3728#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003729PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003730"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003731Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3732Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003733To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003734
3735static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003736posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003737{
Christian Heimes951cc0f2008-01-31 23:08:23 +00003738 int master_fd = -1;
3739 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003740
Fred Drake8cef4cf2000-06-28 16:40:38 +00003741 pid = forkpty(&master_fd, NULL, NULL, NULL);
3742 if (pid == -1)
3743 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003744 if (pid == 0)
3745 PyOS_AfterFork();
Christian Heimes951cc0f2008-01-31 23:08:23 +00003746 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003747}
3748#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003749
Guido van Rossumad0ee831995-03-01 10:34:45 +00003750#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003751PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003752"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003753Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003754
Barry Warsaw53699e91996-12-10 23:23:01 +00003755static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003756posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003757{
Barry Warsaw53699e91996-12-10 23:23:01 +00003758 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003759}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003760#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003762
Guido van Rossumad0ee831995-03-01 10:34:45 +00003763#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003764PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003765"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003766Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003767
Barry Warsaw53699e91996-12-10 23:23:01 +00003768static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003769posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003770{
Barry Warsaw53699e91996-12-10 23:23:01 +00003771 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003772}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003773#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003774
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003775
Guido van Rossumad0ee831995-03-01 10:34:45 +00003776#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003777PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003778"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779Return the current process's group 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_getgid(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)getgid());
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003790"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003792
Barry Warsaw53699e91996-12-10 23:23:01 +00003793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003794posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003795{
Barry Warsaw53699e91996-12-10 23:23:01 +00003796 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003797}
3798
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003799
Fred Drakec9680921999-12-13 16:37:25 +00003800#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003801PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003802"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003803Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003804
3805static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003806posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003807{
3808 PyObject *result = NULL;
3809
Fred Drakec9680921999-12-13 16:37:25 +00003810#ifdef NGROUPS_MAX
3811#define MAX_GROUPS NGROUPS_MAX
3812#else
3813 /* defined to be 16 on Solaris7, so this should be a small number */
3814#define MAX_GROUPS 64
3815#endif
3816 gid_t grouplist[MAX_GROUPS];
3817 int n;
3818
3819 n = getgroups(MAX_GROUPS, grouplist);
3820 if (n < 0)
3821 posix_error();
3822 else {
3823 result = PyList_New(n);
3824 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003825 int i;
3826 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003827 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003828 if (o == NULL) {
3829 Py_DECREF(result);
3830 result = NULL;
3831 break;
3832 }
3833 PyList_SET_ITEM(result, i, o);
3834 }
3835 }
3836 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003837
Fred Drakec9680921999-12-13 16:37:25 +00003838 return result;
3839}
3840#endif
3841
Martin v. Löwis606edc12002-06-13 21:09:11 +00003842#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003843PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003844"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003845Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003846
3847static PyObject *
3848posix_getpgid(PyObject *self, PyObject *args)
3849{
3850 int pid, pgid;
3851 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3852 return NULL;
3853 pgid = getpgid(pid);
3854 if (pgid < 0)
3855 return posix_error();
3856 return PyInt_FromLong((long)pgid);
3857}
3858#endif /* HAVE_GETPGID */
3859
3860
Guido van Rossumb6775db1994-08-01 11:34:53 +00003861#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003862PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003863"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003864Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003865
Barry Warsaw53699e91996-12-10 23:23:01 +00003866static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003867posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003868{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003869#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003870 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003871#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003872 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003873#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003874}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003875#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003876
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003877
Guido van Rossumb6775db1994-08-01 11:34:53 +00003878#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003879PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003880"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003881Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003882
Barry Warsaw53699e91996-12-10 23:23:01 +00003883static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003884posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003885{
Guido van Rossum64933891994-10-20 21:56:42 +00003886#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003887 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003888#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003889 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003890#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003891 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003892 Py_INCREF(Py_None);
3893 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003894}
3895
Guido van Rossumb6775db1994-08-01 11:34:53 +00003896#endif /* HAVE_SETPGRP */
3897
Guido van Rossumad0ee831995-03-01 10:34:45 +00003898#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003899PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003900"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003901Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003902
Barry Warsaw53699e91996-12-10 23:23:01 +00003903static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003904posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003905{
Christian Heimesd491d712008-02-01 18:49:26 +00003906 return PyInt_FromLong(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003907}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003908#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003909
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003910
Fred Drake12c6e2d1999-12-14 21:25:03 +00003911#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003912PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003913"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003914Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003915
3916static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003917posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003918{
Neal Norwitze241ce82003-02-17 18:17:05 +00003919 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003920 char *name;
3921 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003922
Fred Drakea30680b2000-12-06 21:24:28 +00003923 errno = 0;
3924 name = getlogin();
3925 if (name == NULL) {
3926 if (errno)
3927 posix_error();
3928 else
3929 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003930 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003931 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003932 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003933 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003934 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003935
Fred Drake12c6e2d1999-12-14 21:25:03 +00003936 return result;
3937}
3938#endif
3939
Guido van Rossumad0ee831995-03-01 10:34:45 +00003940#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003941PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003942"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003943Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003944
Barry Warsaw53699e91996-12-10 23:23:01 +00003945static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003946posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003947{
Barry Warsaw53699e91996-12-10 23:23:01 +00003948 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003949}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003950#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003951
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003952
Guido van Rossumad0ee831995-03-01 10:34:45 +00003953#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003954PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003955"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003956Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003957
Barry Warsaw53699e91996-12-10 23:23:01 +00003958static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003959posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003960{
Christian Heimesd491d712008-02-01 18:49:26 +00003961 pid_t pid;
3962 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003963 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003964 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003965#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003966 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3967 APIRET rc;
3968 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003969 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003970
3971 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3972 APIRET rc;
3973 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003974 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003975
3976 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003977 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003978#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003979 if (kill(pid, sig) == -1)
3980 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003981#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003982 Py_INCREF(Py_None);
3983 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003984}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003985#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003986
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003987#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003988PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003989"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003990Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003991
3992static PyObject *
3993posix_killpg(PyObject *self, PyObject *args)
3994{
3995 int pgid, sig;
3996 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3997 return NULL;
3998 if (killpg(pgid, sig) == -1)
3999 return posix_error();
4000 Py_INCREF(Py_None);
4001 return Py_None;
4002}
4003#endif
4004
Guido van Rossumc0125471996-06-28 18:55:32 +00004005#ifdef HAVE_PLOCK
4006
4007#ifdef HAVE_SYS_LOCK_H
4008#include <sys/lock.h>
4009#endif
4010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004011PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004012"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004013Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004014
Barry Warsaw53699e91996-12-10 23:23:01 +00004015static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004016posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004017{
4018 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004019 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004020 return NULL;
4021 if (plock(op) == -1)
4022 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004023 Py_INCREF(Py_None);
4024 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004025}
4026#endif
4027
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004028
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004029#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004030PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004031"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004032Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004033
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004034#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004035#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004036static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004037async_system(const char *command)
4038{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004039 char errormsg[256], args[1024];
4040 RESULTCODES rcodes;
4041 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004042
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004043 char *shell = getenv("COMSPEC");
4044 if (!shell)
4045 shell = "cmd";
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004046
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004047 /* avoid overflowing the argument buffer */
4048 if (strlen(shell) + 3 + strlen(command) >= 1024)
4049 return ERROR_NOT_ENOUGH_MEMORY
4050
4051 args[0] = '\0';
4052 strcat(args, shell);
4053 strcat(args, "/c ");
4054 strcat(args, command);
4055
4056 /* execute asynchronously, inheriting the environment */
4057 rc = DosExecPgm(errormsg,
4058 sizeof(errormsg),
4059 EXEC_ASYNC,
4060 args,
4061 NULL,
4062 &rcodes,
4063 shell);
4064 return rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004065}
4066
Guido van Rossumd48f2521997-12-05 22:19:34 +00004067static FILE *
4068popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004069{
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004070 int oldfd, tgtfd;
4071 HFILE pipeh[2];
4072 APIRET rc;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004073
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004074 /* mode determines which of stdin or stdout is reconnected to
4075 * the pipe to the child
4076 */
4077 if (strchr(mode, 'r') != NULL) {
4078 tgt_fd = 1; /* stdout */
4079 } else if (strchr(mode, 'w')) {
4080 tgt_fd = 0; /* stdin */
4081 } else {
4082 *err = ERROR_INVALID_ACCESS;
4083 return NULL;
4084 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004085
Andrew MacIntyrea3be2582004-12-18 09:51:05 +00004086 /* setup the pipe */
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004087 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
4088 *err = rc;
4089 return NULL;
4090 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004091
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004092 /* prevent other threads accessing stdio */
4093 DosEnterCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004094
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004095 /* reconnect stdio and execute child */
4096 oldfd = dup(tgtfd);
4097 close(tgtfd);
4098 if (dup2(pipeh[tgtfd], tgtfd) == 0) {
4099 DosClose(pipeh[tgtfd]);
4100 rc = async_system(command);
4101 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004102
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004103 /* restore stdio */
4104 dup2(oldfd, tgtfd);
4105 close(oldfd);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004106
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004107 /* allow other threads access to stdio */
4108 DosExitCritSec();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004109
Andrew MacIntyrea4a8afb2004-12-12 08:30:51 +00004110 /* if execution of child was successful return file stream */
4111 if (rc == NO_ERROR)
4112 return fdopen(pipeh[1 - tgtfd], mode);
4113 else {
4114 DosClose(pipeh[1 - tgtfd]);
4115 *err = rc;
4116 return NULL;
4117 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004118}
4119
4120static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004121posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004122{
4123 char *name;
4124 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00004125 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004126 FILE *fp;
4127 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004128 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004129 return NULL;
4130 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00004131 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004132 Py_END_ALLOW_THREADS
4133 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004134 return os2_error(err);
4135
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004136 f = PyFile_FromFile(fp, name, mode, fclose);
4137 if (f != NULL)
4138 PyFile_SetBufSize(f, bufsize);
4139 return f;
4140}
4141
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004142#elif defined(PYCC_GCC)
4143
4144/* standard posix version of popen() support */
4145static PyObject *
4146posix_popen(PyObject *self, PyObject *args)
4147{
4148 char *name;
4149 char *mode = "r";
4150 int bufsize = -1;
4151 FILE *fp;
4152 PyObject *f;
4153 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
4154 return NULL;
4155 Py_BEGIN_ALLOW_THREADS
4156 fp = popen(name, mode);
4157 Py_END_ALLOW_THREADS
4158 if (fp == NULL)
4159 return posix_error();
4160 f = PyFile_FromFile(fp, name, mode, pclose);
4161 if (f != NULL)
4162 PyFile_SetBufSize(f, bufsize);
4163 return f;
4164}
4165
4166/* fork() under OS/2 has lots'o'warts
4167 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4168 * most of this code is a ripoff of the win32 code, but using the
4169 * capabilities of EMX's C library routines
4170 */
4171
4172/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4173#define POPEN_1 1
4174#define POPEN_2 2
4175#define POPEN_3 3
4176#define POPEN_4 4
4177
4178static PyObject *_PyPopen(char *, int, int, int);
4179static int _PyPclose(FILE *file);
4180
4181/*
4182 * Internal dictionary mapping popen* file pointers to process handles,
4183 * for use when retrieving the process exit code. See _PyPclose() below
4184 * for more information on this dictionary's use.
4185 */
4186static PyObject *_PyPopenProcs = NULL;
4187
4188/* os2emx version of popen2()
4189 *
4190 * The result of this function is a pipe (file) connected to the
4191 * process's stdin, and a pipe connected to the process's stdout.
4192 */
4193
4194static PyObject *
4195os2emx_popen2(PyObject *self, PyObject *args)
4196{
4197 PyObject *f;
4198 int tm=0;
4199
4200 char *cmdstring;
4201 char *mode = "t";
4202 int bufsize = -1;
4203 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
4204 return NULL;
4205
4206 if (*mode == 't')
4207 tm = O_TEXT;
4208 else if (*mode != 'b') {
4209 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4210 return NULL;
4211 } else
4212 tm = O_BINARY;
4213
4214 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
4215
4216 return f;
4217}
4218
4219/*
4220 * Variation on os2emx.popen2
4221 *
4222 * The result of this function is 3 pipes - the process's stdin,
4223 * stdout and stderr
4224 */
4225
4226static PyObject *
4227os2emx_popen3(PyObject *self, PyObject *args)
4228{
4229 PyObject *f;
4230 int tm = 0;
4231
4232 char *cmdstring;
4233 char *mode = "t";
4234 int bufsize = -1;
4235 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
4236 return NULL;
4237
4238 if (*mode == 't')
4239 tm = O_TEXT;
4240 else if (*mode != 'b') {
4241 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4242 return NULL;
4243 } else
4244 tm = O_BINARY;
4245
4246 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
4247
4248 return f;
4249}
4250
4251/*
4252 * Variation on os2emx.popen2
4253 *
Tim Peters11b23062003-04-23 02:39:17 +00004254 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004255 * and stdout+stderr combined as a single pipe.
4256 */
4257
4258static PyObject *
4259os2emx_popen4(PyObject *self, PyObject *args)
4260{
4261 PyObject *f;
4262 int tm = 0;
4263
4264 char *cmdstring;
4265 char *mode = "t";
4266 int bufsize = -1;
4267 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
4268 return NULL;
4269
4270 if (*mode == 't')
4271 tm = O_TEXT;
4272 else if (*mode != 'b') {
4273 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
4274 return NULL;
4275 } else
4276 tm = O_BINARY;
4277
4278 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
4279
4280 return f;
4281}
4282
4283/* a couple of structures for convenient handling of multiple
4284 * file handles and pipes
4285 */
4286struct file_ref
4287{
4288 int handle;
4289 int flags;
4290};
4291
4292struct pipe_ref
4293{
4294 int rd;
4295 int wr;
4296};
4297
4298/* The following code is derived from the win32 code */
4299
4300static PyObject *
4301_PyPopen(char *cmdstring, int mode, int n, int bufsize)
4302{
4303 struct file_ref stdio[3];
4304 struct pipe_ref p_fd[3];
4305 FILE *p_s[3];
Christian Heimesd491d712008-02-01 18:49:26 +00004306 int file_count, i, pipe_err;
4307 pid_t pipe_pid;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004308 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
4309 PyObject *f, *p_f[3];
4310
4311 /* file modes for subsequent fdopen's on pipe handles */
4312 if (mode == O_TEXT)
4313 {
4314 rd_mode = "rt";
4315 wr_mode = "wt";
4316 }
4317 else
4318 {
4319 rd_mode = "rb";
4320 wr_mode = "wb";
4321 }
4322
4323 /* prepare shell references */
4324 if ((shell = getenv("EMXSHELL")) == NULL)
4325 if ((shell = getenv("COMSPEC")) == NULL)
4326 {
4327 errno = ENOENT;
4328 return posix_error();
4329 }
4330
4331 sh_name = _getname(shell);
4332 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
4333 opt = "/c";
4334 else
4335 opt = "-c";
4336
4337 /* save current stdio fds + their flags, and set not inheritable */
4338 i = pipe_err = 0;
4339 while (pipe_err >= 0 && i < 3)
4340 {
4341 pipe_err = stdio[i].handle = dup(i);
4342 stdio[i].flags = fcntl(i, F_GETFD, 0);
4343 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
4344 i++;
4345 }
4346 if (pipe_err < 0)
4347 {
4348 /* didn't get them all saved - clean up and bail out */
4349 int saved_err = errno;
4350 while (i-- > 0)
4351 {
4352 close(stdio[i].handle);
4353 }
4354 errno = saved_err;
4355 return posix_error();
4356 }
4357
4358 /* create pipe ends */
4359 file_count = 2;
4360 if (n == POPEN_3)
4361 file_count = 3;
4362 i = pipe_err = 0;
4363 while ((pipe_err == 0) && (i < file_count))
4364 pipe_err = pipe((int *)&p_fd[i++]);
4365 if (pipe_err < 0)
4366 {
4367 /* didn't get them all made - clean up and bail out */
4368 while (i-- > 0)
4369 {
4370 close(p_fd[i].wr);
4371 close(p_fd[i].rd);
4372 }
4373 errno = EPIPE;
4374 return posix_error();
4375 }
4376
4377 /* change the actual standard IO streams over temporarily,
4378 * making the retained pipe ends non-inheritable
4379 */
4380 pipe_err = 0;
4381
4382 /* - stdin */
4383 if (dup2(p_fd[0].rd, 0) == 0)
4384 {
4385 close(p_fd[0].rd);
4386 i = fcntl(p_fd[0].wr, F_GETFD, 0);
4387 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
4388 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
4389 {
4390 close(p_fd[0].wr);
4391 pipe_err = -1;
4392 }
4393 }
4394 else
4395 {
4396 pipe_err = -1;
4397 }
4398
4399 /* - stdout */
4400 if (pipe_err == 0)
4401 {
4402 if (dup2(p_fd[1].wr, 1) == 1)
4403 {
4404 close(p_fd[1].wr);
4405 i = fcntl(p_fd[1].rd, F_GETFD, 0);
4406 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
4407 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
4408 {
4409 close(p_fd[1].rd);
4410 pipe_err = -1;
4411 }
4412 }
4413 else
4414 {
4415 pipe_err = -1;
4416 }
4417 }
4418
4419 /* - stderr, as required */
4420 if (pipe_err == 0)
4421 switch (n)
4422 {
4423 case POPEN_3:
4424 {
4425 if (dup2(p_fd[2].wr, 2) == 2)
4426 {
4427 close(p_fd[2].wr);
4428 i = fcntl(p_fd[2].rd, F_GETFD, 0);
4429 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
4430 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
4431 {
4432 close(p_fd[2].rd);
4433 pipe_err = -1;
4434 }
4435 }
4436 else
4437 {
4438 pipe_err = -1;
4439 }
4440 break;
4441 }
4442
4443 case POPEN_4:
4444 {
4445 if (dup2(1, 2) != 2)
4446 {
4447 pipe_err = -1;
4448 }
4449 break;
4450 }
4451 }
4452
4453 /* spawn the child process */
4454 if (pipe_err == 0)
4455 {
4456 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
4457 if (pipe_pid == -1)
4458 {
4459 pipe_err = -1;
4460 }
4461 else
4462 {
4463 /* save the PID into the FILE structure
4464 * NOTE: this implementation doesn't actually
4465 * take advantage of this, but do it for
4466 * completeness - AIM Apr01
4467 */
4468 for (i = 0; i < file_count; i++)
4469 p_s[i]->_pid = pipe_pid;
4470 }
4471 }
4472
4473 /* reset standard IO to normal */
4474 for (i = 0; i < 3; i++)
4475 {
4476 dup2(stdio[i].handle, i);
4477 fcntl(i, F_SETFD, stdio[i].flags);
4478 close(stdio[i].handle);
4479 }
4480
4481 /* if any remnant problems, clean up and bail out */
4482 if (pipe_err < 0)
4483 {
4484 for (i = 0; i < 3; i++)
4485 {
4486 close(p_fd[i].rd);
4487 close(p_fd[i].wr);
4488 }
4489 errno = EPIPE;
4490 return posix_error_with_filename(cmdstring);
4491 }
4492
4493 /* build tuple of file objects to return */
4494 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
4495 PyFile_SetBufSize(p_f[0], bufsize);
4496 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
4497 PyFile_SetBufSize(p_f[1], bufsize);
4498 if (n == POPEN_3)
4499 {
4500 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
4501 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004502 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004503 }
4504 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004505 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004506
4507 /*
4508 * Insert the files we've created into the process dictionary
4509 * all referencing the list with the process handle and the
4510 * initial number of files (see description below in _PyPclose).
4511 * Since if _PyPclose later tried to wait on a process when all
4512 * handles weren't closed, it could create a deadlock with the
4513 * child, we spend some energy here to try to ensure that we
4514 * either insert all file handles into the dictionary or none
4515 * at all. It's a little clumsy with the various popen modes
4516 * and variable number of files involved.
4517 */
4518 if (!_PyPopenProcs)
4519 {
4520 _PyPopenProcs = PyDict_New();
4521 }
4522
4523 if (_PyPopenProcs)
4524 {
4525 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
4526 int ins_rc[3];
4527
4528 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4529 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4530
4531 procObj = PyList_New(2);
4532 pidObj = PyInt_FromLong((long) pipe_pid);
4533 intObj = PyInt_FromLong((long) file_count);
4534
4535 if (procObj && pidObj && intObj)
4536 {
4537 PyList_SetItem(procObj, 0, pidObj);
4538 PyList_SetItem(procObj, 1, intObj);
4539
4540 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
4541 if (fileObj[0])
4542 {
4543 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4544 fileObj[0],
4545 procObj);
4546 }
4547 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
4548 if (fileObj[1])
4549 {
4550 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4551 fileObj[1],
4552 procObj);
4553 }
4554 if (file_count >= 3)
4555 {
4556 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
4557 if (fileObj[2])
4558 {
4559 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4560 fileObj[2],
4561 procObj);
4562 }
4563 }
4564
4565 if (ins_rc[0] < 0 || !fileObj[0] ||
4566 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4567 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
4568 {
4569 /* Something failed - remove any dictionary
4570 * entries that did make it.
4571 */
4572 if (!ins_rc[0] && fileObj[0])
4573 {
4574 PyDict_DelItem(_PyPopenProcs,
4575 fileObj[0]);
4576 }
4577 if (!ins_rc[1] && fileObj[1])
4578 {
4579 PyDict_DelItem(_PyPopenProcs,
4580 fileObj[1]);
4581 }
4582 if (!ins_rc[2] && fileObj[2])
4583 {
4584 PyDict_DelItem(_PyPopenProcs,
4585 fileObj[2]);
4586 }
4587 }
4588 }
Tim Peters11b23062003-04-23 02:39:17 +00004589
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004590 /*
4591 * Clean up our localized references for the dictionary keys
4592 * and value since PyDict_SetItem will Py_INCREF any copies
4593 * that got placed in the dictionary.
4594 */
4595 Py_XDECREF(procObj);
4596 Py_XDECREF(fileObj[0]);
4597 Py_XDECREF(fileObj[1]);
4598 Py_XDECREF(fileObj[2]);
4599 }
4600
4601 /* Child is launched. */
4602 return f;
4603}
4604
4605/*
4606 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4607 * exit code for the child process and return as a result of the close.
4608 *
4609 * This function uses the _PyPopenProcs dictionary in order to map the
4610 * input file pointer to information about the process that was
4611 * originally created by the popen* call that created the file pointer.
4612 * The dictionary uses the file pointer as a key (with one entry
4613 * inserted for each file returned by the original popen* call) and a
4614 * single list object as the value for all files from a single call.
4615 * The list object contains the Win32 process handle at [0], and a file
4616 * count at [1], which is initialized to the total number of file
4617 * handles using that list.
4618 *
4619 * This function closes whichever handle it is passed, and decrements
4620 * the file count in the dictionary for the process handle pointed to
4621 * by this file. On the last close (when the file count reaches zero),
4622 * this function will wait for the child process and then return its
4623 * exit code as the result of the close() operation. This permits the
4624 * files to be closed in any order - it is always the close() of the
4625 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004626 *
4627 * NOTE: This function is currently called with the GIL released.
4628 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004629 */
4630
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004631static int _PyPclose(FILE *file)
4632{
4633 int result;
4634 int exit_code;
Christian Heimesd491d712008-02-01 18:49:26 +00004635 pid_t pipe_pid;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004636 PyObject *procObj, *pidObj, *intObj, *fileObj;
4637 int file_count;
4638#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004639 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004640#endif
4641
4642 /* Close the file handle first, to ensure it can't block the
4643 * child from exiting if it's the last handle.
4644 */
4645 result = fclose(file);
4646
4647#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004648 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004649#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004650 if (_PyPopenProcs)
4651 {
4652 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4653 (procObj = PyDict_GetItem(_PyPopenProcs,
4654 fileObj)) != NULL &&
4655 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
4656 (intObj = PyList_GetItem(procObj,1)) != NULL)
4657 {
4658 pipe_pid = (int) PyInt_AsLong(pidObj);
4659 file_count = (int) PyInt_AsLong(intObj);
4660
4661 if (file_count > 1)
4662 {
4663 /* Still other files referencing process */
4664 file_count--;
4665 PyList_SetItem(procObj,1,
4666 PyInt_FromLong((long) file_count));
4667 }
4668 else
4669 {
4670 /* Last file for this process */
4671 if (result != EOF &&
4672 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
4673 {
4674 /* extract exit status */
4675 if (WIFEXITED(exit_code))
4676 {
4677 result = WEXITSTATUS(exit_code);
4678 }
4679 else
4680 {
4681 errno = EPIPE;
4682 result = -1;
4683 }
4684 }
4685 else
4686 {
4687 /* Indicate failure - this will cause the file object
4688 * to raise an I/O error and translate the last
4689 * error code from errno. We do have a problem with
4690 * last errors that overlap the normal errno table,
4691 * but that's a consistent problem with the file object.
4692 */
4693 result = -1;
4694 }
4695 }
4696
4697 /* Remove this file pointer from dictionary */
4698 PyDict_DelItem(_PyPopenProcs, fileObj);
4699
4700 if (PyDict_Size(_PyPopenProcs) == 0)
4701 {
4702 Py_DECREF(_PyPopenProcs);
4703 _PyPopenProcs = NULL;
4704 }
4705
4706 } /* if object retrieval ok */
4707
4708 Py_XDECREF(fileObj);
4709 } /* if _PyPopenProcs */
4710
4711#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00004712 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004713#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004714 return result;
4715}
4716
4717#endif /* PYCC_??? */
4718
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004719#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004720
4721/*
4722 * Portable 'popen' replacement for Win32.
4723 *
4724 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4725 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00004726 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004727 */
4728
4729#include <malloc.h>
4730#include <io.h>
4731#include <fcntl.h>
4732
4733/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4734#define POPEN_1 1
4735#define POPEN_2 2
4736#define POPEN_3 3
4737#define POPEN_4 4
4738
4739static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004740static int _PyPclose(FILE *file);
4741
4742/*
4743 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00004744 * for use when retrieving the process exit code. See _PyPclose() below
4745 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004746 */
4747static PyObject *_PyPopenProcs = NULL;
4748
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004749
4750/* popen that works from a GUI.
4751 *
4752 * The result of this function is a pipe (file) connected to the
4753 * processes stdin or stdout, depending on the requested mode.
4754 */
4755
4756static PyObject *
4757posix_popen(PyObject *self, PyObject *args)
4758{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00004759 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004760 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004761
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004762 char *cmdstring;
4763 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004764 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004765 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004766 return NULL;
4767
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004768 if (*mode == 'r')
4769 tm = _O_RDONLY;
4770 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00004771 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004772 return NULL;
4773 } else
4774 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00004775
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004776 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004777 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004778 return NULL;
4779 }
4780
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004781 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004782 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004783 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004784 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004785 else
4786 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
4787
4788 return f;
4789}
4790
4791/* Variation on win32pipe.popen
4792 *
4793 * The result of this function is a pipe (file) connected to the
4794 * process's stdin, and a pipe connected to the process's stdout.
4795 */
4796
4797static PyObject *
4798win32_popen2(PyObject *self, PyObject *args)
4799{
4800 PyObject *f;
4801 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00004802
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004803 char *cmdstring;
4804 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004805 int bufsize = -1;
4806 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004807 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004808
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004809 if (*mode == 't')
4810 tm = _O_TEXT;
4811 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004812 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004813 return NULL;
4814 } else
4815 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004816
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004817 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004818 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004819 return NULL;
4820 }
4821
4822 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00004823
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004824 return f;
4825}
4826
4827/*
4828 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004829 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004830 * The result of this function is 3 pipes - the process's stdin,
4831 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004832 */
4833
4834static PyObject *
4835win32_popen3(PyObject *self, PyObject *args)
4836{
4837 PyObject *f;
4838 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004839
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004840 char *cmdstring;
4841 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004842 int bufsize = -1;
4843 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004844 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004845
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004846 if (*mode == 't')
4847 tm = _O_TEXT;
4848 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004849 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004850 return NULL;
4851 } else
4852 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00004853
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004854 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004855 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004856 return NULL;
4857 }
4858
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004859 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00004860
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004861 return f;
4862}
4863
4864/*
4865 * Variation on win32pipe.popen
4866 *
Tim Peters5aa91602002-01-30 05:46:57 +00004867 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004868 * and stdout+stderr combined as a single pipe.
4869 */
4870
4871static PyObject *
4872win32_popen4(PyObject *self, PyObject *args)
4873{
4874 PyObject *f;
4875 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00004876
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004877 char *cmdstring;
4878 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004879 int bufsize = -1;
4880 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004881 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00004882
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004883 if (*mode == 't')
4884 tm = _O_TEXT;
4885 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00004886 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004887 return NULL;
4888 } else
4889 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004890
4891 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00004892 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004893 return NULL;
4894 }
4895
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00004896 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00004897
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004898 return f;
4899}
4900
Mark Hammond08501372001-01-31 07:30:29 +00004901static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00004902_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004903 HANDLE hStdin,
4904 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00004905 HANDLE hStderr,
4906 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004907{
4908 PROCESS_INFORMATION piProcInfo;
4909 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004910 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004911 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00004912 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004913 int i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004914 Py_ssize_t x;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004915
4916 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00004917 char *comshell;
4918
Tim Peters92e4dd82002-10-05 01:47:34 +00004919 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004920 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
Martin v. Löwis18e16552006-02-15 17:27:45 +00004921 /* x < i, so x fits into an integer */
4922 return (int)x;
Tim Peters402d5982001-08-27 06:37:48 +00004923
4924 /* Explicitly check if we are using COMMAND.COM. If we are
4925 * then use the w9xpopen hack.
4926 */
4927 comshell = s1 + x;
4928 while (comshell >= s1 && *comshell != '\\')
4929 --comshell;
4930 ++comshell;
4931
4932 if (GetVersion() < 0x80000000 &&
4933 _stricmp(comshell, "command.com") != 0) {
4934 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004935 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00004936 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004937 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00004938 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004939 }
4940 else {
4941 /*
Tim Peters402d5982001-08-27 06:37:48 +00004942 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4943 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004944 */
Mark Hammond08501372001-01-31 07:30:29 +00004945 char modulepath[_MAX_PATH];
4946 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004947 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
Martin v. Löwis26fd9602006-04-22 11:15:41 +00004948 for (x = i = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004949 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004950 x = i+1;
4951 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004952 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00004953 strncat(modulepath,
4954 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00004955 (sizeof(modulepath)/sizeof(modulepath[0]))
4956 -strlen(modulepath));
4957 if (stat(modulepath, &statinfo) != 0) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004958 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
Tim Peters5aa91602002-01-30 05:46:57 +00004959 /* Eeek - file-not-found - possibly an embedding
4960 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00004961 */
Tim Peters5aa91602002-01-30 05:46:57 +00004962 strncpy(modulepath,
4963 Py_GetExecPrefix(),
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004964 mplen);
4965 modulepath[mplen-1] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00004966 if (modulepath[strlen(modulepath)-1] != '\\')
4967 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00004968 strncat(modulepath,
4969 szConsoleSpawn,
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004970 mplen-strlen(modulepath));
Mark Hammond08501372001-01-31 07:30:29 +00004971 /* No where else to look - raise an easily identifiable
4972 error, rather than leaving Windows to report
4973 "file not found" - as the user is probably blissfully
4974 unaware this shim EXE is used, and it will confuse them.
4975 (well, it confused me for a while ;-)
4976 */
4977 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00004978 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00004979 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00004980 "for popen to work with your shell "
4981 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00004982 szConsoleSpawn);
4983 return FALSE;
4984 }
4985 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004986 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00004987 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004988 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00004989
Tim Peters92e4dd82002-10-05 01:47:34 +00004990 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004991 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00004992 /* To maintain correct argument passing semantics,
4993 we pass the command-line as it stands, and allow
4994 quoting to be applied. w9xpopen.exe will then
4995 use its argv vector, and re-quote the necessary
4996 args for the ultimate child process.
4997 */
Tim Peters75cdad52001-11-28 22:07:30 +00004998 PyOS_snprintf(
4999 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00005000 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005001 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005002 s1,
5003 s3,
5004 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005005 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00005006 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005007 dialog:
5008 "Your program accessed mem currently in use at xxx"
5009 and a hopeful warning about the stability of your
5010 system.
5011 Cost is Ctrl+C wont kill children, but anyone
5012 who cares can have a go!
5013 */
5014 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005015 }
5016 }
5017
5018 /* Could be an else here to try cmd.exe / command.com in the path
5019 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00005020 else {
Tim Peters402d5982001-08-27 06:37:48 +00005021 PyErr_SetString(PyExc_RuntimeError,
5022 "Cannot locate a COMSPEC environment variable to "
5023 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00005024 return FALSE;
5025 }
Tim Peters5aa91602002-01-30 05:46:57 +00005026
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005027 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
5028 siStartInfo.cb = sizeof(STARTUPINFO);
5029 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
5030 siStartInfo.hStdInput = hStdin;
5031 siStartInfo.hStdOutput = hStdout;
5032 siStartInfo.hStdError = hStderr;
5033 siStartInfo.wShowWindow = SW_HIDE;
5034
5035 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005036 s2,
5037 NULL,
5038 NULL,
5039 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005040 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005041 NULL,
5042 NULL,
5043 &siStartInfo,
5044 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005045 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005046 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005047
Mark Hammondb37a3732000-08-14 04:47:33 +00005048 /* Return process handle */
5049 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005050 return TRUE;
5051 }
Tim Peters402d5982001-08-27 06:37:48 +00005052 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005053 return FALSE;
5054}
5055
5056/* The following code is based off of KB: Q190351 */
5057
5058static PyObject *
5059_PyPopen(char *cmdstring, int mode, int n)
5060{
5061 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
5062 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00005063 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00005064
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005065 SECURITY_ATTRIBUTES saAttr;
5066 BOOL fSuccess;
5067 int fd1, fd2, fd3;
5068 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00005069 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005070 PyObject *f;
5071
5072 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
5073 saAttr.bInheritHandle = TRUE;
5074 saAttr.lpSecurityDescriptor = NULL;
5075
5076 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
5077 return win32_error("CreatePipe", NULL);
5078
5079 /* Create new output read handle and the input write handle. Set
5080 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005081 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005082 * being created. */
5083 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005084 GetCurrentProcess(), &hChildStdinWrDup, 0,
5085 FALSE,
5086 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005087 if (!fSuccess)
5088 return win32_error("DuplicateHandle", NULL);
5089
5090 /* Close the inheritable version of ChildStdin
5091 that we're using. */
5092 CloseHandle(hChildStdinWr);
5093
5094 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
5095 return win32_error("CreatePipe", NULL);
5096
5097 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005098 GetCurrentProcess(), &hChildStdoutRdDup, 0,
5099 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005100 if (!fSuccess)
5101 return win32_error("DuplicateHandle", NULL);
5102
5103 /* Close the inheritable version of ChildStdout
5104 that we're using. */
5105 CloseHandle(hChildStdoutRd);
5106
5107 if (n != POPEN_4) {
5108 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
5109 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005110 fSuccess = DuplicateHandle(GetCurrentProcess(),
5111 hChildStderrRd,
5112 GetCurrentProcess(),
5113 &hChildStderrRdDup, 0,
5114 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005115 if (!fSuccess)
5116 return win32_error("DuplicateHandle", NULL);
5117 /* Close the inheritable version of ChildStdErr that we're using. */
5118 CloseHandle(hChildStderrRd);
5119 }
Tim Peters5aa91602002-01-30 05:46:57 +00005120
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005121 switch (n) {
5122 case POPEN_1:
5123 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
5124 case _O_WRONLY | _O_TEXT:
5125 /* Case for writing to child Stdin in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005126 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005127 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005128 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005129 PyFile_SetBufSize(f, 0);
5130 /* We don't care about these pipes anymore, so close them. */
5131 CloseHandle(hChildStdoutRdDup);
5132 CloseHandle(hChildStderrRdDup);
5133 break;
5134
5135 case _O_RDONLY | _O_TEXT:
5136 /* Case for reading from child Stdout in text mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005137 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005138 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005139 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005140 PyFile_SetBufSize(f, 0);
5141 /* We don't care about these pipes anymore, so close them. */
5142 CloseHandle(hChildStdinWrDup);
5143 CloseHandle(hChildStderrRdDup);
5144 break;
5145
5146 case _O_RDONLY | _O_BINARY:
5147 /* Case for readinig from child Stdout in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005148 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005149 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005150 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005151 PyFile_SetBufSize(f, 0);
5152 /* We don't care about these pipes anymore, so close them. */
5153 CloseHandle(hChildStdinWrDup);
5154 CloseHandle(hChildStderrRdDup);
5155 break;
5156
5157 case _O_WRONLY | _O_BINARY:
5158 /* Case for writing to child Stdin in binary mode. */
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005159 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005160 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00005161 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005162 PyFile_SetBufSize(f, 0);
5163 /* We don't care about these pipes anymore, so close them. */
5164 CloseHandle(hChildStdoutRdDup);
5165 CloseHandle(hChildStderrRdDup);
5166 break;
5167 }
Mark Hammondb37a3732000-08-14 04:47:33 +00005168 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005169 break;
Tim Peters5aa91602002-01-30 05:46:57 +00005170
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005171 case POPEN_2:
5172 case POPEN_4:
5173 {
5174 char *m1, *m2;
5175 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00005176
Tim Peters7dca21e2002-08-19 00:42:29 +00005177 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005178 m1 = "r";
5179 m2 = "w";
5180 } else {
5181 m1 = "rb";
5182 m2 = "wb";
5183 }
5184
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005185 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005186 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005187 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005188 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005189 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005190 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00005191 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005192 PyFile_SetBufSize(p2, 0);
5193
5194 if (n != 4)
5195 CloseHandle(hChildStderrRdDup);
5196
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005197 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00005198 Py_XDECREF(p1);
5199 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00005200 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005201 break;
5202 }
Tim Peters5aa91602002-01-30 05:46:57 +00005203
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005204 case POPEN_3:
5205 {
5206 char *m1, *m2;
5207 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00005208
Tim Peters7dca21e2002-08-19 00:42:29 +00005209 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005210 m1 = "r";
5211 m2 = "w";
5212 } else {
5213 m1 = "rb";
5214 m2 = "wb";
5215 }
5216
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005217 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005218 f1 = _fdopen(fd1, m2);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005219 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005220 f2 = _fdopen(fd2, m1);
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005221 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005222 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005223 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00005224 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
5225 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005226 PyFile_SetBufSize(p1, 0);
5227 PyFile_SetBufSize(p2, 0);
5228 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005229 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00005230 Py_XDECREF(p1);
5231 Py_XDECREF(p2);
5232 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00005233 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005234 break;
5235 }
5236 }
5237
5238 if (n == POPEN_4) {
5239 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005240 hChildStdinRd,
5241 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005242 hChildStdoutWr,
5243 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005244 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005245 }
5246 else {
5247 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00005248 hChildStdinRd,
5249 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00005250 hChildStderrWr,
5251 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00005252 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005253 }
5254
Mark Hammondb37a3732000-08-14 04:47:33 +00005255 /*
5256 * Insert the files we've created into the process dictionary
5257 * all referencing the list with the process handle and the
5258 * initial number of files (see description below in _PyPclose).
5259 * Since if _PyPclose later tried to wait on a process when all
5260 * handles weren't closed, it could create a deadlock with the
5261 * child, we spend some energy here to try to ensure that we
5262 * either insert all file handles into the dictionary or none
5263 * at all. It's a little clumsy with the various popen modes
5264 * and variable number of files involved.
5265 */
5266 if (!_PyPopenProcs) {
5267 _PyPopenProcs = PyDict_New();
5268 }
5269
5270 if (_PyPopenProcs) {
5271 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
5272 int ins_rc[3];
5273
5274 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
5275 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
5276
5277 procObj = PyList_New(2);
5278 hProcessObj = PyLong_FromVoidPtr(hProcess);
5279 intObj = PyInt_FromLong(file_count);
5280
5281 if (procObj && hProcessObj && intObj) {
5282 PyList_SetItem(procObj,0,hProcessObj);
5283 PyList_SetItem(procObj,1,intObj);
5284
5285 fileObj[0] = PyLong_FromVoidPtr(f1);
5286 if (fileObj[0]) {
5287 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
5288 fileObj[0],
5289 procObj);
5290 }
5291 if (file_count >= 2) {
5292 fileObj[1] = PyLong_FromVoidPtr(f2);
5293 if (fileObj[1]) {
5294 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
5295 fileObj[1],
5296 procObj);
5297 }
5298 }
5299 if (file_count >= 3) {
5300 fileObj[2] = PyLong_FromVoidPtr(f3);
5301 if (fileObj[2]) {
5302 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
5303 fileObj[2],
5304 procObj);
5305 }
5306 }
5307
5308 if (ins_rc[0] < 0 || !fileObj[0] ||
5309 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
5310 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
5311 /* Something failed - remove any dictionary
5312 * entries that did make it.
5313 */
5314 if (!ins_rc[0] && fileObj[0]) {
5315 PyDict_DelItem(_PyPopenProcs,
5316 fileObj[0]);
5317 }
5318 if (!ins_rc[1] && fileObj[1]) {
5319 PyDict_DelItem(_PyPopenProcs,
5320 fileObj[1]);
5321 }
5322 if (!ins_rc[2] && fileObj[2]) {
5323 PyDict_DelItem(_PyPopenProcs,
5324 fileObj[2]);
5325 }
5326 }
5327 }
Tim Peters5aa91602002-01-30 05:46:57 +00005328
Mark Hammondb37a3732000-08-14 04:47:33 +00005329 /*
5330 * Clean up our localized references for the dictionary keys
5331 * and value since PyDict_SetItem will Py_INCREF any copies
5332 * that got placed in the dictionary.
5333 */
5334 Py_XDECREF(procObj);
5335 Py_XDECREF(fileObj[0]);
5336 Py_XDECREF(fileObj[1]);
5337 Py_XDECREF(fileObj[2]);
5338 }
5339
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005340 /* Child is launched. Close the parents copy of those pipe
5341 * handles that only the child should have open. You need to
5342 * make sure that no handles to the write end of the output pipe
5343 * are maintained in this process or else the pipe will not close
5344 * when the child process exits and the ReadFile will hang. */
5345
5346 if (!CloseHandle(hChildStdinRd))
5347 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005348
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005349 if (!CloseHandle(hChildStdoutWr))
5350 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00005351
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005352 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
5353 return win32_error("CloseHandle", NULL);
5354
5355 return f;
5356}
Fredrik Lundh56055a42000-07-23 19:47:12 +00005357
5358/*
5359 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5360 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00005361 *
5362 * This function uses the _PyPopenProcs dictionary in order to map the
5363 * input file pointer to information about the process that was
5364 * originally created by the popen* call that created the file pointer.
5365 * The dictionary uses the file pointer as a key (with one entry
5366 * inserted for each file returned by the original popen* call) and a
5367 * single list object as the value for all files from a single call.
5368 * The list object contains the Win32 process handle at [0], and a file
5369 * count at [1], which is initialized to the total number of file
5370 * handles using that list.
5371 *
5372 * This function closes whichever handle it is passed, and decrements
5373 * the file count in the dictionary for the process handle pointed to
5374 * by this file. On the last close (when the file count reaches zero),
5375 * this function will wait for the child process and then return its
5376 * exit code as the result of the close() operation. This permits the
5377 * files to be closed in any order - it is always the close() of the
5378 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005379 *
5380 * NOTE: This function is currently called with the GIL released.
5381 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005382 */
Tim Peters736aa322000-09-01 06:51:24 +00005383
Fredrik Lundh56055a42000-07-23 19:47:12 +00005384static int _PyPclose(FILE *file)
5385{
Fredrik Lundh20318932000-07-26 17:29:12 +00005386 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005387 DWORD exit_code;
5388 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00005389 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
5390 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00005391#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005392 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00005393#endif
5394
Fredrik Lundh20318932000-07-26 17:29:12 +00005395 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00005396 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00005397 */
5398 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00005399#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005400 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00005401#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005402 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00005403 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
5404 (procObj = PyDict_GetItem(_PyPopenProcs,
5405 fileObj)) != NULL &&
5406 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
5407 (intObj = PyList_GetItem(procObj,1)) != NULL) {
5408
5409 hProcess = PyLong_AsVoidPtr(hProcessObj);
5410 file_count = PyInt_AsLong(intObj);
5411
5412 if (file_count > 1) {
5413 /* Still other files referencing process */
5414 file_count--;
5415 PyList_SetItem(procObj,1,
5416 PyInt_FromLong(file_count));
5417 } else {
5418 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00005419 if (result != EOF &&
5420 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
5421 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00005422 /* Possible truncation here in 16-bit environments, but
5423 * real exit codes are just the lower byte in any event.
5424 */
5425 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005426 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00005427 /* Indicate failure - this will cause the file object
5428 * to raise an I/O error and translate the last Win32
5429 * error code from errno. We do have a problem with
5430 * last errors that overlap the normal errno table,
5431 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00005432 */
Fredrik Lundh20318932000-07-26 17:29:12 +00005433 if (result != EOF) {
5434 /* If the error wasn't from the fclose(), then
5435 * set errno for the file object error handling.
5436 */
5437 errno = GetLastError();
5438 }
5439 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00005440 }
5441
5442 /* Free up the native handle at this point */
5443 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00005444 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00005445
Mark Hammondb37a3732000-08-14 04:47:33 +00005446 /* Remove this file pointer from dictionary */
5447 PyDict_DelItem(_PyPopenProcs, fileObj);
5448
5449 if (PyDict_Size(_PyPopenProcs) == 0) {
5450 Py_DECREF(_PyPopenProcs);
5451 _PyPopenProcs = NULL;
5452 }
5453
5454 } /* if object retrieval ok */
5455
5456 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00005457 } /* if _PyPopenProcs */
5458
Tim Peters736aa322000-09-01 06:51:24 +00005459#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00005460 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00005461#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00005462 return result;
5463}
Tim Peters9acdd3a2000-09-01 19:26:36 +00005464
5465#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00005466static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005467posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00005468{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005469 char *name;
5470 char *mode = "r";
5471 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00005472 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005473 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005474 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00005475 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00005476 /* Strip mode of binary or text modifiers */
5477 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
5478 mode = "r";
5479 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
5480 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00005481 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005482 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005483 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00005484 if (fp == NULL)
5485 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005486 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005487 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005488 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005489 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00005490}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005491
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005492#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005493#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00005494
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005495
Guido van Rossumb6775db1994-08-01 11:34:53 +00005496#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005497PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005498"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005499Set the current process's user id.");
5500
Barry Warsaw53699e91996-12-10 23:23:01 +00005501static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005502posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005503{
5504 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005505 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005506 return NULL;
5507 if (setuid(uid) < 0)
5508 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005509 Py_INCREF(Py_None);
5510 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005511}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005512#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005513
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005514
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005515#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005516PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005517"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005518Set the current process's effective user id.");
5519
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005520static PyObject *
5521posix_seteuid (PyObject *self, PyObject *args)
5522{
5523 int euid;
5524 if (!PyArg_ParseTuple(args, "i", &euid)) {
5525 return NULL;
5526 } else if (seteuid(euid) < 0) {
5527 return posix_error();
5528 } else {
5529 Py_INCREF(Py_None);
5530 return Py_None;
5531 }
5532}
5533#endif /* HAVE_SETEUID */
5534
5535#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005536PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005537"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005538Set the current process's effective group id.");
5539
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005540static PyObject *
5541posix_setegid (PyObject *self, PyObject *args)
5542{
5543 int egid;
5544 if (!PyArg_ParseTuple(args, "i", &egid)) {
5545 return NULL;
5546 } else if (setegid(egid) < 0) {
5547 return posix_error();
5548 } else {
5549 Py_INCREF(Py_None);
5550 return Py_None;
5551 }
5552}
5553#endif /* HAVE_SETEGID */
5554
5555#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005557"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558Set the current process's real and effective user ids.");
5559
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005560static PyObject *
5561posix_setreuid (PyObject *self, PyObject *args)
5562{
5563 int ruid, euid;
5564 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
5565 return NULL;
5566 } else if (setreuid(ruid, euid) < 0) {
5567 return posix_error();
5568 } else {
5569 Py_INCREF(Py_None);
5570 return Py_None;
5571 }
5572}
5573#endif /* HAVE_SETREUID */
5574
5575#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005576PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005577"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578Set the current process's real and effective group ids.");
5579
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005580static PyObject *
5581posix_setregid (PyObject *self, PyObject *args)
5582{
5583 int rgid, egid;
5584 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
5585 return NULL;
5586 } else if (setregid(rgid, egid) < 0) {
5587 return posix_error();
5588 } else {
5589 Py_INCREF(Py_None);
5590 return Py_None;
5591 }
5592}
5593#endif /* HAVE_SETREGID */
5594
Guido van Rossumb6775db1994-08-01 11:34:53 +00005595#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005596PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005597"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005599
Barry Warsaw53699e91996-12-10 23:23:01 +00005600static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005601posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005602{
5603 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005604 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005605 return NULL;
5606 if (setgid(gid) < 0)
5607 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005608 Py_INCREF(Py_None);
5609 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005610}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005611#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005612
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005613#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005614PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005615"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005616Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005617
5618static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005619posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005620{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005621 int i, len;
5622 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005623
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005624 if (!PySequence_Check(groups)) {
5625 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5626 return NULL;
5627 }
5628 len = PySequence_Size(groups);
5629 if (len > MAX_GROUPS) {
5630 PyErr_SetString(PyExc_ValueError, "too many groups");
5631 return NULL;
5632 }
5633 for(i = 0; i < len; i++) {
5634 PyObject *elem;
5635 elem = PySequence_GetItem(groups, i);
5636 if (!elem)
5637 return NULL;
5638 if (!PyInt_Check(elem)) {
Georg Brandla13c2442005-11-22 19:30:31 +00005639 if (!PyLong_Check(elem)) {
5640 PyErr_SetString(PyExc_TypeError,
5641 "groups must be integers");
5642 Py_DECREF(elem);
5643 return NULL;
5644 } else {
5645 unsigned long x = PyLong_AsUnsignedLong(elem);
5646 if (PyErr_Occurred()) {
5647 PyErr_SetString(PyExc_TypeError,
5648 "group id too big");
5649 Py_DECREF(elem);
5650 return NULL;
5651 }
5652 grouplist[i] = x;
5653 /* read back the value to see if it fitted in gid_t */
5654 if (grouplist[i] != x) {
5655 PyErr_SetString(PyExc_TypeError,
5656 "group id too big");
5657 Py_DECREF(elem);
5658 return NULL;
5659 }
5660 }
5661 } else {
5662 long x = PyInt_AsLong(elem);
5663 grouplist[i] = x;
5664 if (grouplist[i] != x) {
5665 PyErr_SetString(PyExc_TypeError,
5666 "group id too big");
5667 Py_DECREF(elem);
5668 return NULL;
5669 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005670 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005671 Py_DECREF(elem);
5672 }
5673
5674 if (setgroups(len, grouplist) < 0)
5675 return posix_error();
5676 Py_INCREF(Py_None);
5677 return Py_None;
5678}
5679#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005680
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005681#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Neal Norwitz05a45592006-03-20 06:30:08 +00005682static PyObject *
Christian Heimesd491d712008-02-01 18:49:26 +00005683wait_helper(pid_t pid, int status, struct rusage *ru)
Neal Norwitz05a45592006-03-20 06:30:08 +00005684{
5685 PyObject *result;
5686 static PyObject *struct_rusage;
5687
5688 if (pid == -1)
5689 return posix_error();
5690
5691 if (struct_rusage == NULL) {
Christian Heimes000a0742008-01-03 22:16:32 +00005692 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Neal Norwitz05a45592006-03-20 06:30:08 +00005693 if (m == NULL)
5694 return NULL;
5695 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5696 Py_DECREF(m);
5697 if (struct_rusage == NULL)
5698 return NULL;
5699 }
5700
5701 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5702 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5703 if (!result)
5704 return NULL;
5705
5706#ifndef doubletime
5707#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5708#endif
5709
5710 PyStructSequence_SET_ITEM(result, 0,
5711 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5712 PyStructSequence_SET_ITEM(result, 1,
5713 PyFloat_FromDouble(doubletime(ru->ru_stime)));
5714#define SET_INT(result, index, value)\
5715 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5716 SET_INT(result, 2, ru->ru_maxrss);
5717 SET_INT(result, 3, ru->ru_ixrss);
5718 SET_INT(result, 4, ru->ru_idrss);
5719 SET_INT(result, 5, ru->ru_isrss);
5720 SET_INT(result, 6, ru->ru_minflt);
5721 SET_INT(result, 7, ru->ru_majflt);
5722 SET_INT(result, 8, ru->ru_nswap);
5723 SET_INT(result, 9, ru->ru_inblock);
5724 SET_INT(result, 10, ru->ru_oublock);
5725 SET_INT(result, 11, ru->ru_msgsnd);
5726 SET_INT(result, 12, ru->ru_msgrcv);
5727 SET_INT(result, 13, ru->ru_nsignals);
5728 SET_INT(result, 14, ru->ru_nvcsw);
5729 SET_INT(result, 15, ru->ru_nivcsw);
5730#undef SET_INT
5731
5732 if (PyErr_Occurred()) {
5733 Py_DECREF(result);
5734 return NULL;
5735 }
5736
Neal Norwitz9b00a562006-03-20 08:47:12 +00005737 return Py_BuildValue("iiN", pid, status, result);
Neal Norwitz05a45592006-03-20 06:30:08 +00005738}
Neal Norwitz6c2f9132006-03-20 07:25:26 +00005739#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
Neal Norwitz05a45592006-03-20 06:30:08 +00005740
5741#ifdef HAVE_WAIT3
5742PyDoc_STRVAR(posix_wait3__doc__,
5743"wait3(options) -> (pid, status, rusage)\n\n\
5744Wait for completion of a child process.");
5745
5746static PyObject *
5747posix_wait3(PyObject *self, PyObject *args)
5748{
Christian Heimesd491d712008-02-01 18:49:26 +00005749 pid_t pid;
5750 int options;
Neal Norwitz05a45592006-03-20 06:30:08 +00005751 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005752 WAIT_TYPE status;
5753 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005754
5755 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5756 return NULL;
5757
5758 Py_BEGIN_ALLOW_THREADS
5759 pid = wait3(&status, options, &ru);
5760 Py_END_ALLOW_THREADS
5761
Neal Norwitzd5a37542006-03-20 06:48:34 +00005762 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005763}
5764#endif /* HAVE_WAIT3 */
5765
5766#ifdef HAVE_WAIT4
5767PyDoc_STRVAR(posix_wait4__doc__,
5768"wait4(pid, options) -> (pid, status, rusage)\n\n\
5769Wait for completion of a given child process.");
5770
5771static PyObject *
5772posix_wait4(PyObject *self, PyObject *args)
5773{
Christian Heimesd491d712008-02-01 18:49:26 +00005774 pid_t pid;
5775 int options;
Neal Norwitz05a45592006-03-20 06:30:08 +00005776 struct rusage ru;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005777 WAIT_TYPE status;
5778 WAIT_STATUS_INT(status) = 0;
Neal Norwitz05a45592006-03-20 06:30:08 +00005779
5780 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
5781 return NULL;
5782
5783 Py_BEGIN_ALLOW_THREADS
5784 pid = wait4(pid, &status, options, &ru);
5785 Py_END_ALLOW_THREADS
5786
Neal Norwitzd5a37542006-03-20 06:48:34 +00005787 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Neal Norwitz05a45592006-03-20 06:30:08 +00005788}
5789#endif /* HAVE_WAIT4 */
5790
Guido van Rossumb6775db1994-08-01 11:34:53 +00005791#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005792PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005793"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005794Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005795
Barry Warsaw53699e91996-12-10 23:23:01 +00005796static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005797posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005798{
Christian Heimesd491d712008-02-01 18:49:26 +00005799 pid_t pid;
5800 int options;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005801 WAIT_TYPE status;
5802 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005803
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005804 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00005805 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005806 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00005807 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00005808 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00005809 if (pid == -1)
5810 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005811
5812 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005813}
5814
Tim Petersab034fa2002-02-01 11:27:43 +00005815#elif defined(HAVE_CWAIT)
5816
5817/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005818PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005819"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005820"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005821
5822static PyObject *
5823posix_waitpid(PyObject *self, PyObject *args)
5824{
Martin v. Löwisa43190b2006-05-22 09:15:18 +00005825 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005826 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005827
5828 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
5829 return NULL;
5830 Py_BEGIN_ALLOW_THREADS
5831 pid = _cwait(&status, pid, options);
5832 Py_END_ALLOW_THREADS
5833 if (pid == -1)
5834 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005835
5836 /* shift the status left a byte so this is more like the POSIX waitpid */
5837 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005838}
5839#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005840
Guido van Rossumad0ee831995-03-01 10:34:45 +00005841#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005842PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005843"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005844Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005845
Barry Warsaw53699e91996-12-10 23:23:01 +00005846static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005847posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005848{
Christian Heimesd491d712008-02-01 18:49:26 +00005849 pid_t pid;
Neal Norwitzd5a37542006-03-20 06:48:34 +00005850 WAIT_TYPE status;
5851 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005852
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005853 Py_BEGIN_ALLOW_THREADS
5854 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00005855 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00005856 if (pid == -1)
5857 return posix_error();
Neal Norwitzd5a37542006-03-20 06:48:34 +00005858
5859 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005860}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005861#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005862
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005863
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005864PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005865"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005866Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005867
Barry Warsaw53699e91996-12-10 23:23:01 +00005868static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005869posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005870{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005871#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005872 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005873#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005874#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00005875 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005876#else
5877 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
5878#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005879#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005880}
5881
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005882
Guido van Rossumb6775db1994-08-01 11:34:53 +00005883#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005884PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005885"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005886Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005887
Barry Warsaw53699e91996-12-10 23:23:01 +00005888static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005889posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005890{
Ronald Oussoren10168f22006-10-22 10:45:18 +00005891 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005892 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00005893 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005894 int n;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005895#ifdef Py_USING_UNICODE
5896 int arg_is_unicode = 0;
5897#endif
5898
5899 if (!PyArg_ParseTuple(args, "et:readlink",
5900 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005901 return NULL;
Ronald Oussoren10168f22006-10-22 10:45:18 +00005902#ifdef Py_USING_UNICODE
5903 v = PySequence_GetItem(args, 0);
Neal Norwitz91a57212007-08-12 17:11:13 +00005904 if (v == NULL) {
5905 PyMem_Free(path);
5906 return NULL;
5907 }
Ronald Oussoren10168f22006-10-22 10:45:18 +00005908
5909 if (PyUnicode_Check(v)) {
5910 arg_is_unicode = 1;
5911 }
5912 Py_DECREF(v);
5913#endif
5914
Barry Warsaw53699e91996-12-10 23:23:01 +00005915 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00005916 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00005917 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005918 if (n < 0)
Neal Norwitz91a57212007-08-12 17:11:13 +00005919 return posix_error_with_allocated_filename(path);
Ronald Oussoren10168f22006-10-22 10:45:18 +00005920
Neal Norwitz91a57212007-08-12 17:11:13 +00005921 PyMem_Free(path);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005922 v = PyString_FromStringAndSize(buf, n);
Ronald Oussoren10168f22006-10-22 10:45:18 +00005923#ifdef Py_USING_UNICODE
5924 if (arg_is_unicode) {
5925 PyObject *w;
5926
5927 w = PyUnicode_FromEncodedObject(v,
5928 Py_FileSystemDefaultEncoding,
5929 "strict");
5930 if (w != NULL) {
5931 Py_DECREF(v);
5932 v = w;
5933 }
5934 else {
5935 /* fall back to the original byte string, as
5936 discussed in patch #683592 */
5937 PyErr_Clear();
5938 }
5939 }
5940#endif
5941 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005942}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005943#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005944
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005945
Guido van Rossumb6775db1994-08-01 11:34:53 +00005946#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005947PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005948"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005949Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005950
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005951static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005952posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005953{
Martin v. Löwis4fc2bda2006-05-04 12:04:27 +00005954 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005955}
5956#endif /* HAVE_SYMLINK */
5957
5958
5959#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005960#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5961static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005962system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005963{
5964 ULONG value = 0;
5965
5966 Py_BEGIN_ALLOW_THREADS
5967 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5968 Py_END_ALLOW_THREADS
5969
5970 return value;
5971}
5972
5973static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005974posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005975{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005976 /* Currently Only Uptime is Provided -- Others Later */
5977 return Py_BuildValue("ddddd",
5978 (double)0 /* t.tms_utime / HZ */,
5979 (double)0 /* t.tms_stime / HZ */,
5980 (double)0 /* t.tms_cutime / HZ */,
5981 (double)0 /* t.tms_cstime / HZ */,
5982 (double)system_uptime() / 1000);
5983}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005984#else /* not OS2 */
Martin v. Löwis03824e42008-12-29 18:17:34 +00005985#define NEED_TICKS_PER_SECOND
5986static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005987static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005988posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005989{
5990 struct tms t;
5991 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00005992 errno = 0;
5993 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00005994 if (c == (clock_t) -1)
5995 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005996 return Py_BuildValue("ddddd",
Martin v. Löwis03824e42008-12-29 18:17:34 +00005997 (double)t.tms_utime / ticks_per_second,
5998 (double)t.tms_stime / ticks_per_second,
5999 (double)t.tms_cutime / ticks_per_second,
6000 (double)t.tms_cstime / ticks_per_second,
6001 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006002}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006003#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006004#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006005
6006
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006007#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006008#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006009static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006010posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006011{
6012 FILETIME create, exit, kernel, user;
6013 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006014 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00006015 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6016 /* The fields of a FILETIME structure are the hi and lo part
6017 of a 64-bit value expressed in 100 nanosecond units.
6018 1e7 is one second in such units; 1e-7 the inverse.
6019 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6020 */
Barry Warsaw53699e91996-12-10 23:23:01 +00006021 return Py_BuildValue(
6022 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00006023 (double)(user.dwHighDateTime*429.4967296 +
6024 user.dwLowDateTime*1e-7),
Georg Brandl0a40ffb2008-02-13 07:20:22 +00006025 (double)(kernel.dwHighDateTime*429.4967296 +
6026 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00006027 (double)0,
6028 (double)0,
6029 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006030}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006031#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006032
6033#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006034PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006035"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006036Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006037#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006038
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006039
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006040#ifdef HAVE_GETSID
6041PyDoc_STRVAR(posix_getsid__doc__,
6042"getsid(pid) -> sid\n\n\
6043Call the system call getsid().");
6044
6045static PyObject *
6046posix_getsid(PyObject *self, PyObject *args)
6047{
Christian Heimesd491d712008-02-01 18:49:26 +00006048 pid_t pid;
6049 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006050 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
6051 return NULL;
6052 sid = getsid(pid);
6053 if (sid < 0)
6054 return posix_error();
6055 return PyInt_FromLong((long)sid);
6056}
6057#endif /* HAVE_GETSID */
6058
6059
Guido van Rossumb6775db1994-08-01 11:34:53 +00006060#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006061PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006062"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006063Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006064
Barry Warsaw53699e91996-12-10 23:23:01 +00006065static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006066posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006067{
Guido van Rossum687dd131993-05-17 08:34:16 +00006068 if (setsid() < 0)
6069 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006070 Py_INCREF(Py_None);
6071 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006072}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006073#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006074
Guido van Rossumb6775db1994-08-01 11:34:53 +00006075#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006076PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006077"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006078Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006079
Barry Warsaw53699e91996-12-10 23:23:01 +00006080static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006081posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006082{
Christian Heimesd491d712008-02-01 18:49:26 +00006083 pid_t pid;
6084 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006085 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00006086 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006087 if (setpgid(pid, pgrp) < 0)
6088 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006089 Py_INCREF(Py_None);
6090 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006091}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006092#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006093
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006094
Guido van Rossumb6775db1994-08-01 11:34:53 +00006095#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006096PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006097"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006098Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006099
Barry Warsaw53699e91996-12-10 23:23:01 +00006100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006101posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006102{
Christian Heimese6a80742008-02-03 19:51:13 +00006103 int fd;
6104 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006105 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00006106 return NULL;
6107 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006108 if (pgid < 0)
6109 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006110 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006111}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006112#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006113
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006114
Guido van Rossumb6775db1994-08-01 11:34:53 +00006115#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006116PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006117"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006118Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006119
Barry Warsaw53699e91996-12-10 23:23:01 +00006120static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006121posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006122{
6123 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006124 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00006125 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006126 if (tcsetpgrp(fd, pgid) < 0)
6127 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00006128 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00006129 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006130}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006131#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006132
Guido van Rossum687dd131993-05-17 08:34:16 +00006133/* Functions acting on file descriptors */
6134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006136"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006137Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006138
Barry Warsaw53699e91996-12-10 23:23:01 +00006139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006140posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006141{
Mark Hammondef8b6542001-05-13 08:04:26 +00006142 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006143 int flag;
6144 int mode = 0777;
6145 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006146
6147#ifdef MS_WINDOWS
6148 if (unicode_file_names()) {
6149 PyUnicodeObject *po;
6150 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6151 Py_BEGIN_ALLOW_THREADS
6152 /* PyUnicode_AS_UNICODE OK without thread
6153 lock as it is a simple dereference. */
6154 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6155 Py_END_ALLOW_THREADS
6156 if (fd < 0)
6157 return posix_error();
6158 return PyInt_FromLong((long)fd);
6159 }
6160 /* Drop the argument parsing error as narrow strings
6161 are also valid. */
6162 PyErr_Clear();
6163 }
6164#endif
6165
Tim Peters5aa91602002-01-30 05:46:57 +00006166 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00006167 Py_FileSystemDefaultEncoding, &file,
6168 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00006169 return NULL;
6170
Barry Warsaw53699e91996-12-10 23:23:01 +00006171 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006172 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00006173 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006174 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00006175 return posix_error_with_allocated_filename(file);
6176 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00006177 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006178}
6179
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006180
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006181PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006182"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006183Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006184
Barry Warsaw53699e91996-12-10 23:23:01 +00006185static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006186posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006187{
6188 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006189 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006190 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006191 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006192 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006193 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006194 if (res < 0)
6195 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006196 Py_INCREF(Py_None);
6197 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006198}
6199
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006200
Georg Brandl309501a2008-01-19 20:22:13 +00006201PyDoc_STRVAR(posix_closerange__doc__,
6202"closerange(fd_low, fd_high)\n\n\
6203Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6204
6205static PyObject *
6206posix_closerange(PyObject *self, PyObject *args)
6207{
6208 int fd_from, fd_to, i;
6209 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6210 return NULL;
6211 Py_BEGIN_ALLOW_THREADS
6212 for (i = fd_from; i < fd_to; i++)
6213 close(i);
6214 Py_END_ALLOW_THREADS
6215 Py_RETURN_NONE;
6216}
6217
6218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006219PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006220"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006221Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006222
Barry Warsaw53699e91996-12-10 23:23:01 +00006223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006224posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006225{
6226 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006227 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006228 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006229 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006230 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00006231 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006232 if (fd < 0)
6233 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006234 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006235}
6236
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006239"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006240Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006241
Barry Warsaw53699e91996-12-10 23:23:01 +00006242static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006243posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006244{
6245 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006246 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00006247 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006248 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006249 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00006250 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006251 if (res < 0)
6252 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006253 Py_INCREF(Py_None);
6254 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006255}
6256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006258PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006259"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006260Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006261
Barry Warsaw53699e91996-12-10 23:23:01 +00006262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006263posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006264{
6265 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006266#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006267 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006268#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00006269 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006270#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006271 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006272 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00006273 return NULL;
6274#ifdef SEEK_SET
6275 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6276 switch (how) {
6277 case 0: how = SEEK_SET; break;
6278 case 1: how = SEEK_CUR; break;
6279 case 2: how = SEEK_END; break;
6280 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006281#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006282
6283#if !defined(HAVE_LARGEFILE_SUPPORT)
6284 pos = PyInt_AsLong(posobj);
6285#else
6286 pos = PyLong_Check(posobj) ?
6287 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
6288#endif
6289 if (PyErr_Occurred())
6290 return NULL;
6291
Barry Warsaw53699e91996-12-10 23:23:01 +00006292 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006293#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00006294 res = _lseeki64(fd, pos, how);
6295#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006296 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006297#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006298 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006299 if (res < 0)
6300 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006301
6302#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00006303 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006304#else
6305 return PyLong_FromLongLong(res);
6306#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006307}
6308
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006310PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006311"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006312Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006313
Barry Warsaw53699e91996-12-10 23:23:01 +00006314static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006315posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006316{
Guido van Rossum8bac5461996-06-11 18:38:48 +00006317 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00006318 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006319 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00006320 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00006321 if (size < 0) {
6322 errno = EINVAL;
6323 return posix_error();
6324 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006325 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006326 if (buffer == NULL)
6327 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006328 Py_BEGIN_ALLOW_THREADS
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006329 n = read(fd, PyString_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00006330 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00006331 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006332 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00006333 return posix_error();
6334 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00006335 if (n != size)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006336 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00006337 return buffer;
6338}
6339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006341PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006342"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006343Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006344
Barry Warsaw53699e91996-12-10 23:23:01 +00006345static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006346posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006347{
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006348 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006349 int fd;
6350 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006351
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006352 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00006353 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006354 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006355 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00006356 Py_END_ALLOW_THREADS
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00006357 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00006358 if (size < 0)
6359 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00006360 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00006361}
6362
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006363
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006364PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006365"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006366Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006367
Barry Warsaw53699e91996-12-10 23:23:01 +00006368static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006369posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006370{
6371 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00006372 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00006373 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006374 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00006375 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006376#ifdef __VMS
6377 /* on OpenVMS we must ensure that all bytes are written to the file */
6378 fsync(fd);
6379#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006380 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00006381 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00006382 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00006383 if (res != 0) {
6384#ifdef MS_WINDOWS
6385 return win32_error("fstat", NULL);
6386#else
Guido van Rossum687dd131993-05-17 08:34:16 +00006387 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006388#endif
6389 }
Tim Peters5aa91602002-01-30 05:46:57 +00006390
Martin v. Löwis14694662006-02-03 12:54:16 +00006391 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006392}
6393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006395PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006396"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006397Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006398
Barry Warsaw53699e91996-12-10 23:23:01 +00006399static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006400posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006401{
Guido van Rossum687dd131993-05-17 08:34:16 +00006402 int fd;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006403 char *orgmode = "r";
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006404 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00006405 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00006406 PyObject *f;
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006407 char *mode;
6408 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00006409 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00006410
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006411 /* Sanitize mode. See fileobject.c */
6412 mode = PyMem_MALLOC(strlen(orgmode)+3);
6413 if (!mode) {
6414 PyErr_NoMemory();
6415 return NULL;
6416 }
6417 strcpy(mode, orgmode);
6418 if (_PyFile_SanitizeMode(mode)) {
6419 PyMem_FREE(mode);
Thomas Heller1f043e22002-11-07 16:00:59 +00006420 return NULL;
6421 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006422 Py_BEGIN_ALLOW_THREADS
Georg Brandl644b1e72006-03-31 20:27:22 +00006423#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
Georg Brandl54a188a2006-03-31 20:00:11 +00006424 if (mode[0] == 'a') {
6425 /* try to make sure the O_APPEND flag is set */
6426 int flags;
6427 flags = fcntl(fd, F_GETFL);
6428 if (flags != -1)
6429 fcntl(fd, F_SETFL, flags | O_APPEND);
6430 fp = fdopen(fd, mode);
Thomas Wouters2a9a6b02006-03-31 22:38:19 +00006431 if (fp == NULL && flags != -1)
Georg Brandl54a188a2006-03-31 20:00:11 +00006432 /* restore old mode if fdopen failed */
6433 fcntl(fd, F_SETFL, flags);
6434 } else {
6435 fp = fdopen(fd, mode);
6436 }
Georg Brandl644b1e72006-03-31 20:27:22 +00006437#else
6438 fp = fdopen(fd, mode);
6439#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006440 Py_END_ALLOW_THREADS
Neal Norwitzd83eb312007-05-02 04:47:55 +00006441 PyMem_FREE(mode);
Guido van Rossum687dd131993-05-17 08:34:16 +00006442 if (fp == NULL)
6443 return posix_error();
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +00006444 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006445 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00006446 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00006447 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00006448}
6449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006450PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006451"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006452Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006453connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006454
6455static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006456posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006457{
6458 int fd;
6459 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6460 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006461 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006462}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006463
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006464#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006465PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006466"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006467Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006468
Barry Warsaw53699e91996-12-10 23:23:01 +00006469static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006470posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006471{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006472#if defined(PYOS_OS2)
6473 HFILE read, write;
6474 APIRET rc;
6475
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006476 Py_BEGIN_ALLOW_THREADS
6477 rc = DosCreatePipe( &read, &write, 4096);
6478 Py_END_ALLOW_THREADS
6479 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006480 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006481
6482 return Py_BuildValue("(ii)", read, write);
6483#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006484#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00006485 int fds[2];
6486 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00006487 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006488 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00006489 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00006490 if (res != 0)
6491 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006492 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006493#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00006494 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006495 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00006496 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00006497 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006498 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00006499 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00006500 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006501 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00006502 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6503 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00006504 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006505#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006506#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006507}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006508#endif /* HAVE_PIPE */
6509
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006510
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006511#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006512PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006513"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006514Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006515
Barry Warsaw53699e91996-12-10 23:23:01 +00006516static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006517posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006518{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006519 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006520 int mode = 0666;
6521 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006522 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006523 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00006524 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006525 res = mkfifo(filename, mode);
6526 Py_END_ALLOW_THREADS
6527 if (res < 0)
6528 return posix_error();
6529 Py_INCREF(Py_None);
6530 return Py_None;
6531}
6532#endif
6533
6534
Neal Norwitz11690112002-07-30 01:08:28 +00006535#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006536PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006537"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006538Create a filesystem node (file, device special file or named pipe)\n\
6539named filename. mode specifies both the permissions to use and the\n\
6540type of node to be created, being combined (bitwise OR) with one of\n\
6541S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006542device defines the newly created device special file (probably using\n\
6543os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006544
6545
6546static PyObject *
6547posix_mknod(PyObject *self, PyObject *args)
6548{
6549 char *filename;
6550 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006551 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006552 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00006553 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006554 return NULL;
6555 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006556 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00006557 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006558 if (res < 0)
6559 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00006560 Py_INCREF(Py_None);
6561 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006562}
6563#endif
6564
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006565#ifdef HAVE_DEVICE_MACROS
6566PyDoc_STRVAR(posix_major__doc__,
6567"major(device) -> major number\n\
6568Extracts a device major number from a raw device number.");
6569
6570static PyObject *
6571posix_major(PyObject *self, PyObject *args)
6572{
6573 int device;
6574 if (!PyArg_ParseTuple(args, "i:major", &device))
6575 return NULL;
6576 return PyInt_FromLong((long)major(device));
6577}
6578
6579PyDoc_STRVAR(posix_minor__doc__,
6580"minor(device) -> minor number\n\
6581Extracts a device minor number from a raw device number.");
6582
6583static PyObject *
6584posix_minor(PyObject *self, PyObject *args)
6585{
6586 int device;
6587 if (!PyArg_ParseTuple(args, "i:minor", &device))
6588 return NULL;
6589 return PyInt_FromLong((long)minor(device));
6590}
6591
6592PyDoc_STRVAR(posix_makedev__doc__,
6593"makedev(major, minor) -> device number\n\
6594Composes a raw device number from the major and minor device numbers.");
6595
6596static PyObject *
6597posix_makedev(PyObject *self, PyObject *args)
6598{
6599 int major, minor;
6600 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6601 return NULL;
6602 return PyInt_FromLong((long)makedev(major, minor));
6603}
6604#endif /* device macros */
6605
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006606
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006607#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006608PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006609"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006610Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006611
Barry Warsaw53699e91996-12-10 23:23:01 +00006612static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006613posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006614{
6615 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006616 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006617 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006618 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006619
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006620 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006621 return NULL;
6622
6623#if !defined(HAVE_LARGEFILE_SUPPORT)
6624 length = PyInt_AsLong(lenobj);
6625#else
6626 length = PyLong_Check(lenobj) ?
6627 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
6628#endif
6629 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006630 return NULL;
6631
Barry Warsaw53699e91996-12-10 23:23:01 +00006632 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006633 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00006634 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006635 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00006636 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006637 return NULL;
6638 }
Barry Warsaw53699e91996-12-10 23:23:01 +00006639 Py_INCREF(Py_None);
6640 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006641}
6642#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006643
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006644#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006645PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006646"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006647Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006648
Fred Drake762e2061999-08-26 17:23:54 +00006649/* Save putenv() parameters as values here, so we can collect them when they
6650 * get re-set with another call for the same key. */
6651static PyObject *posix_putenv_garbage;
6652
Tim Peters5aa91602002-01-30 05:46:57 +00006653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006654posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006655{
6656 char *s1, *s2;
Anthony Baxter64182fe2006-04-11 12:14:09 +00006657 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00006658 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00006659 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006660
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006661 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006662 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006663
6664#if defined(PYOS_OS2)
6665 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6666 APIRET rc;
6667
Guido van Rossumd48f2521997-12-05 22:19:34 +00006668 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
6669 if (rc != NO_ERROR)
6670 return os2_error(rc);
6671
6672 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6673 APIRET rc;
6674
Guido van Rossumd48f2521997-12-05 22:19:34 +00006675 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
6676 if (rc != NO_ERROR)
6677 return os2_error(rc);
6678 } else {
6679#endif
6680
Fred Drake762e2061999-08-26 17:23:54 +00006681 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00006682 len = strlen(s1) + strlen(s2) + 2;
6683 /* len includes space for a trailing \0; the size arg to
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006684 PyString_FromStringAndSize does not count that */
6685 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00006686 if (newstr == NULL)
6687 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006688 newenv = PyString_AS_STRING(newstr);
Anthony Baxter64182fe2006-04-11 12:14:09 +00006689 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6690 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00006691 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006692 posix_error();
6693 return NULL;
6694 }
Fred Drake762e2061999-08-26 17:23:54 +00006695 /* Install the first arg and newstr in posix_putenv_garbage;
6696 * this will cause previous value to be collected. This has to
6697 * happen after the real putenv() call because the old value
6698 * was still accessible until then. */
6699 if (PyDict_SetItem(posix_putenv_garbage,
6700 PyTuple_GET_ITEM(args, 0), newstr)) {
6701 /* really not much we can do; just leak */
6702 PyErr_Clear();
6703 }
6704 else {
6705 Py_DECREF(newstr);
6706 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006707
6708#if defined(PYOS_OS2)
6709 }
6710#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00006711 Py_INCREF(Py_None);
6712 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006713}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006714#endif /* putenv */
6715
Guido van Rossumc524d952001-10-19 01:31:59 +00006716#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006717PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006718"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006719Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006720
6721static PyObject *
6722posix_unsetenv(PyObject *self, PyObject *args)
6723{
6724 char *s1;
6725
6726 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6727 return NULL;
6728
6729 unsetenv(s1);
6730
6731 /* Remove the key from posix_putenv_garbage;
6732 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00006733 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00006734 * old value was still accessible until then.
6735 */
6736 if (PyDict_DelItem(posix_putenv_garbage,
6737 PyTuple_GET_ITEM(args, 0))) {
6738 /* really not much we can do; just leak */
6739 PyErr_Clear();
6740 }
6741
6742 Py_INCREF(Py_None);
6743 return Py_None;
6744}
6745#endif /* unsetenv */
6746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006747PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006748"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006749Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006750
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006751static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006752posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006753{
6754 int code;
6755 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006756 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00006757 return NULL;
6758 message = strerror(code);
6759 if (message == NULL) {
6760 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00006761 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006762 return NULL;
6763 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006764 return PyString_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006765}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006766
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006767
Guido van Rossumc9641791998-08-04 15:26:23 +00006768#ifdef HAVE_SYS_WAIT_H
6769
Fred Drake106c1a02002-04-23 15:58:02 +00006770#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006771PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006772"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006773Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006774
6775static PyObject *
6776posix_WCOREDUMP(PyObject *self, PyObject *args)
6777{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006778 WAIT_TYPE status;
6779 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006780
Neal Norwitzd5a37542006-03-20 06:48:34 +00006781 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006782 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006783
6784 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006785}
6786#endif /* WCOREDUMP */
6787
6788#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006789PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006790"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006791Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006792job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006793
6794static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006795posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006796{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006797 WAIT_TYPE status;
6798 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006799
Neal Norwitzd5a37542006-03-20 06:48:34 +00006800 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00006801 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006802
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006803 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006804}
6805#endif /* WIFCONTINUED */
6806
Guido van Rossumc9641791998-08-04 15:26:23 +00006807#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006808PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006809"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006810Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006811
6812static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006813posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006814{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006815 WAIT_TYPE status;
6816 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006817
Neal Norwitzd5a37542006-03-20 06:48:34 +00006818 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006819 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006820
Fred Drake106c1a02002-04-23 15:58:02 +00006821 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006822}
6823#endif /* WIFSTOPPED */
6824
6825#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006826PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006827"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006828Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006829
6830static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006831posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006832{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006833 WAIT_TYPE status;
6834 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006835
Neal Norwitzd5a37542006-03-20 06:48:34 +00006836 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006837 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006838
Fred Drake106c1a02002-04-23 15:58:02 +00006839 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006840}
6841#endif /* WIFSIGNALED */
6842
6843#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006844PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006845"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006846Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006847system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006848
6849static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006850posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006851{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006852 WAIT_TYPE status;
6853 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006854
Neal Norwitzd5a37542006-03-20 06:48:34 +00006855 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006856 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006857
Fred Drake106c1a02002-04-23 15:58:02 +00006858 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006859}
6860#endif /* WIFEXITED */
6861
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006862#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006863PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006864"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006865Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006866
6867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006868posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006869{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006870 WAIT_TYPE status;
6871 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006872
Neal Norwitzd5a37542006-03-20 06:48:34 +00006873 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006874 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006875
Guido van Rossumc9641791998-08-04 15:26:23 +00006876 return Py_BuildValue("i", WEXITSTATUS(status));
6877}
6878#endif /* WEXITSTATUS */
6879
6880#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006881PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006882"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006883Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006884value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006885
6886static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006887posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006888{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006889 WAIT_TYPE status;
6890 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006891
Neal Norwitzd5a37542006-03-20 06:48:34 +00006892 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006893 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006894
Guido van Rossumc9641791998-08-04 15:26:23 +00006895 return Py_BuildValue("i", WTERMSIG(status));
6896}
6897#endif /* WTERMSIG */
6898
6899#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006900PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006901"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006902Return the signal that stopped the process that provided\n\
6903the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006904
6905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006906posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006907{
Neal Norwitzd5a37542006-03-20 06:48:34 +00006908 WAIT_TYPE status;
6909 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006910
Neal Norwitzd5a37542006-03-20 06:48:34 +00006911 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00006912 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006913
Guido van Rossumc9641791998-08-04 15:26:23 +00006914 return Py_BuildValue("i", WSTOPSIG(status));
6915}
6916#endif /* WSTOPSIG */
6917
6918#endif /* HAVE_SYS_WAIT_H */
6919
6920
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006921#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006922#ifdef _SCO_DS
6923/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6924 needed definitions in sys/statvfs.h */
6925#define _SVID3
6926#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006927#include <sys/statvfs.h>
6928
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006929static PyObject*
6930_pystatvfs_fromstructstatvfs(struct statvfs st) {
6931 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6932 if (v == NULL)
6933 return NULL;
6934
6935#if !defined(HAVE_LARGEFILE_SUPPORT)
6936 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6937 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
6938 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
6939 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
6940 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
6941 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
6942 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
6943 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
6944 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6945 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6946#else
6947 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
6948 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00006949 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006950 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00006951 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006952 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006953 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006954 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00006955 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006956 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00006957 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006958 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00006959 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00006960 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006961 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
6962 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
6963#endif
6964
6965 return v;
6966}
6967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006968PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006969"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006970Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006971
6972static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006973posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006974{
6975 int fd, res;
6976 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006977
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006978 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00006979 return NULL;
6980 Py_BEGIN_ALLOW_THREADS
6981 res = fstatvfs(fd, &st);
6982 Py_END_ALLOW_THREADS
6983 if (res != 0)
6984 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006985
6986 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006987}
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006988#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006989
6990
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00006991#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006992#include <sys/statvfs.h>
6993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006994PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006995"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006996Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006997
6998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006999posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007000{
7001 char *path;
7002 int res;
7003 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007004 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00007005 return NULL;
7006 Py_BEGIN_ALLOW_THREADS
7007 res = statvfs(path, &st);
7008 Py_END_ALLOW_THREADS
7009 if (res != 0)
7010 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007011
7012 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007013}
7014#endif /* HAVE_STATVFS */
7015
7016
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007017#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007018PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007019"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007020Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00007021The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007022or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007023
7024static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007025posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007026{
7027 PyObject *result = NULL;
7028 char *dir = NULL;
7029 char *pfx = NULL;
7030 char *name;
7031
7032 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
7033 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00007034
7035 if (PyErr_Warn(PyExc_RuntimeWarning,
7036 "tempnam is a potential security risk to your program") < 0)
7037 return NULL;
7038
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007039#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00007040 name = _tempnam(dir, pfx);
7041#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007042 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00007043#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007044 if (name == NULL)
7045 return PyErr_NoMemory();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007046 result = PyString_FromString(name);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007047 free(name);
7048 return result;
7049}
Guido van Rossumd371ff11999-01-25 16:12:23 +00007050#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007051
7052
7053#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007054PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007055"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007056Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007057
7058static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007059posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007060{
7061 FILE *fp;
7062
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007063 fp = tmpfile();
7064 if (fp == NULL)
7065 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00007066 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007067}
7068#endif
7069
7070
7071#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007072PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007073"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007074Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007075
7076static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007077posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007078{
7079 char buffer[L_tmpnam];
7080 char *name;
7081
Skip Montanaro95618b52001-08-18 18:52:10 +00007082 if (PyErr_Warn(PyExc_RuntimeWarning,
7083 "tmpnam is a potential security risk to your program") < 0)
7084 return NULL;
7085
Greg Wardb48bc172000-03-01 21:51:56 +00007086#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007087 name = tmpnam_r(buffer);
7088#else
7089 name = tmpnam(buffer);
7090#endif
7091 if (name == NULL) {
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00007092 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00007093#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007094 "unexpected NULL from tmpnam_r"
7095#else
7096 "unexpected NULL from tmpnam"
7097#endif
Neal Norwitzd1e0ef62006-03-20 04:08:12 +00007098 );
7099 PyErr_SetObject(PyExc_OSError, err);
7100 Py_XDECREF(err);
7101 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007102 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007103 return PyString_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007104}
7105#endif
7106
7107
Fred Drakec9680921999-12-13 16:37:25 +00007108/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7109 * It maps strings representing configuration variable names to
7110 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007111 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007112 * rarely-used constants. There are three separate tables that use
7113 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007114 *
7115 * This code is always included, even if none of the interfaces that
7116 * need it are included. The #if hackery needed to avoid it would be
7117 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007118 */
7119struct constdef {
7120 char *name;
7121 long value;
7122};
7123
Fred Drake12c6e2d1999-12-14 21:25:03 +00007124static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007125conv_confname(PyObject *arg, int *valuep, struct constdef *table,
7126 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007127{
7128 if (PyInt_Check(arg)) {
7129 *valuep = PyInt_AS_LONG(arg);
7130 return 1;
7131 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007132 if (PyString_Check(arg)) {
Fred Drake12c6e2d1999-12-14 21:25:03 +00007133 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00007134 size_t lo = 0;
7135 size_t mid;
7136 size_t hi = tablesize;
7137 int cmp;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007138 char *confname = PyString_AS_STRING(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007139 while (lo < hi) {
7140 mid = (lo + hi) / 2;
7141 cmp = strcmp(confname, table[mid].name);
7142 if (cmp < 0)
7143 hi = mid;
7144 else if (cmp > 0)
7145 lo = mid + 1;
7146 else {
7147 *valuep = table[mid].value;
7148 return 1;
7149 }
7150 }
7151 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7152 }
7153 else
7154 PyErr_SetString(PyExc_TypeError,
7155 "configuration names must be strings or integers");
7156 return 0;
7157}
7158
7159
7160#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7161static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007162#ifdef _PC_ABI_AIO_XFER_MAX
7163 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
7164#endif
7165#ifdef _PC_ABI_ASYNC_IO
7166 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
7167#endif
Fred Drakec9680921999-12-13 16:37:25 +00007168#ifdef _PC_ASYNC_IO
7169 {"PC_ASYNC_IO", _PC_ASYNC_IO},
7170#endif
7171#ifdef _PC_CHOWN_RESTRICTED
7172 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
7173#endif
7174#ifdef _PC_FILESIZEBITS
7175 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
7176#endif
7177#ifdef _PC_LAST
7178 {"PC_LAST", _PC_LAST},
7179#endif
7180#ifdef _PC_LINK_MAX
7181 {"PC_LINK_MAX", _PC_LINK_MAX},
7182#endif
7183#ifdef _PC_MAX_CANON
7184 {"PC_MAX_CANON", _PC_MAX_CANON},
7185#endif
7186#ifdef _PC_MAX_INPUT
7187 {"PC_MAX_INPUT", _PC_MAX_INPUT},
7188#endif
7189#ifdef _PC_NAME_MAX
7190 {"PC_NAME_MAX", _PC_NAME_MAX},
7191#endif
7192#ifdef _PC_NO_TRUNC
7193 {"PC_NO_TRUNC", _PC_NO_TRUNC},
7194#endif
7195#ifdef _PC_PATH_MAX
7196 {"PC_PATH_MAX", _PC_PATH_MAX},
7197#endif
7198#ifdef _PC_PIPE_BUF
7199 {"PC_PIPE_BUF", _PC_PIPE_BUF},
7200#endif
7201#ifdef _PC_PRIO_IO
7202 {"PC_PRIO_IO", _PC_PRIO_IO},
7203#endif
7204#ifdef _PC_SOCK_MAXBUF
7205 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
7206#endif
7207#ifdef _PC_SYNC_IO
7208 {"PC_SYNC_IO", _PC_SYNC_IO},
7209#endif
7210#ifdef _PC_VDISABLE
7211 {"PC_VDISABLE", _PC_VDISABLE},
7212#endif
7213};
7214
Fred Drakec9680921999-12-13 16:37:25 +00007215static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007216conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007217{
7218 return conv_confname(arg, valuep, posix_constants_pathconf,
7219 sizeof(posix_constants_pathconf)
7220 / sizeof(struct constdef));
7221}
7222#endif
7223
7224#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007225PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007226"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007227Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007228If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007229
7230static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007231posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007232{
7233 PyObject *result = NULL;
7234 int name, fd;
7235
Fred Drake12c6e2d1999-12-14 21:25:03 +00007236 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7237 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00007238 long limit;
7239
7240 errno = 0;
7241 limit = fpathconf(fd, name);
7242 if (limit == -1 && errno != 0)
7243 posix_error();
7244 else
7245 result = PyInt_FromLong(limit);
7246 }
7247 return result;
7248}
7249#endif
7250
7251
7252#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007253PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007254"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007255Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007256If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007257
7258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007259posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007260{
7261 PyObject *result = NULL;
7262 int name;
7263 char *path;
7264
7265 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7266 conv_path_confname, &name)) {
7267 long limit;
7268
7269 errno = 0;
7270 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007271 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00007272 if (errno == EINVAL)
7273 /* could be a path or name problem */
7274 posix_error();
7275 else
7276 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00007277 }
Fred Drakec9680921999-12-13 16:37:25 +00007278 else
7279 result = PyInt_FromLong(limit);
7280 }
7281 return result;
7282}
7283#endif
7284
7285#ifdef HAVE_CONFSTR
7286static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007287#ifdef _CS_ARCHITECTURE
7288 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
7289#endif
7290#ifdef _CS_HOSTNAME
7291 {"CS_HOSTNAME", _CS_HOSTNAME},
7292#endif
7293#ifdef _CS_HW_PROVIDER
7294 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
7295#endif
7296#ifdef _CS_HW_SERIAL
7297 {"CS_HW_SERIAL", _CS_HW_SERIAL},
7298#endif
7299#ifdef _CS_INITTAB_NAME
7300 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
7301#endif
Fred Drakec9680921999-12-13 16:37:25 +00007302#ifdef _CS_LFS64_CFLAGS
7303 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
7304#endif
7305#ifdef _CS_LFS64_LDFLAGS
7306 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
7307#endif
7308#ifdef _CS_LFS64_LIBS
7309 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
7310#endif
7311#ifdef _CS_LFS64_LINTFLAGS
7312 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
7313#endif
7314#ifdef _CS_LFS_CFLAGS
7315 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
7316#endif
7317#ifdef _CS_LFS_LDFLAGS
7318 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
7319#endif
7320#ifdef _CS_LFS_LIBS
7321 {"CS_LFS_LIBS", _CS_LFS_LIBS},
7322#endif
7323#ifdef _CS_LFS_LINTFLAGS
7324 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
7325#endif
Fred Draked86ed291999-12-15 15:34:33 +00007326#ifdef _CS_MACHINE
7327 {"CS_MACHINE", _CS_MACHINE},
7328#endif
Fred Drakec9680921999-12-13 16:37:25 +00007329#ifdef _CS_PATH
7330 {"CS_PATH", _CS_PATH},
7331#endif
Fred Draked86ed291999-12-15 15:34:33 +00007332#ifdef _CS_RELEASE
7333 {"CS_RELEASE", _CS_RELEASE},
7334#endif
7335#ifdef _CS_SRPC_DOMAIN
7336 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
7337#endif
7338#ifdef _CS_SYSNAME
7339 {"CS_SYSNAME", _CS_SYSNAME},
7340#endif
7341#ifdef _CS_VERSION
7342 {"CS_VERSION", _CS_VERSION},
7343#endif
Fred Drakec9680921999-12-13 16:37:25 +00007344#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7345 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
7346#endif
7347#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7348 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
7349#endif
7350#ifdef _CS_XBS5_ILP32_OFF32_LIBS
7351 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
7352#endif
7353#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7354 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
7355#endif
7356#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7357 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
7358#endif
7359#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7360 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
7361#endif
7362#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7363 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
7364#endif
7365#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7366 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
7367#endif
7368#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7369 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
7370#endif
7371#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7372 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
7373#endif
7374#ifdef _CS_XBS5_LP64_OFF64_LIBS
7375 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
7376#endif
7377#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7378 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
7379#endif
7380#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7381 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
7382#endif
7383#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7384 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
7385#endif
7386#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7387 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
7388#endif
7389#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7390 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
7391#endif
Fred Draked86ed291999-12-15 15:34:33 +00007392#ifdef _MIPS_CS_AVAIL_PROCESSORS
7393 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
7394#endif
7395#ifdef _MIPS_CS_BASE
7396 {"MIPS_CS_BASE", _MIPS_CS_BASE},
7397#endif
7398#ifdef _MIPS_CS_HOSTID
7399 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
7400#endif
7401#ifdef _MIPS_CS_HW_NAME
7402 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
7403#endif
7404#ifdef _MIPS_CS_NUM_PROCESSORS
7405 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
7406#endif
7407#ifdef _MIPS_CS_OSREL_MAJ
7408 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
7409#endif
7410#ifdef _MIPS_CS_OSREL_MIN
7411 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
7412#endif
7413#ifdef _MIPS_CS_OSREL_PATCH
7414 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
7415#endif
7416#ifdef _MIPS_CS_OS_NAME
7417 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
7418#endif
7419#ifdef _MIPS_CS_OS_PROVIDER
7420 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
7421#endif
7422#ifdef _MIPS_CS_PROCESSORS
7423 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
7424#endif
7425#ifdef _MIPS_CS_SERIAL
7426 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
7427#endif
7428#ifdef _MIPS_CS_VENDOR
7429 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
7430#endif
Fred Drakec9680921999-12-13 16:37:25 +00007431};
7432
7433static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007434conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007435{
7436 return conv_confname(arg, valuep, posix_constants_confstr,
7437 sizeof(posix_constants_confstr)
7438 / sizeof(struct constdef));
7439}
7440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007441PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007442"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007443Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007444
7445static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007446posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007447{
7448 PyObject *result = NULL;
7449 int name;
Neal Norwitz449b24e2006-04-20 06:56:05 +00007450 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00007451
7452 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Skip Montanarodd527fc2006-04-18 00:49:49 +00007453 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007454
Fred Drakec9680921999-12-13 16:37:25 +00007455 errno = 0;
Skip Montanarodd527fc2006-04-18 00:49:49 +00007456 len = confstr(name, buffer, sizeof(buffer));
Skip Montanaro94785ef2006-04-20 01:29:48 +00007457 if (len == 0) {
7458 if (errno) {
7459 posix_error();
7460 }
7461 else {
7462 result = Py_None;
7463 Py_INCREF(Py_None);
7464 }
Fred Drakec9680921999-12-13 16:37:25 +00007465 }
7466 else {
Neal Norwitz0d21b1e2006-04-20 06:44:42 +00007467 if ((unsigned int)len >= sizeof(buffer)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007468 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007469 if (result != NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007470 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00007471 }
7472 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00007473 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007474 }
7475 }
7476 return result;
7477}
7478#endif
7479
7480
7481#ifdef HAVE_SYSCONF
7482static struct constdef posix_constants_sysconf[] = {
7483#ifdef _SC_2_CHAR_TERM
7484 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
7485#endif
7486#ifdef _SC_2_C_BIND
7487 {"SC_2_C_BIND", _SC_2_C_BIND},
7488#endif
7489#ifdef _SC_2_C_DEV
7490 {"SC_2_C_DEV", _SC_2_C_DEV},
7491#endif
7492#ifdef _SC_2_C_VERSION
7493 {"SC_2_C_VERSION", _SC_2_C_VERSION},
7494#endif
7495#ifdef _SC_2_FORT_DEV
7496 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
7497#endif
7498#ifdef _SC_2_FORT_RUN
7499 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
7500#endif
7501#ifdef _SC_2_LOCALEDEF
7502 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
7503#endif
7504#ifdef _SC_2_SW_DEV
7505 {"SC_2_SW_DEV", _SC_2_SW_DEV},
7506#endif
7507#ifdef _SC_2_UPE
7508 {"SC_2_UPE", _SC_2_UPE},
7509#endif
7510#ifdef _SC_2_VERSION
7511 {"SC_2_VERSION", _SC_2_VERSION},
7512#endif
Fred Draked86ed291999-12-15 15:34:33 +00007513#ifdef _SC_ABI_ASYNCHRONOUS_IO
7514 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
7515#endif
7516#ifdef _SC_ACL
7517 {"SC_ACL", _SC_ACL},
7518#endif
Fred Drakec9680921999-12-13 16:37:25 +00007519#ifdef _SC_AIO_LISTIO_MAX
7520 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
7521#endif
Fred Drakec9680921999-12-13 16:37:25 +00007522#ifdef _SC_AIO_MAX
7523 {"SC_AIO_MAX", _SC_AIO_MAX},
7524#endif
7525#ifdef _SC_AIO_PRIO_DELTA_MAX
7526 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
7527#endif
7528#ifdef _SC_ARG_MAX
7529 {"SC_ARG_MAX", _SC_ARG_MAX},
7530#endif
7531#ifdef _SC_ASYNCHRONOUS_IO
7532 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
7533#endif
7534#ifdef _SC_ATEXIT_MAX
7535 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
7536#endif
Fred Draked86ed291999-12-15 15:34:33 +00007537#ifdef _SC_AUDIT
7538 {"SC_AUDIT", _SC_AUDIT},
7539#endif
Fred Drakec9680921999-12-13 16:37:25 +00007540#ifdef _SC_AVPHYS_PAGES
7541 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
7542#endif
7543#ifdef _SC_BC_BASE_MAX
7544 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
7545#endif
7546#ifdef _SC_BC_DIM_MAX
7547 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
7548#endif
7549#ifdef _SC_BC_SCALE_MAX
7550 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
7551#endif
7552#ifdef _SC_BC_STRING_MAX
7553 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
7554#endif
Fred Draked86ed291999-12-15 15:34:33 +00007555#ifdef _SC_CAP
7556 {"SC_CAP", _SC_CAP},
7557#endif
Fred Drakec9680921999-12-13 16:37:25 +00007558#ifdef _SC_CHARCLASS_NAME_MAX
7559 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
7560#endif
7561#ifdef _SC_CHAR_BIT
7562 {"SC_CHAR_BIT", _SC_CHAR_BIT},
7563#endif
7564#ifdef _SC_CHAR_MAX
7565 {"SC_CHAR_MAX", _SC_CHAR_MAX},
7566#endif
7567#ifdef _SC_CHAR_MIN
7568 {"SC_CHAR_MIN", _SC_CHAR_MIN},
7569#endif
7570#ifdef _SC_CHILD_MAX
7571 {"SC_CHILD_MAX", _SC_CHILD_MAX},
7572#endif
7573#ifdef _SC_CLK_TCK
7574 {"SC_CLK_TCK", _SC_CLK_TCK},
7575#endif
7576#ifdef _SC_COHER_BLKSZ
7577 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
7578#endif
7579#ifdef _SC_COLL_WEIGHTS_MAX
7580 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
7581#endif
7582#ifdef _SC_DCACHE_ASSOC
7583 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
7584#endif
7585#ifdef _SC_DCACHE_BLKSZ
7586 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
7587#endif
7588#ifdef _SC_DCACHE_LINESZ
7589 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
7590#endif
7591#ifdef _SC_DCACHE_SZ
7592 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
7593#endif
7594#ifdef _SC_DCACHE_TBLKSZ
7595 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
7596#endif
7597#ifdef _SC_DELAYTIMER_MAX
7598 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
7599#endif
7600#ifdef _SC_EQUIV_CLASS_MAX
7601 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
7602#endif
7603#ifdef _SC_EXPR_NEST_MAX
7604 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
7605#endif
7606#ifdef _SC_FSYNC
7607 {"SC_FSYNC", _SC_FSYNC},
7608#endif
7609#ifdef _SC_GETGR_R_SIZE_MAX
7610 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
7611#endif
7612#ifdef _SC_GETPW_R_SIZE_MAX
7613 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
7614#endif
7615#ifdef _SC_ICACHE_ASSOC
7616 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
7617#endif
7618#ifdef _SC_ICACHE_BLKSZ
7619 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
7620#endif
7621#ifdef _SC_ICACHE_LINESZ
7622 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
7623#endif
7624#ifdef _SC_ICACHE_SZ
7625 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
7626#endif
Fred Draked86ed291999-12-15 15:34:33 +00007627#ifdef _SC_INF
7628 {"SC_INF", _SC_INF},
7629#endif
Fred Drakec9680921999-12-13 16:37:25 +00007630#ifdef _SC_INT_MAX
7631 {"SC_INT_MAX", _SC_INT_MAX},
7632#endif
7633#ifdef _SC_INT_MIN
7634 {"SC_INT_MIN", _SC_INT_MIN},
7635#endif
7636#ifdef _SC_IOV_MAX
7637 {"SC_IOV_MAX", _SC_IOV_MAX},
7638#endif
Fred Draked86ed291999-12-15 15:34:33 +00007639#ifdef _SC_IP_SECOPTS
7640 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
7641#endif
Fred Drakec9680921999-12-13 16:37:25 +00007642#ifdef _SC_JOB_CONTROL
7643 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
7644#endif
Fred Draked86ed291999-12-15 15:34:33 +00007645#ifdef _SC_KERN_POINTERS
7646 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
7647#endif
7648#ifdef _SC_KERN_SIM
7649 {"SC_KERN_SIM", _SC_KERN_SIM},
7650#endif
Fred Drakec9680921999-12-13 16:37:25 +00007651#ifdef _SC_LINE_MAX
7652 {"SC_LINE_MAX", _SC_LINE_MAX},
7653#endif
7654#ifdef _SC_LOGIN_NAME_MAX
7655 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
7656#endif
7657#ifdef _SC_LOGNAME_MAX
7658 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
7659#endif
7660#ifdef _SC_LONG_BIT
7661 {"SC_LONG_BIT", _SC_LONG_BIT},
7662#endif
Fred Draked86ed291999-12-15 15:34:33 +00007663#ifdef _SC_MAC
7664 {"SC_MAC", _SC_MAC},
7665#endif
Fred Drakec9680921999-12-13 16:37:25 +00007666#ifdef _SC_MAPPED_FILES
7667 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
7668#endif
7669#ifdef _SC_MAXPID
7670 {"SC_MAXPID", _SC_MAXPID},
7671#endif
7672#ifdef _SC_MB_LEN_MAX
7673 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
7674#endif
7675#ifdef _SC_MEMLOCK
7676 {"SC_MEMLOCK", _SC_MEMLOCK},
7677#endif
7678#ifdef _SC_MEMLOCK_RANGE
7679 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
7680#endif
7681#ifdef _SC_MEMORY_PROTECTION
7682 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
7683#endif
7684#ifdef _SC_MESSAGE_PASSING
7685 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
7686#endif
Fred Draked86ed291999-12-15 15:34:33 +00007687#ifdef _SC_MMAP_FIXED_ALIGNMENT
7688 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
7689#endif
Fred Drakec9680921999-12-13 16:37:25 +00007690#ifdef _SC_MQ_OPEN_MAX
7691 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
7692#endif
7693#ifdef _SC_MQ_PRIO_MAX
7694 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
7695#endif
Fred Draked86ed291999-12-15 15:34:33 +00007696#ifdef _SC_NACLS_MAX
7697 {"SC_NACLS_MAX", _SC_NACLS_MAX},
7698#endif
Fred Drakec9680921999-12-13 16:37:25 +00007699#ifdef _SC_NGROUPS_MAX
7700 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
7701#endif
7702#ifdef _SC_NL_ARGMAX
7703 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
7704#endif
7705#ifdef _SC_NL_LANGMAX
7706 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
7707#endif
7708#ifdef _SC_NL_MSGMAX
7709 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
7710#endif
7711#ifdef _SC_NL_NMAX
7712 {"SC_NL_NMAX", _SC_NL_NMAX},
7713#endif
7714#ifdef _SC_NL_SETMAX
7715 {"SC_NL_SETMAX", _SC_NL_SETMAX},
7716#endif
7717#ifdef _SC_NL_TEXTMAX
7718 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
7719#endif
7720#ifdef _SC_NPROCESSORS_CONF
7721 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
7722#endif
7723#ifdef _SC_NPROCESSORS_ONLN
7724 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
7725#endif
Fred Draked86ed291999-12-15 15:34:33 +00007726#ifdef _SC_NPROC_CONF
7727 {"SC_NPROC_CONF", _SC_NPROC_CONF},
7728#endif
7729#ifdef _SC_NPROC_ONLN
7730 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
7731#endif
Fred Drakec9680921999-12-13 16:37:25 +00007732#ifdef _SC_NZERO
7733 {"SC_NZERO", _SC_NZERO},
7734#endif
7735#ifdef _SC_OPEN_MAX
7736 {"SC_OPEN_MAX", _SC_OPEN_MAX},
7737#endif
7738#ifdef _SC_PAGESIZE
7739 {"SC_PAGESIZE", _SC_PAGESIZE},
7740#endif
7741#ifdef _SC_PAGE_SIZE
7742 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
7743#endif
7744#ifdef _SC_PASS_MAX
7745 {"SC_PASS_MAX", _SC_PASS_MAX},
7746#endif
7747#ifdef _SC_PHYS_PAGES
7748 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
7749#endif
7750#ifdef _SC_PII
7751 {"SC_PII", _SC_PII},
7752#endif
7753#ifdef _SC_PII_INTERNET
7754 {"SC_PII_INTERNET", _SC_PII_INTERNET},
7755#endif
7756#ifdef _SC_PII_INTERNET_DGRAM
7757 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
7758#endif
7759#ifdef _SC_PII_INTERNET_STREAM
7760 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
7761#endif
7762#ifdef _SC_PII_OSI
7763 {"SC_PII_OSI", _SC_PII_OSI},
7764#endif
7765#ifdef _SC_PII_OSI_CLTS
7766 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
7767#endif
7768#ifdef _SC_PII_OSI_COTS
7769 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
7770#endif
7771#ifdef _SC_PII_OSI_M
7772 {"SC_PII_OSI_M", _SC_PII_OSI_M},
7773#endif
7774#ifdef _SC_PII_SOCKET
7775 {"SC_PII_SOCKET", _SC_PII_SOCKET},
7776#endif
7777#ifdef _SC_PII_XTI
7778 {"SC_PII_XTI", _SC_PII_XTI},
7779#endif
7780#ifdef _SC_POLL
7781 {"SC_POLL", _SC_POLL},
7782#endif
7783#ifdef _SC_PRIORITIZED_IO
7784 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
7785#endif
7786#ifdef _SC_PRIORITY_SCHEDULING
7787 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
7788#endif
7789#ifdef _SC_REALTIME_SIGNALS
7790 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
7791#endif
7792#ifdef _SC_RE_DUP_MAX
7793 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
7794#endif
7795#ifdef _SC_RTSIG_MAX
7796 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
7797#endif
7798#ifdef _SC_SAVED_IDS
7799 {"SC_SAVED_IDS", _SC_SAVED_IDS},
7800#endif
7801#ifdef _SC_SCHAR_MAX
7802 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
7803#endif
7804#ifdef _SC_SCHAR_MIN
7805 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
7806#endif
7807#ifdef _SC_SELECT
7808 {"SC_SELECT", _SC_SELECT},
7809#endif
7810#ifdef _SC_SEMAPHORES
7811 {"SC_SEMAPHORES", _SC_SEMAPHORES},
7812#endif
7813#ifdef _SC_SEM_NSEMS_MAX
7814 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
7815#endif
7816#ifdef _SC_SEM_VALUE_MAX
7817 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
7818#endif
7819#ifdef _SC_SHARED_MEMORY_OBJECTS
7820 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
7821#endif
7822#ifdef _SC_SHRT_MAX
7823 {"SC_SHRT_MAX", _SC_SHRT_MAX},
7824#endif
7825#ifdef _SC_SHRT_MIN
7826 {"SC_SHRT_MIN", _SC_SHRT_MIN},
7827#endif
7828#ifdef _SC_SIGQUEUE_MAX
7829 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
7830#endif
7831#ifdef _SC_SIGRT_MAX
7832 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
7833#endif
7834#ifdef _SC_SIGRT_MIN
7835 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
7836#endif
Fred Draked86ed291999-12-15 15:34:33 +00007837#ifdef _SC_SOFTPOWER
7838 {"SC_SOFTPOWER", _SC_SOFTPOWER},
7839#endif
Fred Drakec9680921999-12-13 16:37:25 +00007840#ifdef _SC_SPLIT_CACHE
7841 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
7842#endif
7843#ifdef _SC_SSIZE_MAX
7844 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
7845#endif
7846#ifdef _SC_STACK_PROT
7847 {"SC_STACK_PROT", _SC_STACK_PROT},
7848#endif
7849#ifdef _SC_STREAM_MAX
7850 {"SC_STREAM_MAX", _SC_STREAM_MAX},
7851#endif
7852#ifdef _SC_SYNCHRONIZED_IO
7853 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
7854#endif
7855#ifdef _SC_THREADS
7856 {"SC_THREADS", _SC_THREADS},
7857#endif
7858#ifdef _SC_THREAD_ATTR_STACKADDR
7859 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
7860#endif
7861#ifdef _SC_THREAD_ATTR_STACKSIZE
7862 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
7863#endif
7864#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7865 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
7866#endif
7867#ifdef _SC_THREAD_KEYS_MAX
7868 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
7869#endif
7870#ifdef _SC_THREAD_PRIORITY_SCHEDULING
7871 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
7872#endif
7873#ifdef _SC_THREAD_PRIO_INHERIT
7874 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
7875#endif
7876#ifdef _SC_THREAD_PRIO_PROTECT
7877 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
7878#endif
7879#ifdef _SC_THREAD_PROCESS_SHARED
7880 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
7881#endif
7882#ifdef _SC_THREAD_SAFE_FUNCTIONS
7883 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
7884#endif
7885#ifdef _SC_THREAD_STACK_MIN
7886 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
7887#endif
7888#ifdef _SC_THREAD_THREADS_MAX
7889 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
7890#endif
7891#ifdef _SC_TIMERS
7892 {"SC_TIMERS", _SC_TIMERS},
7893#endif
7894#ifdef _SC_TIMER_MAX
7895 {"SC_TIMER_MAX", _SC_TIMER_MAX},
7896#endif
7897#ifdef _SC_TTY_NAME_MAX
7898 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
7899#endif
7900#ifdef _SC_TZNAME_MAX
7901 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
7902#endif
7903#ifdef _SC_T_IOV_MAX
7904 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
7905#endif
7906#ifdef _SC_UCHAR_MAX
7907 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
7908#endif
7909#ifdef _SC_UINT_MAX
7910 {"SC_UINT_MAX", _SC_UINT_MAX},
7911#endif
7912#ifdef _SC_UIO_MAXIOV
7913 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
7914#endif
7915#ifdef _SC_ULONG_MAX
7916 {"SC_ULONG_MAX", _SC_ULONG_MAX},
7917#endif
7918#ifdef _SC_USHRT_MAX
7919 {"SC_USHRT_MAX", _SC_USHRT_MAX},
7920#endif
7921#ifdef _SC_VERSION
7922 {"SC_VERSION", _SC_VERSION},
7923#endif
7924#ifdef _SC_WORD_BIT
7925 {"SC_WORD_BIT", _SC_WORD_BIT},
7926#endif
7927#ifdef _SC_XBS5_ILP32_OFF32
7928 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
7929#endif
7930#ifdef _SC_XBS5_ILP32_OFFBIG
7931 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
7932#endif
7933#ifdef _SC_XBS5_LP64_OFF64
7934 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
7935#endif
7936#ifdef _SC_XBS5_LPBIG_OFFBIG
7937 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
7938#endif
7939#ifdef _SC_XOPEN_CRYPT
7940 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
7941#endif
7942#ifdef _SC_XOPEN_ENH_I18N
7943 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
7944#endif
7945#ifdef _SC_XOPEN_LEGACY
7946 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
7947#endif
7948#ifdef _SC_XOPEN_REALTIME
7949 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
7950#endif
7951#ifdef _SC_XOPEN_REALTIME_THREADS
7952 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
7953#endif
7954#ifdef _SC_XOPEN_SHM
7955 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
7956#endif
7957#ifdef _SC_XOPEN_UNIX
7958 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
7959#endif
7960#ifdef _SC_XOPEN_VERSION
7961 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
7962#endif
7963#ifdef _SC_XOPEN_XCU_VERSION
7964 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
7965#endif
7966#ifdef _SC_XOPEN_XPG2
7967 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
7968#endif
7969#ifdef _SC_XOPEN_XPG3
7970 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
7971#endif
7972#ifdef _SC_XOPEN_XPG4
7973 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
7974#endif
7975};
7976
7977static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007978conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007979{
7980 return conv_confname(arg, valuep, posix_constants_sysconf,
7981 sizeof(posix_constants_sysconf)
7982 / sizeof(struct constdef));
7983}
7984
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007985PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007986"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007987Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007988
7989static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007990posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007991{
7992 PyObject *result = NULL;
7993 int name;
7994
7995 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7996 int value;
7997
7998 errno = 0;
7999 value = sysconf(name);
8000 if (value == -1 && errno != 0)
8001 posix_error();
8002 else
8003 result = PyInt_FromLong(value);
8004 }
8005 return result;
8006}
8007#endif
8008
8009
Fred Drakebec628d1999-12-15 18:31:10 +00008010/* This code is used to ensure that the tables of configuration value names
8011 * are in sorted order as required by conv_confname(), and also to build the
8012 * the exported dictionaries that are used to publish information about the
8013 * names available on the host platform.
8014 *
8015 * Sorting the table at runtime ensures that the table is properly ordered
8016 * when used, even for platforms we're not able to test on. It also makes
8017 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008018 */
Fred Drakebec628d1999-12-15 18:31:10 +00008019
8020static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008021cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008022{
8023 const struct constdef *c1 =
8024 (const struct constdef *) v1;
8025 const struct constdef *c2 =
8026 (const struct constdef *) v2;
8027
8028 return strcmp(c1->name, c2->name);
8029}
8030
8031static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008032setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008033 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008034{
Fred Drakebec628d1999-12-15 18:31:10 +00008035 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008036 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008037
8038 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8039 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008040 if (d == NULL)
8041 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008042
Barry Warsaw3155db32000-04-13 15:20:40 +00008043 for (i=0; i < tablesize; ++i) {
8044 PyObject *o = PyInt_FromLong(table[i].value);
8045 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8046 Py_XDECREF(o);
8047 Py_DECREF(d);
8048 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008049 }
Barry Warsaw3155db32000-04-13 15:20:40 +00008050 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008051 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008052 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008053}
8054
Fred Drakebec628d1999-12-15 18:31:10 +00008055/* Return -1 on failure, 0 on success. */
8056static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008057setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008058{
8059#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008060 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008061 sizeof(posix_constants_pathconf)
8062 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008063 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008064 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008065#endif
8066#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008067 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008068 sizeof(posix_constants_confstr)
8069 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008070 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008071 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008072#endif
8073#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008074 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008075 sizeof(posix_constants_sysconf)
8076 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008077 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00008078 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008079#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008080 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008081}
Fred Draked86ed291999-12-15 15:34:33 +00008082
8083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008084PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008085"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008086Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008087in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008088
8089static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008090posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008091{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008092 abort();
8093 /*NOTREACHED*/
8094 Py_FatalError("abort() called from Python code didn't abort!");
8095 return NULL;
8096}
Fred Drakebec628d1999-12-15 18:31:10 +00008097
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008098#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008099PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008100"startfile(filepath [, operation]) - Start a file with its associated\n\
8101application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008102\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008103When \"operation\" is not specified or \"open\", this acts like\n\
8104double-clicking the file in Explorer, or giving the file name as an\n\
8105argument to the DOS \"start\" command: the file is opened with whatever\n\
8106application (if any) its extension is associated.\n\
8107When another \"operation\" is given, it specifies what should be done with\n\
8108the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008109\n\
8110startfile returns as soon as the associated application is launched.\n\
8111There is no option to wait for the application to close, and no way\n\
8112to retrieve the application's exit status.\n\
8113\n\
8114The filepath is relative to the current directory. If you want to use\n\
8115an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008116the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008117
8118static PyObject *
8119win32_startfile(PyObject *self, PyObject *args)
8120{
8121 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00008122 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008123 HINSTANCE rc;
Georg Brandlad89dc82006-04-03 12:26:26 +00008124#ifdef Py_WIN_WIDE_FILENAMES
8125 if (unicode_file_names()) {
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008126 PyObject *unipath, *woperation = NULL;
Georg Brandlad89dc82006-04-03 12:26:26 +00008127 if (!PyArg_ParseTuple(args, "U|s:startfile",
8128 &unipath, &operation)) {
8129 PyErr_Clear();
8130 goto normal;
8131 }
8132
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008133
8134 if (operation) {
8135 woperation = PyUnicode_DecodeASCII(operation,
8136 strlen(operation), NULL);
8137 if (!woperation) {
8138 PyErr_Clear();
8139 operation = NULL;
8140 goto normal;
8141 }
Georg Brandlad89dc82006-04-03 12:26:26 +00008142 }
8143
8144 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008145 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
Georg Brandlad89dc82006-04-03 12:26:26 +00008146 PyUnicode_AS_UNICODE(unipath),
Georg Brandlad89dc82006-04-03 12:26:26 +00008147 NULL, NULL, SW_SHOWNORMAL);
8148 Py_END_ALLOW_THREADS
8149
Martin v. Löwis5fe715f2006-04-03 23:01:24 +00008150 Py_XDECREF(woperation);
Georg Brandlad89dc82006-04-03 12:26:26 +00008151 if (rc <= (HINSTANCE)32) {
8152 PyObject *errval = win32_error_unicode("startfile",
8153 PyUnicode_AS_UNICODE(unipath));
8154 return errval;
8155 }
8156 Py_INCREF(Py_None);
8157 return Py_None;
8158 }
8159#endif
8160
8161normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00008162 if (!PyArg_ParseTuple(args, "et|s:startfile",
8163 Py_FileSystemDefaultEncoding, &filepath,
8164 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00008165 return NULL;
8166 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00008167 rc = ShellExecute((HWND)0, operation, filepath,
8168 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008169 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00008170 if (rc <= (HINSTANCE)32) {
8171 PyObject *errval = win32_error("startfile", filepath);
8172 PyMem_Free(filepath);
8173 return errval;
8174 }
8175 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00008176 Py_INCREF(Py_None);
8177 return Py_None;
8178}
8179#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008180
Martin v. Löwis438b5342002-12-27 10:16:42 +00008181#ifdef HAVE_GETLOADAVG
8182PyDoc_STRVAR(posix_getloadavg__doc__,
8183"getloadavg() -> (float, float, float)\n\n\
8184Return the number of processes in the system run queue averaged over\n\
8185the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8186was unobtainable");
8187
8188static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008189posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008190{
8191 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008192 if (getloadavg(loadavg, 3)!=3) {
8193 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8194 return NULL;
8195 } else
8196 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
8197}
8198#endif
8199
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008200#ifdef MS_WINDOWS
8201
8202PyDoc_STRVAR(win32_urandom__doc__,
8203"urandom(n) -> str\n\n\
8204Return a string of n random bytes suitable for cryptographic use.");
8205
8206typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8207 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8208 DWORD dwFlags );
8209typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8210 BYTE *pbBuffer );
8211
8212static CRYPTGENRANDOM pCryptGenRandom = NULL;
Martin v. Löwisaf699dd2007-09-04 09:51:57 +00008213/* This handle is never explicitly released. Instead, the operating
8214 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008215static HCRYPTPROV hCryptProv = 0;
8216
Tim Peters4ad82172004-08-30 17:02:04 +00008217static PyObject*
8218win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008219{
Tim Petersd3115382004-08-30 17:36:46 +00008220 int howMany;
8221 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008222
Tim Peters4ad82172004-08-30 17:02:04 +00008223 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00008224 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00008225 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00008226 if (howMany < 0)
8227 return PyErr_Format(PyExc_ValueError,
8228 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008229
Tim Peters4ad82172004-08-30 17:02:04 +00008230 if (hCryptProv == 0) {
8231 HINSTANCE hAdvAPI32 = NULL;
8232 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008233
Tim Peters4ad82172004-08-30 17:02:04 +00008234 /* Obtain handle to the DLL containing CryptoAPI
8235 This should not fail */
8236 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8237 if(hAdvAPI32 == NULL)
8238 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008239
Tim Peters4ad82172004-08-30 17:02:04 +00008240 /* Obtain pointers to the CryptoAPI functions
8241 This will fail on some early versions of Win95 */
8242 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8243 hAdvAPI32,
8244 "CryptAcquireContextA");
8245 if (pCryptAcquireContext == NULL)
8246 return PyErr_Format(PyExc_NotImplementedError,
8247 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008248
Tim Peters4ad82172004-08-30 17:02:04 +00008249 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8250 hAdvAPI32, "CryptGenRandom");
Georg Brandl74bb7832006-09-06 06:03:59 +00008251 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00008252 return PyErr_Format(PyExc_NotImplementedError,
8253 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008254
Tim Peters4ad82172004-08-30 17:02:04 +00008255 /* Acquire context */
8256 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8257 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8258 return win32_error("CryptAcquireContext", NULL);
8259 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008260
Tim Peters4ad82172004-08-30 17:02:04 +00008261 /* Allocate bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008262 result = PyString_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00008263 if (result != NULL) {
8264 /* Get random data */
Amaury Forgeot d'Arc74bd40d2008-07-21 21:06:46 +00008265 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00008266 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008267 PyString_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00008268 Py_DECREF(result);
8269 return win32_error("CryptGenRandom", NULL);
8270 }
Tim Peters4ad82172004-08-30 17:02:04 +00008271 }
Tim Petersd3115382004-08-30 17:36:46 +00008272 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008273}
8274#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008275
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008276#ifdef __VMS
8277/* Use openssl random routine */
8278#include <openssl/rand.h>
8279PyDoc_STRVAR(vms_urandom__doc__,
8280"urandom(n) -> str\n\n\
8281Return a string of n random bytes suitable for cryptographic use.");
8282
8283static PyObject*
8284vms_urandom(PyObject *self, PyObject *args)
8285{
8286 int howMany;
8287 PyObject* result;
8288
8289 /* Read arguments */
8290 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8291 return NULL;
8292 if (howMany < 0)
8293 return PyErr_Format(PyExc_ValueError,
8294 "negative argument not allowed");
8295
8296 /* Allocate bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008297 result = PyString_FromStringAndSize(NULL, howMany);
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008298 if (result != NULL) {
8299 /* Get random data */
8300 if (RAND_pseudo_bytes((unsigned char*)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00008301 PyString_AS_STRING(result),
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008302 howMany) < 0) {
8303 Py_DECREF(result);
8304 return PyErr_Format(PyExc_ValueError,
8305 "RAND_pseudo_bytes");
8306 }
8307 }
8308 return result;
8309}
8310#endif
8311
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008312static PyMethodDef posix_methods[] = {
8313 {"access", posix_access, METH_VARARGS, posix_access__doc__},
8314#ifdef HAVE_TTYNAME
8315 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
8316#endif
8317 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Martin v. Löwis382abef2007-02-19 10:55:19 +00008318#ifdef HAVE_CHFLAGS
8319 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
8320#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008321 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes36281872007-11-30 21:11:28 +00008322#ifdef HAVE_FCHMOD
8323 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
8324#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008325#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008326 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008327#endif /* HAVE_CHOWN */
Christian Heimes36281872007-11-30 21:11:28 +00008328#ifdef HAVE_LCHMOD
8329 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
8330#endif /* HAVE_LCHMOD */
8331#ifdef HAVE_FCHOWN
8332 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
8333#endif /* HAVE_FCHOWN */
Martin v. Löwis382abef2007-02-19 10:55:19 +00008334#ifdef HAVE_LCHFLAGS
8335 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
8336#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00008337#ifdef HAVE_LCHOWN
8338 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
8339#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00008340#ifdef HAVE_CHROOT
8341 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
8342#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008343#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00008344 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008345#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00008346#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00008347 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00008348#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00008349 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00008350#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00008351#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00008352#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008353 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008354#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008355 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
8356 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
8357 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008358#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008359 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008360#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008361#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008362 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008363#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008364 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
8365 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
8366 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00008367 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008368#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008369 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008370#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008371#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008372 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008373#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008374 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008375#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00008376 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008377#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008378 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
8379 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
8380 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008381#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00008382 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008383#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008384 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008385#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008386 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
8387 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008388#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00008389#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008390 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
8391 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00008392#if defined(PYOS_OS2)
8393 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
8394 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
8395#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00008396#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008397#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00008398 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00008399#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008400#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00008401 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008402#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008403#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00008404 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00008405#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00008406#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00008407 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00008408#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008409#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008410 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008411#endif /* HAVE_GETEGID */
8412#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008413 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008414#endif /* HAVE_GETEUID */
8415#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00008416 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008417#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00008418#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00008419 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008420#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008421 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00008422#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008423 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008424#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008425#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00008426 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008427#endif /* HAVE_GETPPID */
8428#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00008429 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008430#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00008431#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00008432 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00008433#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00008434#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008435 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008436#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00008437#ifdef HAVE_KILLPG
8438 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
8439#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00008440#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008441 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00008442#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008443#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008444 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008445#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008446 {"popen2", win32_popen2, METH_VARARGS},
8447 {"popen3", win32_popen3, METH_VARARGS},
8448 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00008449 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008450#else
8451#if defined(PYOS_OS2) && defined(PYCC_GCC)
8452 {"popen2", os2emx_popen2, METH_VARARGS},
8453 {"popen3", os2emx_popen3, METH_VARARGS},
8454 {"popen4", os2emx_popen4, METH_VARARGS},
8455#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00008456#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008457#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008458#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008459 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008460#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008461#ifdef HAVE_SETEUID
8462 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
8463#endif /* HAVE_SETEUID */
8464#ifdef HAVE_SETEGID
8465 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
8466#endif /* HAVE_SETEGID */
8467#ifdef HAVE_SETREUID
8468 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
8469#endif /* HAVE_SETREUID */
8470#ifdef HAVE_SETREGID
8471 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
8472#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008473#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008474 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008475#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008476#ifdef HAVE_SETGROUPS
Georg Brandl96a8c392006-05-29 21:04:52 +00008477 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008478#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00008479#ifdef HAVE_GETPGID
8480 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
8481#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008482#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00008483 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008484#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00008485#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00008486 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00008487#endif /* HAVE_WAIT */
Neal Norwitz05a45592006-03-20 06:30:08 +00008488#ifdef HAVE_WAIT3
8489 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
8490#endif /* HAVE_WAIT3 */
8491#ifdef HAVE_WAIT4
8492 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
8493#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00008494#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008495 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008496#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008497#ifdef HAVE_GETSID
8498 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
8499#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008500#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00008501 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008502#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008503#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008504 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008505#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008506#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008507 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008508#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008509#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008510 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008511#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008512 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8513 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Georg Brandl309501a2008-01-19 20:22:13 +00008514 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008515 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8516 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8517 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8518 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8519 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8520 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8521 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00008522 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008523#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00008524 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008525#endif
8526#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008527 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008528#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008529#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008530 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
8531#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008532#ifdef HAVE_DEVICE_MACROS
8533 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8534 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8535 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
8536#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008537#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008538 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008539#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008540#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008541 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008542#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008543#ifdef HAVE_UNSETENV
8544 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
8545#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008546 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008547#ifdef HAVE_FCHDIR
8548 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
8549#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008550#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008551 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008552#endif
8553#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00008554 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008555#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008556#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008557#ifdef WCOREDUMP
8558 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
8559#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008560#ifdef WIFCONTINUED
8561 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
8562#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008563#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008564 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008565#endif /* WIFSTOPPED */
8566#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008567 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008568#endif /* WIFSIGNALED */
8569#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008570 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008571#endif /* WIFEXITED */
8572#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008573 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008574#endif /* WEXITSTATUS */
8575#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008576 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008577#endif /* WTERMSIG */
8578#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008579 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008580#endif /* WSTOPSIG */
8581#endif /* HAVE_SYS_WAIT_H */
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008582#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008583 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008584#endif
Martin v. Löwis5f5d99c2006-05-16 07:05:37 +00008585#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008586 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008587#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00008588#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00008589 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008590#endif
8591#ifdef HAVE_TEMPNAM
8592 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
8593#endif
8594#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00008595 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008596#endif
Fred Drakec9680921999-12-13 16:37:25 +00008597#ifdef HAVE_CONFSTR
8598 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
8599#endif
8600#ifdef HAVE_SYSCONF
8601 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
8602#endif
8603#ifdef HAVE_FPATHCONF
8604 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
8605#endif
8606#ifdef HAVE_PATHCONF
8607 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
8608#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00008609 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008610#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00008611 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
8612#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008613#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00008614 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008615#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008616 #ifdef MS_WINDOWS
8617 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
8618 #endif
Neal Norwitz2a30cd02006-07-10 01:18:57 +00008619 #ifdef __VMS
8620 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
8621 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008622 {NULL, NULL} /* Sentinel */
8623};
8624
8625
Barry Warsaw4a342091996-12-19 23:50:02 +00008626static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008627ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008628{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008629 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008630}
8631
Guido van Rossumd48f2521997-12-05 22:19:34 +00008632#if defined(PYOS_OS2)
8633/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008634static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008635{
8636 APIRET rc;
8637 ULONG values[QSV_MAX+1];
8638 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008639 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008640
8641 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008642 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008643 Py_END_ALLOW_THREADS
8644
8645 if (rc != NO_ERROR) {
8646 os2_error(rc);
8647 return -1;
8648 }
8649
Fred Drake4d1e64b2002-04-15 19:40:07 +00008650 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8651 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8652 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8653 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8654 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8655 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8656 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008657
8658 switch (values[QSV_VERSION_MINOR]) {
8659 case 0: ver = "2.00"; break;
8660 case 10: ver = "2.10"; break;
8661 case 11: ver = "2.11"; break;
8662 case 30: ver = "3.00"; break;
8663 case 40: ver = "4.00"; break;
8664 case 50: ver = "5.00"; break;
8665 default:
Tim Peters885d4572001-11-28 20:27:42 +00008666 PyOS_snprintf(tmp, sizeof(tmp),
8667 "%d-%d", values[QSV_VERSION_MAJOR],
8668 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008669 ver = &tmp[0];
8670 }
8671
8672 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008673 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008674 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008675
8676 /* Add Indicator of Which Drive was Used to Boot the System */
8677 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8678 tmp[1] = ':';
8679 tmp[2] = '\0';
8680
Fred Drake4d1e64b2002-04-15 19:40:07 +00008681 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008682}
8683#endif
8684
Barry Warsaw4a342091996-12-19 23:50:02 +00008685static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008686all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008687{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008688#ifdef F_OK
8689 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008690#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008691#ifdef R_OK
8692 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008693#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008694#ifdef W_OK
8695 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008696#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008697#ifdef X_OK
8698 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008699#endif
Fred Drakec9680921999-12-13 16:37:25 +00008700#ifdef NGROUPS_MAX
8701 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
8702#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008703#ifdef TMP_MAX
8704 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
8705#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008706#ifdef WCONTINUED
8707 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
8708#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008709#ifdef WNOHANG
8710 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008711#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008712#ifdef WUNTRACED
8713 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
8714#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008715#ifdef O_RDONLY
8716 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
8717#endif
8718#ifdef O_WRONLY
8719 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
8720#endif
8721#ifdef O_RDWR
8722 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
8723#endif
8724#ifdef O_NDELAY
8725 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
8726#endif
8727#ifdef O_NONBLOCK
8728 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
8729#endif
8730#ifdef O_APPEND
8731 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
8732#endif
8733#ifdef O_DSYNC
8734 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
8735#endif
8736#ifdef O_RSYNC
8737 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
8738#endif
8739#ifdef O_SYNC
8740 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
8741#endif
8742#ifdef O_NOCTTY
8743 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
8744#endif
8745#ifdef O_CREAT
8746 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
8747#endif
8748#ifdef O_EXCL
8749 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
8750#endif
8751#ifdef O_TRUNC
8752 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
8753#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008754#ifdef O_BINARY
8755 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
8756#endif
8757#ifdef O_TEXT
8758 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
8759#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008760#ifdef O_LARGEFILE
8761 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
8762#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008763#ifdef O_SHLOCK
8764 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
8765#endif
8766#ifdef O_EXLOCK
8767 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
8768#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008769
Tim Peters5aa91602002-01-30 05:46:57 +00008770/* MS Windows */
8771#ifdef O_NOINHERIT
8772 /* Don't inherit in child processes. */
8773 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
8774#endif
8775#ifdef _O_SHORT_LIVED
8776 /* Optimize for short life (keep in memory). */
8777 /* MS forgot to define this one with a non-underscore form too. */
8778 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
8779#endif
8780#ifdef O_TEMPORARY
8781 /* Automatically delete when last handle is closed. */
8782 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
8783#endif
8784#ifdef O_RANDOM
8785 /* Optimize for random access. */
8786 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
8787#endif
8788#ifdef O_SEQUENTIAL
8789 /* Optimize for sequential access. */
8790 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
8791#endif
8792
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008793/* GNU extensions. */
Georg Brandl5049a852008-05-16 13:10:15 +00008794#ifdef O_ASYNC
8795 /* Send a SIGIO signal whenever input or output
8796 becomes available on file descriptor */
8797 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
8798#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008799#ifdef O_DIRECT
8800 /* Direct disk access. */
8801 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
8802#endif
8803#ifdef O_DIRECTORY
8804 /* Must be a directory. */
8805 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
8806#endif
8807#ifdef O_NOFOLLOW
8808 /* Do not follow links. */
8809 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
8810#endif
Georg Brandlb67da6e2007-11-24 13:56:09 +00008811#ifdef O_NOATIME
8812 /* Do not update the access time. */
8813 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
8814#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008815
Barry Warsaw5676bd12003-01-07 20:57:09 +00008816 /* These come from sysexits.h */
8817#ifdef EX_OK
8818 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008819#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008820#ifdef EX_USAGE
8821 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008822#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008823#ifdef EX_DATAERR
8824 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008825#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008826#ifdef EX_NOINPUT
8827 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008828#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008829#ifdef EX_NOUSER
8830 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008831#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008832#ifdef EX_NOHOST
8833 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008834#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008835#ifdef EX_UNAVAILABLE
8836 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008837#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008838#ifdef EX_SOFTWARE
8839 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008840#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008841#ifdef EX_OSERR
8842 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008843#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008844#ifdef EX_OSFILE
8845 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008846#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008847#ifdef EX_CANTCREAT
8848 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008849#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008850#ifdef EX_IOERR
8851 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008852#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008853#ifdef EX_TEMPFAIL
8854 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008855#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008856#ifdef EX_PROTOCOL
8857 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008858#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008859#ifdef EX_NOPERM
8860 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008861#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008862#ifdef EX_CONFIG
8863 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008864#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008865#ifdef EX_NOTFOUND
8866 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008867#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008868
Guido van Rossum246bc171999-02-01 23:54:31 +00008869#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008870#if defined(PYOS_OS2) && defined(PYCC_GCC)
8871 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8872 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8873 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8874 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8875 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8876 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8877 if (ins(d, "P_PM", (long)P_PM)) return -1;
8878 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8879 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8880 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8881 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8882 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8883 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8884 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8885 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8886 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8887 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8888 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8889 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8890 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
8891#else
Guido van Rossum7d385291999-02-16 19:38:04 +00008892 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8893 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8894 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8895 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8896 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008897#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008898#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008899
Guido van Rossumd48f2521997-12-05 22:19:34 +00008900#if defined(PYOS_OS2)
8901 if (insertvalues(d)) return -1;
8902#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008903 return 0;
8904}
8905
8906
Tim Peters5aa91602002-01-30 05:46:57 +00008907#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008908#define INITFUNC initnt
8909#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008910
8911#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008912#define INITFUNC initos2
8913#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008914
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008915#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008916#define INITFUNC initposix
8917#define MODNAME "posix"
8918#endif
8919
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008920PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008921INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008922{
Fred Drake4d1e64b2002-04-15 19:40:07 +00008923 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008924
Fred Drake4d1e64b2002-04-15 19:40:07 +00008925 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008926 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00008927 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00008928 if (m == NULL)
8929 return;
Tim Peters5aa91602002-01-30 05:46:57 +00008930
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008931 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008932 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00008933 Py_XINCREF(v);
8934 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008935 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00008936 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008937
Fred Drake4d1e64b2002-04-15 19:40:07 +00008938 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00008939 return;
8940
Fred Drake4d1e64b2002-04-15 19:40:07 +00008941 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00008942 return;
8943
Fred Drake4d1e64b2002-04-15 19:40:07 +00008944 Py_INCREF(PyExc_OSError);
8945 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008946
Guido van Rossumb3d39562000-01-31 18:41:26 +00008947#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00008948 if (posix_putenv_garbage == NULL)
8949 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008950#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008951
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008952 if (!initialized) {
8953 stat_result_desc.name = MODNAME ".stat_result";
8954 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8955 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8956 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8957 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8958 structseq_new = StatResultType.tp_new;
8959 StatResultType.tp_new = statresult_new;
8960
8961 statvfs_result_desc.name = MODNAME ".statvfs_result";
8962 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis03824e42008-12-29 18:17:34 +00008963#ifdef NEED_TICKS_PER_SECOND
8964# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
8965 ticks_per_second = sysconf(_SC_CLK_TCK);
8966# elif defined(HZ)
8967 ticks_per_second = HZ;
8968# else
8969 ticks_per_second = 60; /* magic fallback value; may be bogus */
8970# endif
8971#endif
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008972 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008973 Py_INCREF((PyObject*) &StatResultType);
8974 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00008975 Py_INCREF((PyObject*) &StatVFSResultType);
8976 PyModule_AddObject(m, "statvfs_result",
8977 (PyObject*) &StatVFSResultType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +00008978 initialized = 1;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008979
8980#ifdef __APPLE__
8981 /*
8982 * Step 2 of weak-linking support on Mac OS X.
8983 *
8984 * The code below removes functions that are not available on the
8985 * currently active platform.
8986 *
8987 * This block allow one to use a python binary that was build on
8988 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8989 * OSX 10.4.
8990 */
8991#ifdef HAVE_FSTATVFS
8992 if (fstatvfs == NULL) {
8993 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8994 return;
8995 }
8996 }
8997#endif /* HAVE_FSTATVFS */
8998
8999#ifdef HAVE_STATVFS
9000 if (statvfs == NULL) {
9001 if (PyObject_DelAttrString(m, "statvfs") == -1) {
9002 return;
9003 }
9004 }
9005#endif /* HAVE_STATVFS */
9006
9007# ifdef HAVE_LCHOWN
9008 if (lchown == NULL) {
9009 if (PyObject_DelAttrString(m, "lchown") == -1) {
9010 return;
9011 }
9012 }
9013#endif /* HAVE_LCHOWN */
9014
9015
9016#endif /* __APPLE__ */
9017
Guido van Rossumb6775db1994-08-01 11:34:53 +00009018}
Anthony Baxterac6bd462006-04-13 02:06:09 +00009019
9020#ifdef __cplusplus
9021}
9022#endif
9023
Martin v. Löwis18aaa562006-10-15 08:43:33 +00009024