blob: 716bf7d78a140a16ef4b160f2940f5571b637fbb [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.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
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000189/*#ifdef HAVE_FCHMOD
190extern int fchmod(int, mode_t);
191#endif*/
192/*#ifdef HAVE_LCHMOD
193extern int lchmod(const char *, mode_t);
194#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chown(const char *, uid_t, gid_t);
196extern char *getcwd(char *, int);
197extern char *strerror(int);
198extern int link(const char *, const char *);
199extern int rename(const char *, const char *);
200extern int stat(const char *, struct stat *);
201extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000267#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000268#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
Guido van Rossumd48f2521997-12-05 22:19:34 +0000270#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000272#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275#if defined(PATH_MAX) && PATH_MAX > 1024
276#define MAXPATHLEN PATH_MAX
277#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000278#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000279#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000280#endif /* MAXPATHLEN */
281
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000282#ifdef UNION_WAIT
283/* Emulate some macros on systems that have a union instead of macros */
284
285#ifndef WIFEXITED
286#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
287#endif
288
289#ifndef WEXITSTATUS
290#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
291#endif
292
293#ifndef WTERMSIG
294#define WTERMSIG(u_wait) ((u_wait).w_termsig)
295#endif
296
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297#define WAIT_TYPE union wait
298#define WAIT_STATUS_INT(s) (s.w_status)
299
300#else /* !UNION_WAIT */
301#define WAIT_TYPE int
302#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000303#endif /* UNION_WAIT */
304
Greg Wardb48bc172000-03-01 21:51:56 +0000305/* Don't use the "_r" form if we don't need it (also, won't have a
306 prototype for it, at least on Solaris -- maybe others as well?). */
307#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
308#define USE_CTERMID_R
309#endif
310
Fred Drake699f3522000-06-29 21:12:41 +0000311/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000312#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000313#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000314# define STAT win32_stat
315# define FSTAT win32_fstat
316# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000317#else
318# define STAT stat
319# define FSTAT fstat
320# define STRUCT_STAT struct stat
321#endif
322
Tim Peters11b23062003-04-23 02:39:17 +0000323#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000324#include <sys/mkdev.h>
325#else
326#if defined(MAJOR_IN_SYSMACROS)
327#include <sys/sysmacros.h>
328#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000329#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
330#include <sys/mkdev.h>
331#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000332#endif
Fred Drake699f3522000-06-29 21:12:41 +0000333
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000335#ifdef WITH_NEXT_FRAMEWORK
336/* On Darwin/MacOSX a shared library or framework has no access to
337** environ directly, we must obtain it with _NSGetEnviron().
338*/
339#include <crt_externs.h>
340static char **environ;
341#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000343#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344
Barry Warsaw53699e91996-12-10 23:23:01 +0000345static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000346convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347{
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000349#ifdef MS_WINDOWS
350 wchar_t **e;
351#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000353#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000354 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 if (d == NULL)
356 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000357#ifdef WITH_NEXT_FRAMEWORK
358 if (environ == NULL)
359 environ = *_NSGetEnviron();
360#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000361#ifdef MS_WINDOWS
362 /* _wenviron must be initialized in this way if the program is started
363 through main() instead of wmain(). */
364 _wgetenv(L"");
365 if (_wenviron == NULL)
366 return d;
367 /* This part ignores errors */
368 for (e = _wenviron; *e != NULL; e++) {
369 PyObject *k;
370 PyObject *v;
371 wchar_t *p = wcschr(*e, L'=');
372 if (p == NULL)
373 continue;
374 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
375 if (k == NULL) {
376 PyErr_Clear();
377 continue;
378 }
379 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
380 if (v == NULL) {
381 PyErr_Clear();
382 Py_DECREF(k);
383 continue;
384 }
385 if (PyDict_GetItem(d, k) == NULL) {
386 if (PyDict_SetItem(d, k, v) != 0)
387 PyErr_Clear();
388 }
389 Py_DECREF(k);
390 Py_DECREF(v);
391 }
392#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393 if (environ == NULL)
394 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000395 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000397 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000398 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399 char *p = strchr(*e, '=');
400 if (p == NULL)
401 continue;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000402 k = PyUnicode_FromStringAndSize(*e, (int)(p-*e));
Guido van Rossum6a619f41999-08-03 19:41:10 +0000403 if (k == NULL) {
404 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000406 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000407 v = PyUnicode_FromString(p+1);
Guido van Rossum6a619f41999-08-03 19:41:10 +0000408 if (v == NULL) {
409 PyErr_Clear();
410 Py_DECREF(k);
411 continue;
412 }
413 if (PyDict_GetItem(d, k) == NULL) {
414 if (PyDict_SetItem(d, k, v) != 0)
415 PyErr_Clear();
416 }
417 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000418 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000420#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000421#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000422 {
423 APIRET rc;
424 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
425
426 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000427 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000428 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000429 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000430 Py_DECREF(v);
431 }
432 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
433 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
434 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000435 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000436 Py_DECREF(v);
437 }
438 }
439#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440 return d;
441}
442
443
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444/* Set a POSIX-specific error from errno, and return NULL */
445
Barry Warsawd58d7641998-07-23 16:14:40 +0000446static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000447posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000448{
Barry Warsawca74da41999-02-09 19:31:45 +0000449 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450}
Barry Warsawd58d7641998-07-23 16:14:40 +0000451static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000452posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000453{
Barry Warsawca74da41999-02-09 19:31:45 +0000454 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000455}
456
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000457#ifdef Py_WIN_WIDE_FILENAMES
458static PyObject *
459posix_error_with_unicode_filename(Py_UNICODE* name)
460{
461 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
462}
463#endif /* Py_WIN_WIDE_FILENAMES */
464
465
Mark Hammondef8b6542001-05-13 08:04:26 +0000466static PyObject *
467posix_error_with_allocated_filename(char* name)
468{
469 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
470 PyMem_Free(name);
471 return rc;
472}
473
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000474#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000475static PyObject *
476win32_error(char* function, char* filename)
477{
Mark Hammond33a6da92000-08-15 00:46:38 +0000478 /* XXX We should pass the function name along in the future.
479 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000480 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000481 Windows error object, which is non-trivial.
482 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000483 errno = GetLastError();
484 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000485 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000486 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000487 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000488}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000489
490#ifdef Py_WIN_WIDE_FILENAMES
491static PyObject *
492win32_error_unicode(char* function, Py_UNICODE* filename)
493{
494 /* XXX - see win32_error for comments on 'function' */
495 errno = GetLastError();
496 if (filename)
497 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
498 else
499 return PyErr_SetFromWindowsErr(errno);
500}
501
502static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
503{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504}
505
506/* Function suitable for O& conversion */
507static int
508convert_to_unicode(PyObject *arg, void* _param)
509{
510 PyObject **param = (PyObject**)_param;
511 if (PyUnicode_CheckExact(arg)) {
512 Py_INCREF(arg);
513 *param = arg;
514 }
515 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000516 /* For a Unicode subtype that's not a Unicode object,
517 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000518 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
519 PyUnicode_GET_SIZE(arg));
520 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000521 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000522 else
523 *param = PyUnicode_FromEncodedObject(arg,
524 Py_FileSystemDefaultEncoding,
525 "strict");
526 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000527}
528
529#endif /* Py_WIN_WIDE_FILENAMES */
530
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000531#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532
Guido van Rossumd48f2521997-12-05 22:19:34 +0000533#if defined(PYOS_OS2)
534/**********************************************************************
535 * Helper Function to Trim and Format OS/2 Messages
536 **********************************************************************/
537 static void
538os2_formatmsg(char *msgbuf, int msglen, char *reason)
539{
540 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
541
542 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
543 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
544
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000545 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000546 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
547 }
548
549 /* Add Optional Reason Text */
550 if (reason) {
551 strcat(msgbuf, " : ");
552 strcat(msgbuf, reason);
553 }
554}
555
556/**********************************************************************
557 * Decode an OS/2 Operating System Error Code
558 *
559 * A convenience function to lookup an OS/2 error code and return a
560 * text message we can use to raise a Python exception.
561 *
562 * Notes:
563 * The messages for errors returned from the OS/2 kernel reside in
564 * the file OSO001.MSG in the \OS2 directory hierarchy.
565 *
566 **********************************************************************/
567 static char *
568os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
569{
570 APIRET rc;
571 ULONG msglen;
572
573 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
574 Py_BEGIN_ALLOW_THREADS
575 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
576 errorcode, "oso001.msg", &msglen);
577 Py_END_ALLOW_THREADS
578
579 if (rc == NO_ERROR)
580 os2_formatmsg(msgbuf, msglen, reason);
581 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000582 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000583 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000584
585 return msgbuf;
586}
587
588/* Set an OS/2-specific error and return NULL. OS/2 kernel
589 errors are not in a global variable e.g. 'errno' nor are
590 they congruent with posix error numbers. */
591
592static PyObject * os2_error(int code)
593{
594 char text[1024];
595 PyObject *v;
596
597 os2_strerror(text, sizeof(text), code, "");
598
599 v = Py_BuildValue("(is)", code, text);
600 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000601 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000602 Py_DECREF(v);
603 }
604 return NULL; /* Signal to Python that an Exception is Pending */
605}
606
607#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608
609/* POSIX generic methods */
610
Barry Warsaw53699e91996-12-10 23:23:01 +0000611static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000612posix_fildes(PyObject *fdobj, int (*func)(int))
613{
614 int fd;
615 int res;
616 fd = PyObject_AsFileDescriptor(fdobj);
617 if (fd < 0)
618 return NULL;
619 Py_BEGIN_ALLOW_THREADS
620 res = (*func)(fd);
621 Py_END_ALLOW_THREADS
622 if (res < 0)
623 return posix_error();
624 Py_INCREF(Py_None);
625 return Py_None;
626}
Guido van Rossum21142a01999-01-08 21:05:37 +0000627
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000628#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000629static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000630unicode_file_names(void)
631{
632 static int canusewide = -1;
633 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000634 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635 the Windows NT family. */
636 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
637 }
638 return canusewide;
639}
640#endif
Tim Peters11b23062003-04-23 02:39:17 +0000641
Guido van Rossum21142a01999-01-08 21:05:37 +0000642static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000643posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644{
Mark Hammondef8b6542001-05-13 08:04:26 +0000645 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000646 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000647 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000648 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000650 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000651 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000652 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000653 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000654 return posix_error_with_allocated_filename(path1);
655 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000656 Py_INCREF(Py_None);
657 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000658}
659
Barry Warsaw53699e91996-12-10 23:23:01 +0000660static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000661posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000662 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664{
Mark Hammondef8b6542001-05-13 08:04:26 +0000665 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000666 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000667 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000668 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000669 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000671 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000672 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000673 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000674 PyMem_Free(path1);
675 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000676 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000677 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000679 Py_INCREF(Py_None);
680 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681}
682
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683#ifdef Py_WIN_WIDE_FILENAMES
684static PyObject*
685win32_1str(PyObject* args, char* func,
686 char* format, BOOL (__stdcall *funcA)(LPCSTR),
687 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
688{
689 PyObject *uni;
690 char *ansi;
691 BOOL result;
692 if (unicode_file_names()) {
693 if (!PyArg_ParseTuple(args, wformat, &uni))
694 PyErr_Clear();
695 else {
696 Py_BEGIN_ALLOW_THREADS
697 result = funcW(PyUnicode_AsUnicode(uni));
698 Py_END_ALLOW_THREADS
699 if (!result)
700 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
701 Py_INCREF(Py_None);
702 return Py_None;
703 }
704 }
705 if (!PyArg_ParseTuple(args, format, &ansi))
706 return NULL;
707 Py_BEGIN_ALLOW_THREADS
708 result = funcA(ansi);
709 Py_END_ALLOW_THREADS
710 if (!result)
711 return win32_error(func, ansi);
712 Py_INCREF(Py_None);
713 return Py_None;
714
715}
716
717/* This is a reimplementation of the C library's chdir function,
718 but one that produces Win32 errors instead of DOS error codes.
719 chdir is essentially a wrapper around SetCurrentDirectory; however,
720 it also needs to set "magic" environment variables indicating
721 the per-drive current directory, which are of the form =<drive>: */
722BOOL __stdcall
723win32_chdir(LPCSTR path)
724{
725 char new_path[MAX_PATH+1];
726 int result;
727 char env[4] = "=x:";
728
729 if(!SetCurrentDirectoryA(path))
730 return FALSE;
731 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
732 if (!result)
733 return FALSE;
734 /* In the ANSI API, there should not be any paths longer
735 than MAX_PATH. */
736 assert(result <= MAX_PATH+1);
737 if (strncmp(new_path, "\\\\", 2) == 0 ||
738 strncmp(new_path, "//", 2) == 0)
739 /* UNC path, nothing to do. */
740 return TRUE;
741 env[1] = new_path[0];
742 return SetEnvironmentVariableA(env, new_path);
743}
744
745/* The Unicode version differs from the ANSI version
746 since the current directory might exceed MAX_PATH characters */
747BOOL __stdcall
748win32_wchdir(LPCWSTR path)
749{
750 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
751 int result;
752 wchar_t env[4] = L"=x:";
753
754 if(!SetCurrentDirectoryW(path))
755 return FALSE;
756 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
757 if (!result)
758 return FALSE;
759 if (result > MAX_PATH+1) {
760 new_path = malloc(result);
761 if (!new_path) {
762 SetLastError(ERROR_OUTOFMEMORY);
763 return FALSE;
764 }
765 }
766 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
767 wcsncmp(new_path, L"//", 2) == 0)
768 /* UNC path, nothing to do. */
769 return TRUE;
770 env[1] = new_path[0];
771 result = SetEnvironmentVariableW(env, new_path);
772 if (new_path != _new_path)
773 free(new_path);
774 return result;
775}
776#endif
777
Martin v. Löwis14694662006-02-03 12:54:16 +0000778#ifdef MS_WINDOWS
779/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
780 - time stamps are restricted to second resolution
781 - file modification times suffer from forth-and-back conversions between
782 UTC and local time
783 Therefore, we implement our own stat, based on the Win32 API directly.
784*/
785#define HAVE_STAT_NSEC 1
786
787struct win32_stat{
788 int st_dev;
789 __int64 st_ino;
790 unsigned short st_mode;
791 int st_nlink;
792 int st_uid;
793 int st_gid;
794 int st_rdev;
795 __int64 st_size;
796 int st_atime;
797 int st_atime_nsec;
798 int st_mtime;
799 int st_mtime_nsec;
800 int st_ctime;
801 int st_ctime_nsec;
802};
803
804static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
805
806static void
807FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
808{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000809 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
810 /* Cannot simply cast and dereference in_ptr,
811 since it might not be aligned properly */
812 __int64 in;
813 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000814 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
815 /* XXX Win32 supports time stamps past 2038; we currently don't */
816 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
817}
818
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819static void
820time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
821{
822 /* XXX endianness */
823 __int64 out;
824 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000825 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826 memcpy(out_ptr, &out, sizeof(out));
827}
828
Martin v. Löwis14694662006-02-03 12:54:16 +0000829/* Below, we *know* that ugo+r is 0444 */
830#if _S_IREAD != 0400
831#error Unsupported C library
832#endif
833static int
834attributes_to_mode(DWORD attr)
835{
836 int m = 0;
837 if (attr & FILE_ATTRIBUTE_DIRECTORY)
838 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
839 else
840 m |= _S_IFREG;
841 if (attr & FILE_ATTRIBUTE_READONLY)
842 m |= 0444;
843 else
844 m |= 0666;
845 return m;
846}
847
848static int
849attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
850{
851 memset(result, 0, sizeof(*result));
852 result->st_mode = attributes_to_mode(info->dwFileAttributes);
853 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
854 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
855 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
856 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
857
858 return 0;
859}
860
Thomas Wouters89f507f2006-12-13 04:49:30 +0000861/* Emulate GetFileAttributesEx[AW] on Windows 95 */
862static int checked = 0;
863static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
864static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
865static void
866check_gfax()
867{
868 HINSTANCE hKernel32;
869 if (checked)
870 return;
871 checked = 1;
872 hKernel32 = GetModuleHandle("KERNEL32");
873 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
874 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
875}
876
Guido van Rossumd8faa362007-04-27 19:54:29 +0000877static BOOL
878attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
879{
880 HANDLE hFindFile;
881 WIN32_FIND_DATAA FileData;
882 hFindFile = FindFirstFileA(pszFile, &FileData);
883 if (hFindFile == INVALID_HANDLE_VALUE)
884 return FALSE;
885 FindClose(hFindFile);
886 pfad->dwFileAttributes = FileData.dwFileAttributes;
887 pfad->ftCreationTime = FileData.ftCreationTime;
888 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
889 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
890 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
891 pfad->nFileSizeLow = FileData.nFileSizeLow;
892 return TRUE;
893}
894
895static BOOL
896attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
897{
898 HANDLE hFindFile;
899 WIN32_FIND_DATAW FileData;
900 hFindFile = FindFirstFileW(pszFile, &FileData);
901 if (hFindFile == INVALID_HANDLE_VALUE)
902 return FALSE;
903 FindClose(hFindFile);
904 pfad->dwFileAttributes = FileData.dwFileAttributes;
905 pfad->ftCreationTime = FileData.ftCreationTime;
906 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
907 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
908 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
909 pfad->nFileSizeLow = FileData.nFileSizeLow;
910 return TRUE;
911}
912
Thomas Wouters89f507f2006-12-13 04:49:30 +0000913static BOOL WINAPI
914Py_GetFileAttributesExA(LPCSTR pszFile,
915 GET_FILEEX_INFO_LEVELS level,
916 LPVOID pv)
917{
918 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +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 = gfaxa(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 (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
939 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000940 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000941}
942
943static BOOL WINAPI
944Py_GetFileAttributesExW(LPCWSTR pszFile,
945 GET_FILEEX_INFO_LEVELS level,
946 LPVOID pv)
947{
948 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000949 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
950 /* First try to use the system's implementation, if that is
951 available and either succeeds to gives an error other than
952 that it isn't implemented. */
953 check_gfax();
954 if (gfaxa) {
955 result = gfaxw(pszFile, level, pv);
956 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
957 return result;
958 }
959 /* It's either not present, or not implemented.
960 Emulate using FindFirstFile. */
961 if (level != GetFileExInfoStandard) {
962 SetLastError(ERROR_INVALID_PARAMETER);
963 return FALSE;
964 }
965 /* Use GetFileAttributes to validate that the file name
966 does not contain wildcards (which FindFirstFile would
967 accept). */
968 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
969 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000970 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000971}
972
Martin v. Löwis14694662006-02-03 12:54:16 +0000973static int
974win32_stat(const char* path, struct win32_stat *result)
975{
976 WIN32_FILE_ATTRIBUTE_DATA info;
977 int code;
978 char *dot;
979 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000980 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000981 if (GetLastError() != ERROR_SHARING_VIOLATION) {
982 /* Protocol violation: we explicitly clear errno, instead of
983 setting it to a POSIX error. Callers should use GetLastError. */
984 errno = 0;
985 return -1;
986 } else {
987 /* Could not get attributes on open file. Fall back to
988 reading the directory. */
989 if (!attributes_from_dir(path, &info)) {
990 /* Very strange. This should not fail now */
991 errno = 0;
992 return -1;
993 }
994 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000995 }
996 code = attribute_data_to_stat(&info, result);
997 if (code != 0)
998 return code;
999 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1000 dot = strrchr(path, '.');
1001 if (dot) {
1002 if (stricmp(dot, ".bat") == 0 ||
1003 stricmp(dot, ".cmd") == 0 ||
1004 stricmp(dot, ".exe") == 0 ||
1005 stricmp(dot, ".com") == 0)
1006 result->st_mode |= 0111;
1007 }
1008 return code;
1009}
1010
1011static int
1012win32_wstat(const wchar_t* path, struct win32_stat *result)
1013{
1014 int code;
1015 const wchar_t *dot;
1016 WIN32_FILE_ATTRIBUTE_DATA info;
1017 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001018 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001019 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1020 /* Protocol violation: we explicitly clear errno, instead of
1021 setting it to a POSIX error. Callers should use GetLastError. */
1022 errno = 0;
1023 return -1;
1024 } else {
1025 /* Could not get attributes on open file. Fall back to
1026 reading the directory. */
1027 if (!attributes_from_dir_w(path, &info)) {
1028 /* Very strange. This should not fail now */
1029 errno = 0;
1030 return -1;
1031 }
1032 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001033 }
1034 code = attribute_data_to_stat(&info, result);
1035 if (code < 0)
1036 return code;
1037 /* Set IFEXEC if it is an .exe, .bat, ... */
1038 dot = wcsrchr(path, '.');
1039 if (dot) {
1040 if (_wcsicmp(dot, L".bat") == 0 ||
1041 _wcsicmp(dot, L".cmd") == 0 ||
1042 _wcsicmp(dot, L".exe") == 0 ||
1043 _wcsicmp(dot, L".com") == 0)
1044 result->st_mode |= 0111;
1045 }
1046 return code;
1047}
1048
1049static int
1050win32_fstat(int file_number, struct win32_stat *result)
1051{
1052 BY_HANDLE_FILE_INFORMATION info;
1053 HANDLE h;
1054 int type;
1055
1056 h = (HANDLE)_get_osfhandle(file_number);
1057
1058 /* Protocol violation: we explicitly clear errno, instead of
1059 setting it to a POSIX error. Callers should use GetLastError. */
1060 errno = 0;
1061
1062 if (h == INVALID_HANDLE_VALUE) {
1063 /* This is really a C library error (invalid file handle).
1064 We set the Win32 error to the closes one matching. */
1065 SetLastError(ERROR_INVALID_HANDLE);
1066 return -1;
1067 }
1068 memset(result, 0, sizeof(*result));
1069
1070 type = GetFileType(h);
1071 if (type == FILE_TYPE_UNKNOWN) {
1072 DWORD error = GetLastError();
1073 if (error != 0) {
1074 return -1;
1075 }
1076 /* else: valid but unknown file */
1077 }
1078
1079 if (type != FILE_TYPE_DISK) {
1080 if (type == FILE_TYPE_CHAR)
1081 result->st_mode = _S_IFCHR;
1082 else if (type == FILE_TYPE_PIPE)
1083 result->st_mode = _S_IFIFO;
1084 return 0;
1085 }
1086
1087 if (!GetFileInformationByHandle(h, &info)) {
1088 return -1;
1089 }
1090
1091 /* similar to stat() */
1092 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1093 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1094 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1095 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1096 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1097 /* specific to fstat() */
1098 result->st_nlink = info.nNumberOfLinks;
1099 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1100 return 0;
1101}
1102
1103#endif /* MS_WINDOWS */
1104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001105PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001106"stat_result: Result from stat or lstat.\n\n\
1107This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001108 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001109or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1110\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001111Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1112or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001113\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001114See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115
1116static PyStructSequence_Field stat_result_fields[] = {
1117 {"st_mode", "protection bits"},
1118 {"st_ino", "inode"},
1119 {"st_dev", "device"},
1120 {"st_nlink", "number of hard links"},
1121 {"st_uid", "user ID of owner"},
1122 {"st_gid", "group ID of owner"},
1123 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001124 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1125 {NULL, "integer time of last access"},
1126 {NULL, "integer time of last modification"},
1127 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001128 {"st_atime", "time of last access"},
1129 {"st_mtime", "time of last modification"},
1130 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001131#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001132 {"st_blksize", "blocksize for filesystem I/O"},
1133#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001134#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001135 {"st_blocks", "number of blocks allocated"},
1136#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001137#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001138 {"st_rdev", "device type (if inode device)"},
1139#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001140#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1141 {"st_flags", "user defined flags for file"},
1142#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001143#ifdef HAVE_STRUCT_STAT_ST_GEN
1144 {"st_gen", "generation number"},
1145#endif
1146#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1147 {"st_birthtime", "time of creation"},
1148#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149 {0}
1150};
1151
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001152#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001153#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001154#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001155#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156#endif
1157
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001158#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001159#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1160#else
1161#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1162#endif
1163
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001164#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001165#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1166#else
1167#define ST_RDEV_IDX ST_BLOCKS_IDX
1168#endif
1169
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001170#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1171#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1172#else
1173#define ST_FLAGS_IDX ST_RDEV_IDX
1174#endif
1175
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001176#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001177#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001178#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001179#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001180#endif
1181
1182#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1183#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1184#else
1185#define ST_BIRTHTIME_IDX ST_GEN_IDX
1186#endif
1187
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001188static PyStructSequence_Desc stat_result_desc = {
1189 "stat_result", /* name */
1190 stat_result__doc__, /* doc */
1191 stat_result_fields,
1192 10
1193};
1194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001195PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001196"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1197This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001198 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001199or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001200\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001201See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001202
1203static PyStructSequence_Field statvfs_result_fields[] = {
1204 {"f_bsize", },
1205 {"f_frsize", },
1206 {"f_blocks", },
1207 {"f_bfree", },
1208 {"f_bavail", },
1209 {"f_files", },
1210 {"f_ffree", },
1211 {"f_favail", },
1212 {"f_flag", },
1213 {"f_namemax",},
1214 {0}
1215};
1216
1217static PyStructSequence_Desc statvfs_result_desc = {
1218 "statvfs_result", /* name */
1219 statvfs_result__doc__, /* doc */
1220 statvfs_result_fields,
1221 10
1222};
1223
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001225static PyTypeObject StatResultType;
1226static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001227static newfunc structseq_new;
1228
1229static PyObject *
1230statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1231{
1232 PyStructSequence *result;
1233 int i;
1234
1235 result = (PyStructSequence*)structseq_new(type, args, kwds);
1236 if (!result)
1237 return NULL;
1238 /* If we have been initialized from a tuple,
1239 st_?time might be set to None. Initialize it
1240 from the int slots. */
1241 for (i = 7; i <= 9; i++) {
1242 if (result->ob_item[i+3] == Py_None) {
1243 Py_DECREF(Py_None);
1244 Py_INCREF(result->ob_item[i]);
1245 result->ob_item[i+3] = result->ob_item[i];
1246 }
1247 }
1248 return (PyObject*)result;
1249}
1250
1251
1252
1253/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001254static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001255
1256PyDoc_STRVAR(stat_float_times__doc__,
1257"stat_float_times([newval]) -> oldval\n\n\
1258Determine whether os.[lf]stat represents time stamps as float objects.\n\
1259If newval is True, future calls to stat() return floats, if it is False,\n\
1260future calls return ints. \n\
1261If newval is omitted, return the current setting.\n");
1262
1263static PyObject*
1264stat_float_times(PyObject* self, PyObject *args)
1265{
1266 int newval = -1;
1267 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1268 return NULL;
1269 if (newval == -1)
1270 /* Return old value */
1271 return PyBool_FromLong(_stat_float_times);
1272 _stat_float_times = newval;
1273 Py_INCREF(Py_None);
1274 return Py_None;
1275}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001276
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001277static void
1278fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1279{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001280 PyObject *fval,*ival;
1281#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001282 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001283#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001284 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001285#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001286 if (!ival)
1287 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001288 if (_stat_float_times) {
1289 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1290 } else {
1291 fval = ival;
1292 Py_INCREF(fval);
1293 }
1294 PyStructSequence_SET_ITEM(v, index, ival);
1295 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001296}
1297
Tim Peters5aa91602002-01-30 05:46:57 +00001298/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001299 (used by posix_stat() and posix_fstat()) */
1300static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001301_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001302{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001303 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001304 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001305 if (v == NULL)
1306 return NULL;
1307
Christian Heimes217cfd12007-12-02 14:31:20 +00001308 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001309#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001310 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001311 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001312#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001313 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001314#endif
1315#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001316 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001318#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001319 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001320#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001321 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1322 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1323 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001324#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001325 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001326 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001327#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001328 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001329#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001330
Martin v. Löwis14694662006-02-03 12:54:16 +00001331#if defined(HAVE_STAT_TV_NSEC)
1332 ansec = st->st_atim.tv_nsec;
1333 mnsec = st->st_mtim.tv_nsec;
1334 cnsec = st->st_ctim.tv_nsec;
1335#elif defined(HAVE_STAT_TV_NSEC2)
1336 ansec = st->st_atimespec.tv_nsec;
1337 mnsec = st->st_mtimespec.tv_nsec;
1338 cnsec = st->st_ctimespec.tv_nsec;
1339#elif defined(HAVE_STAT_NSEC)
1340 ansec = st->st_atime_nsec;
1341 mnsec = st->st_mtime_nsec;
1342 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001343#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001344 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001346 fill_time(v, 7, st->st_atime, ansec);
1347 fill_time(v, 8, st->st_mtime, mnsec);
1348 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001349
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001350#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001351 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001352 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001353#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001354#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001355 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001356 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001357#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001358#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001360 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001361#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001362#ifdef HAVE_STRUCT_STAT_ST_GEN
1363 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001364 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001365#endif
1366#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1367 {
1368 PyObject *val;
1369 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001370 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001371#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001372 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001373#else
1374 bnsec = 0;
1375#endif
1376 if (_stat_float_times) {
1377 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1378 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001379 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001380 }
1381 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1382 val);
1383 }
1384#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001385#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1386 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001387 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001388#endif
Fred Drake699f3522000-06-29 21:12:41 +00001389
1390 if (PyErr_Occurred()) {
1391 Py_DECREF(v);
1392 return NULL;
1393 }
1394
1395 return v;
1396}
1397
Martin v. Löwisd8948722004-06-02 09:57:56 +00001398#ifdef MS_WINDOWS
1399
1400/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1401 where / can be used in place of \ and the trailing slash is optional.
1402 Both SERVER and SHARE must have at least one character.
1403*/
1404
1405#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1406#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001407#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001408#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001409#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001410
Tim Peters4ad82172004-08-30 17:02:04 +00001411static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001412IsUNCRootA(char *path, int pathlen)
1413{
1414 #define ISSLASH ISSLASHA
1415
1416 int i, share;
1417
1418 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1419 /* minimum UNCRoot is \\x\y */
1420 return FALSE;
1421 for (i = 2; i < pathlen ; i++)
1422 if (ISSLASH(path[i])) break;
1423 if (i == 2 || i == pathlen)
1424 /* do not allow \\\SHARE or \\SERVER */
1425 return FALSE;
1426 share = i+1;
1427 for (i = share; i < pathlen; i++)
1428 if (ISSLASH(path[i])) break;
1429 return (i != share && (i == pathlen || i == pathlen-1));
1430
1431 #undef ISSLASH
1432}
1433
1434#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001435static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001436IsUNCRootW(Py_UNICODE *path, int pathlen)
1437{
1438 #define ISSLASH ISSLASHW
1439
1440 int i, share;
1441
1442 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1443 /* minimum UNCRoot is \\x\y */
1444 return FALSE;
1445 for (i = 2; i < pathlen ; i++)
1446 if (ISSLASH(path[i])) break;
1447 if (i == 2 || i == pathlen)
1448 /* do not allow \\\SHARE or \\SERVER */
1449 return FALSE;
1450 share = i+1;
1451 for (i = share; i < pathlen; i++)
1452 if (ISSLASH(path[i])) break;
1453 return (i != share && (i == pathlen || i == pathlen-1));
1454
1455 #undef ISSLASH
1456}
1457#endif /* Py_WIN_WIDE_FILENAMES */
1458#endif /* MS_WINDOWS */
1459
Barry Warsaw53699e91996-12-10 23:23:01 +00001460static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001461posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001462 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001463#ifdef __VMS
1464 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1465#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001466 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001467#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001468 char *wformat,
1469 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001470{
Fred Drake699f3522000-06-29 21:12:41 +00001471 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001472 char *path = NULL; /* pass this to stat; do not free() it */
1473 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001474 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001475 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001476
1477#ifdef Py_WIN_WIDE_FILENAMES
1478 /* If on wide-character-capable OS see if argument
1479 is Unicode and if so use wide API. */
1480 if (unicode_file_names()) {
1481 PyUnicodeObject *po;
1482 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001483 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1484
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001485 Py_BEGIN_ALLOW_THREADS
1486 /* PyUnicode_AS_UNICODE result OK without
1487 thread lock as it is a simple dereference. */
1488 res = wstatfunc(wpath, &st);
1489 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001490
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001491 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001492 return win32_error_unicode("stat", wpath);
1493 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001494 }
1495 /* Drop the argument parsing error as narrow strings
1496 are also valid. */
1497 PyErr_Clear();
1498 }
1499#endif
1500
Tim Peters5aa91602002-01-30 05:46:57 +00001501 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001502 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001503 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001504 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001505
Barry Warsaw53699e91996-12-10 23:23:01 +00001506 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001507 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001508 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001509
1510 if (res != 0) {
1511#ifdef MS_WINDOWS
1512 result = win32_error("stat", pathfree);
1513#else
1514 result = posix_error_with_filename(pathfree);
1515#endif
1516 }
1517 else
1518 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001519
Tim Peters500bd032001-12-19 19:05:01 +00001520 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001521 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001522}
1523
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001524/* POSIX methods */
1525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001527"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001528Use the real uid/gid to test for access to a path. Note that most\n\
1529operations will use the effective uid/gid, therefore this routine can\n\
1530be used in a suid/sgid environment to test if the invoking user has the\n\
1531specified access to the path. The mode argument can be F_OK to test\n\
1532existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001533
1534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001535posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001536{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001537 char *path;
1538 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001539
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001540#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001542 if (unicode_file_names()) {
1543 PyUnicodeObject *po;
1544 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1545 Py_BEGIN_ALLOW_THREADS
1546 /* PyUnicode_AS_UNICODE OK without thread lock as
1547 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001548 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001549 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001550 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001551 }
1552 /* Drop the argument parsing error as narrow strings
1553 are also valid. */
1554 PyErr_Clear();
1555 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001556 if (!PyArg_ParseTuple(args, "eti:access",
1557 Py_FileSystemDefaultEncoding, &path, &mode))
1558 return 0;
1559 Py_BEGIN_ALLOW_THREADS
1560 attr = GetFileAttributesA(path);
1561 Py_END_ALLOW_THREADS
1562 PyMem_Free(path);
1563finish:
1564 if (attr == 0xFFFFFFFF)
1565 /* File does not exist, or cannot read attributes */
1566 return PyBool_FromLong(0);
1567 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001568 the file isn't read-only, or if it's a directory, as there are
1569 no read-only directories on Windows. */
1570 return PyBool_FromLong(!(mode & 2)
1571 || !(attr & FILE_ATTRIBUTE_READONLY)
1572 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001573#else
1574 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001575 if (!PyArg_ParseTuple(args, "eti:access",
1576 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001577 return NULL;
1578 Py_BEGIN_ALLOW_THREADS
1579 res = access(path, mode);
1580 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001581 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001582 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001584}
1585
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#ifndef F_OK
1587#define F_OK 0
1588#endif
1589#ifndef R_OK
1590#define R_OK 4
1591#endif
1592#ifndef W_OK
1593#define W_OK 2
1594#endif
1595#ifndef X_OK
1596#define X_OK 1
1597#endif
1598
1599#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001601"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603
1604static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001605posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001606{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001607 int id;
1608 char *ret;
1609
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001610 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001611 return NULL;
1612
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001613#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001614 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001615 if (id == 0) {
1616 ret = ttyname();
1617 }
1618 else {
1619 ret = NULL;
1620 }
1621#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001622 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001624 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001625 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001626 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001627}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001628#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001629
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001630#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001632"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634
1635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001636posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001637{
1638 char *ret;
1639 char buffer[L_ctermid];
1640
Greg Wardb48bc172000-03-01 21:51:56 +00001641#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001642 ret = ctermid_r(buffer);
1643#else
1644 ret = ctermid(buffer);
1645#endif
1646 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001647 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001648 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001649}
1650#endif
1651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001653"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001655
Barry Warsaw53699e91996-12-10 23:23:01 +00001656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001657posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001658{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001659#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001660 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001661#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001662 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001663#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001664 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001665#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001666 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001667#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
1669
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001671PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001672"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001673Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001674opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001675
1676static PyObject *
1677posix_fchdir(PyObject *self, PyObject *fdobj)
1678{
1679 return posix_fildes(fdobj, fchdir);
1680}
1681#endif /* HAVE_FCHDIR */
1682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001684PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001685"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001687
Barry Warsaw53699e91996-12-10 23:23:01 +00001688static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001689posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001690{
Mark Hammondef8b6542001-05-13 08:04:26 +00001691 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001692 int i;
1693 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001694#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001696 if (unicode_file_names()) {
1697 PyUnicodeObject *po;
1698 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1699 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001700 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1701 if (attr != 0xFFFFFFFF) {
1702 if (i & _S_IWRITE)
1703 attr &= ~FILE_ATTRIBUTE_READONLY;
1704 else
1705 attr |= FILE_ATTRIBUTE_READONLY;
1706 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1707 }
1708 else
1709 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001710 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001711 if (!res)
1712 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001713 PyUnicode_AS_UNICODE(po));
1714 Py_INCREF(Py_None);
1715 return Py_None;
1716 }
1717 /* Drop the argument parsing error as narrow strings
1718 are also valid. */
1719 PyErr_Clear();
1720 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001721 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1722 &path, &i))
1723 return NULL;
1724 Py_BEGIN_ALLOW_THREADS
1725 attr = GetFileAttributesA(path);
1726 if (attr != 0xFFFFFFFF) {
1727 if (i & _S_IWRITE)
1728 attr &= ~FILE_ATTRIBUTE_READONLY;
1729 else
1730 attr |= FILE_ATTRIBUTE_READONLY;
1731 res = SetFileAttributesA(path, attr);
1732 }
1733 else
1734 res = 0;
1735 Py_END_ALLOW_THREADS
1736 if (!res) {
1737 win32_error("chmod", path);
1738 PyMem_Free(path);
1739 return NULL;
1740 }
1741 PyMem_Free(path);
1742 Py_INCREF(Py_None);
1743 return Py_None;
1744#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001745 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001746 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001747 return NULL;
1748 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001749 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001750 Py_END_ALLOW_THREADS
1751 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001752 return posix_error_with_allocated_filename(path);
1753 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001754 Py_INCREF(Py_None);
1755 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001756#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001757}
1758
Christian Heimes4e30a842007-11-30 22:12:06 +00001759#ifdef HAVE_FCHMOD
1760PyDoc_STRVAR(posix_fchmod__doc__,
1761"fchmod(fd, mode)\n\n\
1762Change the access permissions of the file given by file\n\
1763descriptor fd.");
1764
1765static PyObject *
1766posix_fchmod(PyObject *self, PyObject *args)
1767{
1768 int fd, mode, res;
1769 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1770 return NULL;
1771 Py_BEGIN_ALLOW_THREADS
1772 res = fchmod(fd, mode);
1773 Py_END_ALLOW_THREADS
1774 if (res < 0)
1775 return posix_error();
1776 Py_RETURN_NONE;
1777}
1778#endif /* HAVE_FCHMOD */
1779
1780#ifdef HAVE_LCHMOD
1781PyDoc_STRVAR(posix_lchmod__doc__,
1782"lchmod(path, mode)\n\n\
1783Change the access permissions of a file. If path is a symlink, this\n\
1784affects the link itself rather than the target.");
1785
1786static PyObject *
1787posix_lchmod(PyObject *self, PyObject *args)
1788{
1789 char *path = NULL;
1790 int i;
1791 int res;
1792 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1793 &path, &i))
1794 return NULL;
1795 Py_BEGIN_ALLOW_THREADS
1796 res = lchmod(path, i);
1797 Py_END_ALLOW_THREADS
1798 if (res < 0)
1799 return posix_error_with_allocated_filename(path);
1800 PyMem_Free(path);
1801 Py_RETURN_NONE;
1802}
1803#endif /* HAVE_LCHMOD */
1804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001805
Thomas Wouterscf297e42007-02-23 15:07:44 +00001806#ifdef HAVE_CHFLAGS
1807PyDoc_STRVAR(posix_chflags__doc__,
1808"chflags(path, flags)\n\n\
1809Set file flags.");
1810
1811static PyObject *
1812posix_chflags(PyObject *self, PyObject *args)
1813{
1814 char *path;
1815 unsigned long flags;
1816 int res;
1817 if (!PyArg_ParseTuple(args, "etk:chflags",
1818 Py_FileSystemDefaultEncoding, &path, &flags))
1819 return NULL;
1820 Py_BEGIN_ALLOW_THREADS
1821 res = chflags(path, flags);
1822 Py_END_ALLOW_THREADS
1823 if (res < 0)
1824 return posix_error_with_allocated_filename(path);
1825 PyMem_Free(path);
1826 Py_INCREF(Py_None);
1827 return Py_None;
1828}
1829#endif /* HAVE_CHFLAGS */
1830
1831#ifdef HAVE_LCHFLAGS
1832PyDoc_STRVAR(posix_lchflags__doc__,
1833"lchflags(path, flags)\n\n\
1834Set file flags.\n\
1835This function will not follow symbolic links.");
1836
1837static PyObject *
1838posix_lchflags(PyObject *self, PyObject *args)
1839{
1840 char *path;
1841 unsigned long flags;
1842 int res;
1843 if (!PyArg_ParseTuple(args, "etk:lchflags",
1844 Py_FileSystemDefaultEncoding, &path, &flags))
1845 return NULL;
1846 Py_BEGIN_ALLOW_THREADS
1847 res = lchflags(path, flags);
1848 Py_END_ALLOW_THREADS
1849 if (res < 0)
1850 return posix_error_with_allocated_filename(path);
1851 PyMem_Free(path);
1852 Py_INCREF(Py_None);
1853 return Py_None;
1854}
1855#endif /* HAVE_LCHFLAGS */
1856
Martin v. Löwis244edc82001-10-04 22:44:26 +00001857#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001859"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001860Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001861
1862static PyObject *
1863posix_chroot(PyObject *self, PyObject *args)
1864{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001865 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001866}
1867#endif
1868
Guido van Rossum21142a01999-01-08 21:05:37 +00001869#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001870PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001871"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001873
1874static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001875posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001876{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001877 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001878}
1879#endif /* HAVE_FSYNC */
1880
1881#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001882
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001883#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001884extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1885#endif
1886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001888"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001889force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001891
1892static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001893posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001894{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001895 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001896}
1897#endif /* HAVE_FDATASYNC */
1898
1899
Fredrik Lundh10723342000-07-10 16:38:09 +00001900#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001902"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001903Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001904
Barry Warsaw53699e91996-12-10 23:23:01 +00001905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001906posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001907{
Mark Hammondef8b6542001-05-13 08:04:26 +00001908 char *path = NULL;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001909 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001910 int res;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001911 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001912 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001913 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001914 return NULL;
1915 Py_BEGIN_ALLOW_THREADS
1916 res = chown(path, (uid_t) uid, (gid_t) gid);
1917 Py_END_ALLOW_THREADS
1918 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001919 return posix_error_with_allocated_filename(path);
1920 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001921 Py_INCREF(Py_None);
1922 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001923}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001924#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001925
Christian Heimes4e30a842007-11-30 22:12:06 +00001926#ifdef HAVE_FCHOWN
1927PyDoc_STRVAR(posix_fchown__doc__,
1928"fchown(fd, uid, gid)\n\n\
1929Change the owner and group id of the file given by file descriptor\n\
1930fd to the numeric uid and gid.");
1931
1932static PyObject *
1933posix_fchown(PyObject *self, PyObject *args)
1934{
1935 int fd, uid, gid;
1936 int res;
1937 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1938 return NULL;
1939 Py_BEGIN_ALLOW_THREADS
1940 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1941 Py_END_ALLOW_THREADS
1942 if (res < 0)
1943 return posix_error();
1944 Py_RETURN_NONE;
1945}
1946#endif /* HAVE_FCHOWN */
1947
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001948#ifdef HAVE_LCHOWN
1949PyDoc_STRVAR(posix_lchown__doc__,
1950"lchown(path, uid, gid)\n\n\
1951Change the owner and group id of path to the numeric uid and gid.\n\
1952This function will not follow symbolic links.");
1953
1954static PyObject *
1955posix_lchown(PyObject *self, PyObject *args)
1956{
1957 char *path = NULL;
1958 int uid, gid;
1959 int res;
1960 if (!PyArg_ParseTuple(args, "etii:lchown",
1961 Py_FileSystemDefaultEncoding, &path,
1962 &uid, &gid))
1963 return NULL;
1964 Py_BEGIN_ALLOW_THREADS
1965 res = lchown(path, (uid_t) uid, (gid_t) gid);
1966 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001967 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001968 return posix_error_with_allocated_filename(path);
1969 PyMem_Free(path);
1970 Py_INCREF(Py_None);
1971 return Py_None;
1972}
1973#endif /* HAVE_LCHOWN */
1974
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Guido van Rossum36bc6801995-06-14 22:54:23 +00001976#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001977PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001978"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001979Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001980
Barry Warsaw53699e91996-12-10 23:23:01 +00001981static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001982posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001983{
1984 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001985 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001986
Barry Warsaw53699e91996-12-10 23:23:01 +00001987 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001988#if defined(PYOS_OS2) && defined(PYCC_GCC)
1989 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001990#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001991 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001992#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001993 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001994 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001995 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001996 return PyUnicode_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001997}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001998
1999PyDoc_STRVAR(posix_getcwdu__doc__,
2000"getcwdu() -> path\n\n\
2001Return a unicode string representing the current working directory.");
2002
2003static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002004posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002005{
2006 char buf[1026];
2007 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002008
2009#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00002010 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002012 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002013 wchar_t *wbuf2 = wbuf;
2014 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002015 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002016 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2017 /* If the buffer is large enough, len does not include the
2018 terminating \0. If the buffer is too small, len includes
2019 the space needed for the terminator. */
2020 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2021 wbuf2 = malloc(len * sizeof(wchar_t));
2022 if (wbuf2)
2023 len = GetCurrentDirectoryW(len, wbuf2);
2024 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002025 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002026 if (!wbuf2) {
2027 PyErr_NoMemory();
2028 return NULL;
2029 }
2030 if (!len) {
2031 if (wbuf2 != wbuf) free(wbuf2);
2032 return win32_error("getcwdu", NULL);
2033 }
2034 resobj = PyUnicode_FromWideChar(wbuf2, len);
2035 if (wbuf2 != wbuf) free(wbuf2);
2036 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002037 }
2038#endif
2039
2040 Py_BEGIN_ALLOW_THREADS
2041#if defined(PYOS_OS2) && defined(PYCC_GCC)
2042 res = _getcwd2(buf, sizeof buf);
2043#else
2044 res = getcwd(buf, sizeof buf);
2045#endif
2046 Py_END_ALLOW_THREADS
2047 if (res == NULL)
2048 return posix_error();
2049 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2050}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002051#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002053
Guido van Rossumb6775db1994-08-01 11:34:53 +00002054#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002056"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002058
Barry Warsaw53699e91996-12-10 23:23:01 +00002059static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002060posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002061{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002062 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002063}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002064#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002068"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002069Return a list containing the names of the entries in the directory.\n\
2070\n\
2071 path: path of directory to list\n\
2072\n\
2073The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075
Barry Warsaw53699e91996-12-10 23:23:01 +00002076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002077posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002078{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002079 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002080 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002081#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002082
Barry Warsaw53699e91996-12-10 23:23:01 +00002083 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002084 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002085 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002086 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002087 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002088 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002089 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002090
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002091#ifdef Py_WIN_WIDE_FILENAMES
2092 /* If on wide-character-capable OS see if argument
2093 is Unicode and if so use wide API. */
2094 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002095 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002096 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2097 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002098 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002099 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002100 /* Overallocate for \\*.*\0 */
2101 len = PyUnicode_GET_SIZE(po);
2102 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2103 if (!wnamebuf) {
2104 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002105 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002106 }
2107 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2108 wch = len > 0 ? wnamebuf[len-1] : '\0';
2109 if (wch != L'/' && wch != L'\\' && wch != L':')
2110 wnamebuf[len++] = L'\\';
2111 wcscpy(wnamebuf + len, L"*.*");
2112 if ((d = PyList_New(0)) == NULL) {
2113 free(wnamebuf);
2114 return NULL;
2115 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002116 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2117 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002118 int error = GetLastError();
2119 if (error == ERROR_FILE_NOT_FOUND) {
2120 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002121 return d;
2122 }
2123 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002124 win32_error_unicode("FindFirstFileW", wnamebuf);
2125 free(wnamebuf);
2126 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002127 }
2128 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002129 /* Skip over . and .. */
2130 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2131 wcscmp(wFileData.cFileName, L"..") != 0) {
2132 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2133 if (v == NULL) {
2134 Py_DECREF(d);
2135 d = NULL;
2136 break;
2137 }
2138 if (PyList_Append(d, v) != 0) {
2139 Py_DECREF(v);
2140 Py_DECREF(d);
2141 d = NULL;
2142 break;
2143 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002144 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002145 }
Georg Brandl622927b2006-03-07 12:48:03 +00002146 Py_BEGIN_ALLOW_THREADS
2147 result = FindNextFileW(hFindFile, &wFileData);
2148 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002149 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2150 it got to the end of the directory. */
2151 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2152 Py_DECREF(d);
2153 win32_error_unicode("FindNextFileW", wnamebuf);
2154 FindClose(hFindFile);
2155 free(wnamebuf);
2156 return NULL;
2157 }
Georg Brandl622927b2006-03-07 12:48:03 +00002158 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002159
2160 if (FindClose(hFindFile) == FALSE) {
2161 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002162 win32_error_unicode("FindClose", wnamebuf);
2163 free(wnamebuf);
2164 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002165 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002166 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002167 return d;
2168 }
2169 /* Drop the argument parsing error as narrow strings
2170 are also valid. */
2171 PyErr_Clear();
2172 }
2173#endif
2174
Tim Peters5aa91602002-01-30 05:46:57 +00002175 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002176 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002177 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002178 if (len > 0) {
2179 char ch = namebuf[len-1];
2180 if (ch != SEP && ch != ALTSEP && ch != ':')
2181 namebuf[len++] = '/';
2182 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002183 strcpy(namebuf + len, "*.*");
2184
Barry Warsaw53699e91996-12-10 23:23:01 +00002185 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002186 return NULL;
2187
2188 hFindFile = FindFirstFile(namebuf, &FileData);
2189 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002190 int error = GetLastError();
2191 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002192 return d;
2193 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002194 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002195 }
2196 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002197 /* Skip over . and .. */
2198 if (strcmp(FileData.cFileName, ".") != 0 &&
2199 strcmp(FileData.cFileName, "..") != 0) {
2200 v = PyString_FromString(FileData.cFileName);
2201 if (v == NULL) {
2202 Py_DECREF(d);
2203 d = NULL;
2204 break;
2205 }
2206 if (PyList_Append(d, v) != 0) {
2207 Py_DECREF(v);
2208 Py_DECREF(d);
2209 d = NULL;
2210 break;
2211 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002212 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002213 }
Georg Brandl622927b2006-03-07 12:48:03 +00002214 Py_BEGIN_ALLOW_THREADS
2215 result = FindNextFile(hFindFile, &FileData);
2216 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002217 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2218 it got to the end of the directory. */
2219 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2220 Py_DECREF(d);
2221 win32_error("FindNextFile", namebuf);
2222 FindClose(hFindFile);
2223 return NULL;
2224 }
Georg Brandl622927b2006-03-07 12:48:03 +00002225 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002226
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002227 if (FindClose(hFindFile) == FALSE) {
2228 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002229 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002230 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002231
2232 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002233
Tim Peters0bb44a42000-09-15 07:44:49 +00002234#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002235
2236#ifndef MAX_PATH
2237#define MAX_PATH CCHMAXPATH
2238#endif
2239 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002240 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002241 PyObject *d, *v;
2242 char namebuf[MAX_PATH+5];
2243 HDIR hdir = 1;
2244 ULONG srchcnt = 1;
2245 FILEFINDBUF3 ep;
2246 APIRET rc;
2247
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002248 if (!PyArg_ParseTuple(args, "et#:listdir",
2249 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002250 return NULL;
2251 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002252 PyMem_Free(name);
2253 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002254 return NULL;
2255 }
2256 strcpy(namebuf, name);
2257 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002258 if (*pt == ALTSEP)
2259 *pt = SEP;
2260 if (namebuf[len-1] != SEP)
2261 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002262 strcpy(namebuf + len, "*.*");
2263
Neal Norwitz6c913782007-10-14 03:23:09 +00002264 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002265 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002267 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002268
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002269 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2270 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002271 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002272 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2273 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2274 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002275
2276 if (rc != NO_ERROR) {
2277 errno = ENOENT;
Neal Norwitz6c913782007-10-14 03:23:09 +00002278 return posix_error_with_allocated_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002279 }
2280
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002281 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002282 do {
2283 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002284 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002285 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002286
2287 strcpy(namebuf, ep.achName);
2288
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002289 /* Leave Case of Name Alone -- In Native Form */
2290 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002291
2292 v = PyString_FromString(namebuf);
2293 if (v == NULL) {
2294 Py_DECREF(d);
2295 d = NULL;
2296 break;
2297 }
2298 if (PyList_Append(d, v) != 0) {
2299 Py_DECREF(v);
2300 Py_DECREF(d);
2301 d = NULL;
2302 break;
2303 }
2304 Py_DECREF(v);
2305 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2306 }
2307
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002308 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002309 return d;
2310#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002311
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002312 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002313 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002314 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002315 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002316 int arg_is_unicode = 1;
2317
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002318 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002319 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2320 arg_is_unicode = 0;
2321 PyErr_Clear();
2322 }
2323 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002324 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002325 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002326 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002327 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002328 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002329 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002330 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002331 return NULL;
2332 }
Georg Brandl622927b2006-03-07 12:48:03 +00002333 for (;;) {
2334 Py_BEGIN_ALLOW_THREADS
2335 ep = readdir(dirp);
2336 Py_END_ALLOW_THREADS
2337 if (ep == NULL)
2338 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002339 if (ep->d_name[0] == '.' &&
2340 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002341 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002342 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002343 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002344 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002345 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002346 d = NULL;
2347 break;
2348 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002349 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002350 PyObject *w;
2351
2352 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002353 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002354 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002355 if (w != NULL) {
2356 Py_DECREF(v);
2357 v = w;
2358 }
2359 else {
2360 /* fall back to the original byte string, as
2361 discussed in patch #683592 */
2362 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002363 }
Just van Rossum46c97842003-02-25 21:42:15 +00002364 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002365 if (PyList_Append(d, v) != 0) {
2366 Py_DECREF(v);
2367 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002368 d = NULL;
2369 break;
2370 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002371 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002372 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002373 if (errno != 0 && d != NULL) {
2374 /* readdir() returned NULL and set errno */
2375 closedir(dirp);
2376 Py_DECREF(d);
2377 return posix_error_with_allocated_filename(name);
2378 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002379 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002380 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002381
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002382 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002383
Tim Peters0bb44a42000-09-15 07:44:49 +00002384#endif /* which OS */
2385} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002386
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002387#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002388/* A helper function for abspath on win32 */
2389static PyObject *
2390posix__getfullpathname(PyObject *self, PyObject *args)
2391{
2392 /* assume encoded strings wont more than double no of chars */
2393 char inbuf[MAX_PATH*2];
2394 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002395 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002396 char outbuf[MAX_PATH*2];
2397 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002398#ifdef Py_WIN_WIDE_FILENAMES
2399 if (unicode_file_names()) {
2400 PyUnicodeObject *po;
2401 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2402 Py_UNICODE woutbuf[MAX_PATH*2];
2403 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002404 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002405 sizeof(woutbuf)/sizeof(woutbuf[0]),
2406 woutbuf, &wtemp))
2407 return win32_error("GetFullPathName", "");
2408 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2409 }
2410 /* Drop the argument parsing error as narrow strings
2411 are also valid. */
2412 PyErr_Clear();
2413 }
2414#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002415 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2416 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002417 &insize))
2418 return NULL;
2419 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2420 outbuf, &temp))
2421 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002422 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2423 return PyUnicode_Decode(outbuf, strlen(outbuf),
2424 Py_FileSystemDefaultEncoding, NULL);
2425 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002426 return PyString_FromString(outbuf);
2427} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002428#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002429
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002430PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002431"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002432Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002433
Barry Warsaw53699e91996-12-10 23:23:01 +00002434static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002435posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002436{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002437 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002438 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002439 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002440
2441#ifdef Py_WIN_WIDE_FILENAMES
2442 if (unicode_file_names()) {
2443 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002444 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002445 Py_BEGIN_ALLOW_THREADS
2446 /* PyUnicode_AS_UNICODE OK without thread lock as
2447 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002448 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002449 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002450 if (!res)
2451 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002452 Py_INCREF(Py_None);
2453 return Py_None;
2454 }
2455 /* Drop the argument parsing error as narrow strings
2456 are also valid. */
2457 PyErr_Clear();
2458 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002459 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2460 Py_FileSystemDefaultEncoding, &path, &mode))
2461 return NULL;
2462 Py_BEGIN_ALLOW_THREADS
2463 /* PyUnicode_AS_UNICODE OK without thread lock as
2464 it is a simple dereference. */
2465 res = CreateDirectoryA(path, NULL);
2466 Py_END_ALLOW_THREADS
2467 if (!res) {
2468 win32_error("mkdir", path);
2469 PyMem_Free(path);
2470 return NULL;
2471 }
2472 PyMem_Free(path);
2473 Py_INCREF(Py_None);
2474 return Py_None;
2475#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002476
Tim Peters5aa91602002-01-30 05:46:57 +00002477 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002478 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002479 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002480 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002481#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002482 res = mkdir(path);
2483#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002484 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002485#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002486 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002487 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002488 return posix_error_with_allocated_filename(path);
2489 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002490 Py_INCREF(Py_None);
2491 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002492#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002493}
2494
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002495
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002496/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2497#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002498#include <sys/resource.h>
2499#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002500
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002501
2502#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002503PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002504"nice(inc) -> new_priority\n\n\
2505Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002506
Barry Warsaw53699e91996-12-10 23:23:01 +00002507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002508posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002509{
2510 int increment, value;
2511
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002512 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002513 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002514
2515 /* There are two flavours of 'nice': one that returns the new
2516 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002517 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2518 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002519
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002520 If we are of the nice family that returns the new priority, we
2521 need to clear errno before the call, and check if errno is filled
2522 before calling posix_error() on a returnvalue of -1, because the
2523 -1 may be the actual new priority! */
2524
2525 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002526 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002527#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002528 if (value == 0)
2529 value = getpriority(PRIO_PROCESS, 0);
2530#endif
2531 if (value == -1 && errno != 0)
2532 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002533 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002534 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002535}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002536#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002539"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002540Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541
Barry Warsaw53699e91996-12-10 23:23:01 +00002542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002543posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002544{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002545#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002546 PyObject *o1, *o2;
2547 char *p1, *p2;
2548 BOOL result;
2549 if (unicode_file_names()) {
2550 if (!PyArg_ParseTuple(args, "O&O&:rename",
2551 convert_to_unicode, &o1,
2552 convert_to_unicode, &o2))
2553 PyErr_Clear();
2554 else {
2555 Py_BEGIN_ALLOW_THREADS
2556 result = MoveFileW(PyUnicode_AsUnicode(o1),
2557 PyUnicode_AsUnicode(o2));
2558 Py_END_ALLOW_THREADS
2559 Py_DECREF(o1);
2560 Py_DECREF(o2);
2561 if (!result)
2562 return win32_error("rename", NULL);
2563 Py_INCREF(Py_None);
2564 return Py_None;
2565 }
2566 }
2567 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2568 return NULL;
2569 Py_BEGIN_ALLOW_THREADS
2570 result = MoveFileA(p1, p2);
2571 Py_END_ALLOW_THREADS
2572 if (!result)
2573 return win32_error("rename", NULL);
2574 Py_INCREF(Py_None);
2575 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002576#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002577 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002578#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002579}
2580
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002582PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002583"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002584Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002585
Barry Warsaw53699e91996-12-10 23:23:01 +00002586static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002587posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002588{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002589#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002590 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002591#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002592 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002593#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002594}
2595
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002597PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002598"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002599Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002600
Barry Warsaw53699e91996-12-10 23:23:01 +00002601static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002602posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002603{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002604#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002605 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002606#else
2607 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2608#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002609}
2610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002611
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002612#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002613PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002614"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002615Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002616
Barry Warsaw53699e91996-12-10 23:23:01 +00002617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002618posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002619{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002620 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002621#ifdef MS_WINDOWS
2622 wchar_t *command;
2623 if (!PyArg_ParseTuple(args, "u:system", &command))
2624 return NULL;
2625#else
2626 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002627 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002628 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002629#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002630 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002631#ifdef MS_WINDOWS
2632 sts = _wsystem(command);
2633#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002634 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002635#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002636 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002637 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002638}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002639#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002642PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002643"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002644Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Barry Warsaw53699e91996-12-10 23:23:01 +00002646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002647posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002648{
2649 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002650 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002651 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002652 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002653 if (i < 0)
2654 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002655 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002656}
2657
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002659PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002660"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002664"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002665Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002666
Barry Warsaw53699e91996-12-10 23:23:01 +00002667static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002668posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002669{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002670#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002671 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002672#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002673 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002674#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002675}
2676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002677
Guido van Rossumb6775db1994-08-01 11:34:53 +00002678#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002680"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002682
Barry Warsaw53699e91996-12-10 23:23:01 +00002683static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002684posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002685{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002686 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002687 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002688
Barry Warsaw53699e91996-12-10 23:23:01 +00002689 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002690 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002691 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002692 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002693 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002694 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002695 u.sysname,
2696 u.nodename,
2697 u.release,
2698 u.version,
2699 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002700}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002701#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002702
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002703static int
2704extract_time(PyObject *t, long* sec, long* usec)
2705{
2706 long intval;
2707 if (PyFloat_Check(t)) {
2708 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002709 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002710 if (!intobj)
2711 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002712 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002713 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002714 if (intval == -1 && PyErr_Occurred())
2715 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002717 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002718 if (*usec < 0)
2719 /* If rounding gave us a negative number,
2720 truncate. */
2721 *usec = 0;
2722 return 0;
2723 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002724 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002725 if (intval == -1 && PyErr_Occurred())
2726 return -1;
2727 *sec = intval;
2728 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002729 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002730}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002733"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002734utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002735Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002736second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002737
Barry Warsaw53699e91996-12-10 23:23:01 +00002738static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002739posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002740{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002741#ifdef Py_WIN_WIDE_FILENAMES
2742 PyObject *arg;
2743 PyUnicodeObject *obwpath;
2744 wchar_t *wpath = NULL;
2745 char *apath = NULL;
2746 HANDLE hFile;
2747 long atimesec, mtimesec, ausec, musec;
2748 FILETIME atime, mtime;
2749 PyObject *result = NULL;
2750
2751 if (unicode_file_names()) {
2752 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2753 wpath = PyUnicode_AS_UNICODE(obwpath);
2754 Py_BEGIN_ALLOW_THREADS
2755 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002756 NULL, OPEN_EXISTING,
2757 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002758 Py_END_ALLOW_THREADS
2759 if (hFile == INVALID_HANDLE_VALUE)
2760 return win32_error_unicode("utime", wpath);
2761 } else
2762 /* Drop the argument parsing error as narrow strings
2763 are also valid. */
2764 PyErr_Clear();
2765 }
2766 if (!wpath) {
2767 if (!PyArg_ParseTuple(args, "etO:utime",
2768 Py_FileSystemDefaultEncoding, &apath, &arg))
2769 return NULL;
2770 Py_BEGIN_ALLOW_THREADS
2771 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002772 NULL, OPEN_EXISTING,
2773 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002774 Py_END_ALLOW_THREADS
2775 if (hFile == INVALID_HANDLE_VALUE) {
2776 win32_error("utime", apath);
2777 PyMem_Free(apath);
2778 return NULL;
2779 }
2780 PyMem_Free(apath);
2781 }
2782
2783 if (arg == Py_None) {
2784 SYSTEMTIME now;
2785 GetSystemTime(&now);
2786 if (!SystemTimeToFileTime(&now, &mtime) ||
2787 !SystemTimeToFileTime(&now, &atime)) {
2788 win32_error("utime", NULL);
2789 goto done;
2790 }
2791 }
2792 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2793 PyErr_SetString(PyExc_TypeError,
2794 "utime() arg 2 must be a tuple (atime, mtime)");
2795 goto done;
2796 }
2797 else {
2798 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2799 &atimesec, &ausec) == -1)
2800 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002801 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002802 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2803 &mtimesec, &musec) == -1)
2804 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002805 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002806 }
2807 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2808 /* Avoid putting the file name into the error here,
2809 as that may confuse the user into believing that
2810 something is wrong with the file, when it also
2811 could be the time stamp that gives a problem. */
2812 win32_error("utime", NULL);
2813 }
2814 Py_INCREF(Py_None);
2815 result = Py_None;
2816done:
2817 CloseHandle(hFile);
2818 return result;
2819#else /* Py_WIN_WIDE_FILENAMES */
2820
Neal Norwitz2adf2102004-06-09 01:46:02 +00002821 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002822 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002823 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002824 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002825
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002826#if defined(HAVE_UTIMES)
2827 struct timeval buf[2];
2828#define ATIME buf[0].tv_sec
2829#define MTIME buf[1].tv_sec
2830#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002831/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002832 struct utimbuf buf;
2833#define ATIME buf.actime
2834#define MTIME buf.modtime
2835#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002836#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002837 time_t buf[2];
2838#define ATIME buf[0]
2839#define MTIME buf[1]
2840#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002841#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002842
Mark Hammond817c9292003-12-03 01:22:38 +00002843
Thomas Wouters477c8d52006-05-27 19:21:47 +00002844 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002845 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002846 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002847 if (arg == Py_None) {
2848 /* optional time values not given */
2849 Py_BEGIN_ALLOW_THREADS
2850 res = utime(path, NULL);
2851 Py_END_ALLOW_THREADS
2852 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002853 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002854 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002855 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002856 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002857 return NULL;
2858 }
2859 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002860 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002861 &atime, &ausec) == -1) {
2862 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002863 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002864 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002865 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002866 &mtime, &musec) == -1) {
2867 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002868 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002869 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002870 ATIME = atime;
2871 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002872#ifdef HAVE_UTIMES
2873 buf[0].tv_usec = ausec;
2874 buf[1].tv_usec = musec;
2875 Py_BEGIN_ALLOW_THREADS
2876 res = utimes(path, buf);
2877 Py_END_ALLOW_THREADS
2878#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002879 Py_BEGIN_ALLOW_THREADS
2880 res = utime(path, UTIME_ARG);
2881 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002882#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002883 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002884 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002885 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002886 }
Neal Norwitz96652712004-06-06 20:40:27 +00002887 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002888 Py_INCREF(Py_None);
2889 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002890#undef UTIME_ARG
2891#undef ATIME
2892#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002893#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002894}
2895
Guido van Rossum85e3b011991-06-03 12:42:10 +00002896
Guido van Rossum3b066191991-06-04 19:40:25 +00002897/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002899PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002900"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002901Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002902
Barry Warsaw53699e91996-12-10 23:23:01 +00002903static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002904posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002905{
2906 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002907 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002908 return NULL;
2909 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002910 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002911}
2912
Martin v. Löwis114619e2002-10-07 06:44:21 +00002913#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2914static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002915free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002916{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002917 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002918 for (i = 0; i < count; i++)
2919 PyMem_Free(array[i]);
2920 PyMem_DEL(array);
2921}
2922#endif
2923
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002924
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002925#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002926PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002927"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002928Execute an executable path with arguments, replacing current process.\n\
2929\n\
2930 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002931 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002932
Barry Warsaw53699e91996-12-10 23:23:01 +00002933static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002934posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002935{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002936 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002937 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002938 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002939 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002940 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002941
Guido van Rossum89b33251993-10-22 14:26:06 +00002942 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002943 argv is a list or tuple of strings. */
2944
Martin v. Löwis114619e2002-10-07 06:44:21 +00002945 if (!PyArg_ParseTuple(args, "etO:execv",
2946 Py_FileSystemDefaultEncoding,
2947 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002948 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002949 if (PyList_Check(argv)) {
2950 argc = PyList_Size(argv);
2951 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002952 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002953 else if (PyTuple_Check(argv)) {
2954 argc = PyTuple_Size(argv);
2955 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002956 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002957 else {
Fred Drake661ea262000-10-24 19:57:45 +00002958 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002959 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002960 return NULL;
2961 }
Thomas Heller6790d602007-08-30 17:15:14 +00002962 if (argc < 1) {
2963 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2964 PyMem_Free(path);
2965 return NULL;
2966 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002967
Barry Warsaw53699e91996-12-10 23:23:01 +00002968 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002969 if (argvlist == NULL) {
2970 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002971 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002972 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002973 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002974 if (!PyArg_Parse((*getitem)(argv, i), "et",
2975 Py_FileSystemDefaultEncoding,
2976 &argvlist[i])) {
2977 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002978 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002979 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002980 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002981 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002982
Guido van Rossum85e3b011991-06-03 12:42:10 +00002983 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002984 }
2985 argvlist[argc] = NULL;
2986
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002987 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002988
Guido van Rossum85e3b011991-06-03 12:42:10 +00002989 /* If we get here it's definitely an error */
2990
Martin v. Löwis114619e2002-10-07 06:44:21 +00002991 free_string_array(argvlist, argc);
2992 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002993 return posix_error();
2994}
2995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002997PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002998"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002999Execute a path with arguments and environment, replacing current process.\n\
3000\n\
3001 path: path of executable file\n\
3002 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003003 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003004
Barry Warsaw53699e91996-12-10 23:23:01 +00003005static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003006posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003007{
3008 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003009 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003010 char **argvlist;
3011 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003012 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003013 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003014 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003015 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003016
3017 /* execve has three arguments: (path, argv, env), where
3018 argv is a list or tuple of strings and env is a dictionary
3019 like posix.environ. */
3020
Martin v. Löwis114619e2002-10-07 06:44:21 +00003021 if (!PyArg_ParseTuple(args, "etOO:execve",
3022 Py_FileSystemDefaultEncoding,
3023 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003024 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003025 if (PyList_Check(argv)) {
3026 argc = PyList_Size(argv);
3027 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003028 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003029 else if (PyTuple_Check(argv)) {
3030 argc = PyTuple_Size(argv);
3031 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003032 }
3033 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003034 PyErr_SetString(PyExc_TypeError,
3035 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003036 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003037 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003038 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003039 PyErr_SetString(PyExc_TypeError,
3040 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003041 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003042 }
3043
Barry Warsaw53699e91996-12-10 23:23:01 +00003044 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003045 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003046 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003047 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003048 }
3049 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003050 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003051 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003052 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003053 &argvlist[i]))
3054 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003055 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003056 goto fail_1;
3057 }
3058 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003059 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003060 argvlist[argc] = NULL;
3061
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003062 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003063 if (i < 0)
3064 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003065 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003066 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003067 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003068 goto fail_1;
3069 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003070 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003071 keys = PyMapping_Keys(env);
3072 vals = PyMapping_Values(env);
3073 if (!keys || !vals)
3074 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003075 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3076 PyErr_SetString(PyExc_TypeError,
3077 "execve(): env.keys() or env.values() is not a list");
3078 goto fail_2;
3079 }
Tim Peters5aa91602002-01-30 05:46:57 +00003080
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003081 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003082 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003083 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003084
3085 key = PyList_GetItem(keys, pos);
3086 val = PyList_GetItem(vals, pos);
3087 if (!key || !val)
3088 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003089
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003090 if (!PyArg_Parse(
3091 key,
3092 "s;execve() arg 3 contains a non-string key",
3093 &k) ||
3094 !PyArg_Parse(
3095 val,
3096 "s;execve() arg 3 contains a non-string value",
3097 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003098 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003099 goto fail_2;
3100 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003101
3102#if defined(PYOS_OS2)
3103 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3104 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3105#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003106 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003107 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003108 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003109 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003110 goto fail_2;
3111 }
Tim Petersc8996f52001-12-03 20:41:00 +00003112 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003113 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003114#if defined(PYOS_OS2)
3115 }
3116#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003117 }
3118 envlist[envc] = 0;
3119
3120 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003121
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003122 /* If we get here it's definitely an error */
3123
3124 (void) posix_error();
3125
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003126 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003127 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003128 PyMem_DEL(envlist[envc]);
3129 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003130 fail_1:
3131 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003132 Py_XDECREF(vals);
3133 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003134 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003135 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003136 return NULL;
3137}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003138#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003140
Guido van Rossuma1065681999-01-25 23:20:23 +00003141#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003142PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003143"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003144Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003145\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003146 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003147 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003148 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003149
3150static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003151posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003152{
3153 char *path;
3154 PyObject *argv;
3155 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003156 int mode, i;
3157 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003158 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003159 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003160
3161 /* spawnv has three arguments: (mode, path, argv), where
3162 argv is a list or tuple of strings. */
3163
Martin v. Löwis114619e2002-10-07 06:44:21 +00003164 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3165 Py_FileSystemDefaultEncoding,
3166 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003167 return NULL;
3168 if (PyList_Check(argv)) {
3169 argc = PyList_Size(argv);
3170 getitem = PyList_GetItem;
3171 }
3172 else if (PyTuple_Check(argv)) {
3173 argc = PyTuple_Size(argv);
3174 getitem = PyTuple_GetItem;
3175 }
3176 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003177 PyErr_SetString(PyExc_TypeError,
3178 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003179 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003180 return NULL;
3181 }
3182
3183 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003184 if (argvlist == NULL) {
3185 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003186 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003187 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003188 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003189 if (!PyArg_Parse((*getitem)(argv, i), "et",
3190 Py_FileSystemDefaultEncoding,
3191 &argvlist[i])) {
3192 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003193 PyErr_SetString(
3194 PyExc_TypeError,
3195 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003196 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003197 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003198 }
3199 }
3200 argvlist[argc] = NULL;
3201
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003202#if defined(PYOS_OS2) && defined(PYCC_GCC)
3203 Py_BEGIN_ALLOW_THREADS
3204 spawnval = spawnv(mode, path, argvlist);
3205 Py_END_ALLOW_THREADS
3206#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003207 if (mode == _OLD_P_OVERLAY)
3208 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003209
Tim Peters25059d32001-12-07 20:35:43 +00003210 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003211 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003212 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003213#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003214
Martin v. Löwis114619e2002-10-07 06:44:21 +00003215 free_string_array(argvlist, argc);
3216 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003217
Fred Drake699f3522000-06-29 21:12:41 +00003218 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003219 return posix_error();
3220 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003221#if SIZEOF_LONG == SIZEOF_VOID_P
3222 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003223#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003224 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003225#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003226}
3227
3228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003229PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003230"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003231Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003232\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003233 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003234 path: path of executable file\n\
3235 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003236 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003237
3238static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003239posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003240{
3241 char *path;
3242 PyObject *argv, *env;
3243 char **argvlist;
3244 char **envlist;
3245 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003246 int mode, pos, envc;
3247 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003248 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003249 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003250 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003251
3252 /* spawnve has four arguments: (mode, path, argv, env), where
3253 argv is a list or tuple of strings and env is a dictionary
3254 like posix.environ. */
3255
Martin v. Löwis114619e2002-10-07 06:44:21 +00003256 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3257 Py_FileSystemDefaultEncoding,
3258 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003259 return NULL;
3260 if (PyList_Check(argv)) {
3261 argc = PyList_Size(argv);
3262 getitem = PyList_GetItem;
3263 }
3264 else if (PyTuple_Check(argv)) {
3265 argc = PyTuple_Size(argv);
3266 getitem = PyTuple_GetItem;
3267 }
3268 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003269 PyErr_SetString(PyExc_TypeError,
3270 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003271 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003272 }
3273 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003274 PyErr_SetString(PyExc_TypeError,
3275 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003276 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003277 }
3278
3279 argvlist = PyMem_NEW(char *, argc+1);
3280 if (argvlist == NULL) {
3281 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003282 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003283 }
3284 for (i = 0; i < argc; i++) {
3285 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003286 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003287 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003288 &argvlist[i]))
3289 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003290 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003291 goto fail_1;
3292 }
3293 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003294 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003295 argvlist[argc] = NULL;
3296
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003297 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003298 if (i < 0)
3299 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003300 envlist = PyMem_NEW(char *, i + 1);
3301 if (envlist == NULL) {
3302 PyErr_NoMemory();
3303 goto fail_1;
3304 }
3305 envc = 0;
3306 keys = PyMapping_Keys(env);
3307 vals = PyMapping_Values(env);
3308 if (!keys || !vals)
3309 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003310 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3311 PyErr_SetString(PyExc_TypeError,
3312 "spawnve(): env.keys() or env.values() is not a list");
3313 goto fail_2;
3314 }
Tim Peters5aa91602002-01-30 05:46:57 +00003315
Guido van Rossuma1065681999-01-25 23:20:23 +00003316 for (pos = 0; pos < i; pos++) {
3317 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003318 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003319
3320 key = PyList_GetItem(keys, pos);
3321 val = PyList_GetItem(vals, pos);
3322 if (!key || !val)
3323 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003324
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003325 if (!PyArg_Parse(
3326 key,
3327 "s;spawnve() arg 3 contains a non-string key",
3328 &k) ||
3329 !PyArg_Parse(
3330 val,
3331 "s;spawnve() arg 3 contains a non-string value",
3332 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003333 {
3334 goto fail_2;
3335 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003336 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003337 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003338 if (p == NULL) {
3339 PyErr_NoMemory();
3340 goto fail_2;
3341 }
Tim Petersc8996f52001-12-03 20:41:00 +00003342 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003343 envlist[envc++] = p;
3344 }
3345 envlist[envc] = 0;
3346
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003347#if defined(PYOS_OS2) && defined(PYCC_GCC)
3348 Py_BEGIN_ALLOW_THREADS
3349 spawnval = spawnve(mode, path, argvlist, envlist);
3350 Py_END_ALLOW_THREADS
3351#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003352 if (mode == _OLD_P_OVERLAY)
3353 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003354
3355 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003356 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003357 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003358#endif
Tim Peters25059d32001-12-07 20:35:43 +00003359
Fred Drake699f3522000-06-29 21:12:41 +00003360 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003361 (void) posix_error();
3362 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003363#if SIZEOF_LONG == SIZEOF_VOID_P
3364 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003365#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003366 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003367#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003368
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003369 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003370 while (--envc >= 0)
3371 PyMem_DEL(envlist[envc]);
3372 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003373 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003374 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003375 Py_XDECREF(vals);
3376 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003377 fail_0:
3378 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003379 return res;
3380}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003381
3382/* OS/2 supports spawnvp & spawnvpe natively */
3383#if defined(PYOS_OS2)
3384PyDoc_STRVAR(posix_spawnvp__doc__,
3385"spawnvp(mode, file, args)\n\n\
3386Execute the program 'file' in a new process, using the environment\n\
3387search path to find the file.\n\
3388\n\
3389 mode: mode of process creation\n\
3390 file: executable file name\n\
3391 args: tuple or list of strings");
3392
3393static PyObject *
3394posix_spawnvp(PyObject *self, PyObject *args)
3395{
3396 char *path;
3397 PyObject *argv;
3398 char **argvlist;
3399 int mode, i, argc;
3400 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003401 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003402
3403 /* spawnvp has three arguments: (mode, path, argv), where
3404 argv is a list or tuple of strings. */
3405
3406 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3407 Py_FileSystemDefaultEncoding,
3408 &path, &argv))
3409 return NULL;
3410 if (PyList_Check(argv)) {
3411 argc = PyList_Size(argv);
3412 getitem = PyList_GetItem;
3413 }
3414 else if (PyTuple_Check(argv)) {
3415 argc = PyTuple_Size(argv);
3416 getitem = PyTuple_GetItem;
3417 }
3418 else {
3419 PyErr_SetString(PyExc_TypeError,
3420 "spawnvp() arg 2 must be a tuple or list");
3421 PyMem_Free(path);
3422 return NULL;
3423 }
3424
3425 argvlist = PyMem_NEW(char *, argc+1);
3426 if (argvlist == NULL) {
3427 PyMem_Free(path);
3428 return PyErr_NoMemory();
3429 }
3430 for (i = 0; i < argc; i++) {
3431 if (!PyArg_Parse((*getitem)(argv, i), "et",
3432 Py_FileSystemDefaultEncoding,
3433 &argvlist[i])) {
3434 free_string_array(argvlist, i);
3435 PyErr_SetString(
3436 PyExc_TypeError,
3437 "spawnvp() arg 2 must contain only strings");
3438 PyMem_Free(path);
3439 return NULL;
3440 }
3441 }
3442 argvlist[argc] = NULL;
3443
3444 Py_BEGIN_ALLOW_THREADS
3445#if defined(PYCC_GCC)
3446 spawnval = spawnvp(mode, path, argvlist);
3447#else
3448 spawnval = _spawnvp(mode, path, argvlist);
3449#endif
3450 Py_END_ALLOW_THREADS
3451
3452 free_string_array(argvlist, argc);
3453 PyMem_Free(path);
3454
3455 if (spawnval == -1)
3456 return posix_error();
3457 else
3458 return Py_BuildValue("l", (long) spawnval);
3459}
3460
3461
3462PyDoc_STRVAR(posix_spawnvpe__doc__,
3463"spawnvpe(mode, file, args, env)\n\n\
3464Execute the program 'file' in a new process, using the environment\n\
3465search path to find the file.\n\
3466\n\
3467 mode: mode of process creation\n\
3468 file: executable file name\n\
3469 args: tuple or list of arguments\n\
3470 env: dictionary of strings mapping to strings");
3471
3472static PyObject *
3473posix_spawnvpe(PyObject *self, PyObject *args)
3474{
3475 char *path;
3476 PyObject *argv, *env;
3477 char **argvlist;
3478 char **envlist;
3479 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3480 int mode, i, pos, argc, envc;
3481 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003482 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003483 int lastarg = 0;
3484
3485 /* spawnvpe has four arguments: (mode, path, argv, env), where
3486 argv is a list or tuple of strings and env is a dictionary
3487 like posix.environ. */
3488
3489 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3490 Py_FileSystemDefaultEncoding,
3491 &path, &argv, &env))
3492 return NULL;
3493 if (PyList_Check(argv)) {
3494 argc = PyList_Size(argv);
3495 getitem = PyList_GetItem;
3496 }
3497 else if (PyTuple_Check(argv)) {
3498 argc = PyTuple_Size(argv);
3499 getitem = PyTuple_GetItem;
3500 }
3501 else {
3502 PyErr_SetString(PyExc_TypeError,
3503 "spawnvpe() arg 2 must be a tuple or list");
3504 goto fail_0;
3505 }
3506 if (!PyMapping_Check(env)) {
3507 PyErr_SetString(PyExc_TypeError,
3508 "spawnvpe() arg 3 must be a mapping object");
3509 goto fail_0;
3510 }
3511
3512 argvlist = PyMem_NEW(char *, argc+1);
3513 if (argvlist == NULL) {
3514 PyErr_NoMemory();
3515 goto fail_0;
3516 }
3517 for (i = 0; i < argc; i++) {
3518 if (!PyArg_Parse((*getitem)(argv, i),
3519 "et;spawnvpe() arg 2 must contain only strings",
3520 Py_FileSystemDefaultEncoding,
3521 &argvlist[i]))
3522 {
3523 lastarg = i;
3524 goto fail_1;
3525 }
3526 }
3527 lastarg = argc;
3528 argvlist[argc] = NULL;
3529
3530 i = PyMapping_Size(env);
3531 if (i < 0)
3532 goto fail_1;
3533 envlist = PyMem_NEW(char *, i + 1);
3534 if (envlist == NULL) {
3535 PyErr_NoMemory();
3536 goto fail_1;
3537 }
3538 envc = 0;
3539 keys = PyMapping_Keys(env);
3540 vals = PyMapping_Values(env);
3541 if (!keys || !vals)
3542 goto fail_2;
3543 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3544 PyErr_SetString(PyExc_TypeError,
3545 "spawnvpe(): env.keys() or env.values() is not a list");
3546 goto fail_2;
3547 }
3548
3549 for (pos = 0; pos < i; pos++) {
3550 char *p, *k, *v;
3551 size_t len;
3552
3553 key = PyList_GetItem(keys, pos);
3554 val = PyList_GetItem(vals, pos);
3555 if (!key || !val)
3556 goto fail_2;
3557
3558 if (!PyArg_Parse(
3559 key,
3560 "s;spawnvpe() arg 3 contains a non-string key",
3561 &k) ||
3562 !PyArg_Parse(
3563 val,
3564 "s;spawnvpe() arg 3 contains a non-string value",
3565 &v))
3566 {
3567 goto fail_2;
3568 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003569 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003570 p = PyMem_NEW(char, len);
3571 if (p == NULL) {
3572 PyErr_NoMemory();
3573 goto fail_2;
3574 }
3575 PyOS_snprintf(p, len, "%s=%s", k, v);
3576 envlist[envc++] = p;
3577 }
3578 envlist[envc] = 0;
3579
3580 Py_BEGIN_ALLOW_THREADS
3581#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003582 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003583#else
Christian Heimes292d3512008-02-03 16:51:08 +00003584 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003585#endif
3586 Py_END_ALLOW_THREADS
3587
3588 if (spawnval == -1)
3589 (void) posix_error();
3590 else
3591 res = Py_BuildValue("l", (long) spawnval);
3592
3593 fail_2:
3594 while (--envc >= 0)
3595 PyMem_DEL(envlist[envc]);
3596 PyMem_DEL(envlist);
3597 fail_1:
3598 free_string_array(argvlist, lastarg);
3599 Py_XDECREF(vals);
3600 Py_XDECREF(keys);
3601 fail_0:
3602 PyMem_Free(path);
3603 return res;
3604}
3605#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003606#endif /* HAVE_SPAWNV */
3607
3608
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003609#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003610PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003611"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003612Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3613\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003614Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003615
3616static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003617posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003618{
Christian Heimes400adb02008-02-01 08:12:03 +00003619 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003620 if (pid == -1)
3621 return posix_error();
3622 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003623 return PyLong_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003624}
3625#endif
3626
3627
Guido van Rossumad0ee831995-03-01 10:34:45 +00003628#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003629PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003630"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003631Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003632Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003633
Barry Warsaw53699e91996-12-10 23:23:01 +00003634static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003635posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003636{
Christian Heimes400adb02008-02-01 08:12:03 +00003637 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003638 if (pid == -1)
3639 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003640 if (pid == 0)
3641 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003642 return PyLong_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003643}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003644#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003645
Neal Norwitzb59798b2003-03-21 01:43:31 +00003646/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003647/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3648#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003649#define DEV_PTY_FILE "/dev/ptc"
3650#define HAVE_DEV_PTMX
3651#else
3652#define DEV_PTY_FILE "/dev/ptmx"
3653#endif
3654
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003655#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003656#ifdef HAVE_PTY_H
3657#include <pty.h>
3658#else
3659#ifdef HAVE_LIBUTIL_H
3660#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003661#endif /* HAVE_LIBUTIL_H */
3662#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003663#ifdef HAVE_STROPTS_H
3664#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003665#endif
3666#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003667
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003668#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003669PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003670"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003671Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003672
3673static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003674posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003675{
3676 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003677#ifndef HAVE_OPENPTY
3678 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003679#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003680#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003681 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003682#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003683 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003684#endif
3685#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003686
Thomas Wouters70c21a12000-07-14 14:28:33 +00003687#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003688 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3689 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003690#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003691 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3692 if (slave_name == NULL)
3693 return posix_error();
3694
3695 slave_fd = open(slave_name, O_RDWR);
3696 if (slave_fd < 0)
3697 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003698#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003699 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003700 if (master_fd < 0)
3701 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003702 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003703 /* change permission of slave */
3704 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003705 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003706 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003707 }
3708 /* unlock slave */
3709 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003710 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003711 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003712 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003713 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003714 slave_name = ptsname(master_fd); /* get name of slave */
3715 if (slave_name == NULL)
3716 return posix_error();
3717 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3718 if (slave_fd < 0)
3719 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003720#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003721 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3722 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003723#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003724 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003725#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003726#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003727#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003728
Fred Drake8cef4cf2000-06-28 16:40:38 +00003729 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003730
Fred Drake8cef4cf2000-06-28 16:40:38 +00003731}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003732#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003733
3734#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003735PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003736"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003737Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3738Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003739To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003740
3741static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003742posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003743{
Christian Heimes400adb02008-02-01 08:12:03 +00003744 int master_fd = -1;
3745 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003746
Fred Drake8cef4cf2000-06-28 16:40:38 +00003747 pid = forkpty(&master_fd, NULL, NULL, NULL);
3748 if (pid == -1)
3749 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003750 if (pid == 0)
3751 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003752 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003753}
3754#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003755
Guido van Rossumad0ee831995-03-01 10:34:45 +00003756#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003757PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003758"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003759Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003760
Barry Warsaw53699e91996-12-10 23:23:01 +00003761static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003762posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003763{
Christian Heimes217cfd12007-12-02 14:31:20 +00003764 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003765}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003766#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003767
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003768
Guido van Rossumad0ee831995-03-01 10:34:45 +00003769#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003770PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003771"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003772Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003773
Barry Warsaw53699e91996-12-10 23:23:01 +00003774static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003775posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003776{
Christian Heimes217cfd12007-12-02 14:31:20 +00003777 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003778}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003779#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003780
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003781
Guido van Rossumad0ee831995-03-01 10:34:45 +00003782#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003783PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003784"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003785Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003786
Barry Warsaw53699e91996-12-10 23:23:01 +00003787static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003788posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003789{
Christian Heimes217cfd12007-12-02 14:31:20 +00003790 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003791}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003792#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003795PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003796"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003797Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003798
Barry Warsaw53699e91996-12-10 23:23:01 +00003799static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003800posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003801{
Christian Heimes217cfd12007-12-02 14:31:20 +00003802 return PyLong_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003803}
3804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003805
Fred Drakec9680921999-12-13 16:37:25 +00003806#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003807PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003808"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003809Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003810
3811static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003812posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003813{
3814 PyObject *result = NULL;
3815
Fred Drakec9680921999-12-13 16:37:25 +00003816#ifdef NGROUPS_MAX
3817#define MAX_GROUPS NGROUPS_MAX
3818#else
3819 /* defined to be 16 on Solaris7, so this should be a small number */
3820#define MAX_GROUPS 64
3821#endif
3822 gid_t grouplist[MAX_GROUPS];
3823 int n;
3824
3825 n = getgroups(MAX_GROUPS, grouplist);
3826 if (n < 0)
3827 posix_error();
3828 else {
3829 result = PyList_New(n);
3830 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003831 int i;
3832 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003833 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003834 if (o == NULL) {
3835 Py_DECREF(result);
3836 result = NULL;
3837 break;
3838 }
3839 PyList_SET_ITEM(result, i, o);
3840 }
3841 }
3842 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003843
Fred Drakec9680921999-12-13 16:37:25 +00003844 return result;
3845}
3846#endif
3847
Martin v. Löwis606edc12002-06-13 21:09:11 +00003848#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003849PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003850"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003851Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003852
3853static PyObject *
3854posix_getpgid(PyObject *self, PyObject *args)
3855{
3856 int pid, pgid;
3857 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3858 return NULL;
3859 pgid = getpgid(pid);
3860 if (pgid < 0)
3861 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00003862 return PyLong_FromLong((long)pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003863}
3864#endif /* HAVE_GETPGID */
3865
3866
Guido van Rossumb6775db1994-08-01 11:34:53 +00003867#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003868PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003869"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003870Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003871
Barry Warsaw53699e91996-12-10 23:23:01 +00003872static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003873posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003874{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003875#ifdef GETPGRP_HAVE_ARG
Christian Heimes217cfd12007-12-02 14:31:20 +00003876 return PyLong_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003877#else /* GETPGRP_HAVE_ARG */
Christian Heimes217cfd12007-12-02 14:31:20 +00003878 return PyLong_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003879#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003880}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003881#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003882
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003883
Guido van Rossumb6775db1994-08-01 11:34:53 +00003884#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003885PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003886"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003887Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003888
Barry Warsaw53699e91996-12-10 23:23:01 +00003889static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003890posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003891{
Guido van Rossum64933891994-10-20 21:56:42 +00003892#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003893 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003894#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003895 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003896#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003897 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003898 Py_INCREF(Py_None);
3899 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003900}
3901
Guido van Rossumb6775db1994-08-01 11:34:53 +00003902#endif /* HAVE_SETPGRP */
3903
Guido van Rossumad0ee831995-03-01 10:34:45 +00003904#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003905PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003906"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003907Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003908
Barry Warsaw53699e91996-12-10 23:23:01 +00003909static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003910posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003911{
Christian Heimes217cfd12007-12-02 14:31:20 +00003912 return PyLong_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003913}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003914#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003915
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003916
Fred Drake12c6e2d1999-12-14 21:25:03 +00003917#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003918PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003919"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003920Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003921
3922static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003923posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003924{
Neal Norwitze241ce82003-02-17 18:17:05 +00003925 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003926 char *name;
3927 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003928
Fred Drakea30680b2000-12-06 21:24:28 +00003929 errno = 0;
3930 name = getlogin();
3931 if (name == NULL) {
3932 if (errno)
3933 posix_error();
3934 else
3935 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003936 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003937 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003938 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003939 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003940 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003941
Fred Drake12c6e2d1999-12-14 21:25:03 +00003942 return result;
3943}
3944#endif
3945
Guido van Rossumad0ee831995-03-01 10:34:45 +00003946#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003947PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003948"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003949Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003950
Barry Warsaw53699e91996-12-10 23:23:01 +00003951static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003952posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003953{
Christian Heimes217cfd12007-12-02 14:31:20 +00003954 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003955}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003956#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003957
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003958
Guido van Rossumad0ee831995-03-01 10:34:45 +00003959#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003960PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003961"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003962Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003963
Barry Warsaw53699e91996-12-10 23:23:01 +00003964static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003965posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003966{
Christian Heimes292d3512008-02-03 16:51:08 +00003967 pid_t pid;
3968 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003969 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003970 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003971#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003972 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3973 APIRET rc;
3974 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003975 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003976
3977 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3978 APIRET rc;
3979 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003980 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003981
3982 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003983 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003984#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003985 if (kill(pid, sig) == -1)
3986 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003987#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003988 Py_INCREF(Py_None);
3989 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003990}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003991#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003992
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003993#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003994PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003995"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003996Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003997
3998static PyObject *
3999posix_killpg(PyObject *self, PyObject *args)
4000{
4001 int pgid, sig;
4002 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4003 return NULL;
4004 if (killpg(pgid, sig) == -1)
4005 return posix_error();
4006 Py_INCREF(Py_None);
4007 return Py_None;
4008}
4009#endif
4010
Guido van Rossumc0125471996-06-28 18:55:32 +00004011#ifdef HAVE_PLOCK
4012
4013#ifdef HAVE_SYS_LOCK_H
4014#include <sys/lock.h>
4015#endif
4016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004018"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004019Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004020
Barry Warsaw53699e91996-12-10 23:23:01 +00004021static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004022posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004023{
4024 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004025 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004026 return NULL;
4027 if (plock(op) == -1)
4028 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004029 Py_INCREF(Py_None);
4030 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004031}
4032#endif
4033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004034
Guido van Rossum3b066191991-06-04 19:40:25 +00004035
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004036
Guido van Rossumb6775db1994-08-01 11:34:53 +00004037#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004038PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004039"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004040Set the current process's user id.");
4041
Barry Warsaw53699e91996-12-10 23:23:01 +00004042static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004043posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004044{
4045 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004046 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004047 return NULL;
4048 if (setuid(uid) < 0)
4049 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004050 Py_INCREF(Py_None);
4051 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004052}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004053#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004055
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004056#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004057PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004058"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004059Set the current process's effective user id.");
4060
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004061static PyObject *
4062posix_seteuid (PyObject *self, PyObject *args)
4063{
4064 int euid;
4065 if (!PyArg_ParseTuple(args, "i", &euid)) {
4066 return NULL;
4067 } else if (seteuid(euid) < 0) {
4068 return posix_error();
4069 } else {
4070 Py_INCREF(Py_None);
4071 return Py_None;
4072 }
4073}
4074#endif /* HAVE_SETEUID */
4075
4076#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004077PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004078"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004079Set the current process's effective group id.");
4080
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004081static PyObject *
4082posix_setegid (PyObject *self, PyObject *args)
4083{
4084 int egid;
4085 if (!PyArg_ParseTuple(args, "i", &egid)) {
4086 return NULL;
4087 } else if (setegid(egid) < 0) {
4088 return posix_error();
4089 } else {
4090 Py_INCREF(Py_None);
4091 return Py_None;
4092 }
4093}
4094#endif /* HAVE_SETEGID */
4095
4096#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004097PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004098"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004099Set the current process's real and effective user ids.");
4100
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004101static PyObject *
4102posix_setreuid (PyObject *self, PyObject *args)
4103{
4104 int ruid, euid;
4105 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4106 return NULL;
4107 } else if (setreuid(ruid, euid) < 0) {
4108 return posix_error();
4109 } else {
4110 Py_INCREF(Py_None);
4111 return Py_None;
4112 }
4113}
4114#endif /* HAVE_SETREUID */
4115
4116#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004117PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004118"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004119Set the current process's real and effective group ids.");
4120
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004121static PyObject *
4122posix_setregid (PyObject *self, PyObject *args)
4123{
4124 int rgid, egid;
4125 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4126 return NULL;
4127 } else if (setregid(rgid, egid) < 0) {
4128 return posix_error();
4129 } else {
4130 Py_INCREF(Py_None);
4131 return Py_None;
4132 }
4133}
4134#endif /* HAVE_SETREGID */
4135
Guido van Rossumb6775db1994-08-01 11:34:53 +00004136#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004137PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004138"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004139Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004140
Barry Warsaw53699e91996-12-10 23:23:01 +00004141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004142posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004143{
4144 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004145 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004146 return NULL;
4147 if (setgid(gid) < 0)
4148 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004149 Py_INCREF(Py_None);
4150 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004151}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004152#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004153
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004154#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004155PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004156"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004157Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004158
4159static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004160posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004161{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004162 int i, len;
4163 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004164
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004165 if (!PySequence_Check(groups)) {
4166 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4167 return NULL;
4168 }
4169 len = PySequence_Size(groups);
4170 if (len > MAX_GROUPS) {
4171 PyErr_SetString(PyExc_ValueError, "too many groups");
4172 return NULL;
4173 }
4174 for(i = 0; i < len; i++) {
4175 PyObject *elem;
4176 elem = PySequence_GetItem(groups, i);
4177 if (!elem)
4178 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004179 if (!PyLong_Check(elem)) {
4180 PyErr_SetString(PyExc_TypeError,
4181 "groups must be integers");
4182 Py_DECREF(elem);
4183 return NULL;
4184 } else {
4185 unsigned long x = PyLong_AsUnsignedLong(elem);
4186 if (PyErr_Occurred()) {
4187 PyErr_SetString(PyExc_TypeError,
4188 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004189 Py_DECREF(elem);
4190 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004191 }
Georg Brandla13c2442005-11-22 19:30:31 +00004192 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004193 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004194 if (grouplist[i] != x) {
4195 PyErr_SetString(PyExc_TypeError,
4196 "group id too big");
4197 Py_DECREF(elem);
4198 return NULL;
4199 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004200 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004201 Py_DECREF(elem);
4202 }
4203
4204 if (setgroups(len, grouplist) < 0)
4205 return posix_error();
4206 Py_INCREF(Py_None);
4207 return Py_None;
4208}
4209#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004210
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004211#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4212static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004213wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004214{
4215 PyObject *result;
4216 static PyObject *struct_rusage;
4217
4218 if (pid == -1)
4219 return posix_error();
4220
4221 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004222 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004223 if (m == NULL)
4224 return NULL;
4225 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4226 Py_DECREF(m);
4227 if (struct_rusage == NULL)
4228 return NULL;
4229 }
4230
4231 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4232 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4233 if (!result)
4234 return NULL;
4235
4236#ifndef doubletime
4237#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4238#endif
4239
4240 PyStructSequence_SET_ITEM(result, 0,
4241 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4242 PyStructSequence_SET_ITEM(result, 1,
4243 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4244#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004245 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004246 SET_INT(result, 2, ru->ru_maxrss);
4247 SET_INT(result, 3, ru->ru_ixrss);
4248 SET_INT(result, 4, ru->ru_idrss);
4249 SET_INT(result, 5, ru->ru_isrss);
4250 SET_INT(result, 6, ru->ru_minflt);
4251 SET_INT(result, 7, ru->ru_majflt);
4252 SET_INT(result, 8, ru->ru_nswap);
4253 SET_INT(result, 9, ru->ru_inblock);
4254 SET_INT(result, 10, ru->ru_oublock);
4255 SET_INT(result, 11, ru->ru_msgsnd);
4256 SET_INT(result, 12, ru->ru_msgrcv);
4257 SET_INT(result, 13, ru->ru_nsignals);
4258 SET_INT(result, 14, ru->ru_nvcsw);
4259 SET_INT(result, 15, ru->ru_nivcsw);
4260#undef SET_INT
4261
4262 if (PyErr_Occurred()) {
4263 Py_DECREF(result);
4264 return NULL;
4265 }
4266
4267 return Py_BuildValue("iiN", pid, status, result);
4268}
4269#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4270
4271#ifdef HAVE_WAIT3
4272PyDoc_STRVAR(posix_wait3__doc__,
4273"wait3(options) -> (pid, status, rusage)\n\n\
4274Wait for completion of a child process.");
4275
4276static PyObject *
4277posix_wait3(PyObject *self, PyObject *args)
4278{
Christian Heimes292d3512008-02-03 16:51:08 +00004279 pid_t pid;
4280 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004281 struct rusage ru;
4282 WAIT_TYPE status;
4283 WAIT_STATUS_INT(status) = 0;
4284
4285 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4286 return NULL;
4287
4288 Py_BEGIN_ALLOW_THREADS
4289 pid = wait3(&status, options, &ru);
4290 Py_END_ALLOW_THREADS
4291
4292 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4293}
4294#endif /* HAVE_WAIT3 */
4295
4296#ifdef HAVE_WAIT4
4297PyDoc_STRVAR(posix_wait4__doc__,
4298"wait4(pid, options) -> (pid, status, rusage)\n\n\
4299Wait for completion of a given child process.");
4300
4301static PyObject *
4302posix_wait4(PyObject *self, PyObject *args)
4303{
Christian Heimes292d3512008-02-03 16:51:08 +00004304 pid_t pid;
4305 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004306 struct rusage ru;
4307 WAIT_TYPE status;
4308 WAIT_STATUS_INT(status) = 0;
4309
4310 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4311 return NULL;
4312
4313 Py_BEGIN_ALLOW_THREADS
4314 pid = wait4(pid, &status, options, &ru);
4315 Py_END_ALLOW_THREADS
4316
4317 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4318}
4319#endif /* HAVE_WAIT4 */
4320
Guido van Rossumb6775db1994-08-01 11:34:53 +00004321#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004322PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004323"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004324Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004325
Barry Warsaw53699e91996-12-10 23:23:01 +00004326static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004327posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004328{
Christian Heimes292d3512008-02-03 16:51:08 +00004329 pid_t pid;
4330 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004331 WAIT_TYPE status;
4332 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004333
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004334 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004335 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004336 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004337 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004338 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004339 if (pid == -1)
4340 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004341
4342 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004343}
4344
Tim Petersab034fa2002-02-01 11:27:43 +00004345#elif defined(HAVE_CWAIT)
4346
4347/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004348PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004349"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004350"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004351
4352static PyObject *
4353posix_waitpid(PyObject *self, PyObject *args)
4354{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004355 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004356 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004357
4358 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4359 return NULL;
4360 Py_BEGIN_ALLOW_THREADS
4361 pid = _cwait(&status, pid, options);
4362 Py_END_ALLOW_THREADS
4363 if (pid == -1)
4364 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004365
4366 /* shift the status left a byte so this is more like the POSIX waitpid */
4367 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004368}
4369#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004370
Guido van Rossumad0ee831995-03-01 10:34:45 +00004371#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004372PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004373"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004374Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004375
Barry Warsaw53699e91996-12-10 23:23:01 +00004376static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004377posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004378{
Christian Heimes292d3512008-02-03 16:51:08 +00004379 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004380 WAIT_TYPE status;
4381 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004382
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004383 Py_BEGIN_ALLOW_THREADS
4384 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004385 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004386 if (pid == -1)
4387 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004388
4389 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004390}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004391#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004392
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004394PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004395"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004396Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004397
Barry Warsaw53699e91996-12-10 23:23:01 +00004398static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004399posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004400{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004401#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004402 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004403#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004404#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004405 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004406#else
4407 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4408#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004409#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004410}
4411
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004412
Guido van Rossumb6775db1994-08-01 11:34:53 +00004413#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004414PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004415"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004416Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004417
Barry Warsaw53699e91996-12-10 23:23:01 +00004418static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004419posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004420{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004421 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004422 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004423 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004424 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004425 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004426
4427 if (!PyArg_ParseTuple(args, "et:readlink",
4428 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004429 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004430 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004431 if (v == NULL) {
4432 PyMem_Free(path);
4433 return NULL;
4434 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004435
4436 if (PyUnicode_Check(v)) {
4437 arg_is_unicode = 1;
4438 }
4439 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004440
Barry Warsaw53699e91996-12-10 23:23:01 +00004441 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004442 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004443 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004444 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004445 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004446
Neal Norwitzfca70052007-08-12 16:56:02 +00004447 PyMem_Free(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004448 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004449 if (arg_is_unicode) {
4450 PyObject *w;
4451
4452 w = PyUnicode_FromEncodedObject(v,
4453 Py_FileSystemDefaultEncoding,
4454 "strict");
4455 if (w != NULL) {
4456 Py_DECREF(v);
4457 v = w;
4458 }
4459 else {
4460 /* fall back to the original byte string, as
4461 discussed in patch #683592 */
4462 PyErr_Clear();
4463 }
4464 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004465 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004466}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004467#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004468
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004469
Guido van Rossumb6775db1994-08-01 11:34:53 +00004470#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004471PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004472"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004473Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004474
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004476posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004477{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004478 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004479}
4480#endif /* HAVE_SYMLINK */
4481
4482
4483#ifdef HAVE_TIMES
4484#ifndef HZ
4485#define HZ 60 /* Universal constant :-) */
4486#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004487
Guido van Rossumd48f2521997-12-05 22:19:34 +00004488#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4489static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004490system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004491{
4492 ULONG value = 0;
4493
4494 Py_BEGIN_ALLOW_THREADS
4495 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4496 Py_END_ALLOW_THREADS
4497
4498 return value;
4499}
4500
4501static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004502posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004503{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004504 /* Currently Only Uptime is Provided -- Others Later */
4505 return Py_BuildValue("ddddd",
4506 (double)0 /* t.tms_utime / HZ */,
4507 (double)0 /* t.tms_stime / HZ */,
4508 (double)0 /* t.tms_cutime / HZ */,
4509 (double)0 /* t.tms_cstime / HZ */,
4510 (double)system_uptime() / 1000);
4511}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004512#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004513static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004514posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004515{
4516 struct tms t;
4517 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004518 errno = 0;
4519 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004520 if (c == (clock_t) -1)
4521 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004522 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004523 (double)t.tms_utime / HZ,
4524 (double)t.tms_stime / HZ,
4525 (double)t.tms_cutime / HZ,
4526 (double)t.tms_cstime / HZ,
4527 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004528}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004529#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004530#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004531
4532
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004533#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004534#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004535static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004536posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004537{
4538 FILETIME create, exit, kernel, user;
4539 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004540 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004541 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4542 /* The fields of a FILETIME structure are the hi and lo part
4543 of a 64-bit value expressed in 100 nanosecond units.
4544 1e7 is one second in such units; 1e-7 the inverse.
4545 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4546 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004547 return Py_BuildValue(
4548 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004549 (double)(user.dwHighDateTime*429.4967296 +
4550 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004551 (double)(kernel.dwHighDateTime*429.4967296 +
4552 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004553 (double)0,
4554 (double)0,
4555 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004556}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004557#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004558
4559#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004560PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004561"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004562Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004563#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004565
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004566#ifdef HAVE_GETSID
4567PyDoc_STRVAR(posix_getsid__doc__,
4568"getsid(pid) -> sid\n\n\
4569Call the system call getsid().");
4570
4571static PyObject *
4572posix_getsid(PyObject *self, PyObject *args)
4573{
Christian Heimes292d3512008-02-03 16:51:08 +00004574 pid_t pid;
4575 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004576 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4577 return NULL;
4578 sid = getsid(pid);
4579 if (sid < 0)
4580 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004581 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004582}
4583#endif /* HAVE_GETSID */
4584
4585
Guido van Rossumb6775db1994-08-01 11:34:53 +00004586#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004587PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004588"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004589Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004590
Barry Warsaw53699e91996-12-10 23:23:01 +00004591static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004592posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004593{
Guido van Rossum687dd131993-05-17 08:34:16 +00004594 if (setsid() < 0)
4595 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004596 Py_INCREF(Py_None);
4597 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004598}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004599#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004600
Guido van Rossumb6775db1994-08-01 11:34:53 +00004601#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004603"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004604Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004605
Barry Warsaw53699e91996-12-10 23:23:01 +00004606static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004607posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004608{
Christian Heimes292d3512008-02-03 16:51:08 +00004609 pid_t pid;
4610 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004611 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004612 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004613 if (setpgid(pid, pgrp) < 0)
4614 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004615 Py_INCREF(Py_None);
4616 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004617}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004618#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004619
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004620
Guido van Rossumb6775db1994-08-01 11:34:53 +00004621#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004622PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004623"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004624Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004625
Barry Warsaw53699e91996-12-10 23:23:01 +00004626static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004627posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004628{
Christian Heimes15ebc882008-02-04 18:48:49 +00004629 int fd;
4630 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004631 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004632 return NULL;
4633 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004634 if (pgid < 0)
4635 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004636 return PyLong_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004637}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004638#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004639
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004640
Guido van Rossumb6775db1994-08-01 11:34:53 +00004641#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004642PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004643"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004644Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004645
Barry Warsaw53699e91996-12-10 23:23:01 +00004646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004647posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004648{
4649 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004650 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004651 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004652 if (tcsetpgrp(fd, pgid) < 0)
4653 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004654 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004655 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004656}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004657#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004658
Guido van Rossum687dd131993-05-17 08:34:16 +00004659/* Functions acting on file descriptors */
4660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004661PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004662"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004663Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004664
Barry Warsaw53699e91996-12-10 23:23:01 +00004665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004666posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004667{
Mark Hammondef8b6542001-05-13 08:04:26 +00004668 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004669 int flag;
4670 int mode = 0777;
4671 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004672
4673#ifdef MS_WINDOWS
4674 if (unicode_file_names()) {
4675 PyUnicodeObject *po;
4676 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4677 Py_BEGIN_ALLOW_THREADS
4678 /* PyUnicode_AS_UNICODE OK without thread
4679 lock as it is a simple dereference. */
4680 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4681 Py_END_ALLOW_THREADS
4682 if (fd < 0)
4683 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004684 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004685 }
4686 /* Drop the argument parsing error as narrow strings
4687 are also valid. */
4688 PyErr_Clear();
4689 }
4690#endif
4691
Tim Peters5aa91602002-01-30 05:46:57 +00004692 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004693 Py_FileSystemDefaultEncoding, &file,
4694 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004695 return NULL;
4696
Barry Warsaw53699e91996-12-10 23:23:01 +00004697 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004698 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004700 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004701 return posix_error_with_allocated_filename(file);
4702 PyMem_Free(file);
Christian Heimes217cfd12007-12-02 14:31:20 +00004703 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004704}
4705
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004707PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004708"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004709Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004710
Barry Warsaw53699e91996-12-10 23:23:01 +00004711static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004712posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004713{
4714 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004715 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004716 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004717 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004718 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004719 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004720 if (res < 0)
4721 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004722 Py_INCREF(Py_None);
4723 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004724}
4725
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004726
Christian Heimesfdab48e2008-01-20 09:06:41 +00004727PyDoc_STRVAR(posix_closerange__doc__,
4728"closerange(fd_low, fd_high)\n\n\
4729Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4730
4731static PyObject *
4732posix_closerange(PyObject *self, PyObject *args)
4733{
4734 int fd_from, fd_to, i;
4735 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4736 return NULL;
4737 Py_BEGIN_ALLOW_THREADS
4738 for (i = fd_from; i < fd_to; i++)
4739 close(i);
4740 Py_END_ALLOW_THREADS
4741 Py_RETURN_NONE;
4742}
4743
4744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004745PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004746"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004747Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004748
Barry Warsaw53699e91996-12-10 23:23:01 +00004749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004750posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004751{
4752 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004753 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004754 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004755 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004756 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004757 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004758 if (fd < 0)
4759 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004760 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004761}
4762
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004764PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004765"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004766Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004767
Barry Warsaw53699e91996-12-10 23:23:01 +00004768static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004769posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004770{
4771 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004772 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004773 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004774 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004775 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004776 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004777 if (res < 0)
4778 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004779 Py_INCREF(Py_None);
4780 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004781}
4782
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004783
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004784PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004785"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004786Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004787
Barry Warsaw53699e91996-12-10 23:23:01 +00004788static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004789posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004790{
4791 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004792#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004793 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004794#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004795 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004796#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004797 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004798 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004799 return NULL;
4800#ifdef SEEK_SET
4801 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4802 switch (how) {
4803 case 0: how = SEEK_SET; break;
4804 case 1: how = SEEK_CUR; break;
4805 case 2: how = SEEK_END; break;
4806 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004807#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004808
4809#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004810 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004811#else
4812 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004813 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004814#endif
4815 if (PyErr_Occurred())
4816 return NULL;
4817
Barry Warsaw53699e91996-12-10 23:23:01 +00004818 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004819#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004820 res = _lseeki64(fd, pos, how);
4821#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004822 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004823#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004824 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004825 if (res < 0)
4826 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004827
4828#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004829 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004830#else
4831 return PyLong_FromLongLong(res);
4832#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004833}
4834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004835
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004836PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004837"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004838Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004839
Barry Warsaw53699e91996-12-10 23:23:01 +00004840static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004841posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004842{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004843 int fd, size;
4844 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004845 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004846 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004847 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004848 if (size < 0) {
4849 errno = EINVAL;
4850 return posix_error();
4851 }
Guido van Rossumf9e443c2007-11-21 20:17:11 +00004852 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004853 if (buffer == NULL)
4854 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004855 Py_BEGIN_ALLOW_THREADS
Guido van Rossumf9e443c2007-11-21 20:17:11 +00004856 n = read(fd, PyString_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004857 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004858 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004859 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004860 return posix_error();
4861 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004862 if (n != size)
Guido van Rossumf9e443c2007-11-21 20:17:11 +00004863 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004864 return buffer;
4865}
4866
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004867
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004868PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004869"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004870Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004871
Barry Warsaw53699e91996-12-10 23:23:01 +00004872static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004873posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004874{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004875 int fd;
4876 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004877 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004878
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004879 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004880 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004881 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004882 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004883 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004884 if (size < 0)
4885 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004886 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004887}
4888
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004890PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004891"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004892Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004893
Barry Warsaw53699e91996-12-10 23:23:01 +00004894static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004895posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004896{
4897 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004898 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004899 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004900 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004901 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004902#ifdef __VMS
4903 /* on OpenVMS we must ensure that all bytes are written to the file */
4904 fsync(fd);
4905#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004906 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004907 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004908 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004909 if (res != 0) {
4910#ifdef MS_WINDOWS
4911 return win32_error("fstat", NULL);
4912#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004913 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004914#endif
4915 }
Tim Peters5aa91602002-01-30 05:46:57 +00004916
Martin v. Löwis14694662006-02-03 12:54:16 +00004917 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004918}
4919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004920PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004921"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004922Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004923connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004924
4925static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004926posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004927{
4928 int fd;
4929 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4930 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004931 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004932}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004933
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004934#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004935PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004936"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004937Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004938
Barry Warsaw53699e91996-12-10 23:23:01 +00004939static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004940posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004941{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004942#if defined(PYOS_OS2)
4943 HFILE read, write;
4944 APIRET rc;
4945
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004946 Py_BEGIN_ALLOW_THREADS
4947 rc = DosCreatePipe( &read, &write, 4096);
4948 Py_END_ALLOW_THREADS
4949 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004950 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004951
4952 return Py_BuildValue("(ii)", read, write);
4953#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004954#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004955 int fds[2];
4956 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004957 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004958 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004959 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004960 if (res != 0)
4961 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004962 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004963#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004964 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004965 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004966 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004967 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004968 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004969 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004970 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004971 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004972 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4973 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004974 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004975#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004976#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004977}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004978#endif /* HAVE_PIPE */
4979
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004980
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004981#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004982PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004983"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004984Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004985
Barry Warsaw53699e91996-12-10 23:23:01 +00004986static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004987posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004988{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004989 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004990 int mode = 0666;
4991 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004992 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004993 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004994 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004995 res = mkfifo(filename, mode);
4996 Py_END_ALLOW_THREADS
4997 if (res < 0)
4998 return posix_error();
4999 Py_INCREF(Py_None);
5000 return Py_None;
5001}
5002#endif
5003
5004
Neal Norwitz11690112002-07-30 01:08:28 +00005005#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005006PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005007"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005008Create a filesystem node (file, device special file or named pipe)\n\
5009named filename. mode specifies both the permissions to use and the\n\
5010type of node to be created, being combined (bitwise OR) with one of\n\
5011S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005012device defines the newly created device special file (probably using\n\
5013os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005014
5015
5016static PyObject *
5017posix_mknod(PyObject *self, PyObject *args)
5018{
5019 char *filename;
5020 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005021 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005022 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005023 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005024 return NULL;
5025 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005026 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005027 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005028 if (res < 0)
5029 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005030 Py_INCREF(Py_None);
5031 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005032}
5033#endif
5034
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005035#ifdef HAVE_DEVICE_MACROS
5036PyDoc_STRVAR(posix_major__doc__,
5037"major(device) -> major number\n\
5038Extracts a device major number from a raw device number.");
5039
5040static PyObject *
5041posix_major(PyObject *self, PyObject *args)
5042{
5043 int device;
5044 if (!PyArg_ParseTuple(args, "i:major", &device))
5045 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005046 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005047}
5048
5049PyDoc_STRVAR(posix_minor__doc__,
5050"minor(device) -> minor number\n\
5051Extracts a device minor number from a raw device number.");
5052
5053static PyObject *
5054posix_minor(PyObject *self, PyObject *args)
5055{
5056 int device;
5057 if (!PyArg_ParseTuple(args, "i:minor", &device))
5058 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005059 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005060}
5061
5062PyDoc_STRVAR(posix_makedev__doc__,
5063"makedev(major, minor) -> device number\n\
5064Composes a raw device number from the major and minor device numbers.");
5065
5066static PyObject *
5067posix_makedev(PyObject *self, PyObject *args)
5068{
5069 int major, minor;
5070 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5071 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005072 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005073}
5074#endif /* device macros */
5075
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005076
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005077#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005078PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005079"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005080Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005081
Barry Warsaw53699e91996-12-10 23:23:01 +00005082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005083posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005084{
5085 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005086 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005087 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005088 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005089
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005090 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005091 return NULL;
5092
5093#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005094 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005095#else
5096 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005097 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005098#endif
5099 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005100 return NULL;
5101
Barry Warsaw53699e91996-12-10 23:23:01 +00005102 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005103 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005104 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005105 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005106 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005107 return NULL;
5108 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005109 Py_INCREF(Py_None);
5110 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005111}
5112#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005113
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005114#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005115PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005116"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005117Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005118
Fred Drake762e2061999-08-26 17:23:54 +00005119/* Save putenv() parameters as values here, so we can collect them when they
5120 * get re-set with another call for the same key. */
5121static PyObject *posix_putenv_garbage;
5122
Tim Peters5aa91602002-01-30 05:46:57 +00005123static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005124posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005125{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005126#ifdef MS_WINDOWS
5127 wchar_t *s1, *s2;
5128 wchar_t *newenv;
5129#else
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005130 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005131 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005132#endif
Fred Drake762e2061999-08-26 17:23:54 +00005133 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005134 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005135
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005136 if (!PyArg_ParseTuple(args,
5137#ifdef MS_WINDOWS
5138 "uu:putenv",
5139#else
5140 "ss:putenv",
5141#endif
5142 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005143 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005144
5145#if defined(PYOS_OS2)
5146 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5147 APIRET rc;
5148
Guido van Rossumd48f2521997-12-05 22:19:34 +00005149 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5150 if (rc != NO_ERROR)
5151 return os2_error(rc);
5152
5153 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5154 APIRET rc;
5155
Guido van Rossumd48f2521997-12-05 22:19:34 +00005156 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5157 if (rc != NO_ERROR)
5158 return os2_error(rc);
5159 } else {
5160#endif
Fred Drake762e2061999-08-26 17:23:54 +00005161 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005162 /* len includes space for a trailing \0; the size arg to
5163 PyString_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005164#ifdef MS_WINDOWS
5165 len = wcslen(s1) + wcslen(s2) + 2;
5166 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5167#else
5168 len = strlen(s1) + strlen(s2) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00005169 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005170#endif
Fred Drake762e2061999-08-26 17:23:54 +00005171 if (newstr == NULL)
5172 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005173#ifdef MS_WINDOWS
5174 newenv = PyUnicode_AsUnicode(newstr);
5175 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5176 if (_wputenv(newenv)) {
5177 Py_DECREF(newstr);
5178 posix_error();
5179 return NULL;
5180 }
5181#else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005182 newenv = PyString_AS_STRING(newstr);
5183 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5184 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005185 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005186 posix_error();
5187 return NULL;
5188 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005189#endif
Fred Drake762e2061999-08-26 17:23:54 +00005190 /* Install the first arg and newstr in posix_putenv_garbage;
5191 * this will cause previous value to be collected. This has to
5192 * happen after the real putenv() call because the old value
5193 * was still accessible until then. */
5194 if (PyDict_SetItem(posix_putenv_garbage,
5195 PyTuple_GET_ITEM(args, 0), newstr)) {
5196 /* really not much we can do; just leak */
5197 PyErr_Clear();
5198 }
5199 else {
5200 Py_DECREF(newstr);
5201 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005202
5203#if defined(PYOS_OS2)
5204 }
5205#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005206 Py_INCREF(Py_None);
5207 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005208}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005209#endif /* putenv */
5210
Guido van Rossumc524d952001-10-19 01:31:59 +00005211#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005213"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005214Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005215
5216static PyObject *
5217posix_unsetenv(PyObject *self, PyObject *args)
5218{
5219 char *s1;
5220
5221 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5222 return NULL;
5223
5224 unsetenv(s1);
5225
5226 /* Remove the key from posix_putenv_garbage;
5227 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005228 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005229 * old value was still accessible until then.
5230 */
5231 if (PyDict_DelItem(posix_putenv_garbage,
5232 PyTuple_GET_ITEM(args, 0))) {
5233 /* really not much we can do; just leak */
5234 PyErr_Clear();
5235 }
5236
5237 Py_INCREF(Py_None);
5238 return Py_None;
5239}
5240#endif /* unsetenv */
5241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005242PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005243"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005244Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005245
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005246static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005247posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005248{
5249 int code;
5250 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005251 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005252 return NULL;
5253 message = strerror(code);
5254 if (message == NULL) {
5255 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005256 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005257 return NULL;
5258 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005259 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005260}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005261
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005262
Guido van Rossumc9641791998-08-04 15:26:23 +00005263#ifdef HAVE_SYS_WAIT_H
5264
Fred Drake106c1a02002-04-23 15:58:02 +00005265#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005266PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005267"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005268Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005269
5270static PyObject *
5271posix_WCOREDUMP(PyObject *self, PyObject *args)
5272{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005273 WAIT_TYPE status;
5274 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005275
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005276 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005277 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005278
5279 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005280}
5281#endif /* WCOREDUMP */
5282
5283#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005284PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005285"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005286Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005287job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005288
5289static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005290posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005291{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005292 WAIT_TYPE status;
5293 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005294
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005295 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005296 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005297
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005298 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005299}
5300#endif /* WIFCONTINUED */
5301
Guido van Rossumc9641791998-08-04 15:26:23 +00005302#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005303PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005304"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005305Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005306
5307static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005308posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005309{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005310 WAIT_TYPE status;
5311 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005312
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005313 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005314 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005315
Fred Drake106c1a02002-04-23 15:58:02 +00005316 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005317}
5318#endif /* WIFSTOPPED */
5319
5320#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005321PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005322"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005323Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005324
5325static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005326posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005327{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005328 WAIT_TYPE status;
5329 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005330
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005331 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005332 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005333
Fred Drake106c1a02002-04-23 15:58:02 +00005334 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005335}
5336#endif /* WIFSIGNALED */
5337
5338#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005339PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005340"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005341Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005342system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005343
5344static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005345posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005346{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005347 WAIT_TYPE status;
5348 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005349
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005350 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005351 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005352
Fred Drake106c1a02002-04-23 15:58:02 +00005353 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005354}
5355#endif /* WIFEXITED */
5356
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005357#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005359"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005361
5362static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005363posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005364{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005365 WAIT_TYPE status;
5366 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005367
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005368 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005369 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005370
Guido van Rossumc9641791998-08-04 15:26:23 +00005371 return Py_BuildValue("i", WEXITSTATUS(status));
5372}
5373#endif /* WEXITSTATUS */
5374
5375#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005376PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005377"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005378Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005379value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005380
5381static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005382posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005383{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005384 WAIT_TYPE status;
5385 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005386
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005387 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005388 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005389
Guido van Rossumc9641791998-08-04 15:26:23 +00005390 return Py_BuildValue("i", WTERMSIG(status));
5391}
5392#endif /* WTERMSIG */
5393
5394#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005395PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005396"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005397Return the signal that stopped the process that provided\n\
5398the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005399
5400static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005401posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005402{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005403 WAIT_TYPE status;
5404 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005405
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005406 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005407 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005408
Guido van Rossumc9641791998-08-04 15:26:23 +00005409 return Py_BuildValue("i", WSTOPSIG(status));
5410}
5411#endif /* WSTOPSIG */
5412
5413#endif /* HAVE_SYS_WAIT_H */
5414
5415
Thomas Wouters477c8d52006-05-27 19:21:47 +00005416#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005417#ifdef _SCO_DS
5418/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5419 needed definitions in sys/statvfs.h */
5420#define _SVID3
5421#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005422#include <sys/statvfs.h>
5423
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005424static PyObject*
5425_pystatvfs_fromstructstatvfs(struct statvfs st) {
5426 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5427 if (v == NULL)
5428 return NULL;
5429
5430#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005431 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5432 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5433 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5434 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5435 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5436 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5437 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5438 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5439 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5440 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005441#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005442 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5443 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005444 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005445 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005446 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005447 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005448 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005449 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005450 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005451 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005452 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005453 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005454 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005455 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005456 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5457 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005458#endif
5459
5460 return v;
5461}
5462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005463PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005464"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005465Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005466
5467static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005468posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005469{
5470 int fd, res;
5471 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005472
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005473 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005474 return NULL;
5475 Py_BEGIN_ALLOW_THREADS
5476 res = fstatvfs(fd, &st);
5477 Py_END_ALLOW_THREADS
5478 if (res != 0)
5479 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005480
5481 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005482}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005483#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005484
5485
Thomas Wouters477c8d52006-05-27 19:21:47 +00005486#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005487#include <sys/statvfs.h>
5488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005489PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005490"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005491Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005492
5493static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005494posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005495{
5496 char *path;
5497 int res;
5498 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005499 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005500 return NULL;
5501 Py_BEGIN_ALLOW_THREADS
5502 res = statvfs(path, &st);
5503 Py_END_ALLOW_THREADS
5504 if (res != 0)
5505 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005506
5507 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005508}
5509#endif /* HAVE_STATVFS */
5510
Fred Drakec9680921999-12-13 16:37:25 +00005511/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5512 * It maps strings representing configuration variable names to
5513 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005514 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005515 * rarely-used constants. There are three separate tables that use
5516 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005517 *
5518 * This code is always included, even if none of the interfaces that
5519 * need it are included. The #if hackery needed to avoid it would be
5520 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005521 */
5522struct constdef {
5523 char *name;
5524 long value;
5525};
5526
Fred Drake12c6e2d1999-12-14 21:25:03 +00005527static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005528conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005529 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005530{
Christian Heimes217cfd12007-12-02 14:31:20 +00005531 if (PyLong_Check(arg)) {
5532 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005533 return 1;
5534 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005535 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005536 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005537 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005538 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005539 size_t hi = tablesize;
5540 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005541 const char *confname;
5542 Py_ssize_t namelen;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005543 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005544 PyErr_SetString(PyExc_TypeError,
5545 "configuration names must be strings or integers");
5546 return 0;
5547 }
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005548 confname = PyUnicode_AsString(arg);
5549 if (confname == NULL)
5550 return 0;
5551 namelen = strlen(confname);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005552 while (lo < hi) {
5553 mid = (lo + hi) / 2;
5554 cmp = strcmp(confname, table[mid].name);
5555 if (cmp < 0)
5556 hi = mid;
5557 else if (cmp > 0)
5558 lo = mid + 1;
5559 else {
5560 *valuep = table[mid].value;
5561 return 1;
5562 }
5563 }
5564 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005565 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005566 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005567}
5568
5569
5570#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5571static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005572#ifdef _PC_ABI_AIO_XFER_MAX
5573 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5574#endif
5575#ifdef _PC_ABI_ASYNC_IO
5576 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5577#endif
Fred Drakec9680921999-12-13 16:37:25 +00005578#ifdef _PC_ASYNC_IO
5579 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5580#endif
5581#ifdef _PC_CHOWN_RESTRICTED
5582 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5583#endif
5584#ifdef _PC_FILESIZEBITS
5585 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5586#endif
5587#ifdef _PC_LAST
5588 {"PC_LAST", _PC_LAST},
5589#endif
5590#ifdef _PC_LINK_MAX
5591 {"PC_LINK_MAX", _PC_LINK_MAX},
5592#endif
5593#ifdef _PC_MAX_CANON
5594 {"PC_MAX_CANON", _PC_MAX_CANON},
5595#endif
5596#ifdef _PC_MAX_INPUT
5597 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5598#endif
5599#ifdef _PC_NAME_MAX
5600 {"PC_NAME_MAX", _PC_NAME_MAX},
5601#endif
5602#ifdef _PC_NO_TRUNC
5603 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5604#endif
5605#ifdef _PC_PATH_MAX
5606 {"PC_PATH_MAX", _PC_PATH_MAX},
5607#endif
5608#ifdef _PC_PIPE_BUF
5609 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5610#endif
5611#ifdef _PC_PRIO_IO
5612 {"PC_PRIO_IO", _PC_PRIO_IO},
5613#endif
5614#ifdef _PC_SOCK_MAXBUF
5615 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5616#endif
5617#ifdef _PC_SYNC_IO
5618 {"PC_SYNC_IO", _PC_SYNC_IO},
5619#endif
5620#ifdef _PC_VDISABLE
5621 {"PC_VDISABLE", _PC_VDISABLE},
5622#endif
5623};
5624
Fred Drakec9680921999-12-13 16:37:25 +00005625static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005626conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005627{
5628 return conv_confname(arg, valuep, posix_constants_pathconf,
5629 sizeof(posix_constants_pathconf)
5630 / sizeof(struct constdef));
5631}
5632#endif
5633
5634#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005635PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005636"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005637Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005638If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005639
5640static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005641posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005642{
5643 PyObject *result = NULL;
5644 int name, fd;
5645
Fred Drake12c6e2d1999-12-14 21:25:03 +00005646 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5647 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005648 long limit;
5649
5650 errno = 0;
5651 limit = fpathconf(fd, name);
5652 if (limit == -1 && errno != 0)
5653 posix_error();
5654 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005655 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005656 }
5657 return result;
5658}
5659#endif
5660
5661
5662#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005663PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005664"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005665Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005666If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005667
5668static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005669posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005670{
5671 PyObject *result = NULL;
5672 int name;
5673 char *path;
5674
5675 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5676 conv_path_confname, &name)) {
5677 long limit;
5678
5679 errno = 0;
5680 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005681 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005682 if (errno == EINVAL)
5683 /* could be a path or name problem */
5684 posix_error();
5685 else
5686 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005687 }
Fred Drakec9680921999-12-13 16:37:25 +00005688 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005689 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005690 }
5691 return result;
5692}
5693#endif
5694
5695#ifdef HAVE_CONFSTR
5696static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005697#ifdef _CS_ARCHITECTURE
5698 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5699#endif
5700#ifdef _CS_HOSTNAME
5701 {"CS_HOSTNAME", _CS_HOSTNAME},
5702#endif
5703#ifdef _CS_HW_PROVIDER
5704 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5705#endif
5706#ifdef _CS_HW_SERIAL
5707 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5708#endif
5709#ifdef _CS_INITTAB_NAME
5710 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5711#endif
Fred Drakec9680921999-12-13 16:37:25 +00005712#ifdef _CS_LFS64_CFLAGS
5713 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5714#endif
5715#ifdef _CS_LFS64_LDFLAGS
5716 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5717#endif
5718#ifdef _CS_LFS64_LIBS
5719 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5720#endif
5721#ifdef _CS_LFS64_LINTFLAGS
5722 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5723#endif
5724#ifdef _CS_LFS_CFLAGS
5725 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5726#endif
5727#ifdef _CS_LFS_LDFLAGS
5728 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5729#endif
5730#ifdef _CS_LFS_LIBS
5731 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5732#endif
5733#ifdef _CS_LFS_LINTFLAGS
5734 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5735#endif
Fred Draked86ed291999-12-15 15:34:33 +00005736#ifdef _CS_MACHINE
5737 {"CS_MACHINE", _CS_MACHINE},
5738#endif
Fred Drakec9680921999-12-13 16:37:25 +00005739#ifdef _CS_PATH
5740 {"CS_PATH", _CS_PATH},
5741#endif
Fred Draked86ed291999-12-15 15:34:33 +00005742#ifdef _CS_RELEASE
5743 {"CS_RELEASE", _CS_RELEASE},
5744#endif
5745#ifdef _CS_SRPC_DOMAIN
5746 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5747#endif
5748#ifdef _CS_SYSNAME
5749 {"CS_SYSNAME", _CS_SYSNAME},
5750#endif
5751#ifdef _CS_VERSION
5752 {"CS_VERSION", _CS_VERSION},
5753#endif
Fred Drakec9680921999-12-13 16:37:25 +00005754#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5755 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5756#endif
5757#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5758 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5759#endif
5760#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5761 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5762#endif
5763#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5764 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5765#endif
5766#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5767 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5768#endif
5769#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5770 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5771#endif
5772#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5773 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5774#endif
5775#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5776 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5777#endif
5778#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5779 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5780#endif
5781#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5782 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5783#endif
5784#ifdef _CS_XBS5_LP64_OFF64_LIBS
5785 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5786#endif
5787#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5788 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5789#endif
5790#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5791 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5792#endif
5793#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5794 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5795#endif
5796#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5797 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5798#endif
5799#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5800 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5801#endif
Fred Draked86ed291999-12-15 15:34:33 +00005802#ifdef _MIPS_CS_AVAIL_PROCESSORS
5803 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5804#endif
5805#ifdef _MIPS_CS_BASE
5806 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5807#endif
5808#ifdef _MIPS_CS_HOSTID
5809 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5810#endif
5811#ifdef _MIPS_CS_HW_NAME
5812 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5813#endif
5814#ifdef _MIPS_CS_NUM_PROCESSORS
5815 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5816#endif
5817#ifdef _MIPS_CS_OSREL_MAJ
5818 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5819#endif
5820#ifdef _MIPS_CS_OSREL_MIN
5821 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5822#endif
5823#ifdef _MIPS_CS_OSREL_PATCH
5824 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5825#endif
5826#ifdef _MIPS_CS_OS_NAME
5827 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5828#endif
5829#ifdef _MIPS_CS_OS_PROVIDER
5830 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5831#endif
5832#ifdef _MIPS_CS_PROCESSORS
5833 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5834#endif
5835#ifdef _MIPS_CS_SERIAL
5836 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5837#endif
5838#ifdef _MIPS_CS_VENDOR
5839 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5840#endif
Fred Drakec9680921999-12-13 16:37:25 +00005841};
5842
5843static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005844conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005845{
5846 return conv_confname(arg, valuep, posix_constants_confstr,
5847 sizeof(posix_constants_confstr)
5848 / sizeof(struct constdef));
5849}
5850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005851PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005852"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005853Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005854
5855static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005856posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005857{
5858 PyObject *result = NULL;
5859 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005860 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005861
5862 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005863 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005864
Fred Drakec9680921999-12-13 16:37:25 +00005865 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005866 len = confstr(name, buffer, sizeof(buffer));
5867 if (len == 0) {
5868 if (errno) {
5869 posix_error();
5870 }
5871 else {
5872 result = Py_None;
5873 Py_INCREF(Py_None);
5874 }
Fred Drakec9680921999-12-13 16:37:25 +00005875 }
5876 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005877 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005878 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005879 if (result != NULL)
Neal Norwitz93c56822007-08-26 07:10:06 +00005880 confstr(name, PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005881 }
5882 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005883 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005884 }
5885 }
5886 return result;
5887}
5888#endif
5889
5890
5891#ifdef HAVE_SYSCONF
5892static struct constdef posix_constants_sysconf[] = {
5893#ifdef _SC_2_CHAR_TERM
5894 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5895#endif
5896#ifdef _SC_2_C_BIND
5897 {"SC_2_C_BIND", _SC_2_C_BIND},
5898#endif
5899#ifdef _SC_2_C_DEV
5900 {"SC_2_C_DEV", _SC_2_C_DEV},
5901#endif
5902#ifdef _SC_2_C_VERSION
5903 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5904#endif
5905#ifdef _SC_2_FORT_DEV
5906 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5907#endif
5908#ifdef _SC_2_FORT_RUN
5909 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5910#endif
5911#ifdef _SC_2_LOCALEDEF
5912 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5913#endif
5914#ifdef _SC_2_SW_DEV
5915 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5916#endif
5917#ifdef _SC_2_UPE
5918 {"SC_2_UPE", _SC_2_UPE},
5919#endif
5920#ifdef _SC_2_VERSION
5921 {"SC_2_VERSION", _SC_2_VERSION},
5922#endif
Fred Draked86ed291999-12-15 15:34:33 +00005923#ifdef _SC_ABI_ASYNCHRONOUS_IO
5924 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5925#endif
5926#ifdef _SC_ACL
5927 {"SC_ACL", _SC_ACL},
5928#endif
Fred Drakec9680921999-12-13 16:37:25 +00005929#ifdef _SC_AIO_LISTIO_MAX
5930 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5931#endif
Fred Drakec9680921999-12-13 16:37:25 +00005932#ifdef _SC_AIO_MAX
5933 {"SC_AIO_MAX", _SC_AIO_MAX},
5934#endif
5935#ifdef _SC_AIO_PRIO_DELTA_MAX
5936 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5937#endif
5938#ifdef _SC_ARG_MAX
5939 {"SC_ARG_MAX", _SC_ARG_MAX},
5940#endif
5941#ifdef _SC_ASYNCHRONOUS_IO
5942 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5943#endif
5944#ifdef _SC_ATEXIT_MAX
5945 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5946#endif
Fred Draked86ed291999-12-15 15:34:33 +00005947#ifdef _SC_AUDIT
5948 {"SC_AUDIT", _SC_AUDIT},
5949#endif
Fred Drakec9680921999-12-13 16:37:25 +00005950#ifdef _SC_AVPHYS_PAGES
5951 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5952#endif
5953#ifdef _SC_BC_BASE_MAX
5954 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5955#endif
5956#ifdef _SC_BC_DIM_MAX
5957 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5958#endif
5959#ifdef _SC_BC_SCALE_MAX
5960 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5961#endif
5962#ifdef _SC_BC_STRING_MAX
5963 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5964#endif
Fred Draked86ed291999-12-15 15:34:33 +00005965#ifdef _SC_CAP
5966 {"SC_CAP", _SC_CAP},
5967#endif
Fred Drakec9680921999-12-13 16:37:25 +00005968#ifdef _SC_CHARCLASS_NAME_MAX
5969 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5970#endif
5971#ifdef _SC_CHAR_BIT
5972 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5973#endif
5974#ifdef _SC_CHAR_MAX
5975 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5976#endif
5977#ifdef _SC_CHAR_MIN
5978 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5979#endif
5980#ifdef _SC_CHILD_MAX
5981 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5982#endif
5983#ifdef _SC_CLK_TCK
5984 {"SC_CLK_TCK", _SC_CLK_TCK},
5985#endif
5986#ifdef _SC_COHER_BLKSZ
5987 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5988#endif
5989#ifdef _SC_COLL_WEIGHTS_MAX
5990 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5991#endif
5992#ifdef _SC_DCACHE_ASSOC
5993 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5994#endif
5995#ifdef _SC_DCACHE_BLKSZ
5996 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5997#endif
5998#ifdef _SC_DCACHE_LINESZ
5999 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6000#endif
6001#ifdef _SC_DCACHE_SZ
6002 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6003#endif
6004#ifdef _SC_DCACHE_TBLKSZ
6005 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6006#endif
6007#ifdef _SC_DELAYTIMER_MAX
6008 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6009#endif
6010#ifdef _SC_EQUIV_CLASS_MAX
6011 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6012#endif
6013#ifdef _SC_EXPR_NEST_MAX
6014 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6015#endif
6016#ifdef _SC_FSYNC
6017 {"SC_FSYNC", _SC_FSYNC},
6018#endif
6019#ifdef _SC_GETGR_R_SIZE_MAX
6020 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6021#endif
6022#ifdef _SC_GETPW_R_SIZE_MAX
6023 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6024#endif
6025#ifdef _SC_ICACHE_ASSOC
6026 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6027#endif
6028#ifdef _SC_ICACHE_BLKSZ
6029 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6030#endif
6031#ifdef _SC_ICACHE_LINESZ
6032 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6033#endif
6034#ifdef _SC_ICACHE_SZ
6035 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6036#endif
Fred Draked86ed291999-12-15 15:34:33 +00006037#ifdef _SC_INF
6038 {"SC_INF", _SC_INF},
6039#endif
Fred Drakec9680921999-12-13 16:37:25 +00006040#ifdef _SC_INT_MAX
6041 {"SC_INT_MAX", _SC_INT_MAX},
6042#endif
6043#ifdef _SC_INT_MIN
6044 {"SC_INT_MIN", _SC_INT_MIN},
6045#endif
6046#ifdef _SC_IOV_MAX
6047 {"SC_IOV_MAX", _SC_IOV_MAX},
6048#endif
Fred Draked86ed291999-12-15 15:34:33 +00006049#ifdef _SC_IP_SECOPTS
6050 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6051#endif
Fred Drakec9680921999-12-13 16:37:25 +00006052#ifdef _SC_JOB_CONTROL
6053 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6054#endif
Fred Draked86ed291999-12-15 15:34:33 +00006055#ifdef _SC_KERN_POINTERS
6056 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6057#endif
6058#ifdef _SC_KERN_SIM
6059 {"SC_KERN_SIM", _SC_KERN_SIM},
6060#endif
Fred Drakec9680921999-12-13 16:37:25 +00006061#ifdef _SC_LINE_MAX
6062 {"SC_LINE_MAX", _SC_LINE_MAX},
6063#endif
6064#ifdef _SC_LOGIN_NAME_MAX
6065 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6066#endif
6067#ifdef _SC_LOGNAME_MAX
6068 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6069#endif
6070#ifdef _SC_LONG_BIT
6071 {"SC_LONG_BIT", _SC_LONG_BIT},
6072#endif
Fred Draked86ed291999-12-15 15:34:33 +00006073#ifdef _SC_MAC
6074 {"SC_MAC", _SC_MAC},
6075#endif
Fred Drakec9680921999-12-13 16:37:25 +00006076#ifdef _SC_MAPPED_FILES
6077 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6078#endif
6079#ifdef _SC_MAXPID
6080 {"SC_MAXPID", _SC_MAXPID},
6081#endif
6082#ifdef _SC_MB_LEN_MAX
6083 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6084#endif
6085#ifdef _SC_MEMLOCK
6086 {"SC_MEMLOCK", _SC_MEMLOCK},
6087#endif
6088#ifdef _SC_MEMLOCK_RANGE
6089 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6090#endif
6091#ifdef _SC_MEMORY_PROTECTION
6092 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6093#endif
6094#ifdef _SC_MESSAGE_PASSING
6095 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6096#endif
Fred Draked86ed291999-12-15 15:34:33 +00006097#ifdef _SC_MMAP_FIXED_ALIGNMENT
6098 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6099#endif
Fred Drakec9680921999-12-13 16:37:25 +00006100#ifdef _SC_MQ_OPEN_MAX
6101 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6102#endif
6103#ifdef _SC_MQ_PRIO_MAX
6104 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6105#endif
Fred Draked86ed291999-12-15 15:34:33 +00006106#ifdef _SC_NACLS_MAX
6107 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6108#endif
Fred Drakec9680921999-12-13 16:37:25 +00006109#ifdef _SC_NGROUPS_MAX
6110 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6111#endif
6112#ifdef _SC_NL_ARGMAX
6113 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6114#endif
6115#ifdef _SC_NL_LANGMAX
6116 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6117#endif
6118#ifdef _SC_NL_MSGMAX
6119 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6120#endif
6121#ifdef _SC_NL_NMAX
6122 {"SC_NL_NMAX", _SC_NL_NMAX},
6123#endif
6124#ifdef _SC_NL_SETMAX
6125 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6126#endif
6127#ifdef _SC_NL_TEXTMAX
6128 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6129#endif
6130#ifdef _SC_NPROCESSORS_CONF
6131 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6132#endif
6133#ifdef _SC_NPROCESSORS_ONLN
6134 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6135#endif
Fred Draked86ed291999-12-15 15:34:33 +00006136#ifdef _SC_NPROC_CONF
6137 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6138#endif
6139#ifdef _SC_NPROC_ONLN
6140 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6141#endif
Fred Drakec9680921999-12-13 16:37:25 +00006142#ifdef _SC_NZERO
6143 {"SC_NZERO", _SC_NZERO},
6144#endif
6145#ifdef _SC_OPEN_MAX
6146 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6147#endif
6148#ifdef _SC_PAGESIZE
6149 {"SC_PAGESIZE", _SC_PAGESIZE},
6150#endif
6151#ifdef _SC_PAGE_SIZE
6152 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6153#endif
6154#ifdef _SC_PASS_MAX
6155 {"SC_PASS_MAX", _SC_PASS_MAX},
6156#endif
6157#ifdef _SC_PHYS_PAGES
6158 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6159#endif
6160#ifdef _SC_PII
6161 {"SC_PII", _SC_PII},
6162#endif
6163#ifdef _SC_PII_INTERNET
6164 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6165#endif
6166#ifdef _SC_PII_INTERNET_DGRAM
6167 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6168#endif
6169#ifdef _SC_PII_INTERNET_STREAM
6170 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6171#endif
6172#ifdef _SC_PII_OSI
6173 {"SC_PII_OSI", _SC_PII_OSI},
6174#endif
6175#ifdef _SC_PII_OSI_CLTS
6176 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6177#endif
6178#ifdef _SC_PII_OSI_COTS
6179 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6180#endif
6181#ifdef _SC_PII_OSI_M
6182 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6183#endif
6184#ifdef _SC_PII_SOCKET
6185 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6186#endif
6187#ifdef _SC_PII_XTI
6188 {"SC_PII_XTI", _SC_PII_XTI},
6189#endif
6190#ifdef _SC_POLL
6191 {"SC_POLL", _SC_POLL},
6192#endif
6193#ifdef _SC_PRIORITIZED_IO
6194 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6195#endif
6196#ifdef _SC_PRIORITY_SCHEDULING
6197 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6198#endif
6199#ifdef _SC_REALTIME_SIGNALS
6200 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6201#endif
6202#ifdef _SC_RE_DUP_MAX
6203 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6204#endif
6205#ifdef _SC_RTSIG_MAX
6206 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6207#endif
6208#ifdef _SC_SAVED_IDS
6209 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6210#endif
6211#ifdef _SC_SCHAR_MAX
6212 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6213#endif
6214#ifdef _SC_SCHAR_MIN
6215 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6216#endif
6217#ifdef _SC_SELECT
6218 {"SC_SELECT", _SC_SELECT},
6219#endif
6220#ifdef _SC_SEMAPHORES
6221 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6222#endif
6223#ifdef _SC_SEM_NSEMS_MAX
6224 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6225#endif
6226#ifdef _SC_SEM_VALUE_MAX
6227 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6228#endif
6229#ifdef _SC_SHARED_MEMORY_OBJECTS
6230 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6231#endif
6232#ifdef _SC_SHRT_MAX
6233 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6234#endif
6235#ifdef _SC_SHRT_MIN
6236 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6237#endif
6238#ifdef _SC_SIGQUEUE_MAX
6239 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6240#endif
6241#ifdef _SC_SIGRT_MAX
6242 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6243#endif
6244#ifdef _SC_SIGRT_MIN
6245 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6246#endif
Fred Draked86ed291999-12-15 15:34:33 +00006247#ifdef _SC_SOFTPOWER
6248 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6249#endif
Fred Drakec9680921999-12-13 16:37:25 +00006250#ifdef _SC_SPLIT_CACHE
6251 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6252#endif
6253#ifdef _SC_SSIZE_MAX
6254 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6255#endif
6256#ifdef _SC_STACK_PROT
6257 {"SC_STACK_PROT", _SC_STACK_PROT},
6258#endif
6259#ifdef _SC_STREAM_MAX
6260 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6261#endif
6262#ifdef _SC_SYNCHRONIZED_IO
6263 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6264#endif
6265#ifdef _SC_THREADS
6266 {"SC_THREADS", _SC_THREADS},
6267#endif
6268#ifdef _SC_THREAD_ATTR_STACKADDR
6269 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6270#endif
6271#ifdef _SC_THREAD_ATTR_STACKSIZE
6272 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6273#endif
6274#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6275 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6276#endif
6277#ifdef _SC_THREAD_KEYS_MAX
6278 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6279#endif
6280#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6281 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6282#endif
6283#ifdef _SC_THREAD_PRIO_INHERIT
6284 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6285#endif
6286#ifdef _SC_THREAD_PRIO_PROTECT
6287 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6288#endif
6289#ifdef _SC_THREAD_PROCESS_SHARED
6290 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6291#endif
6292#ifdef _SC_THREAD_SAFE_FUNCTIONS
6293 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6294#endif
6295#ifdef _SC_THREAD_STACK_MIN
6296 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6297#endif
6298#ifdef _SC_THREAD_THREADS_MAX
6299 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6300#endif
6301#ifdef _SC_TIMERS
6302 {"SC_TIMERS", _SC_TIMERS},
6303#endif
6304#ifdef _SC_TIMER_MAX
6305 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6306#endif
6307#ifdef _SC_TTY_NAME_MAX
6308 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6309#endif
6310#ifdef _SC_TZNAME_MAX
6311 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6312#endif
6313#ifdef _SC_T_IOV_MAX
6314 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6315#endif
6316#ifdef _SC_UCHAR_MAX
6317 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6318#endif
6319#ifdef _SC_UINT_MAX
6320 {"SC_UINT_MAX", _SC_UINT_MAX},
6321#endif
6322#ifdef _SC_UIO_MAXIOV
6323 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6324#endif
6325#ifdef _SC_ULONG_MAX
6326 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6327#endif
6328#ifdef _SC_USHRT_MAX
6329 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6330#endif
6331#ifdef _SC_VERSION
6332 {"SC_VERSION", _SC_VERSION},
6333#endif
6334#ifdef _SC_WORD_BIT
6335 {"SC_WORD_BIT", _SC_WORD_BIT},
6336#endif
6337#ifdef _SC_XBS5_ILP32_OFF32
6338 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6339#endif
6340#ifdef _SC_XBS5_ILP32_OFFBIG
6341 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6342#endif
6343#ifdef _SC_XBS5_LP64_OFF64
6344 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6345#endif
6346#ifdef _SC_XBS5_LPBIG_OFFBIG
6347 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6348#endif
6349#ifdef _SC_XOPEN_CRYPT
6350 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6351#endif
6352#ifdef _SC_XOPEN_ENH_I18N
6353 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6354#endif
6355#ifdef _SC_XOPEN_LEGACY
6356 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6357#endif
6358#ifdef _SC_XOPEN_REALTIME
6359 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6360#endif
6361#ifdef _SC_XOPEN_REALTIME_THREADS
6362 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6363#endif
6364#ifdef _SC_XOPEN_SHM
6365 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6366#endif
6367#ifdef _SC_XOPEN_UNIX
6368 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6369#endif
6370#ifdef _SC_XOPEN_VERSION
6371 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6372#endif
6373#ifdef _SC_XOPEN_XCU_VERSION
6374 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6375#endif
6376#ifdef _SC_XOPEN_XPG2
6377 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6378#endif
6379#ifdef _SC_XOPEN_XPG3
6380 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6381#endif
6382#ifdef _SC_XOPEN_XPG4
6383 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6384#endif
6385};
6386
6387static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006388conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006389{
6390 return conv_confname(arg, valuep, posix_constants_sysconf,
6391 sizeof(posix_constants_sysconf)
6392 / sizeof(struct constdef));
6393}
6394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006395PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006396"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006397Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006398
6399static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006400posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006401{
6402 PyObject *result = NULL;
6403 int name;
6404
6405 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6406 int value;
6407
6408 errno = 0;
6409 value = sysconf(name);
6410 if (value == -1 && errno != 0)
6411 posix_error();
6412 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006413 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006414 }
6415 return result;
6416}
6417#endif
6418
6419
Fred Drakebec628d1999-12-15 18:31:10 +00006420/* This code is used to ensure that the tables of configuration value names
6421 * are in sorted order as required by conv_confname(), and also to build the
6422 * the exported dictionaries that are used to publish information about the
6423 * names available on the host platform.
6424 *
6425 * Sorting the table at runtime ensures that the table is properly ordered
6426 * when used, even for platforms we're not able to test on. It also makes
6427 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006428 */
Fred Drakebec628d1999-12-15 18:31:10 +00006429
6430static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006431cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006432{
6433 const struct constdef *c1 =
6434 (const struct constdef *) v1;
6435 const struct constdef *c2 =
6436 (const struct constdef *) v2;
6437
6438 return strcmp(c1->name, c2->name);
6439}
6440
6441static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006442setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006443 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006444{
Fred Drakebec628d1999-12-15 18:31:10 +00006445 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006446 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006447
6448 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6449 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006450 if (d == NULL)
6451 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006452
Barry Warsaw3155db32000-04-13 15:20:40 +00006453 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006454 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006455 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6456 Py_XDECREF(o);
6457 Py_DECREF(d);
6458 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006459 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006460 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006461 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006462 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006463}
6464
Fred Drakebec628d1999-12-15 18:31:10 +00006465/* Return -1 on failure, 0 on success. */
6466static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006467setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006468{
6469#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006470 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006471 sizeof(posix_constants_pathconf)
6472 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006473 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006474 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006475#endif
6476#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006477 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006478 sizeof(posix_constants_confstr)
6479 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006480 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006481 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006482#endif
6483#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006484 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006485 sizeof(posix_constants_sysconf)
6486 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006487 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006488 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006489#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006490 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006491}
Fred Draked86ed291999-12-15 15:34:33 +00006492
6493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006494PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006495"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006496Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006497in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006498
6499static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006500posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006501{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006502 abort();
6503 /*NOTREACHED*/
6504 Py_FatalError("abort() called from Python code didn't abort!");
6505 return NULL;
6506}
Fred Drakebec628d1999-12-15 18:31:10 +00006507
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006508#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006509PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006510"startfile(filepath [, operation]) - Start a file with its associated\n\
6511application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006512\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006513When \"operation\" is not specified or \"open\", this acts like\n\
6514double-clicking the file in Explorer, or giving the file name as an\n\
6515argument to the DOS \"start\" command: the file is opened with whatever\n\
6516application (if any) its extension is associated.\n\
6517When another \"operation\" is given, it specifies what should be done with\n\
6518the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006519\n\
6520startfile returns as soon as the associated application is launched.\n\
6521There is no option to wait for the application to close, and no way\n\
6522to retrieve the application's exit status.\n\
6523\n\
6524The filepath is relative to the current directory. If you want to use\n\
6525an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006526the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006527
6528static PyObject *
6529win32_startfile(PyObject *self, PyObject *args)
6530{
6531 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006532 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006533 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006534#ifdef Py_WIN_WIDE_FILENAMES
6535 if (unicode_file_names()) {
6536 PyObject *unipath, *woperation = NULL;
6537 if (!PyArg_ParseTuple(args, "U|s:startfile",
6538 &unipath, &operation)) {
6539 PyErr_Clear();
6540 goto normal;
6541 }
6542
6543
6544 if (operation) {
6545 woperation = PyUnicode_DecodeASCII(operation,
6546 strlen(operation), NULL);
6547 if (!woperation) {
6548 PyErr_Clear();
6549 operation = NULL;
6550 goto normal;
6551 }
6552 }
6553
6554 Py_BEGIN_ALLOW_THREADS
6555 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6556 PyUnicode_AS_UNICODE(unipath),
6557 NULL, NULL, SW_SHOWNORMAL);
6558 Py_END_ALLOW_THREADS
6559
6560 Py_XDECREF(woperation);
6561 if (rc <= (HINSTANCE)32) {
6562 PyObject *errval = win32_error_unicode("startfile",
6563 PyUnicode_AS_UNICODE(unipath));
6564 return errval;
6565 }
6566 Py_INCREF(Py_None);
6567 return Py_None;
6568 }
6569#endif
6570
6571normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006572 if (!PyArg_ParseTuple(args, "et|s:startfile",
6573 Py_FileSystemDefaultEncoding, &filepath,
6574 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006575 return NULL;
6576 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006577 rc = ShellExecute((HWND)0, operation, filepath,
6578 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006579 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006580 if (rc <= (HINSTANCE)32) {
6581 PyObject *errval = win32_error("startfile", filepath);
6582 PyMem_Free(filepath);
6583 return errval;
6584 }
6585 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006586 Py_INCREF(Py_None);
6587 return Py_None;
6588}
6589#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006590
Martin v. Löwis438b5342002-12-27 10:16:42 +00006591#ifdef HAVE_GETLOADAVG
6592PyDoc_STRVAR(posix_getloadavg__doc__,
6593"getloadavg() -> (float, float, float)\n\n\
6594Return the number of processes in the system run queue averaged over\n\
6595the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6596was unobtainable");
6597
6598static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006599posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006600{
6601 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006602 if (getloadavg(loadavg, 3)!=3) {
6603 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6604 return NULL;
6605 } else
6606 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6607}
6608#endif
6609
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006610#ifdef MS_WINDOWS
6611
6612PyDoc_STRVAR(win32_urandom__doc__,
6613"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006614Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006615
6616typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6617 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6618 DWORD dwFlags );
6619typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6620 BYTE *pbBuffer );
6621
6622static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006623/* This handle is never explicitly released. Instead, the operating
6624 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006625static HCRYPTPROV hCryptProv = 0;
6626
Tim Peters4ad82172004-08-30 17:02:04 +00006627static PyObject*
6628win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006629{
Tim Petersd3115382004-08-30 17:36:46 +00006630 int howMany;
6631 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006632
Tim Peters4ad82172004-08-30 17:02:04 +00006633 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006634 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006635 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006636 if (howMany < 0)
6637 return PyErr_Format(PyExc_ValueError,
6638 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006639
Tim Peters4ad82172004-08-30 17:02:04 +00006640 if (hCryptProv == 0) {
6641 HINSTANCE hAdvAPI32 = NULL;
6642 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006643
Tim Peters4ad82172004-08-30 17:02:04 +00006644 /* Obtain handle to the DLL containing CryptoAPI
6645 This should not fail */
6646 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6647 if(hAdvAPI32 == NULL)
6648 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006649
Tim Peters4ad82172004-08-30 17:02:04 +00006650 /* Obtain pointers to the CryptoAPI functions
6651 This will fail on some early versions of Win95 */
6652 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6653 hAdvAPI32,
6654 "CryptAcquireContextA");
6655 if (pCryptAcquireContext == NULL)
6656 return PyErr_Format(PyExc_NotImplementedError,
6657 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006658
Tim Peters4ad82172004-08-30 17:02:04 +00006659 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6660 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006661 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006662 return PyErr_Format(PyExc_NotImplementedError,
6663 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006664
Tim Peters4ad82172004-08-30 17:02:04 +00006665 /* Acquire context */
6666 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6667 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6668 return win32_error("CryptAcquireContext", NULL);
6669 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006670
Tim Peters4ad82172004-08-30 17:02:04 +00006671 /* Allocate bytes */
Guido van Rossum18980842007-11-21 21:53:11 +00006672 result = PyString_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006673 if (result != NULL) {
6674 /* Get random data */
6675 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Guido van Rossum18980842007-11-21 21:53:11 +00006676 PyString_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006677 Py_DECREF(result);
6678 return win32_error("CryptGenRandom", NULL);
6679 }
Tim Peters4ad82172004-08-30 17:02:04 +00006680 }
Tim Petersd3115382004-08-30 17:36:46 +00006681 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006682}
6683#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006684
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006685PyDoc_STRVAR(device_encoding__doc__,
6686"device_encoding(fd) -> str\n\n\
6687Return a string describing the encoding of the device\n\
6688if the output is a terminal; else return None.");
6689
6690static PyObject *
6691device_encoding(PyObject *self, PyObject *args)
6692{
6693 int fd;
6694 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6695 return NULL;
6696 if (!isatty(fd)) {
6697 Py_INCREF(Py_None);
6698 return Py_None;
6699 }
6700#if defined(MS_WINDOWS) || defined(MS_WIN64)
6701 if (fd == 0) {
6702 char buf[100];
6703 sprintf(buf, "cp%d", GetConsoleCP());
6704 return PyUnicode_FromString(buf);
6705 }
6706 if (fd == 1 || fd == 2) {
6707 char buf[100];
6708 sprintf(buf, "cp%d", GetConsoleOutputCP());
6709 return PyUnicode_FromString(buf);
6710 }
6711#elif defined(CODESET)
6712 {
6713 char *codeset = nl_langinfo(CODESET);
6714 if (codeset)
6715 return PyUnicode_FromString(codeset);
6716 }
6717#endif
6718 Py_INCREF(Py_None);
6719 return Py_None;
6720}
6721
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006722#ifdef __VMS
6723/* Use openssl random routine */
6724#include <openssl/rand.h>
6725PyDoc_STRVAR(vms_urandom__doc__,
6726"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006727Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006728
6729static PyObject*
6730vms_urandom(PyObject *self, PyObject *args)
6731{
6732 int howMany;
6733 PyObject* result;
6734
6735 /* Read arguments */
6736 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6737 return NULL;
6738 if (howMany < 0)
6739 return PyErr_Format(PyExc_ValueError,
6740 "negative argument not allowed");
6741
6742 /* Allocate bytes */
Guido van Rossum18980842007-11-21 21:53:11 +00006743 result = PyString_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006744 if (result != NULL) {
6745 /* Get random data */
6746 if (RAND_pseudo_bytes((unsigned char*)
Guido van Rossum18980842007-11-21 21:53:11 +00006747 PyString_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006748 howMany) < 0) {
6749 Py_DECREF(result);
6750 return PyErr_Format(PyExc_ValueError,
6751 "RAND_pseudo_bytes");
6752 }
6753 }
6754 return result;
6755}
6756#endif
6757
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006758static PyMethodDef posix_methods[] = {
6759 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6760#ifdef HAVE_TTYNAME
6761 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6762#endif
6763 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006764#ifdef HAVE_CHFLAGS
6765 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6766#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006767 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00006768#ifdef HAVE_FCHMOD
6769 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6770#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006771#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006772 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006773#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00006774#ifdef HAVE_LCHMOD
6775 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6776#endif /* HAVE_LCHMOD */
6777#ifdef HAVE_FCHOWN
6778 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6779#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006780#ifdef HAVE_LCHFLAGS
6781 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6782#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006783#ifdef HAVE_LCHOWN
6784 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6785#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006786#ifdef HAVE_CHROOT
6787 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6788#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006789#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006790 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006791#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006792#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006793 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006794 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006795#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006796#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006797 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006798#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006799 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6800 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6801 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006802#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006803 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006804#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006805#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006806 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006807#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006808 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6809 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6810 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006811 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006812#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006813 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006814#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006815#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006816 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006817#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006818 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006819#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006820 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006821#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006822 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6823 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6824 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006825#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006826 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006827#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006828 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006829#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006830 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6831 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006832#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006833#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006834 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6835 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006836#if defined(PYOS_OS2)
6837 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6838 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6839#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006840#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006841#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006842 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006843#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006844#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006845 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006846#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006847#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006848 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006849#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006850#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006851 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006852#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006853#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006854 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006855#endif /* HAVE_GETEGID */
6856#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006857 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006858#endif /* HAVE_GETEUID */
6859#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006860 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006861#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006862#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006863 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006864#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006865 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006866#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006867 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006868#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006869#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006870 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006871#endif /* HAVE_GETPPID */
6872#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006873 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006874#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006875#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006876 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006877#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006878#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006879 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006880#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006881#ifdef HAVE_KILLPG
6882 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6883#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006884#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006885 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006886#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006887#ifdef MS_WINDOWS
6888 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6889#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006890#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006891 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006892#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006893#ifdef HAVE_SETEUID
6894 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6895#endif /* HAVE_SETEUID */
6896#ifdef HAVE_SETEGID
6897 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6898#endif /* HAVE_SETEGID */
6899#ifdef HAVE_SETREUID
6900 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6901#endif /* HAVE_SETREUID */
6902#ifdef HAVE_SETREGID
6903 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6904#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006905#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006906 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006907#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006908#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006909 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006910#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006911#ifdef HAVE_GETPGID
6912 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6913#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006914#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006915 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006916#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006917#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006918 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006919#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006920#ifdef HAVE_WAIT3
6921 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6922#endif /* HAVE_WAIT3 */
6923#ifdef HAVE_WAIT4
6924 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6925#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006926#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006927 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006928#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006929#ifdef HAVE_GETSID
6930 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6931#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006932#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006933 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006934#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006935#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006936 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006937#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006938#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006939 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006940#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006941#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006942 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006943#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006944 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6945 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00006946 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006947 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006948 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6949 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6950 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6951 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6952 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6953 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006954 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006955#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006956 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006957#endif
6958#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006959 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006960#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006961#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006962 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6963#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006964#ifdef HAVE_DEVICE_MACROS
6965 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6966 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6967 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6968#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006969#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006970 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006971#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006972#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006973 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006974#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006975#ifdef HAVE_UNSETENV
6976 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6977#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006978 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00006979#ifdef HAVE_FCHDIR
6980 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6981#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006982#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006983 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006984#endif
6985#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006986 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006987#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006988#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006989#ifdef WCOREDUMP
6990 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6991#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006992#ifdef WIFCONTINUED
6993 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6994#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006995#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006996 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006997#endif /* WIFSTOPPED */
6998#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006999 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007000#endif /* WIFSIGNALED */
7001#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007002 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007003#endif /* WIFEXITED */
7004#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007005 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007006#endif /* WEXITSTATUS */
7007#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007008 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007009#endif /* WTERMSIG */
7010#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007011 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007012#endif /* WSTOPSIG */
7013#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007014#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007015 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007016#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007017#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007018 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007019#endif
Fred Drakec9680921999-12-13 16:37:25 +00007020#ifdef HAVE_CONFSTR
7021 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7022#endif
7023#ifdef HAVE_SYSCONF
7024 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7025#endif
7026#ifdef HAVE_FPATHCONF
7027 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7028#endif
7029#ifdef HAVE_PATHCONF
7030 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7031#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007032 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007033#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007034 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7035#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007036#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007037 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007038#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007039 #ifdef MS_WINDOWS
7040 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7041 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007042 #ifdef __VMS
7043 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7044 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007045 {NULL, NULL} /* Sentinel */
7046};
7047
7048
Barry Warsaw4a342091996-12-19 23:50:02 +00007049static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007050ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007051{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007052 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007053}
7054
Guido van Rossumd48f2521997-12-05 22:19:34 +00007055#if defined(PYOS_OS2)
7056/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007057static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007058{
7059 APIRET rc;
7060 ULONG values[QSV_MAX+1];
7061 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007062 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007063
7064 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007065 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007066 Py_END_ALLOW_THREADS
7067
7068 if (rc != NO_ERROR) {
7069 os2_error(rc);
7070 return -1;
7071 }
7072
Fred Drake4d1e64b2002-04-15 19:40:07 +00007073 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7074 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7075 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7076 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7077 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7078 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7079 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007080
7081 switch (values[QSV_VERSION_MINOR]) {
7082 case 0: ver = "2.00"; break;
7083 case 10: ver = "2.10"; break;
7084 case 11: ver = "2.11"; break;
7085 case 30: ver = "3.00"; break;
7086 case 40: ver = "4.00"; break;
7087 case 50: ver = "5.00"; break;
7088 default:
Tim Peters885d4572001-11-28 20:27:42 +00007089 PyOS_snprintf(tmp, sizeof(tmp),
7090 "%d-%d", values[QSV_VERSION_MAJOR],
7091 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007092 ver = &tmp[0];
7093 }
7094
7095 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007096 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007097 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007098
7099 /* Add Indicator of Which Drive was Used to Boot the System */
7100 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7101 tmp[1] = ':';
7102 tmp[2] = '\0';
7103
Fred Drake4d1e64b2002-04-15 19:40:07 +00007104 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007105}
7106#endif
7107
Barry Warsaw4a342091996-12-19 23:50:02 +00007108static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007109all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007110{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007111#ifdef F_OK
7112 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007113#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007114#ifdef R_OK
7115 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007116#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007117#ifdef W_OK
7118 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007119#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007120#ifdef X_OK
7121 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007122#endif
Fred Drakec9680921999-12-13 16:37:25 +00007123#ifdef NGROUPS_MAX
7124 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7125#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007126#ifdef TMP_MAX
7127 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7128#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007129#ifdef WCONTINUED
7130 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7131#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007132#ifdef WNOHANG
7133 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007134#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007135#ifdef WUNTRACED
7136 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7137#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007138#ifdef O_RDONLY
7139 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7140#endif
7141#ifdef O_WRONLY
7142 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7143#endif
7144#ifdef O_RDWR
7145 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7146#endif
7147#ifdef O_NDELAY
7148 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7149#endif
7150#ifdef O_NONBLOCK
7151 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7152#endif
7153#ifdef O_APPEND
7154 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7155#endif
7156#ifdef O_DSYNC
7157 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7158#endif
7159#ifdef O_RSYNC
7160 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7161#endif
7162#ifdef O_SYNC
7163 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7164#endif
7165#ifdef O_NOCTTY
7166 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7167#endif
7168#ifdef O_CREAT
7169 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7170#endif
7171#ifdef O_EXCL
7172 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7173#endif
7174#ifdef O_TRUNC
7175 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7176#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007177#ifdef O_BINARY
7178 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7179#endif
7180#ifdef O_TEXT
7181 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7182#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007183#ifdef O_LARGEFILE
7184 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7185#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007186#ifdef O_SHLOCK
7187 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7188#endif
7189#ifdef O_EXLOCK
7190 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7191#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007192
Tim Peters5aa91602002-01-30 05:46:57 +00007193/* MS Windows */
7194#ifdef O_NOINHERIT
7195 /* Don't inherit in child processes. */
7196 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7197#endif
7198#ifdef _O_SHORT_LIVED
7199 /* Optimize for short life (keep in memory). */
7200 /* MS forgot to define this one with a non-underscore form too. */
7201 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7202#endif
7203#ifdef O_TEMPORARY
7204 /* Automatically delete when last handle is closed. */
7205 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7206#endif
7207#ifdef O_RANDOM
7208 /* Optimize for random access. */
7209 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7210#endif
7211#ifdef O_SEQUENTIAL
7212 /* Optimize for sequential access. */
7213 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7214#endif
7215
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007216/* GNU extensions. */
7217#ifdef O_DIRECT
7218 /* Direct disk access. */
7219 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7220#endif
7221#ifdef O_DIRECTORY
7222 /* Must be a directory. */
7223 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7224#endif
7225#ifdef O_NOFOLLOW
7226 /* Do not follow links. */
7227 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7228#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007229#ifdef O_NOATIME
7230 /* Do not update the access time. */
7231 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7232#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007233
Barry Warsaw5676bd12003-01-07 20:57:09 +00007234 /* These come from sysexits.h */
7235#ifdef EX_OK
7236 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007237#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007238#ifdef EX_USAGE
7239 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007240#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007241#ifdef EX_DATAERR
7242 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007243#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007244#ifdef EX_NOINPUT
7245 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007246#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007247#ifdef EX_NOUSER
7248 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007249#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007250#ifdef EX_NOHOST
7251 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007252#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007253#ifdef EX_UNAVAILABLE
7254 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007255#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007256#ifdef EX_SOFTWARE
7257 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007258#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007259#ifdef EX_OSERR
7260 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007261#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007262#ifdef EX_OSFILE
7263 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007264#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007265#ifdef EX_CANTCREAT
7266 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007267#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007268#ifdef EX_IOERR
7269 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007270#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007271#ifdef EX_TEMPFAIL
7272 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007273#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007274#ifdef EX_PROTOCOL
7275 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007276#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007277#ifdef EX_NOPERM
7278 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007279#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007280#ifdef EX_CONFIG
7281 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007282#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007283#ifdef EX_NOTFOUND
7284 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007285#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007286
Guido van Rossum246bc171999-02-01 23:54:31 +00007287#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007288#if defined(PYOS_OS2) && defined(PYCC_GCC)
7289 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7290 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7291 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7292 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7293 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7294 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7295 if (ins(d, "P_PM", (long)P_PM)) return -1;
7296 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7297 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7298 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7299 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7300 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7301 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7302 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7303 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7304 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7305 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7306 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7307 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7308 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7309#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007310 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7311 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7312 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7313 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7314 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007315#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007316#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007317
Guido van Rossumd48f2521997-12-05 22:19:34 +00007318#if defined(PYOS_OS2)
7319 if (insertvalues(d)) return -1;
7320#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007321 return 0;
7322}
7323
7324
Tim Peters5aa91602002-01-30 05:46:57 +00007325#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007326#define INITFUNC initnt
7327#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007328
7329#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007330#define INITFUNC initos2
7331#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007332
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007333#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007334#define INITFUNC initposix
7335#define MODNAME "posix"
7336#endif
7337
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007338PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007339INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007340{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007341 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007342
Fred Drake4d1e64b2002-04-15 19:40:07 +00007343 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007344 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007345 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007346 if (m == NULL)
7347 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007348
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007349 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007350 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007351 Py_XINCREF(v);
7352 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007353 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007354 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007355
Fred Drake4d1e64b2002-04-15 19:40:07 +00007356 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007357 return;
7358
Fred Drake4d1e64b2002-04-15 19:40:07 +00007359 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007360 return;
7361
Fred Drake4d1e64b2002-04-15 19:40:07 +00007362 Py_INCREF(PyExc_OSError);
7363 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007364
Guido van Rossumb3d39562000-01-31 18:41:26 +00007365#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007366 if (posix_putenv_garbage == NULL)
7367 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007368#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007369
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007370 if (!initialized) {
7371 stat_result_desc.name = MODNAME ".stat_result";
7372 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7373 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7374 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7375 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7376 structseq_new = StatResultType.tp_new;
7377 StatResultType.tp_new = statresult_new;
7378
7379 statvfs_result_desc.name = MODNAME ".statvfs_result";
7380 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7381 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007382 Py_INCREF((PyObject*) &StatResultType);
7383 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007384 Py_INCREF((PyObject*) &StatVFSResultType);
7385 PyModule_AddObject(m, "statvfs_result",
7386 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007387 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007388
7389#ifdef __APPLE__
7390 /*
7391 * Step 2 of weak-linking support on Mac OS X.
7392 *
7393 * The code below removes functions that are not available on the
7394 * currently active platform.
7395 *
7396 * This block allow one to use a python binary that was build on
7397 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7398 * OSX 10.4.
7399 */
7400#ifdef HAVE_FSTATVFS
7401 if (fstatvfs == NULL) {
7402 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7403 return;
7404 }
7405 }
7406#endif /* HAVE_FSTATVFS */
7407
7408#ifdef HAVE_STATVFS
7409 if (statvfs == NULL) {
7410 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7411 return;
7412 }
7413 }
7414#endif /* HAVE_STATVFS */
7415
7416# ifdef HAVE_LCHOWN
7417 if (lchown == NULL) {
7418 if (PyObject_DelAttrString(m, "lchown") == -1) {
7419 return;
7420 }
7421 }
7422#endif /* HAVE_LCHOWN */
7423
7424
7425#endif /* __APPLE__ */
7426
Guido van Rossumb6775db1994-08-01 11:34:53 +00007427}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007428
7429#ifdef __cplusplus
7430}
7431#endif