blob: 82821343760e551e1cf37acfb16571171f4e51d4 [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
502static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
503{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504}
505
506/* Function suitable for O& conversion */
507static int
508convert_to_unicode(PyObject *arg, void* _param)
509{
510 PyObject **param = (PyObject**)_param;
511 if (PyUnicode_CheckExact(arg)) {
512 Py_INCREF(arg);
513 *param = arg;
514 }
515 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000516 /* For a Unicode subtype that's not a Unicode object,
517 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000518 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
519 PyUnicode_GET_SIZE(arg));
520 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000521 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000522 else
523 *param = PyUnicode_FromEncodedObject(arg,
524 Py_FileSystemDefaultEncoding,
525 "strict");
526 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000527}
528
529#endif /* Py_WIN_WIDE_FILENAMES */
530
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000531#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532
Guido van Rossumd48f2521997-12-05 22:19:34 +0000533#if defined(PYOS_OS2)
534/**********************************************************************
535 * Helper Function to Trim and Format OS/2 Messages
536 **********************************************************************/
537 static void
538os2_formatmsg(char *msgbuf, int msglen, char *reason)
539{
540 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
541
542 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
543 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
544
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000545 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000546 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
547 }
548
549 /* Add Optional Reason Text */
550 if (reason) {
551 strcat(msgbuf, " : ");
552 strcat(msgbuf, reason);
553 }
554}
555
556/**********************************************************************
557 * Decode an OS/2 Operating System Error Code
558 *
559 * A convenience function to lookup an OS/2 error code and return a
560 * text message we can use to raise a Python exception.
561 *
562 * Notes:
563 * The messages for errors returned from the OS/2 kernel reside in
564 * the file OSO001.MSG in the \OS2 directory hierarchy.
565 *
566 **********************************************************************/
567 static char *
568os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
569{
570 APIRET rc;
571 ULONG msglen;
572
573 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
574 Py_BEGIN_ALLOW_THREADS
575 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
576 errorcode, "oso001.msg", &msglen);
577 Py_END_ALLOW_THREADS
578
579 if (rc == NO_ERROR)
580 os2_formatmsg(msgbuf, msglen, reason);
581 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000582 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000583 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000584
585 return msgbuf;
586}
587
588/* Set an OS/2-specific error and return NULL. OS/2 kernel
589 errors are not in a global variable e.g. 'errno' nor are
590 they congruent with posix error numbers. */
591
592static PyObject * os2_error(int code)
593{
594 char text[1024];
595 PyObject *v;
596
597 os2_strerror(text, sizeof(text), code, "");
598
599 v = Py_BuildValue("(is)", code, text);
600 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000601 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000602 Py_DECREF(v);
603 }
604 return NULL; /* Signal to Python that an Exception is Pending */
605}
606
607#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608
609/* POSIX generic methods */
610
Barry Warsaw53699e91996-12-10 23:23:01 +0000611static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000612posix_fildes(PyObject *fdobj, int (*func)(int))
613{
614 int fd;
615 int res;
616 fd = PyObject_AsFileDescriptor(fdobj);
617 if (fd < 0)
618 return NULL;
619 Py_BEGIN_ALLOW_THREADS
620 res = (*func)(fd);
621 Py_END_ALLOW_THREADS
622 if (res < 0)
623 return posix_error();
624 Py_INCREF(Py_None);
625 return Py_None;
626}
Guido van Rossum21142a01999-01-08 21:05:37 +0000627
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000628#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000629static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000630unicode_file_names(void)
631{
632 static int canusewide = -1;
633 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000634 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635 the Windows NT family. */
636 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
637 }
638 return canusewide;
639}
640#endif
Tim Peters11b23062003-04-23 02:39:17 +0000641
Guido van Rossum21142a01999-01-08 21:05:37 +0000642static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000643posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644{
Mark Hammondef8b6542001-05-13 08:04:26 +0000645 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000646 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000647 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000648 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000650 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000651 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000652 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000653 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000654 return posix_error_with_allocated_filename(path1);
655 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000656 Py_INCREF(Py_None);
657 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000658}
659
Barry Warsaw53699e91996-12-10 23:23:01 +0000660static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000661posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000662 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664{
Mark Hammondef8b6542001-05-13 08:04:26 +0000665 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000666 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000667 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000668 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000669 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000671 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000672 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000673 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000674 PyMem_Free(path1);
675 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000676 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000677 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000679 Py_INCREF(Py_None);
680 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681}
682
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683#ifdef Py_WIN_WIDE_FILENAMES
684static PyObject*
685win32_1str(PyObject* args, char* func,
686 char* format, BOOL (__stdcall *funcA)(LPCSTR),
687 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
688{
689 PyObject *uni;
690 char *ansi;
691 BOOL result;
692 if (unicode_file_names()) {
693 if (!PyArg_ParseTuple(args, wformat, &uni))
694 PyErr_Clear();
695 else {
696 Py_BEGIN_ALLOW_THREADS
697 result = funcW(PyUnicode_AsUnicode(uni));
698 Py_END_ALLOW_THREADS
699 if (!result)
700 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
701 Py_INCREF(Py_None);
702 return Py_None;
703 }
704 }
705 if (!PyArg_ParseTuple(args, format, &ansi))
706 return NULL;
707 Py_BEGIN_ALLOW_THREADS
708 result = funcA(ansi);
709 Py_END_ALLOW_THREADS
710 if (!result)
711 return win32_error(func, ansi);
712 Py_INCREF(Py_None);
713 return Py_None;
714
715}
716
717/* This is a reimplementation of the C library's chdir function,
718 but one that produces Win32 errors instead of DOS error codes.
719 chdir is essentially a wrapper around SetCurrentDirectory; however,
720 it also needs to set "magic" environment variables indicating
721 the per-drive current directory, which are of the form =<drive>: */
722BOOL __stdcall
723win32_chdir(LPCSTR path)
724{
725 char new_path[MAX_PATH+1];
726 int result;
727 char env[4] = "=x:";
728
729 if(!SetCurrentDirectoryA(path))
730 return FALSE;
731 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
732 if (!result)
733 return FALSE;
734 /* In the ANSI API, there should not be any paths longer
735 than MAX_PATH. */
736 assert(result <= MAX_PATH+1);
737 if (strncmp(new_path, "\\\\", 2) == 0 ||
738 strncmp(new_path, "//", 2) == 0)
739 /* UNC path, nothing to do. */
740 return TRUE;
741 env[1] = new_path[0];
742 return SetEnvironmentVariableA(env, new_path);
743}
744
745/* The Unicode version differs from the ANSI version
746 since the current directory might exceed MAX_PATH characters */
747BOOL __stdcall
748win32_wchdir(LPCWSTR path)
749{
750 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
751 int result;
752 wchar_t env[4] = L"=x:";
753
754 if(!SetCurrentDirectoryW(path))
755 return FALSE;
756 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
757 if (!result)
758 return FALSE;
759 if (result > MAX_PATH+1) {
760 new_path = malloc(result);
761 if (!new_path) {
762 SetLastError(ERROR_OUTOFMEMORY);
763 return FALSE;
764 }
765 }
766 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
767 wcsncmp(new_path, L"//", 2) == 0)
768 /* UNC path, nothing to do. */
769 return TRUE;
770 env[1] = new_path[0];
771 result = SetEnvironmentVariableW(env, new_path);
772 if (new_path != _new_path)
773 free(new_path);
774 return result;
775}
776#endif
777
Martin v. Löwis14694662006-02-03 12:54:16 +0000778#ifdef MS_WINDOWS
779/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
780 - time stamps are restricted to second resolution
781 - file modification times suffer from forth-and-back conversions between
782 UTC and local time
783 Therefore, we implement our own stat, based on the Win32 API directly.
784*/
785#define HAVE_STAT_NSEC 1
786
787struct win32_stat{
788 int st_dev;
789 __int64 st_ino;
790 unsigned short st_mode;
791 int st_nlink;
792 int st_uid;
793 int st_gid;
794 int st_rdev;
795 __int64 st_size;
796 int st_atime;
797 int st_atime_nsec;
798 int st_mtime;
799 int st_mtime_nsec;
800 int st_ctime;
801 int st_ctime_nsec;
802};
803
804static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
805
806static void
807FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
808{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000809 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
810 /* Cannot simply cast and dereference in_ptr,
811 since it might not be aligned properly */
812 __int64 in;
813 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000814 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
815 /* XXX Win32 supports time stamps past 2038; we currently don't */
816 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
817}
818
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819static void
820time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
821{
822 /* XXX endianness */
823 __int64 out;
824 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000825 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826 memcpy(out_ptr, &out, sizeof(out));
827}
828
Martin v. Löwis14694662006-02-03 12:54:16 +0000829/* Below, we *know* that ugo+r is 0444 */
830#if _S_IREAD != 0400
831#error Unsupported C library
832#endif
833static int
834attributes_to_mode(DWORD attr)
835{
836 int m = 0;
837 if (attr & FILE_ATTRIBUTE_DIRECTORY)
838 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
839 else
840 m |= _S_IFREG;
841 if (attr & FILE_ATTRIBUTE_READONLY)
842 m |= 0444;
843 else
844 m |= 0666;
845 return m;
846}
847
848static int
849attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
850{
851 memset(result, 0, sizeof(*result));
852 result->st_mode = attributes_to_mode(info->dwFileAttributes);
853 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
854 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
855 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
856 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
857
858 return 0;
859}
860
Thomas Wouters89f507f2006-12-13 04:49:30 +0000861/* Emulate GetFileAttributesEx[AW] on Windows 95 */
862static int checked = 0;
863static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
864static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
865static void
866check_gfax()
867{
868 HINSTANCE hKernel32;
869 if (checked)
870 return;
871 checked = 1;
872 hKernel32 = GetModuleHandle("KERNEL32");
873 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
874 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
875}
876
Guido van Rossumd8faa362007-04-27 19:54:29 +0000877static BOOL
878attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
879{
880 HANDLE hFindFile;
881 WIN32_FIND_DATAA FileData;
882 hFindFile = FindFirstFileA(pszFile, &FileData);
883 if (hFindFile == INVALID_HANDLE_VALUE)
884 return FALSE;
885 FindClose(hFindFile);
886 pfad->dwFileAttributes = FileData.dwFileAttributes;
887 pfad->ftCreationTime = FileData.ftCreationTime;
888 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
889 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
890 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
891 pfad->nFileSizeLow = FileData.nFileSizeLow;
892 return TRUE;
893}
894
895static BOOL
896attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
897{
898 HANDLE hFindFile;
899 WIN32_FIND_DATAW FileData;
900 hFindFile = FindFirstFileW(pszFile, &FileData);
901 if (hFindFile == INVALID_HANDLE_VALUE)
902 return FALSE;
903 FindClose(hFindFile);
904 pfad->dwFileAttributes = FileData.dwFileAttributes;
905 pfad->ftCreationTime = FileData.ftCreationTime;
906 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
907 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
908 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
909 pfad->nFileSizeLow = FileData.nFileSizeLow;
910 return TRUE;
911}
912
Thomas Wouters89f507f2006-12-13 04:49:30 +0000913static BOOL WINAPI
914Py_GetFileAttributesExA(LPCSTR pszFile,
915 GET_FILEEX_INFO_LEVELS level,
916 LPVOID pv)
917{
918 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000919 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
920 /* First try to use the system's implementation, if that is
921 available and either succeeds to gives an error other than
922 that it isn't implemented. */
923 check_gfax();
924 if (gfaxa) {
925 result = gfaxa(pszFile, level, pv);
926 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
927 return result;
928 }
929 /* It's either not present, or not implemented.
930 Emulate using FindFirstFile. */
931 if (level != GetFileExInfoStandard) {
932 SetLastError(ERROR_INVALID_PARAMETER);
933 return FALSE;
934 }
935 /* Use GetFileAttributes to validate that the file name
936 does not contain wildcards (which FindFirstFile would
937 accept). */
938 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
939 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000940 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000941}
942
943static BOOL WINAPI
944Py_GetFileAttributesExW(LPCWSTR pszFile,
945 GET_FILEEX_INFO_LEVELS level,
946 LPVOID pv)
947{
948 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000949 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
950 /* First try to use the system's implementation, if that is
951 available and either succeeds to gives an error other than
952 that it isn't implemented. */
953 check_gfax();
954 if (gfaxa) {
955 result = gfaxw(pszFile, level, pv);
956 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
957 return result;
958 }
959 /* It's either not present, or not implemented.
960 Emulate using FindFirstFile. */
961 if (level != GetFileExInfoStandard) {
962 SetLastError(ERROR_INVALID_PARAMETER);
963 return FALSE;
964 }
965 /* Use GetFileAttributes to validate that the file name
966 does not contain wildcards (which FindFirstFile would
967 accept). */
968 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
969 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000970 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000971}
972
Martin v. Löwis14694662006-02-03 12:54:16 +0000973static int
974win32_stat(const char* path, struct win32_stat *result)
975{
976 WIN32_FILE_ATTRIBUTE_DATA info;
977 int code;
978 char *dot;
979 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000980 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000981 if (GetLastError() != ERROR_SHARING_VIOLATION) {
982 /* Protocol violation: we explicitly clear errno, instead of
983 setting it to a POSIX error. Callers should use GetLastError. */
984 errno = 0;
985 return -1;
986 } else {
987 /* Could not get attributes on open file. Fall back to
988 reading the directory. */
989 if (!attributes_from_dir(path, &info)) {
990 /* Very strange. This should not fail now */
991 errno = 0;
992 return -1;
993 }
994 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000995 }
996 code = attribute_data_to_stat(&info, result);
997 if (code != 0)
998 return code;
999 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1000 dot = strrchr(path, '.');
1001 if (dot) {
1002 if (stricmp(dot, ".bat") == 0 ||
1003 stricmp(dot, ".cmd") == 0 ||
1004 stricmp(dot, ".exe") == 0 ||
1005 stricmp(dot, ".com") == 0)
1006 result->st_mode |= 0111;
1007 }
1008 return code;
1009}
1010
1011static int
1012win32_wstat(const wchar_t* path, struct win32_stat *result)
1013{
1014 int code;
1015 const wchar_t *dot;
1016 WIN32_FILE_ATTRIBUTE_DATA info;
1017 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001018 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001019 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1020 /* Protocol violation: we explicitly clear errno, instead of
1021 setting it to a POSIX error. Callers should use GetLastError. */
1022 errno = 0;
1023 return -1;
1024 } else {
1025 /* Could not get attributes on open file. Fall back to
1026 reading the directory. */
1027 if (!attributes_from_dir_w(path, &info)) {
1028 /* Very strange. This should not fail now */
1029 errno = 0;
1030 return -1;
1031 }
1032 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001033 }
1034 code = attribute_data_to_stat(&info, result);
1035 if (code < 0)
1036 return code;
1037 /* Set IFEXEC if it is an .exe, .bat, ... */
1038 dot = wcsrchr(path, '.');
1039 if (dot) {
1040 if (_wcsicmp(dot, L".bat") == 0 ||
1041 _wcsicmp(dot, L".cmd") == 0 ||
1042 _wcsicmp(dot, L".exe") == 0 ||
1043 _wcsicmp(dot, L".com") == 0)
1044 result->st_mode |= 0111;
1045 }
1046 return code;
1047}
1048
1049static int
1050win32_fstat(int file_number, struct win32_stat *result)
1051{
1052 BY_HANDLE_FILE_INFORMATION info;
1053 HANDLE h;
1054 int type;
1055
1056 h = (HANDLE)_get_osfhandle(file_number);
1057
1058 /* Protocol violation: we explicitly clear errno, instead of
1059 setting it to a POSIX error. Callers should use GetLastError. */
1060 errno = 0;
1061
1062 if (h == INVALID_HANDLE_VALUE) {
1063 /* This is really a C library error (invalid file handle).
1064 We set the Win32 error to the closes one matching. */
1065 SetLastError(ERROR_INVALID_HANDLE);
1066 return -1;
1067 }
1068 memset(result, 0, sizeof(*result));
1069
1070 type = GetFileType(h);
1071 if (type == FILE_TYPE_UNKNOWN) {
1072 DWORD error = GetLastError();
1073 if (error != 0) {
1074 return -1;
1075 }
1076 /* else: valid but unknown file */
1077 }
1078
1079 if (type != FILE_TYPE_DISK) {
1080 if (type == FILE_TYPE_CHAR)
1081 result->st_mode = _S_IFCHR;
1082 else if (type == FILE_TYPE_PIPE)
1083 result->st_mode = _S_IFIFO;
1084 return 0;
1085 }
1086
1087 if (!GetFileInformationByHandle(h, &info)) {
1088 return -1;
1089 }
1090
1091 /* similar to stat() */
1092 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1093 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1094 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1095 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1096 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1097 /* specific to fstat() */
1098 result->st_nlink = info.nNumberOfLinks;
1099 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1100 return 0;
1101}
1102
1103#endif /* MS_WINDOWS */
1104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001105PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001106"stat_result: Result from stat or lstat.\n\n\
1107This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001108 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001109or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1110\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001111Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1112or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001113\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001114See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115
1116static PyStructSequence_Field stat_result_fields[] = {
1117 {"st_mode", "protection bits"},
1118 {"st_ino", "inode"},
1119 {"st_dev", "device"},
1120 {"st_nlink", "number of hard links"},
1121 {"st_uid", "user ID of owner"},
1122 {"st_gid", "group ID of owner"},
1123 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001124 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1125 {NULL, "integer time of last access"},
1126 {NULL, "integer time of last modification"},
1127 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001128 {"st_atime", "time of last access"},
1129 {"st_mtime", "time of last modification"},
1130 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001131#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001132 {"st_blksize", "blocksize for filesystem I/O"},
1133#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001134#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001135 {"st_blocks", "number of blocks allocated"},
1136#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001137#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001138 {"st_rdev", "device type (if inode device)"},
1139#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001140#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1141 {"st_flags", "user defined flags for file"},
1142#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001143#ifdef HAVE_STRUCT_STAT_ST_GEN
1144 {"st_gen", "generation number"},
1145#endif
1146#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1147 {"st_birthtime", "time of creation"},
1148#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149 {0}
1150};
1151
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001152#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001153#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001154#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001155#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001156#endif
1157
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001158#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001159#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1160#else
1161#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1162#endif
1163
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001164#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001165#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1166#else
1167#define ST_RDEV_IDX ST_BLOCKS_IDX
1168#endif
1169
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001170#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1171#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1172#else
1173#define ST_FLAGS_IDX ST_RDEV_IDX
1174#endif
1175
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001176#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001177#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001178#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001179#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001180#endif
1181
1182#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1183#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1184#else
1185#define ST_BIRTHTIME_IDX ST_GEN_IDX
1186#endif
1187
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001188static PyStructSequence_Desc stat_result_desc = {
1189 "stat_result", /* name */
1190 stat_result__doc__, /* doc */
1191 stat_result_fields,
1192 10
1193};
1194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001195PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001196"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1197This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001198 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001199or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001200\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001201See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001202
1203static PyStructSequence_Field statvfs_result_fields[] = {
1204 {"f_bsize", },
1205 {"f_frsize", },
1206 {"f_blocks", },
1207 {"f_bfree", },
1208 {"f_bavail", },
1209 {"f_files", },
1210 {"f_ffree", },
1211 {"f_favail", },
1212 {"f_flag", },
1213 {"f_namemax",},
1214 {0}
1215};
1216
1217static PyStructSequence_Desc statvfs_result_desc = {
1218 "statvfs_result", /* name */
1219 statvfs_result__doc__, /* doc */
1220 statvfs_result_fields,
1221 10
1222};
1223
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001225static PyTypeObject StatResultType;
1226static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001227static newfunc structseq_new;
1228
1229static PyObject *
1230statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1231{
1232 PyStructSequence *result;
1233 int i;
1234
1235 result = (PyStructSequence*)structseq_new(type, args, kwds);
1236 if (!result)
1237 return NULL;
1238 /* If we have been initialized from a tuple,
1239 st_?time might be set to None. Initialize it
1240 from the int slots. */
1241 for (i = 7; i <= 9; i++) {
1242 if (result->ob_item[i+3] == Py_None) {
1243 Py_DECREF(Py_None);
1244 Py_INCREF(result->ob_item[i]);
1245 result->ob_item[i+3] = result->ob_item[i];
1246 }
1247 }
1248 return (PyObject*)result;
1249}
1250
1251
1252
1253/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001254static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001255
1256PyDoc_STRVAR(stat_float_times__doc__,
1257"stat_float_times([newval]) -> oldval\n\n\
1258Determine whether os.[lf]stat represents time stamps as float objects.\n\
1259If newval is True, future calls to stat() return floats, if it is False,\n\
1260future calls return ints. \n\
1261If newval is omitted, return the current setting.\n");
1262
1263static PyObject*
1264stat_float_times(PyObject* self, PyObject *args)
1265{
1266 int newval = -1;
1267 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1268 return NULL;
1269 if (newval == -1)
1270 /* Return old value */
1271 return PyBool_FromLong(_stat_float_times);
1272 _stat_float_times = newval;
1273 Py_INCREF(Py_None);
1274 return Py_None;
1275}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001276
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001277static void
1278fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1279{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001280 PyObject *fval,*ival;
1281#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001282 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001283#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001284 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001285#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001286 if (!ival)
1287 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001288 if (_stat_float_times) {
1289 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1290 } else {
1291 fval = ival;
1292 Py_INCREF(fval);
1293 }
1294 PyStructSequence_SET_ITEM(v, index, ival);
1295 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001296}
1297
Tim Peters5aa91602002-01-30 05:46:57 +00001298/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001299 (used by posix_stat() and posix_fstat()) */
1300static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001301_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001302{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001303 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001304 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001305 if (v == NULL)
1306 return NULL;
1307
Christian Heimes217cfd12007-12-02 14:31:20 +00001308 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001309#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001310 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001311 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001312#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001313 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001314#endif
1315#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001316 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001318#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001319 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001320#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001321 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1322 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1323 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001324#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001325 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001326 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001327#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001328 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001329#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001330
Martin v. Löwis14694662006-02-03 12:54:16 +00001331#if defined(HAVE_STAT_TV_NSEC)
1332 ansec = st->st_atim.tv_nsec;
1333 mnsec = st->st_mtim.tv_nsec;
1334 cnsec = st->st_ctim.tv_nsec;
1335#elif defined(HAVE_STAT_TV_NSEC2)
1336 ansec = st->st_atimespec.tv_nsec;
1337 mnsec = st->st_mtimespec.tv_nsec;
1338 cnsec = st->st_ctimespec.tv_nsec;
1339#elif defined(HAVE_STAT_NSEC)
1340 ansec = st->st_atime_nsec;
1341 mnsec = st->st_mtime_nsec;
1342 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001343#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001344 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001346 fill_time(v, 7, st->st_atime, ansec);
1347 fill_time(v, 8, st->st_mtime, mnsec);
1348 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001349
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001350#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001351 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001352 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001353#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001354#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001355 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001356 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001357#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001358#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001360 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001361#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001362#ifdef HAVE_STRUCT_STAT_ST_GEN
1363 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001364 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001365#endif
1366#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1367 {
1368 PyObject *val;
1369 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001370 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001371#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001372 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001373#else
1374 bnsec = 0;
1375#endif
1376 if (_stat_float_times) {
1377 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1378 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001379 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001380 }
1381 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1382 val);
1383 }
1384#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001385#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1386 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001387 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001388#endif
Fred Drake699f3522000-06-29 21:12:41 +00001389
1390 if (PyErr_Occurred()) {
1391 Py_DECREF(v);
1392 return NULL;
1393 }
1394
1395 return v;
1396}
1397
Martin v. Löwisd8948722004-06-02 09:57:56 +00001398#ifdef MS_WINDOWS
1399
1400/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1401 where / can be used in place of \ and the trailing slash is optional.
1402 Both SERVER and SHARE must have at least one character.
1403*/
1404
1405#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1406#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001407#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001408#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001409#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001410
Tim Peters4ad82172004-08-30 17:02:04 +00001411static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001412IsUNCRootA(char *path, int pathlen)
1413{
1414 #define ISSLASH ISSLASHA
1415
1416 int i, share;
1417
1418 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1419 /* minimum UNCRoot is \\x\y */
1420 return FALSE;
1421 for (i = 2; i < pathlen ; i++)
1422 if (ISSLASH(path[i])) break;
1423 if (i == 2 || i == pathlen)
1424 /* do not allow \\\SHARE or \\SERVER */
1425 return FALSE;
1426 share = i+1;
1427 for (i = share; i < pathlen; i++)
1428 if (ISSLASH(path[i])) break;
1429 return (i != share && (i == pathlen || i == pathlen-1));
1430
1431 #undef ISSLASH
1432}
1433
1434#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001435static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001436IsUNCRootW(Py_UNICODE *path, int pathlen)
1437{
1438 #define ISSLASH ISSLASHW
1439
1440 int i, share;
1441
1442 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1443 /* minimum UNCRoot is \\x\y */
1444 return FALSE;
1445 for (i = 2; i < pathlen ; i++)
1446 if (ISSLASH(path[i])) break;
1447 if (i == 2 || i == pathlen)
1448 /* do not allow \\\SHARE or \\SERVER */
1449 return FALSE;
1450 share = i+1;
1451 for (i = share; i < pathlen; i++)
1452 if (ISSLASH(path[i])) break;
1453 return (i != share && (i == pathlen || i == pathlen-1));
1454
1455 #undef ISSLASH
1456}
1457#endif /* Py_WIN_WIDE_FILENAMES */
1458#endif /* MS_WINDOWS */
1459
Barry Warsaw53699e91996-12-10 23:23:01 +00001460static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001461posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001462 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001463#ifdef __VMS
1464 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1465#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001466 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001467#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001468 char *wformat,
1469 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001470{
Fred Drake699f3522000-06-29 21:12:41 +00001471 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001472 char *path = NULL; /* pass this to stat; do not free() it */
1473 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001474 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001475 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001476
1477#ifdef Py_WIN_WIDE_FILENAMES
1478 /* If on wide-character-capable OS see if argument
1479 is Unicode and if so use wide API. */
1480 if (unicode_file_names()) {
1481 PyUnicodeObject *po;
1482 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001483 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1484
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001485 Py_BEGIN_ALLOW_THREADS
1486 /* PyUnicode_AS_UNICODE result OK without
1487 thread lock as it is a simple dereference. */
1488 res = wstatfunc(wpath, &st);
1489 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001490
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001491 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001492 return win32_error_unicode("stat", wpath);
1493 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001494 }
1495 /* Drop the argument parsing error as narrow strings
1496 are also valid. */
1497 PyErr_Clear();
1498 }
1499#endif
1500
Tim Peters5aa91602002-01-30 05:46:57 +00001501 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001502 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001503 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001504 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001505
Barry Warsaw53699e91996-12-10 23:23:01 +00001506 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001507 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001508 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001509
1510 if (res != 0) {
1511#ifdef MS_WINDOWS
1512 result = win32_error("stat", pathfree);
1513#else
1514 result = posix_error_with_filename(pathfree);
1515#endif
1516 }
1517 else
1518 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001519
Tim Peters500bd032001-12-19 19:05:01 +00001520 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001521 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001522}
1523
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001524/* POSIX methods */
1525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001527"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001528Use the real uid/gid to test for access to a path. Note that most\n\
1529operations will use the effective uid/gid, therefore this routine can\n\
1530be used in a suid/sgid environment to test if the invoking user has the\n\
1531specified access to the path. The mode argument can be F_OK to test\n\
1532existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001533
1534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001535posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001536{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001537 char *path;
1538 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001539
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001540#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001542 if (unicode_file_names()) {
1543 PyUnicodeObject *po;
1544 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1545 Py_BEGIN_ALLOW_THREADS
1546 /* PyUnicode_AS_UNICODE OK without thread lock as
1547 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001548 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001549 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001550 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001551 }
1552 /* Drop the argument parsing error as narrow strings
1553 are also valid. */
1554 PyErr_Clear();
1555 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001556 if (!PyArg_ParseTuple(args, "eti:access",
1557 Py_FileSystemDefaultEncoding, &path, &mode))
1558 return 0;
1559 Py_BEGIN_ALLOW_THREADS
1560 attr = GetFileAttributesA(path);
1561 Py_END_ALLOW_THREADS
1562 PyMem_Free(path);
1563finish:
1564 if (attr == 0xFFFFFFFF)
1565 /* File does not exist, or cannot read attributes */
1566 return PyBool_FromLong(0);
1567 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001568 the file isn't read-only, or if it's a directory, as there are
1569 no read-only directories on Windows. */
1570 return PyBool_FromLong(!(mode & 2)
1571 || !(attr & FILE_ATTRIBUTE_READONLY)
1572 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001573#else
1574 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001575 if (!PyArg_ParseTuple(args, "eti:access",
1576 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001577 return NULL;
1578 Py_BEGIN_ALLOW_THREADS
1579 res = access(path, mode);
1580 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001581 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001582 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001584}
1585
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#ifndef F_OK
1587#define F_OK 0
1588#endif
1589#ifndef R_OK
1590#define R_OK 4
1591#endif
1592#ifndef W_OK
1593#define W_OK 2
1594#endif
1595#ifndef X_OK
1596#define X_OK 1
1597#endif
1598
1599#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001601"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001603
1604static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001605posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001606{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001607 int id;
1608 char *ret;
1609
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001610 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001611 return NULL;
1612
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001613#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001614 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001615 if (id == 0) {
1616 ret = ttyname();
1617 }
1618 else {
1619 ret = NULL;
1620 }
1621#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001622 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001624 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001625 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001626 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001627}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001628#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001629
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001630#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001632"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001634
1635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001636posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001637{
1638 char *ret;
1639 char buffer[L_ctermid];
1640
Greg Wardb48bc172000-03-01 21:51:56 +00001641#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001642 ret = ctermid_r(buffer);
1643#else
1644 ret = ctermid(buffer);
1645#endif
1646 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001647 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001648 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001649}
1650#endif
1651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001653"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001655
Barry Warsaw53699e91996-12-10 23:23:01 +00001656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001657posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001658{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001659#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001660 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001661#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001662 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001663#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001664 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001665#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001666 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001667#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001668}
1669
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001671PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001672"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001673Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001674opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001675
1676static PyObject *
1677posix_fchdir(PyObject *self, PyObject *fdobj)
1678{
1679 return posix_fildes(fdobj, fchdir);
1680}
1681#endif /* HAVE_FCHDIR */
1682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001684PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001685"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001687
Barry Warsaw53699e91996-12-10 23:23:01 +00001688static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001689posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001690{
Mark Hammondef8b6542001-05-13 08:04:26 +00001691 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001692 int i;
1693 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001694#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001696 if (unicode_file_names()) {
1697 PyUnicodeObject *po;
1698 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1699 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001700 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1701 if (attr != 0xFFFFFFFF) {
1702 if (i & _S_IWRITE)
1703 attr &= ~FILE_ATTRIBUTE_READONLY;
1704 else
1705 attr |= FILE_ATTRIBUTE_READONLY;
1706 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1707 }
1708 else
1709 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001710 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001711 if (!res)
1712 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001713 PyUnicode_AS_UNICODE(po));
1714 Py_INCREF(Py_None);
1715 return Py_None;
1716 }
1717 /* Drop the argument parsing error as narrow strings
1718 are also valid. */
1719 PyErr_Clear();
1720 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001721 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1722 &path, &i))
1723 return NULL;
1724 Py_BEGIN_ALLOW_THREADS
1725 attr = GetFileAttributesA(path);
1726 if (attr != 0xFFFFFFFF) {
1727 if (i & _S_IWRITE)
1728 attr &= ~FILE_ATTRIBUTE_READONLY;
1729 else
1730 attr |= FILE_ATTRIBUTE_READONLY;
1731 res = SetFileAttributesA(path, attr);
1732 }
1733 else
1734 res = 0;
1735 Py_END_ALLOW_THREADS
1736 if (!res) {
1737 win32_error("chmod", path);
1738 PyMem_Free(path);
1739 return NULL;
1740 }
1741 PyMem_Free(path);
1742 Py_INCREF(Py_None);
1743 return Py_None;
1744#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001745 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001746 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001747 return NULL;
1748 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001749 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001750 Py_END_ALLOW_THREADS
1751 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001752 return posix_error_with_allocated_filename(path);
1753 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001754 Py_INCREF(Py_None);
1755 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001756#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001757}
1758
Christian Heimes4e30a842007-11-30 22:12:06 +00001759#ifdef HAVE_FCHMOD
1760PyDoc_STRVAR(posix_fchmod__doc__,
1761"fchmod(fd, mode)\n\n\
1762Change the access permissions of the file given by file\n\
1763descriptor fd.");
1764
1765static PyObject *
1766posix_fchmod(PyObject *self, PyObject *args)
1767{
1768 int fd, mode, res;
1769 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1770 return NULL;
1771 Py_BEGIN_ALLOW_THREADS
1772 res = fchmod(fd, mode);
1773 Py_END_ALLOW_THREADS
1774 if (res < 0)
1775 return posix_error();
1776 Py_RETURN_NONE;
1777}
1778#endif /* HAVE_FCHMOD */
1779
1780#ifdef HAVE_LCHMOD
1781PyDoc_STRVAR(posix_lchmod__doc__,
1782"lchmod(path, mode)\n\n\
1783Change the access permissions of a file. If path is a symlink, this\n\
1784affects the link itself rather than the target.");
1785
1786static PyObject *
1787posix_lchmod(PyObject *self, PyObject *args)
1788{
1789 char *path = NULL;
1790 int i;
1791 int res;
1792 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1793 &path, &i))
1794 return NULL;
1795 Py_BEGIN_ALLOW_THREADS
1796 res = lchmod(path, i);
1797 Py_END_ALLOW_THREADS
1798 if (res < 0)
1799 return posix_error_with_allocated_filename(path);
1800 PyMem_Free(path);
1801 Py_RETURN_NONE;
1802}
1803#endif /* HAVE_LCHMOD */
1804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001805
Thomas Wouterscf297e42007-02-23 15:07:44 +00001806#ifdef HAVE_CHFLAGS
1807PyDoc_STRVAR(posix_chflags__doc__,
1808"chflags(path, flags)\n\n\
1809Set file flags.");
1810
1811static PyObject *
1812posix_chflags(PyObject *self, PyObject *args)
1813{
1814 char *path;
1815 unsigned long flags;
1816 int res;
1817 if (!PyArg_ParseTuple(args, "etk:chflags",
1818 Py_FileSystemDefaultEncoding, &path, &flags))
1819 return NULL;
1820 Py_BEGIN_ALLOW_THREADS
1821 res = chflags(path, flags);
1822 Py_END_ALLOW_THREADS
1823 if (res < 0)
1824 return posix_error_with_allocated_filename(path);
1825 PyMem_Free(path);
1826 Py_INCREF(Py_None);
1827 return Py_None;
1828}
1829#endif /* HAVE_CHFLAGS */
1830
1831#ifdef HAVE_LCHFLAGS
1832PyDoc_STRVAR(posix_lchflags__doc__,
1833"lchflags(path, flags)\n\n\
1834Set file flags.\n\
1835This function will not follow symbolic links.");
1836
1837static PyObject *
1838posix_lchflags(PyObject *self, PyObject *args)
1839{
1840 char *path;
1841 unsigned long flags;
1842 int res;
1843 if (!PyArg_ParseTuple(args, "etk:lchflags",
1844 Py_FileSystemDefaultEncoding, &path, &flags))
1845 return NULL;
1846 Py_BEGIN_ALLOW_THREADS
1847 res = lchflags(path, flags);
1848 Py_END_ALLOW_THREADS
1849 if (res < 0)
1850 return posix_error_with_allocated_filename(path);
1851 PyMem_Free(path);
1852 Py_INCREF(Py_None);
1853 return Py_None;
1854}
1855#endif /* HAVE_LCHFLAGS */
1856
Martin v. Löwis244edc82001-10-04 22:44:26 +00001857#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001859"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001860Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001861
1862static PyObject *
1863posix_chroot(PyObject *self, PyObject *args)
1864{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001865 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001866}
1867#endif
1868
Guido van Rossum21142a01999-01-08 21:05:37 +00001869#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001870PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001871"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001873
1874static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001875posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001876{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001877 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001878}
1879#endif /* HAVE_FSYNC */
1880
1881#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001882
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001883#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001884extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1885#endif
1886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001888"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001889force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001891
1892static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001893posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001894{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001895 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001896}
1897#endif /* HAVE_FDATASYNC */
1898
1899
Fredrik Lundh10723342000-07-10 16:38:09 +00001900#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001902"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001903Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001904
Barry Warsaw53699e91996-12-10 23:23:01 +00001905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001906posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001907{
Mark Hammondef8b6542001-05-13 08:04:26 +00001908 char *path = NULL;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001909 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001910 int res;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001911 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001912 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001913 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001914 return NULL;
1915 Py_BEGIN_ALLOW_THREADS
1916 res = chown(path, (uid_t) uid, (gid_t) gid);
1917 Py_END_ALLOW_THREADS
1918 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001919 return posix_error_with_allocated_filename(path);
1920 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001921 Py_INCREF(Py_None);
1922 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001923}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001924#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001925
Christian Heimes4e30a842007-11-30 22:12:06 +00001926#ifdef HAVE_FCHOWN
1927PyDoc_STRVAR(posix_fchown__doc__,
1928"fchown(fd, uid, gid)\n\n\
1929Change the owner and group id of the file given by file descriptor\n\
1930fd to the numeric uid and gid.");
1931
1932static PyObject *
1933posix_fchown(PyObject *self, PyObject *args)
1934{
1935 int fd, uid, gid;
1936 int res;
1937 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1938 return NULL;
1939 Py_BEGIN_ALLOW_THREADS
1940 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1941 Py_END_ALLOW_THREADS
1942 if (res < 0)
1943 return posix_error();
1944 Py_RETURN_NONE;
1945}
1946#endif /* HAVE_FCHOWN */
1947
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001948#ifdef HAVE_LCHOWN
1949PyDoc_STRVAR(posix_lchown__doc__,
1950"lchown(path, uid, gid)\n\n\
1951Change the owner and group id of path to the numeric uid and gid.\n\
1952This function will not follow symbolic links.");
1953
1954static PyObject *
1955posix_lchown(PyObject *self, PyObject *args)
1956{
1957 char *path = NULL;
1958 int uid, gid;
1959 int res;
1960 if (!PyArg_ParseTuple(args, "etii:lchown",
1961 Py_FileSystemDefaultEncoding, &path,
1962 &uid, &gid))
1963 return NULL;
1964 Py_BEGIN_ALLOW_THREADS
1965 res = lchown(path, (uid_t) uid, (gid_t) gid);
1966 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001967 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001968 return posix_error_with_allocated_filename(path);
1969 PyMem_Free(path);
1970 Py_INCREF(Py_None);
1971 return Py_None;
1972}
1973#endif /* HAVE_LCHOWN */
1974
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Guido van Rossum36bc6801995-06-14 22:54:23 +00001976#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001977PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001978"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001979Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001980
Barry Warsaw53699e91996-12-10 23:23:01 +00001981static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001982posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001983{
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001984 int bufsize_incr = 1024;
1985 int bufsize = 0;
1986 char *tmpbuf = NULL;
1987 char *res = NULL;
1988 PyObject *dynamic_return;
Neal Norwitze241ce82003-02-17 18:17:05 +00001989
Barry Warsaw53699e91996-12-10 23:23:01 +00001990 Py_BEGIN_ALLOW_THREADS
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001991 do {
1992 bufsize = bufsize + bufsize_incr;
1993 tmpbuf = malloc(bufsize);
1994 if (tmpbuf == NULL) {
1995 break;
1996 }
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001997#if defined(PYOS_OS2) && defined(PYCC_GCC)
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001998 res = _getcwd2(tmpbuf, bufsize);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001999#else
Benjamin Petersondcf97b92008-07-02 17:30:14 +00002000 res = getcwd(tmpbuf, bufsize);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002001#endif
Benjamin Petersondcf97b92008-07-02 17:30:14 +00002002
2003 if (res == NULL) {
2004 free(tmpbuf);
2005 }
2006 } while ((res == NULL) && (errno == ERANGE));
Barry Warsaw53699e91996-12-10 23:23:01 +00002007 Py_END_ALLOW_THREADS
Benjamin Petersondcf97b92008-07-02 17:30:14 +00002008
Guido van Rossumff4949e1992-08-05 19:58:53 +00002009 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002010 return posix_error();
Benjamin Petersondcf97b92008-07-02 17:30:14 +00002011
2012 dynamic_return = PyUnicode_FromString(tmpbuf);
2013 free(tmpbuf);
2014
2015 return dynamic_return;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002016}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017
2018PyDoc_STRVAR(posix_getcwdu__doc__,
2019"getcwdu() -> path\n\n\
2020Return a unicode string representing the current working directory.");
2021
2022static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002023posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002024{
2025 char buf[1026];
2026 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002027
2028#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00002029 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002030 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002031 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002032 wchar_t *wbuf2 = wbuf;
2033 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002035 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2036 /* If the buffer is large enough, len does not include the
2037 terminating \0. If the buffer is too small, len includes
2038 the space needed for the terminator. */
2039 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2040 wbuf2 = malloc(len * sizeof(wchar_t));
2041 if (wbuf2)
2042 len = GetCurrentDirectoryW(len, wbuf2);
2043 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002044 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002045 if (!wbuf2) {
2046 PyErr_NoMemory();
2047 return NULL;
2048 }
2049 if (!len) {
2050 if (wbuf2 != wbuf) free(wbuf2);
2051 return win32_error("getcwdu", NULL);
2052 }
2053 resobj = PyUnicode_FromWideChar(wbuf2, len);
2054 if (wbuf2 != wbuf) free(wbuf2);
2055 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002056 }
2057#endif
2058
2059 Py_BEGIN_ALLOW_THREADS
2060#if defined(PYOS_OS2) && defined(PYCC_GCC)
2061 res = _getcwd2(buf, sizeof buf);
2062#else
2063 res = getcwd(buf, sizeof buf);
2064#endif
2065 Py_END_ALLOW_THREADS
2066 if (res == NULL)
2067 return posix_error();
2068 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2069}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002070#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002072
Guido van Rossumb6775db1994-08-01 11:34:53 +00002073#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002075"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002076Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002077
Barry Warsaw53699e91996-12-10 23:23:01 +00002078static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002079posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002080{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002081 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002082}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002083#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002084
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002086PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002087"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088Return a list containing the names of the entries in the directory.\n\
2089\n\
2090 path: path of directory to list\n\
2091\n\
2092The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002093entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002094
Barry Warsaw53699e91996-12-10 23:23:01 +00002095static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002096posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002097{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002098 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002099 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002100#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002101
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002104 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002105 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002106 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002107 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002108 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002109
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002110#ifdef Py_WIN_WIDE_FILENAMES
2111 /* If on wide-character-capable OS see if argument
2112 is Unicode and if so use wide API. */
2113 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002114 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002115 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2116 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002117 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002118 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002119 /* Overallocate for \\*.*\0 */
2120 len = PyUnicode_GET_SIZE(po);
2121 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2122 if (!wnamebuf) {
2123 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002124 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002125 }
2126 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2127 wch = len > 0 ? wnamebuf[len-1] : '\0';
2128 if (wch != L'/' && wch != L'\\' && wch != L':')
2129 wnamebuf[len++] = L'\\';
2130 wcscpy(wnamebuf + len, L"*.*");
2131 if ((d = PyList_New(0)) == NULL) {
2132 free(wnamebuf);
2133 return NULL;
2134 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002135 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2136 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002137 int error = GetLastError();
2138 if (error == ERROR_FILE_NOT_FOUND) {
2139 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002140 return d;
2141 }
2142 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002143 win32_error_unicode("FindFirstFileW", wnamebuf);
2144 free(wnamebuf);
2145 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002146 }
2147 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002148 /* Skip over . and .. */
2149 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2150 wcscmp(wFileData.cFileName, L"..") != 0) {
2151 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2152 if (v == NULL) {
2153 Py_DECREF(d);
2154 d = NULL;
2155 break;
2156 }
2157 if (PyList_Append(d, v) != 0) {
2158 Py_DECREF(v);
2159 Py_DECREF(d);
2160 d = NULL;
2161 break;
2162 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002163 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002164 }
Georg Brandl622927b2006-03-07 12:48:03 +00002165 Py_BEGIN_ALLOW_THREADS
2166 result = FindNextFileW(hFindFile, &wFileData);
2167 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002168 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2169 it got to the end of the directory. */
2170 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2171 Py_DECREF(d);
2172 win32_error_unicode("FindNextFileW", wnamebuf);
2173 FindClose(hFindFile);
2174 free(wnamebuf);
2175 return NULL;
2176 }
Georg Brandl622927b2006-03-07 12:48:03 +00002177 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002178
2179 if (FindClose(hFindFile) == FALSE) {
2180 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002181 win32_error_unicode("FindClose", wnamebuf);
2182 free(wnamebuf);
2183 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002184 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002185 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002186 return d;
2187 }
2188 /* Drop the argument parsing error as narrow strings
2189 are also valid. */
2190 PyErr_Clear();
2191 }
2192#endif
2193
Tim Peters5aa91602002-01-30 05:46:57 +00002194 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002195 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002196 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002197 if (len > 0) {
2198 char ch = namebuf[len-1];
2199 if (ch != SEP && ch != ALTSEP && ch != ':')
2200 namebuf[len++] = '/';
2201 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002202 strcpy(namebuf + len, "*.*");
2203
Barry Warsaw53699e91996-12-10 23:23:01 +00002204 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002205 return NULL;
2206
2207 hFindFile = FindFirstFile(namebuf, &FileData);
2208 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002209 int error = GetLastError();
2210 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002211 return d;
2212 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002213 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002214 }
2215 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002216 /* Skip over . and .. */
2217 if (strcmp(FileData.cFileName, ".") != 0 &&
2218 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002219 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002220 if (v == NULL) {
2221 Py_DECREF(d);
2222 d = NULL;
2223 break;
2224 }
2225 if (PyList_Append(d, v) != 0) {
2226 Py_DECREF(v);
2227 Py_DECREF(d);
2228 d = NULL;
2229 break;
2230 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002231 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002232 }
Georg Brandl622927b2006-03-07 12:48:03 +00002233 Py_BEGIN_ALLOW_THREADS
2234 result = FindNextFile(hFindFile, &FileData);
2235 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002236 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2237 it got to the end of the directory. */
2238 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2239 Py_DECREF(d);
2240 win32_error("FindNextFile", namebuf);
2241 FindClose(hFindFile);
2242 return NULL;
2243 }
Georg Brandl622927b2006-03-07 12:48:03 +00002244 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002245
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002246 if (FindClose(hFindFile) == FALSE) {
2247 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002248 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002249 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250
2251 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002252
Tim Peters0bb44a42000-09-15 07:44:49 +00002253#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002254
2255#ifndef MAX_PATH
2256#define MAX_PATH CCHMAXPATH
2257#endif
2258 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002259 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002260 PyObject *d, *v;
2261 char namebuf[MAX_PATH+5];
2262 HDIR hdir = 1;
2263 ULONG srchcnt = 1;
2264 FILEFINDBUF3 ep;
2265 APIRET rc;
2266
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002267 if (!PyArg_ParseTuple(args, "et#:listdir",
2268 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002269 return NULL;
2270 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002271 PyMem_Free(name);
2272 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273 return NULL;
2274 }
2275 strcpy(namebuf, name);
2276 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002277 if (*pt == ALTSEP)
2278 *pt = SEP;
2279 if (namebuf[len-1] != SEP)
2280 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002281 strcpy(namebuf + len, "*.*");
2282
Neal Norwitz6c913782007-10-14 03:23:09 +00002283 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002284 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002285 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002286 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002287
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002288 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2289 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002290 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002291 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2292 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2293 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002294
2295 if (rc != NO_ERROR) {
2296 errno = ENOENT;
Neal Norwitz6c913782007-10-14 03:23:09 +00002297 return posix_error_with_allocated_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002298 }
2299
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002300 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002301 do {
2302 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002303 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002304 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002305
2306 strcpy(namebuf, ep.achName);
2307
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002308 /* Leave Case of Name Alone -- In Native Form */
2309 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002310
Christian Heimes72b710a2008-05-26 13:28:38 +00002311 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002312 if (v == NULL) {
2313 Py_DECREF(d);
2314 d = NULL;
2315 break;
2316 }
2317 if (PyList_Append(d, v) != 0) {
2318 Py_DECREF(v);
2319 Py_DECREF(d);
2320 d = NULL;
2321 break;
2322 }
2323 Py_DECREF(v);
2324 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2325 }
2326
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002327 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002328 return d;
2329#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002330
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002331 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002332 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002333 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002334 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002335 int arg_is_unicode = 1;
2336
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002337 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002338 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2339 arg_is_unicode = 0;
2340 PyErr_Clear();
2341 }
2342 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002343 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002344 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002345 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002346 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002347 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002348 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002349 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002350 return NULL;
2351 }
Georg Brandl622927b2006-03-07 12:48:03 +00002352 for (;;) {
2353 Py_BEGIN_ALLOW_THREADS
2354 ep = readdir(dirp);
2355 Py_END_ALLOW_THREADS
2356 if (ep == NULL)
2357 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002358 if (ep->d_name[0] == '.' &&
2359 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002360 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002361 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002362 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002363 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002364 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002365 d = NULL;
2366 break;
2367 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002368 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002369 PyObject *w;
2370
2371 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002372 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002373 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002374 if (w != NULL) {
2375 Py_DECREF(v);
2376 v = w;
2377 }
2378 else {
2379 /* fall back to the original byte string, as
2380 discussed in patch #683592 */
2381 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002382 }
Just van Rossum46c97842003-02-25 21:42:15 +00002383 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002384 if (PyList_Append(d, v) != 0) {
2385 Py_DECREF(v);
2386 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002387 d = NULL;
2388 break;
2389 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002390 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002391 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002392 if (errno != 0 && d != NULL) {
2393 /* readdir() returned NULL and set errno */
2394 closedir(dirp);
2395 Py_DECREF(d);
2396 return posix_error_with_allocated_filename(name);
2397 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002398 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002399 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002400
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002401 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002402
Tim Peters0bb44a42000-09-15 07:44:49 +00002403#endif /* which OS */
2404} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002405
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002406#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002407/* A helper function for abspath on win32 */
2408static PyObject *
2409posix__getfullpathname(PyObject *self, PyObject *args)
2410{
2411 /* assume encoded strings wont more than double no of chars */
2412 char inbuf[MAX_PATH*2];
2413 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002414 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002415 char outbuf[MAX_PATH*2];
2416 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002417#ifdef Py_WIN_WIDE_FILENAMES
2418 if (unicode_file_names()) {
2419 PyUnicodeObject *po;
2420 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2421 Py_UNICODE woutbuf[MAX_PATH*2];
2422 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002423 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002424 sizeof(woutbuf)/sizeof(woutbuf[0]),
2425 woutbuf, &wtemp))
2426 return win32_error("GetFullPathName", "");
2427 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2428 }
2429 /* Drop the argument parsing error as narrow strings
2430 are also valid. */
2431 PyErr_Clear();
2432 }
2433#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002434 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2435 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002436 &insize))
2437 return NULL;
2438 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2439 outbuf, &temp))
2440 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002441 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2442 return PyUnicode_Decode(outbuf, strlen(outbuf),
2443 Py_FileSystemDefaultEncoding, NULL);
2444 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002445 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002446} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002447#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002449PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002450"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002451Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002452
Barry Warsaw53699e91996-12-10 23:23:01 +00002453static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002454posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002455{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002456 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002457 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002458 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002459
2460#ifdef Py_WIN_WIDE_FILENAMES
2461 if (unicode_file_names()) {
2462 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002463 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002464 Py_BEGIN_ALLOW_THREADS
2465 /* PyUnicode_AS_UNICODE OK without thread lock as
2466 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002467 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002468 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002469 if (!res)
2470 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002471 Py_INCREF(Py_None);
2472 return Py_None;
2473 }
2474 /* Drop the argument parsing error as narrow strings
2475 are also valid. */
2476 PyErr_Clear();
2477 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002478 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2479 Py_FileSystemDefaultEncoding, &path, &mode))
2480 return NULL;
2481 Py_BEGIN_ALLOW_THREADS
2482 /* PyUnicode_AS_UNICODE OK without thread lock as
2483 it is a simple dereference. */
2484 res = CreateDirectoryA(path, NULL);
2485 Py_END_ALLOW_THREADS
2486 if (!res) {
2487 win32_error("mkdir", path);
2488 PyMem_Free(path);
2489 return NULL;
2490 }
2491 PyMem_Free(path);
2492 Py_INCREF(Py_None);
2493 return Py_None;
2494#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002495
Tim Peters5aa91602002-01-30 05:46:57 +00002496 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002497 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002498 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002499 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002500#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002501 res = mkdir(path);
2502#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002503 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002504#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002505 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002506 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002507 return posix_error_with_allocated_filename(path);
2508 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002509 Py_INCREF(Py_None);
2510 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002511#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002512}
2513
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002514
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002515/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2516#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002517#include <sys/resource.h>
2518#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002519
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002520
2521#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002522PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002523"nice(inc) -> new_priority\n\n\
2524Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002525
Barry Warsaw53699e91996-12-10 23:23:01 +00002526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002527posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002528{
2529 int increment, value;
2530
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002531 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002532 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002533
2534 /* There are two flavours of 'nice': one that returns the new
2535 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002536 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2537 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002538
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002539 If we are of the nice family that returns the new priority, we
2540 need to clear errno before the call, and check if errno is filled
2541 before calling posix_error() on a returnvalue of -1, because the
2542 -1 may be the actual new priority! */
2543
2544 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002545 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002546#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002547 if (value == 0)
2548 value = getpriority(PRIO_PROCESS, 0);
2549#endif
2550 if (value == -1 && errno != 0)
2551 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002552 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002553 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002554}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002555#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002557PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002558"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002559Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002560
Barry Warsaw53699e91996-12-10 23:23:01 +00002561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002562posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002563{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002564#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002565 PyObject *o1, *o2;
2566 char *p1, *p2;
2567 BOOL result;
2568 if (unicode_file_names()) {
2569 if (!PyArg_ParseTuple(args, "O&O&:rename",
2570 convert_to_unicode, &o1,
2571 convert_to_unicode, &o2))
2572 PyErr_Clear();
2573 else {
2574 Py_BEGIN_ALLOW_THREADS
2575 result = MoveFileW(PyUnicode_AsUnicode(o1),
2576 PyUnicode_AsUnicode(o2));
2577 Py_END_ALLOW_THREADS
2578 Py_DECREF(o1);
2579 Py_DECREF(o2);
2580 if (!result)
2581 return win32_error("rename", NULL);
2582 Py_INCREF(Py_None);
2583 return Py_None;
2584 }
2585 }
2586 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2587 return NULL;
2588 Py_BEGIN_ALLOW_THREADS
2589 result = MoveFileA(p1, p2);
2590 Py_END_ALLOW_THREADS
2591 if (!result)
2592 return win32_error("rename", NULL);
2593 Py_INCREF(Py_None);
2594 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002595#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002596 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002597#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002598}
2599
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002601PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002602"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002603Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002604
Barry Warsaw53699e91996-12-10 23:23:01 +00002605static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002606posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002607{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002608#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002609 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002610#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002611 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002612#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002613}
2614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002616PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002617"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002618Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002619
Barry Warsaw53699e91996-12-10 23:23:01 +00002620static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002621posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002622{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002623#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002624 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002625#else
2626 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2627#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002628}
2629
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002631#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002632PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002633"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002634Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002635
Barry Warsaw53699e91996-12-10 23:23:01 +00002636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002637posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002638{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002639 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002640#ifdef MS_WINDOWS
2641 wchar_t *command;
2642 if (!PyArg_ParseTuple(args, "u:system", &command))
2643 return NULL;
2644#else
2645 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002646 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002647 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002648#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002649 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002650#ifdef MS_WINDOWS
2651 sts = _wsystem(command);
2652#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002653 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002654#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002655 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002656 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002657}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002658#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002662"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002664
Barry Warsaw53699e91996-12-10 23:23:01 +00002665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002666posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002667{
2668 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002669 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002670 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002671 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002672 if (i < 0)
2673 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002674 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002675}
2676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002679"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002683"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002684Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002685
Barry Warsaw53699e91996-12-10 23:23:01 +00002686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002687posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002688{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002690 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002691#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002692 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002693#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002694}
2695
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002696
Guido van Rossumb6775db1994-08-01 11:34:53 +00002697#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002699"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002700Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Barry Warsaw53699e91996-12-10 23:23:01 +00002702static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002703posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002704{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002705 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002706 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002707
Barry Warsaw53699e91996-12-10 23:23:01 +00002708 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002709 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002710 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002711 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002712 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002713 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002714 u.sysname,
2715 u.nodename,
2716 u.release,
2717 u.version,
2718 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002719}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002720#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002721
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002722static int
2723extract_time(PyObject *t, long* sec, long* usec)
2724{
2725 long intval;
2726 if (PyFloat_Check(t)) {
2727 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002728 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002729 if (!intobj)
2730 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002731 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002732 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002733 if (intval == -1 && PyErr_Occurred())
2734 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002735 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002736 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002737 if (*usec < 0)
2738 /* If rounding gave us a negative number,
2739 truncate. */
2740 *usec = 0;
2741 return 0;
2742 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002743 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002744 if (intval == -1 && PyErr_Occurred())
2745 return -1;
2746 *sec = intval;
2747 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002748 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002749}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002751PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002752"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002753utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002754Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002755second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002756
Barry Warsaw53699e91996-12-10 23:23:01 +00002757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002758posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002759{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002760#ifdef Py_WIN_WIDE_FILENAMES
2761 PyObject *arg;
2762 PyUnicodeObject *obwpath;
2763 wchar_t *wpath = NULL;
2764 char *apath = NULL;
2765 HANDLE hFile;
2766 long atimesec, mtimesec, ausec, musec;
2767 FILETIME atime, mtime;
2768 PyObject *result = NULL;
2769
2770 if (unicode_file_names()) {
2771 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2772 wpath = PyUnicode_AS_UNICODE(obwpath);
2773 Py_BEGIN_ALLOW_THREADS
2774 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002775 NULL, OPEN_EXISTING,
2776 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002777 Py_END_ALLOW_THREADS
2778 if (hFile == INVALID_HANDLE_VALUE)
2779 return win32_error_unicode("utime", wpath);
2780 } else
2781 /* Drop the argument parsing error as narrow strings
2782 are also valid. */
2783 PyErr_Clear();
2784 }
2785 if (!wpath) {
2786 if (!PyArg_ParseTuple(args, "etO:utime",
2787 Py_FileSystemDefaultEncoding, &apath, &arg))
2788 return NULL;
2789 Py_BEGIN_ALLOW_THREADS
2790 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002791 NULL, OPEN_EXISTING,
2792 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002793 Py_END_ALLOW_THREADS
2794 if (hFile == INVALID_HANDLE_VALUE) {
2795 win32_error("utime", apath);
2796 PyMem_Free(apath);
2797 return NULL;
2798 }
2799 PyMem_Free(apath);
2800 }
2801
2802 if (arg == Py_None) {
2803 SYSTEMTIME now;
2804 GetSystemTime(&now);
2805 if (!SystemTimeToFileTime(&now, &mtime) ||
2806 !SystemTimeToFileTime(&now, &atime)) {
2807 win32_error("utime", NULL);
2808 goto done;
2809 }
2810 }
2811 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2812 PyErr_SetString(PyExc_TypeError,
2813 "utime() arg 2 must be a tuple (atime, mtime)");
2814 goto done;
2815 }
2816 else {
2817 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2818 &atimesec, &ausec) == -1)
2819 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002820 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002821 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2822 &mtimesec, &musec) == -1)
2823 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002824 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002825 }
2826 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2827 /* Avoid putting the file name into the error here,
2828 as that may confuse the user into believing that
2829 something is wrong with the file, when it also
2830 could be the time stamp that gives a problem. */
2831 win32_error("utime", NULL);
2832 }
2833 Py_INCREF(Py_None);
2834 result = Py_None;
2835done:
2836 CloseHandle(hFile);
2837 return result;
2838#else /* Py_WIN_WIDE_FILENAMES */
2839
Neal Norwitz2adf2102004-06-09 01:46:02 +00002840 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002841 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002842 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002843 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002844
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002845#if defined(HAVE_UTIMES)
2846 struct timeval buf[2];
2847#define ATIME buf[0].tv_sec
2848#define MTIME buf[1].tv_sec
2849#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002850/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002851 struct utimbuf buf;
2852#define ATIME buf.actime
2853#define MTIME buf.modtime
2854#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002855#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002856 time_t buf[2];
2857#define ATIME buf[0]
2858#define MTIME buf[1]
2859#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002860#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002861
Mark Hammond817c9292003-12-03 01:22:38 +00002862
Thomas Wouters477c8d52006-05-27 19:21:47 +00002863 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002864 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002865 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002866 if (arg == Py_None) {
2867 /* optional time values not given */
2868 Py_BEGIN_ALLOW_THREADS
2869 res = utime(path, NULL);
2870 Py_END_ALLOW_THREADS
2871 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002872 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002873 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002874 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002875 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002876 return NULL;
2877 }
2878 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002879 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002880 &atime, &ausec) == -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 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002884 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002885 &mtime, &musec) == -1) {
2886 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002887 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002888 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002889 ATIME = atime;
2890 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002891#ifdef HAVE_UTIMES
2892 buf[0].tv_usec = ausec;
2893 buf[1].tv_usec = musec;
2894 Py_BEGIN_ALLOW_THREADS
2895 res = utimes(path, buf);
2896 Py_END_ALLOW_THREADS
2897#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002898 Py_BEGIN_ALLOW_THREADS
2899 res = utime(path, UTIME_ARG);
2900 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002901#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002902 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002903 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002904 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002905 }
Neal Norwitz96652712004-06-06 20:40:27 +00002906 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002907 Py_INCREF(Py_None);
2908 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002909#undef UTIME_ARG
2910#undef ATIME
2911#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002912#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002913}
2914
Guido van Rossum85e3b011991-06-03 12:42:10 +00002915
Guido van Rossum3b066191991-06-04 19:40:25 +00002916/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002918PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002919"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002920Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002921
Barry Warsaw53699e91996-12-10 23:23:01 +00002922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002923posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002924{
2925 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002926 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002927 return NULL;
2928 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002929 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002930}
2931
Martin v. Löwis114619e2002-10-07 06:44:21 +00002932#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2933static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002934free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002935{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002936 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002937 for (i = 0; i < count; i++)
2938 PyMem_Free(array[i]);
2939 PyMem_DEL(array);
2940}
2941#endif
2942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002943
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002944#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002945PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002946"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002947Execute an executable path with arguments, replacing current process.\n\
2948\n\
2949 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002950 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002951
Barry Warsaw53699e91996-12-10 23:23:01 +00002952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002953posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002954{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002955 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002956 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002957 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002958 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002959 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002960
Guido van Rossum89b33251993-10-22 14:26:06 +00002961 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002962 argv is a list or tuple of strings. */
2963
Martin v. Löwis114619e2002-10-07 06:44:21 +00002964 if (!PyArg_ParseTuple(args, "etO:execv",
2965 Py_FileSystemDefaultEncoding,
2966 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002967 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002968 if (PyList_Check(argv)) {
2969 argc = PyList_Size(argv);
2970 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002971 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002972 else if (PyTuple_Check(argv)) {
2973 argc = PyTuple_Size(argv);
2974 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002975 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002976 else {
Fred Drake661ea262000-10-24 19:57:45 +00002977 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002978 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002979 return NULL;
2980 }
Thomas Heller6790d602007-08-30 17:15:14 +00002981 if (argc < 1) {
2982 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2983 PyMem_Free(path);
2984 return NULL;
2985 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002986
Barry Warsaw53699e91996-12-10 23:23:01 +00002987 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002988 if (argvlist == NULL) {
2989 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002990 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002991 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002992 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002993 if (!PyArg_Parse((*getitem)(argv, i), "et",
2994 Py_FileSystemDefaultEncoding,
2995 &argvlist[i])) {
2996 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002997 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002998 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002999 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00003000 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003001
Guido van Rossum85e3b011991-06-03 12:42:10 +00003002 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00003003 }
3004 argvlist[argc] = NULL;
3005
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003006 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003007
Guido van Rossum85e3b011991-06-03 12:42:10 +00003008 /* If we get here it's definitely an error */
3009
Martin v. Löwis114619e2002-10-07 06:44:21 +00003010 free_string_array(argvlist, argc);
3011 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003012 return posix_error();
3013}
3014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003016PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003017"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003018Execute a path with arguments and environment, replacing current process.\n\
3019\n\
3020 path: path of executable file\n\
3021 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003022 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003023
Barry Warsaw53699e91996-12-10 23:23:01 +00003024static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003025posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003026{
3027 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003028 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003029 char **argvlist;
3030 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003031 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003032 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003033 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003034 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003035
3036 /* execve has three arguments: (path, argv, env), where
3037 argv is a list or tuple of strings and env is a dictionary
3038 like posix.environ. */
3039
Martin v. Löwis114619e2002-10-07 06:44:21 +00003040 if (!PyArg_ParseTuple(args, "etOO:execve",
3041 Py_FileSystemDefaultEncoding,
3042 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003043 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003044 if (PyList_Check(argv)) {
3045 argc = PyList_Size(argv);
3046 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003047 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003048 else if (PyTuple_Check(argv)) {
3049 argc = PyTuple_Size(argv);
3050 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003051 }
3052 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003053 PyErr_SetString(PyExc_TypeError,
3054 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003055 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003056 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003057 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003058 PyErr_SetString(PyExc_TypeError,
3059 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003060 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003061 }
3062
Barry Warsaw53699e91996-12-10 23:23:01 +00003063 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003064 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003065 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003066 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003067 }
3068 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003069 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003070 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003071 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003072 &argvlist[i]))
3073 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003074 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003075 goto fail_1;
3076 }
3077 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003078 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003079 argvlist[argc] = NULL;
3080
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003081 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003082 if (i < 0)
3083 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003084 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003085 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003086 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003087 goto fail_1;
3088 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003089 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003090 keys = PyMapping_Keys(env);
3091 vals = PyMapping_Values(env);
3092 if (!keys || !vals)
3093 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003094 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3095 PyErr_SetString(PyExc_TypeError,
3096 "execve(): env.keys() or env.values() is not a list");
3097 goto fail_2;
3098 }
Tim Peters5aa91602002-01-30 05:46:57 +00003099
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003100 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003101 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003102 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003103
3104 key = PyList_GetItem(keys, pos);
3105 val = PyList_GetItem(vals, pos);
3106 if (!key || !val)
3107 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003108
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003109 if (!PyArg_Parse(
3110 key,
3111 "s;execve() arg 3 contains a non-string key",
3112 &k) ||
3113 !PyArg_Parse(
3114 val,
3115 "s;execve() arg 3 contains a non-string value",
3116 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003117 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003118 goto fail_2;
3119 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003120
3121#if defined(PYOS_OS2)
3122 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3123 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3124#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003125 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003126 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003127 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003128 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003129 goto fail_2;
3130 }
Tim Petersc8996f52001-12-03 20:41:00 +00003131 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003132 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003133#if defined(PYOS_OS2)
3134 }
3135#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003136 }
3137 envlist[envc] = 0;
3138
3139 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003140
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003141 /* If we get here it's definitely an error */
3142
3143 (void) posix_error();
3144
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003145 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003146 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003147 PyMem_DEL(envlist[envc]);
3148 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 fail_1:
3150 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003151 Py_XDECREF(vals);
3152 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003153 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003154 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003155 return NULL;
3156}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003157#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003158
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003159
Guido van Rossuma1065681999-01-25 23:20:23 +00003160#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003161PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003162"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003163Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003164\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003165 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003166 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003167 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003168
3169static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003170posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003171{
3172 char *path;
3173 PyObject *argv;
3174 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003175 int mode, i;
3176 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003177 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003178 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003179
3180 /* spawnv has three arguments: (mode, path, argv), where
3181 argv is a list or tuple of strings. */
3182
Martin v. Löwis114619e2002-10-07 06:44:21 +00003183 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3184 Py_FileSystemDefaultEncoding,
3185 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003186 return NULL;
3187 if (PyList_Check(argv)) {
3188 argc = PyList_Size(argv);
3189 getitem = PyList_GetItem;
3190 }
3191 else if (PyTuple_Check(argv)) {
3192 argc = PyTuple_Size(argv);
3193 getitem = PyTuple_GetItem;
3194 }
3195 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003196 PyErr_SetString(PyExc_TypeError,
3197 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003198 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003199 return NULL;
3200 }
3201
3202 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003203 if (argvlist == NULL) {
3204 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003205 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003206 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003207 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003208 if (!PyArg_Parse((*getitem)(argv, i), "et",
3209 Py_FileSystemDefaultEncoding,
3210 &argvlist[i])) {
3211 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003212 PyErr_SetString(
3213 PyExc_TypeError,
3214 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003215 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003216 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003217 }
3218 }
3219 argvlist[argc] = NULL;
3220
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003221#if defined(PYOS_OS2) && defined(PYCC_GCC)
3222 Py_BEGIN_ALLOW_THREADS
3223 spawnval = spawnv(mode, path, argvlist);
3224 Py_END_ALLOW_THREADS
3225#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003226 if (mode == _OLD_P_OVERLAY)
3227 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003228
Tim Peters25059d32001-12-07 20:35:43 +00003229 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003230 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003231 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003232#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003233
Martin v. Löwis114619e2002-10-07 06:44:21 +00003234 free_string_array(argvlist, argc);
3235 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003236
Fred Drake699f3522000-06-29 21:12:41 +00003237 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003238 return posix_error();
3239 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003240#if SIZEOF_LONG == SIZEOF_VOID_P
3241 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003242#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003243 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003244#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003245}
3246
3247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003248PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003249"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003250Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003251\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003252 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003253 path: path of executable file\n\
3254 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003255 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003256
3257static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003258posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003259{
3260 char *path;
3261 PyObject *argv, *env;
3262 char **argvlist;
3263 char **envlist;
3264 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003265 int mode, pos, envc;
3266 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003267 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003268 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003269 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003270
3271 /* spawnve has four arguments: (mode, path, argv, env), where
3272 argv is a list or tuple of strings and env is a dictionary
3273 like posix.environ. */
3274
Martin v. Löwis114619e2002-10-07 06:44:21 +00003275 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3276 Py_FileSystemDefaultEncoding,
3277 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003278 return NULL;
3279 if (PyList_Check(argv)) {
3280 argc = PyList_Size(argv);
3281 getitem = PyList_GetItem;
3282 }
3283 else if (PyTuple_Check(argv)) {
3284 argc = PyTuple_Size(argv);
3285 getitem = PyTuple_GetItem;
3286 }
3287 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003288 PyErr_SetString(PyExc_TypeError,
3289 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003290 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003291 }
3292 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003293 PyErr_SetString(PyExc_TypeError,
3294 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003295 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003296 }
3297
3298 argvlist = PyMem_NEW(char *, argc+1);
3299 if (argvlist == NULL) {
3300 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003301 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003302 }
3303 for (i = 0; i < argc; i++) {
3304 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003305 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003306 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003307 &argvlist[i]))
3308 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003309 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003310 goto fail_1;
3311 }
3312 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003313 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003314 argvlist[argc] = NULL;
3315
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003316 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003317 if (i < 0)
3318 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003319 envlist = PyMem_NEW(char *, i + 1);
3320 if (envlist == NULL) {
3321 PyErr_NoMemory();
3322 goto fail_1;
3323 }
3324 envc = 0;
3325 keys = PyMapping_Keys(env);
3326 vals = PyMapping_Values(env);
3327 if (!keys || !vals)
3328 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003329 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3330 PyErr_SetString(PyExc_TypeError,
3331 "spawnve(): env.keys() or env.values() is not a list");
3332 goto fail_2;
3333 }
Tim Peters5aa91602002-01-30 05:46:57 +00003334
Guido van Rossuma1065681999-01-25 23:20:23 +00003335 for (pos = 0; pos < i; pos++) {
3336 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003337 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003338
3339 key = PyList_GetItem(keys, pos);
3340 val = PyList_GetItem(vals, pos);
3341 if (!key || !val)
3342 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003343
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003344 if (!PyArg_Parse(
3345 key,
3346 "s;spawnve() arg 3 contains a non-string key",
3347 &k) ||
3348 !PyArg_Parse(
3349 val,
3350 "s;spawnve() arg 3 contains a non-string value",
3351 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003352 {
3353 goto fail_2;
3354 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003355 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003356 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003357 if (p == NULL) {
3358 PyErr_NoMemory();
3359 goto fail_2;
3360 }
Tim Petersc8996f52001-12-03 20:41:00 +00003361 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003362 envlist[envc++] = p;
3363 }
3364 envlist[envc] = 0;
3365
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003366#if defined(PYOS_OS2) && defined(PYCC_GCC)
3367 Py_BEGIN_ALLOW_THREADS
3368 spawnval = spawnve(mode, path, argvlist, envlist);
3369 Py_END_ALLOW_THREADS
3370#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003371 if (mode == _OLD_P_OVERLAY)
3372 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003373
3374 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003375 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003376 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003377#endif
Tim Peters25059d32001-12-07 20:35:43 +00003378
Fred Drake699f3522000-06-29 21:12:41 +00003379 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003380 (void) posix_error();
3381 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003382#if SIZEOF_LONG == SIZEOF_VOID_P
3383 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003384#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003385 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003386#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003387
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003388 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003389 while (--envc >= 0)
3390 PyMem_DEL(envlist[envc]);
3391 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003392 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003393 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003394 Py_XDECREF(vals);
3395 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003396 fail_0:
3397 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003398 return res;
3399}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003400
3401/* OS/2 supports spawnvp & spawnvpe natively */
3402#if defined(PYOS_OS2)
3403PyDoc_STRVAR(posix_spawnvp__doc__,
3404"spawnvp(mode, file, args)\n\n\
3405Execute the program 'file' in a new process, using the environment\n\
3406search path to find the file.\n\
3407\n\
3408 mode: mode of process creation\n\
3409 file: executable file name\n\
3410 args: tuple or list of strings");
3411
3412static PyObject *
3413posix_spawnvp(PyObject *self, PyObject *args)
3414{
3415 char *path;
3416 PyObject *argv;
3417 char **argvlist;
3418 int mode, i, argc;
3419 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003420 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003421
3422 /* spawnvp has three arguments: (mode, path, argv), where
3423 argv is a list or tuple of strings. */
3424
3425 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3426 Py_FileSystemDefaultEncoding,
3427 &path, &argv))
3428 return NULL;
3429 if (PyList_Check(argv)) {
3430 argc = PyList_Size(argv);
3431 getitem = PyList_GetItem;
3432 }
3433 else if (PyTuple_Check(argv)) {
3434 argc = PyTuple_Size(argv);
3435 getitem = PyTuple_GetItem;
3436 }
3437 else {
3438 PyErr_SetString(PyExc_TypeError,
3439 "spawnvp() arg 2 must be a tuple or list");
3440 PyMem_Free(path);
3441 return NULL;
3442 }
3443
3444 argvlist = PyMem_NEW(char *, argc+1);
3445 if (argvlist == NULL) {
3446 PyMem_Free(path);
3447 return PyErr_NoMemory();
3448 }
3449 for (i = 0; i < argc; i++) {
3450 if (!PyArg_Parse((*getitem)(argv, i), "et",
3451 Py_FileSystemDefaultEncoding,
3452 &argvlist[i])) {
3453 free_string_array(argvlist, i);
3454 PyErr_SetString(
3455 PyExc_TypeError,
3456 "spawnvp() arg 2 must contain only strings");
3457 PyMem_Free(path);
3458 return NULL;
3459 }
3460 }
3461 argvlist[argc] = NULL;
3462
3463 Py_BEGIN_ALLOW_THREADS
3464#if defined(PYCC_GCC)
3465 spawnval = spawnvp(mode, path, argvlist);
3466#else
3467 spawnval = _spawnvp(mode, path, argvlist);
3468#endif
3469 Py_END_ALLOW_THREADS
3470
3471 free_string_array(argvlist, argc);
3472 PyMem_Free(path);
3473
3474 if (spawnval == -1)
3475 return posix_error();
3476 else
3477 return Py_BuildValue("l", (long) spawnval);
3478}
3479
3480
3481PyDoc_STRVAR(posix_spawnvpe__doc__,
3482"spawnvpe(mode, file, args, env)\n\n\
3483Execute the program 'file' in a new process, using the environment\n\
3484search path to find the file.\n\
3485\n\
3486 mode: mode of process creation\n\
3487 file: executable file name\n\
3488 args: tuple or list of arguments\n\
3489 env: dictionary of strings mapping to strings");
3490
3491static PyObject *
3492posix_spawnvpe(PyObject *self, PyObject *args)
3493{
3494 char *path;
3495 PyObject *argv, *env;
3496 char **argvlist;
3497 char **envlist;
3498 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3499 int mode, i, pos, argc, envc;
3500 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003501 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003502 int lastarg = 0;
3503
3504 /* spawnvpe has four arguments: (mode, path, argv, env), where
3505 argv is a list or tuple of strings and env is a dictionary
3506 like posix.environ. */
3507
3508 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3509 Py_FileSystemDefaultEncoding,
3510 &path, &argv, &env))
3511 return NULL;
3512 if (PyList_Check(argv)) {
3513 argc = PyList_Size(argv);
3514 getitem = PyList_GetItem;
3515 }
3516 else if (PyTuple_Check(argv)) {
3517 argc = PyTuple_Size(argv);
3518 getitem = PyTuple_GetItem;
3519 }
3520 else {
3521 PyErr_SetString(PyExc_TypeError,
3522 "spawnvpe() arg 2 must be a tuple or list");
3523 goto fail_0;
3524 }
3525 if (!PyMapping_Check(env)) {
3526 PyErr_SetString(PyExc_TypeError,
3527 "spawnvpe() arg 3 must be a mapping object");
3528 goto fail_0;
3529 }
3530
3531 argvlist = PyMem_NEW(char *, argc+1);
3532 if (argvlist == NULL) {
3533 PyErr_NoMemory();
3534 goto fail_0;
3535 }
3536 for (i = 0; i < argc; i++) {
3537 if (!PyArg_Parse((*getitem)(argv, i),
3538 "et;spawnvpe() arg 2 must contain only strings",
3539 Py_FileSystemDefaultEncoding,
3540 &argvlist[i]))
3541 {
3542 lastarg = i;
3543 goto fail_1;
3544 }
3545 }
3546 lastarg = argc;
3547 argvlist[argc] = NULL;
3548
3549 i = PyMapping_Size(env);
3550 if (i < 0)
3551 goto fail_1;
3552 envlist = PyMem_NEW(char *, i + 1);
3553 if (envlist == NULL) {
3554 PyErr_NoMemory();
3555 goto fail_1;
3556 }
3557 envc = 0;
3558 keys = PyMapping_Keys(env);
3559 vals = PyMapping_Values(env);
3560 if (!keys || !vals)
3561 goto fail_2;
3562 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3563 PyErr_SetString(PyExc_TypeError,
3564 "spawnvpe(): env.keys() or env.values() is not a list");
3565 goto fail_2;
3566 }
3567
3568 for (pos = 0; pos < i; pos++) {
3569 char *p, *k, *v;
3570 size_t len;
3571
3572 key = PyList_GetItem(keys, pos);
3573 val = PyList_GetItem(vals, pos);
3574 if (!key || !val)
3575 goto fail_2;
3576
3577 if (!PyArg_Parse(
3578 key,
3579 "s;spawnvpe() arg 3 contains a non-string key",
3580 &k) ||
3581 !PyArg_Parse(
3582 val,
3583 "s;spawnvpe() arg 3 contains a non-string value",
3584 &v))
3585 {
3586 goto fail_2;
3587 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003588 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003589 p = PyMem_NEW(char, len);
3590 if (p == NULL) {
3591 PyErr_NoMemory();
3592 goto fail_2;
3593 }
3594 PyOS_snprintf(p, len, "%s=%s", k, v);
3595 envlist[envc++] = p;
3596 }
3597 envlist[envc] = 0;
3598
3599 Py_BEGIN_ALLOW_THREADS
3600#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003601 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003602#else
Christian Heimes292d3512008-02-03 16:51:08 +00003603 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003604#endif
3605 Py_END_ALLOW_THREADS
3606
3607 if (spawnval == -1)
3608 (void) posix_error();
3609 else
3610 res = Py_BuildValue("l", (long) spawnval);
3611
3612 fail_2:
3613 while (--envc >= 0)
3614 PyMem_DEL(envlist[envc]);
3615 PyMem_DEL(envlist);
3616 fail_1:
3617 free_string_array(argvlist, lastarg);
3618 Py_XDECREF(vals);
3619 Py_XDECREF(keys);
3620 fail_0:
3621 PyMem_Free(path);
3622 return res;
3623}
3624#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003625#endif /* HAVE_SPAWNV */
3626
3627
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003628#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003629PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003630"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003631Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3632\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003633Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003634
3635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003636posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003637{
Christian Heimes400adb02008-02-01 08:12:03 +00003638 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003639 if (pid == -1)
3640 return posix_error();
3641 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003642 return PyLong_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003643}
3644#endif
3645
3646
Guido van Rossumad0ee831995-03-01 10:34:45 +00003647#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003648PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003649"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003650Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003651Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003652
Barry Warsaw53699e91996-12-10 23:23:01 +00003653static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003654posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003655{
Christian Heimes400adb02008-02-01 08:12:03 +00003656 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003657 if (pid == -1)
3658 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003659 if (pid == 0)
3660 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003661 return PyLong_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003662}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003663#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003664
Neal Norwitzb59798b2003-03-21 01:43:31 +00003665/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003666/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3667#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003668#define DEV_PTY_FILE "/dev/ptc"
3669#define HAVE_DEV_PTMX
3670#else
3671#define DEV_PTY_FILE "/dev/ptmx"
3672#endif
3673
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003674#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003675#ifdef HAVE_PTY_H
3676#include <pty.h>
3677#else
3678#ifdef HAVE_LIBUTIL_H
3679#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003680#endif /* HAVE_LIBUTIL_H */
3681#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003682#ifdef HAVE_STROPTS_H
3683#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003684#endif
3685#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003686
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003687#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003688PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003689"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003690Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003691
3692static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003693posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003694{
3695 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003696#ifndef HAVE_OPENPTY
3697 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003698#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003699#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003700 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003701#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003702 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003703#endif
3704#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003705
Thomas Wouters70c21a12000-07-14 14:28:33 +00003706#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003707 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3708 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003709#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003710 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3711 if (slave_name == NULL)
3712 return posix_error();
3713
3714 slave_fd = open(slave_name, O_RDWR);
3715 if (slave_fd < 0)
3716 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003717#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003718 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003719 if (master_fd < 0)
3720 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003721 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003722 /* change permission of slave */
3723 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003724 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003725 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003726 }
3727 /* unlock slave */
3728 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003729 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003730 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003731 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003732 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003733 slave_name = ptsname(master_fd); /* get name of slave */
3734 if (slave_name == NULL)
3735 return posix_error();
3736 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3737 if (slave_fd < 0)
3738 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003739#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003740 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3741 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003742#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003743 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003744#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003745#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003746#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003747
Fred Drake8cef4cf2000-06-28 16:40:38 +00003748 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003749
Fred Drake8cef4cf2000-06-28 16:40:38 +00003750}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003751#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003752
3753#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003754PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003755"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003756Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3757Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003758To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003759
3760static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003761posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003762{
Christian Heimes400adb02008-02-01 08:12:03 +00003763 int master_fd = -1;
3764 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003765
Fred Drake8cef4cf2000-06-28 16:40:38 +00003766 pid = forkpty(&master_fd, NULL, NULL, NULL);
3767 if (pid == -1)
3768 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003769 if (pid == 0)
3770 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003771 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003772}
3773#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003774
Guido van Rossumad0ee831995-03-01 10:34:45 +00003775#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003776PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003777"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003778Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003779
Barry Warsaw53699e91996-12-10 23:23:01 +00003780static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003781posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003782{
Christian Heimes217cfd12007-12-02 14:31:20 +00003783 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003784}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003785#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003786
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003787
Guido van Rossumad0ee831995-03-01 10:34:45 +00003788#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003790"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003792
Barry Warsaw53699e91996-12-10 23:23:01 +00003793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003794posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003795{
Christian Heimes217cfd12007-12-02 14:31:20 +00003796 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003797}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003798#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003800
Guido van Rossumad0ee831995-03-01 10:34:45 +00003801#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003802PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003803"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003804Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003805
Barry Warsaw53699e91996-12-10 23:23:01 +00003806static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003807posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003808{
Christian Heimes217cfd12007-12-02 14:31:20 +00003809 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003810}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003811#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003812
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003813
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003814PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003815"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003816Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003817
Barry Warsaw53699e91996-12-10 23:23:01 +00003818static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003819posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003820{
Christian Heimes217cfd12007-12-02 14:31:20 +00003821 return PyLong_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003822}
3823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003824
Fred Drakec9680921999-12-13 16:37:25 +00003825#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003827"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003829
3830static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003831posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003832{
3833 PyObject *result = NULL;
3834
Fred Drakec9680921999-12-13 16:37:25 +00003835#ifdef NGROUPS_MAX
3836#define MAX_GROUPS NGROUPS_MAX
3837#else
3838 /* defined to be 16 on Solaris7, so this should be a small number */
3839#define MAX_GROUPS 64
3840#endif
3841 gid_t grouplist[MAX_GROUPS];
3842 int n;
3843
3844 n = getgroups(MAX_GROUPS, grouplist);
3845 if (n < 0)
3846 posix_error();
3847 else {
3848 result = PyList_New(n);
3849 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003850 int i;
3851 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003852 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003853 if (o == NULL) {
3854 Py_DECREF(result);
3855 result = NULL;
3856 break;
3857 }
3858 PyList_SET_ITEM(result, i, o);
3859 }
3860 }
3861 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003862
Fred Drakec9680921999-12-13 16:37:25 +00003863 return result;
3864}
3865#endif
3866
Martin v. Löwis606edc12002-06-13 21:09:11 +00003867#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003868PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003869"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003870Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003871
3872static PyObject *
3873posix_getpgid(PyObject *self, PyObject *args)
3874{
3875 int pid, pgid;
3876 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3877 return NULL;
3878 pgid = getpgid(pid);
3879 if (pgid < 0)
3880 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00003881 return PyLong_FromLong((long)pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003882}
3883#endif /* HAVE_GETPGID */
3884
3885
Guido van Rossumb6775db1994-08-01 11:34:53 +00003886#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003887PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003888"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003889Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003890
Barry Warsaw53699e91996-12-10 23:23:01 +00003891static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003892posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003893{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003894#ifdef GETPGRP_HAVE_ARG
Christian Heimes217cfd12007-12-02 14:31:20 +00003895 return PyLong_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003896#else /* GETPGRP_HAVE_ARG */
Christian Heimes217cfd12007-12-02 14:31:20 +00003897 return PyLong_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003898#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003899}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003900#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003902
Guido van Rossumb6775db1994-08-01 11:34:53 +00003903#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003904PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003905"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003906Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003907
Barry Warsaw53699e91996-12-10 23:23:01 +00003908static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003909posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003910{
Guido van Rossum64933891994-10-20 21:56:42 +00003911#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003912 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003913#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003914 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003915#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003916 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003917 Py_INCREF(Py_None);
3918 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003919}
3920
Guido van Rossumb6775db1994-08-01 11:34:53 +00003921#endif /* HAVE_SETPGRP */
3922
Guido van Rossumad0ee831995-03-01 10:34:45 +00003923#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003924PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003925"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003926Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003927
Barry Warsaw53699e91996-12-10 23:23:01 +00003928static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003929posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003930{
Christian Heimes217cfd12007-12-02 14:31:20 +00003931 return PyLong_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003932}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003933#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003935
Fred Drake12c6e2d1999-12-14 21:25:03 +00003936#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003937PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003938"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003939Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003940
3941static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003942posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003943{
Neal Norwitze241ce82003-02-17 18:17:05 +00003944 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003945 char *name;
3946 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003947
Fred Drakea30680b2000-12-06 21:24:28 +00003948 errno = 0;
3949 name = getlogin();
3950 if (name == NULL) {
3951 if (errno)
3952 posix_error();
3953 else
3954 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003955 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003956 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003957 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003958 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003959 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003960
Fred Drake12c6e2d1999-12-14 21:25:03 +00003961 return result;
3962}
3963#endif
3964
Guido van Rossumad0ee831995-03-01 10:34:45 +00003965#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003966PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003967"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003968Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003969
Barry Warsaw53699e91996-12-10 23:23:01 +00003970static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003971posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003972{
Christian Heimes217cfd12007-12-02 14:31:20 +00003973 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003974}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003975#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003976
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003977
Guido van Rossumad0ee831995-03-01 10:34:45 +00003978#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003979PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003980"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003981Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003982
Barry Warsaw53699e91996-12-10 23:23:01 +00003983static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003984posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003985{
Christian Heimes292d3512008-02-03 16:51:08 +00003986 pid_t pid;
3987 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003988 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003989 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003990#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003991 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3992 APIRET rc;
3993 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003994 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003995
3996 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3997 APIRET rc;
3998 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003999 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004000
4001 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004002 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004003#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004004 if (kill(pid, sig) == -1)
4005 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004006#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004007 Py_INCREF(Py_None);
4008 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004009}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004010#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004011
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004012#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004013PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004014"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004016
4017static PyObject *
4018posix_killpg(PyObject *self, PyObject *args)
4019{
4020 int pgid, sig;
4021 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4022 return NULL;
4023 if (killpg(pgid, sig) == -1)
4024 return posix_error();
4025 Py_INCREF(Py_None);
4026 return Py_None;
4027}
4028#endif
4029
Guido van Rossumc0125471996-06-28 18:55:32 +00004030#ifdef HAVE_PLOCK
4031
4032#ifdef HAVE_SYS_LOCK_H
4033#include <sys/lock.h>
4034#endif
4035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004036PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004037"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004038Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004039
Barry Warsaw53699e91996-12-10 23:23:01 +00004040static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004041posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004042{
4043 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004044 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004045 return NULL;
4046 if (plock(op) == -1)
4047 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004048 Py_INCREF(Py_None);
4049 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004050}
4051#endif
4052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004053
Guido van Rossum3b066191991-06-04 19:40:25 +00004054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004055
Guido van Rossumb6775db1994-08-01 11:34:53 +00004056#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004057PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004058"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004059Set the current process's user id.");
4060
Barry Warsaw53699e91996-12-10 23:23:01 +00004061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004062posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004063{
4064 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004065 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004066 return NULL;
4067 if (setuid(uid) < 0)
4068 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004069 Py_INCREF(Py_None);
4070 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004071}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004072#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004073
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004074
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004075#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004076PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004077"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004078Set the current process's effective user id.");
4079
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004080static PyObject *
4081posix_seteuid (PyObject *self, PyObject *args)
4082{
4083 int euid;
4084 if (!PyArg_ParseTuple(args, "i", &euid)) {
4085 return NULL;
4086 } else if (seteuid(euid) < 0) {
4087 return posix_error();
4088 } else {
4089 Py_INCREF(Py_None);
4090 return Py_None;
4091 }
4092}
4093#endif /* HAVE_SETEUID */
4094
4095#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004096PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004097"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004098Set the current process's effective group id.");
4099
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004100static PyObject *
4101posix_setegid (PyObject *self, PyObject *args)
4102{
4103 int egid;
4104 if (!PyArg_ParseTuple(args, "i", &egid)) {
4105 return NULL;
4106 } else if (setegid(egid) < 0) {
4107 return posix_error();
4108 } else {
4109 Py_INCREF(Py_None);
4110 return Py_None;
4111 }
4112}
4113#endif /* HAVE_SETEGID */
4114
4115#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004116PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004117"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004118Set the current process's real and effective user ids.");
4119
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004120static PyObject *
4121posix_setreuid (PyObject *self, PyObject *args)
4122{
4123 int ruid, euid;
4124 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4125 return NULL;
4126 } else if (setreuid(ruid, euid) < 0) {
4127 return posix_error();
4128 } else {
4129 Py_INCREF(Py_None);
4130 return Py_None;
4131 }
4132}
4133#endif /* HAVE_SETREUID */
4134
4135#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004136PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004137"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004138Set the current process's real and effective group ids.");
4139
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004140static PyObject *
4141posix_setregid (PyObject *self, PyObject *args)
4142{
4143 int rgid, egid;
4144 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4145 return NULL;
4146 } else if (setregid(rgid, egid) < 0) {
4147 return posix_error();
4148 } else {
4149 Py_INCREF(Py_None);
4150 return Py_None;
4151 }
4152}
4153#endif /* HAVE_SETREGID */
4154
Guido van Rossumb6775db1994-08-01 11:34:53 +00004155#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004156PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004157"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004158Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004159
Barry Warsaw53699e91996-12-10 23:23:01 +00004160static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004161posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004162{
4163 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004164 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004165 return NULL;
4166 if (setgid(gid) < 0)
4167 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004168 Py_INCREF(Py_None);
4169 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004170}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004171#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004172
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004173#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004174PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004175"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004176Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004177
4178static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004179posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004180{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004181 int i, len;
4182 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004183
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004184 if (!PySequence_Check(groups)) {
4185 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4186 return NULL;
4187 }
4188 len = PySequence_Size(groups);
4189 if (len > MAX_GROUPS) {
4190 PyErr_SetString(PyExc_ValueError, "too many groups");
4191 return NULL;
4192 }
4193 for(i = 0; i < len; i++) {
4194 PyObject *elem;
4195 elem = PySequence_GetItem(groups, i);
4196 if (!elem)
4197 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004198 if (!PyLong_Check(elem)) {
4199 PyErr_SetString(PyExc_TypeError,
4200 "groups must be integers");
4201 Py_DECREF(elem);
4202 return NULL;
4203 } else {
4204 unsigned long x = PyLong_AsUnsignedLong(elem);
4205 if (PyErr_Occurred()) {
4206 PyErr_SetString(PyExc_TypeError,
4207 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004208 Py_DECREF(elem);
4209 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004210 }
Georg Brandla13c2442005-11-22 19:30:31 +00004211 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004212 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004213 if (grouplist[i] != x) {
4214 PyErr_SetString(PyExc_TypeError,
4215 "group id too big");
4216 Py_DECREF(elem);
4217 return NULL;
4218 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004219 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004220 Py_DECREF(elem);
4221 }
4222
4223 if (setgroups(len, grouplist) < 0)
4224 return posix_error();
4225 Py_INCREF(Py_None);
4226 return Py_None;
4227}
4228#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004229
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004230#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4231static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004232wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004233{
4234 PyObject *result;
4235 static PyObject *struct_rusage;
4236
4237 if (pid == -1)
4238 return posix_error();
4239
4240 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004241 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004242 if (m == NULL)
4243 return NULL;
4244 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4245 Py_DECREF(m);
4246 if (struct_rusage == NULL)
4247 return NULL;
4248 }
4249
4250 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4251 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4252 if (!result)
4253 return NULL;
4254
4255#ifndef doubletime
4256#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4257#endif
4258
4259 PyStructSequence_SET_ITEM(result, 0,
4260 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4261 PyStructSequence_SET_ITEM(result, 1,
4262 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4263#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004264 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004265 SET_INT(result, 2, ru->ru_maxrss);
4266 SET_INT(result, 3, ru->ru_ixrss);
4267 SET_INT(result, 4, ru->ru_idrss);
4268 SET_INT(result, 5, ru->ru_isrss);
4269 SET_INT(result, 6, ru->ru_minflt);
4270 SET_INT(result, 7, ru->ru_majflt);
4271 SET_INT(result, 8, ru->ru_nswap);
4272 SET_INT(result, 9, ru->ru_inblock);
4273 SET_INT(result, 10, ru->ru_oublock);
4274 SET_INT(result, 11, ru->ru_msgsnd);
4275 SET_INT(result, 12, ru->ru_msgrcv);
4276 SET_INT(result, 13, ru->ru_nsignals);
4277 SET_INT(result, 14, ru->ru_nvcsw);
4278 SET_INT(result, 15, ru->ru_nivcsw);
4279#undef SET_INT
4280
4281 if (PyErr_Occurred()) {
4282 Py_DECREF(result);
4283 return NULL;
4284 }
4285
4286 return Py_BuildValue("iiN", pid, status, result);
4287}
4288#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4289
4290#ifdef HAVE_WAIT3
4291PyDoc_STRVAR(posix_wait3__doc__,
4292"wait3(options) -> (pid, status, rusage)\n\n\
4293Wait for completion of a child process.");
4294
4295static PyObject *
4296posix_wait3(PyObject *self, PyObject *args)
4297{
Christian Heimes292d3512008-02-03 16:51:08 +00004298 pid_t pid;
4299 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004300 struct rusage ru;
4301 WAIT_TYPE status;
4302 WAIT_STATUS_INT(status) = 0;
4303
4304 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4305 return NULL;
4306
4307 Py_BEGIN_ALLOW_THREADS
4308 pid = wait3(&status, options, &ru);
4309 Py_END_ALLOW_THREADS
4310
4311 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4312}
4313#endif /* HAVE_WAIT3 */
4314
4315#ifdef HAVE_WAIT4
4316PyDoc_STRVAR(posix_wait4__doc__,
4317"wait4(pid, options) -> (pid, status, rusage)\n\n\
4318Wait for completion of a given child process.");
4319
4320static PyObject *
4321posix_wait4(PyObject *self, PyObject *args)
4322{
Christian Heimes292d3512008-02-03 16:51:08 +00004323 pid_t pid;
4324 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004325 struct rusage ru;
4326 WAIT_TYPE status;
4327 WAIT_STATUS_INT(status) = 0;
4328
4329 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4330 return NULL;
4331
4332 Py_BEGIN_ALLOW_THREADS
4333 pid = wait4(pid, &status, options, &ru);
4334 Py_END_ALLOW_THREADS
4335
4336 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4337}
4338#endif /* HAVE_WAIT4 */
4339
Guido van Rossumb6775db1994-08-01 11:34:53 +00004340#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004341PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004342"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004343Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004344
Barry Warsaw53699e91996-12-10 23:23:01 +00004345static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004346posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004347{
Christian Heimes292d3512008-02-03 16:51:08 +00004348 pid_t pid;
4349 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004350 WAIT_TYPE status;
4351 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004352
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004353 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004354 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004355 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004356 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004357 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004358 if (pid == -1)
4359 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004360
4361 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004362}
4363
Tim Petersab034fa2002-02-01 11:27:43 +00004364#elif defined(HAVE_CWAIT)
4365
4366/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004367PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004368"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004369"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004370
4371static PyObject *
4372posix_waitpid(PyObject *self, PyObject *args)
4373{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004374 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004375 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004376
4377 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4378 return NULL;
4379 Py_BEGIN_ALLOW_THREADS
4380 pid = _cwait(&status, pid, options);
4381 Py_END_ALLOW_THREADS
4382 if (pid == -1)
4383 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004384
4385 /* shift the status left a byte so this is more like the POSIX waitpid */
4386 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004387}
4388#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004389
Guido van Rossumad0ee831995-03-01 10:34:45 +00004390#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004391PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004392"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004393Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004394
Barry Warsaw53699e91996-12-10 23:23:01 +00004395static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004396posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004397{
Christian Heimes292d3512008-02-03 16:51:08 +00004398 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004399 WAIT_TYPE status;
4400 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004401
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004402 Py_BEGIN_ALLOW_THREADS
4403 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004404 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004405 if (pid == -1)
4406 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004407
4408 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004409}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004410#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004411
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004413PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004414"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004415Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004416
Barry Warsaw53699e91996-12-10 23:23:01 +00004417static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004418posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004419{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004420#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004421 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004422#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004423#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004424 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004425#else
4426 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4427#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004428#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004429}
4430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004431
Guido van Rossumb6775db1994-08-01 11:34:53 +00004432#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004433PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004434"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004435Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004436
Barry Warsaw53699e91996-12-10 23:23:01 +00004437static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004438posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004439{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004440 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004441 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004442 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004443 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004444 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004445
4446 if (!PyArg_ParseTuple(args, "et:readlink",
4447 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004448 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004449 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004450 if (v == NULL) {
4451 PyMem_Free(path);
4452 return NULL;
4453 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004454
4455 if (PyUnicode_Check(v)) {
4456 arg_is_unicode = 1;
4457 }
4458 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004459
Barry Warsaw53699e91996-12-10 23:23:01 +00004460 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004461 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004462 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004463 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004464 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004465
Neal Norwitzfca70052007-08-12 16:56:02 +00004466 PyMem_Free(path);
Christian Heimes72b710a2008-05-26 13:28:38 +00004467 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004468 if (arg_is_unicode) {
4469 PyObject *w;
4470
4471 w = PyUnicode_FromEncodedObject(v,
4472 Py_FileSystemDefaultEncoding,
4473 "strict");
4474 if (w != NULL) {
4475 Py_DECREF(v);
4476 v = w;
4477 }
4478 else {
4479 /* fall back to the original byte string, as
4480 discussed in patch #683592 */
4481 PyErr_Clear();
4482 }
4483 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004484 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004485}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004486#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004487
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004488
Guido van Rossumb6775db1994-08-01 11:34:53 +00004489#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004490PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004491"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004492Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004493
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004494static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004495posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004496{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004497 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004498}
4499#endif /* HAVE_SYMLINK */
4500
4501
4502#ifdef HAVE_TIMES
4503#ifndef HZ
4504#define HZ 60 /* Universal constant :-) */
4505#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004506
Guido van Rossumd48f2521997-12-05 22:19:34 +00004507#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4508static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004509system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004510{
4511 ULONG value = 0;
4512
4513 Py_BEGIN_ALLOW_THREADS
4514 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4515 Py_END_ALLOW_THREADS
4516
4517 return value;
4518}
4519
4520static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004521posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004522{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004523 /* Currently Only Uptime is Provided -- Others Later */
4524 return Py_BuildValue("ddddd",
4525 (double)0 /* t.tms_utime / HZ */,
4526 (double)0 /* t.tms_stime / HZ */,
4527 (double)0 /* t.tms_cutime / HZ */,
4528 (double)0 /* t.tms_cstime / HZ */,
4529 (double)system_uptime() / 1000);
4530}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004531#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004532static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004533posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004534{
4535 struct tms t;
4536 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004537 errno = 0;
4538 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004539 if (c == (clock_t) -1)
4540 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004541 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004542 (double)t.tms_utime / HZ,
4543 (double)t.tms_stime / HZ,
4544 (double)t.tms_cutime / HZ,
4545 (double)t.tms_cstime / HZ,
4546 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004547}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004548#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004549#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004550
4551
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004552#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004553#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004554static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004555posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004556{
4557 FILETIME create, exit, kernel, user;
4558 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004559 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004560 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4561 /* The fields of a FILETIME structure are the hi and lo part
4562 of a 64-bit value expressed in 100 nanosecond units.
4563 1e7 is one second in such units; 1e-7 the inverse.
4564 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4565 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004566 return Py_BuildValue(
4567 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004568 (double)(user.dwHighDateTime*429.4967296 +
4569 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004570 (double)(kernel.dwHighDateTime*429.4967296 +
4571 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004572 (double)0,
4573 (double)0,
4574 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004575}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004576#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004577
4578#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004579PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004580"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004581Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004582#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004583
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004584
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004585#ifdef HAVE_GETSID
4586PyDoc_STRVAR(posix_getsid__doc__,
4587"getsid(pid) -> sid\n\n\
4588Call the system call getsid().");
4589
4590static PyObject *
4591posix_getsid(PyObject *self, PyObject *args)
4592{
Christian Heimes292d3512008-02-03 16:51:08 +00004593 pid_t pid;
4594 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004595 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4596 return NULL;
4597 sid = getsid(pid);
4598 if (sid < 0)
4599 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004600 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004601}
4602#endif /* HAVE_GETSID */
4603
4604
Guido van Rossumb6775db1994-08-01 11:34:53 +00004605#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004606PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004607"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004608Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004609
Barry Warsaw53699e91996-12-10 23:23:01 +00004610static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004611posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004612{
Guido van Rossum687dd131993-05-17 08:34:16 +00004613 if (setsid() < 0)
4614 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004615 Py_INCREF(Py_None);
4616 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004617}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004618#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004619
Guido van Rossumb6775db1994-08-01 11:34:53 +00004620#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004621PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004622"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004623Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Barry Warsaw53699e91996-12-10 23:23:01 +00004625static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004626posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004627{
Christian Heimes292d3512008-02-03 16:51:08 +00004628 pid_t pid;
4629 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004630 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004631 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004632 if (setpgid(pid, pgrp) < 0)
4633 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004634 Py_INCREF(Py_None);
4635 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004636}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004637#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004638
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004639
Guido van Rossumb6775db1994-08-01 11:34:53 +00004640#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004641PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004642"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004643Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004644
Barry Warsaw53699e91996-12-10 23:23:01 +00004645static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004646posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004647{
Christian Heimes15ebc882008-02-04 18:48:49 +00004648 int fd;
4649 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004650 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004651 return NULL;
4652 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004653 if (pgid < 0)
4654 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004655 return PyLong_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004656}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004657#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004658
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004659
Guido van Rossumb6775db1994-08-01 11:34:53 +00004660#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004661PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004662"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004663Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004664
Barry Warsaw53699e91996-12-10 23:23:01 +00004665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004666posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004667{
4668 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004669 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004670 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004671 if (tcsetpgrp(fd, pgid) < 0)
4672 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004673 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004674 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004675}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004676#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004677
Guido van Rossum687dd131993-05-17 08:34:16 +00004678/* Functions acting on file descriptors */
4679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004680PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004681"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004682Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004683
Barry Warsaw53699e91996-12-10 23:23:01 +00004684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004685posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004686{
Mark Hammondef8b6542001-05-13 08:04:26 +00004687 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004688 int flag;
4689 int mode = 0777;
4690 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004691
4692#ifdef MS_WINDOWS
4693 if (unicode_file_names()) {
4694 PyUnicodeObject *po;
4695 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4696 Py_BEGIN_ALLOW_THREADS
4697 /* PyUnicode_AS_UNICODE OK without thread
4698 lock as it is a simple dereference. */
4699 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4700 Py_END_ALLOW_THREADS
4701 if (fd < 0)
4702 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004703 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004704 }
4705 /* Drop the argument parsing error as narrow strings
4706 are also valid. */
4707 PyErr_Clear();
4708 }
4709#endif
4710
Tim Peters5aa91602002-01-30 05:46:57 +00004711 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004712 Py_FileSystemDefaultEncoding, &file,
4713 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004714 return NULL;
4715
Barry Warsaw53699e91996-12-10 23:23:01 +00004716 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004717 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004718 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004719 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004720 return posix_error_with_allocated_filename(file);
4721 PyMem_Free(file);
Christian Heimes217cfd12007-12-02 14:31:20 +00004722 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004723}
4724
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004726PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004727"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004728Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004729
Barry Warsaw53699e91996-12-10 23:23:01 +00004730static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004731posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004732{
4733 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004734 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004735 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004736 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004737 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004738 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004739 if (res < 0)
4740 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004741 Py_INCREF(Py_None);
4742 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004743}
4744
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004745
Christian Heimesfdab48e2008-01-20 09:06:41 +00004746PyDoc_STRVAR(posix_closerange__doc__,
4747"closerange(fd_low, fd_high)\n\n\
4748Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4749
4750static PyObject *
4751posix_closerange(PyObject *self, PyObject *args)
4752{
4753 int fd_from, fd_to, i;
4754 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4755 return NULL;
4756 Py_BEGIN_ALLOW_THREADS
4757 for (i = fd_from; i < fd_to; i++)
4758 close(i);
4759 Py_END_ALLOW_THREADS
4760 Py_RETURN_NONE;
4761}
4762
4763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004764PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004765"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004766Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004767
Barry Warsaw53699e91996-12-10 23:23:01 +00004768static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004769posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004770{
4771 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004772 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004773 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004774 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004775 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004776 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004777 if (fd < 0)
4778 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004779 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004780}
4781
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004783PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004784"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004785Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004786
Barry Warsaw53699e91996-12-10 23:23:01 +00004787static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004788posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004789{
4790 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004791 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004792 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004793 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004794 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004795 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004796 if (res < 0)
4797 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004798 Py_INCREF(Py_None);
4799 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004800}
4801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004803PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004804"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004805Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004806
Barry Warsaw53699e91996-12-10 23:23:01 +00004807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004808posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004809{
4810 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004811#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004812 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004813#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004814 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004815#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004816 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004817 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004818 return NULL;
4819#ifdef SEEK_SET
4820 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4821 switch (how) {
4822 case 0: how = SEEK_SET; break;
4823 case 1: how = SEEK_CUR; break;
4824 case 2: how = SEEK_END; break;
4825 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004826#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004827
4828#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004829 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004830#else
4831 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004832 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004833#endif
4834 if (PyErr_Occurred())
4835 return NULL;
4836
Barry Warsaw53699e91996-12-10 23:23:01 +00004837 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004838#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004839 res = _lseeki64(fd, pos, how);
4840#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004841 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004842#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004843 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004844 if (res < 0)
4845 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004846
4847#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004848 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004849#else
4850 return PyLong_FromLongLong(res);
4851#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004852}
4853
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004854
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004855PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004856"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004857Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004858
Barry Warsaw53699e91996-12-10 23:23:01 +00004859static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004860posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004861{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004862 int fd, size;
4863 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004864 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004865 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004866 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004867 if (size < 0) {
4868 errno = EINVAL;
4869 return posix_error();
4870 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004871 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004872 if (buffer == NULL)
4873 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004874 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00004875 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004876 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004877 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004878 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004879 return posix_error();
4880 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004881 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00004882 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004883 return buffer;
4884}
4885
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004887PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004888"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004889Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004890
Barry Warsaw53699e91996-12-10 23:23:01 +00004891static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004892posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004893{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004894 int fd;
4895 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004896 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004897
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004898 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004899 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004900 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004901 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004902 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004903 if (size < 0)
4904 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004905 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004906}
4907
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004908
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004909PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004910"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004911Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004912
Barry Warsaw53699e91996-12-10 23:23:01 +00004913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004914posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004915{
4916 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004917 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004918 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004919 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004920 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004921#ifdef __VMS
4922 /* on OpenVMS we must ensure that all bytes are written to the file */
4923 fsync(fd);
4924#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004925 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004926 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004927 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004928 if (res != 0) {
4929#ifdef MS_WINDOWS
4930 return win32_error("fstat", NULL);
4931#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004932 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004933#endif
4934 }
Tim Peters5aa91602002-01-30 05:46:57 +00004935
Martin v. Löwis14694662006-02-03 12:54:16 +00004936 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004937}
4938
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004939PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004940"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004941Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004942connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004943
4944static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004945posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004946{
4947 int fd;
4948 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4949 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004950 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004951}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004952
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004953#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004954PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004955"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004956Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004957
Barry Warsaw53699e91996-12-10 23:23:01 +00004958static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004959posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004960{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004961#if defined(PYOS_OS2)
4962 HFILE read, write;
4963 APIRET rc;
4964
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004965 Py_BEGIN_ALLOW_THREADS
4966 rc = DosCreatePipe( &read, &write, 4096);
4967 Py_END_ALLOW_THREADS
4968 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004969 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004970
4971 return Py_BuildValue("(ii)", read, write);
4972#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004973#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004974 int fds[2];
4975 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004976 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004977 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004978 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004979 if (res != 0)
4980 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004981 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004982#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004983 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004984 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004985 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004986 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004987 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004988 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004989 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004990 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004991 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4992 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004993 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004994#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004995#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004996}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004997#endif /* HAVE_PIPE */
4998
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004999
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005000#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005001PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005002"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005003Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005004
Barry Warsaw53699e91996-12-10 23:23:01 +00005005static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005006posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005007{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005008 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005009 int mode = 0666;
5010 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005011 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005012 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005013 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005014 res = mkfifo(filename, mode);
5015 Py_END_ALLOW_THREADS
5016 if (res < 0)
5017 return posix_error();
5018 Py_INCREF(Py_None);
5019 return Py_None;
5020}
5021#endif
5022
5023
Neal Norwitz11690112002-07-30 01:08:28 +00005024#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005026"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005027Create a filesystem node (file, device special file or named pipe)\n\
5028named filename. mode specifies both the permissions to use and the\n\
5029type of node to be created, being combined (bitwise OR) with one of\n\
5030S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005031device defines the newly created device special file (probably using\n\
5032os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005033
5034
5035static PyObject *
5036posix_mknod(PyObject *self, PyObject *args)
5037{
5038 char *filename;
5039 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005040 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005041 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005042 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005043 return NULL;
5044 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005045 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005046 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005047 if (res < 0)
5048 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005049 Py_INCREF(Py_None);
5050 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005051}
5052#endif
5053
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005054#ifdef HAVE_DEVICE_MACROS
5055PyDoc_STRVAR(posix_major__doc__,
5056"major(device) -> major number\n\
5057Extracts a device major number from a raw device number.");
5058
5059static PyObject *
5060posix_major(PyObject *self, PyObject *args)
5061{
5062 int device;
5063 if (!PyArg_ParseTuple(args, "i:major", &device))
5064 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005065 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005066}
5067
5068PyDoc_STRVAR(posix_minor__doc__,
5069"minor(device) -> minor number\n\
5070Extracts a device minor number from a raw device number.");
5071
5072static PyObject *
5073posix_minor(PyObject *self, PyObject *args)
5074{
5075 int device;
5076 if (!PyArg_ParseTuple(args, "i:minor", &device))
5077 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005078 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005079}
5080
5081PyDoc_STRVAR(posix_makedev__doc__,
5082"makedev(major, minor) -> device number\n\
5083Composes a raw device number from the major and minor device numbers.");
5084
5085static PyObject *
5086posix_makedev(PyObject *self, PyObject *args)
5087{
5088 int major, minor;
5089 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5090 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005091 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005092}
5093#endif /* device macros */
5094
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005095
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005096#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005097PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005098"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005099Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005100
Barry Warsaw53699e91996-12-10 23:23:01 +00005101static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005102posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005103{
5104 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005105 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005106 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005107 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005108
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005109 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005110 return NULL;
5111
5112#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005113 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005114#else
5115 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005116 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005117#endif
5118 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005119 return NULL;
5120
Barry Warsaw53699e91996-12-10 23:23:01 +00005121 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005122 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005123 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005124 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005125 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005126 return NULL;
5127 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005128 Py_INCREF(Py_None);
5129 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005130}
5131#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005132
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005133#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005134PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005135"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005136Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005137
Fred Drake762e2061999-08-26 17:23:54 +00005138/* Save putenv() parameters as values here, so we can collect them when they
5139 * get re-set with another call for the same key. */
5140static PyObject *posix_putenv_garbage;
5141
Tim Peters5aa91602002-01-30 05:46:57 +00005142static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005143posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005144{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005145#ifdef MS_WINDOWS
5146 wchar_t *s1, *s2;
5147 wchar_t *newenv;
5148#else
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005149 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005150 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005151#endif
Fred Drake762e2061999-08-26 17:23:54 +00005152 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005153 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005154
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005155 if (!PyArg_ParseTuple(args,
5156#ifdef MS_WINDOWS
5157 "uu:putenv",
5158#else
5159 "ss:putenv",
5160#endif
5161 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005162 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005163
5164#if defined(PYOS_OS2)
5165 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5166 APIRET rc;
5167
Guido van Rossumd48f2521997-12-05 22:19:34 +00005168 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5169 if (rc != NO_ERROR)
5170 return os2_error(rc);
5171
5172 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5173 APIRET rc;
5174
Guido van Rossumd48f2521997-12-05 22:19:34 +00005175 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5176 if (rc != NO_ERROR)
5177 return os2_error(rc);
5178 } else {
5179#endif
Fred Drake762e2061999-08-26 17:23:54 +00005180 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005181 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005182 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005183#ifdef MS_WINDOWS
5184 len = wcslen(s1) + wcslen(s2) + 2;
5185 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5186#else
5187 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005188 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005189#endif
Fred Drake762e2061999-08-26 17:23:54 +00005190 if (newstr == NULL)
5191 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005192#ifdef MS_WINDOWS
5193 newenv = PyUnicode_AsUnicode(newstr);
5194 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5195 if (_wputenv(newenv)) {
5196 Py_DECREF(newstr);
5197 posix_error();
5198 return NULL;
5199 }
5200#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005201 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005202 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5203 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005204 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005205 posix_error();
5206 return NULL;
5207 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005208#endif
Fred Drake762e2061999-08-26 17:23:54 +00005209 /* Install the first arg and newstr in posix_putenv_garbage;
5210 * this will cause previous value to be collected. This has to
5211 * happen after the real putenv() call because the old value
5212 * was still accessible until then. */
5213 if (PyDict_SetItem(posix_putenv_garbage,
5214 PyTuple_GET_ITEM(args, 0), newstr)) {
5215 /* really not much we can do; just leak */
5216 PyErr_Clear();
5217 }
5218 else {
5219 Py_DECREF(newstr);
5220 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005221
5222#if defined(PYOS_OS2)
5223 }
5224#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005225 Py_INCREF(Py_None);
5226 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005227}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005228#endif /* putenv */
5229
Guido van Rossumc524d952001-10-19 01:31:59 +00005230#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005231PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005232"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005233Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005234
5235static PyObject *
5236posix_unsetenv(PyObject *self, PyObject *args)
5237{
5238 char *s1;
5239
5240 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5241 return NULL;
5242
5243 unsetenv(s1);
5244
5245 /* Remove the key from posix_putenv_garbage;
5246 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005247 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005248 * old value was still accessible until then.
5249 */
5250 if (PyDict_DelItem(posix_putenv_garbage,
5251 PyTuple_GET_ITEM(args, 0))) {
5252 /* really not much we can do; just leak */
5253 PyErr_Clear();
5254 }
5255
5256 Py_INCREF(Py_None);
5257 return Py_None;
5258}
5259#endif /* unsetenv */
5260
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005261PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005262"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005263Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005264
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005265static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005266posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005267{
5268 int code;
5269 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005270 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005271 return NULL;
5272 message = strerror(code);
5273 if (message == NULL) {
5274 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005275 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005276 return NULL;
5277 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005278 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005279}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005280
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005281
Guido van Rossumc9641791998-08-04 15:26:23 +00005282#ifdef HAVE_SYS_WAIT_H
5283
Fred Drake106c1a02002-04-23 15:58:02 +00005284#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005285PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005286"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005287Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005288
5289static PyObject *
5290posix_WCOREDUMP(PyObject *self, PyObject *args)
5291{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005292 WAIT_TYPE status;
5293 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005294
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005295 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005296 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005297
5298 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005299}
5300#endif /* WCOREDUMP */
5301
5302#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005303PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005304"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005305Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005306job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005307
5308static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005309posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005310{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005311 WAIT_TYPE status;
5312 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005313
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005314 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005315 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005316
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005317 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005318}
5319#endif /* WIFCONTINUED */
5320
Guido van Rossumc9641791998-08-04 15:26:23 +00005321#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005322PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005323"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005324Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005325
5326static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005327posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005328{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005329 WAIT_TYPE status;
5330 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005331
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005332 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005333 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005334
Fred Drake106c1a02002-04-23 15:58:02 +00005335 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005336}
5337#endif /* WIFSTOPPED */
5338
5339#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005340PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005341"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005342Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005343
5344static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005345posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005346{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005347 WAIT_TYPE status;
5348 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005349
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005350 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005351 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005352
Fred Drake106c1a02002-04-23 15:58:02 +00005353 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005354}
5355#endif /* WIFSIGNALED */
5356
5357#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005359"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005360Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005361system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005362
5363static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005364posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005365{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005366 WAIT_TYPE status;
5367 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005368
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005369 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005370 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005371
Fred Drake106c1a02002-04-23 15:58:02 +00005372 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005373}
5374#endif /* WIFEXITED */
5375
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005376#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005377PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005378"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005379Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005380
5381static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005382posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005383{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005384 WAIT_TYPE status;
5385 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005386
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005387 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005388 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005389
Guido van Rossumc9641791998-08-04 15:26:23 +00005390 return Py_BuildValue("i", WEXITSTATUS(status));
5391}
5392#endif /* WEXITSTATUS */
5393
5394#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005395PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005396"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005397Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005398value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005399
5400static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005401posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005402{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005403 WAIT_TYPE status;
5404 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005405
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005406 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005407 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005408
Guido van Rossumc9641791998-08-04 15:26:23 +00005409 return Py_BuildValue("i", WTERMSIG(status));
5410}
5411#endif /* WTERMSIG */
5412
5413#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005414PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005415"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416Return the signal that stopped the process that provided\n\
5417the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005418
5419static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005420posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005421{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005422 WAIT_TYPE status;
5423 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005424
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005425 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005426 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005427
Guido van Rossumc9641791998-08-04 15:26:23 +00005428 return Py_BuildValue("i", WSTOPSIG(status));
5429}
5430#endif /* WSTOPSIG */
5431
5432#endif /* HAVE_SYS_WAIT_H */
5433
5434
Thomas Wouters477c8d52006-05-27 19:21:47 +00005435#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005436#ifdef _SCO_DS
5437/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5438 needed definitions in sys/statvfs.h */
5439#define _SVID3
5440#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005441#include <sys/statvfs.h>
5442
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005443static PyObject*
5444_pystatvfs_fromstructstatvfs(struct statvfs st) {
5445 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5446 if (v == NULL)
5447 return NULL;
5448
5449#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005450 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5451 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5452 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5453 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5454 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5455 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5456 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5457 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5458 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5459 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005460#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005461 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5462 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005463 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005464 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005465 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005466 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005467 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005468 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005469 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005470 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005471 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005472 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005473 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005474 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005475 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5476 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005477#endif
5478
5479 return v;
5480}
5481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005482PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005483"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005484Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005485
5486static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005487posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005488{
5489 int fd, res;
5490 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005491
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005492 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005493 return NULL;
5494 Py_BEGIN_ALLOW_THREADS
5495 res = fstatvfs(fd, &st);
5496 Py_END_ALLOW_THREADS
5497 if (res != 0)
5498 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005499
5500 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005501}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005502#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005503
5504
Thomas Wouters477c8d52006-05-27 19:21:47 +00005505#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005506#include <sys/statvfs.h>
5507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005508PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005509"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005510Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005511
5512static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005513posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005514{
5515 char *path;
5516 int res;
5517 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005518 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005519 return NULL;
5520 Py_BEGIN_ALLOW_THREADS
5521 res = statvfs(path, &st);
5522 Py_END_ALLOW_THREADS
5523 if (res != 0)
5524 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005525
5526 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005527}
5528#endif /* HAVE_STATVFS */
5529
Fred Drakec9680921999-12-13 16:37:25 +00005530/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5531 * It maps strings representing configuration variable names to
5532 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005533 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005534 * rarely-used constants. There are three separate tables that use
5535 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005536 *
5537 * This code is always included, even if none of the interfaces that
5538 * need it are included. The #if hackery needed to avoid it would be
5539 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005540 */
5541struct constdef {
5542 char *name;
5543 long value;
5544};
5545
Fred Drake12c6e2d1999-12-14 21:25:03 +00005546static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005547conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005548 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005549{
Christian Heimes217cfd12007-12-02 14:31:20 +00005550 if (PyLong_Check(arg)) {
5551 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005552 return 1;
5553 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005554 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005555 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005556 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005557 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005558 size_t hi = tablesize;
5559 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005560 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005561 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005562 PyErr_SetString(PyExc_TypeError,
5563 "configuration names must be strings or integers");
5564 return 0;
5565 }
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005566 confname = PyUnicode_AsString(arg);
5567 if (confname == NULL)
5568 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005569 while (lo < hi) {
5570 mid = (lo + hi) / 2;
5571 cmp = strcmp(confname, table[mid].name);
5572 if (cmp < 0)
5573 hi = mid;
5574 else if (cmp > 0)
5575 lo = mid + 1;
5576 else {
5577 *valuep = table[mid].value;
5578 return 1;
5579 }
5580 }
5581 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005582 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005583 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005584}
5585
5586
5587#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5588static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005589#ifdef _PC_ABI_AIO_XFER_MAX
5590 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5591#endif
5592#ifdef _PC_ABI_ASYNC_IO
5593 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5594#endif
Fred Drakec9680921999-12-13 16:37:25 +00005595#ifdef _PC_ASYNC_IO
5596 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5597#endif
5598#ifdef _PC_CHOWN_RESTRICTED
5599 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5600#endif
5601#ifdef _PC_FILESIZEBITS
5602 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5603#endif
5604#ifdef _PC_LAST
5605 {"PC_LAST", _PC_LAST},
5606#endif
5607#ifdef _PC_LINK_MAX
5608 {"PC_LINK_MAX", _PC_LINK_MAX},
5609#endif
5610#ifdef _PC_MAX_CANON
5611 {"PC_MAX_CANON", _PC_MAX_CANON},
5612#endif
5613#ifdef _PC_MAX_INPUT
5614 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5615#endif
5616#ifdef _PC_NAME_MAX
5617 {"PC_NAME_MAX", _PC_NAME_MAX},
5618#endif
5619#ifdef _PC_NO_TRUNC
5620 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5621#endif
5622#ifdef _PC_PATH_MAX
5623 {"PC_PATH_MAX", _PC_PATH_MAX},
5624#endif
5625#ifdef _PC_PIPE_BUF
5626 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5627#endif
5628#ifdef _PC_PRIO_IO
5629 {"PC_PRIO_IO", _PC_PRIO_IO},
5630#endif
5631#ifdef _PC_SOCK_MAXBUF
5632 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5633#endif
5634#ifdef _PC_SYNC_IO
5635 {"PC_SYNC_IO", _PC_SYNC_IO},
5636#endif
5637#ifdef _PC_VDISABLE
5638 {"PC_VDISABLE", _PC_VDISABLE},
5639#endif
5640};
5641
Fred Drakec9680921999-12-13 16:37:25 +00005642static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005643conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005644{
5645 return conv_confname(arg, valuep, posix_constants_pathconf,
5646 sizeof(posix_constants_pathconf)
5647 / sizeof(struct constdef));
5648}
5649#endif
5650
5651#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005652PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005653"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005654Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005655If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005656
5657static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005658posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005659{
5660 PyObject *result = NULL;
5661 int name, fd;
5662
Fred Drake12c6e2d1999-12-14 21:25:03 +00005663 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5664 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005665 long limit;
5666
5667 errno = 0;
5668 limit = fpathconf(fd, name);
5669 if (limit == -1 && errno != 0)
5670 posix_error();
5671 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005672 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005673 }
5674 return result;
5675}
5676#endif
5677
5678
5679#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005680PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005681"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005682Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005684
5685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005686posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005687{
5688 PyObject *result = NULL;
5689 int name;
5690 char *path;
5691
5692 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5693 conv_path_confname, &name)) {
5694 long limit;
5695
5696 errno = 0;
5697 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005698 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005699 if (errno == EINVAL)
5700 /* could be a path or name problem */
5701 posix_error();
5702 else
5703 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005704 }
Fred Drakec9680921999-12-13 16:37:25 +00005705 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005706 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005707 }
5708 return result;
5709}
5710#endif
5711
5712#ifdef HAVE_CONFSTR
5713static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005714#ifdef _CS_ARCHITECTURE
5715 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5716#endif
5717#ifdef _CS_HOSTNAME
5718 {"CS_HOSTNAME", _CS_HOSTNAME},
5719#endif
5720#ifdef _CS_HW_PROVIDER
5721 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5722#endif
5723#ifdef _CS_HW_SERIAL
5724 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5725#endif
5726#ifdef _CS_INITTAB_NAME
5727 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5728#endif
Fred Drakec9680921999-12-13 16:37:25 +00005729#ifdef _CS_LFS64_CFLAGS
5730 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5731#endif
5732#ifdef _CS_LFS64_LDFLAGS
5733 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5734#endif
5735#ifdef _CS_LFS64_LIBS
5736 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5737#endif
5738#ifdef _CS_LFS64_LINTFLAGS
5739 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5740#endif
5741#ifdef _CS_LFS_CFLAGS
5742 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5743#endif
5744#ifdef _CS_LFS_LDFLAGS
5745 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5746#endif
5747#ifdef _CS_LFS_LIBS
5748 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5749#endif
5750#ifdef _CS_LFS_LINTFLAGS
5751 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5752#endif
Fred Draked86ed291999-12-15 15:34:33 +00005753#ifdef _CS_MACHINE
5754 {"CS_MACHINE", _CS_MACHINE},
5755#endif
Fred Drakec9680921999-12-13 16:37:25 +00005756#ifdef _CS_PATH
5757 {"CS_PATH", _CS_PATH},
5758#endif
Fred Draked86ed291999-12-15 15:34:33 +00005759#ifdef _CS_RELEASE
5760 {"CS_RELEASE", _CS_RELEASE},
5761#endif
5762#ifdef _CS_SRPC_DOMAIN
5763 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5764#endif
5765#ifdef _CS_SYSNAME
5766 {"CS_SYSNAME", _CS_SYSNAME},
5767#endif
5768#ifdef _CS_VERSION
5769 {"CS_VERSION", _CS_VERSION},
5770#endif
Fred Drakec9680921999-12-13 16:37:25 +00005771#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5772 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5773#endif
5774#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5775 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5776#endif
5777#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5778 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5779#endif
5780#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5781 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5782#endif
5783#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5784 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5785#endif
5786#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5787 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5788#endif
5789#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5790 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5791#endif
5792#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5793 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5794#endif
5795#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5796 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5797#endif
5798#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5799 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5800#endif
5801#ifdef _CS_XBS5_LP64_OFF64_LIBS
5802 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5803#endif
5804#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5805 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5806#endif
5807#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5808 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5809#endif
5810#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5811 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5812#endif
5813#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5814 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5815#endif
5816#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5817 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5818#endif
Fred Draked86ed291999-12-15 15:34:33 +00005819#ifdef _MIPS_CS_AVAIL_PROCESSORS
5820 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5821#endif
5822#ifdef _MIPS_CS_BASE
5823 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5824#endif
5825#ifdef _MIPS_CS_HOSTID
5826 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5827#endif
5828#ifdef _MIPS_CS_HW_NAME
5829 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5830#endif
5831#ifdef _MIPS_CS_NUM_PROCESSORS
5832 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5833#endif
5834#ifdef _MIPS_CS_OSREL_MAJ
5835 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5836#endif
5837#ifdef _MIPS_CS_OSREL_MIN
5838 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5839#endif
5840#ifdef _MIPS_CS_OSREL_PATCH
5841 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5842#endif
5843#ifdef _MIPS_CS_OS_NAME
5844 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5845#endif
5846#ifdef _MIPS_CS_OS_PROVIDER
5847 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5848#endif
5849#ifdef _MIPS_CS_PROCESSORS
5850 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5851#endif
5852#ifdef _MIPS_CS_SERIAL
5853 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5854#endif
5855#ifdef _MIPS_CS_VENDOR
5856 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5857#endif
Fred Drakec9680921999-12-13 16:37:25 +00005858};
5859
5860static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005861conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005862{
5863 return conv_confname(arg, valuep, posix_constants_confstr,
5864 sizeof(posix_constants_confstr)
5865 / sizeof(struct constdef));
5866}
5867
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005868PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005869"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005870Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005871
5872static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005873posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005874{
5875 PyObject *result = NULL;
5876 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005877 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005878
5879 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005880 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005881
Fred Drakec9680921999-12-13 16:37:25 +00005882 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005883 len = confstr(name, buffer, sizeof(buffer));
5884 if (len == 0) {
5885 if (errno) {
5886 posix_error();
5887 }
5888 else {
5889 result = Py_None;
5890 Py_INCREF(Py_None);
5891 }
Fred Drakec9680921999-12-13 16:37:25 +00005892 }
5893 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005894 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005895 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005896 if (result != NULL)
Neal Norwitz93c56822007-08-26 07:10:06 +00005897 confstr(name, PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005898 }
5899 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005900 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005901 }
5902 }
5903 return result;
5904}
5905#endif
5906
5907
5908#ifdef HAVE_SYSCONF
5909static struct constdef posix_constants_sysconf[] = {
5910#ifdef _SC_2_CHAR_TERM
5911 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5912#endif
5913#ifdef _SC_2_C_BIND
5914 {"SC_2_C_BIND", _SC_2_C_BIND},
5915#endif
5916#ifdef _SC_2_C_DEV
5917 {"SC_2_C_DEV", _SC_2_C_DEV},
5918#endif
5919#ifdef _SC_2_C_VERSION
5920 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5921#endif
5922#ifdef _SC_2_FORT_DEV
5923 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5924#endif
5925#ifdef _SC_2_FORT_RUN
5926 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5927#endif
5928#ifdef _SC_2_LOCALEDEF
5929 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5930#endif
5931#ifdef _SC_2_SW_DEV
5932 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5933#endif
5934#ifdef _SC_2_UPE
5935 {"SC_2_UPE", _SC_2_UPE},
5936#endif
5937#ifdef _SC_2_VERSION
5938 {"SC_2_VERSION", _SC_2_VERSION},
5939#endif
Fred Draked86ed291999-12-15 15:34:33 +00005940#ifdef _SC_ABI_ASYNCHRONOUS_IO
5941 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5942#endif
5943#ifdef _SC_ACL
5944 {"SC_ACL", _SC_ACL},
5945#endif
Fred Drakec9680921999-12-13 16:37:25 +00005946#ifdef _SC_AIO_LISTIO_MAX
5947 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5948#endif
Fred Drakec9680921999-12-13 16:37:25 +00005949#ifdef _SC_AIO_MAX
5950 {"SC_AIO_MAX", _SC_AIO_MAX},
5951#endif
5952#ifdef _SC_AIO_PRIO_DELTA_MAX
5953 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5954#endif
5955#ifdef _SC_ARG_MAX
5956 {"SC_ARG_MAX", _SC_ARG_MAX},
5957#endif
5958#ifdef _SC_ASYNCHRONOUS_IO
5959 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5960#endif
5961#ifdef _SC_ATEXIT_MAX
5962 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5963#endif
Fred Draked86ed291999-12-15 15:34:33 +00005964#ifdef _SC_AUDIT
5965 {"SC_AUDIT", _SC_AUDIT},
5966#endif
Fred Drakec9680921999-12-13 16:37:25 +00005967#ifdef _SC_AVPHYS_PAGES
5968 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5969#endif
5970#ifdef _SC_BC_BASE_MAX
5971 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5972#endif
5973#ifdef _SC_BC_DIM_MAX
5974 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5975#endif
5976#ifdef _SC_BC_SCALE_MAX
5977 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5978#endif
5979#ifdef _SC_BC_STRING_MAX
5980 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5981#endif
Fred Draked86ed291999-12-15 15:34:33 +00005982#ifdef _SC_CAP
5983 {"SC_CAP", _SC_CAP},
5984#endif
Fred Drakec9680921999-12-13 16:37:25 +00005985#ifdef _SC_CHARCLASS_NAME_MAX
5986 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5987#endif
5988#ifdef _SC_CHAR_BIT
5989 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5990#endif
5991#ifdef _SC_CHAR_MAX
5992 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5993#endif
5994#ifdef _SC_CHAR_MIN
5995 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5996#endif
5997#ifdef _SC_CHILD_MAX
5998 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5999#endif
6000#ifdef _SC_CLK_TCK
6001 {"SC_CLK_TCK", _SC_CLK_TCK},
6002#endif
6003#ifdef _SC_COHER_BLKSZ
6004 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6005#endif
6006#ifdef _SC_COLL_WEIGHTS_MAX
6007 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6008#endif
6009#ifdef _SC_DCACHE_ASSOC
6010 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6011#endif
6012#ifdef _SC_DCACHE_BLKSZ
6013 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6014#endif
6015#ifdef _SC_DCACHE_LINESZ
6016 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6017#endif
6018#ifdef _SC_DCACHE_SZ
6019 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6020#endif
6021#ifdef _SC_DCACHE_TBLKSZ
6022 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6023#endif
6024#ifdef _SC_DELAYTIMER_MAX
6025 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6026#endif
6027#ifdef _SC_EQUIV_CLASS_MAX
6028 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6029#endif
6030#ifdef _SC_EXPR_NEST_MAX
6031 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6032#endif
6033#ifdef _SC_FSYNC
6034 {"SC_FSYNC", _SC_FSYNC},
6035#endif
6036#ifdef _SC_GETGR_R_SIZE_MAX
6037 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6038#endif
6039#ifdef _SC_GETPW_R_SIZE_MAX
6040 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6041#endif
6042#ifdef _SC_ICACHE_ASSOC
6043 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6044#endif
6045#ifdef _SC_ICACHE_BLKSZ
6046 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6047#endif
6048#ifdef _SC_ICACHE_LINESZ
6049 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6050#endif
6051#ifdef _SC_ICACHE_SZ
6052 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6053#endif
Fred Draked86ed291999-12-15 15:34:33 +00006054#ifdef _SC_INF
6055 {"SC_INF", _SC_INF},
6056#endif
Fred Drakec9680921999-12-13 16:37:25 +00006057#ifdef _SC_INT_MAX
6058 {"SC_INT_MAX", _SC_INT_MAX},
6059#endif
6060#ifdef _SC_INT_MIN
6061 {"SC_INT_MIN", _SC_INT_MIN},
6062#endif
6063#ifdef _SC_IOV_MAX
6064 {"SC_IOV_MAX", _SC_IOV_MAX},
6065#endif
Fred Draked86ed291999-12-15 15:34:33 +00006066#ifdef _SC_IP_SECOPTS
6067 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6068#endif
Fred Drakec9680921999-12-13 16:37:25 +00006069#ifdef _SC_JOB_CONTROL
6070 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6071#endif
Fred Draked86ed291999-12-15 15:34:33 +00006072#ifdef _SC_KERN_POINTERS
6073 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6074#endif
6075#ifdef _SC_KERN_SIM
6076 {"SC_KERN_SIM", _SC_KERN_SIM},
6077#endif
Fred Drakec9680921999-12-13 16:37:25 +00006078#ifdef _SC_LINE_MAX
6079 {"SC_LINE_MAX", _SC_LINE_MAX},
6080#endif
6081#ifdef _SC_LOGIN_NAME_MAX
6082 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6083#endif
6084#ifdef _SC_LOGNAME_MAX
6085 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6086#endif
6087#ifdef _SC_LONG_BIT
6088 {"SC_LONG_BIT", _SC_LONG_BIT},
6089#endif
Fred Draked86ed291999-12-15 15:34:33 +00006090#ifdef _SC_MAC
6091 {"SC_MAC", _SC_MAC},
6092#endif
Fred Drakec9680921999-12-13 16:37:25 +00006093#ifdef _SC_MAPPED_FILES
6094 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6095#endif
6096#ifdef _SC_MAXPID
6097 {"SC_MAXPID", _SC_MAXPID},
6098#endif
6099#ifdef _SC_MB_LEN_MAX
6100 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6101#endif
6102#ifdef _SC_MEMLOCK
6103 {"SC_MEMLOCK", _SC_MEMLOCK},
6104#endif
6105#ifdef _SC_MEMLOCK_RANGE
6106 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6107#endif
6108#ifdef _SC_MEMORY_PROTECTION
6109 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6110#endif
6111#ifdef _SC_MESSAGE_PASSING
6112 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6113#endif
Fred Draked86ed291999-12-15 15:34:33 +00006114#ifdef _SC_MMAP_FIXED_ALIGNMENT
6115 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6116#endif
Fred Drakec9680921999-12-13 16:37:25 +00006117#ifdef _SC_MQ_OPEN_MAX
6118 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6119#endif
6120#ifdef _SC_MQ_PRIO_MAX
6121 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6122#endif
Fred Draked86ed291999-12-15 15:34:33 +00006123#ifdef _SC_NACLS_MAX
6124 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6125#endif
Fred Drakec9680921999-12-13 16:37:25 +00006126#ifdef _SC_NGROUPS_MAX
6127 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6128#endif
6129#ifdef _SC_NL_ARGMAX
6130 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6131#endif
6132#ifdef _SC_NL_LANGMAX
6133 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6134#endif
6135#ifdef _SC_NL_MSGMAX
6136 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6137#endif
6138#ifdef _SC_NL_NMAX
6139 {"SC_NL_NMAX", _SC_NL_NMAX},
6140#endif
6141#ifdef _SC_NL_SETMAX
6142 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6143#endif
6144#ifdef _SC_NL_TEXTMAX
6145 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6146#endif
6147#ifdef _SC_NPROCESSORS_CONF
6148 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6149#endif
6150#ifdef _SC_NPROCESSORS_ONLN
6151 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6152#endif
Fred Draked86ed291999-12-15 15:34:33 +00006153#ifdef _SC_NPROC_CONF
6154 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6155#endif
6156#ifdef _SC_NPROC_ONLN
6157 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6158#endif
Fred Drakec9680921999-12-13 16:37:25 +00006159#ifdef _SC_NZERO
6160 {"SC_NZERO", _SC_NZERO},
6161#endif
6162#ifdef _SC_OPEN_MAX
6163 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6164#endif
6165#ifdef _SC_PAGESIZE
6166 {"SC_PAGESIZE", _SC_PAGESIZE},
6167#endif
6168#ifdef _SC_PAGE_SIZE
6169 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6170#endif
6171#ifdef _SC_PASS_MAX
6172 {"SC_PASS_MAX", _SC_PASS_MAX},
6173#endif
6174#ifdef _SC_PHYS_PAGES
6175 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6176#endif
6177#ifdef _SC_PII
6178 {"SC_PII", _SC_PII},
6179#endif
6180#ifdef _SC_PII_INTERNET
6181 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6182#endif
6183#ifdef _SC_PII_INTERNET_DGRAM
6184 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6185#endif
6186#ifdef _SC_PII_INTERNET_STREAM
6187 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6188#endif
6189#ifdef _SC_PII_OSI
6190 {"SC_PII_OSI", _SC_PII_OSI},
6191#endif
6192#ifdef _SC_PII_OSI_CLTS
6193 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6194#endif
6195#ifdef _SC_PII_OSI_COTS
6196 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6197#endif
6198#ifdef _SC_PII_OSI_M
6199 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6200#endif
6201#ifdef _SC_PII_SOCKET
6202 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6203#endif
6204#ifdef _SC_PII_XTI
6205 {"SC_PII_XTI", _SC_PII_XTI},
6206#endif
6207#ifdef _SC_POLL
6208 {"SC_POLL", _SC_POLL},
6209#endif
6210#ifdef _SC_PRIORITIZED_IO
6211 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6212#endif
6213#ifdef _SC_PRIORITY_SCHEDULING
6214 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6215#endif
6216#ifdef _SC_REALTIME_SIGNALS
6217 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6218#endif
6219#ifdef _SC_RE_DUP_MAX
6220 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6221#endif
6222#ifdef _SC_RTSIG_MAX
6223 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6224#endif
6225#ifdef _SC_SAVED_IDS
6226 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6227#endif
6228#ifdef _SC_SCHAR_MAX
6229 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6230#endif
6231#ifdef _SC_SCHAR_MIN
6232 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6233#endif
6234#ifdef _SC_SELECT
6235 {"SC_SELECT", _SC_SELECT},
6236#endif
6237#ifdef _SC_SEMAPHORES
6238 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6239#endif
6240#ifdef _SC_SEM_NSEMS_MAX
6241 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6242#endif
6243#ifdef _SC_SEM_VALUE_MAX
6244 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6245#endif
6246#ifdef _SC_SHARED_MEMORY_OBJECTS
6247 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6248#endif
6249#ifdef _SC_SHRT_MAX
6250 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6251#endif
6252#ifdef _SC_SHRT_MIN
6253 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6254#endif
6255#ifdef _SC_SIGQUEUE_MAX
6256 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6257#endif
6258#ifdef _SC_SIGRT_MAX
6259 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6260#endif
6261#ifdef _SC_SIGRT_MIN
6262 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6263#endif
Fred Draked86ed291999-12-15 15:34:33 +00006264#ifdef _SC_SOFTPOWER
6265 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6266#endif
Fred Drakec9680921999-12-13 16:37:25 +00006267#ifdef _SC_SPLIT_CACHE
6268 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6269#endif
6270#ifdef _SC_SSIZE_MAX
6271 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6272#endif
6273#ifdef _SC_STACK_PROT
6274 {"SC_STACK_PROT", _SC_STACK_PROT},
6275#endif
6276#ifdef _SC_STREAM_MAX
6277 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6278#endif
6279#ifdef _SC_SYNCHRONIZED_IO
6280 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6281#endif
6282#ifdef _SC_THREADS
6283 {"SC_THREADS", _SC_THREADS},
6284#endif
6285#ifdef _SC_THREAD_ATTR_STACKADDR
6286 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6287#endif
6288#ifdef _SC_THREAD_ATTR_STACKSIZE
6289 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6290#endif
6291#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6292 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6293#endif
6294#ifdef _SC_THREAD_KEYS_MAX
6295 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6296#endif
6297#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6298 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6299#endif
6300#ifdef _SC_THREAD_PRIO_INHERIT
6301 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6302#endif
6303#ifdef _SC_THREAD_PRIO_PROTECT
6304 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6305#endif
6306#ifdef _SC_THREAD_PROCESS_SHARED
6307 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6308#endif
6309#ifdef _SC_THREAD_SAFE_FUNCTIONS
6310 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6311#endif
6312#ifdef _SC_THREAD_STACK_MIN
6313 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6314#endif
6315#ifdef _SC_THREAD_THREADS_MAX
6316 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6317#endif
6318#ifdef _SC_TIMERS
6319 {"SC_TIMERS", _SC_TIMERS},
6320#endif
6321#ifdef _SC_TIMER_MAX
6322 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6323#endif
6324#ifdef _SC_TTY_NAME_MAX
6325 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6326#endif
6327#ifdef _SC_TZNAME_MAX
6328 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6329#endif
6330#ifdef _SC_T_IOV_MAX
6331 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6332#endif
6333#ifdef _SC_UCHAR_MAX
6334 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6335#endif
6336#ifdef _SC_UINT_MAX
6337 {"SC_UINT_MAX", _SC_UINT_MAX},
6338#endif
6339#ifdef _SC_UIO_MAXIOV
6340 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6341#endif
6342#ifdef _SC_ULONG_MAX
6343 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6344#endif
6345#ifdef _SC_USHRT_MAX
6346 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6347#endif
6348#ifdef _SC_VERSION
6349 {"SC_VERSION", _SC_VERSION},
6350#endif
6351#ifdef _SC_WORD_BIT
6352 {"SC_WORD_BIT", _SC_WORD_BIT},
6353#endif
6354#ifdef _SC_XBS5_ILP32_OFF32
6355 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6356#endif
6357#ifdef _SC_XBS5_ILP32_OFFBIG
6358 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6359#endif
6360#ifdef _SC_XBS5_LP64_OFF64
6361 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6362#endif
6363#ifdef _SC_XBS5_LPBIG_OFFBIG
6364 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6365#endif
6366#ifdef _SC_XOPEN_CRYPT
6367 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6368#endif
6369#ifdef _SC_XOPEN_ENH_I18N
6370 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6371#endif
6372#ifdef _SC_XOPEN_LEGACY
6373 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6374#endif
6375#ifdef _SC_XOPEN_REALTIME
6376 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6377#endif
6378#ifdef _SC_XOPEN_REALTIME_THREADS
6379 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6380#endif
6381#ifdef _SC_XOPEN_SHM
6382 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6383#endif
6384#ifdef _SC_XOPEN_UNIX
6385 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6386#endif
6387#ifdef _SC_XOPEN_VERSION
6388 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6389#endif
6390#ifdef _SC_XOPEN_XCU_VERSION
6391 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6392#endif
6393#ifdef _SC_XOPEN_XPG2
6394 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6395#endif
6396#ifdef _SC_XOPEN_XPG3
6397 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6398#endif
6399#ifdef _SC_XOPEN_XPG4
6400 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6401#endif
6402};
6403
6404static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006405conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006406{
6407 return conv_confname(arg, valuep, posix_constants_sysconf,
6408 sizeof(posix_constants_sysconf)
6409 / sizeof(struct constdef));
6410}
6411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006412PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006413"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006414Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006415
6416static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006417posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006418{
6419 PyObject *result = NULL;
6420 int name;
6421
6422 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6423 int value;
6424
6425 errno = 0;
6426 value = sysconf(name);
6427 if (value == -1 && errno != 0)
6428 posix_error();
6429 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006430 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006431 }
6432 return result;
6433}
6434#endif
6435
6436
Fred Drakebec628d1999-12-15 18:31:10 +00006437/* This code is used to ensure that the tables of configuration value names
6438 * are in sorted order as required by conv_confname(), and also to build the
6439 * the exported dictionaries that are used to publish information about the
6440 * names available on the host platform.
6441 *
6442 * Sorting the table at runtime ensures that the table is properly ordered
6443 * when used, even for platforms we're not able to test on. It also makes
6444 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006445 */
Fred Drakebec628d1999-12-15 18:31:10 +00006446
6447static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006448cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006449{
6450 const struct constdef *c1 =
6451 (const struct constdef *) v1;
6452 const struct constdef *c2 =
6453 (const struct constdef *) v2;
6454
6455 return strcmp(c1->name, c2->name);
6456}
6457
6458static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006459setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006460 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006461{
Fred Drakebec628d1999-12-15 18:31:10 +00006462 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006463 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006464
6465 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6466 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006467 if (d == NULL)
6468 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006469
Barry Warsaw3155db32000-04-13 15:20:40 +00006470 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006471 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006472 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6473 Py_XDECREF(o);
6474 Py_DECREF(d);
6475 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006476 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006477 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006478 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006479 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006480}
6481
Fred Drakebec628d1999-12-15 18:31:10 +00006482/* Return -1 on failure, 0 on success. */
6483static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006484setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006485{
6486#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006487 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006488 sizeof(posix_constants_pathconf)
6489 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006490 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006491 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006492#endif
6493#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006494 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006495 sizeof(posix_constants_confstr)
6496 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006497 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006498 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006499#endif
6500#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006501 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006502 sizeof(posix_constants_sysconf)
6503 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006504 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006505 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006506#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006507 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006508}
Fred Draked86ed291999-12-15 15:34:33 +00006509
6510
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006511PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006512"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006513Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006514in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006515
6516static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006517posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006518{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006519 abort();
6520 /*NOTREACHED*/
6521 Py_FatalError("abort() called from Python code didn't abort!");
6522 return NULL;
6523}
Fred Drakebec628d1999-12-15 18:31:10 +00006524
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006525#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006526PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006527"startfile(filepath [, operation]) - Start a file with its associated\n\
6528application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006529\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006530When \"operation\" is not specified or \"open\", this acts like\n\
6531double-clicking the file in Explorer, or giving the file name as an\n\
6532argument to the DOS \"start\" command: the file is opened with whatever\n\
6533application (if any) its extension is associated.\n\
6534When another \"operation\" is given, it specifies what should be done with\n\
6535the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006536\n\
6537startfile returns as soon as the associated application is launched.\n\
6538There is no option to wait for the application to close, and no way\n\
6539to retrieve the application's exit status.\n\
6540\n\
6541The filepath is relative to the current directory. If you want to use\n\
6542an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006543the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006544
6545static PyObject *
6546win32_startfile(PyObject *self, PyObject *args)
6547{
6548 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006549 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006550 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006551#ifdef Py_WIN_WIDE_FILENAMES
6552 if (unicode_file_names()) {
6553 PyObject *unipath, *woperation = NULL;
6554 if (!PyArg_ParseTuple(args, "U|s:startfile",
6555 &unipath, &operation)) {
6556 PyErr_Clear();
6557 goto normal;
6558 }
6559
6560
6561 if (operation) {
6562 woperation = PyUnicode_DecodeASCII(operation,
6563 strlen(operation), NULL);
6564 if (!woperation) {
6565 PyErr_Clear();
6566 operation = NULL;
6567 goto normal;
6568 }
6569 }
6570
6571 Py_BEGIN_ALLOW_THREADS
6572 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6573 PyUnicode_AS_UNICODE(unipath),
6574 NULL, NULL, SW_SHOWNORMAL);
6575 Py_END_ALLOW_THREADS
6576
6577 Py_XDECREF(woperation);
6578 if (rc <= (HINSTANCE)32) {
6579 PyObject *errval = win32_error_unicode("startfile",
6580 PyUnicode_AS_UNICODE(unipath));
6581 return errval;
6582 }
6583 Py_INCREF(Py_None);
6584 return Py_None;
6585 }
6586#endif
6587
6588normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006589 if (!PyArg_ParseTuple(args, "et|s:startfile",
6590 Py_FileSystemDefaultEncoding, &filepath,
6591 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006592 return NULL;
6593 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006594 rc = ShellExecute((HWND)0, operation, filepath,
6595 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006596 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006597 if (rc <= (HINSTANCE)32) {
6598 PyObject *errval = win32_error("startfile", filepath);
6599 PyMem_Free(filepath);
6600 return errval;
6601 }
6602 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006603 Py_INCREF(Py_None);
6604 return Py_None;
6605}
6606#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006607
Martin v. Löwis438b5342002-12-27 10:16:42 +00006608#ifdef HAVE_GETLOADAVG
6609PyDoc_STRVAR(posix_getloadavg__doc__,
6610"getloadavg() -> (float, float, float)\n\n\
6611Return the number of processes in the system run queue averaged over\n\
6612the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6613was unobtainable");
6614
6615static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006616posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006617{
6618 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006619 if (getloadavg(loadavg, 3)!=3) {
6620 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6621 return NULL;
6622 } else
6623 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6624}
6625#endif
6626
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006627#ifdef MS_WINDOWS
6628
6629PyDoc_STRVAR(win32_urandom__doc__,
6630"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006631Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006632
6633typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6634 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6635 DWORD dwFlags );
6636typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6637 BYTE *pbBuffer );
6638
6639static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006640/* This handle is never explicitly released. Instead, the operating
6641 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006642static HCRYPTPROV hCryptProv = 0;
6643
Tim Peters4ad82172004-08-30 17:02:04 +00006644static PyObject*
6645win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006646{
Tim Petersd3115382004-08-30 17:36:46 +00006647 int howMany;
6648 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006649
Tim Peters4ad82172004-08-30 17:02:04 +00006650 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006651 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006652 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006653 if (howMany < 0)
6654 return PyErr_Format(PyExc_ValueError,
6655 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006656
Tim Peters4ad82172004-08-30 17:02:04 +00006657 if (hCryptProv == 0) {
6658 HINSTANCE hAdvAPI32 = NULL;
6659 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006660
Tim Peters4ad82172004-08-30 17:02:04 +00006661 /* Obtain handle to the DLL containing CryptoAPI
6662 This should not fail */
6663 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6664 if(hAdvAPI32 == NULL)
6665 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006666
Tim Peters4ad82172004-08-30 17:02:04 +00006667 /* Obtain pointers to the CryptoAPI functions
6668 This will fail on some early versions of Win95 */
6669 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6670 hAdvAPI32,
6671 "CryptAcquireContextA");
6672 if (pCryptAcquireContext == NULL)
6673 return PyErr_Format(PyExc_NotImplementedError,
6674 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006675
Tim Peters4ad82172004-08-30 17:02:04 +00006676 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6677 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006678 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006679 return PyErr_Format(PyExc_NotImplementedError,
6680 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006681
Tim Peters4ad82172004-08-30 17:02:04 +00006682 /* Acquire context */
6683 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6684 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6685 return win32_error("CryptAcquireContext", NULL);
6686 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006687
Tim Peters4ad82172004-08-30 17:02:04 +00006688 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006689 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006690 if (result != NULL) {
6691 /* Get random data */
6692 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006693 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006694 Py_DECREF(result);
6695 return win32_error("CryptGenRandom", NULL);
6696 }
Tim Peters4ad82172004-08-30 17:02:04 +00006697 }
Tim Petersd3115382004-08-30 17:36:46 +00006698 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006699}
6700#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006701
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006702PyDoc_STRVAR(device_encoding__doc__,
6703"device_encoding(fd) -> str\n\n\
6704Return a string describing the encoding of the device\n\
6705if the output is a terminal; else return None.");
6706
6707static PyObject *
6708device_encoding(PyObject *self, PyObject *args)
6709{
6710 int fd;
6711 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6712 return NULL;
6713 if (!isatty(fd)) {
6714 Py_INCREF(Py_None);
6715 return Py_None;
6716 }
6717#if defined(MS_WINDOWS) || defined(MS_WIN64)
6718 if (fd == 0) {
6719 char buf[100];
6720 sprintf(buf, "cp%d", GetConsoleCP());
6721 return PyUnicode_FromString(buf);
6722 }
6723 if (fd == 1 || fd == 2) {
6724 char buf[100];
6725 sprintf(buf, "cp%d", GetConsoleOutputCP());
6726 return PyUnicode_FromString(buf);
6727 }
6728#elif defined(CODESET)
6729 {
6730 char *codeset = nl_langinfo(CODESET);
6731 if (codeset)
6732 return PyUnicode_FromString(codeset);
6733 }
6734#endif
6735 Py_INCREF(Py_None);
6736 return Py_None;
6737}
6738
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006739#ifdef __VMS
6740/* Use openssl random routine */
6741#include <openssl/rand.h>
6742PyDoc_STRVAR(vms_urandom__doc__,
6743"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006744Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006745
6746static PyObject*
6747vms_urandom(PyObject *self, PyObject *args)
6748{
6749 int howMany;
6750 PyObject* result;
6751
6752 /* Read arguments */
6753 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6754 return NULL;
6755 if (howMany < 0)
6756 return PyErr_Format(PyExc_ValueError,
6757 "negative argument not allowed");
6758
6759 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006760 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006761 if (result != NULL) {
6762 /* Get random data */
6763 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006764 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006765 howMany) < 0) {
6766 Py_DECREF(result);
6767 return PyErr_Format(PyExc_ValueError,
6768 "RAND_pseudo_bytes");
6769 }
6770 }
6771 return result;
6772}
6773#endif
6774
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006775static PyMethodDef posix_methods[] = {
6776 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6777#ifdef HAVE_TTYNAME
6778 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6779#endif
6780 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006781#ifdef HAVE_CHFLAGS
6782 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6783#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006784 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00006785#ifdef HAVE_FCHMOD
6786 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6787#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006788#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006789 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006790#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00006791#ifdef HAVE_LCHMOD
6792 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6793#endif /* HAVE_LCHMOD */
6794#ifdef HAVE_FCHOWN
6795 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6796#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006797#ifdef HAVE_LCHFLAGS
6798 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6799#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006800#ifdef HAVE_LCHOWN
6801 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6802#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006803#ifdef HAVE_CHROOT
6804 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6805#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006806#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006807 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006808#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006809#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006810 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006811 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006812#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006813#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006814 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006815#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006816 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6817 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6818 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006819#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006820 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006821#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006822#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006823 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006824#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006825 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6826 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6827 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006828 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006829#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006830 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006831#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006832#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006833 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006834#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006835 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006836#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006837 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006838#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006839 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6840 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6841 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006842#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006843 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006844#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006845 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006846#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006847 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6848 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006849#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006850#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006851 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6852 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006853#if defined(PYOS_OS2)
6854 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6855 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6856#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006857#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006858#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006859 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006860#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006861#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006862 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006863#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006864#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006865 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006866#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006867#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006868 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006869#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006870#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006871 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006872#endif /* HAVE_GETEGID */
6873#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006874 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006875#endif /* HAVE_GETEUID */
6876#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006877 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006878#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006879#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006880 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006882 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006883#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006884 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006885#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006886#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006887 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006888#endif /* HAVE_GETPPID */
6889#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006890 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006891#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006892#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006893 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006894#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006895#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006896 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006897#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006898#ifdef HAVE_KILLPG
6899 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6900#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006901#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006902 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006903#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006904#ifdef MS_WINDOWS
6905 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6906#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006907#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006908 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006909#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006910#ifdef HAVE_SETEUID
6911 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6912#endif /* HAVE_SETEUID */
6913#ifdef HAVE_SETEGID
6914 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6915#endif /* HAVE_SETEGID */
6916#ifdef HAVE_SETREUID
6917 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6918#endif /* HAVE_SETREUID */
6919#ifdef HAVE_SETREGID
6920 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6921#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006922#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006923 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006924#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006925#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006926 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006927#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006928#ifdef HAVE_GETPGID
6929 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6930#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006931#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006932 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006933#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006934#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006935 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006936#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006937#ifdef HAVE_WAIT3
6938 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6939#endif /* HAVE_WAIT3 */
6940#ifdef HAVE_WAIT4
6941 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6942#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006943#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006944 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006945#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006946#ifdef HAVE_GETSID
6947 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6948#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006949#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006950 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006951#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006952#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006953 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006954#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006955#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006956 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006957#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006958#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006959 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006960#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006961 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6962 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00006963 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006964 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006965 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6966 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6967 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6968 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6969 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6970 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006971 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006972#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006973 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006974#endif
6975#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006976 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006977#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006978#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006979 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6980#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006981#ifdef HAVE_DEVICE_MACROS
6982 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6983 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6984 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6985#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006986#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006987 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006988#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006989#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006990 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006991#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006992#ifdef HAVE_UNSETENV
6993 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6994#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006995 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00006996#ifdef HAVE_FCHDIR
6997 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6998#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006999#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007000 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007001#endif
7002#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007003 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007004#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007005#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007006#ifdef WCOREDUMP
7007 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7008#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007009#ifdef WIFCONTINUED
7010 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7011#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007012#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007013 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007014#endif /* WIFSTOPPED */
7015#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007016 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007017#endif /* WIFSIGNALED */
7018#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007019 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007020#endif /* WIFEXITED */
7021#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007022 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007023#endif /* WEXITSTATUS */
7024#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007025 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007026#endif /* WTERMSIG */
7027#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007028 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007029#endif /* WSTOPSIG */
7030#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007031#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007032 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007033#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007034#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007035 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007036#endif
Fred Drakec9680921999-12-13 16:37:25 +00007037#ifdef HAVE_CONFSTR
7038 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7039#endif
7040#ifdef HAVE_SYSCONF
7041 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7042#endif
7043#ifdef HAVE_FPATHCONF
7044 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7045#endif
7046#ifdef HAVE_PATHCONF
7047 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7048#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007049 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007050#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007051 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7052#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007053#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007054 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007055#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007056 #ifdef MS_WINDOWS
7057 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7058 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007059 #ifdef __VMS
7060 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7061 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007062 {NULL, NULL} /* Sentinel */
7063};
7064
7065
Barry Warsaw4a342091996-12-19 23:50:02 +00007066static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007067ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007068{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007069 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007070}
7071
Guido van Rossumd48f2521997-12-05 22:19:34 +00007072#if defined(PYOS_OS2)
7073/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007074static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007075{
7076 APIRET rc;
7077 ULONG values[QSV_MAX+1];
7078 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007079 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007080
7081 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007082 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007083 Py_END_ALLOW_THREADS
7084
7085 if (rc != NO_ERROR) {
7086 os2_error(rc);
7087 return -1;
7088 }
7089
Fred Drake4d1e64b2002-04-15 19:40:07 +00007090 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7091 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7092 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7093 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7094 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7095 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7096 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007097
7098 switch (values[QSV_VERSION_MINOR]) {
7099 case 0: ver = "2.00"; break;
7100 case 10: ver = "2.10"; break;
7101 case 11: ver = "2.11"; break;
7102 case 30: ver = "3.00"; break;
7103 case 40: ver = "4.00"; break;
7104 case 50: ver = "5.00"; break;
7105 default:
Tim Peters885d4572001-11-28 20:27:42 +00007106 PyOS_snprintf(tmp, sizeof(tmp),
7107 "%d-%d", values[QSV_VERSION_MAJOR],
7108 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007109 ver = &tmp[0];
7110 }
7111
7112 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007113 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007114 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007115
7116 /* Add Indicator of Which Drive was Used to Boot the System */
7117 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7118 tmp[1] = ':';
7119 tmp[2] = '\0';
7120
Fred Drake4d1e64b2002-04-15 19:40:07 +00007121 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007122}
7123#endif
7124
Barry Warsaw4a342091996-12-19 23:50:02 +00007125static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007126all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007127{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007128#ifdef F_OK
7129 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007130#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007131#ifdef R_OK
7132 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007133#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007134#ifdef W_OK
7135 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007136#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007137#ifdef X_OK
7138 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007139#endif
Fred Drakec9680921999-12-13 16:37:25 +00007140#ifdef NGROUPS_MAX
7141 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7142#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007143#ifdef TMP_MAX
7144 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7145#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007146#ifdef WCONTINUED
7147 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7148#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007149#ifdef WNOHANG
7150 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007151#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007152#ifdef WUNTRACED
7153 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7154#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007155#ifdef O_RDONLY
7156 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7157#endif
7158#ifdef O_WRONLY
7159 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7160#endif
7161#ifdef O_RDWR
7162 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7163#endif
7164#ifdef O_NDELAY
7165 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7166#endif
7167#ifdef O_NONBLOCK
7168 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7169#endif
7170#ifdef O_APPEND
7171 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7172#endif
7173#ifdef O_DSYNC
7174 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7175#endif
7176#ifdef O_RSYNC
7177 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7178#endif
7179#ifdef O_SYNC
7180 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7181#endif
7182#ifdef O_NOCTTY
7183 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7184#endif
7185#ifdef O_CREAT
7186 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7187#endif
7188#ifdef O_EXCL
7189 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7190#endif
7191#ifdef O_TRUNC
7192 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7193#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007194#ifdef O_BINARY
7195 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7196#endif
7197#ifdef O_TEXT
7198 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7199#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007200#ifdef O_LARGEFILE
7201 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7202#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007203#ifdef O_SHLOCK
7204 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7205#endif
7206#ifdef O_EXLOCK
7207 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7208#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007209
Tim Peters5aa91602002-01-30 05:46:57 +00007210/* MS Windows */
7211#ifdef O_NOINHERIT
7212 /* Don't inherit in child processes. */
7213 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7214#endif
7215#ifdef _O_SHORT_LIVED
7216 /* Optimize for short life (keep in memory). */
7217 /* MS forgot to define this one with a non-underscore form too. */
7218 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7219#endif
7220#ifdef O_TEMPORARY
7221 /* Automatically delete when last handle is closed. */
7222 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7223#endif
7224#ifdef O_RANDOM
7225 /* Optimize for random access. */
7226 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7227#endif
7228#ifdef O_SEQUENTIAL
7229 /* Optimize for sequential access. */
7230 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7231#endif
7232
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007233/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007234#ifdef O_ASYNC
7235 /* Send a SIGIO signal whenever input or output
7236 becomes available on file descriptor */
7237 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7238#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007239#ifdef O_DIRECT
7240 /* Direct disk access. */
7241 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7242#endif
7243#ifdef O_DIRECTORY
7244 /* Must be a directory. */
7245 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7246#endif
7247#ifdef O_NOFOLLOW
7248 /* Do not follow links. */
7249 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7250#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007251#ifdef O_NOATIME
7252 /* Do not update the access time. */
7253 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7254#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007255
Barry Warsaw5676bd12003-01-07 20:57:09 +00007256 /* These come from sysexits.h */
7257#ifdef EX_OK
7258 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007259#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007260#ifdef EX_USAGE
7261 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007262#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007263#ifdef EX_DATAERR
7264 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007265#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007266#ifdef EX_NOINPUT
7267 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007268#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007269#ifdef EX_NOUSER
7270 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007271#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007272#ifdef EX_NOHOST
7273 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007274#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007275#ifdef EX_UNAVAILABLE
7276 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007277#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007278#ifdef EX_SOFTWARE
7279 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007280#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007281#ifdef EX_OSERR
7282 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007283#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007284#ifdef EX_OSFILE
7285 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007286#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007287#ifdef EX_CANTCREAT
7288 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007289#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007290#ifdef EX_IOERR
7291 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007292#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007293#ifdef EX_TEMPFAIL
7294 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007295#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007296#ifdef EX_PROTOCOL
7297 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007298#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007299#ifdef EX_NOPERM
7300 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007301#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007302#ifdef EX_CONFIG
7303 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007304#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007305#ifdef EX_NOTFOUND
7306 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007307#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007308
Guido van Rossum246bc171999-02-01 23:54:31 +00007309#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007310#if defined(PYOS_OS2) && defined(PYCC_GCC)
7311 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7312 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7313 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7314 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7315 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7316 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7317 if (ins(d, "P_PM", (long)P_PM)) return -1;
7318 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7319 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7320 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7321 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7322 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7323 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7324 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7325 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7326 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7327 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7328 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7329 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7330 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7331#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007332 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7333 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7334 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7335 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7336 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007337#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007338#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007339
Guido van Rossumd48f2521997-12-05 22:19:34 +00007340#if defined(PYOS_OS2)
7341 if (insertvalues(d)) return -1;
7342#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007343 return 0;
7344}
7345
7346
Tim Peters5aa91602002-01-30 05:46:57 +00007347#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007348#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007349#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007350
7351#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007352#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007353#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007354
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007355#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007356#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007357#define MODNAME "posix"
7358#endif
7359
Martin v. Löwis1a214512008-06-11 05:26:20 +00007360static struct PyModuleDef posixmodule = {
7361 PyModuleDef_HEAD_INIT,
7362 MODNAME,
7363 posix__doc__,
7364 -1,
7365 posix_methods,
7366 NULL,
7367 NULL,
7368 NULL,
7369 NULL
7370};
7371
7372
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007373PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007374INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007375{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007376 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007377
Martin v. Löwis1a214512008-06-11 05:26:20 +00007378 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007379 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007380 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007381
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007382 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007383 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007384 Py_XINCREF(v);
7385 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007386 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007387 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007388
Fred Drake4d1e64b2002-04-15 19:40:07 +00007389 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007390 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007391
Fred Drake4d1e64b2002-04-15 19:40:07 +00007392 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007393 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007394
Fred Drake4d1e64b2002-04-15 19:40:07 +00007395 Py_INCREF(PyExc_OSError);
7396 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007397
Guido van Rossumb3d39562000-01-31 18:41:26 +00007398#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007399 if (posix_putenv_garbage == NULL)
7400 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007401#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007402
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007403 if (!initialized) {
7404 stat_result_desc.name = MODNAME ".stat_result";
7405 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7406 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7407 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7408 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7409 structseq_new = StatResultType.tp_new;
7410 StatResultType.tp_new = statresult_new;
7411
7412 statvfs_result_desc.name = MODNAME ".statvfs_result";
7413 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7414 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007415 Py_INCREF((PyObject*) &StatResultType);
7416 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007417 Py_INCREF((PyObject*) &StatVFSResultType);
7418 PyModule_AddObject(m, "statvfs_result",
7419 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007420 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007421
7422#ifdef __APPLE__
7423 /*
7424 * Step 2 of weak-linking support on Mac OS X.
7425 *
7426 * The code below removes functions that are not available on the
7427 * currently active platform.
7428 *
7429 * This block allow one to use a python binary that was build on
7430 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7431 * OSX 10.4.
7432 */
7433#ifdef HAVE_FSTATVFS
7434 if (fstatvfs == NULL) {
7435 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007436 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007437 }
7438 }
7439#endif /* HAVE_FSTATVFS */
7440
7441#ifdef HAVE_STATVFS
7442 if (statvfs == NULL) {
7443 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007444 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007445 }
7446 }
7447#endif /* HAVE_STATVFS */
7448
7449# ifdef HAVE_LCHOWN
7450 if (lchown == NULL) {
7451 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007452 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007453 }
7454 }
7455#endif /* HAVE_LCHOWN */
7456
7457
7458#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007459 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007460
Guido van Rossumb6775db1994-08-01 11:34:53 +00007461}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007462
7463#ifdef __cplusplus
7464}
7465#endif