blob: 490999386abac60a00bc54abef10f0d417713de3 [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') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000428 PyObject *v = PyBytes_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') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000434 PyObject *v = PyBytes_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.
Georg Brandl38feaf02008-05-25 07:45:51 +0000479 (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
Thomas Wouters477c8d52006-05-27 19:21:47 +0000502static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000503convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000505 if (PyUnicode_CheckExact(*param))
506 Py_INCREF(*param);
507 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000508 /* For a Unicode subtype that's not a Unicode object,
509 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000510 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
511 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000513 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000514 Py_FileSystemDefaultEncoding,
515 "strict");
516 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000517}
518
519#endif /* Py_WIN_WIDE_FILENAMES */
520
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000521#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522
Guido van Rossumd48f2521997-12-05 22:19:34 +0000523#if defined(PYOS_OS2)
524/**********************************************************************
525 * Helper Function to Trim and Format OS/2 Messages
526 **********************************************************************/
527 static void
528os2_formatmsg(char *msgbuf, int msglen, char *reason)
529{
530 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
531
532 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
533 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
534
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000535 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000536 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
537 }
538
539 /* Add Optional Reason Text */
540 if (reason) {
541 strcat(msgbuf, " : ");
542 strcat(msgbuf, reason);
543 }
544}
545
546/**********************************************************************
547 * Decode an OS/2 Operating System Error Code
548 *
549 * A convenience function to lookup an OS/2 error code and return a
550 * text message we can use to raise a Python exception.
551 *
552 * Notes:
553 * The messages for errors returned from the OS/2 kernel reside in
554 * the file OSO001.MSG in the \OS2 directory hierarchy.
555 *
556 **********************************************************************/
557 static char *
558os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
559{
560 APIRET rc;
561 ULONG msglen;
562
563 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
564 Py_BEGIN_ALLOW_THREADS
565 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
566 errorcode, "oso001.msg", &msglen);
567 Py_END_ALLOW_THREADS
568
569 if (rc == NO_ERROR)
570 os2_formatmsg(msgbuf, msglen, reason);
571 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000572 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000573 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000574
575 return msgbuf;
576}
577
578/* Set an OS/2-specific error and return NULL. OS/2 kernel
579 errors are not in a global variable e.g. 'errno' nor are
580 they congruent with posix error numbers. */
581
582static PyObject * os2_error(int code)
583{
584 char text[1024];
585 PyObject *v;
586
587 os2_strerror(text, sizeof(text), code, "");
588
589 v = Py_BuildValue("(is)", code, text);
590 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000591 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000592 Py_DECREF(v);
593 }
594 return NULL; /* Signal to Python that an Exception is Pending */
595}
596
597#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598
599/* POSIX generic methods */
600
Barry Warsaw53699e91996-12-10 23:23:01 +0000601static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000602posix_fildes(PyObject *fdobj, int (*func)(int))
603{
604 int fd;
605 int res;
606 fd = PyObject_AsFileDescriptor(fdobj);
607 if (fd < 0)
608 return NULL;
609 Py_BEGIN_ALLOW_THREADS
610 res = (*func)(fd);
611 Py_END_ALLOW_THREADS
612 if (res < 0)
613 return posix_error();
614 Py_INCREF(Py_None);
615 return Py_None;
616}
Guido van Rossum21142a01999-01-08 21:05:37 +0000617
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000618#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000619static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000620unicode_file_names(void)
621{
622 static int canusewide = -1;
623 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000624 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000625 the Windows NT family. */
626 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
627 }
628 return canusewide;
629}
630#endif
Tim Peters11b23062003-04-23 02:39:17 +0000631
Guido van Rossum21142a01999-01-08 21:05:37 +0000632static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000634{
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000636 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000637 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000638 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000641 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000642 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000643 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000644 return posix_error_with_allocated_filename(path1);
645 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000646 Py_INCREF(Py_None);
647 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648}
649
Barry Warsaw53699e91996-12-10 23:23:01 +0000650static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000651posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000652 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654{
Mark Hammondef8b6542001-05-13 08:04:26 +0000655 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000656 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000657 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000658 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000659 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000661 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000662 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000663 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000664 PyMem_Free(path1);
665 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000666 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000667 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_INCREF(Py_None);
670 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671}
672
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673#ifdef Py_WIN_WIDE_FILENAMES
674static PyObject*
675win32_1str(PyObject* args, char* func,
676 char* format, BOOL (__stdcall *funcA)(LPCSTR),
677 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
678{
679 PyObject *uni;
680 char *ansi;
681 BOOL result;
682 if (unicode_file_names()) {
683 if (!PyArg_ParseTuple(args, wformat, &uni))
684 PyErr_Clear();
685 else {
686 Py_BEGIN_ALLOW_THREADS
687 result = funcW(PyUnicode_AsUnicode(uni));
688 Py_END_ALLOW_THREADS
689 if (!result)
690 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
691 Py_INCREF(Py_None);
692 return Py_None;
693 }
694 }
695 if (!PyArg_ParseTuple(args, format, &ansi))
696 return NULL;
697 Py_BEGIN_ALLOW_THREADS
698 result = funcA(ansi);
699 Py_END_ALLOW_THREADS
700 if (!result)
701 return win32_error(func, ansi);
702 Py_INCREF(Py_None);
703 return Py_None;
704
705}
706
707/* This is a reimplementation of the C library's chdir function,
708 but one that produces Win32 errors instead of DOS error codes.
709 chdir is essentially a wrapper around SetCurrentDirectory; however,
710 it also needs to set "magic" environment variables indicating
711 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000712static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713win32_chdir(LPCSTR path)
714{
715 char new_path[MAX_PATH+1];
716 int result;
717 char env[4] = "=x:";
718
719 if(!SetCurrentDirectoryA(path))
720 return FALSE;
721 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
722 if (!result)
723 return FALSE;
724 /* In the ANSI API, there should not be any paths longer
725 than MAX_PATH. */
726 assert(result <= MAX_PATH+1);
727 if (strncmp(new_path, "\\\\", 2) == 0 ||
728 strncmp(new_path, "//", 2) == 0)
729 /* UNC path, nothing to do. */
730 return TRUE;
731 env[1] = new_path[0];
732 return SetEnvironmentVariableA(env, new_path);
733}
734
735/* The Unicode version differs from the ANSI version
736 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000737static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738win32_wchdir(LPCWSTR path)
739{
740 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
741 int result;
742 wchar_t env[4] = L"=x:";
743
744 if(!SetCurrentDirectoryW(path))
745 return FALSE;
746 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
747 if (!result)
748 return FALSE;
749 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000750 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751 if (!new_path) {
752 SetLastError(ERROR_OUTOFMEMORY);
753 return FALSE;
754 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000755 result = GetCurrentDirectoryW(result, new_path);
756 if (!result) {
757 free(new_path);
758 return FALSE;
759 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760 }
761 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
762 wcsncmp(new_path, L"//", 2) == 0)
763 /* UNC path, nothing to do. */
764 return TRUE;
765 env[1] = new_path[0];
766 result = SetEnvironmentVariableW(env, new_path);
767 if (new_path != _new_path)
768 free(new_path);
769 return result;
770}
771#endif
772
Martin v. Löwis14694662006-02-03 12:54:16 +0000773#ifdef MS_WINDOWS
774/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
775 - time stamps are restricted to second resolution
776 - file modification times suffer from forth-and-back conversions between
777 UTC and local time
778 Therefore, we implement our own stat, based on the Win32 API directly.
779*/
780#define HAVE_STAT_NSEC 1
781
782struct win32_stat{
783 int st_dev;
784 __int64 st_ino;
785 unsigned short st_mode;
786 int st_nlink;
787 int st_uid;
788 int st_gid;
789 int st_rdev;
790 __int64 st_size;
791 int st_atime;
792 int st_atime_nsec;
793 int st_mtime;
794 int st_mtime_nsec;
795 int st_ctime;
796 int st_ctime_nsec;
797};
798
799static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
800
801static void
802FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
803{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000804 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
805 /* Cannot simply cast and dereference in_ptr,
806 since it might not be aligned properly */
807 __int64 in;
808 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000809 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
810 /* XXX Win32 supports time stamps past 2038; we currently don't */
811 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
812}
813
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814static void
815time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
816{
817 /* XXX endianness */
818 __int64 out;
819 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000820 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000821 memcpy(out_ptr, &out, sizeof(out));
822}
823
Martin v. Löwis14694662006-02-03 12:54:16 +0000824/* Below, we *know* that ugo+r is 0444 */
825#if _S_IREAD != 0400
826#error Unsupported C library
827#endif
828static int
829attributes_to_mode(DWORD attr)
830{
831 int m = 0;
832 if (attr & FILE_ATTRIBUTE_DIRECTORY)
833 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
834 else
835 m |= _S_IFREG;
836 if (attr & FILE_ATTRIBUTE_READONLY)
837 m |= 0444;
838 else
839 m |= 0666;
840 return m;
841}
842
843static int
844attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
845{
846 memset(result, 0, sizeof(*result));
847 result->st_mode = attributes_to_mode(info->dwFileAttributes);
848 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
849 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
850 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
851 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
852
853 return 0;
854}
855
Thomas Wouters89f507f2006-12-13 04:49:30 +0000856/* Emulate GetFileAttributesEx[AW] on Windows 95 */
857static int checked = 0;
858static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
859static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
860static void
861check_gfax()
862{
863 HINSTANCE hKernel32;
864 if (checked)
865 return;
866 checked = 1;
867 hKernel32 = GetModuleHandle("KERNEL32");
868 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
869 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
870}
871
Guido van Rossumd8faa362007-04-27 19:54:29 +0000872static BOOL
873attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
874{
875 HANDLE hFindFile;
876 WIN32_FIND_DATAA FileData;
877 hFindFile = FindFirstFileA(pszFile, &FileData);
878 if (hFindFile == INVALID_HANDLE_VALUE)
879 return FALSE;
880 FindClose(hFindFile);
881 pfad->dwFileAttributes = FileData.dwFileAttributes;
882 pfad->ftCreationTime = FileData.ftCreationTime;
883 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
884 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
885 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
886 pfad->nFileSizeLow = FileData.nFileSizeLow;
887 return TRUE;
888}
889
890static BOOL
891attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
892{
893 HANDLE hFindFile;
894 WIN32_FIND_DATAW FileData;
895 hFindFile = FindFirstFileW(pszFile, &FileData);
896 if (hFindFile == INVALID_HANDLE_VALUE)
897 return FALSE;
898 FindClose(hFindFile);
899 pfad->dwFileAttributes = FileData.dwFileAttributes;
900 pfad->ftCreationTime = FileData.ftCreationTime;
901 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
902 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
903 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
904 pfad->nFileSizeLow = FileData.nFileSizeLow;
905 return TRUE;
906}
907
Thomas Wouters89f507f2006-12-13 04:49:30 +0000908static BOOL WINAPI
909Py_GetFileAttributesExA(LPCSTR pszFile,
910 GET_FILEEX_INFO_LEVELS level,
911 LPVOID pv)
912{
913 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000914 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
915 /* First try to use the system's implementation, if that is
916 available and either succeeds to gives an error other than
917 that it isn't implemented. */
918 check_gfax();
919 if (gfaxa) {
920 result = gfaxa(pszFile, level, pv);
921 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
922 return result;
923 }
924 /* It's either not present, or not implemented.
925 Emulate using FindFirstFile. */
926 if (level != GetFileExInfoStandard) {
927 SetLastError(ERROR_INVALID_PARAMETER);
928 return FALSE;
929 }
930 /* Use GetFileAttributes to validate that the file name
931 does not contain wildcards (which FindFirstFile would
932 accept). */
933 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
934 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000935 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000936}
937
938static BOOL WINAPI
939Py_GetFileAttributesExW(LPCWSTR pszFile,
940 GET_FILEEX_INFO_LEVELS level,
941 LPVOID pv)
942{
943 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000944 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
945 /* First try to use the system's implementation, if that is
946 available and either succeeds to gives an error other than
947 that it isn't implemented. */
948 check_gfax();
949 if (gfaxa) {
950 result = gfaxw(pszFile, level, pv);
951 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
952 return result;
953 }
954 /* It's either not present, or not implemented.
955 Emulate using FindFirstFile. */
956 if (level != GetFileExInfoStandard) {
957 SetLastError(ERROR_INVALID_PARAMETER);
958 return FALSE;
959 }
960 /* Use GetFileAttributes to validate that the file name
961 does not contain wildcards (which FindFirstFile would
962 accept). */
963 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
964 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000965 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000966}
967
Martin v. Löwis14694662006-02-03 12:54:16 +0000968static int
969win32_stat(const char* path, struct win32_stat *result)
970{
971 WIN32_FILE_ATTRIBUTE_DATA info;
972 int code;
973 char *dot;
974 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000975 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976 if (GetLastError() != ERROR_SHARING_VIOLATION) {
977 /* Protocol violation: we explicitly clear errno, instead of
978 setting it to a POSIX error. Callers should use GetLastError. */
979 errno = 0;
980 return -1;
981 } else {
982 /* Could not get attributes on open file. Fall back to
983 reading the directory. */
984 if (!attributes_from_dir(path, &info)) {
985 /* Very strange. This should not fail now */
986 errno = 0;
987 return -1;
988 }
989 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000990 }
991 code = attribute_data_to_stat(&info, result);
992 if (code != 0)
993 return code;
994 /* Set S_IFEXEC if it is an .exe, .bat, ... */
995 dot = strrchr(path, '.');
996 if (dot) {
997 if (stricmp(dot, ".bat") == 0 ||
998 stricmp(dot, ".cmd") == 0 ||
999 stricmp(dot, ".exe") == 0 ||
1000 stricmp(dot, ".com") == 0)
1001 result->st_mode |= 0111;
1002 }
1003 return code;
1004}
1005
1006static int
1007win32_wstat(const wchar_t* path, struct win32_stat *result)
1008{
1009 int code;
1010 const wchar_t *dot;
1011 WIN32_FILE_ATTRIBUTE_DATA info;
1012 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001013 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001014 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1015 /* Protocol violation: we explicitly clear errno, instead of
1016 setting it to a POSIX error. Callers should use GetLastError. */
1017 errno = 0;
1018 return -1;
1019 } else {
1020 /* Could not get attributes on open file. Fall back to
1021 reading the directory. */
1022 if (!attributes_from_dir_w(path, &info)) {
1023 /* Very strange. This should not fail now */
1024 errno = 0;
1025 return -1;
1026 }
1027 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001028 }
1029 code = attribute_data_to_stat(&info, result);
1030 if (code < 0)
1031 return code;
1032 /* Set IFEXEC if it is an .exe, .bat, ... */
1033 dot = wcsrchr(path, '.');
1034 if (dot) {
1035 if (_wcsicmp(dot, L".bat") == 0 ||
1036 _wcsicmp(dot, L".cmd") == 0 ||
1037 _wcsicmp(dot, L".exe") == 0 ||
1038 _wcsicmp(dot, L".com") == 0)
1039 result->st_mode |= 0111;
1040 }
1041 return code;
1042}
1043
1044static int
1045win32_fstat(int file_number, struct win32_stat *result)
1046{
1047 BY_HANDLE_FILE_INFORMATION info;
1048 HANDLE h;
1049 int type;
1050
1051 h = (HANDLE)_get_osfhandle(file_number);
1052
1053 /* Protocol violation: we explicitly clear errno, instead of
1054 setting it to a POSIX error. Callers should use GetLastError. */
1055 errno = 0;
1056
1057 if (h == INVALID_HANDLE_VALUE) {
1058 /* This is really a C library error (invalid file handle).
1059 We set the Win32 error to the closes one matching. */
1060 SetLastError(ERROR_INVALID_HANDLE);
1061 return -1;
1062 }
1063 memset(result, 0, sizeof(*result));
1064
1065 type = GetFileType(h);
1066 if (type == FILE_TYPE_UNKNOWN) {
1067 DWORD error = GetLastError();
1068 if (error != 0) {
1069 return -1;
1070 }
1071 /* else: valid but unknown file */
1072 }
1073
1074 if (type != FILE_TYPE_DISK) {
1075 if (type == FILE_TYPE_CHAR)
1076 result->st_mode = _S_IFCHR;
1077 else if (type == FILE_TYPE_PIPE)
1078 result->st_mode = _S_IFIFO;
1079 return 0;
1080 }
1081
1082 if (!GetFileInformationByHandle(h, &info)) {
1083 return -1;
1084 }
1085
1086 /* similar to stat() */
1087 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1088 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1089 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1090 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1091 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1092 /* specific to fstat() */
1093 result->st_nlink = info.nNumberOfLinks;
1094 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1095 return 0;
1096}
1097
1098#endif /* MS_WINDOWS */
1099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001100PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001101"stat_result: Result from stat or lstat.\n\n\
1102This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001103 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1105\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001106Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1107or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001108\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110
1111static PyStructSequence_Field stat_result_fields[] = {
1112 {"st_mode", "protection bits"},
1113 {"st_ino", "inode"},
1114 {"st_dev", "device"},
1115 {"st_nlink", "number of hard links"},
1116 {"st_uid", "user ID of owner"},
1117 {"st_gid", "group ID of owner"},
1118 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001119 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1120 {NULL, "integer time of last access"},
1121 {NULL, "integer time of last modification"},
1122 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001123 {"st_atime", "time of last access"},
1124 {"st_mtime", "time of last modification"},
1125 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001126#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001127 {"st_blksize", "blocksize for filesystem I/O"},
1128#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001129#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001130 {"st_blocks", "number of blocks allocated"},
1131#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001132#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001133 {"st_rdev", "device type (if inode device)"},
1134#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001135#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1136 {"st_flags", "user defined flags for file"},
1137#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001138#ifdef HAVE_STRUCT_STAT_ST_GEN
1139 {"st_gen", "generation number"},
1140#endif
1141#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1142 {"st_birthtime", "time of creation"},
1143#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001144 {0}
1145};
1146
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001147#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001148#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001150#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001151#endif
1152
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001153#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001154#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1155#else
1156#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1157#endif
1158
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001159#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001160#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1161#else
1162#define ST_RDEV_IDX ST_BLOCKS_IDX
1163#endif
1164
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001165#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1166#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1167#else
1168#define ST_FLAGS_IDX ST_RDEV_IDX
1169#endif
1170
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001171#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001172#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001173#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001174#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001175#endif
1176
1177#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1178#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1179#else
1180#define ST_BIRTHTIME_IDX ST_GEN_IDX
1181#endif
1182
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001183static PyStructSequence_Desc stat_result_desc = {
1184 "stat_result", /* name */
1185 stat_result__doc__, /* doc */
1186 stat_result_fields,
1187 10
1188};
1189
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001190PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1192This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001193 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001194or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001195\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001196See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001197
1198static PyStructSequence_Field statvfs_result_fields[] = {
1199 {"f_bsize", },
1200 {"f_frsize", },
1201 {"f_blocks", },
1202 {"f_bfree", },
1203 {"f_bavail", },
1204 {"f_files", },
1205 {"f_ffree", },
1206 {"f_favail", },
1207 {"f_flag", },
1208 {"f_namemax",},
1209 {0}
1210};
1211
1212static PyStructSequence_Desc statvfs_result_desc = {
1213 "statvfs_result", /* name */
1214 statvfs_result__doc__, /* doc */
1215 statvfs_result_fields,
1216 10
1217};
1218
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001219static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001220static PyTypeObject StatResultType;
1221static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001222static newfunc structseq_new;
1223
1224static PyObject *
1225statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1226{
1227 PyStructSequence *result;
1228 int i;
1229
1230 result = (PyStructSequence*)structseq_new(type, args, kwds);
1231 if (!result)
1232 return NULL;
1233 /* If we have been initialized from a tuple,
1234 st_?time might be set to None. Initialize it
1235 from the int slots. */
1236 for (i = 7; i <= 9; i++) {
1237 if (result->ob_item[i+3] == Py_None) {
1238 Py_DECREF(Py_None);
1239 Py_INCREF(result->ob_item[i]);
1240 result->ob_item[i+3] = result->ob_item[i];
1241 }
1242 }
1243 return (PyObject*)result;
1244}
1245
1246
1247
1248/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001249static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001250
1251PyDoc_STRVAR(stat_float_times__doc__,
1252"stat_float_times([newval]) -> oldval\n\n\
1253Determine whether os.[lf]stat represents time stamps as float objects.\n\
1254If newval is True, future calls to stat() return floats, if it is False,\n\
1255future calls return ints. \n\
1256If newval is omitted, return the current setting.\n");
1257
1258static PyObject*
1259stat_float_times(PyObject* self, PyObject *args)
1260{
1261 int newval = -1;
1262 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1263 return NULL;
1264 if (newval == -1)
1265 /* Return old value */
1266 return PyBool_FromLong(_stat_float_times);
1267 _stat_float_times = newval;
1268 Py_INCREF(Py_None);
1269 return Py_None;
1270}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001271
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001272static void
1273fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1274{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001275 PyObject *fval,*ival;
1276#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001277 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001278#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001279 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001280#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001281 if (!ival)
1282 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001283 if (_stat_float_times) {
1284 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1285 } else {
1286 fval = ival;
1287 Py_INCREF(fval);
1288 }
1289 PyStructSequence_SET_ITEM(v, index, ival);
1290 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001291}
1292
Tim Peters5aa91602002-01-30 05:46:57 +00001293/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001294 (used by posix_stat() and posix_fstat()) */
1295static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001296_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001297{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001298 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001299 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001300 if (v == NULL)
1301 return NULL;
1302
Christian Heimes217cfd12007-12-02 14:31:20 +00001303 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001304#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001305 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001306 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001307#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001308 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001309#endif
1310#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001311 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001312 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001313#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001314 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001315#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001316 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1317 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1318 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001319#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001320 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001322#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001323 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001324#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001325
Martin v. Löwis14694662006-02-03 12:54:16 +00001326#if defined(HAVE_STAT_TV_NSEC)
1327 ansec = st->st_atim.tv_nsec;
1328 mnsec = st->st_mtim.tv_nsec;
1329 cnsec = st->st_ctim.tv_nsec;
1330#elif defined(HAVE_STAT_TV_NSEC2)
1331 ansec = st->st_atimespec.tv_nsec;
1332 mnsec = st->st_mtimespec.tv_nsec;
1333 cnsec = st->st_ctimespec.tv_nsec;
1334#elif defined(HAVE_STAT_NSEC)
1335 ansec = st->st_atime_nsec;
1336 mnsec = st->st_mtime_nsec;
1337 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001338#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001339 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001340#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001341 fill_time(v, 7, st->st_atime, ansec);
1342 fill_time(v, 8, st->st_mtime, mnsec);
1343 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001344
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001345#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001346 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001347 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001349#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001350 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001351 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001352#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001353#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001354 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001355 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001356#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001357#ifdef HAVE_STRUCT_STAT_ST_GEN
1358 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001359 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001360#endif
1361#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1362 {
1363 PyObject *val;
1364 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001365 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001366#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001367 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001368#else
1369 bnsec = 0;
1370#endif
1371 if (_stat_float_times) {
1372 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1373 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001374 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375 }
1376 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1377 val);
1378 }
1379#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001380#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1381 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001382 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001383#endif
Fred Drake699f3522000-06-29 21:12:41 +00001384
1385 if (PyErr_Occurred()) {
1386 Py_DECREF(v);
1387 return NULL;
1388 }
1389
1390 return v;
1391}
1392
Martin v. Löwisd8948722004-06-02 09:57:56 +00001393#ifdef MS_WINDOWS
1394
1395/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1396 where / can be used in place of \ and the trailing slash is optional.
1397 Both SERVER and SHARE must have at least one character.
1398*/
1399
1400#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1401#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001402#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001403#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001404#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001405
Tim Peters4ad82172004-08-30 17:02:04 +00001406static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001407IsUNCRootA(char *path, int pathlen)
1408{
1409 #define ISSLASH ISSLASHA
1410
1411 int i, share;
1412
1413 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1414 /* minimum UNCRoot is \\x\y */
1415 return FALSE;
1416 for (i = 2; i < pathlen ; i++)
1417 if (ISSLASH(path[i])) break;
1418 if (i == 2 || i == pathlen)
1419 /* do not allow \\\SHARE or \\SERVER */
1420 return FALSE;
1421 share = i+1;
1422 for (i = share; i < pathlen; i++)
1423 if (ISSLASH(path[i])) break;
1424 return (i != share && (i == pathlen || i == pathlen-1));
1425
1426 #undef ISSLASH
1427}
1428
1429#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001430static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001431IsUNCRootW(Py_UNICODE *path, int pathlen)
1432{
1433 #define ISSLASH ISSLASHW
1434
1435 int i, share;
1436
1437 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1438 /* minimum UNCRoot is \\x\y */
1439 return FALSE;
1440 for (i = 2; i < pathlen ; i++)
1441 if (ISSLASH(path[i])) break;
1442 if (i == 2 || i == pathlen)
1443 /* do not allow \\\SHARE or \\SERVER */
1444 return FALSE;
1445 share = i+1;
1446 for (i = share; i < pathlen; i++)
1447 if (ISSLASH(path[i])) break;
1448 return (i != share && (i == pathlen || i == pathlen-1));
1449
1450 #undef ISSLASH
1451}
1452#endif /* Py_WIN_WIDE_FILENAMES */
1453#endif /* MS_WINDOWS */
1454
Barry Warsaw53699e91996-12-10 23:23:01 +00001455static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001456posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001457 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001458#ifdef __VMS
1459 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1460#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001461 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001462#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001463 char *wformat,
1464 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001465{
Fred Drake699f3522000-06-29 21:12:41 +00001466 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001467 char *path = NULL; /* pass this to stat; do not free() it */
1468 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001469 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001470 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001471
1472#ifdef Py_WIN_WIDE_FILENAMES
1473 /* If on wide-character-capable OS see if argument
1474 is Unicode and if so use wide API. */
1475 if (unicode_file_names()) {
1476 PyUnicodeObject *po;
1477 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001478 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1479
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001480 Py_BEGIN_ALLOW_THREADS
1481 /* PyUnicode_AS_UNICODE result OK without
1482 thread lock as it is a simple dereference. */
1483 res = wstatfunc(wpath, &st);
1484 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001485
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001486 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001487 return win32_error_unicode("stat", wpath);
1488 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001489 }
1490 /* Drop the argument parsing error as narrow strings
1491 are also valid. */
1492 PyErr_Clear();
1493 }
1494#endif
1495
Tim Peters5aa91602002-01-30 05:46:57 +00001496 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001497 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001498 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001499 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001500
Barry Warsaw53699e91996-12-10 23:23:01 +00001501 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001502 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001503 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001504
1505 if (res != 0) {
1506#ifdef MS_WINDOWS
1507 result = win32_error("stat", pathfree);
1508#else
1509 result = posix_error_with_filename(pathfree);
1510#endif
1511 }
1512 else
1513 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001514
Tim Peters500bd032001-12-19 19:05:01 +00001515 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001516 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001517}
1518
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519/* POSIX methods */
1520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001521PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001522"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001523Use the real uid/gid to test for access to a path. Note that most\n\
1524operations will use the effective uid/gid, therefore this routine can\n\
1525be used in a suid/sgid environment to test if the invoking user has the\n\
1526specified access to the path. The mode argument can be F_OK to test\n\
1527existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001528
1529static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001530posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001531{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001532 char *path;
1533 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001534
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001535#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001536 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001537 if (unicode_file_names()) {
1538 PyUnicodeObject *po;
1539 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1540 Py_BEGIN_ALLOW_THREADS
1541 /* PyUnicode_AS_UNICODE OK without thread lock as
1542 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001543 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001544 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001545 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001546 }
1547 /* Drop the argument parsing error as narrow strings
1548 are also valid. */
1549 PyErr_Clear();
1550 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001551 if (!PyArg_ParseTuple(args, "eti:access",
1552 Py_FileSystemDefaultEncoding, &path, &mode))
1553 return 0;
1554 Py_BEGIN_ALLOW_THREADS
1555 attr = GetFileAttributesA(path);
1556 Py_END_ALLOW_THREADS
1557 PyMem_Free(path);
1558finish:
1559 if (attr == 0xFFFFFFFF)
1560 /* File does not exist, or cannot read attributes */
1561 return PyBool_FromLong(0);
1562 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001563 the file isn't read-only, or if it's a directory, as there are
1564 no read-only directories on Windows. */
1565 return PyBool_FromLong(!(mode & 2)
1566 || !(attr & FILE_ATTRIBUTE_READONLY)
1567 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568#else
1569 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001570 if (!PyArg_ParseTuple(args, "eti:access",
1571 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001572 return NULL;
1573 Py_BEGIN_ALLOW_THREADS
1574 res = access(path, mode);
1575 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001576 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001577 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001578#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001579}
1580
Guido van Rossumd371ff11999-01-25 16:12:23 +00001581#ifndef F_OK
1582#define F_OK 0
1583#endif
1584#ifndef R_OK
1585#define R_OK 4
1586#endif
1587#ifndef W_OK
1588#define W_OK 2
1589#endif
1590#ifndef X_OK
1591#define X_OK 1
1592#endif
1593
1594#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001596"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001598
1599static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001600posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001601{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001602 int id;
1603 char *ret;
1604
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001605 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001606 return NULL;
1607
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001608#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001609 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001610 if (id == 0) {
1611 ret = ttyname();
1612 }
1613 else {
1614 ret = NULL;
1615 }
1616#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001617 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001618#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001619 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001620 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001621 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001622}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001623#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001624
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001625#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001627"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001629
1630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001631posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001632{
1633 char *ret;
1634 char buffer[L_ctermid];
1635
Greg Wardb48bc172000-03-01 21:51:56 +00001636#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001637 ret = ctermid_r(buffer);
1638#else
1639 ret = ctermid(buffer);
1640#endif
1641 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001642 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001643 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001644}
1645#endif
1646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001647PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001648"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001650
Barry Warsaw53699e91996-12-10 23:23:01 +00001651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001652posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001653{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001654#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001655 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001656#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001657 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001658#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001659 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001660#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001661 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001662#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001663}
1664
Fred Drake4d1e64b2002-04-15 19:40:07 +00001665#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001666PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001667"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001668Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001669opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670
1671static PyObject *
1672posix_fchdir(PyObject *self, PyObject *fdobj)
1673{
1674 return posix_fildes(fdobj, fchdir);
1675}
1676#endif /* HAVE_FCHDIR */
1677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001679PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001680"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682
Barry Warsaw53699e91996-12-10 23:23:01 +00001683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001684posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001685{
Mark Hammondef8b6542001-05-13 08:04:26 +00001686 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001687 int i;
1688 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001689#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001690 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001691 if (unicode_file_names()) {
1692 PyUnicodeObject *po;
1693 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1694 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1696 if (attr != 0xFFFFFFFF) {
1697 if (i & _S_IWRITE)
1698 attr &= ~FILE_ATTRIBUTE_READONLY;
1699 else
1700 attr |= FILE_ATTRIBUTE_READONLY;
1701 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1702 }
1703 else
1704 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001705 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001706 if (!res)
1707 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001708 PyUnicode_AS_UNICODE(po));
1709 Py_INCREF(Py_None);
1710 return Py_None;
1711 }
1712 /* Drop the argument parsing error as narrow strings
1713 are also valid. */
1714 PyErr_Clear();
1715 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001716 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1717 &path, &i))
1718 return NULL;
1719 Py_BEGIN_ALLOW_THREADS
1720 attr = GetFileAttributesA(path);
1721 if (attr != 0xFFFFFFFF) {
1722 if (i & _S_IWRITE)
1723 attr &= ~FILE_ATTRIBUTE_READONLY;
1724 else
1725 attr |= FILE_ATTRIBUTE_READONLY;
1726 res = SetFileAttributesA(path, attr);
1727 }
1728 else
1729 res = 0;
1730 Py_END_ALLOW_THREADS
1731 if (!res) {
1732 win32_error("chmod", path);
1733 PyMem_Free(path);
1734 return NULL;
1735 }
1736 PyMem_Free(path);
1737 Py_INCREF(Py_None);
1738 return Py_None;
1739#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001740 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001741 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001742 return NULL;
1743 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001744 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001745 Py_END_ALLOW_THREADS
1746 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001747 return posix_error_with_allocated_filename(path);
1748 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001749 Py_INCREF(Py_None);
1750 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001751#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001752}
1753
Christian Heimes4e30a842007-11-30 22:12:06 +00001754#ifdef HAVE_FCHMOD
1755PyDoc_STRVAR(posix_fchmod__doc__,
1756"fchmod(fd, mode)\n\n\
1757Change the access permissions of the file given by file\n\
1758descriptor fd.");
1759
1760static PyObject *
1761posix_fchmod(PyObject *self, PyObject *args)
1762{
1763 int fd, mode, res;
1764 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1765 return NULL;
1766 Py_BEGIN_ALLOW_THREADS
1767 res = fchmod(fd, mode);
1768 Py_END_ALLOW_THREADS
1769 if (res < 0)
1770 return posix_error();
1771 Py_RETURN_NONE;
1772}
1773#endif /* HAVE_FCHMOD */
1774
1775#ifdef HAVE_LCHMOD
1776PyDoc_STRVAR(posix_lchmod__doc__,
1777"lchmod(path, mode)\n\n\
1778Change the access permissions of a file. If path is a symlink, this\n\
1779affects the link itself rather than the target.");
1780
1781static PyObject *
1782posix_lchmod(PyObject *self, PyObject *args)
1783{
1784 char *path = NULL;
1785 int i;
1786 int res;
1787 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1788 &path, &i))
1789 return NULL;
1790 Py_BEGIN_ALLOW_THREADS
1791 res = lchmod(path, i);
1792 Py_END_ALLOW_THREADS
1793 if (res < 0)
1794 return posix_error_with_allocated_filename(path);
1795 PyMem_Free(path);
1796 Py_RETURN_NONE;
1797}
1798#endif /* HAVE_LCHMOD */
1799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001800
Thomas Wouterscf297e42007-02-23 15:07:44 +00001801#ifdef HAVE_CHFLAGS
1802PyDoc_STRVAR(posix_chflags__doc__,
1803"chflags(path, flags)\n\n\
1804Set file flags.");
1805
1806static PyObject *
1807posix_chflags(PyObject *self, PyObject *args)
1808{
1809 char *path;
1810 unsigned long flags;
1811 int res;
1812 if (!PyArg_ParseTuple(args, "etk:chflags",
1813 Py_FileSystemDefaultEncoding, &path, &flags))
1814 return NULL;
1815 Py_BEGIN_ALLOW_THREADS
1816 res = chflags(path, flags);
1817 Py_END_ALLOW_THREADS
1818 if (res < 0)
1819 return posix_error_with_allocated_filename(path);
1820 PyMem_Free(path);
1821 Py_INCREF(Py_None);
1822 return Py_None;
1823}
1824#endif /* HAVE_CHFLAGS */
1825
1826#ifdef HAVE_LCHFLAGS
1827PyDoc_STRVAR(posix_lchflags__doc__,
1828"lchflags(path, flags)\n\n\
1829Set file flags.\n\
1830This function will not follow symbolic links.");
1831
1832static PyObject *
1833posix_lchflags(PyObject *self, PyObject *args)
1834{
1835 char *path;
1836 unsigned long flags;
1837 int res;
1838 if (!PyArg_ParseTuple(args, "etk:lchflags",
1839 Py_FileSystemDefaultEncoding, &path, &flags))
1840 return NULL;
1841 Py_BEGIN_ALLOW_THREADS
1842 res = lchflags(path, flags);
1843 Py_END_ALLOW_THREADS
1844 if (res < 0)
1845 return posix_error_with_allocated_filename(path);
1846 PyMem_Free(path);
1847 Py_INCREF(Py_None);
1848 return Py_None;
1849}
1850#endif /* HAVE_LCHFLAGS */
1851
Martin v. Löwis244edc82001-10-04 22:44:26 +00001852#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001853PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001854"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001856
1857static PyObject *
1858posix_chroot(PyObject *self, PyObject *args)
1859{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001860 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001861}
1862#endif
1863
Guido van Rossum21142a01999-01-08 21:05:37 +00001864#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001866"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001868
1869static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001870posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001871{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001872 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001873}
1874#endif /* HAVE_FSYNC */
1875
1876#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001877
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001878#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001879extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1880#endif
1881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001882PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001883"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001884force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001886
1887static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001888posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001889{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001890 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001891}
1892#endif /* HAVE_FDATASYNC */
1893
1894
Fredrik Lundh10723342000-07-10 16:38:09 +00001895#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001896PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001897"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001899
Barry Warsaw53699e91996-12-10 23:23:01 +00001900static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001901posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001902{
Mark Hammondef8b6542001-05-13 08:04:26 +00001903 char *path = NULL;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001904 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001905 int res;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001906 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001907 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001908 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001909 return NULL;
1910 Py_BEGIN_ALLOW_THREADS
1911 res = chown(path, (uid_t) uid, (gid_t) gid);
1912 Py_END_ALLOW_THREADS
1913 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001914 return posix_error_with_allocated_filename(path);
1915 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001916 Py_INCREF(Py_None);
1917 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001918}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001919#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001920
Christian Heimes4e30a842007-11-30 22:12:06 +00001921#ifdef HAVE_FCHOWN
1922PyDoc_STRVAR(posix_fchown__doc__,
1923"fchown(fd, uid, gid)\n\n\
1924Change the owner and group id of the file given by file descriptor\n\
1925fd to the numeric uid and gid.");
1926
1927static PyObject *
1928posix_fchown(PyObject *self, PyObject *args)
1929{
1930 int fd, uid, gid;
1931 int res;
1932 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1933 return NULL;
1934 Py_BEGIN_ALLOW_THREADS
1935 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1936 Py_END_ALLOW_THREADS
1937 if (res < 0)
1938 return posix_error();
1939 Py_RETURN_NONE;
1940}
1941#endif /* HAVE_FCHOWN */
1942
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001943#ifdef HAVE_LCHOWN
1944PyDoc_STRVAR(posix_lchown__doc__,
1945"lchown(path, uid, gid)\n\n\
1946Change the owner and group id of path to the numeric uid and gid.\n\
1947This function will not follow symbolic links.");
1948
1949static PyObject *
1950posix_lchown(PyObject *self, PyObject *args)
1951{
1952 char *path = NULL;
1953 int uid, gid;
1954 int res;
1955 if (!PyArg_ParseTuple(args, "etii:lchown",
1956 Py_FileSystemDefaultEncoding, &path,
1957 &uid, &gid))
1958 return NULL;
1959 Py_BEGIN_ALLOW_THREADS
1960 res = lchown(path, (uid_t) uid, (gid_t) gid);
1961 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001962 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001963 return posix_error_with_allocated_filename(path);
1964 PyMem_Free(path);
1965 Py_INCREF(Py_None);
1966 return Py_None;
1967}
1968#endif /* HAVE_LCHOWN */
1969
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001970
Guido van Rossum36bc6801995-06-14 22:54:23 +00001971#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001972static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001973posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001974{
1975 char buf[1026];
1976 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001977
1978#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001979 if (!use_bytes && unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001980 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001981 wchar_t *wbuf2 = wbuf;
1982 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001983 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001984 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001985 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1986 /* If the buffer is large enough, len does not include the
1987 terminating \0. If the buffer is too small, len includes
1988 the space needed for the terminator. */
1989 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1990 wbuf2 = malloc(len * sizeof(wchar_t));
1991 if (wbuf2)
1992 len = GetCurrentDirectoryW(len, wbuf2);
1993 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001994 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001995 if (!wbuf2) {
1996 PyErr_NoMemory();
1997 return NULL;
1998 }
1999 if (!len) {
2000 if (wbuf2 != wbuf) free(wbuf2);
2001 return win32_error("getcwdu", NULL);
2002 }
2003 resobj = PyUnicode_FromWideChar(wbuf2, len);
2004 if (wbuf2 != wbuf) free(wbuf2);
2005 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006 }
2007#endif
2008
2009 Py_BEGIN_ALLOW_THREADS
2010#if defined(PYOS_OS2) && defined(PYCC_GCC)
2011 res = _getcwd2(buf, sizeof buf);
2012#else
2013 res = getcwd(buf, sizeof buf);
2014#endif
2015 Py_END_ALLOW_THREADS
2016 if (res == NULL)
2017 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002018 if (use_bytes)
2019 return PyBytes_FromStringAndSize(buf, strlen(buf));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002020 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2021}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002022
2023PyDoc_STRVAR(posix_getcwd__doc__,
2024"getcwd() -> path\n\n\
2025Return a unicode string representing the current working directory.");
2026
2027static PyObject *
2028posix_getcwd_unicode(PyObject *self)
2029{
2030 return posix_getcwd(0);
2031}
2032
2033PyDoc_STRVAR(posix_getcwdb__doc__,
2034"getcwdb() -> path\n\n\
2035Return a bytes string representing the current working directory.");
2036
2037static PyObject *
2038posix_getcwd_bytes(PyObject *self)
2039{
2040 return posix_getcwd(1);
2041}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002042#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002043
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002044
Guido van Rossumb6775db1994-08-01 11:34:53 +00002045#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002046PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002047"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002049
Barry Warsaw53699e91996-12-10 23:23:01 +00002050static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002051posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002052{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002053 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002054}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002055#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002059"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060Return a list containing the names of the entries in the directory.\n\
2061\n\
2062 path: path of directory to list\n\
2063\n\
2064The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066
Barry Warsaw53699e91996-12-10 23:23:01 +00002067static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002068posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002069{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002070 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002071 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002072#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002073
Barry Warsaw53699e91996-12-10 23:23:01 +00002074 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002075 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002076 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002078 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002079 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002082#ifdef Py_WIN_WIDE_FILENAMES
2083 /* If on wide-character-capable OS see if argument
2084 is Unicode and if so use wide API. */
2085 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002086 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002087 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2088 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002089 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002090 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002091 /* Overallocate for \\*.*\0 */
2092 len = PyUnicode_GET_SIZE(po);
2093 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2094 if (!wnamebuf) {
2095 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002096 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002097 }
2098 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2099 wch = len > 0 ? wnamebuf[len-1] : '\0';
2100 if (wch != L'/' && wch != L'\\' && wch != L':')
2101 wnamebuf[len++] = L'\\';
2102 wcscpy(wnamebuf + len, L"*.*");
2103 if ((d = PyList_New(0)) == NULL) {
2104 free(wnamebuf);
2105 return NULL;
2106 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002107 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2108 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002109 int error = GetLastError();
2110 if (error == ERROR_FILE_NOT_FOUND) {
2111 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002112 return d;
2113 }
2114 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002115 win32_error_unicode("FindFirstFileW", wnamebuf);
2116 free(wnamebuf);
2117 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002118 }
2119 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002120 /* Skip over . and .. */
2121 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2122 wcscmp(wFileData.cFileName, L"..") != 0) {
2123 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2124 if (v == NULL) {
2125 Py_DECREF(d);
2126 d = NULL;
2127 break;
2128 }
2129 if (PyList_Append(d, v) != 0) {
2130 Py_DECREF(v);
2131 Py_DECREF(d);
2132 d = NULL;
2133 break;
2134 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002135 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002136 }
Georg Brandl622927b2006-03-07 12:48:03 +00002137 Py_BEGIN_ALLOW_THREADS
2138 result = FindNextFileW(hFindFile, &wFileData);
2139 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002140 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2141 it got to the end of the directory. */
2142 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2143 Py_DECREF(d);
2144 win32_error_unicode("FindNextFileW", wnamebuf);
2145 FindClose(hFindFile);
2146 free(wnamebuf);
2147 return NULL;
2148 }
Georg Brandl622927b2006-03-07 12:48:03 +00002149 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002150
2151 if (FindClose(hFindFile) == FALSE) {
2152 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002153 win32_error_unicode("FindClose", wnamebuf);
2154 free(wnamebuf);
2155 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002157 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002158 return d;
2159 }
2160 /* Drop the argument parsing error as narrow strings
2161 are also valid. */
2162 PyErr_Clear();
2163 }
2164#endif
2165
Tim Peters5aa91602002-01-30 05:46:57 +00002166 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002167 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002168 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002169 if (len > 0) {
2170 char ch = namebuf[len-1];
2171 if (ch != SEP && ch != ALTSEP && ch != ':')
2172 namebuf[len++] = '/';
2173 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002174 strcpy(namebuf + len, "*.*");
2175
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002177 return NULL;
2178
2179 hFindFile = FindFirstFile(namebuf, &FileData);
2180 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002181 int error = GetLastError();
2182 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002183 return d;
2184 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002185 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002186 }
2187 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002188 /* Skip over . and .. */
2189 if (strcmp(FileData.cFileName, ".") != 0 &&
2190 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002191 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002192 if (v == NULL) {
2193 Py_DECREF(d);
2194 d = NULL;
2195 break;
2196 }
2197 if (PyList_Append(d, v) != 0) {
2198 Py_DECREF(v);
2199 Py_DECREF(d);
2200 d = NULL;
2201 break;
2202 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002204 }
Georg Brandl622927b2006-03-07 12:48:03 +00002205 Py_BEGIN_ALLOW_THREADS
2206 result = FindNextFile(hFindFile, &FileData);
2207 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002208 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2209 it got to the end of the directory. */
2210 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2211 Py_DECREF(d);
2212 win32_error("FindNextFile", namebuf);
2213 FindClose(hFindFile);
2214 return NULL;
2215 }
Georg Brandl622927b2006-03-07 12:48:03 +00002216 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002217
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002218 if (FindClose(hFindFile) == FALSE) {
2219 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002220 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002221 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002222
2223 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002224
Tim Peters0bb44a42000-09-15 07:44:49 +00002225#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002226
2227#ifndef MAX_PATH
2228#define MAX_PATH CCHMAXPATH
2229#endif
2230 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002231 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002232 PyObject *d, *v;
2233 char namebuf[MAX_PATH+5];
2234 HDIR hdir = 1;
2235 ULONG srchcnt = 1;
2236 FILEFINDBUF3 ep;
2237 APIRET rc;
2238
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002239 if (!PyArg_ParseTuple(args, "et#:listdir",
2240 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002241 return NULL;
2242 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002243 PyMem_Free(name);
2244 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002245 return NULL;
2246 }
2247 strcpy(namebuf, name);
2248 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002249 if (*pt == ALTSEP)
2250 *pt = SEP;
2251 if (namebuf[len-1] != SEP)
2252 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002253 strcpy(namebuf + len, "*.*");
2254
Neal Norwitz6c913782007-10-14 03:23:09 +00002255 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002256 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002257 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002258 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002259
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002260 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2261 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002262 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002263 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2264 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2265 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266
2267 if (rc != NO_ERROR) {
2268 errno = ENOENT;
Neal Norwitz6c913782007-10-14 03:23:09 +00002269 return posix_error_with_allocated_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002270 }
2271
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002272 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273 do {
2274 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002275 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002276 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002277
2278 strcpy(namebuf, ep.achName);
2279
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002280 /* Leave Case of Name Alone -- In Native Form */
2281 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002282
Christian Heimes72b710a2008-05-26 13:28:38 +00002283 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002284 if (v == NULL) {
2285 Py_DECREF(d);
2286 d = NULL;
2287 break;
2288 }
2289 if (PyList_Append(d, v) != 0) {
2290 Py_DECREF(v);
2291 Py_DECREF(d);
2292 d = NULL;
2293 break;
2294 }
2295 Py_DECREF(v);
2296 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2297 }
2298
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002299 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002300 return d;
2301#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002302
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002303 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002304 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002305 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002306 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002307 int arg_is_unicode = 1;
2308
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002309 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002310 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2311 arg_is_unicode = 0;
2312 PyErr_Clear();
2313 }
2314 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002315 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002316 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002317 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002318 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002319 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002320 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002321 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002322 return NULL;
2323 }
Georg Brandl622927b2006-03-07 12:48:03 +00002324 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002325 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002326 Py_BEGIN_ALLOW_THREADS
2327 ep = readdir(dirp);
2328 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002329 if (ep == NULL) {
2330 if (errno == 0) {
2331 break;
2332 } else {
2333 closedir(dirp);
2334 Py_DECREF(d);
2335 return posix_error_with_allocated_filename(name);
2336 }
2337 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002338 if (ep->d_name[0] == '.' &&
2339 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002340 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002341 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002342 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002343 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002344 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002345 d = NULL;
2346 break;
2347 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002348 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002349 PyObject *w;
2350
2351 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002352 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002353 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002354 if (w != NULL) {
2355 Py_DECREF(v);
2356 v = w;
2357 }
2358 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002359 /* Ignore undecodable filenames, as discussed
2360 * in issue 3187. To include these,
2361 * use getcwdb(). */
Just van Rossum6a421832003-03-04 19:30:44 +00002362 PyErr_Clear();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002363 Py_DECREF(v);
2364 continue;
Just van Rossum46c97842003-02-25 21:42:15 +00002365 }
Just van Rossum46c97842003-02-25 21:42:15 +00002366 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002367 if (PyList_Append(d, v) != 0) {
2368 Py_DECREF(v);
2369 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002370 d = NULL;
2371 break;
2372 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002373 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002374 }
2375 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002376 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002377
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002379
Tim Peters0bb44a42000-09-15 07:44:49 +00002380#endif /* which OS */
2381} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002382
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002383#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002384/* A helper function for abspath on win32 */
2385static PyObject *
2386posix__getfullpathname(PyObject *self, PyObject *args)
2387{
2388 /* assume encoded strings wont more than double no of chars */
2389 char inbuf[MAX_PATH*2];
2390 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002391 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002392 char outbuf[MAX_PATH*2];
2393 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002394#ifdef Py_WIN_WIDE_FILENAMES
2395 if (unicode_file_names()) {
2396 PyUnicodeObject *po;
2397 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002398 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2399 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002400 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002401 DWORD result;
2402 PyObject *v;
2403 result = GetFullPathNameW(wpath,
2404 sizeof(woutbuf)/sizeof(woutbuf[0]),
2405 woutbuf, &wtemp);
2406 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2407 woutbufp = malloc(result * sizeof(Py_UNICODE));
2408 if (!woutbufp)
2409 return PyErr_NoMemory();
2410 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2411 }
2412 if (result)
2413 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2414 else
2415 v = win32_error_unicode("GetFullPathNameW", wpath);
2416 if (woutbufp != woutbuf)
2417 free(woutbufp);
2418 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002419 }
2420 /* Drop the argument parsing error as narrow strings
2421 are also valid. */
2422 PyErr_Clear();
2423 }
2424#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002425 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2426 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002427 &insize))
2428 return NULL;
2429 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2430 outbuf, &temp))
2431 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002432 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2433 return PyUnicode_Decode(outbuf, strlen(outbuf),
2434 Py_FileSystemDefaultEncoding, NULL);
2435 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002436 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002437} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002438#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002440PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002441"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002442Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002443
Barry Warsaw53699e91996-12-10 23:23:01 +00002444static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002445posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002446{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002447 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002448 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002449 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002450
2451#ifdef Py_WIN_WIDE_FILENAMES
2452 if (unicode_file_names()) {
2453 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002454 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002455 Py_BEGIN_ALLOW_THREADS
2456 /* PyUnicode_AS_UNICODE OK without thread lock as
2457 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002458 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002459 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002460 if (!res)
2461 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002462 Py_INCREF(Py_None);
2463 return Py_None;
2464 }
2465 /* Drop the argument parsing error as narrow strings
2466 are also valid. */
2467 PyErr_Clear();
2468 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002469 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2470 Py_FileSystemDefaultEncoding, &path, &mode))
2471 return NULL;
2472 Py_BEGIN_ALLOW_THREADS
2473 /* PyUnicode_AS_UNICODE OK without thread lock as
2474 it is a simple dereference. */
2475 res = CreateDirectoryA(path, NULL);
2476 Py_END_ALLOW_THREADS
2477 if (!res) {
2478 win32_error("mkdir", path);
2479 PyMem_Free(path);
2480 return NULL;
2481 }
2482 PyMem_Free(path);
2483 Py_INCREF(Py_None);
2484 return Py_None;
2485#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002486
Tim Peters5aa91602002-01-30 05:46:57 +00002487 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002488 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002489 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002490 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002491#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002492 res = mkdir(path);
2493#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002494 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002495#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002496 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002497 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002498 return posix_error_with_allocated_filename(path);
2499 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002500 Py_INCREF(Py_None);
2501 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002502#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002503}
2504
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002505
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002506/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2507#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002508#include <sys/resource.h>
2509#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002510
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002511
2512#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002513PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002514"nice(inc) -> new_priority\n\n\
2515Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002516
Barry Warsaw53699e91996-12-10 23:23:01 +00002517static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002518posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002519{
2520 int increment, value;
2521
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002522 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002523 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002524
2525 /* There are two flavours of 'nice': one that returns the new
2526 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002527 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2528 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002529
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002530 If we are of the nice family that returns the new priority, we
2531 need to clear errno before the call, and check if errno is filled
2532 before calling posix_error() on a returnvalue of -1, because the
2533 -1 may be the actual new priority! */
2534
2535 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002536 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002537#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002538 if (value == 0)
2539 value = getpriority(PRIO_PROCESS, 0);
2540#endif
2541 if (value == -1 && errno != 0)
2542 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002543 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002544 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002545}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002546#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002548PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002549"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551
Barry Warsaw53699e91996-12-10 23:23:01 +00002552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002553posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002554{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002555#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002556 PyObject *o1, *o2;
2557 char *p1, *p2;
2558 BOOL result;
2559 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002560 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2561 goto error;
2562 if (!convert_to_unicode(&o1))
2563 goto error;
2564 if (!convert_to_unicode(&o2)) {
2565 Py_DECREF(o1);
2566 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002567 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002568 Py_BEGIN_ALLOW_THREADS
2569 result = MoveFileW(PyUnicode_AsUnicode(o1),
2570 PyUnicode_AsUnicode(o2));
2571 Py_END_ALLOW_THREADS
2572 Py_DECREF(o1);
2573 Py_DECREF(o2);
2574 if (!result)
2575 return win32_error("rename", NULL);
2576 Py_INCREF(Py_None);
2577 return Py_None;
2578error:
2579 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002580 }
2581 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2582 return NULL;
2583 Py_BEGIN_ALLOW_THREADS
2584 result = MoveFileA(p1, p2);
2585 Py_END_ALLOW_THREADS
2586 if (!result)
2587 return win32_error("rename", NULL);
2588 Py_INCREF(Py_None);
2589 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002590#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002591 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002592#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002593}
2594
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002596PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002597"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002598Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002599
Barry Warsaw53699e91996-12-10 23:23:01 +00002600static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002601posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002602{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002603#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002604 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002605#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002606 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002607#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002608}
2609
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002612"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002613Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002614
Barry Warsaw53699e91996-12-10 23:23:01 +00002615static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002616posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002617{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002618#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002619 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002620#else
2621 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2622#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002623}
2624
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002625
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002626#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002627PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002628"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Barry Warsaw53699e91996-12-10 23:23:01 +00002631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002632posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002633{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002634 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002635#ifdef MS_WINDOWS
2636 wchar_t *command;
2637 if (!PyArg_ParseTuple(args, "u:system", &command))
2638 return NULL;
2639#else
2640 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002641 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002642 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002643#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002644 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002645#ifdef MS_WINDOWS
2646 sts = _wsystem(command);
2647#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002648 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002649#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002650 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002651 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002653#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002654
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002657"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002658Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002659
Barry Warsaw53699e91996-12-10 23:23:01 +00002660static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002661posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002662{
2663 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002664 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002665 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002666 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002667 if (i < 0)
2668 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002669 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002670}
2671
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002673PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002674"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002675Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002677PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002678"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Barry Warsaw53699e91996-12-10 23:23:01 +00002681static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002682posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002683{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002684#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002685 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002686#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002687 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002688#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002689}
2690
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002691
Guido van Rossumb6775db1994-08-01 11:34:53 +00002692#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002693PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002694"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002696
Barry Warsaw53699e91996-12-10 23:23:01 +00002697static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002698posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002699{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002700 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002701 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002702
Barry Warsaw53699e91996-12-10 23:23:01 +00002703 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002704 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002705 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002706 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002707 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002708 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002709 u.sysname,
2710 u.nodename,
2711 u.release,
2712 u.version,
2713 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002714}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002715#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002716
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002717static int
2718extract_time(PyObject *t, long* sec, long* usec)
2719{
2720 long intval;
2721 if (PyFloat_Check(t)) {
2722 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002723 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002724 if (!intobj)
2725 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002726 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002727 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002728 if (intval == -1 && PyErr_Occurred())
2729 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002730 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002731 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002732 if (*usec < 0)
2733 /* If rounding gave us a negative number,
2734 truncate. */
2735 *usec = 0;
2736 return 0;
2737 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002738 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002739 if (intval == -1 && PyErr_Occurred())
2740 return -1;
2741 *sec = intval;
2742 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002743 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002744}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002747"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002748utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002749Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002750second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002751
Barry Warsaw53699e91996-12-10 23:23:01 +00002752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002753posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002754{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002755#ifdef Py_WIN_WIDE_FILENAMES
2756 PyObject *arg;
2757 PyUnicodeObject *obwpath;
2758 wchar_t *wpath = NULL;
2759 char *apath = NULL;
2760 HANDLE hFile;
2761 long atimesec, mtimesec, ausec, musec;
2762 FILETIME atime, mtime;
2763 PyObject *result = NULL;
2764
2765 if (unicode_file_names()) {
2766 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2767 wpath = PyUnicode_AS_UNICODE(obwpath);
2768 Py_BEGIN_ALLOW_THREADS
2769 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002770 NULL, OPEN_EXISTING,
2771 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002772 Py_END_ALLOW_THREADS
2773 if (hFile == INVALID_HANDLE_VALUE)
2774 return win32_error_unicode("utime", wpath);
2775 } else
2776 /* Drop the argument parsing error as narrow strings
2777 are also valid. */
2778 PyErr_Clear();
2779 }
2780 if (!wpath) {
2781 if (!PyArg_ParseTuple(args, "etO:utime",
2782 Py_FileSystemDefaultEncoding, &apath, &arg))
2783 return NULL;
2784 Py_BEGIN_ALLOW_THREADS
2785 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002786 NULL, OPEN_EXISTING,
2787 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002788 Py_END_ALLOW_THREADS
2789 if (hFile == INVALID_HANDLE_VALUE) {
2790 win32_error("utime", apath);
2791 PyMem_Free(apath);
2792 return NULL;
2793 }
2794 PyMem_Free(apath);
2795 }
2796
2797 if (arg == Py_None) {
2798 SYSTEMTIME now;
2799 GetSystemTime(&now);
2800 if (!SystemTimeToFileTime(&now, &mtime) ||
2801 !SystemTimeToFileTime(&now, &atime)) {
2802 win32_error("utime", NULL);
2803 goto done;
2804 }
2805 }
2806 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2807 PyErr_SetString(PyExc_TypeError,
2808 "utime() arg 2 must be a tuple (atime, mtime)");
2809 goto done;
2810 }
2811 else {
2812 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2813 &atimesec, &ausec) == -1)
2814 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002816 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2817 &mtimesec, &musec) == -1)
2818 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002820 }
2821 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2822 /* Avoid putting the file name into the error here,
2823 as that may confuse the user into believing that
2824 something is wrong with the file, when it also
2825 could be the time stamp that gives a problem. */
2826 win32_error("utime", NULL);
2827 }
2828 Py_INCREF(Py_None);
2829 result = Py_None;
2830done:
2831 CloseHandle(hFile);
2832 return result;
2833#else /* Py_WIN_WIDE_FILENAMES */
2834
Neal Norwitz2adf2102004-06-09 01:46:02 +00002835 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002836 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002837 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002838 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002839
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002840#if defined(HAVE_UTIMES)
2841 struct timeval buf[2];
2842#define ATIME buf[0].tv_sec
2843#define MTIME buf[1].tv_sec
2844#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002845/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002846 struct utimbuf buf;
2847#define ATIME buf.actime
2848#define MTIME buf.modtime
2849#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002850#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002851 time_t buf[2];
2852#define ATIME buf[0]
2853#define MTIME buf[1]
2854#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002855#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002856
Mark Hammond817c9292003-12-03 01:22:38 +00002857
Thomas Wouters477c8d52006-05-27 19:21:47 +00002858 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002859 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002860 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002861 if (arg == Py_None) {
2862 /* optional time values not given */
2863 Py_BEGIN_ALLOW_THREADS
2864 res = utime(path, NULL);
2865 Py_END_ALLOW_THREADS
2866 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002867 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002868 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002869 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002870 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002871 return NULL;
2872 }
2873 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002874 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002875 &atime, &ausec) == -1) {
2876 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002877 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002878 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002879 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002880 &mtime, &musec) == -1) {
2881 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002882 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002883 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002884 ATIME = atime;
2885 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002886#ifdef HAVE_UTIMES
2887 buf[0].tv_usec = ausec;
2888 buf[1].tv_usec = musec;
2889 Py_BEGIN_ALLOW_THREADS
2890 res = utimes(path, buf);
2891 Py_END_ALLOW_THREADS
2892#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002893 Py_BEGIN_ALLOW_THREADS
2894 res = utime(path, UTIME_ARG);
2895 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002896#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002897 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002898 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002899 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002900 }
Neal Norwitz96652712004-06-06 20:40:27 +00002901 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002902 Py_INCREF(Py_None);
2903 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002904#undef UTIME_ARG
2905#undef ATIME
2906#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002907#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002908}
2909
Guido van Rossum85e3b011991-06-03 12:42:10 +00002910
Guido van Rossum3b066191991-06-04 19:40:25 +00002911/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002913PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002914"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002916
Barry Warsaw53699e91996-12-10 23:23:01 +00002917static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002918posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002919{
2920 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002921 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002922 return NULL;
2923 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002924 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002925}
2926
Martin v. Löwis114619e2002-10-07 06:44:21 +00002927#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2928static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002929free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002930{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002931 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002932 for (i = 0; i < count; i++)
2933 PyMem_Free(array[i]);
2934 PyMem_DEL(array);
2935}
2936#endif
2937
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002938
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002939#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002941"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002942Execute an executable path with arguments, replacing current process.\n\
2943\n\
2944 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002945 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002946
Barry Warsaw53699e91996-12-10 23:23:01 +00002947static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002948posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002949{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002950 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002951 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002952 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002953 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002954 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002955
Guido van Rossum89b33251993-10-22 14:26:06 +00002956 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002957 argv is a list or tuple of strings. */
2958
Martin v. Löwis114619e2002-10-07 06:44:21 +00002959 if (!PyArg_ParseTuple(args, "etO:execv",
2960 Py_FileSystemDefaultEncoding,
2961 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002962 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002963 if (PyList_Check(argv)) {
2964 argc = PyList_Size(argv);
2965 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002966 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002967 else if (PyTuple_Check(argv)) {
2968 argc = PyTuple_Size(argv);
2969 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002970 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002971 else {
Fred Drake661ea262000-10-24 19:57:45 +00002972 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002973 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002974 return NULL;
2975 }
Thomas Heller6790d602007-08-30 17:15:14 +00002976 if (argc < 1) {
2977 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2978 PyMem_Free(path);
2979 return NULL;
2980 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002981
Barry Warsaw53699e91996-12-10 23:23:01 +00002982 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002983 if (argvlist == NULL) {
2984 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002985 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002986 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002987 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002988 if (!PyArg_Parse((*getitem)(argv, i), "et",
2989 Py_FileSystemDefaultEncoding,
2990 &argvlist[i])) {
2991 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002992 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002993 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002994 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002995 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002996
Guido van Rossum85e3b011991-06-03 12:42:10 +00002997 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002998 }
2999 argvlist[argc] = NULL;
3000
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003001 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003002
Guido van Rossum85e3b011991-06-03 12:42:10 +00003003 /* If we get here it's definitely an error */
3004
Martin v. Löwis114619e2002-10-07 06:44:21 +00003005 free_string_array(argvlist, argc);
3006 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003007 return posix_error();
3008}
3009
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003011PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003012"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003013Execute a path with arguments and environment, replacing current process.\n\
3014\n\
3015 path: path of executable file\n\
3016 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003018
Barry Warsaw53699e91996-12-10 23:23:01 +00003019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003020posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003021{
3022 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003023 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003024 char **argvlist;
3025 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003026 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003027 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003028 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003029 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003030
3031 /* execve has three arguments: (path, argv, env), where
3032 argv is a list or tuple of strings and env is a dictionary
3033 like posix.environ. */
3034
Martin v. Löwis114619e2002-10-07 06:44:21 +00003035 if (!PyArg_ParseTuple(args, "etOO:execve",
3036 Py_FileSystemDefaultEncoding,
3037 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003038 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003039 if (PyList_Check(argv)) {
3040 argc = PyList_Size(argv);
3041 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003042 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003043 else if (PyTuple_Check(argv)) {
3044 argc = PyTuple_Size(argv);
3045 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003046 }
3047 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003048 PyErr_SetString(PyExc_TypeError,
3049 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003050 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003051 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003052 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003053 PyErr_SetString(PyExc_TypeError,
3054 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003055 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003056 }
3057
Barry Warsaw53699e91996-12-10 23:23:01 +00003058 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003059 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003060 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003061 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003062 }
3063 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003064 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003065 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003066 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003067 &argvlist[i]))
3068 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003070 goto fail_1;
3071 }
3072 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003073 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003074 argvlist[argc] = NULL;
3075
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003076 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003077 if (i < 0)
3078 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003079 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003080 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003081 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003082 goto fail_1;
3083 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003084 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003085 keys = PyMapping_Keys(env);
3086 vals = PyMapping_Values(env);
3087 if (!keys || !vals)
3088 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003089 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3090 PyErr_SetString(PyExc_TypeError,
3091 "execve(): env.keys() or env.values() is not a list");
3092 goto fail_2;
3093 }
Tim Peters5aa91602002-01-30 05:46:57 +00003094
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003095 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003096 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003097 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003098
3099 key = PyList_GetItem(keys, pos);
3100 val = PyList_GetItem(vals, pos);
3101 if (!key || !val)
3102 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003103
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003104 if (!PyArg_Parse(
3105 key,
3106 "s;execve() arg 3 contains a non-string key",
3107 &k) ||
3108 !PyArg_Parse(
3109 val,
3110 "s;execve() arg 3 contains a non-string value",
3111 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003112 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003113 goto fail_2;
3114 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003115
3116#if defined(PYOS_OS2)
3117 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3118 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3119#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003120 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003121 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003122 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003123 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003124 goto fail_2;
3125 }
Tim Petersc8996f52001-12-03 20:41:00 +00003126 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003127 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003128#if defined(PYOS_OS2)
3129 }
3130#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003131 }
3132 envlist[envc] = 0;
3133
3134 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003135
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003136 /* If we get here it's definitely an error */
3137
3138 (void) posix_error();
3139
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003140 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003141 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003142 PyMem_DEL(envlist[envc]);
3143 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 fail_1:
3145 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003146 Py_XDECREF(vals);
3147 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003148 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003149 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003150 return NULL;
3151}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003152#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003154
Guido van Rossuma1065681999-01-25 23:20:23 +00003155#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003157"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003158Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003159\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003160 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003161 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003162 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003163
3164static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003165posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003166{
3167 char *path;
3168 PyObject *argv;
3169 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003170 int mode, i;
3171 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003172 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003173 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003174
3175 /* spawnv has three arguments: (mode, path, argv), where
3176 argv is a list or tuple of strings. */
3177
Martin v. Löwis114619e2002-10-07 06:44:21 +00003178 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3179 Py_FileSystemDefaultEncoding,
3180 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003181 return NULL;
3182 if (PyList_Check(argv)) {
3183 argc = PyList_Size(argv);
3184 getitem = PyList_GetItem;
3185 }
3186 else if (PyTuple_Check(argv)) {
3187 argc = PyTuple_Size(argv);
3188 getitem = PyTuple_GetItem;
3189 }
3190 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003191 PyErr_SetString(PyExc_TypeError,
3192 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003193 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003194 return NULL;
3195 }
3196
3197 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003198 if (argvlist == NULL) {
3199 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003200 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003201 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003202 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003203 if (!PyArg_Parse((*getitem)(argv, i), "et",
3204 Py_FileSystemDefaultEncoding,
3205 &argvlist[i])) {
3206 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003207 PyErr_SetString(
3208 PyExc_TypeError,
3209 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003210 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003211 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003212 }
3213 }
3214 argvlist[argc] = NULL;
3215
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003216#if defined(PYOS_OS2) && defined(PYCC_GCC)
3217 Py_BEGIN_ALLOW_THREADS
3218 spawnval = spawnv(mode, path, argvlist);
3219 Py_END_ALLOW_THREADS
3220#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003221 if (mode == _OLD_P_OVERLAY)
3222 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003223
Tim Peters25059d32001-12-07 20:35:43 +00003224 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003225 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003226 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003227#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003228
Martin v. Löwis114619e2002-10-07 06:44:21 +00003229 free_string_array(argvlist, argc);
3230 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003231
Fred Drake699f3522000-06-29 21:12:41 +00003232 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003233 return posix_error();
3234 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003235#if SIZEOF_LONG == SIZEOF_VOID_P
3236 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003237#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003238 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003239#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003240}
3241
3242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003243PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003244"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003245Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003246\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003247 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003248 path: path of executable file\n\
3249 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003250 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003251
3252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003253posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003254{
3255 char *path;
3256 PyObject *argv, *env;
3257 char **argvlist;
3258 char **envlist;
3259 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003260 int mode, pos, envc;
3261 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003262 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003263 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003264 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003265
3266 /* spawnve has four arguments: (mode, path, argv, env), where
3267 argv is a list or tuple of strings and env is a dictionary
3268 like posix.environ. */
3269
Martin v. Löwis114619e2002-10-07 06:44:21 +00003270 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3271 Py_FileSystemDefaultEncoding,
3272 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003273 return NULL;
3274 if (PyList_Check(argv)) {
3275 argc = PyList_Size(argv);
3276 getitem = PyList_GetItem;
3277 }
3278 else if (PyTuple_Check(argv)) {
3279 argc = PyTuple_Size(argv);
3280 getitem = PyTuple_GetItem;
3281 }
3282 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003283 PyErr_SetString(PyExc_TypeError,
3284 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003285 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003286 }
3287 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003288 PyErr_SetString(PyExc_TypeError,
3289 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003290 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003291 }
3292
3293 argvlist = PyMem_NEW(char *, argc+1);
3294 if (argvlist == NULL) {
3295 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003296 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003297 }
3298 for (i = 0; i < argc; i++) {
3299 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003300 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003301 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003302 &argvlist[i]))
3303 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003304 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003305 goto fail_1;
3306 }
3307 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003308 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003309 argvlist[argc] = NULL;
3310
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003311 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003312 if (i < 0)
3313 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003314 envlist = PyMem_NEW(char *, i + 1);
3315 if (envlist == NULL) {
3316 PyErr_NoMemory();
3317 goto fail_1;
3318 }
3319 envc = 0;
3320 keys = PyMapping_Keys(env);
3321 vals = PyMapping_Values(env);
3322 if (!keys || !vals)
3323 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003324 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3325 PyErr_SetString(PyExc_TypeError,
3326 "spawnve(): env.keys() or env.values() is not a list");
3327 goto fail_2;
3328 }
Tim Peters5aa91602002-01-30 05:46:57 +00003329
Guido van Rossuma1065681999-01-25 23:20:23 +00003330 for (pos = 0; pos < i; pos++) {
3331 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003332 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003333
3334 key = PyList_GetItem(keys, pos);
3335 val = PyList_GetItem(vals, pos);
3336 if (!key || !val)
3337 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003338
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003339 if (!PyArg_Parse(
3340 key,
3341 "s;spawnve() arg 3 contains a non-string key",
3342 &k) ||
3343 !PyArg_Parse(
3344 val,
3345 "s;spawnve() arg 3 contains a non-string value",
3346 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003347 {
3348 goto fail_2;
3349 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003350 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003351 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003352 if (p == NULL) {
3353 PyErr_NoMemory();
3354 goto fail_2;
3355 }
Tim Petersc8996f52001-12-03 20:41:00 +00003356 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003357 envlist[envc++] = p;
3358 }
3359 envlist[envc] = 0;
3360
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003361#if defined(PYOS_OS2) && defined(PYCC_GCC)
3362 Py_BEGIN_ALLOW_THREADS
3363 spawnval = spawnve(mode, path, argvlist, envlist);
3364 Py_END_ALLOW_THREADS
3365#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003366 if (mode == _OLD_P_OVERLAY)
3367 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003368
3369 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003370 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003371 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003372#endif
Tim Peters25059d32001-12-07 20:35:43 +00003373
Fred Drake699f3522000-06-29 21:12:41 +00003374 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003375 (void) posix_error();
3376 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003377#if SIZEOF_LONG == SIZEOF_VOID_P
3378 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003379#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003380 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003381#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003382
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003383 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 while (--envc >= 0)
3385 PyMem_DEL(envlist[envc]);
3386 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003387 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003388 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003389 Py_XDECREF(vals);
3390 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003391 fail_0:
3392 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003393 return res;
3394}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003395
3396/* OS/2 supports spawnvp & spawnvpe natively */
3397#if defined(PYOS_OS2)
3398PyDoc_STRVAR(posix_spawnvp__doc__,
3399"spawnvp(mode, file, args)\n\n\
3400Execute the program 'file' in a new process, using the environment\n\
3401search path to find the file.\n\
3402\n\
3403 mode: mode of process creation\n\
3404 file: executable file name\n\
3405 args: tuple or list of strings");
3406
3407static PyObject *
3408posix_spawnvp(PyObject *self, PyObject *args)
3409{
3410 char *path;
3411 PyObject *argv;
3412 char **argvlist;
3413 int mode, i, argc;
3414 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003415 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003416
3417 /* spawnvp has three arguments: (mode, path, argv), where
3418 argv is a list or tuple of strings. */
3419
3420 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3421 Py_FileSystemDefaultEncoding,
3422 &path, &argv))
3423 return NULL;
3424 if (PyList_Check(argv)) {
3425 argc = PyList_Size(argv);
3426 getitem = PyList_GetItem;
3427 }
3428 else if (PyTuple_Check(argv)) {
3429 argc = PyTuple_Size(argv);
3430 getitem = PyTuple_GetItem;
3431 }
3432 else {
3433 PyErr_SetString(PyExc_TypeError,
3434 "spawnvp() arg 2 must be a tuple or list");
3435 PyMem_Free(path);
3436 return NULL;
3437 }
3438
3439 argvlist = PyMem_NEW(char *, argc+1);
3440 if (argvlist == NULL) {
3441 PyMem_Free(path);
3442 return PyErr_NoMemory();
3443 }
3444 for (i = 0; i < argc; i++) {
3445 if (!PyArg_Parse((*getitem)(argv, i), "et",
3446 Py_FileSystemDefaultEncoding,
3447 &argvlist[i])) {
3448 free_string_array(argvlist, i);
3449 PyErr_SetString(
3450 PyExc_TypeError,
3451 "spawnvp() arg 2 must contain only strings");
3452 PyMem_Free(path);
3453 return NULL;
3454 }
3455 }
3456 argvlist[argc] = NULL;
3457
3458 Py_BEGIN_ALLOW_THREADS
3459#if defined(PYCC_GCC)
3460 spawnval = spawnvp(mode, path, argvlist);
3461#else
3462 spawnval = _spawnvp(mode, path, argvlist);
3463#endif
3464 Py_END_ALLOW_THREADS
3465
3466 free_string_array(argvlist, argc);
3467 PyMem_Free(path);
3468
3469 if (spawnval == -1)
3470 return posix_error();
3471 else
3472 return Py_BuildValue("l", (long) spawnval);
3473}
3474
3475
3476PyDoc_STRVAR(posix_spawnvpe__doc__,
3477"spawnvpe(mode, file, args, env)\n\n\
3478Execute the program 'file' in a new process, using the environment\n\
3479search path to find the file.\n\
3480\n\
3481 mode: mode of process creation\n\
3482 file: executable file name\n\
3483 args: tuple or list of arguments\n\
3484 env: dictionary of strings mapping to strings");
3485
3486static PyObject *
3487posix_spawnvpe(PyObject *self, PyObject *args)
3488{
3489 char *path;
3490 PyObject *argv, *env;
3491 char **argvlist;
3492 char **envlist;
3493 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3494 int mode, i, pos, argc, envc;
3495 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003496 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003497 int lastarg = 0;
3498
3499 /* spawnvpe has four arguments: (mode, path, argv, env), where
3500 argv is a list or tuple of strings and env is a dictionary
3501 like posix.environ. */
3502
3503 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3504 Py_FileSystemDefaultEncoding,
3505 &path, &argv, &env))
3506 return NULL;
3507 if (PyList_Check(argv)) {
3508 argc = PyList_Size(argv);
3509 getitem = PyList_GetItem;
3510 }
3511 else if (PyTuple_Check(argv)) {
3512 argc = PyTuple_Size(argv);
3513 getitem = PyTuple_GetItem;
3514 }
3515 else {
3516 PyErr_SetString(PyExc_TypeError,
3517 "spawnvpe() arg 2 must be a tuple or list");
3518 goto fail_0;
3519 }
3520 if (!PyMapping_Check(env)) {
3521 PyErr_SetString(PyExc_TypeError,
3522 "spawnvpe() arg 3 must be a mapping object");
3523 goto fail_0;
3524 }
3525
3526 argvlist = PyMem_NEW(char *, argc+1);
3527 if (argvlist == NULL) {
3528 PyErr_NoMemory();
3529 goto fail_0;
3530 }
3531 for (i = 0; i < argc; i++) {
3532 if (!PyArg_Parse((*getitem)(argv, i),
3533 "et;spawnvpe() arg 2 must contain only strings",
3534 Py_FileSystemDefaultEncoding,
3535 &argvlist[i]))
3536 {
3537 lastarg = i;
3538 goto fail_1;
3539 }
3540 }
3541 lastarg = argc;
3542 argvlist[argc] = NULL;
3543
3544 i = PyMapping_Size(env);
3545 if (i < 0)
3546 goto fail_1;
3547 envlist = PyMem_NEW(char *, i + 1);
3548 if (envlist == NULL) {
3549 PyErr_NoMemory();
3550 goto fail_1;
3551 }
3552 envc = 0;
3553 keys = PyMapping_Keys(env);
3554 vals = PyMapping_Values(env);
3555 if (!keys || !vals)
3556 goto fail_2;
3557 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3558 PyErr_SetString(PyExc_TypeError,
3559 "spawnvpe(): env.keys() or env.values() is not a list");
3560 goto fail_2;
3561 }
3562
3563 for (pos = 0; pos < i; pos++) {
3564 char *p, *k, *v;
3565 size_t len;
3566
3567 key = PyList_GetItem(keys, pos);
3568 val = PyList_GetItem(vals, pos);
3569 if (!key || !val)
3570 goto fail_2;
3571
3572 if (!PyArg_Parse(
3573 key,
3574 "s;spawnvpe() arg 3 contains a non-string key",
3575 &k) ||
3576 !PyArg_Parse(
3577 val,
3578 "s;spawnvpe() arg 3 contains a non-string value",
3579 &v))
3580 {
3581 goto fail_2;
3582 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003583 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003584 p = PyMem_NEW(char, len);
3585 if (p == NULL) {
3586 PyErr_NoMemory();
3587 goto fail_2;
3588 }
3589 PyOS_snprintf(p, len, "%s=%s", k, v);
3590 envlist[envc++] = p;
3591 }
3592 envlist[envc] = 0;
3593
3594 Py_BEGIN_ALLOW_THREADS
3595#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003596 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003597#else
Christian Heimes292d3512008-02-03 16:51:08 +00003598 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003599#endif
3600 Py_END_ALLOW_THREADS
3601
3602 if (spawnval == -1)
3603 (void) posix_error();
3604 else
3605 res = Py_BuildValue("l", (long) spawnval);
3606
3607 fail_2:
3608 while (--envc >= 0)
3609 PyMem_DEL(envlist[envc]);
3610 PyMem_DEL(envlist);
3611 fail_1:
3612 free_string_array(argvlist, lastarg);
3613 Py_XDECREF(vals);
3614 Py_XDECREF(keys);
3615 fail_0:
3616 PyMem_Free(path);
3617 return res;
3618}
3619#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003620#endif /* HAVE_SPAWNV */
3621
3622
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003623#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003624PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003625"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003626Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3627\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003628Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003629
3630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003631posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003632{
Christian Heimes400adb02008-02-01 08:12:03 +00003633 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003634 if (pid == -1)
3635 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003636 if (pid == 0)
3637 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003638 return PyLong_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003639}
3640#endif
3641
3642
Guido van Rossumad0ee831995-03-01 10:34:45 +00003643#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003644PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003645"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003646Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003647Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003648
Barry Warsaw53699e91996-12-10 23:23:01 +00003649static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003650posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003651{
Christian Heimes400adb02008-02-01 08:12:03 +00003652 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003653 if (pid == -1)
3654 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003655 if (pid == 0)
3656 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003657 return PyLong_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003658}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003659#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003660
Neal Norwitzb59798b2003-03-21 01:43:31 +00003661/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003662/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3663#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003664#define DEV_PTY_FILE "/dev/ptc"
3665#define HAVE_DEV_PTMX
3666#else
3667#define DEV_PTY_FILE "/dev/ptmx"
3668#endif
3669
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003670#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003671#ifdef HAVE_PTY_H
3672#include <pty.h>
3673#else
3674#ifdef HAVE_LIBUTIL_H
3675#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003676#endif /* HAVE_LIBUTIL_H */
3677#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003678#ifdef HAVE_STROPTS_H
3679#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003680#endif
3681#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003682
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003683#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003684PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003685"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003686Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003687
3688static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003689posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003690{
3691 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003692#ifndef HAVE_OPENPTY
3693 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003694#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003695#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003696 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003697#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003698 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003699#endif
3700#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003701
Thomas Wouters70c21a12000-07-14 14:28:33 +00003702#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003703 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3704 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003705#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003706 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3707 if (slave_name == NULL)
3708 return posix_error();
3709
3710 slave_fd = open(slave_name, O_RDWR);
3711 if (slave_fd < 0)
3712 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003713#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003714 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003715 if (master_fd < 0)
3716 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003717 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003718 /* change permission of slave */
3719 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003720 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003721 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003722 }
3723 /* unlock slave */
3724 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003725 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003726 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003727 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003728 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003729 slave_name = ptsname(master_fd); /* get name of slave */
3730 if (slave_name == NULL)
3731 return posix_error();
3732 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3733 if (slave_fd < 0)
3734 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003735#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003736 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3737 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003738#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003739 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003740#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003741#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003742#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003743
Fred Drake8cef4cf2000-06-28 16:40:38 +00003744 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003745
Fred Drake8cef4cf2000-06-28 16:40:38 +00003746}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003747#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003748
3749#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003750PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003751"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003752Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3753Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003754To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003755
3756static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003757posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003758{
Christian Heimes400adb02008-02-01 08:12:03 +00003759 int master_fd = -1;
3760 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003761
Fred Drake8cef4cf2000-06-28 16:40:38 +00003762 pid = forkpty(&master_fd, NULL, NULL, NULL);
3763 if (pid == -1)
3764 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003765 if (pid == 0)
3766 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003767 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003768}
3769#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003770
Guido van Rossumad0ee831995-03-01 10:34:45 +00003771#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003772PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003773"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003774Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003775
Barry Warsaw53699e91996-12-10 23:23:01 +00003776static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003777posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003778{
Christian Heimes217cfd12007-12-02 14:31:20 +00003779 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003780}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003781#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003782
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003783
Guido van Rossumad0ee831995-03-01 10:34:45 +00003784#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003785PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003786"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003788
Barry Warsaw53699e91996-12-10 23:23:01 +00003789static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003790posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003791{
Christian Heimes217cfd12007-12-02 14:31:20 +00003792 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003793}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003794#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003796
Guido van Rossumad0ee831995-03-01 10:34:45 +00003797#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003798PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003799"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003800Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003801
Barry Warsaw53699e91996-12-10 23:23:01 +00003802static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003803posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003804{
Christian Heimes217cfd12007-12-02 14:31:20 +00003805 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003806}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003807#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003808
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003810PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003811"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003812Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003813
Barry Warsaw53699e91996-12-10 23:23:01 +00003814static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003815posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003816{
Christian Heimes217cfd12007-12-02 14:31:20 +00003817 return PyLong_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003818}
3819
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003820
Fred Drakec9680921999-12-13 16:37:25 +00003821#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003822PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003823"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003825
3826static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003827posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003828{
3829 PyObject *result = NULL;
3830
Fred Drakec9680921999-12-13 16:37:25 +00003831#ifdef NGROUPS_MAX
3832#define MAX_GROUPS NGROUPS_MAX
3833#else
3834 /* defined to be 16 on Solaris7, so this should be a small number */
3835#define MAX_GROUPS 64
3836#endif
3837 gid_t grouplist[MAX_GROUPS];
3838 int n;
3839
3840 n = getgroups(MAX_GROUPS, grouplist);
3841 if (n < 0)
3842 posix_error();
3843 else {
3844 result = PyList_New(n);
3845 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003846 int i;
3847 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003848 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003849 if (o == NULL) {
3850 Py_DECREF(result);
3851 result = NULL;
3852 break;
3853 }
3854 PyList_SET_ITEM(result, i, o);
3855 }
3856 }
3857 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003858
Fred Drakec9680921999-12-13 16:37:25 +00003859 return result;
3860}
3861#endif
3862
Martin v. Löwis606edc12002-06-13 21:09:11 +00003863#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003864PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003865"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003866Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003867
3868static PyObject *
3869posix_getpgid(PyObject *self, PyObject *args)
3870{
3871 int pid, pgid;
3872 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3873 return NULL;
3874 pgid = getpgid(pid);
3875 if (pgid < 0)
3876 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00003877 return PyLong_FromLong((long)pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003878}
3879#endif /* HAVE_GETPGID */
3880
3881
Guido van Rossumb6775db1994-08-01 11:34:53 +00003882#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003883PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003884"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003885Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003886
Barry Warsaw53699e91996-12-10 23:23:01 +00003887static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003888posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003889{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003890#ifdef GETPGRP_HAVE_ARG
Christian Heimes217cfd12007-12-02 14:31:20 +00003891 return PyLong_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003892#else /* GETPGRP_HAVE_ARG */
Christian Heimes217cfd12007-12-02 14:31:20 +00003893 return PyLong_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003894#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003895}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003896#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003898
Guido van Rossumb6775db1994-08-01 11:34:53 +00003899#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003900PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003901"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003902Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003903
Barry Warsaw53699e91996-12-10 23:23:01 +00003904static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003905posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003906{
Guido van Rossum64933891994-10-20 21:56:42 +00003907#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003908 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003909#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003910 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003911#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003912 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003913 Py_INCREF(Py_None);
3914 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003915}
3916
Guido van Rossumb6775db1994-08-01 11:34:53 +00003917#endif /* HAVE_SETPGRP */
3918
Guido van Rossumad0ee831995-03-01 10:34:45 +00003919#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003920PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003921"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003922Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003923
Barry Warsaw53699e91996-12-10 23:23:01 +00003924static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003925posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003926{
Christian Heimes217cfd12007-12-02 14:31:20 +00003927 return PyLong_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003928}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003929#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003930
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003931
Fred Drake12c6e2d1999-12-14 21:25:03 +00003932#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003933PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003934"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003935Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003936
3937static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003938posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003939{
Neal Norwitze241ce82003-02-17 18:17:05 +00003940 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003941 char *name;
3942 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003943
Fred Drakea30680b2000-12-06 21:24:28 +00003944 errno = 0;
3945 name = getlogin();
3946 if (name == NULL) {
3947 if (errno)
3948 posix_error();
3949 else
3950 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003951 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003952 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003953 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003954 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003955 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003956
Fred Drake12c6e2d1999-12-14 21:25:03 +00003957 return result;
3958}
3959#endif
3960
Guido van Rossumad0ee831995-03-01 10:34:45 +00003961#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003962PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003963"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003964Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003965
Barry Warsaw53699e91996-12-10 23:23:01 +00003966static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003967posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003968{
Christian Heimes217cfd12007-12-02 14:31:20 +00003969 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003970}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003971#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003972
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003973
Guido van Rossumad0ee831995-03-01 10:34:45 +00003974#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003975PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003976"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003977Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003978
Barry Warsaw53699e91996-12-10 23:23:01 +00003979static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003980posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003981{
Christian Heimes292d3512008-02-03 16:51:08 +00003982 pid_t pid;
3983 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003984 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003985 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003986#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003987 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3988 APIRET rc;
3989 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003990 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003991
3992 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3993 APIRET rc;
3994 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003995 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003996
3997 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003998 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003999#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004000 if (kill(pid, sig) == -1)
4001 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004002#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004003 Py_INCREF(Py_None);
4004 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004005}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004006#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004007
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004008#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004009PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004010"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004011Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004012
4013static PyObject *
4014posix_killpg(PyObject *self, PyObject *args)
4015{
4016 int pgid, sig;
4017 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4018 return NULL;
4019 if (killpg(pgid, sig) == -1)
4020 return posix_error();
4021 Py_INCREF(Py_None);
4022 return Py_None;
4023}
4024#endif
4025
Guido van Rossumc0125471996-06-28 18:55:32 +00004026#ifdef HAVE_PLOCK
4027
4028#ifdef HAVE_SYS_LOCK_H
4029#include <sys/lock.h>
4030#endif
4031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004032PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004033"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004034Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004035
Barry Warsaw53699e91996-12-10 23:23:01 +00004036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004037posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004038{
4039 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004040 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004041 return NULL;
4042 if (plock(op) == -1)
4043 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004044 Py_INCREF(Py_None);
4045 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004046}
4047#endif
4048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004049
Guido van Rossum3b066191991-06-04 19:40:25 +00004050
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004051
Guido van Rossumb6775db1994-08-01 11:34:53 +00004052#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004053PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004054"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004055Set the current process's user id.");
4056
Barry Warsaw53699e91996-12-10 23:23:01 +00004057static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004058posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004059{
4060 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004061 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004062 return NULL;
4063 if (setuid(uid) < 0)
4064 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004065 Py_INCREF(Py_None);
4066 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004067}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004068#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004069
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004070
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004071#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004072PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004073"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004074Set the current process's effective user id.");
4075
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004076static PyObject *
4077posix_seteuid (PyObject *self, PyObject *args)
4078{
4079 int euid;
4080 if (!PyArg_ParseTuple(args, "i", &euid)) {
4081 return NULL;
4082 } else if (seteuid(euid) < 0) {
4083 return posix_error();
4084 } else {
4085 Py_INCREF(Py_None);
4086 return Py_None;
4087 }
4088}
4089#endif /* HAVE_SETEUID */
4090
4091#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004092PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004093"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004094Set the current process's effective group id.");
4095
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004096static PyObject *
4097posix_setegid (PyObject *self, PyObject *args)
4098{
4099 int egid;
4100 if (!PyArg_ParseTuple(args, "i", &egid)) {
4101 return NULL;
4102 } else if (setegid(egid) < 0) {
4103 return posix_error();
4104 } else {
4105 Py_INCREF(Py_None);
4106 return Py_None;
4107 }
4108}
4109#endif /* HAVE_SETEGID */
4110
4111#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004112PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004113"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004114Set the current process's real and effective user ids.");
4115
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004116static PyObject *
4117posix_setreuid (PyObject *self, PyObject *args)
4118{
4119 int ruid, euid;
4120 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4121 return NULL;
4122 } else if (setreuid(ruid, euid) < 0) {
4123 return posix_error();
4124 } else {
4125 Py_INCREF(Py_None);
4126 return Py_None;
4127 }
4128}
4129#endif /* HAVE_SETREUID */
4130
4131#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004132PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004133"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004134Set the current process's real and effective group ids.");
4135
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004136static PyObject *
4137posix_setregid (PyObject *self, PyObject *args)
4138{
4139 int rgid, egid;
4140 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4141 return NULL;
4142 } else if (setregid(rgid, egid) < 0) {
4143 return posix_error();
4144 } else {
4145 Py_INCREF(Py_None);
4146 return Py_None;
4147 }
4148}
4149#endif /* HAVE_SETREGID */
4150
Guido van Rossumb6775db1994-08-01 11:34:53 +00004151#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004152PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004153"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004154Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004155
Barry Warsaw53699e91996-12-10 23:23:01 +00004156static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004157posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004158{
4159 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004160 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004161 return NULL;
4162 if (setgid(gid) < 0)
4163 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004164 Py_INCREF(Py_None);
4165 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004166}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004167#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004168
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004169#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004170PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004171"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004172Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004173
4174static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004175posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004176{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004177 int i, len;
4178 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004179
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004180 if (!PySequence_Check(groups)) {
4181 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4182 return NULL;
4183 }
4184 len = PySequence_Size(groups);
4185 if (len > MAX_GROUPS) {
4186 PyErr_SetString(PyExc_ValueError, "too many groups");
4187 return NULL;
4188 }
4189 for(i = 0; i < len; i++) {
4190 PyObject *elem;
4191 elem = PySequence_GetItem(groups, i);
4192 if (!elem)
4193 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004194 if (!PyLong_Check(elem)) {
4195 PyErr_SetString(PyExc_TypeError,
4196 "groups must be integers");
4197 Py_DECREF(elem);
4198 return NULL;
4199 } else {
4200 unsigned long x = PyLong_AsUnsignedLong(elem);
4201 if (PyErr_Occurred()) {
4202 PyErr_SetString(PyExc_TypeError,
4203 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004204 Py_DECREF(elem);
4205 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004206 }
Georg Brandla13c2442005-11-22 19:30:31 +00004207 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004208 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004209 if (grouplist[i] != x) {
4210 PyErr_SetString(PyExc_TypeError,
4211 "group id too big");
4212 Py_DECREF(elem);
4213 return NULL;
4214 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004215 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004216 Py_DECREF(elem);
4217 }
4218
4219 if (setgroups(len, grouplist) < 0)
4220 return posix_error();
4221 Py_INCREF(Py_None);
4222 return Py_None;
4223}
4224#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004225
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004226#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4227static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004228wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004229{
4230 PyObject *result;
4231 static PyObject *struct_rusage;
4232
4233 if (pid == -1)
4234 return posix_error();
4235
4236 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004237 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004238 if (m == NULL)
4239 return NULL;
4240 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4241 Py_DECREF(m);
4242 if (struct_rusage == NULL)
4243 return NULL;
4244 }
4245
4246 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4247 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4248 if (!result)
4249 return NULL;
4250
4251#ifndef doubletime
4252#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4253#endif
4254
4255 PyStructSequence_SET_ITEM(result, 0,
4256 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4257 PyStructSequence_SET_ITEM(result, 1,
4258 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4259#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004260 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004261 SET_INT(result, 2, ru->ru_maxrss);
4262 SET_INT(result, 3, ru->ru_ixrss);
4263 SET_INT(result, 4, ru->ru_idrss);
4264 SET_INT(result, 5, ru->ru_isrss);
4265 SET_INT(result, 6, ru->ru_minflt);
4266 SET_INT(result, 7, ru->ru_majflt);
4267 SET_INT(result, 8, ru->ru_nswap);
4268 SET_INT(result, 9, ru->ru_inblock);
4269 SET_INT(result, 10, ru->ru_oublock);
4270 SET_INT(result, 11, ru->ru_msgsnd);
4271 SET_INT(result, 12, ru->ru_msgrcv);
4272 SET_INT(result, 13, ru->ru_nsignals);
4273 SET_INT(result, 14, ru->ru_nvcsw);
4274 SET_INT(result, 15, ru->ru_nivcsw);
4275#undef SET_INT
4276
4277 if (PyErr_Occurred()) {
4278 Py_DECREF(result);
4279 return NULL;
4280 }
4281
4282 return Py_BuildValue("iiN", pid, status, result);
4283}
4284#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4285
4286#ifdef HAVE_WAIT3
4287PyDoc_STRVAR(posix_wait3__doc__,
4288"wait3(options) -> (pid, status, rusage)\n\n\
4289Wait for completion of a child process.");
4290
4291static PyObject *
4292posix_wait3(PyObject *self, PyObject *args)
4293{
Christian Heimes292d3512008-02-03 16:51:08 +00004294 pid_t pid;
4295 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004296 struct rusage ru;
4297 WAIT_TYPE status;
4298 WAIT_STATUS_INT(status) = 0;
4299
4300 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4301 return NULL;
4302
4303 Py_BEGIN_ALLOW_THREADS
4304 pid = wait3(&status, options, &ru);
4305 Py_END_ALLOW_THREADS
4306
4307 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4308}
4309#endif /* HAVE_WAIT3 */
4310
4311#ifdef HAVE_WAIT4
4312PyDoc_STRVAR(posix_wait4__doc__,
4313"wait4(pid, options) -> (pid, status, rusage)\n\n\
4314Wait for completion of a given child process.");
4315
4316static PyObject *
4317posix_wait4(PyObject *self, PyObject *args)
4318{
Christian Heimes292d3512008-02-03 16:51:08 +00004319 pid_t pid;
4320 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004321 struct rusage ru;
4322 WAIT_TYPE status;
4323 WAIT_STATUS_INT(status) = 0;
4324
4325 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4326 return NULL;
4327
4328 Py_BEGIN_ALLOW_THREADS
4329 pid = wait4(pid, &status, options, &ru);
4330 Py_END_ALLOW_THREADS
4331
4332 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4333}
4334#endif /* HAVE_WAIT4 */
4335
Guido van Rossumb6775db1994-08-01 11:34:53 +00004336#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004337PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004338"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004339Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004340
Barry Warsaw53699e91996-12-10 23:23:01 +00004341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004342posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004343{
Christian Heimes292d3512008-02-03 16:51:08 +00004344 pid_t pid;
4345 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004346 WAIT_TYPE status;
4347 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004348
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004349 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004350 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004351 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004352 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004353 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004354 if (pid == -1)
4355 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004356
4357 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004358}
4359
Tim Petersab034fa2002-02-01 11:27:43 +00004360#elif defined(HAVE_CWAIT)
4361
4362/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004363PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004364"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004365"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004366
4367static PyObject *
4368posix_waitpid(PyObject *self, PyObject *args)
4369{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004370 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004371 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004372
4373 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4374 return NULL;
4375 Py_BEGIN_ALLOW_THREADS
4376 pid = _cwait(&status, pid, options);
4377 Py_END_ALLOW_THREADS
4378 if (pid == -1)
4379 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004380
4381 /* shift the status left a byte so this is more like the POSIX waitpid */
4382 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004383}
4384#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004385
Guido van Rossumad0ee831995-03-01 10:34:45 +00004386#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004387PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004388"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004389Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004390
Barry Warsaw53699e91996-12-10 23:23:01 +00004391static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004392posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004393{
Christian Heimes292d3512008-02-03 16:51:08 +00004394 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004395 WAIT_TYPE status;
4396 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004397
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004398 Py_BEGIN_ALLOW_THREADS
4399 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004400 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004401 if (pid == -1)
4402 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004403
4404 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004405}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004406#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004407
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004409PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004410"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004411Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004412
Barry Warsaw53699e91996-12-10 23:23:01 +00004413static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004414posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004415{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004416#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004417 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004418#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004419#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004420 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004421#else
4422 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4423#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004424#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004425}
4426
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004427
Guido van Rossumb6775db1994-08-01 11:34:53 +00004428#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004429PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004430"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004432
Barry Warsaw53699e91996-12-10 23:23:01 +00004433static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004434posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004435{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004436 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004437 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004438 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004439 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004440 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004441
4442 if (!PyArg_ParseTuple(args, "et:readlink",
4443 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004444 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004445 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004446 if (v == NULL) {
4447 PyMem_Free(path);
4448 return NULL;
4449 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004450
4451 if (PyUnicode_Check(v)) {
4452 arg_is_unicode = 1;
4453 }
4454 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004455
Barry Warsaw53699e91996-12-10 23:23:01 +00004456 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004457 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004458 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004459 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004460 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004461
Neal Norwitzfca70052007-08-12 16:56:02 +00004462 PyMem_Free(path);
Christian Heimes72b710a2008-05-26 13:28:38 +00004463 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004464 if (arg_is_unicode) {
4465 PyObject *w;
4466
4467 w = PyUnicode_FromEncodedObject(v,
4468 Py_FileSystemDefaultEncoding,
4469 "strict");
4470 if (w != NULL) {
4471 Py_DECREF(v);
4472 v = w;
4473 }
4474 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004475 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004476 }
4477 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004478 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004479}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004480#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004481
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004482
Guido van Rossumb6775db1994-08-01 11:34:53 +00004483#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004484PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004485"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004486Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004487
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004488static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004489posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004490{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004491 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004492}
4493#endif /* HAVE_SYMLINK */
4494
4495
4496#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004497#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4498static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004499system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004500{
4501 ULONG value = 0;
4502
4503 Py_BEGIN_ALLOW_THREADS
4504 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4505 Py_END_ALLOW_THREADS
4506
4507 return value;
4508}
4509
4510static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004511posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004512{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004513 /* Currently Only Uptime is Provided -- Others Later */
4514 return Py_BuildValue("ddddd",
4515 (double)0 /* t.tms_utime / HZ */,
4516 (double)0 /* t.tms_stime / HZ */,
4517 (double)0 /* t.tms_cutime / HZ */,
4518 (double)0 /* t.tms_cstime / HZ */,
4519 (double)system_uptime() / 1000);
4520}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004521#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004522#define NEED_TICKS_PER_SECOND
4523static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004524static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004525posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004526{
4527 struct tms t;
4528 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004529 errno = 0;
4530 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004531 if (c == (clock_t) -1)
4532 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004533 return Py_BuildValue("ddddd",
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004534 (double)t.tms_utime / ticks_per_second,
4535 (double)t.tms_stime / ticks_per_second,
4536 (double)t.tms_cutime / ticks_per_second,
4537 (double)t.tms_cstime / ticks_per_second,
4538 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004539}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004540#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004541#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004542
4543
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004544#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004545#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004546static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004547posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004548{
4549 FILETIME create, exit, kernel, user;
4550 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004551 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004552 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4553 /* The fields of a FILETIME structure are the hi and lo part
4554 of a 64-bit value expressed in 100 nanosecond units.
4555 1e7 is one second in such units; 1e-7 the inverse.
4556 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4557 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004558 return Py_BuildValue(
4559 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004560 (double)(user.dwHighDateTime*429.4967296 +
4561 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004562 (double)(kernel.dwHighDateTime*429.4967296 +
4563 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004564 (double)0,
4565 (double)0,
4566 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004567}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004568#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004569
4570#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004571PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004572"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004573Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004574#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004575
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004576
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004577#ifdef HAVE_GETSID
4578PyDoc_STRVAR(posix_getsid__doc__,
4579"getsid(pid) -> sid\n\n\
4580Call the system call getsid().");
4581
4582static PyObject *
4583posix_getsid(PyObject *self, PyObject *args)
4584{
Christian Heimes292d3512008-02-03 16:51:08 +00004585 pid_t pid;
4586 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004587 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4588 return NULL;
4589 sid = getsid(pid);
4590 if (sid < 0)
4591 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004592 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004593}
4594#endif /* HAVE_GETSID */
4595
4596
Guido van Rossumb6775db1994-08-01 11:34:53 +00004597#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004598PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004599"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004600Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004601
Barry Warsaw53699e91996-12-10 23:23:01 +00004602static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004603posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004604{
Guido van Rossum687dd131993-05-17 08:34:16 +00004605 if (setsid() < 0)
4606 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004607 Py_INCREF(Py_None);
4608 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004609}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004610#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004611
Guido van Rossumb6775db1994-08-01 11:34:53 +00004612#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004613PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004614"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004615Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004616
Barry Warsaw53699e91996-12-10 23:23:01 +00004617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004618posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004619{
Christian Heimes292d3512008-02-03 16:51:08 +00004620 pid_t pid;
4621 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004622 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004623 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004624 if (setpgid(pid, pgrp) < 0)
4625 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004626 Py_INCREF(Py_None);
4627 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004628}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004629#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004631
Guido van Rossumb6775db1994-08-01 11:34:53 +00004632#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004633PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004634"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004635Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004636
Barry Warsaw53699e91996-12-10 23:23:01 +00004637static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004638posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004639{
Christian Heimes15ebc882008-02-04 18:48:49 +00004640 int fd;
4641 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004642 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004643 return NULL;
4644 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004645 if (pgid < 0)
4646 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004647 return PyLong_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004648}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004649#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004650
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004651
Guido van Rossumb6775db1994-08-01 11:34:53 +00004652#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004653PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004654"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004655Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004656
Barry Warsaw53699e91996-12-10 23:23:01 +00004657static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004658posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004659{
4660 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004661 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004662 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004663 if (tcsetpgrp(fd, pgid) < 0)
4664 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004665 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004666 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004667}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004668#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004669
Guido van Rossum687dd131993-05-17 08:34:16 +00004670/* Functions acting on file descriptors */
4671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004672PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004673"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004674Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004675
Barry Warsaw53699e91996-12-10 23:23:01 +00004676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004677posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004678{
Mark Hammondef8b6542001-05-13 08:04:26 +00004679 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004680 int flag;
4681 int mode = 0777;
4682 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004683
4684#ifdef MS_WINDOWS
4685 if (unicode_file_names()) {
4686 PyUnicodeObject *po;
4687 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4688 Py_BEGIN_ALLOW_THREADS
4689 /* PyUnicode_AS_UNICODE OK without thread
4690 lock as it is a simple dereference. */
4691 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4692 Py_END_ALLOW_THREADS
4693 if (fd < 0)
4694 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004695 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004696 }
4697 /* Drop the argument parsing error as narrow strings
4698 are also valid. */
4699 PyErr_Clear();
4700 }
4701#endif
4702
Tim Peters5aa91602002-01-30 05:46:57 +00004703 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004704 Py_FileSystemDefaultEncoding, &file,
4705 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004706 return NULL;
4707
Barry Warsaw53699e91996-12-10 23:23:01 +00004708 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004709 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004710 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004711 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004712 return posix_error_with_allocated_filename(file);
4713 PyMem_Free(file);
Christian Heimes217cfd12007-12-02 14:31:20 +00004714 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004715}
4716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004718PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004719"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004720Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004721
Barry Warsaw53699e91996-12-10 23:23:01 +00004722static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004723posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004724{
4725 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004726 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004727 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004728 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004729 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004730 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004731 if (res < 0)
4732 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004733 Py_INCREF(Py_None);
4734 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004735}
4736
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004737
Christian Heimesfdab48e2008-01-20 09:06:41 +00004738PyDoc_STRVAR(posix_closerange__doc__,
4739"closerange(fd_low, fd_high)\n\n\
4740Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4741
4742static PyObject *
4743posix_closerange(PyObject *self, PyObject *args)
4744{
4745 int fd_from, fd_to, i;
4746 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4747 return NULL;
4748 Py_BEGIN_ALLOW_THREADS
4749 for (i = fd_from; i < fd_to; i++)
4750 close(i);
4751 Py_END_ALLOW_THREADS
4752 Py_RETURN_NONE;
4753}
4754
4755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004756PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004757"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004758Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004759
Barry Warsaw53699e91996-12-10 23:23:01 +00004760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004761posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004762{
4763 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004764 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004765 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004766 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004767 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004768 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004769 if (fd < 0)
4770 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004771 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004772}
4773
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004775PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004776"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004778
Barry Warsaw53699e91996-12-10 23:23:01 +00004779static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004780posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004781{
4782 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004783 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004784 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004785 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004786 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004787 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004788 if (res < 0)
4789 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004790 Py_INCREF(Py_None);
4791 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004792}
4793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004796"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004797Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004798
Barry Warsaw53699e91996-12-10 23:23:01 +00004799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004800posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004801{
4802 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004803#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004804 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004805#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004806 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004807#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004808 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004809 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004810 return NULL;
4811#ifdef SEEK_SET
4812 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4813 switch (how) {
4814 case 0: how = SEEK_SET; break;
4815 case 1: how = SEEK_CUR; break;
4816 case 2: how = SEEK_END; break;
4817 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004818#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004819
4820#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004821 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004822#else
4823 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004824 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004825#endif
4826 if (PyErr_Occurred())
4827 return NULL;
4828
Barry Warsaw53699e91996-12-10 23:23:01 +00004829 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004830#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004831 res = _lseeki64(fd, pos, how);
4832#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004833 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004834#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004835 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004836 if (res < 0)
4837 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004838
4839#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004840 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004841#else
4842 return PyLong_FromLongLong(res);
4843#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004844}
4845
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004847PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004848"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004849Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004850
Barry Warsaw53699e91996-12-10 23:23:01 +00004851static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004852posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004853{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004854 int fd, size;
4855 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004856 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004857 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004858 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004859 if (size < 0) {
4860 errno = EINVAL;
4861 return posix_error();
4862 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004863 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004864 if (buffer == NULL)
4865 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004866 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00004867 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004868 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004869 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004870 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004871 return posix_error();
4872 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004873 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00004874 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004875 return buffer;
4876}
4877
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004879PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004880"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004881Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004882
Barry Warsaw53699e91996-12-10 23:23:01 +00004883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004884posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004885{
Martin v. Löwis423be952008-08-13 15:53:07 +00004886 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004887 int fd;
4888 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004889
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00004890 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00004891 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004892 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00004893 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00004894 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00004895 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00004896 if (size < 0)
4897 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004898 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004899}
4900
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004902PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004903"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004904Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004905
Barry Warsaw53699e91996-12-10 23:23:01 +00004906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004907posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004908{
4909 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004910 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004911 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004912 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004913 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004914#ifdef __VMS
4915 /* on OpenVMS we must ensure that all bytes are written to the file */
4916 fsync(fd);
4917#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004918 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004919 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004920 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004921 if (res != 0) {
4922#ifdef MS_WINDOWS
4923 return win32_error("fstat", NULL);
4924#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004925 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004926#endif
4927 }
Tim Peters5aa91602002-01-30 05:46:57 +00004928
Martin v. Löwis14694662006-02-03 12:54:16 +00004929 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004930}
4931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004932PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004933"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004934Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004935connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004936
4937static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004938posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004939{
4940 int fd;
4941 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4942 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004943 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004944}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004945
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004946#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004947PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004948"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004949Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004950
Barry Warsaw53699e91996-12-10 23:23:01 +00004951static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004952posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004953{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004954#if defined(PYOS_OS2)
4955 HFILE read, write;
4956 APIRET rc;
4957
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004958 Py_BEGIN_ALLOW_THREADS
4959 rc = DosCreatePipe( &read, &write, 4096);
4960 Py_END_ALLOW_THREADS
4961 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004962 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004963
4964 return Py_BuildValue("(ii)", read, write);
4965#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004966#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004967 int fds[2];
4968 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004969 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004970 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004971 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004972 if (res != 0)
4973 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004974 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004975#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004976 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004977 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004978 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004979 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004980 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004981 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004982 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004983 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004984 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4985 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004986 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004987#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004988#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004989}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004990#endif /* HAVE_PIPE */
4991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004992
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004993#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004994PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004995"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004996Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004997
Barry Warsaw53699e91996-12-10 23:23:01 +00004998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004999posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005000{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005001 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005002 int mode = 0666;
5003 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005004 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005005 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005006 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005007 res = mkfifo(filename, mode);
5008 Py_END_ALLOW_THREADS
5009 if (res < 0)
5010 return posix_error();
5011 Py_INCREF(Py_None);
5012 return Py_None;
5013}
5014#endif
5015
5016
Neal Norwitz11690112002-07-30 01:08:28 +00005017#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005018PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005019"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005020Create a filesystem node (file, device special file or named pipe)\n\
5021named filename. mode specifies both the permissions to use and the\n\
5022type of node to be created, being combined (bitwise OR) with one of\n\
5023S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005024device defines the newly created device special file (probably using\n\
5025os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005026
5027
5028static PyObject *
5029posix_mknod(PyObject *self, PyObject *args)
5030{
5031 char *filename;
5032 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005033 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005034 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005035 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005036 return NULL;
5037 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005038 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005039 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005040 if (res < 0)
5041 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005042 Py_INCREF(Py_None);
5043 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005044}
5045#endif
5046
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005047#ifdef HAVE_DEVICE_MACROS
5048PyDoc_STRVAR(posix_major__doc__,
5049"major(device) -> major number\n\
5050Extracts a device major number from a raw device number.");
5051
5052static PyObject *
5053posix_major(PyObject *self, PyObject *args)
5054{
5055 int device;
5056 if (!PyArg_ParseTuple(args, "i:major", &device))
5057 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005058 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005059}
5060
5061PyDoc_STRVAR(posix_minor__doc__,
5062"minor(device) -> minor number\n\
5063Extracts a device minor number from a raw device number.");
5064
5065static PyObject *
5066posix_minor(PyObject *self, PyObject *args)
5067{
5068 int device;
5069 if (!PyArg_ParseTuple(args, "i:minor", &device))
5070 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005071 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005072}
5073
5074PyDoc_STRVAR(posix_makedev__doc__,
5075"makedev(major, minor) -> device number\n\
5076Composes a raw device number from the major and minor device numbers.");
5077
5078static PyObject *
5079posix_makedev(PyObject *self, PyObject *args)
5080{
5081 int major, minor;
5082 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5083 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005084 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005085}
5086#endif /* device macros */
5087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005088
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005089#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005090PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005091"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005092Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005093
Barry Warsaw53699e91996-12-10 23:23:01 +00005094static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005095posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005096{
5097 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005098 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005099 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005100 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005101
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005102 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005103 return NULL;
5104
5105#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005106 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005107#else
5108 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005109 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005110#endif
5111 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005112 return NULL;
5113
Barry Warsaw53699e91996-12-10 23:23:01 +00005114 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005115 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005116 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005117 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005118 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005119 return NULL;
5120 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005121 Py_INCREF(Py_None);
5122 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005123}
5124#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005125
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005126#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005127PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005128"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005129Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005130
Fred Drake762e2061999-08-26 17:23:54 +00005131/* Save putenv() parameters as values here, so we can collect them when they
5132 * get re-set with another call for the same key. */
5133static PyObject *posix_putenv_garbage;
5134
Tim Peters5aa91602002-01-30 05:46:57 +00005135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005136posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005137{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005138#ifdef MS_WINDOWS
5139 wchar_t *s1, *s2;
5140 wchar_t *newenv;
5141#else
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005142 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005143 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005144#endif
Fred Drake762e2061999-08-26 17:23:54 +00005145 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005146 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005147
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005148 if (!PyArg_ParseTuple(args,
5149#ifdef MS_WINDOWS
5150 "uu:putenv",
5151#else
5152 "ss:putenv",
5153#endif
5154 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005155 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005156
5157#if defined(PYOS_OS2)
5158 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5159 APIRET rc;
5160
Guido van Rossumd48f2521997-12-05 22:19:34 +00005161 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5162 if (rc != NO_ERROR)
5163 return os2_error(rc);
5164
5165 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5166 APIRET rc;
5167
Guido van Rossumd48f2521997-12-05 22:19:34 +00005168 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5169 if (rc != NO_ERROR)
5170 return os2_error(rc);
5171 } else {
5172#endif
Fred Drake762e2061999-08-26 17:23:54 +00005173 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005174 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005175 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005176#ifdef MS_WINDOWS
5177 len = wcslen(s1) + wcslen(s2) + 2;
5178 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5179#else
5180 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005181 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005182#endif
Fred Drake762e2061999-08-26 17:23:54 +00005183 if (newstr == NULL)
5184 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005185#ifdef MS_WINDOWS
5186 newenv = PyUnicode_AsUnicode(newstr);
5187 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5188 if (_wputenv(newenv)) {
5189 Py_DECREF(newstr);
5190 posix_error();
5191 return NULL;
5192 }
5193#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005194 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005195 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5196 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005197 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005198 posix_error();
5199 return NULL;
5200 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005201#endif
Fred Drake762e2061999-08-26 17:23:54 +00005202 /* Install the first arg and newstr in posix_putenv_garbage;
5203 * this will cause previous value to be collected. This has to
5204 * happen after the real putenv() call because the old value
5205 * was still accessible until then. */
5206 if (PyDict_SetItem(posix_putenv_garbage,
5207 PyTuple_GET_ITEM(args, 0), newstr)) {
5208 /* really not much we can do; just leak */
5209 PyErr_Clear();
5210 }
5211 else {
5212 Py_DECREF(newstr);
5213 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005214
5215#if defined(PYOS_OS2)
5216 }
5217#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005218 Py_INCREF(Py_None);
5219 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005220}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005221#endif /* putenv */
5222
Guido van Rossumc524d952001-10-19 01:31:59 +00005223#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005225"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005226Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005227
5228static PyObject *
5229posix_unsetenv(PyObject *self, PyObject *args)
5230{
5231 char *s1;
5232
5233 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5234 return NULL;
5235
5236 unsetenv(s1);
5237
5238 /* Remove the key from posix_putenv_garbage;
5239 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005240 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005241 * old value was still accessible until then.
5242 */
5243 if (PyDict_DelItem(posix_putenv_garbage,
5244 PyTuple_GET_ITEM(args, 0))) {
5245 /* really not much we can do; just leak */
5246 PyErr_Clear();
5247 }
5248
5249 Py_INCREF(Py_None);
5250 return Py_None;
5251}
5252#endif /* unsetenv */
5253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005254PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005255"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005256Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005257
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005259posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005260{
5261 int code;
5262 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005263 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005264 return NULL;
5265 message = strerror(code);
5266 if (message == NULL) {
5267 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005268 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005269 return NULL;
5270 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005271 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005272}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005273
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005274
Guido van Rossumc9641791998-08-04 15:26:23 +00005275#ifdef HAVE_SYS_WAIT_H
5276
Fred Drake106c1a02002-04-23 15:58:02 +00005277#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005278PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005279"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005280Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005281
5282static PyObject *
5283posix_WCOREDUMP(PyObject *self, PyObject *args)
5284{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005285 WAIT_TYPE status;
5286 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005287
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005288 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005289 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005290
5291 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005292}
5293#endif /* WCOREDUMP */
5294
5295#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005296PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005297"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005298Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005299job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005300
5301static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005302posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005303{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005304 WAIT_TYPE status;
5305 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005306
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005307 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005308 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005309
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005310 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005311}
5312#endif /* WIFCONTINUED */
5313
Guido van Rossumc9641791998-08-04 15:26:23 +00005314#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005315PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005316"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005317Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005318
5319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005320posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005321{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005322 WAIT_TYPE status;
5323 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005324
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005325 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005326 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005327
Fred Drake106c1a02002-04-23 15:58:02 +00005328 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005329}
5330#endif /* WIFSTOPPED */
5331
5332#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005333PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005334"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005335Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005336
5337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005338posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005339{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005340 WAIT_TYPE status;
5341 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005342
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005343 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005344 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005345
Fred Drake106c1a02002-04-23 15:58:02 +00005346 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005347}
5348#endif /* WIFSIGNALED */
5349
5350#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005351PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005352"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005353Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005354system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005355
5356static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005357posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005358{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005359 WAIT_TYPE status;
5360 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005361
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005362 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005363 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005364
Fred Drake106c1a02002-04-23 15:58:02 +00005365 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005366}
5367#endif /* WIFEXITED */
5368
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005369#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005370PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005371"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005372Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005373
5374static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005375posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005376{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005377 WAIT_TYPE status;
5378 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005379
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005380 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005381 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005382
Guido van Rossumc9641791998-08-04 15:26:23 +00005383 return Py_BuildValue("i", WEXITSTATUS(status));
5384}
5385#endif /* WEXITSTATUS */
5386
5387#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005388PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005389"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005390Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005391value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005392
5393static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005394posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005395{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005396 WAIT_TYPE status;
5397 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005398
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005399 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005400 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005401
Guido van Rossumc9641791998-08-04 15:26:23 +00005402 return Py_BuildValue("i", WTERMSIG(status));
5403}
5404#endif /* WTERMSIG */
5405
5406#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005407PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005408"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005409Return the signal that stopped the process that provided\n\
5410the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005411
5412static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005413posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005414{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005415 WAIT_TYPE status;
5416 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005417
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005418 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005419 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005420
Guido van Rossumc9641791998-08-04 15:26:23 +00005421 return Py_BuildValue("i", WSTOPSIG(status));
5422}
5423#endif /* WSTOPSIG */
5424
5425#endif /* HAVE_SYS_WAIT_H */
5426
5427
Thomas Wouters477c8d52006-05-27 19:21:47 +00005428#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005429#ifdef _SCO_DS
5430/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5431 needed definitions in sys/statvfs.h */
5432#define _SVID3
5433#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005434#include <sys/statvfs.h>
5435
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005436static PyObject*
5437_pystatvfs_fromstructstatvfs(struct statvfs st) {
5438 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5439 if (v == NULL)
5440 return NULL;
5441
5442#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005443 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5444 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5445 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5446 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5447 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5448 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5449 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5450 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5451 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5452 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005453#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005454 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5455 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005456 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005457 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005458 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005459 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005460 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005461 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005462 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005463 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005464 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005465 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005466 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005467 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005468 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5469 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005470#endif
5471
5472 return v;
5473}
5474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005475PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005476"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005477Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005478
5479static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005480posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005481{
5482 int fd, res;
5483 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005484
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005485 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005486 return NULL;
5487 Py_BEGIN_ALLOW_THREADS
5488 res = fstatvfs(fd, &st);
5489 Py_END_ALLOW_THREADS
5490 if (res != 0)
5491 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005492
5493 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005494}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005495#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005496
5497
Thomas Wouters477c8d52006-05-27 19:21:47 +00005498#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005499#include <sys/statvfs.h>
5500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005501PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005502"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005504
5505static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005506posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005507{
5508 char *path;
5509 int res;
5510 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005511 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005512 return NULL;
5513 Py_BEGIN_ALLOW_THREADS
5514 res = statvfs(path, &st);
5515 Py_END_ALLOW_THREADS
5516 if (res != 0)
5517 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005518
5519 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005520}
5521#endif /* HAVE_STATVFS */
5522
Fred Drakec9680921999-12-13 16:37:25 +00005523/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5524 * It maps strings representing configuration variable names to
5525 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005526 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005527 * rarely-used constants. There are three separate tables that use
5528 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005529 *
5530 * This code is always included, even if none of the interfaces that
5531 * need it are included. The #if hackery needed to avoid it would be
5532 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005533 */
5534struct constdef {
5535 char *name;
5536 long value;
5537};
5538
Fred Drake12c6e2d1999-12-14 21:25:03 +00005539static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005540conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005541 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005542{
Christian Heimes217cfd12007-12-02 14:31:20 +00005543 if (PyLong_Check(arg)) {
5544 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005545 return 1;
5546 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005547 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005548 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005549 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005550 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005551 size_t hi = tablesize;
5552 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005553 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005554 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005555 PyErr_SetString(PyExc_TypeError,
5556 "configuration names must be strings or integers");
5557 return 0;
5558 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005559 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005560 if (confname == NULL)
5561 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005562 while (lo < hi) {
5563 mid = (lo + hi) / 2;
5564 cmp = strcmp(confname, table[mid].name);
5565 if (cmp < 0)
5566 hi = mid;
5567 else if (cmp > 0)
5568 lo = mid + 1;
5569 else {
5570 *valuep = table[mid].value;
5571 return 1;
5572 }
5573 }
5574 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005575 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005576 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005577}
5578
5579
5580#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5581static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005582#ifdef _PC_ABI_AIO_XFER_MAX
5583 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5584#endif
5585#ifdef _PC_ABI_ASYNC_IO
5586 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5587#endif
Fred Drakec9680921999-12-13 16:37:25 +00005588#ifdef _PC_ASYNC_IO
5589 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5590#endif
5591#ifdef _PC_CHOWN_RESTRICTED
5592 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5593#endif
5594#ifdef _PC_FILESIZEBITS
5595 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5596#endif
5597#ifdef _PC_LAST
5598 {"PC_LAST", _PC_LAST},
5599#endif
5600#ifdef _PC_LINK_MAX
5601 {"PC_LINK_MAX", _PC_LINK_MAX},
5602#endif
5603#ifdef _PC_MAX_CANON
5604 {"PC_MAX_CANON", _PC_MAX_CANON},
5605#endif
5606#ifdef _PC_MAX_INPUT
5607 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5608#endif
5609#ifdef _PC_NAME_MAX
5610 {"PC_NAME_MAX", _PC_NAME_MAX},
5611#endif
5612#ifdef _PC_NO_TRUNC
5613 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5614#endif
5615#ifdef _PC_PATH_MAX
5616 {"PC_PATH_MAX", _PC_PATH_MAX},
5617#endif
5618#ifdef _PC_PIPE_BUF
5619 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5620#endif
5621#ifdef _PC_PRIO_IO
5622 {"PC_PRIO_IO", _PC_PRIO_IO},
5623#endif
5624#ifdef _PC_SOCK_MAXBUF
5625 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5626#endif
5627#ifdef _PC_SYNC_IO
5628 {"PC_SYNC_IO", _PC_SYNC_IO},
5629#endif
5630#ifdef _PC_VDISABLE
5631 {"PC_VDISABLE", _PC_VDISABLE},
5632#endif
5633};
5634
Fred Drakec9680921999-12-13 16:37:25 +00005635static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005636conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005637{
5638 return conv_confname(arg, valuep, posix_constants_pathconf,
5639 sizeof(posix_constants_pathconf)
5640 / sizeof(struct constdef));
5641}
5642#endif
5643
5644#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005645PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005646"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005647Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005648If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005649
5650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005651posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005652{
5653 PyObject *result = NULL;
5654 int name, fd;
5655
Fred Drake12c6e2d1999-12-14 21:25:03 +00005656 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5657 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005658 long limit;
5659
5660 errno = 0;
5661 limit = fpathconf(fd, name);
5662 if (limit == -1 && errno != 0)
5663 posix_error();
5664 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005665 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005666 }
5667 return result;
5668}
5669#endif
5670
5671
5672#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005673PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005674"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005675Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005676If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005677
5678static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005679posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005680{
5681 PyObject *result = NULL;
5682 int name;
5683 char *path;
5684
5685 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5686 conv_path_confname, &name)) {
5687 long limit;
5688
5689 errno = 0;
5690 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005691 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005692 if (errno == EINVAL)
5693 /* could be a path or name problem */
5694 posix_error();
5695 else
5696 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005697 }
Fred Drakec9680921999-12-13 16:37:25 +00005698 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005699 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005700 }
5701 return result;
5702}
5703#endif
5704
5705#ifdef HAVE_CONFSTR
5706static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005707#ifdef _CS_ARCHITECTURE
5708 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5709#endif
5710#ifdef _CS_HOSTNAME
5711 {"CS_HOSTNAME", _CS_HOSTNAME},
5712#endif
5713#ifdef _CS_HW_PROVIDER
5714 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5715#endif
5716#ifdef _CS_HW_SERIAL
5717 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5718#endif
5719#ifdef _CS_INITTAB_NAME
5720 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5721#endif
Fred Drakec9680921999-12-13 16:37:25 +00005722#ifdef _CS_LFS64_CFLAGS
5723 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5724#endif
5725#ifdef _CS_LFS64_LDFLAGS
5726 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5727#endif
5728#ifdef _CS_LFS64_LIBS
5729 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5730#endif
5731#ifdef _CS_LFS64_LINTFLAGS
5732 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5733#endif
5734#ifdef _CS_LFS_CFLAGS
5735 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5736#endif
5737#ifdef _CS_LFS_LDFLAGS
5738 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5739#endif
5740#ifdef _CS_LFS_LIBS
5741 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5742#endif
5743#ifdef _CS_LFS_LINTFLAGS
5744 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5745#endif
Fred Draked86ed291999-12-15 15:34:33 +00005746#ifdef _CS_MACHINE
5747 {"CS_MACHINE", _CS_MACHINE},
5748#endif
Fred Drakec9680921999-12-13 16:37:25 +00005749#ifdef _CS_PATH
5750 {"CS_PATH", _CS_PATH},
5751#endif
Fred Draked86ed291999-12-15 15:34:33 +00005752#ifdef _CS_RELEASE
5753 {"CS_RELEASE", _CS_RELEASE},
5754#endif
5755#ifdef _CS_SRPC_DOMAIN
5756 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5757#endif
5758#ifdef _CS_SYSNAME
5759 {"CS_SYSNAME", _CS_SYSNAME},
5760#endif
5761#ifdef _CS_VERSION
5762 {"CS_VERSION", _CS_VERSION},
5763#endif
Fred Drakec9680921999-12-13 16:37:25 +00005764#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5765 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5766#endif
5767#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5768 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5769#endif
5770#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5771 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5772#endif
5773#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5774 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5775#endif
5776#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5777 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5778#endif
5779#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5780 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5781#endif
5782#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5783 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5784#endif
5785#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5786 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5787#endif
5788#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5789 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5790#endif
5791#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5792 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5793#endif
5794#ifdef _CS_XBS5_LP64_OFF64_LIBS
5795 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5796#endif
5797#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5798 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5799#endif
5800#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5801 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5802#endif
5803#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5804 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5805#endif
5806#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5807 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5808#endif
5809#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5810 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5811#endif
Fred Draked86ed291999-12-15 15:34:33 +00005812#ifdef _MIPS_CS_AVAIL_PROCESSORS
5813 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5814#endif
5815#ifdef _MIPS_CS_BASE
5816 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5817#endif
5818#ifdef _MIPS_CS_HOSTID
5819 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5820#endif
5821#ifdef _MIPS_CS_HW_NAME
5822 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5823#endif
5824#ifdef _MIPS_CS_NUM_PROCESSORS
5825 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5826#endif
5827#ifdef _MIPS_CS_OSREL_MAJ
5828 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5829#endif
5830#ifdef _MIPS_CS_OSREL_MIN
5831 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5832#endif
5833#ifdef _MIPS_CS_OSREL_PATCH
5834 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5835#endif
5836#ifdef _MIPS_CS_OS_NAME
5837 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5838#endif
5839#ifdef _MIPS_CS_OS_PROVIDER
5840 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5841#endif
5842#ifdef _MIPS_CS_PROCESSORS
5843 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5844#endif
5845#ifdef _MIPS_CS_SERIAL
5846 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5847#endif
5848#ifdef _MIPS_CS_VENDOR
5849 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5850#endif
Fred Drakec9680921999-12-13 16:37:25 +00005851};
5852
5853static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005854conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005855{
5856 return conv_confname(arg, valuep, posix_constants_confstr,
5857 sizeof(posix_constants_confstr)
5858 / sizeof(struct constdef));
5859}
5860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005861PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005862"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005863Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005864
5865static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005866posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005867{
5868 PyObject *result = NULL;
5869 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005870 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005871
5872 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005873 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005874
Fred Drakec9680921999-12-13 16:37:25 +00005875 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005876 len = confstr(name, buffer, sizeof(buffer));
5877 if (len == 0) {
5878 if (errno) {
5879 posix_error();
5880 }
5881 else {
5882 result = Py_None;
5883 Py_INCREF(Py_None);
5884 }
Fred Drakec9680921999-12-13 16:37:25 +00005885 }
5886 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005887 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005888 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005889 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005890 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005891 }
5892 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005893 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005894 }
5895 }
5896 return result;
5897}
5898#endif
5899
5900
5901#ifdef HAVE_SYSCONF
5902static struct constdef posix_constants_sysconf[] = {
5903#ifdef _SC_2_CHAR_TERM
5904 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5905#endif
5906#ifdef _SC_2_C_BIND
5907 {"SC_2_C_BIND", _SC_2_C_BIND},
5908#endif
5909#ifdef _SC_2_C_DEV
5910 {"SC_2_C_DEV", _SC_2_C_DEV},
5911#endif
5912#ifdef _SC_2_C_VERSION
5913 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5914#endif
5915#ifdef _SC_2_FORT_DEV
5916 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5917#endif
5918#ifdef _SC_2_FORT_RUN
5919 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5920#endif
5921#ifdef _SC_2_LOCALEDEF
5922 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5923#endif
5924#ifdef _SC_2_SW_DEV
5925 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5926#endif
5927#ifdef _SC_2_UPE
5928 {"SC_2_UPE", _SC_2_UPE},
5929#endif
5930#ifdef _SC_2_VERSION
5931 {"SC_2_VERSION", _SC_2_VERSION},
5932#endif
Fred Draked86ed291999-12-15 15:34:33 +00005933#ifdef _SC_ABI_ASYNCHRONOUS_IO
5934 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5935#endif
5936#ifdef _SC_ACL
5937 {"SC_ACL", _SC_ACL},
5938#endif
Fred Drakec9680921999-12-13 16:37:25 +00005939#ifdef _SC_AIO_LISTIO_MAX
5940 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5941#endif
Fred Drakec9680921999-12-13 16:37:25 +00005942#ifdef _SC_AIO_MAX
5943 {"SC_AIO_MAX", _SC_AIO_MAX},
5944#endif
5945#ifdef _SC_AIO_PRIO_DELTA_MAX
5946 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5947#endif
5948#ifdef _SC_ARG_MAX
5949 {"SC_ARG_MAX", _SC_ARG_MAX},
5950#endif
5951#ifdef _SC_ASYNCHRONOUS_IO
5952 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5953#endif
5954#ifdef _SC_ATEXIT_MAX
5955 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5956#endif
Fred Draked86ed291999-12-15 15:34:33 +00005957#ifdef _SC_AUDIT
5958 {"SC_AUDIT", _SC_AUDIT},
5959#endif
Fred Drakec9680921999-12-13 16:37:25 +00005960#ifdef _SC_AVPHYS_PAGES
5961 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5962#endif
5963#ifdef _SC_BC_BASE_MAX
5964 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5965#endif
5966#ifdef _SC_BC_DIM_MAX
5967 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5968#endif
5969#ifdef _SC_BC_SCALE_MAX
5970 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5971#endif
5972#ifdef _SC_BC_STRING_MAX
5973 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5974#endif
Fred Draked86ed291999-12-15 15:34:33 +00005975#ifdef _SC_CAP
5976 {"SC_CAP", _SC_CAP},
5977#endif
Fred Drakec9680921999-12-13 16:37:25 +00005978#ifdef _SC_CHARCLASS_NAME_MAX
5979 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5980#endif
5981#ifdef _SC_CHAR_BIT
5982 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5983#endif
5984#ifdef _SC_CHAR_MAX
5985 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5986#endif
5987#ifdef _SC_CHAR_MIN
5988 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5989#endif
5990#ifdef _SC_CHILD_MAX
5991 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5992#endif
5993#ifdef _SC_CLK_TCK
5994 {"SC_CLK_TCK", _SC_CLK_TCK},
5995#endif
5996#ifdef _SC_COHER_BLKSZ
5997 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5998#endif
5999#ifdef _SC_COLL_WEIGHTS_MAX
6000 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6001#endif
6002#ifdef _SC_DCACHE_ASSOC
6003 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6004#endif
6005#ifdef _SC_DCACHE_BLKSZ
6006 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6007#endif
6008#ifdef _SC_DCACHE_LINESZ
6009 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6010#endif
6011#ifdef _SC_DCACHE_SZ
6012 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6013#endif
6014#ifdef _SC_DCACHE_TBLKSZ
6015 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6016#endif
6017#ifdef _SC_DELAYTIMER_MAX
6018 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6019#endif
6020#ifdef _SC_EQUIV_CLASS_MAX
6021 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6022#endif
6023#ifdef _SC_EXPR_NEST_MAX
6024 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6025#endif
6026#ifdef _SC_FSYNC
6027 {"SC_FSYNC", _SC_FSYNC},
6028#endif
6029#ifdef _SC_GETGR_R_SIZE_MAX
6030 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6031#endif
6032#ifdef _SC_GETPW_R_SIZE_MAX
6033 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6034#endif
6035#ifdef _SC_ICACHE_ASSOC
6036 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6037#endif
6038#ifdef _SC_ICACHE_BLKSZ
6039 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6040#endif
6041#ifdef _SC_ICACHE_LINESZ
6042 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6043#endif
6044#ifdef _SC_ICACHE_SZ
6045 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6046#endif
Fred Draked86ed291999-12-15 15:34:33 +00006047#ifdef _SC_INF
6048 {"SC_INF", _SC_INF},
6049#endif
Fred Drakec9680921999-12-13 16:37:25 +00006050#ifdef _SC_INT_MAX
6051 {"SC_INT_MAX", _SC_INT_MAX},
6052#endif
6053#ifdef _SC_INT_MIN
6054 {"SC_INT_MIN", _SC_INT_MIN},
6055#endif
6056#ifdef _SC_IOV_MAX
6057 {"SC_IOV_MAX", _SC_IOV_MAX},
6058#endif
Fred Draked86ed291999-12-15 15:34:33 +00006059#ifdef _SC_IP_SECOPTS
6060 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6061#endif
Fred Drakec9680921999-12-13 16:37:25 +00006062#ifdef _SC_JOB_CONTROL
6063 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6064#endif
Fred Draked86ed291999-12-15 15:34:33 +00006065#ifdef _SC_KERN_POINTERS
6066 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6067#endif
6068#ifdef _SC_KERN_SIM
6069 {"SC_KERN_SIM", _SC_KERN_SIM},
6070#endif
Fred Drakec9680921999-12-13 16:37:25 +00006071#ifdef _SC_LINE_MAX
6072 {"SC_LINE_MAX", _SC_LINE_MAX},
6073#endif
6074#ifdef _SC_LOGIN_NAME_MAX
6075 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6076#endif
6077#ifdef _SC_LOGNAME_MAX
6078 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6079#endif
6080#ifdef _SC_LONG_BIT
6081 {"SC_LONG_BIT", _SC_LONG_BIT},
6082#endif
Fred Draked86ed291999-12-15 15:34:33 +00006083#ifdef _SC_MAC
6084 {"SC_MAC", _SC_MAC},
6085#endif
Fred Drakec9680921999-12-13 16:37:25 +00006086#ifdef _SC_MAPPED_FILES
6087 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6088#endif
6089#ifdef _SC_MAXPID
6090 {"SC_MAXPID", _SC_MAXPID},
6091#endif
6092#ifdef _SC_MB_LEN_MAX
6093 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6094#endif
6095#ifdef _SC_MEMLOCK
6096 {"SC_MEMLOCK", _SC_MEMLOCK},
6097#endif
6098#ifdef _SC_MEMLOCK_RANGE
6099 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6100#endif
6101#ifdef _SC_MEMORY_PROTECTION
6102 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6103#endif
6104#ifdef _SC_MESSAGE_PASSING
6105 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6106#endif
Fred Draked86ed291999-12-15 15:34:33 +00006107#ifdef _SC_MMAP_FIXED_ALIGNMENT
6108 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6109#endif
Fred Drakec9680921999-12-13 16:37:25 +00006110#ifdef _SC_MQ_OPEN_MAX
6111 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6112#endif
6113#ifdef _SC_MQ_PRIO_MAX
6114 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6115#endif
Fred Draked86ed291999-12-15 15:34:33 +00006116#ifdef _SC_NACLS_MAX
6117 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6118#endif
Fred Drakec9680921999-12-13 16:37:25 +00006119#ifdef _SC_NGROUPS_MAX
6120 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6121#endif
6122#ifdef _SC_NL_ARGMAX
6123 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6124#endif
6125#ifdef _SC_NL_LANGMAX
6126 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6127#endif
6128#ifdef _SC_NL_MSGMAX
6129 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6130#endif
6131#ifdef _SC_NL_NMAX
6132 {"SC_NL_NMAX", _SC_NL_NMAX},
6133#endif
6134#ifdef _SC_NL_SETMAX
6135 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6136#endif
6137#ifdef _SC_NL_TEXTMAX
6138 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6139#endif
6140#ifdef _SC_NPROCESSORS_CONF
6141 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6142#endif
6143#ifdef _SC_NPROCESSORS_ONLN
6144 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6145#endif
Fred Draked86ed291999-12-15 15:34:33 +00006146#ifdef _SC_NPROC_CONF
6147 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6148#endif
6149#ifdef _SC_NPROC_ONLN
6150 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6151#endif
Fred Drakec9680921999-12-13 16:37:25 +00006152#ifdef _SC_NZERO
6153 {"SC_NZERO", _SC_NZERO},
6154#endif
6155#ifdef _SC_OPEN_MAX
6156 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6157#endif
6158#ifdef _SC_PAGESIZE
6159 {"SC_PAGESIZE", _SC_PAGESIZE},
6160#endif
6161#ifdef _SC_PAGE_SIZE
6162 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6163#endif
6164#ifdef _SC_PASS_MAX
6165 {"SC_PASS_MAX", _SC_PASS_MAX},
6166#endif
6167#ifdef _SC_PHYS_PAGES
6168 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6169#endif
6170#ifdef _SC_PII
6171 {"SC_PII", _SC_PII},
6172#endif
6173#ifdef _SC_PII_INTERNET
6174 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6175#endif
6176#ifdef _SC_PII_INTERNET_DGRAM
6177 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6178#endif
6179#ifdef _SC_PII_INTERNET_STREAM
6180 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6181#endif
6182#ifdef _SC_PII_OSI
6183 {"SC_PII_OSI", _SC_PII_OSI},
6184#endif
6185#ifdef _SC_PII_OSI_CLTS
6186 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6187#endif
6188#ifdef _SC_PII_OSI_COTS
6189 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6190#endif
6191#ifdef _SC_PII_OSI_M
6192 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6193#endif
6194#ifdef _SC_PII_SOCKET
6195 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6196#endif
6197#ifdef _SC_PII_XTI
6198 {"SC_PII_XTI", _SC_PII_XTI},
6199#endif
6200#ifdef _SC_POLL
6201 {"SC_POLL", _SC_POLL},
6202#endif
6203#ifdef _SC_PRIORITIZED_IO
6204 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6205#endif
6206#ifdef _SC_PRIORITY_SCHEDULING
6207 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6208#endif
6209#ifdef _SC_REALTIME_SIGNALS
6210 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6211#endif
6212#ifdef _SC_RE_DUP_MAX
6213 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6214#endif
6215#ifdef _SC_RTSIG_MAX
6216 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6217#endif
6218#ifdef _SC_SAVED_IDS
6219 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6220#endif
6221#ifdef _SC_SCHAR_MAX
6222 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6223#endif
6224#ifdef _SC_SCHAR_MIN
6225 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6226#endif
6227#ifdef _SC_SELECT
6228 {"SC_SELECT", _SC_SELECT},
6229#endif
6230#ifdef _SC_SEMAPHORES
6231 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6232#endif
6233#ifdef _SC_SEM_NSEMS_MAX
6234 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6235#endif
6236#ifdef _SC_SEM_VALUE_MAX
6237 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6238#endif
6239#ifdef _SC_SHARED_MEMORY_OBJECTS
6240 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6241#endif
6242#ifdef _SC_SHRT_MAX
6243 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6244#endif
6245#ifdef _SC_SHRT_MIN
6246 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6247#endif
6248#ifdef _SC_SIGQUEUE_MAX
6249 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6250#endif
6251#ifdef _SC_SIGRT_MAX
6252 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6253#endif
6254#ifdef _SC_SIGRT_MIN
6255 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6256#endif
Fred Draked86ed291999-12-15 15:34:33 +00006257#ifdef _SC_SOFTPOWER
6258 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6259#endif
Fred Drakec9680921999-12-13 16:37:25 +00006260#ifdef _SC_SPLIT_CACHE
6261 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6262#endif
6263#ifdef _SC_SSIZE_MAX
6264 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6265#endif
6266#ifdef _SC_STACK_PROT
6267 {"SC_STACK_PROT", _SC_STACK_PROT},
6268#endif
6269#ifdef _SC_STREAM_MAX
6270 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6271#endif
6272#ifdef _SC_SYNCHRONIZED_IO
6273 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6274#endif
6275#ifdef _SC_THREADS
6276 {"SC_THREADS", _SC_THREADS},
6277#endif
6278#ifdef _SC_THREAD_ATTR_STACKADDR
6279 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6280#endif
6281#ifdef _SC_THREAD_ATTR_STACKSIZE
6282 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6283#endif
6284#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6285 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6286#endif
6287#ifdef _SC_THREAD_KEYS_MAX
6288 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6289#endif
6290#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6291 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6292#endif
6293#ifdef _SC_THREAD_PRIO_INHERIT
6294 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6295#endif
6296#ifdef _SC_THREAD_PRIO_PROTECT
6297 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6298#endif
6299#ifdef _SC_THREAD_PROCESS_SHARED
6300 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6301#endif
6302#ifdef _SC_THREAD_SAFE_FUNCTIONS
6303 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6304#endif
6305#ifdef _SC_THREAD_STACK_MIN
6306 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6307#endif
6308#ifdef _SC_THREAD_THREADS_MAX
6309 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6310#endif
6311#ifdef _SC_TIMERS
6312 {"SC_TIMERS", _SC_TIMERS},
6313#endif
6314#ifdef _SC_TIMER_MAX
6315 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6316#endif
6317#ifdef _SC_TTY_NAME_MAX
6318 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6319#endif
6320#ifdef _SC_TZNAME_MAX
6321 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6322#endif
6323#ifdef _SC_T_IOV_MAX
6324 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6325#endif
6326#ifdef _SC_UCHAR_MAX
6327 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6328#endif
6329#ifdef _SC_UINT_MAX
6330 {"SC_UINT_MAX", _SC_UINT_MAX},
6331#endif
6332#ifdef _SC_UIO_MAXIOV
6333 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6334#endif
6335#ifdef _SC_ULONG_MAX
6336 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6337#endif
6338#ifdef _SC_USHRT_MAX
6339 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6340#endif
6341#ifdef _SC_VERSION
6342 {"SC_VERSION", _SC_VERSION},
6343#endif
6344#ifdef _SC_WORD_BIT
6345 {"SC_WORD_BIT", _SC_WORD_BIT},
6346#endif
6347#ifdef _SC_XBS5_ILP32_OFF32
6348 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6349#endif
6350#ifdef _SC_XBS5_ILP32_OFFBIG
6351 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6352#endif
6353#ifdef _SC_XBS5_LP64_OFF64
6354 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6355#endif
6356#ifdef _SC_XBS5_LPBIG_OFFBIG
6357 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6358#endif
6359#ifdef _SC_XOPEN_CRYPT
6360 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6361#endif
6362#ifdef _SC_XOPEN_ENH_I18N
6363 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6364#endif
6365#ifdef _SC_XOPEN_LEGACY
6366 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6367#endif
6368#ifdef _SC_XOPEN_REALTIME
6369 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6370#endif
6371#ifdef _SC_XOPEN_REALTIME_THREADS
6372 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6373#endif
6374#ifdef _SC_XOPEN_SHM
6375 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6376#endif
6377#ifdef _SC_XOPEN_UNIX
6378 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6379#endif
6380#ifdef _SC_XOPEN_VERSION
6381 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6382#endif
6383#ifdef _SC_XOPEN_XCU_VERSION
6384 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6385#endif
6386#ifdef _SC_XOPEN_XPG2
6387 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6388#endif
6389#ifdef _SC_XOPEN_XPG3
6390 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6391#endif
6392#ifdef _SC_XOPEN_XPG4
6393 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6394#endif
6395};
6396
6397static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006398conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006399{
6400 return conv_confname(arg, valuep, posix_constants_sysconf,
6401 sizeof(posix_constants_sysconf)
6402 / sizeof(struct constdef));
6403}
6404
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006406"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006407Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006408
6409static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006410posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006411{
6412 PyObject *result = NULL;
6413 int name;
6414
6415 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6416 int value;
6417
6418 errno = 0;
6419 value = sysconf(name);
6420 if (value == -1 && errno != 0)
6421 posix_error();
6422 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006423 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006424 }
6425 return result;
6426}
6427#endif
6428
6429
Fred Drakebec628d1999-12-15 18:31:10 +00006430/* This code is used to ensure that the tables of configuration value names
6431 * are in sorted order as required by conv_confname(), and also to build the
6432 * the exported dictionaries that are used to publish information about the
6433 * names available on the host platform.
6434 *
6435 * Sorting the table at runtime ensures that the table is properly ordered
6436 * when used, even for platforms we're not able to test on. It also makes
6437 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006438 */
Fred Drakebec628d1999-12-15 18:31:10 +00006439
6440static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006441cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006442{
6443 const struct constdef *c1 =
6444 (const struct constdef *) v1;
6445 const struct constdef *c2 =
6446 (const struct constdef *) v2;
6447
6448 return strcmp(c1->name, c2->name);
6449}
6450
6451static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006452setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006453 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006454{
Fred Drakebec628d1999-12-15 18:31:10 +00006455 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006456 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006457
6458 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6459 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006460 if (d == NULL)
6461 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006462
Barry Warsaw3155db32000-04-13 15:20:40 +00006463 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006464 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006465 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6466 Py_XDECREF(o);
6467 Py_DECREF(d);
6468 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006469 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006470 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006471 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006472 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006473}
6474
Fred Drakebec628d1999-12-15 18:31:10 +00006475/* Return -1 on failure, 0 on success. */
6476static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006477setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006478{
6479#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006480 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006481 sizeof(posix_constants_pathconf)
6482 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006483 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006484 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006485#endif
6486#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006487 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006488 sizeof(posix_constants_confstr)
6489 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006490 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006491 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006492#endif
6493#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006494 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006495 sizeof(posix_constants_sysconf)
6496 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006497 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006498 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006499#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006500 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006501}
Fred Draked86ed291999-12-15 15:34:33 +00006502
6503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006504PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006505"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006506Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006507in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006508
6509static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006510posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006511{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006512 abort();
6513 /*NOTREACHED*/
6514 Py_FatalError("abort() called from Python code didn't abort!");
6515 return NULL;
6516}
Fred Drakebec628d1999-12-15 18:31:10 +00006517
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006518#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006519PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006520"startfile(filepath [, operation]) - Start a file with its associated\n\
6521application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006522\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006523When \"operation\" is not specified or \"open\", this acts like\n\
6524double-clicking the file in Explorer, or giving the file name as an\n\
6525argument to the DOS \"start\" command: the file is opened with whatever\n\
6526application (if any) its extension is associated.\n\
6527When another \"operation\" is given, it specifies what should be done with\n\
6528the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006529\n\
6530startfile returns as soon as the associated application is launched.\n\
6531There is no option to wait for the application to close, and no way\n\
6532to retrieve the application's exit status.\n\
6533\n\
6534The filepath is relative to the current directory. If you want to use\n\
6535an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006536the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006537
6538static PyObject *
6539win32_startfile(PyObject *self, PyObject *args)
6540{
6541 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006542 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006543 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006544#ifdef Py_WIN_WIDE_FILENAMES
6545 if (unicode_file_names()) {
6546 PyObject *unipath, *woperation = NULL;
6547 if (!PyArg_ParseTuple(args, "U|s:startfile",
6548 &unipath, &operation)) {
6549 PyErr_Clear();
6550 goto normal;
6551 }
6552
6553
6554 if (operation) {
6555 woperation = PyUnicode_DecodeASCII(operation,
6556 strlen(operation), NULL);
6557 if (!woperation) {
6558 PyErr_Clear();
6559 operation = NULL;
6560 goto normal;
6561 }
6562 }
6563
6564 Py_BEGIN_ALLOW_THREADS
6565 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6566 PyUnicode_AS_UNICODE(unipath),
6567 NULL, NULL, SW_SHOWNORMAL);
6568 Py_END_ALLOW_THREADS
6569
6570 Py_XDECREF(woperation);
6571 if (rc <= (HINSTANCE)32) {
6572 PyObject *errval = win32_error_unicode("startfile",
6573 PyUnicode_AS_UNICODE(unipath));
6574 return errval;
6575 }
6576 Py_INCREF(Py_None);
6577 return Py_None;
6578 }
6579#endif
6580
6581normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006582 if (!PyArg_ParseTuple(args, "et|s:startfile",
6583 Py_FileSystemDefaultEncoding, &filepath,
6584 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006585 return NULL;
6586 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006587 rc = ShellExecute((HWND)0, operation, filepath,
6588 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006589 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006590 if (rc <= (HINSTANCE)32) {
6591 PyObject *errval = win32_error("startfile", filepath);
6592 PyMem_Free(filepath);
6593 return errval;
6594 }
6595 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006596 Py_INCREF(Py_None);
6597 return Py_None;
6598}
6599#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006600
Martin v. Löwis438b5342002-12-27 10:16:42 +00006601#ifdef HAVE_GETLOADAVG
6602PyDoc_STRVAR(posix_getloadavg__doc__,
6603"getloadavg() -> (float, float, float)\n\n\
6604Return the number of processes in the system run queue averaged over\n\
6605the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6606was unobtainable");
6607
6608static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006609posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006610{
6611 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006612 if (getloadavg(loadavg, 3)!=3) {
6613 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6614 return NULL;
6615 } else
6616 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6617}
6618#endif
6619
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006620#ifdef MS_WINDOWS
6621
6622PyDoc_STRVAR(win32_urandom__doc__,
6623"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006624Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006625
6626typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6627 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6628 DWORD dwFlags );
6629typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6630 BYTE *pbBuffer );
6631
6632static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006633/* This handle is never explicitly released. Instead, the operating
6634 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006635static HCRYPTPROV hCryptProv = 0;
6636
Tim Peters4ad82172004-08-30 17:02:04 +00006637static PyObject*
6638win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006639{
Tim Petersd3115382004-08-30 17:36:46 +00006640 int howMany;
6641 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006642
Tim Peters4ad82172004-08-30 17:02:04 +00006643 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006644 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006645 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006646 if (howMany < 0)
6647 return PyErr_Format(PyExc_ValueError,
6648 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006649
Tim Peters4ad82172004-08-30 17:02:04 +00006650 if (hCryptProv == 0) {
6651 HINSTANCE hAdvAPI32 = NULL;
6652 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006653
Tim Peters4ad82172004-08-30 17:02:04 +00006654 /* Obtain handle to the DLL containing CryptoAPI
6655 This should not fail */
6656 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6657 if(hAdvAPI32 == NULL)
6658 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006659
Tim Peters4ad82172004-08-30 17:02:04 +00006660 /* Obtain pointers to the CryptoAPI functions
6661 This will fail on some early versions of Win95 */
6662 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6663 hAdvAPI32,
6664 "CryptAcquireContextA");
6665 if (pCryptAcquireContext == NULL)
6666 return PyErr_Format(PyExc_NotImplementedError,
6667 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006668
Tim Peters4ad82172004-08-30 17:02:04 +00006669 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6670 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006671 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006672 return PyErr_Format(PyExc_NotImplementedError,
6673 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006674
Tim Peters4ad82172004-08-30 17:02:04 +00006675 /* Acquire context */
6676 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6677 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6678 return win32_error("CryptAcquireContext", NULL);
6679 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006680
Tim Peters4ad82172004-08-30 17:02:04 +00006681 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006682 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006683 if (result != NULL) {
6684 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006685 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006686 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006687 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006688 Py_DECREF(result);
6689 return win32_error("CryptGenRandom", NULL);
6690 }
Tim Peters4ad82172004-08-30 17:02:04 +00006691 }
Tim Petersd3115382004-08-30 17:36:46 +00006692 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006693}
6694#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006695
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006696PyDoc_STRVAR(device_encoding__doc__,
6697"device_encoding(fd) -> str\n\n\
6698Return a string describing the encoding of the device\n\
6699if the output is a terminal; else return None.");
6700
6701static PyObject *
6702device_encoding(PyObject *self, PyObject *args)
6703{
6704 int fd;
6705 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6706 return NULL;
6707 if (!isatty(fd)) {
6708 Py_INCREF(Py_None);
6709 return Py_None;
6710 }
6711#if defined(MS_WINDOWS) || defined(MS_WIN64)
6712 if (fd == 0) {
6713 char buf[100];
6714 sprintf(buf, "cp%d", GetConsoleCP());
6715 return PyUnicode_FromString(buf);
6716 }
6717 if (fd == 1 || fd == 2) {
6718 char buf[100];
6719 sprintf(buf, "cp%d", GetConsoleOutputCP());
6720 return PyUnicode_FromString(buf);
6721 }
6722#elif defined(CODESET)
6723 {
6724 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006725 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006726 return PyUnicode_FromString(codeset);
6727 }
6728#endif
6729 Py_INCREF(Py_None);
6730 return Py_None;
6731}
6732
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006733#ifdef __VMS
6734/* Use openssl random routine */
6735#include <openssl/rand.h>
6736PyDoc_STRVAR(vms_urandom__doc__,
6737"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006738Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006739
6740static PyObject*
6741vms_urandom(PyObject *self, PyObject *args)
6742{
6743 int howMany;
6744 PyObject* result;
6745
6746 /* Read arguments */
6747 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6748 return NULL;
6749 if (howMany < 0)
6750 return PyErr_Format(PyExc_ValueError,
6751 "negative argument not allowed");
6752
6753 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006754 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006755 if (result != NULL) {
6756 /* Get random data */
6757 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006758 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006759 howMany) < 0) {
6760 Py_DECREF(result);
6761 return PyErr_Format(PyExc_ValueError,
6762 "RAND_pseudo_bytes");
6763 }
6764 }
6765 return result;
6766}
6767#endif
6768
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006769static PyMethodDef posix_methods[] = {
6770 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6771#ifdef HAVE_TTYNAME
6772 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6773#endif
6774 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006775#ifdef HAVE_CHFLAGS
6776 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6777#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006778 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00006779#ifdef HAVE_FCHMOD
6780 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6781#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006782#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006783 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006784#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00006785#ifdef HAVE_LCHMOD
6786 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6787#endif /* HAVE_LCHMOD */
6788#ifdef HAVE_FCHOWN
6789 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6790#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006791#ifdef HAVE_LCHFLAGS
6792 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6793#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006794#ifdef HAVE_LCHOWN
6795 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6796#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006797#ifdef HAVE_CHROOT
6798 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6799#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006800#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006801 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006802#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006803#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00006804 {"getcwd", (PyCFunction)posix_getcwd_unicode,
6805 METH_NOARGS, posix_getcwd__doc__},
6806 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
6807 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006808#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006809#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006810 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006811#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006812 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6813 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6814 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006815#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006816 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006817#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006818#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006819 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006820#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006821 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6822 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6823 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006824 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006825#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006826 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006827#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006828#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006829 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006830#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006831 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006832#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006833 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006834#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006835 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6836 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6837 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006838#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006839 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006840#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006841 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006842#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006843 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6844 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006845#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006846#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006847 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6848 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006849#if defined(PYOS_OS2)
6850 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6851 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6852#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006853#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006854#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006855 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006856#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006857#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006858 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006859#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006860#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006861 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006862#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006863#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006864 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006865#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006866#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006867 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006868#endif /* HAVE_GETEGID */
6869#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006870 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006871#endif /* HAVE_GETEUID */
6872#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006873 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006874#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006875#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006876 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006877#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006878 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006879#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006880 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006881#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006882#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006883 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006884#endif /* HAVE_GETPPID */
6885#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006886 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006887#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006888#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006889 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006890#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006891#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006892 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006893#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006894#ifdef HAVE_KILLPG
6895 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6896#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006897#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006898 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006899#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006900#ifdef MS_WINDOWS
6901 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6902#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006903#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006904 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006905#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006906#ifdef HAVE_SETEUID
6907 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6908#endif /* HAVE_SETEUID */
6909#ifdef HAVE_SETEGID
6910 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6911#endif /* HAVE_SETEGID */
6912#ifdef HAVE_SETREUID
6913 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6914#endif /* HAVE_SETREUID */
6915#ifdef HAVE_SETREGID
6916 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6917#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006918#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006919 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006920#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006921#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006922 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006923#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006924#ifdef HAVE_GETPGID
6925 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6926#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006927#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006928 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006929#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006930#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006931 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006932#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006933#ifdef HAVE_WAIT3
6934 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6935#endif /* HAVE_WAIT3 */
6936#ifdef HAVE_WAIT4
6937 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6938#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006939#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006940 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006941#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006942#ifdef HAVE_GETSID
6943 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6944#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006945#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006946 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006947#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006948#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006949 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006950#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006951#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006952 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006953#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006954#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006955 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006956#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006957 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6958 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00006959 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006960 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006961 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6962 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6963 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6964 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6965 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6966 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006967 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006968#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006969 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006970#endif
6971#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006972 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006973#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006974#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006975 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6976#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006977#ifdef HAVE_DEVICE_MACROS
6978 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6979 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6980 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6981#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006982#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006983 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006984#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006985#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006986 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006987#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006988#ifdef HAVE_UNSETENV
6989 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6990#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006991 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00006992#ifdef HAVE_FCHDIR
6993 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6994#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006995#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006996 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006997#endif
6998#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006999 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007000#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007001#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007002#ifdef WCOREDUMP
7003 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7004#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007005#ifdef WIFCONTINUED
7006 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7007#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007008#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007009 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007010#endif /* WIFSTOPPED */
7011#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007012 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007013#endif /* WIFSIGNALED */
7014#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007015 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007016#endif /* WIFEXITED */
7017#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007018 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007019#endif /* WEXITSTATUS */
7020#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007021 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007022#endif /* WTERMSIG */
7023#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007024 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007025#endif /* WSTOPSIG */
7026#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007027#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007028 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007029#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007030#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007031 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007032#endif
Fred Drakec9680921999-12-13 16:37:25 +00007033#ifdef HAVE_CONFSTR
7034 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7035#endif
7036#ifdef HAVE_SYSCONF
7037 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7038#endif
7039#ifdef HAVE_FPATHCONF
7040 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7041#endif
7042#ifdef HAVE_PATHCONF
7043 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7044#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007045 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007046#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007047 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7048#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007049#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007050 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007051#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007052 #ifdef MS_WINDOWS
7053 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7054 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007055 #ifdef __VMS
7056 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7057 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007058 {NULL, NULL} /* Sentinel */
7059};
7060
7061
Barry Warsaw4a342091996-12-19 23:50:02 +00007062static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007063ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007064{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007065 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007066}
7067
Guido van Rossumd48f2521997-12-05 22:19:34 +00007068#if defined(PYOS_OS2)
7069/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007070static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007071{
7072 APIRET rc;
7073 ULONG values[QSV_MAX+1];
7074 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007075 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007076
7077 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007078 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007079 Py_END_ALLOW_THREADS
7080
7081 if (rc != NO_ERROR) {
7082 os2_error(rc);
7083 return -1;
7084 }
7085
Fred Drake4d1e64b2002-04-15 19:40:07 +00007086 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7087 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7088 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7089 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7090 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7091 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7092 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007093
7094 switch (values[QSV_VERSION_MINOR]) {
7095 case 0: ver = "2.00"; break;
7096 case 10: ver = "2.10"; break;
7097 case 11: ver = "2.11"; break;
7098 case 30: ver = "3.00"; break;
7099 case 40: ver = "4.00"; break;
7100 case 50: ver = "5.00"; break;
7101 default:
Tim Peters885d4572001-11-28 20:27:42 +00007102 PyOS_snprintf(tmp, sizeof(tmp),
7103 "%d-%d", values[QSV_VERSION_MAJOR],
7104 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007105 ver = &tmp[0];
7106 }
7107
7108 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007109 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007110 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007111
7112 /* Add Indicator of Which Drive was Used to Boot the System */
7113 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7114 tmp[1] = ':';
7115 tmp[2] = '\0';
7116
Fred Drake4d1e64b2002-04-15 19:40:07 +00007117 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007118}
7119#endif
7120
Barry Warsaw4a342091996-12-19 23:50:02 +00007121static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007122all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007123{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007124#ifdef F_OK
7125 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007126#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007127#ifdef R_OK
7128 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007129#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007130#ifdef W_OK
7131 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007132#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007133#ifdef X_OK
7134 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007135#endif
Fred Drakec9680921999-12-13 16:37:25 +00007136#ifdef NGROUPS_MAX
7137 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7138#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007139#ifdef TMP_MAX
7140 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7141#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007142#ifdef WCONTINUED
7143 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7144#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007145#ifdef WNOHANG
7146 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007147#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007148#ifdef WUNTRACED
7149 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7150#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007151#ifdef O_RDONLY
7152 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7153#endif
7154#ifdef O_WRONLY
7155 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7156#endif
7157#ifdef O_RDWR
7158 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7159#endif
7160#ifdef O_NDELAY
7161 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7162#endif
7163#ifdef O_NONBLOCK
7164 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7165#endif
7166#ifdef O_APPEND
7167 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7168#endif
7169#ifdef O_DSYNC
7170 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7171#endif
7172#ifdef O_RSYNC
7173 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7174#endif
7175#ifdef O_SYNC
7176 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7177#endif
7178#ifdef O_NOCTTY
7179 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7180#endif
7181#ifdef O_CREAT
7182 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7183#endif
7184#ifdef O_EXCL
7185 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7186#endif
7187#ifdef O_TRUNC
7188 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7189#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007190#ifdef O_BINARY
7191 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7192#endif
7193#ifdef O_TEXT
7194 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7195#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007196#ifdef O_LARGEFILE
7197 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7198#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007199#ifdef O_SHLOCK
7200 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7201#endif
7202#ifdef O_EXLOCK
7203 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7204#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007205
Tim Peters5aa91602002-01-30 05:46:57 +00007206/* MS Windows */
7207#ifdef O_NOINHERIT
7208 /* Don't inherit in child processes. */
7209 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7210#endif
7211#ifdef _O_SHORT_LIVED
7212 /* Optimize for short life (keep in memory). */
7213 /* MS forgot to define this one with a non-underscore form too. */
7214 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7215#endif
7216#ifdef O_TEMPORARY
7217 /* Automatically delete when last handle is closed. */
7218 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7219#endif
7220#ifdef O_RANDOM
7221 /* Optimize for random access. */
7222 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7223#endif
7224#ifdef O_SEQUENTIAL
7225 /* Optimize for sequential access. */
7226 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7227#endif
7228
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007229/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007230#ifdef O_ASYNC
7231 /* Send a SIGIO signal whenever input or output
7232 becomes available on file descriptor */
7233 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7234#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007235#ifdef O_DIRECT
7236 /* Direct disk access. */
7237 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7238#endif
7239#ifdef O_DIRECTORY
7240 /* Must be a directory. */
7241 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7242#endif
7243#ifdef O_NOFOLLOW
7244 /* Do not follow links. */
7245 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7246#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007247#ifdef O_NOATIME
7248 /* Do not update the access time. */
7249 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7250#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007251
Barry Warsaw5676bd12003-01-07 20:57:09 +00007252 /* These come from sysexits.h */
7253#ifdef EX_OK
7254 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007255#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007256#ifdef EX_USAGE
7257 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007258#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007259#ifdef EX_DATAERR
7260 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007261#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007262#ifdef EX_NOINPUT
7263 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007264#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007265#ifdef EX_NOUSER
7266 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007267#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007268#ifdef EX_NOHOST
7269 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007270#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007271#ifdef EX_UNAVAILABLE
7272 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007273#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007274#ifdef EX_SOFTWARE
7275 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007276#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007277#ifdef EX_OSERR
7278 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007279#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007280#ifdef EX_OSFILE
7281 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007282#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007283#ifdef EX_CANTCREAT
7284 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007285#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007286#ifdef EX_IOERR
7287 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007288#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007289#ifdef EX_TEMPFAIL
7290 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007291#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007292#ifdef EX_PROTOCOL
7293 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007294#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007295#ifdef EX_NOPERM
7296 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007297#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007298#ifdef EX_CONFIG
7299 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007300#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007301#ifdef EX_NOTFOUND
7302 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007303#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007304
Guido van Rossum246bc171999-02-01 23:54:31 +00007305#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007306#if defined(PYOS_OS2) && defined(PYCC_GCC)
7307 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7308 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7309 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7310 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7311 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7312 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7313 if (ins(d, "P_PM", (long)P_PM)) return -1;
7314 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7315 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7316 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7317 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7318 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7319 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7320 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7321 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7322 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7323 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7324 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7325 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7326 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7327#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007328 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7329 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7330 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7331 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7332 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007333#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007334#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007335
Guido van Rossumd48f2521997-12-05 22:19:34 +00007336#if defined(PYOS_OS2)
7337 if (insertvalues(d)) return -1;
7338#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007339 return 0;
7340}
7341
7342
Tim Peters5aa91602002-01-30 05:46:57 +00007343#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007344#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007345#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007346
7347#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007348#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007349#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007350
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007351#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007352#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007353#define MODNAME "posix"
7354#endif
7355
Martin v. Löwis1a214512008-06-11 05:26:20 +00007356static struct PyModuleDef posixmodule = {
7357 PyModuleDef_HEAD_INIT,
7358 MODNAME,
7359 posix__doc__,
7360 -1,
7361 posix_methods,
7362 NULL,
7363 NULL,
7364 NULL,
7365 NULL
7366};
7367
7368
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007369PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007370INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007371{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007372 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007373
Martin v. Löwis1a214512008-06-11 05:26:20 +00007374 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007375 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007376 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007377
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007378 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007379 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007380 Py_XINCREF(v);
7381 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007382 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007383 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007384
Fred Drake4d1e64b2002-04-15 19:40:07 +00007385 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007386 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007387
Fred Drake4d1e64b2002-04-15 19:40:07 +00007388 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007389 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007390
Fred Drake4d1e64b2002-04-15 19:40:07 +00007391 Py_INCREF(PyExc_OSError);
7392 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007393
Guido van Rossumb3d39562000-01-31 18:41:26 +00007394#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007395 if (posix_putenv_garbage == NULL)
7396 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007397#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007398
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007399 if (!initialized) {
7400 stat_result_desc.name = MODNAME ".stat_result";
7401 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7402 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7403 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7404 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7405 structseq_new = StatResultType.tp_new;
7406 StatResultType.tp_new = statresult_new;
7407
7408 statvfs_result_desc.name = MODNAME ".statvfs_result";
7409 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007410#ifdef NEED_TICKS_PER_SECOND
7411# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7412 ticks_per_second = sysconf(_SC_CLK_TCK);
7413# elif defined(HZ)
7414 ticks_per_second = HZ;
7415# else
7416 ticks_per_second = 60; /* magic fallback value; may be bogus */
7417# endif
7418#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007419 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007420 Py_INCREF((PyObject*) &StatResultType);
7421 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007422 Py_INCREF((PyObject*) &StatVFSResultType);
7423 PyModule_AddObject(m, "statvfs_result",
7424 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007425 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007426
7427#ifdef __APPLE__
7428 /*
7429 * Step 2 of weak-linking support on Mac OS X.
7430 *
7431 * The code below removes functions that are not available on the
7432 * currently active platform.
7433 *
7434 * This block allow one to use a python binary that was build on
7435 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7436 * OSX 10.4.
7437 */
7438#ifdef HAVE_FSTATVFS
7439 if (fstatvfs == NULL) {
7440 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007441 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007442 }
7443 }
7444#endif /* HAVE_FSTATVFS */
7445
7446#ifdef HAVE_STATVFS
7447 if (statvfs == NULL) {
7448 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007449 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007450 }
7451 }
7452#endif /* HAVE_STATVFS */
7453
7454# ifdef HAVE_LCHOWN
7455 if (lchown == NULL) {
7456 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007457 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007458 }
7459 }
7460#endif /* HAVE_LCHOWN */
7461
7462
7463#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007464 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007465
Guido van Rossumb6775db1994-08-01 11:34:53 +00007466}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007467
7468#ifdef __cplusplus
7469}
7470#endif