blob: 527c92a62d93160cfb310cd49df93acc65dd7b44 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000189/*#ifdef HAVE_FCHMOD
190extern int fchmod(int, mode_t);
191#endif*/
192/*#ifdef HAVE_LCHMOD
193extern int lchmod(const char *, mode_t);
194#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chown(const char *, uid_t, gid_t);
196extern char *getcwd(char *, int);
197extern char *strerror(int);
198extern int link(const char *, const char *);
199extern int rename(const char *, const char *);
200extern int stat(const char *, struct stat *);
201extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000267#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000268#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
Guido van Rossumd48f2521997-12-05 22:19:34 +0000270#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000272#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275#if defined(PATH_MAX) && PATH_MAX > 1024
276#define MAXPATHLEN PATH_MAX
277#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000278#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000279#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000280#endif /* MAXPATHLEN */
281
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000282#ifdef UNION_WAIT
283/* Emulate some macros on systems that have a union instead of macros */
284
285#ifndef WIFEXITED
286#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
287#endif
288
289#ifndef WEXITSTATUS
290#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
291#endif
292
293#ifndef WTERMSIG
294#define WTERMSIG(u_wait) ((u_wait).w_termsig)
295#endif
296
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297#define WAIT_TYPE union wait
298#define WAIT_STATUS_INT(s) (s.w_status)
299
300#else /* !UNION_WAIT */
301#define WAIT_TYPE int
302#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000303#endif /* UNION_WAIT */
304
Greg Wardb48bc172000-03-01 21:51:56 +0000305/* Don't use the "_r" form if we don't need it (also, won't have a
306 prototype for it, at least on Solaris -- maybe others as well?). */
307#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
308#define USE_CTERMID_R
309#endif
310
Fred Drake699f3522000-06-29 21:12:41 +0000311/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000312#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000313#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000314# define STAT win32_stat
315# define FSTAT win32_fstat
316# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000317#else
318# define STAT stat
319# define FSTAT fstat
320# define STRUCT_STAT struct stat
321#endif
322
Tim Peters11b23062003-04-23 02:39:17 +0000323#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000324#include <sys/mkdev.h>
325#else
326#if defined(MAJOR_IN_SYSMACROS)
327#include <sys/sysmacros.h>
328#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000329#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
330#include <sys/mkdev.h>
331#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000332#endif
Fred Drake699f3522000-06-29 21:12:41 +0000333
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000335#ifdef WITH_NEXT_FRAMEWORK
336/* On Darwin/MacOSX a shared library or framework has no access to
337** environ directly, we must obtain it with _NSGetEnviron().
338*/
339#include <crt_externs.h>
340static char **environ;
341#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000343#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344
Barry Warsaw53699e91996-12-10 23:23:01 +0000345static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000346convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347{
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000349#ifdef MS_WINDOWS
350 wchar_t **e;
351#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000353#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000354 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 if (d == NULL)
356 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000357#ifdef WITH_NEXT_FRAMEWORK
358 if (environ == NULL)
359 environ = *_NSGetEnviron();
360#endif
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000361#ifdef MS_WINDOWS
362 /* _wenviron must be initialized in this way if the program is started
363 through main() instead of wmain(). */
364 _wgetenv(L"");
365 if (_wenviron == NULL)
366 return d;
367 /* This part ignores errors */
368 for (e = _wenviron; *e != NULL; e++) {
369 PyObject *k;
370 PyObject *v;
371 wchar_t *p = wcschr(*e, L'=');
372 if (p == NULL)
373 continue;
374 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
375 if (k == NULL) {
376 PyErr_Clear();
377 continue;
378 }
379 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
380 if (v == NULL) {
381 PyErr_Clear();
382 Py_DECREF(k);
383 continue;
384 }
385 if (PyDict_GetItem(d, k) == NULL) {
386 if (PyDict_SetItem(d, k, v) != 0)
387 PyErr_Clear();
388 }
389 Py_DECREF(k);
390 Py_DECREF(v);
391 }
392#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393 if (environ == NULL)
394 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000395 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000397 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000398 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399 char *p = strchr(*e, '=');
400 if (p == NULL)
401 continue;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000402 k = PyUnicode_FromStringAndSize(*e, (int)(p-*e));
Guido van Rossum6a619f41999-08-03 19:41:10 +0000403 if (k == NULL) {
404 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000406 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000407 v = PyUnicode_FromString(p+1);
Guido van Rossum6a619f41999-08-03 19:41:10 +0000408 if (v == NULL) {
409 PyErr_Clear();
410 Py_DECREF(k);
411 continue;
412 }
413 if (PyDict_GetItem(d, k) == NULL) {
414 if (PyDict_SetItem(d, k, v) != 0)
415 PyErr_Clear();
416 }
417 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000418 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000420#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000421#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000422 {
423 APIRET rc;
424 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
425
426 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000427 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000428 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000429 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000430 Py_DECREF(v);
431 }
432 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
433 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
Christian Heimes72b710a2008-05-26 13:28:38 +0000434 PyObject *v = PyBytes_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000435 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000436 Py_DECREF(v);
437 }
438 }
439#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440 return d;
441}
442
443
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444/* Set a POSIX-specific error from errno, and return NULL */
445
Barry Warsawd58d7641998-07-23 16:14:40 +0000446static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000447posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000448{
Barry Warsawca74da41999-02-09 19:31:45 +0000449 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450}
Barry Warsawd58d7641998-07-23 16:14:40 +0000451static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000452posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000453{
Barry Warsawca74da41999-02-09 19:31:45 +0000454 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000455}
456
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000457#ifdef Py_WIN_WIDE_FILENAMES
458static PyObject *
459posix_error_with_unicode_filename(Py_UNICODE* name)
460{
461 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
462}
463#endif /* Py_WIN_WIDE_FILENAMES */
464
465
Mark Hammondef8b6542001-05-13 08:04:26 +0000466static PyObject *
467posix_error_with_allocated_filename(char* name)
468{
469 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
470 PyMem_Free(name);
471 return rc;
472}
473
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000474#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000475static PyObject *
476win32_error(char* function, char* filename)
477{
Mark Hammond33a6da92000-08-15 00:46:38 +0000478 /* XXX We should pass the function name along in the future.
Georg Brandl38feaf02008-05-25 07:45:51 +0000479 (winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000480 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000481 Windows error object, which is non-trivial.
482 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000483 errno = GetLastError();
484 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000485 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000486 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000487 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000488}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000489
490#ifdef Py_WIN_WIDE_FILENAMES
491static PyObject *
492win32_error_unicode(char* function, Py_UNICODE* filename)
493{
494 /* XXX - see win32_error for comments on 'function' */
495 errno = GetLastError();
496 if (filename)
497 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
498 else
499 return PyErr_SetFromWindowsErr(errno);
500}
501
Thomas Wouters477c8d52006-05-27 19:21:47 +0000502static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000503convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504{
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000505 if (PyUnicode_CheckExact(*param))
506 Py_INCREF(*param);
507 else if (PyUnicode_Check(*param))
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000508 /* For a Unicode subtype that's not a Unicode object,
509 return a true Unicode object with the same data. */
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000510 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
511 PyUnicode_GET_SIZE(*param));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512 else
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000513 *param = PyUnicode_FromEncodedObject(*param,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000514 Py_FileSystemDefaultEncoding,
515 "strict");
516 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000517}
518
519#endif /* Py_WIN_WIDE_FILENAMES */
520
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000521#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522
Guido van Rossumd48f2521997-12-05 22:19:34 +0000523#if defined(PYOS_OS2)
524/**********************************************************************
525 * Helper Function to Trim and Format OS/2 Messages
526 **********************************************************************/
527 static void
528os2_formatmsg(char *msgbuf, int msglen, char *reason)
529{
530 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
531
532 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
533 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
534
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000535 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000536 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
537 }
538
539 /* Add Optional Reason Text */
540 if (reason) {
541 strcat(msgbuf, " : ");
542 strcat(msgbuf, reason);
543 }
544}
545
546/**********************************************************************
547 * Decode an OS/2 Operating System Error Code
548 *
549 * A convenience function to lookup an OS/2 error code and return a
550 * text message we can use to raise a Python exception.
551 *
552 * Notes:
553 * The messages for errors returned from the OS/2 kernel reside in
554 * the file OSO001.MSG in the \OS2 directory hierarchy.
555 *
556 **********************************************************************/
557 static char *
558os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
559{
560 APIRET rc;
561 ULONG msglen;
562
563 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
564 Py_BEGIN_ALLOW_THREADS
565 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
566 errorcode, "oso001.msg", &msglen);
567 Py_END_ALLOW_THREADS
568
569 if (rc == NO_ERROR)
570 os2_formatmsg(msgbuf, msglen, reason);
571 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000572 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000573 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000574
575 return msgbuf;
576}
577
578/* Set an OS/2-specific error and return NULL. OS/2 kernel
579 errors are not in a global variable e.g. 'errno' nor are
580 they congruent with posix error numbers. */
581
582static PyObject * os2_error(int code)
583{
584 char text[1024];
585 PyObject *v;
586
587 os2_strerror(text, sizeof(text), code, "");
588
589 v = Py_BuildValue("(is)", code, text);
590 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000591 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000592 Py_DECREF(v);
593 }
594 return NULL; /* Signal to Python that an Exception is Pending */
595}
596
597#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598
599/* POSIX generic methods */
600
Barry Warsaw53699e91996-12-10 23:23:01 +0000601static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000602posix_fildes(PyObject *fdobj, int (*func)(int))
603{
604 int fd;
605 int res;
606 fd = PyObject_AsFileDescriptor(fdobj);
607 if (fd < 0)
608 return NULL;
609 Py_BEGIN_ALLOW_THREADS
610 res = (*func)(fd);
611 Py_END_ALLOW_THREADS
612 if (res < 0)
613 return posix_error();
614 Py_INCREF(Py_None);
615 return Py_None;
616}
Guido van Rossum21142a01999-01-08 21:05:37 +0000617
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000618#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000619static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000620unicode_file_names(void)
621{
622 static int canusewide = -1;
623 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000624 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000625 the Windows NT family. */
626 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
627 }
628 return canusewide;
629}
630#endif
Tim Peters11b23062003-04-23 02:39:17 +0000631
Guido van Rossum21142a01999-01-08 21:05:37 +0000632static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000634{
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000636 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000637 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000638 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000641 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000642 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000643 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000644 return posix_error_with_allocated_filename(path1);
645 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000646 Py_INCREF(Py_None);
647 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648}
649
Barry Warsaw53699e91996-12-10 23:23:01 +0000650static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000651posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000652 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654{
Mark Hammondef8b6542001-05-13 08:04:26 +0000655 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000656 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000657 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000658 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000659 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000661 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000662 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000663 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000664 PyMem_Free(path1);
665 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000666 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000667 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_INCREF(Py_None);
670 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671}
672
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673#ifdef Py_WIN_WIDE_FILENAMES
674static PyObject*
675win32_1str(PyObject* args, char* func,
676 char* format, BOOL (__stdcall *funcA)(LPCSTR),
677 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
678{
679 PyObject *uni;
680 char *ansi;
681 BOOL result;
682 if (unicode_file_names()) {
683 if (!PyArg_ParseTuple(args, wformat, &uni))
684 PyErr_Clear();
685 else {
686 Py_BEGIN_ALLOW_THREADS
687 result = funcW(PyUnicode_AsUnicode(uni));
688 Py_END_ALLOW_THREADS
689 if (!result)
690 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
691 Py_INCREF(Py_None);
692 return Py_None;
693 }
694 }
695 if (!PyArg_ParseTuple(args, format, &ansi))
696 return NULL;
697 Py_BEGIN_ALLOW_THREADS
698 result = funcA(ansi);
699 Py_END_ALLOW_THREADS
700 if (!result)
701 return win32_error(func, ansi);
702 Py_INCREF(Py_None);
703 return Py_None;
704
705}
706
707/* This is a reimplementation of the C library's chdir function,
708 but one that produces Win32 errors instead of DOS error codes.
709 chdir is essentially a wrapper around SetCurrentDirectory; however,
710 it also needs to set "magic" environment variables indicating
711 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000712static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713win32_chdir(LPCSTR path)
714{
715 char new_path[MAX_PATH+1];
716 int result;
717 char env[4] = "=x:";
718
719 if(!SetCurrentDirectoryA(path))
720 return FALSE;
721 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
722 if (!result)
723 return FALSE;
724 /* In the ANSI API, there should not be any paths longer
725 than MAX_PATH. */
726 assert(result <= MAX_PATH+1);
727 if (strncmp(new_path, "\\\\", 2) == 0 ||
728 strncmp(new_path, "//", 2) == 0)
729 /* UNC path, nothing to do. */
730 return TRUE;
731 env[1] = new_path[0];
732 return SetEnvironmentVariableA(env, new_path);
733}
734
735/* The Unicode version differs from the ANSI version
736 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000737static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738win32_wchdir(LPCWSTR path)
739{
740 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
741 int result;
742 wchar_t env[4] = L"=x:";
743
744 if(!SetCurrentDirectoryW(path))
745 return FALSE;
746 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
747 if (!result)
748 return FALSE;
749 if (result > MAX_PATH+1) {
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000750 new_path = malloc(result * sizeof(wchar_t));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751 if (!new_path) {
752 SetLastError(ERROR_OUTOFMEMORY);
753 return FALSE;
754 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000755 result = GetCurrentDirectoryW(result, new_path);
756 if (!result) {
757 free(new_path);
758 return FALSE;
759 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760 }
761 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
762 wcsncmp(new_path, L"//", 2) == 0)
763 /* UNC path, nothing to do. */
764 return TRUE;
765 env[1] = new_path[0];
766 result = SetEnvironmentVariableW(env, new_path);
767 if (new_path != _new_path)
768 free(new_path);
769 return result;
770}
771#endif
772
Martin v. Löwis14694662006-02-03 12:54:16 +0000773#ifdef MS_WINDOWS
774/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
775 - time stamps are restricted to second resolution
776 - file modification times suffer from forth-and-back conversions between
777 UTC and local time
778 Therefore, we implement our own stat, based on the Win32 API directly.
779*/
780#define HAVE_STAT_NSEC 1
781
782struct win32_stat{
783 int st_dev;
784 __int64 st_ino;
785 unsigned short st_mode;
786 int st_nlink;
787 int st_uid;
788 int st_gid;
789 int st_rdev;
790 __int64 st_size;
791 int st_atime;
792 int st_atime_nsec;
793 int st_mtime;
794 int st_mtime_nsec;
795 int st_ctime;
796 int st_ctime_nsec;
797};
798
799static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
800
801static void
802FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
803{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000804 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
805 /* Cannot simply cast and dereference in_ptr,
806 since it might not be aligned properly */
807 __int64 in;
808 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000809 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
810 /* XXX Win32 supports time stamps past 2038; we currently don't */
811 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
812}
813
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814static void
815time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
816{
817 /* XXX endianness */
818 __int64 out;
819 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000820 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000821 memcpy(out_ptr, &out, sizeof(out));
822}
823
Martin v. Löwis14694662006-02-03 12:54:16 +0000824/* Below, we *know* that ugo+r is 0444 */
825#if _S_IREAD != 0400
826#error Unsupported C library
827#endif
828static int
829attributes_to_mode(DWORD attr)
830{
831 int m = 0;
832 if (attr & FILE_ATTRIBUTE_DIRECTORY)
833 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
834 else
835 m |= _S_IFREG;
836 if (attr & FILE_ATTRIBUTE_READONLY)
837 m |= 0444;
838 else
839 m |= 0666;
840 return m;
841}
842
843static int
844attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
845{
846 memset(result, 0, sizeof(*result));
847 result->st_mode = attributes_to_mode(info->dwFileAttributes);
848 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
849 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
850 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
851 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
852
853 return 0;
854}
855
Thomas Wouters89f507f2006-12-13 04:49:30 +0000856/* Emulate GetFileAttributesEx[AW] on Windows 95 */
857static int checked = 0;
858static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
859static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
860static void
861check_gfax()
862{
863 HINSTANCE hKernel32;
864 if (checked)
865 return;
866 checked = 1;
867 hKernel32 = GetModuleHandle("KERNEL32");
868 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
869 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
870}
871
Guido van Rossumd8faa362007-04-27 19:54:29 +0000872static BOOL
873attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
874{
875 HANDLE hFindFile;
876 WIN32_FIND_DATAA FileData;
877 hFindFile = FindFirstFileA(pszFile, &FileData);
878 if (hFindFile == INVALID_HANDLE_VALUE)
879 return FALSE;
880 FindClose(hFindFile);
881 pfad->dwFileAttributes = FileData.dwFileAttributes;
882 pfad->ftCreationTime = FileData.ftCreationTime;
883 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
884 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
885 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
886 pfad->nFileSizeLow = FileData.nFileSizeLow;
887 return TRUE;
888}
889
890static BOOL
891attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
892{
893 HANDLE hFindFile;
894 WIN32_FIND_DATAW FileData;
895 hFindFile = FindFirstFileW(pszFile, &FileData);
896 if (hFindFile == INVALID_HANDLE_VALUE)
897 return FALSE;
898 FindClose(hFindFile);
899 pfad->dwFileAttributes = FileData.dwFileAttributes;
900 pfad->ftCreationTime = FileData.ftCreationTime;
901 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
902 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
903 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
904 pfad->nFileSizeLow = FileData.nFileSizeLow;
905 return TRUE;
906}
907
Thomas Wouters89f507f2006-12-13 04:49:30 +0000908static BOOL WINAPI
909Py_GetFileAttributesExA(LPCSTR pszFile,
910 GET_FILEEX_INFO_LEVELS level,
911 LPVOID pv)
912{
913 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000914 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
915 /* First try to use the system's implementation, if that is
916 available and either succeeds to gives an error other than
917 that it isn't implemented. */
918 check_gfax();
919 if (gfaxa) {
920 result = gfaxa(pszFile, level, pv);
921 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
922 return result;
923 }
924 /* It's either not present, or not implemented.
925 Emulate using FindFirstFile. */
926 if (level != GetFileExInfoStandard) {
927 SetLastError(ERROR_INVALID_PARAMETER);
928 return FALSE;
929 }
930 /* Use GetFileAttributes to validate that the file name
931 does not contain wildcards (which FindFirstFile would
932 accept). */
933 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
934 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000935 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000936}
937
938static BOOL WINAPI
939Py_GetFileAttributesExW(LPCWSTR pszFile,
940 GET_FILEEX_INFO_LEVELS level,
941 LPVOID pv)
942{
943 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000944 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
945 /* First try to use the system's implementation, if that is
946 available and either succeeds to gives an error other than
947 that it isn't implemented. */
948 check_gfax();
949 if (gfaxa) {
950 result = gfaxw(pszFile, level, pv);
951 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
952 return result;
953 }
954 /* It's either not present, or not implemented.
955 Emulate using FindFirstFile. */
956 if (level != GetFileExInfoStandard) {
957 SetLastError(ERROR_INVALID_PARAMETER);
958 return FALSE;
959 }
960 /* Use GetFileAttributes to validate that the file name
961 does not contain wildcards (which FindFirstFile would
962 accept). */
963 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
964 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000965 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000966}
967
Martin v. Löwis14694662006-02-03 12:54:16 +0000968static int
969win32_stat(const char* path, struct win32_stat *result)
970{
971 WIN32_FILE_ATTRIBUTE_DATA info;
972 int code;
973 char *dot;
974 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000975 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976 if (GetLastError() != ERROR_SHARING_VIOLATION) {
977 /* Protocol violation: we explicitly clear errno, instead of
978 setting it to a POSIX error. Callers should use GetLastError. */
979 errno = 0;
980 return -1;
981 } else {
982 /* Could not get attributes on open file. Fall back to
983 reading the directory. */
984 if (!attributes_from_dir(path, &info)) {
985 /* Very strange. This should not fail now */
986 errno = 0;
987 return -1;
988 }
989 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000990 }
991 code = attribute_data_to_stat(&info, result);
992 if (code != 0)
993 return code;
994 /* Set S_IFEXEC if it is an .exe, .bat, ... */
995 dot = strrchr(path, '.');
996 if (dot) {
997 if (stricmp(dot, ".bat") == 0 ||
998 stricmp(dot, ".cmd") == 0 ||
999 stricmp(dot, ".exe") == 0 ||
1000 stricmp(dot, ".com") == 0)
1001 result->st_mode |= 0111;
1002 }
1003 return code;
1004}
1005
1006static int
1007win32_wstat(const wchar_t* path, struct win32_stat *result)
1008{
1009 int code;
1010 const wchar_t *dot;
1011 WIN32_FILE_ATTRIBUTE_DATA info;
1012 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001013 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001014 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1015 /* Protocol violation: we explicitly clear errno, instead of
1016 setting it to a POSIX error. Callers should use GetLastError. */
1017 errno = 0;
1018 return -1;
1019 } else {
1020 /* Could not get attributes on open file. Fall back to
1021 reading the directory. */
1022 if (!attributes_from_dir_w(path, &info)) {
1023 /* Very strange. This should not fail now */
1024 errno = 0;
1025 return -1;
1026 }
1027 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001028 }
1029 code = attribute_data_to_stat(&info, result);
1030 if (code < 0)
1031 return code;
1032 /* Set IFEXEC if it is an .exe, .bat, ... */
1033 dot = wcsrchr(path, '.');
1034 if (dot) {
1035 if (_wcsicmp(dot, L".bat") == 0 ||
1036 _wcsicmp(dot, L".cmd") == 0 ||
1037 _wcsicmp(dot, L".exe") == 0 ||
1038 _wcsicmp(dot, L".com") == 0)
1039 result->st_mode |= 0111;
1040 }
1041 return code;
1042}
1043
1044static int
1045win32_fstat(int file_number, struct win32_stat *result)
1046{
1047 BY_HANDLE_FILE_INFORMATION info;
1048 HANDLE h;
1049 int type;
1050
1051 h = (HANDLE)_get_osfhandle(file_number);
1052
1053 /* Protocol violation: we explicitly clear errno, instead of
1054 setting it to a POSIX error. Callers should use GetLastError. */
1055 errno = 0;
1056
1057 if (h == INVALID_HANDLE_VALUE) {
1058 /* This is really a C library error (invalid file handle).
1059 We set the Win32 error to the closes one matching. */
1060 SetLastError(ERROR_INVALID_HANDLE);
1061 return -1;
1062 }
1063 memset(result, 0, sizeof(*result));
1064
1065 type = GetFileType(h);
1066 if (type == FILE_TYPE_UNKNOWN) {
1067 DWORD error = GetLastError();
1068 if (error != 0) {
1069 return -1;
1070 }
1071 /* else: valid but unknown file */
1072 }
1073
1074 if (type != FILE_TYPE_DISK) {
1075 if (type == FILE_TYPE_CHAR)
1076 result->st_mode = _S_IFCHR;
1077 else if (type == FILE_TYPE_PIPE)
1078 result->st_mode = _S_IFIFO;
1079 return 0;
1080 }
1081
1082 if (!GetFileInformationByHandle(h, &info)) {
1083 return -1;
1084 }
1085
1086 /* similar to stat() */
1087 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1088 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1089 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1090 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1091 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1092 /* specific to fstat() */
1093 result->st_nlink = info.nNumberOfLinks;
1094 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1095 return 0;
1096}
1097
1098#endif /* MS_WINDOWS */
1099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001100PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001101"stat_result: Result from stat or lstat.\n\n\
1102This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001103 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001104or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1105\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001106Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1107or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001108\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110
1111static PyStructSequence_Field stat_result_fields[] = {
1112 {"st_mode", "protection bits"},
1113 {"st_ino", "inode"},
1114 {"st_dev", "device"},
1115 {"st_nlink", "number of hard links"},
1116 {"st_uid", "user ID of owner"},
1117 {"st_gid", "group ID of owner"},
1118 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001119 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1120 {NULL, "integer time of last access"},
1121 {NULL, "integer time of last modification"},
1122 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001123 {"st_atime", "time of last access"},
1124 {"st_mtime", "time of last modification"},
1125 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001126#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001127 {"st_blksize", "blocksize for filesystem I/O"},
1128#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001129#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001130 {"st_blocks", "number of blocks allocated"},
1131#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001132#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001133 {"st_rdev", "device type (if inode device)"},
1134#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001135#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1136 {"st_flags", "user defined flags for file"},
1137#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001138#ifdef HAVE_STRUCT_STAT_ST_GEN
1139 {"st_gen", "generation number"},
1140#endif
1141#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1142 {"st_birthtime", "time of creation"},
1143#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001144 {0}
1145};
1146
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001147#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001148#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001150#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001151#endif
1152
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001153#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001154#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1155#else
1156#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1157#endif
1158
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001159#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001160#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1161#else
1162#define ST_RDEV_IDX ST_BLOCKS_IDX
1163#endif
1164
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001165#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1166#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1167#else
1168#define ST_FLAGS_IDX ST_RDEV_IDX
1169#endif
1170
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001171#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001172#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001173#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001174#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001175#endif
1176
1177#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1178#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1179#else
1180#define ST_BIRTHTIME_IDX ST_GEN_IDX
1181#endif
1182
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001183static PyStructSequence_Desc stat_result_desc = {
1184 "stat_result", /* name */
1185 stat_result__doc__, /* doc */
1186 stat_result_fields,
1187 10
1188};
1189
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001190PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001191"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1192This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001193 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001194or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001195\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001196See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001197
1198static PyStructSequence_Field statvfs_result_fields[] = {
1199 {"f_bsize", },
1200 {"f_frsize", },
1201 {"f_blocks", },
1202 {"f_bfree", },
1203 {"f_bavail", },
1204 {"f_files", },
1205 {"f_ffree", },
1206 {"f_favail", },
1207 {"f_flag", },
1208 {"f_namemax",},
1209 {0}
1210};
1211
1212static PyStructSequence_Desc statvfs_result_desc = {
1213 "statvfs_result", /* name */
1214 statvfs_result__doc__, /* doc */
1215 statvfs_result_fields,
1216 10
1217};
1218
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001219static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001220static PyTypeObject StatResultType;
1221static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001222static newfunc structseq_new;
1223
1224static PyObject *
1225statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1226{
1227 PyStructSequence *result;
1228 int i;
1229
1230 result = (PyStructSequence*)structseq_new(type, args, kwds);
1231 if (!result)
1232 return NULL;
1233 /* If we have been initialized from a tuple,
1234 st_?time might be set to None. Initialize it
1235 from the int slots. */
1236 for (i = 7; i <= 9; i++) {
1237 if (result->ob_item[i+3] == Py_None) {
1238 Py_DECREF(Py_None);
1239 Py_INCREF(result->ob_item[i]);
1240 result->ob_item[i+3] = result->ob_item[i];
1241 }
1242 }
1243 return (PyObject*)result;
1244}
1245
1246
1247
1248/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001249static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001250
1251PyDoc_STRVAR(stat_float_times__doc__,
1252"stat_float_times([newval]) -> oldval\n\n\
1253Determine whether os.[lf]stat represents time stamps as float objects.\n\
1254If newval is True, future calls to stat() return floats, if it is False,\n\
1255future calls return ints. \n\
1256If newval is omitted, return the current setting.\n");
1257
1258static PyObject*
1259stat_float_times(PyObject* self, PyObject *args)
1260{
1261 int newval = -1;
1262 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1263 return NULL;
1264 if (newval == -1)
1265 /* Return old value */
1266 return PyBool_FromLong(_stat_float_times);
1267 _stat_float_times = newval;
1268 Py_INCREF(Py_None);
1269 return Py_None;
1270}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001271
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001272static void
1273fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1274{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001275 PyObject *fval,*ival;
1276#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001277 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001278#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001279 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001280#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001281 if (!ival)
1282 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001283 if (_stat_float_times) {
1284 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1285 } else {
1286 fval = ival;
1287 Py_INCREF(fval);
1288 }
1289 PyStructSequence_SET_ITEM(v, index, ival);
1290 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001291}
1292
Tim Peters5aa91602002-01-30 05:46:57 +00001293/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001294 (used by posix_stat() and posix_fstat()) */
1295static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001296_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001297{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001298 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001299 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001300 if (v == NULL)
1301 return NULL;
1302
Christian Heimes217cfd12007-12-02 14:31:20 +00001303 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001304#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001305 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001306 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001307#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001308 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001309#endif
1310#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001311 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001312 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001313#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001314 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001315#endif
Christian Heimes217cfd12007-12-02 14:31:20 +00001316 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1317 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1318 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001319#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001320 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001322#else
Christian Heimes217cfd12007-12-02 14:31:20 +00001323 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001324#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001325
Martin v. Löwis14694662006-02-03 12:54:16 +00001326#if defined(HAVE_STAT_TV_NSEC)
1327 ansec = st->st_atim.tv_nsec;
1328 mnsec = st->st_mtim.tv_nsec;
1329 cnsec = st->st_ctim.tv_nsec;
1330#elif defined(HAVE_STAT_TV_NSEC2)
1331 ansec = st->st_atimespec.tv_nsec;
1332 mnsec = st->st_mtimespec.tv_nsec;
1333 cnsec = st->st_ctimespec.tv_nsec;
1334#elif defined(HAVE_STAT_NSEC)
1335 ansec = st->st_atime_nsec;
1336 mnsec = st->st_mtime_nsec;
1337 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001338#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001339 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001340#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001341 fill_time(v, 7, st->st_atime, ansec);
1342 fill_time(v, 8, st->st_mtime, mnsec);
1343 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001344
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001345#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001346 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001347 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001348#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001349#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001350 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001351 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001352#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001353#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001354 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001355 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001356#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001357#ifdef HAVE_STRUCT_STAT_ST_GEN
1358 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001359 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001360#endif
1361#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1362 {
1363 PyObject *val;
1364 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001365 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001366#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001367 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001368#else
1369 bnsec = 0;
1370#endif
1371 if (_stat_float_times) {
1372 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1373 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001374 val = PyLong_FromLong((long)bsec);
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375 }
1376 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1377 val);
1378 }
1379#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001380#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1381 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Christian Heimes217cfd12007-12-02 14:31:20 +00001382 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001383#endif
Fred Drake699f3522000-06-29 21:12:41 +00001384
1385 if (PyErr_Occurred()) {
1386 Py_DECREF(v);
1387 return NULL;
1388 }
1389
1390 return v;
1391}
1392
Martin v. Löwisd8948722004-06-02 09:57:56 +00001393#ifdef MS_WINDOWS
1394
1395/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1396 where / can be used in place of \ and the trailing slash is optional.
1397 Both SERVER and SHARE must have at least one character.
1398*/
1399
1400#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1401#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001402#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001403#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001404#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001405
Tim Peters4ad82172004-08-30 17:02:04 +00001406static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001407IsUNCRootA(char *path, int pathlen)
1408{
1409 #define ISSLASH ISSLASHA
1410
1411 int i, share;
1412
1413 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1414 /* minimum UNCRoot is \\x\y */
1415 return FALSE;
1416 for (i = 2; i < pathlen ; i++)
1417 if (ISSLASH(path[i])) break;
1418 if (i == 2 || i == pathlen)
1419 /* do not allow \\\SHARE or \\SERVER */
1420 return FALSE;
1421 share = i+1;
1422 for (i = share; i < pathlen; i++)
1423 if (ISSLASH(path[i])) break;
1424 return (i != share && (i == pathlen || i == pathlen-1));
1425
1426 #undef ISSLASH
1427}
1428
1429#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001430static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001431IsUNCRootW(Py_UNICODE *path, int pathlen)
1432{
1433 #define ISSLASH ISSLASHW
1434
1435 int i, share;
1436
1437 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1438 /* minimum UNCRoot is \\x\y */
1439 return FALSE;
1440 for (i = 2; i < pathlen ; i++)
1441 if (ISSLASH(path[i])) break;
1442 if (i == 2 || i == pathlen)
1443 /* do not allow \\\SHARE or \\SERVER */
1444 return FALSE;
1445 share = i+1;
1446 for (i = share; i < pathlen; i++)
1447 if (ISSLASH(path[i])) break;
1448 return (i != share && (i == pathlen || i == pathlen-1));
1449
1450 #undef ISSLASH
1451}
1452#endif /* Py_WIN_WIDE_FILENAMES */
1453#endif /* MS_WINDOWS */
1454
Barry Warsaw53699e91996-12-10 23:23:01 +00001455static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001456posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001457 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001458#ifdef __VMS
1459 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1460#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001461 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001462#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001463 char *wformat,
1464 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001465{
Fred Drake699f3522000-06-29 21:12:41 +00001466 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001467 char *path = NULL; /* pass this to stat; do not free() it */
1468 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001469 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001470 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001471
1472#ifdef Py_WIN_WIDE_FILENAMES
1473 /* If on wide-character-capable OS see if argument
1474 is Unicode and if so use wide API. */
1475 if (unicode_file_names()) {
1476 PyUnicodeObject *po;
1477 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001478 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1479
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001480 Py_BEGIN_ALLOW_THREADS
1481 /* PyUnicode_AS_UNICODE result OK without
1482 thread lock as it is a simple dereference. */
1483 res = wstatfunc(wpath, &st);
1484 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001485
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001486 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001487 return win32_error_unicode("stat", wpath);
1488 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001489 }
1490 /* Drop the argument parsing error as narrow strings
1491 are also valid. */
1492 PyErr_Clear();
1493 }
1494#endif
1495
Tim Peters5aa91602002-01-30 05:46:57 +00001496 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001497 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001498 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001499 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001500
Barry Warsaw53699e91996-12-10 23:23:01 +00001501 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001502 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001503 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001504
1505 if (res != 0) {
1506#ifdef MS_WINDOWS
1507 result = win32_error("stat", pathfree);
1508#else
1509 result = posix_error_with_filename(pathfree);
1510#endif
1511 }
1512 else
1513 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001514
Tim Peters500bd032001-12-19 19:05:01 +00001515 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001516 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001517}
1518
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519/* POSIX methods */
1520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001521PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001522"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001523Use the real uid/gid to test for access to a path. Note that most\n\
1524operations will use the effective uid/gid, therefore this routine can\n\
1525be used in a suid/sgid environment to test if the invoking user has the\n\
1526specified access to the path. The mode argument can be F_OK to test\n\
1527existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001528
1529static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001530posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001531{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001532 char *path;
1533 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001534
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001535#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001536 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001537 if (unicode_file_names()) {
1538 PyUnicodeObject *po;
1539 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1540 Py_BEGIN_ALLOW_THREADS
1541 /* PyUnicode_AS_UNICODE OK without thread lock as
1542 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001543 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001544 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001545 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001546 }
1547 /* Drop the argument parsing error as narrow strings
1548 are also valid. */
1549 PyErr_Clear();
1550 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001551 if (!PyArg_ParseTuple(args, "eti:access",
1552 Py_FileSystemDefaultEncoding, &path, &mode))
1553 return 0;
1554 Py_BEGIN_ALLOW_THREADS
1555 attr = GetFileAttributesA(path);
1556 Py_END_ALLOW_THREADS
1557 PyMem_Free(path);
1558finish:
1559 if (attr == 0xFFFFFFFF)
1560 /* File does not exist, or cannot read attributes */
1561 return PyBool_FromLong(0);
1562 /* Access is possible if either write access wasn't requested, or
Guido van Rossumb00324f2007-12-04 01:13:14 +00001563 the file isn't read-only, or if it's a directory, as there are
1564 no read-only directories on Windows. */
1565 return PyBool_FromLong(!(mode & 2)
1566 || !(attr & FILE_ATTRIBUTE_READONLY)
1567 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568#else
1569 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001570 if (!PyArg_ParseTuple(args, "eti:access",
1571 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001572 return NULL;
1573 Py_BEGIN_ALLOW_THREADS
1574 res = access(path, mode);
1575 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001576 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001577 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001578#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001579}
1580
Guido van Rossumd371ff11999-01-25 16:12:23 +00001581#ifndef F_OK
1582#define F_OK 0
1583#endif
1584#ifndef R_OK
1585#define R_OK 4
1586#endif
1587#ifndef W_OK
1588#define W_OK 2
1589#endif
1590#ifndef X_OK
1591#define X_OK 1
1592#endif
1593
1594#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001596"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001598
1599static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001600posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001601{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001602 int id;
1603 char *ret;
1604
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001605 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001606 return NULL;
1607
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001608#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001609 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001610 if (id == 0) {
1611 ret = ttyname();
1612 }
1613 else {
1614 ret = NULL;
1615 }
1616#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001617 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001618#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001619 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001620 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001621 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001622}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001623#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001624
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001625#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001627"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001629
1630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001631posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001632{
1633 char *ret;
1634 char buffer[L_ctermid];
1635
Greg Wardb48bc172000-03-01 21:51:56 +00001636#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001637 ret = ctermid_r(buffer);
1638#else
1639 ret = ctermid(buffer);
1640#endif
1641 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001642 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001643 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001644}
1645#endif
1646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001647PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001648"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001650
Barry Warsaw53699e91996-12-10 23:23:01 +00001651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001652posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001653{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001654#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00001655 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001656#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001657 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001658#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001659 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001660#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001661 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001662#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001663}
1664
Fred Drake4d1e64b2002-04-15 19:40:07 +00001665#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001666PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001667"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001668Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001669opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001670
1671static PyObject *
1672posix_fchdir(PyObject *self, PyObject *fdobj)
1673{
1674 return posix_fildes(fdobj, fchdir);
1675}
1676#endif /* HAVE_FCHDIR */
1677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001679PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001680"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682
Barry Warsaw53699e91996-12-10 23:23:01 +00001683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001684posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001685{
Mark Hammondef8b6542001-05-13 08:04:26 +00001686 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001687 int i;
1688 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001689#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001690 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001691 if (unicode_file_names()) {
1692 PyUnicodeObject *po;
1693 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1694 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1696 if (attr != 0xFFFFFFFF) {
1697 if (i & _S_IWRITE)
1698 attr &= ~FILE_ATTRIBUTE_READONLY;
1699 else
1700 attr |= FILE_ATTRIBUTE_READONLY;
1701 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1702 }
1703 else
1704 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001705 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001706 if (!res)
1707 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001708 PyUnicode_AS_UNICODE(po));
1709 Py_INCREF(Py_None);
1710 return Py_None;
1711 }
1712 /* Drop the argument parsing error as narrow strings
1713 are also valid. */
1714 PyErr_Clear();
1715 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001716 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1717 &path, &i))
1718 return NULL;
1719 Py_BEGIN_ALLOW_THREADS
1720 attr = GetFileAttributesA(path);
1721 if (attr != 0xFFFFFFFF) {
1722 if (i & _S_IWRITE)
1723 attr &= ~FILE_ATTRIBUTE_READONLY;
1724 else
1725 attr |= FILE_ATTRIBUTE_READONLY;
1726 res = SetFileAttributesA(path, attr);
1727 }
1728 else
1729 res = 0;
1730 Py_END_ALLOW_THREADS
1731 if (!res) {
1732 win32_error("chmod", path);
1733 PyMem_Free(path);
1734 return NULL;
1735 }
1736 PyMem_Free(path);
1737 Py_INCREF(Py_None);
1738 return Py_None;
1739#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001740 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001741 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001742 return NULL;
1743 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001744 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001745 Py_END_ALLOW_THREADS
1746 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001747 return posix_error_with_allocated_filename(path);
1748 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001749 Py_INCREF(Py_None);
1750 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001751#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001752}
1753
Christian Heimes4e30a842007-11-30 22:12:06 +00001754#ifdef HAVE_FCHMOD
1755PyDoc_STRVAR(posix_fchmod__doc__,
1756"fchmod(fd, mode)\n\n\
1757Change the access permissions of the file given by file\n\
1758descriptor fd.");
1759
1760static PyObject *
1761posix_fchmod(PyObject *self, PyObject *args)
1762{
1763 int fd, mode, res;
1764 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1765 return NULL;
1766 Py_BEGIN_ALLOW_THREADS
1767 res = fchmod(fd, mode);
1768 Py_END_ALLOW_THREADS
1769 if (res < 0)
1770 return posix_error();
1771 Py_RETURN_NONE;
1772}
1773#endif /* HAVE_FCHMOD */
1774
1775#ifdef HAVE_LCHMOD
1776PyDoc_STRVAR(posix_lchmod__doc__,
1777"lchmod(path, mode)\n\n\
1778Change the access permissions of a file. If path is a symlink, this\n\
1779affects the link itself rather than the target.");
1780
1781static PyObject *
1782posix_lchmod(PyObject *self, PyObject *args)
1783{
1784 char *path = NULL;
1785 int i;
1786 int res;
1787 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
1788 &path, &i))
1789 return NULL;
1790 Py_BEGIN_ALLOW_THREADS
1791 res = lchmod(path, i);
1792 Py_END_ALLOW_THREADS
1793 if (res < 0)
1794 return posix_error_with_allocated_filename(path);
1795 PyMem_Free(path);
1796 Py_RETURN_NONE;
1797}
1798#endif /* HAVE_LCHMOD */
1799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001800
Thomas Wouterscf297e42007-02-23 15:07:44 +00001801#ifdef HAVE_CHFLAGS
1802PyDoc_STRVAR(posix_chflags__doc__,
1803"chflags(path, flags)\n\n\
1804Set file flags.");
1805
1806static PyObject *
1807posix_chflags(PyObject *self, PyObject *args)
1808{
1809 char *path;
1810 unsigned long flags;
1811 int res;
1812 if (!PyArg_ParseTuple(args, "etk:chflags",
1813 Py_FileSystemDefaultEncoding, &path, &flags))
1814 return NULL;
1815 Py_BEGIN_ALLOW_THREADS
1816 res = chflags(path, flags);
1817 Py_END_ALLOW_THREADS
1818 if (res < 0)
1819 return posix_error_with_allocated_filename(path);
1820 PyMem_Free(path);
1821 Py_INCREF(Py_None);
1822 return Py_None;
1823}
1824#endif /* HAVE_CHFLAGS */
1825
1826#ifdef HAVE_LCHFLAGS
1827PyDoc_STRVAR(posix_lchflags__doc__,
1828"lchflags(path, flags)\n\n\
1829Set file flags.\n\
1830This function will not follow symbolic links.");
1831
1832static PyObject *
1833posix_lchflags(PyObject *self, PyObject *args)
1834{
1835 char *path;
1836 unsigned long flags;
1837 int res;
1838 if (!PyArg_ParseTuple(args, "etk:lchflags",
1839 Py_FileSystemDefaultEncoding, &path, &flags))
1840 return NULL;
1841 Py_BEGIN_ALLOW_THREADS
1842 res = lchflags(path, flags);
1843 Py_END_ALLOW_THREADS
1844 if (res < 0)
1845 return posix_error_with_allocated_filename(path);
1846 PyMem_Free(path);
1847 Py_INCREF(Py_None);
1848 return Py_None;
1849}
1850#endif /* HAVE_LCHFLAGS */
1851
Martin v. Löwis244edc82001-10-04 22:44:26 +00001852#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001853PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001854"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001856
1857static PyObject *
1858posix_chroot(PyObject *self, PyObject *args)
1859{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001860 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001861}
1862#endif
1863
Guido van Rossum21142a01999-01-08 21:05:37 +00001864#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001866"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001868
1869static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001870posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001871{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001872 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001873}
1874#endif /* HAVE_FSYNC */
1875
1876#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001877
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001878#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001879extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1880#endif
1881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001882PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001883"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001884force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001886
1887static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001888posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001889{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001890 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001891}
1892#endif /* HAVE_FDATASYNC */
1893
1894
Fredrik Lundh10723342000-07-10 16:38:09 +00001895#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001896PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001897"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001899
Barry Warsaw53699e91996-12-10 23:23:01 +00001900static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001901posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001902{
Mark Hammondef8b6542001-05-13 08:04:26 +00001903 char *path = NULL;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001904 long uid, gid;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001905 int res;
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00001906 if (!PyArg_ParseTuple(args, "etll:chown",
Tim Peters5aa91602002-01-30 05:46:57 +00001907 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001908 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001909 return NULL;
1910 Py_BEGIN_ALLOW_THREADS
1911 res = chown(path, (uid_t) uid, (gid_t) gid);
1912 Py_END_ALLOW_THREADS
1913 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001914 return posix_error_with_allocated_filename(path);
1915 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001916 Py_INCREF(Py_None);
1917 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001918}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001919#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001920
Christian Heimes4e30a842007-11-30 22:12:06 +00001921#ifdef HAVE_FCHOWN
1922PyDoc_STRVAR(posix_fchown__doc__,
1923"fchown(fd, uid, gid)\n\n\
1924Change the owner and group id of the file given by file descriptor\n\
1925fd to the numeric uid and gid.");
1926
1927static PyObject *
1928posix_fchown(PyObject *self, PyObject *args)
1929{
1930 int fd, uid, gid;
1931 int res;
1932 if (!PyArg_ParseTuple(args, "iii:chown", &fd, &uid, &gid))
1933 return NULL;
1934 Py_BEGIN_ALLOW_THREADS
1935 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1936 Py_END_ALLOW_THREADS
1937 if (res < 0)
1938 return posix_error();
1939 Py_RETURN_NONE;
1940}
1941#endif /* HAVE_FCHOWN */
1942
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001943#ifdef HAVE_LCHOWN
1944PyDoc_STRVAR(posix_lchown__doc__,
1945"lchown(path, uid, gid)\n\n\
1946Change the owner and group id of path to the numeric uid and gid.\n\
1947This function will not follow symbolic links.");
1948
1949static PyObject *
1950posix_lchown(PyObject *self, PyObject *args)
1951{
1952 char *path = NULL;
1953 int uid, gid;
1954 int res;
1955 if (!PyArg_ParseTuple(args, "etii:lchown",
1956 Py_FileSystemDefaultEncoding, &path,
1957 &uid, &gid))
1958 return NULL;
1959 Py_BEGIN_ALLOW_THREADS
1960 res = lchown(path, (uid_t) uid, (gid_t) gid);
1961 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001962 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001963 return posix_error_with_allocated_filename(path);
1964 PyMem_Free(path);
1965 Py_INCREF(Py_None);
1966 return Py_None;
1967}
1968#endif /* HAVE_LCHOWN */
1969
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001970
Guido van Rossum36bc6801995-06-14 22:54:23 +00001971#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001972static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001973posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001974{
1975 char buf[1026];
1976 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001977
1978#ifdef Py_WIN_WIDE_FILENAMES
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001979 if (!use_bytes && unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001980 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001981 wchar_t *wbuf2 = wbuf;
1982 PyObject *resobj;
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001983 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001984 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001985 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1986 /* If the buffer is large enough, len does not include the
1987 terminating \0. If the buffer is too small, len includes
1988 the space needed for the terminator. */
1989 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1990 wbuf2 = malloc(len * sizeof(wchar_t));
1991 if (wbuf2)
1992 len = GetCurrentDirectoryW(len, wbuf2);
1993 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001994 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001995 if (!wbuf2) {
1996 PyErr_NoMemory();
1997 return NULL;
1998 }
1999 if (!len) {
2000 if (wbuf2 != wbuf) free(wbuf2);
2001 return win32_error("getcwdu", NULL);
2002 }
2003 resobj = PyUnicode_FromWideChar(wbuf2, len);
2004 if (wbuf2 != wbuf) free(wbuf2);
2005 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006 }
2007#endif
2008
2009 Py_BEGIN_ALLOW_THREADS
2010#if defined(PYOS_OS2) && defined(PYCC_GCC)
2011 res = _getcwd2(buf, sizeof buf);
2012#else
2013 res = getcwd(buf, sizeof buf);
2014#endif
2015 Py_END_ALLOW_THREADS
2016 if (res == NULL)
2017 return posix_error();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002018 if (use_bytes)
2019 return PyBytes_FromStringAndSize(buf, strlen(buf));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002020 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
2021}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002022
2023PyDoc_STRVAR(posix_getcwd__doc__,
2024"getcwd() -> path\n\n\
2025Return a unicode string representing the current working directory.");
2026
2027static PyObject *
2028posix_getcwd_unicode(PyObject *self)
2029{
2030 return posix_getcwd(0);
2031}
2032
2033PyDoc_STRVAR(posix_getcwdb__doc__,
2034"getcwdb() -> path\n\n\
2035Return a bytes string representing the current working directory.");
2036
2037static PyObject *
2038posix_getcwd_bytes(PyObject *self)
2039{
2040 return posix_getcwd(1);
2041}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002042#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002043
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002044
Guido van Rossumb6775db1994-08-01 11:34:53 +00002045#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002046PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002047"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002049
Barry Warsaw53699e91996-12-10 23:23:01 +00002050static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002051posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002052{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002053 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002054}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002055#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002059"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060Return a list containing the names of the entries in the directory.\n\
2061\n\
2062 path: path of directory to list\n\
2063\n\
2064The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066
Barry Warsaw53699e91996-12-10 23:23:01 +00002067static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002068posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002069{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002070 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002071 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002072#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002073
Barry Warsaw53699e91996-12-10 23:23:01 +00002074 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002075 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002076 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002078 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00002079 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002082#ifdef Py_WIN_WIDE_FILENAMES
2083 /* If on wide-character-capable OS see if argument
2084 is Unicode and if so use wide API. */
2085 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002086 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002087 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2088 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002089 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002090 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002091 /* Overallocate for \\*.*\0 */
2092 len = PyUnicode_GET_SIZE(po);
2093 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2094 if (!wnamebuf) {
2095 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002096 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002097 }
2098 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2099 wch = len > 0 ? wnamebuf[len-1] : '\0';
2100 if (wch != L'/' && wch != L'\\' && wch != L':')
2101 wnamebuf[len++] = L'\\';
2102 wcscpy(wnamebuf + len, L"*.*");
2103 if ((d = PyList_New(0)) == NULL) {
2104 free(wnamebuf);
2105 return NULL;
2106 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002107 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2108 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002109 int error = GetLastError();
2110 if (error == ERROR_FILE_NOT_FOUND) {
2111 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002112 return d;
2113 }
2114 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002115 win32_error_unicode("FindFirstFileW", wnamebuf);
2116 free(wnamebuf);
2117 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002118 }
2119 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002120 /* Skip over . and .. */
2121 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2122 wcscmp(wFileData.cFileName, L"..") != 0) {
2123 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2124 if (v == NULL) {
2125 Py_DECREF(d);
2126 d = NULL;
2127 break;
2128 }
2129 if (PyList_Append(d, v) != 0) {
2130 Py_DECREF(v);
2131 Py_DECREF(d);
2132 d = NULL;
2133 break;
2134 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002135 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002136 }
Georg Brandl622927b2006-03-07 12:48:03 +00002137 Py_BEGIN_ALLOW_THREADS
2138 result = FindNextFileW(hFindFile, &wFileData);
2139 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002140 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2141 it got to the end of the directory. */
2142 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2143 Py_DECREF(d);
2144 win32_error_unicode("FindNextFileW", wnamebuf);
2145 FindClose(hFindFile);
2146 free(wnamebuf);
2147 return NULL;
2148 }
Georg Brandl622927b2006-03-07 12:48:03 +00002149 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002150
2151 if (FindClose(hFindFile) == FALSE) {
2152 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002153 win32_error_unicode("FindClose", wnamebuf);
2154 free(wnamebuf);
2155 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002157 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002158 return d;
2159 }
2160 /* Drop the argument parsing error as narrow strings
2161 are also valid. */
2162 PyErr_Clear();
2163 }
2164#endif
2165
Tim Peters5aa91602002-01-30 05:46:57 +00002166 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002167 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002168 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002169 if (len > 0) {
2170 char ch = namebuf[len-1];
2171 if (ch != SEP && ch != ALTSEP && ch != ':')
2172 namebuf[len++] = '/';
2173 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002174 strcpy(namebuf + len, "*.*");
2175
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002177 return NULL;
2178
2179 hFindFile = FindFirstFile(namebuf, &FileData);
2180 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002181 int error = GetLastError();
2182 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002183 return d;
2184 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002185 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002186 }
2187 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002188 /* Skip over . and .. */
2189 if (strcmp(FileData.cFileName, ".") != 0 &&
2190 strcmp(FileData.cFileName, "..") != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +00002191 v = PyBytes_FromString(FileData.cFileName);
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002192 if (v == NULL) {
2193 Py_DECREF(d);
2194 d = NULL;
2195 break;
2196 }
2197 if (PyList_Append(d, v) != 0) {
2198 Py_DECREF(v);
2199 Py_DECREF(d);
2200 d = NULL;
2201 break;
2202 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002204 }
Georg Brandl622927b2006-03-07 12:48:03 +00002205 Py_BEGIN_ALLOW_THREADS
2206 result = FindNextFile(hFindFile, &FileData);
2207 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002208 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2209 it got to the end of the directory. */
2210 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2211 Py_DECREF(d);
2212 win32_error("FindNextFile", namebuf);
2213 FindClose(hFindFile);
2214 return NULL;
2215 }
Georg Brandl622927b2006-03-07 12:48:03 +00002216 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002217
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002218 if (FindClose(hFindFile) == FALSE) {
2219 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002220 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002221 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002222
2223 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002224
Tim Peters0bb44a42000-09-15 07:44:49 +00002225#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002226
2227#ifndef MAX_PATH
2228#define MAX_PATH CCHMAXPATH
2229#endif
2230 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002231 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002232 PyObject *d, *v;
2233 char namebuf[MAX_PATH+5];
2234 HDIR hdir = 1;
2235 ULONG srchcnt = 1;
2236 FILEFINDBUF3 ep;
2237 APIRET rc;
2238
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002239 if (!PyArg_ParseTuple(args, "et#:listdir",
2240 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002241 return NULL;
2242 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002243 PyMem_Free(name);
2244 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002245 return NULL;
2246 }
2247 strcpy(namebuf, name);
2248 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002249 if (*pt == ALTSEP)
2250 *pt = SEP;
2251 if (namebuf[len-1] != SEP)
2252 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002253 strcpy(namebuf + len, "*.*");
2254
Neal Norwitz6c913782007-10-14 03:23:09 +00002255 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002256 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002257 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002258 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002259
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002260 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2261 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002262 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002263 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2264 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2265 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002266
2267 if (rc != NO_ERROR) {
2268 errno = ENOENT;
Neal Norwitz6c913782007-10-14 03:23:09 +00002269 return posix_error_with_allocated_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002270 }
2271
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002272 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002273 do {
2274 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002275 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002276 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002277
2278 strcpy(namebuf, ep.achName);
2279
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002280 /* Leave Case of Name Alone -- In Native Form */
2281 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002282
Christian Heimes72b710a2008-05-26 13:28:38 +00002283 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002284 if (v == NULL) {
2285 Py_DECREF(d);
2286 d = NULL;
2287 break;
2288 }
2289 if (PyList_Append(d, v) != 0) {
2290 Py_DECREF(v);
2291 Py_DECREF(d);
2292 d = NULL;
2293 break;
2294 }
2295 Py_DECREF(v);
2296 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2297 }
2298
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002299 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002300 return d;
2301#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002302
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002303 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002304 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002305 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002306 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002307 int arg_is_unicode = 1;
2308
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002309 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002310 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2311 arg_is_unicode = 0;
2312 PyErr_Clear();
2313 }
2314 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002315 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002316 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002317 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002318 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002319 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002320 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002321 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002322 return NULL;
2323 }
Georg Brandl622927b2006-03-07 12:48:03 +00002324 for (;;) {
Georg Brandl3dbca812008-07-23 16:10:53 +00002325 errno = 0;
Georg Brandl622927b2006-03-07 12:48:03 +00002326 Py_BEGIN_ALLOW_THREADS
2327 ep = readdir(dirp);
2328 Py_END_ALLOW_THREADS
Georg Brandl3dbca812008-07-23 16:10:53 +00002329 if (ep == NULL) {
2330 if (errno == 0) {
2331 break;
2332 } else {
2333 closedir(dirp);
2334 Py_DECREF(d);
2335 return posix_error_with_allocated_filename(name);
2336 }
2337 }
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002338 if (ep->d_name[0] == '.' &&
2339 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002340 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002341 continue;
Christian Heimes72b710a2008-05-26 13:28:38 +00002342 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002343 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002344 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002345 d = NULL;
2346 break;
2347 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002348 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002349 PyObject *w;
2350
2351 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002352 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002353 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002354 if (w != NULL) {
2355 Py_DECREF(v);
2356 v = w;
2357 }
2358 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002359 /* Ignore undecodable filenames, as discussed
2360 * in issue 3187. To include these,
2361 * use getcwdb(). */
Just van Rossum6a421832003-03-04 19:30:44 +00002362 PyErr_Clear();
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002363 Py_DECREF(v);
2364 continue;
Just van Rossum46c97842003-02-25 21:42:15 +00002365 }
Just van Rossum46c97842003-02-25 21:42:15 +00002366 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002367 if (PyList_Append(d, v) != 0) {
2368 Py_DECREF(v);
2369 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002370 d = NULL;
2371 break;
2372 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002373 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002374 }
2375 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002376 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002377
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002379
Tim Peters0bb44a42000-09-15 07:44:49 +00002380#endif /* which OS */
2381} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002382
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002383#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002384/* A helper function for abspath on win32 */
2385static PyObject *
2386posix__getfullpathname(PyObject *self, PyObject *args)
2387{
2388 /* assume encoded strings wont more than double no of chars */
2389 char inbuf[MAX_PATH*2];
2390 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002391 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002392 char outbuf[MAX_PATH*2];
2393 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002394#ifdef Py_WIN_WIDE_FILENAMES
2395 if (unicode_file_names()) {
2396 PyUnicodeObject *po;
2397 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
Benjamin Petersonf608c612008-11-16 18:33:53 +00002398 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2399 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002400 Py_UNICODE *wtemp;
Benjamin Petersonf608c612008-11-16 18:33:53 +00002401 DWORD result;
2402 PyObject *v;
2403 result = GetFullPathNameW(wpath,
2404 sizeof(woutbuf)/sizeof(woutbuf[0]),
2405 woutbuf, &wtemp);
2406 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2407 woutbufp = malloc(result * sizeof(Py_UNICODE));
2408 if (!woutbufp)
2409 return PyErr_NoMemory();
2410 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2411 }
2412 if (result)
2413 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2414 else
2415 v = win32_error_unicode("GetFullPathNameW", wpath);
2416 if (woutbufp != woutbuf)
2417 free(woutbufp);
2418 return v;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002419 }
2420 /* Drop the argument parsing error as narrow strings
2421 are also valid. */
2422 PyErr_Clear();
2423 }
2424#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002425 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2426 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002427 &insize))
2428 return NULL;
2429 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2430 outbuf, &temp))
2431 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002432 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2433 return PyUnicode_Decode(outbuf, strlen(outbuf),
2434 Py_FileSystemDefaultEncoding, NULL);
2435 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002436 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002437} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002438#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002440PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002441"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002442Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002443
Barry Warsaw53699e91996-12-10 23:23:01 +00002444static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002445posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002446{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002447 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002448 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002449 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002450
2451#ifdef Py_WIN_WIDE_FILENAMES
2452 if (unicode_file_names()) {
2453 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002454 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002455 Py_BEGIN_ALLOW_THREADS
2456 /* PyUnicode_AS_UNICODE OK without thread lock as
2457 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002458 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002459 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002460 if (!res)
2461 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002462 Py_INCREF(Py_None);
2463 return Py_None;
2464 }
2465 /* Drop the argument parsing error as narrow strings
2466 are also valid. */
2467 PyErr_Clear();
2468 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002469 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2470 Py_FileSystemDefaultEncoding, &path, &mode))
2471 return NULL;
2472 Py_BEGIN_ALLOW_THREADS
2473 /* PyUnicode_AS_UNICODE OK without thread lock as
2474 it is a simple dereference. */
2475 res = CreateDirectoryA(path, NULL);
2476 Py_END_ALLOW_THREADS
2477 if (!res) {
2478 win32_error("mkdir", path);
2479 PyMem_Free(path);
2480 return NULL;
2481 }
2482 PyMem_Free(path);
2483 Py_INCREF(Py_None);
2484 return Py_None;
2485#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002486
Tim Peters5aa91602002-01-30 05:46:57 +00002487 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002488 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002489 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002490 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002491#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002492 res = mkdir(path);
2493#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002494 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002495#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002496 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002497 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002498 return posix_error_with_allocated_filename(path);
2499 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002500 Py_INCREF(Py_None);
2501 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002502#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002503}
2504
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002505
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002506/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2507#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002508#include <sys/resource.h>
2509#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002510
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002511
2512#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002513PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002514"nice(inc) -> new_priority\n\n\
2515Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002516
Barry Warsaw53699e91996-12-10 23:23:01 +00002517static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002518posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002519{
2520 int increment, value;
2521
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002522 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002523 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002524
2525 /* There are two flavours of 'nice': one that returns the new
2526 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002527 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2528 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002529
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002530 If we are of the nice family that returns the new priority, we
2531 need to clear errno before the call, and check if errno is filled
2532 before calling posix_error() on a returnvalue of -1, because the
2533 -1 may be the actual new priority! */
2534
2535 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002536 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002537#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002538 if (value == 0)
2539 value = getpriority(PRIO_PROCESS, 0);
2540#endif
2541 if (value == -1 && errno != 0)
2542 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002543 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002544 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002545}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002546#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002548PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002549"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551
Barry Warsaw53699e91996-12-10 23:23:01 +00002552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002553posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002554{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002555#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002556 PyObject *o1, *o2;
2557 char *p1, *p2;
2558 BOOL result;
2559 if (unicode_file_names()) {
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002560 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2561 goto error;
2562 if (!convert_to_unicode(&o1))
2563 goto error;
2564 if (!convert_to_unicode(&o2)) {
2565 Py_DECREF(o1);
2566 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002567 }
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +00002568 Py_BEGIN_ALLOW_THREADS
2569 result = MoveFileW(PyUnicode_AsUnicode(o1),
2570 PyUnicode_AsUnicode(o2));
2571 Py_END_ALLOW_THREADS
2572 Py_DECREF(o1);
2573 Py_DECREF(o2);
2574 if (!result)
2575 return win32_error("rename", NULL);
2576 Py_INCREF(Py_None);
2577 return Py_None;
2578error:
2579 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002580 }
2581 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2582 return NULL;
2583 Py_BEGIN_ALLOW_THREADS
2584 result = MoveFileA(p1, p2);
2585 Py_END_ALLOW_THREADS
2586 if (!result)
2587 return win32_error("rename", NULL);
2588 Py_INCREF(Py_None);
2589 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002590#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002591 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002592#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002593}
2594
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002596PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002597"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002598Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002599
Barry Warsaw53699e91996-12-10 23:23:01 +00002600static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002601posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002602{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002603#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002604 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002605#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002606 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002607#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002608}
2609
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002612"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002613Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002614
Barry Warsaw53699e91996-12-10 23:23:01 +00002615static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002616posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002617{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002618#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002619 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002620#else
2621 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2622#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002623}
2624
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002625
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002626#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002627PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002628"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
Barry Warsaw53699e91996-12-10 23:23:01 +00002631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002632posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002633{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002634 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002635#ifdef MS_WINDOWS
2636 wchar_t *command;
2637 if (!PyArg_ParseTuple(args, "u:system", &command))
2638 return NULL;
2639#else
2640 char *command;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002641 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002642 return NULL;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002643#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002644 Py_BEGIN_ALLOW_THREADS
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002645#ifdef MS_WINDOWS
2646 sts = _wsystem(command);
2647#else
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002648 sts = system(command);
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002649#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002650 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +00002651 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002653#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002654
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002657"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002658Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002659
Barry Warsaw53699e91996-12-10 23:23:01 +00002660static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002661posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002662{
2663 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002664 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002665 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002666 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002667 if (i < 0)
2668 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00002669 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002670}
2671
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002673PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002674"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002675Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002677PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002678"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Barry Warsaw53699e91996-12-10 23:23:01 +00002681static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002682posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002683{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002684#ifdef MS_WINDOWS
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +00002685 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002686#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002687 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002688#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002689}
2690
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002691
Guido van Rossumb6775db1994-08-01 11:34:53 +00002692#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002693PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002694"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002695Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002696
Barry Warsaw53699e91996-12-10 23:23:01 +00002697static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002698posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002699{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002700 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002701 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002702
Barry Warsaw53699e91996-12-10 23:23:01 +00002703 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002704 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002705 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002706 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002707 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002708 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002709 u.sysname,
2710 u.nodename,
2711 u.release,
2712 u.version,
2713 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002714}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002715#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002716
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002717static int
2718extract_time(PyObject *t, long* sec, long* usec)
2719{
2720 long intval;
2721 if (PyFloat_Check(t)) {
2722 double tval = PyFloat_AsDouble(t);
Christian Heimes90aa7642007-12-19 02:45:37 +00002723 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002724 if (!intobj)
2725 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002726 intval = PyLong_AsLong(intobj);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002727 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002728 if (intval == -1 && PyErr_Occurred())
2729 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002730 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002731 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002732 if (*usec < 0)
2733 /* If rounding gave us a negative number,
2734 truncate. */
2735 *usec = 0;
2736 return 0;
2737 }
Christian Heimes217cfd12007-12-02 14:31:20 +00002738 intval = PyLong_AsLong(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002739 if (intval == -1 && PyErr_Occurred())
2740 return -1;
2741 *sec = intval;
2742 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002743 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002744}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002747"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002748utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002749Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002750second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002751
Barry Warsaw53699e91996-12-10 23:23:01 +00002752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002753posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002754{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002755#ifdef Py_WIN_WIDE_FILENAMES
2756 PyObject *arg;
2757 PyUnicodeObject *obwpath;
2758 wchar_t *wpath = NULL;
2759 char *apath = NULL;
2760 HANDLE hFile;
2761 long atimesec, mtimesec, ausec, musec;
2762 FILETIME atime, mtime;
2763 PyObject *result = NULL;
2764
2765 if (unicode_file_names()) {
2766 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2767 wpath = PyUnicode_AS_UNICODE(obwpath);
2768 Py_BEGIN_ALLOW_THREADS
2769 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002770 NULL, OPEN_EXISTING,
2771 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002772 Py_END_ALLOW_THREADS
2773 if (hFile == INVALID_HANDLE_VALUE)
2774 return win32_error_unicode("utime", wpath);
2775 } else
2776 /* Drop the argument parsing error as narrow strings
2777 are also valid. */
2778 PyErr_Clear();
2779 }
2780 if (!wpath) {
2781 if (!PyArg_ParseTuple(args, "etO:utime",
2782 Py_FileSystemDefaultEncoding, &apath, &arg))
2783 return NULL;
2784 Py_BEGIN_ALLOW_THREADS
2785 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002786 NULL, OPEN_EXISTING,
2787 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002788 Py_END_ALLOW_THREADS
2789 if (hFile == INVALID_HANDLE_VALUE) {
2790 win32_error("utime", apath);
2791 PyMem_Free(apath);
2792 return NULL;
2793 }
2794 PyMem_Free(apath);
2795 }
2796
2797 if (arg == Py_None) {
2798 SYSTEMTIME now;
2799 GetSystemTime(&now);
2800 if (!SystemTimeToFileTime(&now, &mtime) ||
2801 !SystemTimeToFileTime(&now, &atime)) {
2802 win32_error("utime", NULL);
2803 goto done;
2804 }
2805 }
2806 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2807 PyErr_SetString(PyExc_TypeError,
2808 "utime() arg 2 must be a tuple (atime, mtime)");
2809 goto done;
2810 }
2811 else {
2812 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2813 &atimesec, &ausec) == -1)
2814 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002816 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2817 &mtimesec, &musec) == -1)
2818 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002820 }
2821 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2822 /* Avoid putting the file name into the error here,
2823 as that may confuse the user into believing that
2824 something is wrong with the file, when it also
2825 could be the time stamp that gives a problem. */
2826 win32_error("utime", NULL);
2827 }
2828 Py_INCREF(Py_None);
2829 result = Py_None;
2830done:
2831 CloseHandle(hFile);
2832 return result;
2833#else /* Py_WIN_WIDE_FILENAMES */
2834
Neal Norwitz2adf2102004-06-09 01:46:02 +00002835 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002836 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002837 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002838 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002839
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002840#if defined(HAVE_UTIMES)
2841 struct timeval buf[2];
2842#define ATIME buf[0].tv_sec
2843#define MTIME buf[1].tv_sec
2844#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002845/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002846 struct utimbuf buf;
2847#define ATIME buf.actime
2848#define MTIME buf.modtime
2849#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002850#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002851 time_t buf[2];
2852#define ATIME buf[0]
2853#define MTIME buf[1]
2854#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002855#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002856
Mark Hammond817c9292003-12-03 01:22:38 +00002857
Thomas Wouters477c8d52006-05-27 19:21:47 +00002858 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002859 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002860 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002861 if (arg == Py_None) {
2862 /* optional time values not given */
2863 Py_BEGIN_ALLOW_THREADS
2864 res = utime(path, NULL);
2865 Py_END_ALLOW_THREADS
2866 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002867 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002868 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002869 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002870 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002871 return NULL;
2872 }
2873 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002874 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002875 &atime, &ausec) == -1) {
2876 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002877 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002878 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002879 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002880 &mtime, &musec) == -1) {
2881 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002882 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002883 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002884 ATIME = atime;
2885 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002886#ifdef HAVE_UTIMES
2887 buf[0].tv_usec = ausec;
2888 buf[1].tv_usec = musec;
2889 Py_BEGIN_ALLOW_THREADS
2890 res = utimes(path, buf);
2891 Py_END_ALLOW_THREADS
2892#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002893 Py_BEGIN_ALLOW_THREADS
2894 res = utime(path, UTIME_ARG);
2895 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002896#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002897 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002898 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002899 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002900 }
Neal Norwitz96652712004-06-06 20:40:27 +00002901 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002902 Py_INCREF(Py_None);
2903 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002904#undef UTIME_ARG
2905#undef ATIME
2906#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002907#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002908}
2909
Guido van Rossum85e3b011991-06-03 12:42:10 +00002910
Guido van Rossum3b066191991-06-04 19:40:25 +00002911/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002913PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002914"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002916
Barry Warsaw53699e91996-12-10 23:23:01 +00002917static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002918posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002919{
2920 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002921 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002922 return NULL;
2923 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002924 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002925}
2926
Martin v. Löwis114619e2002-10-07 06:44:21 +00002927#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2928static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002929free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002930{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002931 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002932 for (i = 0; i < count; i++)
2933 PyMem_Free(array[i]);
2934 PyMem_DEL(array);
2935}
2936#endif
2937
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002938
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002939#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002941"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002942Execute an executable path with arguments, replacing current process.\n\
2943\n\
2944 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002945 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002946
Barry Warsaw53699e91996-12-10 23:23:01 +00002947static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002948posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002949{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002950 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002951 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002952 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002953 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002954 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002955
Guido van Rossum89b33251993-10-22 14:26:06 +00002956 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002957 argv is a list or tuple of strings. */
2958
Martin v. Löwis114619e2002-10-07 06:44:21 +00002959 if (!PyArg_ParseTuple(args, "etO:execv",
2960 Py_FileSystemDefaultEncoding,
2961 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002962 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002963 if (PyList_Check(argv)) {
2964 argc = PyList_Size(argv);
2965 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002966 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002967 else if (PyTuple_Check(argv)) {
2968 argc = PyTuple_Size(argv);
2969 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002970 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002971 else {
Fred Drake661ea262000-10-24 19:57:45 +00002972 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002973 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002974 return NULL;
2975 }
Thomas Heller6790d602007-08-30 17:15:14 +00002976 if (argc < 1) {
2977 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2978 PyMem_Free(path);
2979 return NULL;
2980 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002981
Barry Warsaw53699e91996-12-10 23:23:01 +00002982 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002983 if (argvlist == NULL) {
2984 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002985 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002986 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002987 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002988 if (!PyArg_Parse((*getitem)(argv, i), "et",
2989 Py_FileSystemDefaultEncoding,
2990 &argvlist[i])) {
2991 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002992 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002993 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002994 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002995 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002996
Guido van Rossum85e3b011991-06-03 12:42:10 +00002997 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002998 }
2999 argvlist[argc] = NULL;
3000
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003001 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003002
Guido van Rossum85e3b011991-06-03 12:42:10 +00003003 /* If we get here it's definitely an error */
3004
Martin v. Löwis114619e2002-10-07 06:44:21 +00003005 free_string_array(argvlist, argc);
3006 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003007 return posix_error();
3008}
3009
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003011PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003012"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003013Execute a path with arguments and environment, replacing current process.\n\
3014\n\
3015 path: path of executable file\n\
3016 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003018
Barry Warsaw53699e91996-12-10 23:23:01 +00003019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003020posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003021{
3022 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00003023 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003024 char **argvlist;
3025 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003026 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00003027 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003028 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003029 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003030
3031 /* execve has three arguments: (path, argv, env), where
3032 argv is a list or tuple of strings and env is a dictionary
3033 like posix.environ. */
3034
Martin v. Löwis114619e2002-10-07 06:44:21 +00003035 if (!PyArg_ParseTuple(args, "etOO:execve",
3036 Py_FileSystemDefaultEncoding,
3037 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003038 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003039 if (PyList_Check(argv)) {
3040 argc = PyList_Size(argv);
3041 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003042 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003043 else if (PyTuple_Check(argv)) {
3044 argc = PyTuple_Size(argv);
3045 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003046 }
3047 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003048 PyErr_SetString(PyExc_TypeError,
3049 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003050 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003051 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003052 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003053 PyErr_SetString(PyExc_TypeError,
3054 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003055 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003056 }
3057
Barry Warsaw53699e91996-12-10 23:23:01 +00003058 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003059 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003060 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003061 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003062 }
3063 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003064 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00003065 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00003066 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00003067 &argvlist[i]))
3068 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003070 goto fail_1;
3071 }
3072 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003073 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003074 argvlist[argc] = NULL;
3075
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003076 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003077 if (i < 0)
3078 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00003079 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003080 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003081 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003082 goto fail_1;
3083 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003084 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003085 keys = PyMapping_Keys(env);
3086 vals = PyMapping_Values(env);
3087 if (!keys || !vals)
3088 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003089 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3090 PyErr_SetString(PyExc_TypeError,
3091 "execve(): env.keys() or env.values() is not a list");
3092 goto fail_2;
3093 }
Tim Peters5aa91602002-01-30 05:46:57 +00003094
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003095 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003096 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003097 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003098
3099 key = PyList_GetItem(keys, pos);
3100 val = PyList_GetItem(vals, pos);
3101 if (!key || !val)
3102 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003103
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003104 if (!PyArg_Parse(
3105 key,
3106 "s;execve() arg 3 contains a non-string key",
3107 &k) ||
3108 !PyArg_Parse(
3109 val,
3110 "s;execve() arg 3 contains a non-string value",
3111 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00003112 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003113 goto fail_2;
3114 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003115
3116#if defined(PYOS_OS2)
3117 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3118 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
3119#endif
Christian Heimes830a4bc2007-11-22 07:43:40 +00003120 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003121 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003122 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003123 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003124 goto fail_2;
3125 }
Tim Petersc8996f52001-12-03 20:41:00 +00003126 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003127 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003128#if defined(PYOS_OS2)
3129 }
3130#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003131 }
3132 envlist[envc] = 0;
3133
3134 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003135
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003136 /* If we get here it's definitely an error */
3137
3138 (void) posix_error();
3139
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003140 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003141 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003142 PyMem_DEL(envlist[envc]);
3143 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 fail_1:
3145 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003146 Py_XDECREF(vals);
3147 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003148 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003149 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003150 return NULL;
3151}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003152#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003154
Guido van Rossuma1065681999-01-25 23:20:23 +00003155#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003157"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003158Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003159\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003160 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003161 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003162 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003163
3164static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003165posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003166{
3167 char *path;
3168 PyObject *argv;
3169 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003170 int mode, i;
3171 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003172 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003173 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003174
3175 /* spawnv has three arguments: (mode, path, argv), where
3176 argv is a list or tuple of strings. */
3177
Martin v. Löwis114619e2002-10-07 06:44:21 +00003178 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3179 Py_FileSystemDefaultEncoding,
3180 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003181 return NULL;
3182 if (PyList_Check(argv)) {
3183 argc = PyList_Size(argv);
3184 getitem = PyList_GetItem;
3185 }
3186 else if (PyTuple_Check(argv)) {
3187 argc = PyTuple_Size(argv);
3188 getitem = PyTuple_GetItem;
3189 }
3190 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003191 PyErr_SetString(PyExc_TypeError,
3192 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003193 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003194 return NULL;
3195 }
3196
3197 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003198 if (argvlist == NULL) {
3199 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003200 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003201 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003202 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003203 if (!PyArg_Parse((*getitem)(argv, i), "et",
3204 Py_FileSystemDefaultEncoding,
3205 &argvlist[i])) {
3206 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003207 PyErr_SetString(
3208 PyExc_TypeError,
3209 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003210 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003211 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003212 }
3213 }
3214 argvlist[argc] = NULL;
3215
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003216#if defined(PYOS_OS2) && defined(PYCC_GCC)
3217 Py_BEGIN_ALLOW_THREADS
3218 spawnval = spawnv(mode, path, argvlist);
3219 Py_END_ALLOW_THREADS
3220#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003221 if (mode == _OLD_P_OVERLAY)
3222 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003223
Tim Peters25059d32001-12-07 20:35:43 +00003224 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003225 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003226 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003227#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003228
Martin v. Löwis114619e2002-10-07 06:44:21 +00003229 free_string_array(argvlist, argc);
3230 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003231
Fred Drake699f3522000-06-29 21:12:41 +00003232 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003233 return posix_error();
3234 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003235#if SIZEOF_LONG == SIZEOF_VOID_P
3236 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003237#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003238 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003239#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003240}
3241
3242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003243PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003244"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003245Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003246\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003247 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003248 path: path of executable file\n\
3249 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003250 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003251
3252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003253posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003254{
3255 char *path;
3256 PyObject *argv, *env;
3257 char **argvlist;
3258 char **envlist;
3259 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003260 int mode, pos, envc;
3261 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003262 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003263 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003264 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003265
3266 /* spawnve has four arguments: (mode, path, argv, env), where
3267 argv is a list or tuple of strings and env is a dictionary
3268 like posix.environ. */
3269
Martin v. Löwis114619e2002-10-07 06:44:21 +00003270 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3271 Py_FileSystemDefaultEncoding,
3272 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003273 return NULL;
3274 if (PyList_Check(argv)) {
3275 argc = PyList_Size(argv);
3276 getitem = PyList_GetItem;
3277 }
3278 else if (PyTuple_Check(argv)) {
3279 argc = PyTuple_Size(argv);
3280 getitem = PyTuple_GetItem;
3281 }
3282 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003283 PyErr_SetString(PyExc_TypeError,
3284 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003285 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003286 }
3287 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003288 PyErr_SetString(PyExc_TypeError,
3289 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003290 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003291 }
3292
3293 argvlist = PyMem_NEW(char *, argc+1);
3294 if (argvlist == NULL) {
3295 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003296 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003297 }
3298 for (i = 0; i < argc; i++) {
3299 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003300 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003301 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003302 &argvlist[i]))
3303 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003304 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003305 goto fail_1;
3306 }
3307 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003308 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003309 argvlist[argc] = NULL;
3310
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003311 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003312 if (i < 0)
3313 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003314 envlist = PyMem_NEW(char *, i + 1);
3315 if (envlist == NULL) {
3316 PyErr_NoMemory();
3317 goto fail_1;
3318 }
3319 envc = 0;
3320 keys = PyMapping_Keys(env);
3321 vals = PyMapping_Values(env);
3322 if (!keys || !vals)
3323 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003324 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3325 PyErr_SetString(PyExc_TypeError,
3326 "spawnve(): env.keys() or env.values() is not a list");
3327 goto fail_2;
3328 }
Tim Peters5aa91602002-01-30 05:46:57 +00003329
Guido van Rossuma1065681999-01-25 23:20:23 +00003330 for (pos = 0; pos < i; pos++) {
3331 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003332 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003333
3334 key = PyList_GetItem(keys, pos);
3335 val = PyList_GetItem(vals, pos);
3336 if (!key || !val)
3337 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003338
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003339 if (!PyArg_Parse(
3340 key,
3341 "s;spawnve() arg 3 contains a non-string key",
3342 &k) ||
3343 !PyArg_Parse(
3344 val,
3345 "s;spawnve() arg 3 contains a non-string value",
3346 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003347 {
3348 goto fail_2;
3349 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003350 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Tim Petersc8996f52001-12-03 20:41:00 +00003351 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003352 if (p == NULL) {
3353 PyErr_NoMemory();
3354 goto fail_2;
3355 }
Tim Petersc8996f52001-12-03 20:41:00 +00003356 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003357 envlist[envc++] = p;
3358 }
3359 envlist[envc] = 0;
3360
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003361#if defined(PYOS_OS2) && defined(PYCC_GCC)
3362 Py_BEGIN_ALLOW_THREADS
3363 spawnval = spawnve(mode, path, argvlist, envlist);
3364 Py_END_ALLOW_THREADS
3365#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003366 if (mode == _OLD_P_OVERLAY)
3367 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003368
3369 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003370 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003371 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003372#endif
Tim Peters25059d32001-12-07 20:35:43 +00003373
Fred Drake699f3522000-06-29 21:12:41 +00003374 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003375 (void) posix_error();
3376 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003377#if SIZEOF_LONG == SIZEOF_VOID_P
3378 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003379#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003380 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003381#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003382
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003383 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003384 while (--envc >= 0)
3385 PyMem_DEL(envlist[envc]);
3386 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003387 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003388 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003389 Py_XDECREF(vals);
3390 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003391 fail_0:
3392 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003393 return res;
3394}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003395
3396/* OS/2 supports spawnvp & spawnvpe natively */
3397#if defined(PYOS_OS2)
3398PyDoc_STRVAR(posix_spawnvp__doc__,
3399"spawnvp(mode, file, args)\n\n\
3400Execute the program 'file' in a new process, using the environment\n\
3401search path to find the file.\n\
3402\n\
3403 mode: mode of process creation\n\
3404 file: executable file name\n\
3405 args: tuple or list of strings");
3406
3407static PyObject *
3408posix_spawnvp(PyObject *self, PyObject *args)
3409{
3410 char *path;
3411 PyObject *argv;
3412 char **argvlist;
3413 int mode, i, argc;
3414 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003415 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003416
3417 /* spawnvp has three arguments: (mode, path, argv), where
3418 argv is a list or tuple of strings. */
3419
3420 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3421 Py_FileSystemDefaultEncoding,
3422 &path, &argv))
3423 return NULL;
3424 if (PyList_Check(argv)) {
3425 argc = PyList_Size(argv);
3426 getitem = PyList_GetItem;
3427 }
3428 else if (PyTuple_Check(argv)) {
3429 argc = PyTuple_Size(argv);
3430 getitem = PyTuple_GetItem;
3431 }
3432 else {
3433 PyErr_SetString(PyExc_TypeError,
3434 "spawnvp() arg 2 must be a tuple or list");
3435 PyMem_Free(path);
3436 return NULL;
3437 }
3438
3439 argvlist = PyMem_NEW(char *, argc+1);
3440 if (argvlist == NULL) {
3441 PyMem_Free(path);
3442 return PyErr_NoMemory();
3443 }
3444 for (i = 0; i < argc; i++) {
3445 if (!PyArg_Parse((*getitem)(argv, i), "et",
3446 Py_FileSystemDefaultEncoding,
3447 &argvlist[i])) {
3448 free_string_array(argvlist, i);
3449 PyErr_SetString(
3450 PyExc_TypeError,
3451 "spawnvp() arg 2 must contain only strings");
3452 PyMem_Free(path);
3453 return NULL;
3454 }
3455 }
3456 argvlist[argc] = NULL;
3457
3458 Py_BEGIN_ALLOW_THREADS
3459#if defined(PYCC_GCC)
3460 spawnval = spawnvp(mode, path, argvlist);
3461#else
3462 spawnval = _spawnvp(mode, path, argvlist);
3463#endif
3464 Py_END_ALLOW_THREADS
3465
3466 free_string_array(argvlist, argc);
3467 PyMem_Free(path);
3468
3469 if (spawnval == -1)
3470 return posix_error();
3471 else
3472 return Py_BuildValue("l", (long) spawnval);
3473}
3474
3475
3476PyDoc_STRVAR(posix_spawnvpe__doc__,
3477"spawnvpe(mode, file, args, env)\n\n\
3478Execute the program 'file' in a new process, using the environment\n\
3479search path to find the file.\n\
3480\n\
3481 mode: mode of process creation\n\
3482 file: executable file name\n\
3483 args: tuple or list of arguments\n\
3484 env: dictionary of strings mapping to strings");
3485
3486static PyObject *
3487posix_spawnvpe(PyObject *self, PyObject *args)
3488{
3489 char *path;
3490 PyObject *argv, *env;
3491 char **argvlist;
3492 char **envlist;
3493 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3494 int mode, i, pos, argc, envc;
3495 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003496 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003497 int lastarg = 0;
3498
3499 /* spawnvpe has four arguments: (mode, path, argv, env), where
3500 argv is a list or tuple of strings and env is a dictionary
3501 like posix.environ. */
3502
3503 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3504 Py_FileSystemDefaultEncoding,
3505 &path, &argv, &env))
3506 return NULL;
3507 if (PyList_Check(argv)) {
3508 argc = PyList_Size(argv);
3509 getitem = PyList_GetItem;
3510 }
3511 else if (PyTuple_Check(argv)) {
3512 argc = PyTuple_Size(argv);
3513 getitem = PyTuple_GetItem;
3514 }
3515 else {
3516 PyErr_SetString(PyExc_TypeError,
3517 "spawnvpe() arg 2 must be a tuple or list");
3518 goto fail_0;
3519 }
3520 if (!PyMapping_Check(env)) {
3521 PyErr_SetString(PyExc_TypeError,
3522 "spawnvpe() arg 3 must be a mapping object");
3523 goto fail_0;
3524 }
3525
3526 argvlist = PyMem_NEW(char *, argc+1);
3527 if (argvlist == NULL) {
3528 PyErr_NoMemory();
3529 goto fail_0;
3530 }
3531 for (i = 0; i < argc; i++) {
3532 if (!PyArg_Parse((*getitem)(argv, i),
3533 "et;spawnvpe() arg 2 must contain only strings",
3534 Py_FileSystemDefaultEncoding,
3535 &argvlist[i]))
3536 {
3537 lastarg = i;
3538 goto fail_1;
3539 }
3540 }
3541 lastarg = argc;
3542 argvlist[argc] = NULL;
3543
3544 i = PyMapping_Size(env);
3545 if (i < 0)
3546 goto fail_1;
3547 envlist = PyMem_NEW(char *, i + 1);
3548 if (envlist == NULL) {
3549 PyErr_NoMemory();
3550 goto fail_1;
3551 }
3552 envc = 0;
3553 keys = PyMapping_Keys(env);
3554 vals = PyMapping_Values(env);
3555 if (!keys || !vals)
3556 goto fail_2;
3557 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3558 PyErr_SetString(PyExc_TypeError,
3559 "spawnvpe(): env.keys() or env.values() is not a list");
3560 goto fail_2;
3561 }
3562
3563 for (pos = 0; pos < i; pos++) {
3564 char *p, *k, *v;
3565 size_t len;
3566
3567 key = PyList_GetItem(keys, pos);
3568 val = PyList_GetItem(vals, pos);
3569 if (!key || !val)
3570 goto fail_2;
3571
3572 if (!PyArg_Parse(
3573 key,
3574 "s;spawnvpe() arg 3 contains a non-string key",
3575 &k) ||
3576 !PyArg_Parse(
3577 val,
3578 "s;spawnvpe() arg 3 contains a non-string value",
3579 &v))
3580 {
3581 goto fail_2;
3582 }
Christian Heimes830a4bc2007-11-22 07:43:40 +00003583 len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003584 p = PyMem_NEW(char, len);
3585 if (p == NULL) {
3586 PyErr_NoMemory();
3587 goto fail_2;
3588 }
3589 PyOS_snprintf(p, len, "%s=%s", k, v);
3590 envlist[envc++] = p;
3591 }
3592 envlist[envc] = 0;
3593
3594 Py_BEGIN_ALLOW_THREADS
3595#if defined(PYCC_GCC)
Christian Heimes292d3512008-02-03 16:51:08 +00003596 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003597#else
Christian Heimes292d3512008-02-03 16:51:08 +00003598 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003599#endif
3600 Py_END_ALLOW_THREADS
3601
3602 if (spawnval == -1)
3603 (void) posix_error();
3604 else
3605 res = Py_BuildValue("l", (long) spawnval);
3606
3607 fail_2:
3608 while (--envc >= 0)
3609 PyMem_DEL(envlist[envc]);
3610 PyMem_DEL(envlist);
3611 fail_1:
3612 free_string_array(argvlist, lastarg);
3613 Py_XDECREF(vals);
3614 Py_XDECREF(keys);
3615 fail_0:
3616 PyMem_Free(path);
3617 return res;
3618}
3619#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003620#endif /* HAVE_SPAWNV */
3621
3622
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003623#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003624PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003625"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003626Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3627\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003628Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003629
3630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003631posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003632{
Christian Heimes400adb02008-02-01 08:12:03 +00003633 pid_t pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003634 if (pid == -1)
3635 return posix_error();
Georg Brandl2ee470f2008-07-16 12:55:28 +00003636 if (pid == 0)
3637 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003638 return PyLong_FromLong(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003639}
3640#endif
3641
3642
Guido van Rossumad0ee831995-03-01 10:34:45 +00003643#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003644PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003645"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003646Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003647Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003648
Barry Warsaw53699e91996-12-10 23:23:01 +00003649static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003650posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003651{
Christian Heimes400adb02008-02-01 08:12:03 +00003652 pid_t pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003653 if (pid == -1)
3654 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003655 if (pid == 0)
3656 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003657 return PyLong_FromLong(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003658}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003659#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003660
Neal Norwitzb59798b2003-03-21 01:43:31 +00003661/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003662/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3663#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003664#define DEV_PTY_FILE "/dev/ptc"
3665#define HAVE_DEV_PTMX
3666#else
3667#define DEV_PTY_FILE "/dev/ptmx"
3668#endif
3669
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003670#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003671#ifdef HAVE_PTY_H
3672#include <pty.h>
3673#else
3674#ifdef HAVE_LIBUTIL_H
3675#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003676#endif /* HAVE_LIBUTIL_H */
3677#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003678#ifdef HAVE_STROPTS_H
3679#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003680#endif
3681#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003682
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003683#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003684PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003685"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003686Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003687
3688static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003689posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003690{
3691 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003692#ifndef HAVE_OPENPTY
3693 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003694#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003695#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003696 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003697#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003698 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003699#endif
3700#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003701
Thomas Wouters70c21a12000-07-14 14:28:33 +00003702#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003703 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3704 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003705#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003706 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3707 if (slave_name == NULL)
3708 return posix_error();
3709
3710 slave_fd = open(slave_name, O_RDWR);
3711 if (slave_fd < 0)
3712 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003713#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003714 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003715 if (master_fd < 0)
3716 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003717 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003718 /* change permission of slave */
3719 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003720 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003721 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003722 }
3723 /* unlock slave */
3724 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003725 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003726 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003727 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003728 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003729 slave_name = ptsname(master_fd); /* get name of slave */
3730 if (slave_name == NULL)
3731 return posix_error();
3732 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3733 if (slave_fd < 0)
3734 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003735#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003736 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3737 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003738#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003739 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003740#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003741#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003742#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003743
Fred Drake8cef4cf2000-06-28 16:40:38 +00003744 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003745
Fred Drake8cef4cf2000-06-28 16:40:38 +00003746}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003747#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003748
3749#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003750PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003751"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003752Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3753Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003754To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003755
3756static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003757posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003758{
Christian Heimes400adb02008-02-01 08:12:03 +00003759 int master_fd = -1;
3760 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003761
Fred Drake8cef4cf2000-06-28 16:40:38 +00003762 pid = forkpty(&master_fd, NULL, NULL, NULL);
3763 if (pid == -1)
3764 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003765 if (pid == 0)
3766 PyOS_AfterFork();
Christian Heimes400adb02008-02-01 08:12:03 +00003767 return Py_BuildValue("(li)", pid, master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003768}
3769#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003770
Guido van Rossumad0ee831995-03-01 10:34:45 +00003771#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003772PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003773"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003774Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003775
Barry Warsaw53699e91996-12-10 23:23:01 +00003776static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003777posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003778{
Christian Heimes217cfd12007-12-02 14:31:20 +00003779 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003780}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003781#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003782
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003783
Guido van Rossumad0ee831995-03-01 10:34:45 +00003784#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003785PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003786"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003788
Barry Warsaw53699e91996-12-10 23:23:01 +00003789static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003790posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003791{
Christian Heimes217cfd12007-12-02 14:31:20 +00003792 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003793}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003794#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003796
Guido van Rossumad0ee831995-03-01 10:34:45 +00003797#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003798PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003799"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003800Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003801
Barry Warsaw53699e91996-12-10 23:23:01 +00003802static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003803posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003804{
Christian Heimes217cfd12007-12-02 14:31:20 +00003805 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003806}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003807#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003808
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003810PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003811"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003812Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003813
Barry Warsaw53699e91996-12-10 23:23:01 +00003814static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003815posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003816{
Christian Heimes217cfd12007-12-02 14:31:20 +00003817 return PyLong_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003818}
3819
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003820
Fred Drakec9680921999-12-13 16:37:25 +00003821#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003822PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003823"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003825
3826static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003827posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003828{
3829 PyObject *result = NULL;
3830
Fred Drakec9680921999-12-13 16:37:25 +00003831#ifdef NGROUPS_MAX
3832#define MAX_GROUPS NGROUPS_MAX
3833#else
3834 /* defined to be 16 on Solaris7, so this should be a small number */
3835#define MAX_GROUPS 64
3836#endif
3837 gid_t grouplist[MAX_GROUPS];
3838 int n;
3839
3840 n = getgroups(MAX_GROUPS, grouplist);
3841 if (n < 0)
3842 posix_error();
3843 else {
3844 result = PyList_New(n);
3845 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003846 int i;
3847 for (i = 0; i < n; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003848 PyObject *o = PyLong_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003849 if (o == NULL) {
3850 Py_DECREF(result);
3851 result = NULL;
3852 break;
3853 }
3854 PyList_SET_ITEM(result, i, o);
3855 }
3856 }
3857 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003858
Fred Drakec9680921999-12-13 16:37:25 +00003859 return result;
3860}
3861#endif
3862
Martin v. Löwis606edc12002-06-13 21:09:11 +00003863#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003864PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003865"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003866Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003867
3868static PyObject *
3869posix_getpgid(PyObject *self, PyObject *args)
3870{
3871 int pid, pgid;
3872 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3873 return NULL;
3874 pgid = getpgid(pid);
3875 if (pgid < 0)
3876 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00003877 return PyLong_FromLong((long)pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003878}
3879#endif /* HAVE_GETPGID */
3880
3881
Guido van Rossumb6775db1994-08-01 11:34:53 +00003882#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003883PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003884"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003885Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003886
Barry Warsaw53699e91996-12-10 23:23:01 +00003887static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003888posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003889{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003890#ifdef GETPGRP_HAVE_ARG
Christian Heimes217cfd12007-12-02 14:31:20 +00003891 return PyLong_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003892#else /* GETPGRP_HAVE_ARG */
Christian Heimes217cfd12007-12-02 14:31:20 +00003893 return PyLong_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003894#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003895}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003896#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003898
Guido van Rossumb6775db1994-08-01 11:34:53 +00003899#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003900PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003901"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003902Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003903
Barry Warsaw53699e91996-12-10 23:23:01 +00003904static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003905posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003906{
Guido van Rossum64933891994-10-20 21:56:42 +00003907#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003908 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003909#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003910 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003911#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003912 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003913 Py_INCREF(Py_None);
3914 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003915}
3916
Guido van Rossumb6775db1994-08-01 11:34:53 +00003917#endif /* HAVE_SETPGRP */
3918
Guido van Rossumad0ee831995-03-01 10:34:45 +00003919#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003920PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003921"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003922Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003923
Barry Warsaw53699e91996-12-10 23:23:01 +00003924static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003925posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003926{
Christian Heimes217cfd12007-12-02 14:31:20 +00003927 return PyLong_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003928}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003929#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003930
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003931
Fred Drake12c6e2d1999-12-14 21:25:03 +00003932#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003933PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003934"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003935Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003936
3937static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003938posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003939{
Neal Norwitze241ce82003-02-17 18:17:05 +00003940 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003941 char *name;
3942 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003943
Fred Drakea30680b2000-12-06 21:24:28 +00003944 errno = 0;
3945 name = getlogin();
3946 if (name == NULL) {
3947 if (errno)
3948 posix_error();
3949 else
3950 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003951 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003952 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003953 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003954 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003955 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003956
Fred Drake12c6e2d1999-12-14 21:25:03 +00003957 return result;
3958}
3959#endif
3960
Guido van Rossumad0ee831995-03-01 10:34:45 +00003961#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003962PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003963"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003964Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003965
Barry Warsaw53699e91996-12-10 23:23:01 +00003966static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003967posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003968{
Christian Heimes217cfd12007-12-02 14:31:20 +00003969 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003970}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003971#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003972
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003973
Guido van Rossumad0ee831995-03-01 10:34:45 +00003974#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003975PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003976"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003977Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003978
Barry Warsaw53699e91996-12-10 23:23:01 +00003979static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003980posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003981{
Christian Heimes292d3512008-02-03 16:51:08 +00003982 pid_t pid;
3983 int sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003984 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003985 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003986#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003987 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3988 APIRET rc;
3989 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003990 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003991
3992 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3993 APIRET rc;
3994 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003995 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003996
3997 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003998 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003999#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00004000 if (kill(pid, sig) == -1)
4001 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004002#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004003 Py_INCREF(Py_None);
4004 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004005}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004006#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004007
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004008#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004009PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004010"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004011Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004012
4013static PyObject *
4014posix_killpg(PyObject *self, PyObject *args)
4015{
4016 int pgid, sig;
4017 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
4018 return NULL;
4019 if (killpg(pgid, sig) == -1)
4020 return posix_error();
4021 Py_INCREF(Py_None);
4022 return Py_None;
4023}
4024#endif
4025
Guido van Rossumc0125471996-06-28 18:55:32 +00004026#ifdef HAVE_PLOCK
4027
4028#ifdef HAVE_SYS_LOCK_H
4029#include <sys/lock.h>
4030#endif
4031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004032PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004033"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004034Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004035
Barry Warsaw53699e91996-12-10 23:23:01 +00004036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004037posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004038{
4039 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004040 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00004041 return NULL;
4042 if (plock(op) == -1)
4043 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004044 Py_INCREF(Py_None);
4045 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004046}
4047#endif
4048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004049
Guido van Rossum3b066191991-06-04 19:40:25 +00004050
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004051
Guido van Rossumb6775db1994-08-01 11:34:53 +00004052#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004053PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004054"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004055Set the current process's user id.");
4056
Barry Warsaw53699e91996-12-10 23:23:01 +00004057static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004058posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004059{
4060 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004061 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004062 return NULL;
4063 if (setuid(uid) < 0)
4064 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004065 Py_INCREF(Py_None);
4066 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004067}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004068#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004069
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004070
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004071#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004072PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004073"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004074Set the current process's effective user id.");
4075
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004076static PyObject *
4077posix_seteuid (PyObject *self, PyObject *args)
4078{
4079 int euid;
4080 if (!PyArg_ParseTuple(args, "i", &euid)) {
4081 return NULL;
4082 } else if (seteuid(euid) < 0) {
4083 return posix_error();
4084 } else {
4085 Py_INCREF(Py_None);
4086 return Py_None;
4087 }
4088}
4089#endif /* HAVE_SETEUID */
4090
4091#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004092PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004093"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004094Set the current process's effective group id.");
4095
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004096static PyObject *
4097posix_setegid (PyObject *self, PyObject *args)
4098{
4099 int egid;
4100 if (!PyArg_ParseTuple(args, "i", &egid)) {
4101 return NULL;
4102 } else if (setegid(egid) < 0) {
4103 return posix_error();
4104 } else {
4105 Py_INCREF(Py_None);
4106 return Py_None;
4107 }
4108}
4109#endif /* HAVE_SETEGID */
4110
4111#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004112PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004113"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004114Set the current process's real and effective user ids.");
4115
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004116static PyObject *
4117posix_setreuid (PyObject *self, PyObject *args)
4118{
4119 int ruid, euid;
4120 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4121 return NULL;
4122 } else if (setreuid(ruid, euid) < 0) {
4123 return posix_error();
4124 } else {
4125 Py_INCREF(Py_None);
4126 return Py_None;
4127 }
4128}
4129#endif /* HAVE_SETREUID */
4130
4131#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004132PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004133"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004134Set the current process's real and effective group ids.");
4135
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004136static PyObject *
4137posix_setregid (PyObject *self, PyObject *args)
4138{
4139 int rgid, egid;
4140 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4141 return NULL;
4142 } else if (setregid(rgid, egid) < 0) {
4143 return posix_error();
4144 } else {
4145 Py_INCREF(Py_None);
4146 return Py_None;
4147 }
4148}
4149#endif /* HAVE_SETREGID */
4150
Guido van Rossumb6775db1994-08-01 11:34:53 +00004151#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004152PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004153"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004154Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004155
Barry Warsaw53699e91996-12-10 23:23:01 +00004156static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004157posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004158{
4159 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004160 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004161 return NULL;
4162 if (setgid(gid) < 0)
4163 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004164 Py_INCREF(Py_None);
4165 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004166}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004167#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004168
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004169#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004170PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004171"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004172Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004173
4174static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004175posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004176{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004177 int i, len;
4178 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004179
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004180 if (!PySequence_Check(groups)) {
4181 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4182 return NULL;
4183 }
4184 len = PySequence_Size(groups);
4185 if (len > MAX_GROUPS) {
4186 PyErr_SetString(PyExc_ValueError, "too many groups");
4187 return NULL;
4188 }
4189 for(i = 0; i < len; i++) {
4190 PyObject *elem;
4191 elem = PySequence_GetItem(groups, i);
4192 if (!elem)
4193 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004194 if (!PyLong_Check(elem)) {
4195 PyErr_SetString(PyExc_TypeError,
4196 "groups must be integers");
4197 Py_DECREF(elem);
4198 return NULL;
4199 } else {
4200 unsigned long x = PyLong_AsUnsignedLong(elem);
4201 if (PyErr_Occurred()) {
4202 PyErr_SetString(PyExc_TypeError,
4203 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004204 Py_DECREF(elem);
4205 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004206 }
Georg Brandla13c2442005-11-22 19:30:31 +00004207 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004208 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004209 if (grouplist[i] != x) {
4210 PyErr_SetString(PyExc_TypeError,
4211 "group id too big");
4212 Py_DECREF(elem);
4213 return NULL;
4214 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004215 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004216 Py_DECREF(elem);
4217 }
4218
4219 if (setgroups(len, grouplist) < 0)
4220 return posix_error();
4221 Py_INCREF(Py_None);
4222 return Py_None;
4223}
4224#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004225
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004226#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4227static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004228wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004229{
4230 PyObject *result;
4231 static PyObject *struct_rusage;
4232
4233 if (pid == -1)
4234 return posix_error();
4235
4236 if (struct_rusage == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +00004237 PyObject *m = PyImport_ImportModuleNoBlock("resource");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004238 if (m == NULL)
4239 return NULL;
4240 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4241 Py_DECREF(m);
4242 if (struct_rusage == NULL)
4243 return NULL;
4244 }
4245
4246 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4247 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4248 if (!result)
4249 return NULL;
4250
4251#ifndef doubletime
4252#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4253#endif
4254
4255 PyStructSequence_SET_ITEM(result, 0,
4256 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4257 PyStructSequence_SET_ITEM(result, 1,
4258 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4259#define SET_INT(result, index, value)\
Christian Heimes217cfd12007-12-02 14:31:20 +00004260 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004261 SET_INT(result, 2, ru->ru_maxrss);
4262 SET_INT(result, 3, ru->ru_ixrss);
4263 SET_INT(result, 4, ru->ru_idrss);
4264 SET_INT(result, 5, ru->ru_isrss);
4265 SET_INT(result, 6, ru->ru_minflt);
4266 SET_INT(result, 7, ru->ru_majflt);
4267 SET_INT(result, 8, ru->ru_nswap);
4268 SET_INT(result, 9, ru->ru_inblock);
4269 SET_INT(result, 10, ru->ru_oublock);
4270 SET_INT(result, 11, ru->ru_msgsnd);
4271 SET_INT(result, 12, ru->ru_msgrcv);
4272 SET_INT(result, 13, ru->ru_nsignals);
4273 SET_INT(result, 14, ru->ru_nvcsw);
4274 SET_INT(result, 15, ru->ru_nivcsw);
4275#undef SET_INT
4276
4277 if (PyErr_Occurred()) {
4278 Py_DECREF(result);
4279 return NULL;
4280 }
4281
4282 return Py_BuildValue("iiN", pid, status, result);
4283}
4284#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4285
4286#ifdef HAVE_WAIT3
4287PyDoc_STRVAR(posix_wait3__doc__,
4288"wait3(options) -> (pid, status, rusage)\n\n\
4289Wait for completion of a child process.");
4290
4291static PyObject *
4292posix_wait3(PyObject *self, PyObject *args)
4293{
Christian Heimes292d3512008-02-03 16:51:08 +00004294 pid_t pid;
4295 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004296 struct rusage ru;
4297 WAIT_TYPE status;
4298 WAIT_STATUS_INT(status) = 0;
4299
4300 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4301 return NULL;
4302
4303 Py_BEGIN_ALLOW_THREADS
4304 pid = wait3(&status, options, &ru);
4305 Py_END_ALLOW_THREADS
4306
4307 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4308}
4309#endif /* HAVE_WAIT3 */
4310
4311#ifdef HAVE_WAIT4
4312PyDoc_STRVAR(posix_wait4__doc__,
4313"wait4(pid, options) -> (pid, status, rusage)\n\n\
4314Wait for completion of a given child process.");
4315
4316static PyObject *
4317posix_wait4(PyObject *self, PyObject *args)
4318{
Christian Heimes292d3512008-02-03 16:51:08 +00004319 pid_t pid;
4320 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004321 struct rusage ru;
4322 WAIT_TYPE status;
4323 WAIT_STATUS_INT(status) = 0;
4324
4325 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4326 return NULL;
4327
4328 Py_BEGIN_ALLOW_THREADS
4329 pid = wait4(pid, &status, options, &ru);
4330 Py_END_ALLOW_THREADS
4331
4332 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4333}
4334#endif /* HAVE_WAIT4 */
4335
Guido van Rossumb6775db1994-08-01 11:34:53 +00004336#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004337PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004338"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004339Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004340
Barry Warsaw53699e91996-12-10 23:23:01 +00004341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004342posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004343{
Christian Heimes292d3512008-02-03 16:51:08 +00004344 pid_t pid;
4345 int options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004346 WAIT_TYPE status;
4347 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004348
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004349 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004350 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004351 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004352 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004353 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004354 if (pid == -1)
4355 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004356
4357 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004358}
4359
Tim Petersab034fa2002-02-01 11:27:43 +00004360#elif defined(HAVE_CWAIT)
4361
4362/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004363PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004364"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004365"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004366
4367static PyObject *
4368posix_waitpid(PyObject *self, PyObject *args)
4369{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004370 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004371 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004372
4373 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4374 return NULL;
4375 Py_BEGIN_ALLOW_THREADS
4376 pid = _cwait(&status, pid, options);
4377 Py_END_ALLOW_THREADS
4378 if (pid == -1)
4379 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004380
4381 /* shift the status left a byte so this is more like the POSIX waitpid */
4382 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004383}
4384#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004385
Guido van Rossumad0ee831995-03-01 10:34:45 +00004386#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004387PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004388"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004389Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004390
Barry Warsaw53699e91996-12-10 23:23:01 +00004391static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004392posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004393{
Christian Heimes292d3512008-02-03 16:51:08 +00004394 pid_t pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004395 WAIT_TYPE status;
4396 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004397
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004398 Py_BEGIN_ALLOW_THREADS
4399 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004400 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004401 if (pid == -1)
4402 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004403
4404 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004405}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004406#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004407
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004409PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004410"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004411Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004412
Barry Warsaw53699e91996-12-10 23:23:01 +00004413static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004414posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004415{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004416#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004417 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004418#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004419#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004420 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004421#else
4422 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4423#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004424#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004425}
4426
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004427
Guido van Rossumb6775db1994-08-01 11:34:53 +00004428#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004429PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004430"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004432
Barry Warsaw53699e91996-12-10 23:23:01 +00004433static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004434posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004435{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004436 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004437 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004438 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004439 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004440 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004441
4442 if (!PyArg_ParseTuple(args, "et:readlink",
4443 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004444 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004445 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004446 if (v == NULL) {
4447 PyMem_Free(path);
4448 return NULL;
4449 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004450
4451 if (PyUnicode_Check(v)) {
4452 arg_is_unicode = 1;
4453 }
4454 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004455
Barry Warsaw53699e91996-12-10 23:23:01 +00004456 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004457 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004458 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004459 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004460 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004461
Neal Norwitzfca70052007-08-12 16:56:02 +00004462 PyMem_Free(path);
Christian Heimes72b710a2008-05-26 13:28:38 +00004463 v = PyBytes_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004464 if (arg_is_unicode) {
4465 PyObject *w;
4466
4467 w = PyUnicode_FromEncodedObject(v,
4468 Py_FileSystemDefaultEncoding,
4469 "strict");
4470 if (w != NULL) {
4471 Py_DECREF(v);
4472 v = w;
4473 }
4474 else {
Guido van Rossumf0af3e32008-10-02 18:55:37 +00004475 v = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004476 }
4477 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004478 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004479}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004480#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004481
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004482
Guido van Rossumb6775db1994-08-01 11:34:53 +00004483#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004484PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004485"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004486Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004487
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004488static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004489posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004490{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004491 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004492}
4493#endif /* HAVE_SYMLINK */
4494
4495
4496#ifdef HAVE_TIMES
4497#ifndef HZ
4498#define HZ 60 /* Universal constant :-) */
4499#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004500
Guido van Rossumd48f2521997-12-05 22:19:34 +00004501#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4502static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004503system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004504{
4505 ULONG value = 0;
4506
4507 Py_BEGIN_ALLOW_THREADS
4508 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4509 Py_END_ALLOW_THREADS
4510
4511 return value;
4512}
4513
4514static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004515posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004516{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004517 /* Currently Only Uptime is Provided -- Others Later */
4518 return Py_BuildValue("ddddd",
4519 (double)0 /* t.tms_utime / HZ */,
4520 (double)0 /* t.tms_stime / HZ */,
4521 (double)0 /* t.tms_cutime / HZ */,
4522 (double)0 /* t.tms_cstime / HZ */,
4523 (double)system_uptime() / 1000);
4524}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004525#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004526static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004527posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004528{
4529 struct tms t;
4530 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004531 errno = 0;
4532 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004533 if (c == (clock_t) -1)
4534 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004535 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004536 (double)t.tms_utime / HZ,
4537 (double)t.tms_stime / HZ,
4538 (double)t.tms_cutime / HZ,
4539 (double)t.tms_cstime / HZ,
4540 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004541}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004542#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004543#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004544
4545
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004546#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004547#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004548static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004549posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004550{
4551 FILETIME create, exit, kernel, user;
4552 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004553 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004554 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4555 /* The fields of a FILETIME structure are the hi and lo part
4556 of a 64-bit value expressed in 100 nanosecond units.
4557 1e7 is one second in such units; 1e-7 the inverse.
4558 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4559 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004560 return Py_BuildValue(
4561 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004562 (double)(user.dwHighDateTime*429.4967296 +
4563 user.dwLowDateTime*1e-7),
Christian Heimes68f5fbe2008-02-14 08:27:37 +00004564 (double)(kernel.dwHighDateTime*429.4967296 +
4565 kernel.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004566 (double)0,
4567 (double)0,
4568 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004569}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004570#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004571
4572#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004573PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004574"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004575Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004576#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004577
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004578
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004579#ifdef HAVE_GETSID
4580PyDoc_STRVAR(posix_getsid__doc__,
4581"getsid(pid) -> sid\n\n\
4582Call the system call getsid().");
4583
4584static PyObject *
4585posix_getsid(PyObject *self, PyObject *args)
4586{
Christian Heimes292d3512008-02-03 16:51:08 +00004587 pid_t pid;
4588 int sid;
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004589 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4590 return NULL;
4591 sid = getsid(pid);
4592 if (sid < 0)
4593 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004594 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004595}
4596#endif /* HAVE_GETSID */
4597
4598
Guido van Rossumb6775db1994-08-01 11:34:53 +00004599#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004600PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004601"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004603
Barry Warsaw53699e91996-12-10 23:23:01 +00004604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004605posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004606{
Guido van Rossum687dd131993-05-17 08:34:16 +00004607 if (setsid() < 0)
4608 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004609 Py_INCREF(Py_None);
4610 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004611}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004612#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004613
Guido van Rossumb6775db1994-08-01 11:34:53 +00004614#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004615PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004616"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004617Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004618
Barry Warsaw53699e91996-12-10 23:23:01 +00004619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004620posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004621{
Christian Heimes292d3512008-02-03 16:51:08 +00004622 pid_t pid;
4623 int pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004624 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004625 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004626 if (setpgid(pid, pgrp) < 0)
4627 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004628 Py_INCREF(Py_None);
4629 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004630}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004631#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004633
Guido van Rossumb6775db1994-08-01 11:34:53 +00004634#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004635PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004636"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004637Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004638
Barry Warsaw53699e91996-12-10 23:23:01 +00004639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004640posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004641{
Christian Heimes15ebc882008-02-04 18:48:49 +00004642 int fd;
4643 pid_t pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004644 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004645 return NULL;
4646 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004647 if (pgid < 0)
4648 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004649 return PyLong_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004650}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004651#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004652
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004653
Guido van Rossumb6775db1994-08-01 11:34:53 +00004654#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004655PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004656"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004657Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004658
Barry Warsaw53699e91996-12-10 23:23:01 +00004659static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004660posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004661{
4662 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004663 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004664 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004665 if (tcsetpgrp(fd, pgid) < 0)
4666 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004667 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004668 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004669}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004670#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004671
Guido van Rossum687dd131993-05-17 08:34:16 +00004672/* Functions acting on file descriptors */
4673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004674PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004675"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004676Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004677
Barry Warsaw53699e91996-12-10 23:23:01 +00004678static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004679posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004680{
Mark Hammondef8b6542001-05-13 08:04:26 +00004681 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004682 int flag;
4683 int mode = 0777;
4684 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004685
4686#ifdef MS_WINDOWS
4687 if (unicode_file_names()) {
4688 PyUnicodeObject *po;
4689 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4690 Py_BEGIN_ALLOW_THREADS
4691 /* PyUnicode_AS_UNICODE OK without thread
4692 lock as it is a simple dereference. */
4693 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4694 Py_END_ALLOW_THREADS
4695 if (fd < 0)
4696 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004697 return PyLong_FromLong((long)fd);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004698 }
4699 /* Drop the argument parsing error as narrow strings
4700 are also valid. */
4701 PyErr_Clear();
4702 }
4703#endif
4704
Tim Peters5aa91602002-01-30 05:46:57 +00004705 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004706 Py_FileSystemDefaultEncoding, &file,
4707 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004708 return NULL;
4709
Barry Warsaw53699e91996-12-10 23:23:01 +00004710 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004711 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004712 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004713 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004714 return posix_error_with_allocated_filename(file);
4715 PyMem_Free(file);
Christian Heimes217cfd12007-12-02 14:31:20 +00004716 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004717}
4718
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004720PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004721"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004722Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004723
Barry Warsaw53699e91996-12-10 23:23:01 +00004724static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004725posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004726{
4727 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004728 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004729 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004730 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004731 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004732 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004733 if (res < 0)
4734 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004735 Py_INCREF(Py_None);
4736 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004737}
4738
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004739
Christian Heimesfdab48e2008-01-20 09:06:41 +00004740PyDoc_STRVAR(posix_closerange__doc__,
4741"closerange(fd_low, fd_high)\n\n\
4742Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4743
4744static PyObject *
4745posix_closerange(PyObject *self, PyObject *args)
4746{
4747 int fd_from, fd_to, i;
4748 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4749 return NULL;
4750 Py_BEGIN_ALLOW_THREADS
4751 for (i = fd_from; i < fd_to; i++)
4752 close(i);
4753 Py_END_ALLOW_THREADS
4754 Py_RETURN_NONE;
4755}
4756
4757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004758PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004759"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004760Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004761
Barry Warsaw53699e91996-12-10 23:23:01 +00004762static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004763posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004764{
4765 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004766 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004767 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004768 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004769 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004770 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004771 if (fd < 0)
4772 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004773 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004774}
4775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004778"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004779Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004780
Barry Warsaw53699e91996-12-10 23:23:01 +00004781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004782posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004783{
4784 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004785 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004786 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004787 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004788 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004789 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004790 if (res < 0)
4791 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004792 Py_INCREF(Py_None);
4793 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004794}
4795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004797PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004798"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004799Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004800
Barry Warsaw53699e91996-12-10 23:23:01 +00004801static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004802posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004803{
4804 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004805#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004806 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004807#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004808 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004809#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004810 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004811 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004812 return NULL;
4813#ifdef SEEK_SET
4814 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4815 switch (how) {
4816 case 0: how = SEEK_SET; break;
4817 case 1: how = SEEK_CUR; break;
4818 case 2: how = SEEK_END; break;
4819 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004820#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004821
4822#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004823 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004824#else
4825 pos = PyLong_Check(posobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00004826 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004827#endif
4828 if (PyErr_Occurred())
4829 return NULL;
4830
Barry Warsaw53699e91996-12-10 23:23:01 +00004831 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004832#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004833 res = _lseeki64(fd, pos, how);
4834#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004835 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004836#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004837 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004838 if (res < 0)
4839 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004840
4841#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00004842 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004843#else
4844 return PyLong_FromLongLong(res);
4845#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004846}
4847
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004849PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004850"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004851Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004852
Barry Warsaw53699e91996-12-10 23:23:01 +00004853static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004854posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004855{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004856 int fd, size;
4857 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004858 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004859 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004860 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004861 if (size < 0) {
4862 errno = EINVAL;
4863 return posix_error();
4864 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004865 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004866 if (buffer == NULL)
4867 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004868 Py_BEGIN_ALLOW_THREADS
Christian Heimes72b710a2008-05-26 13:28:38 +00004869 n = read(fd, PyBytes_AS_STRING(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004870 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004871 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004872 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004873 return posix_error();
4874 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004875 if (n != size)
Christian Heimes72b710a2008-05-26 13:28:38 +00004876 _PyBytes_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004877 return buffer;
4878}
4879
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004880
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004881PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004882"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004883Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004884
Barry Warsaw53699e91996-12-10 23:23:01 +00004885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004886posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004887{
Martin v. Löwis423be952008-08-13 15:53:07 +00004888 Py_buffer pbuf;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004889 int fd;
4890 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004891
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +00004892 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
Guido van Rossum687dd131993-05-17 08:34:16 +00004893 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004894 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00004895 size = write(fd, pbuf.buf, (size_t)pbuf.len);
Barry Warsaw53699e91996-12-10 23:23:01 +00004896 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00004897 PyBuffer_Release(&pbuf);
Guido van Rossum687dd131993-05-17 08:34:16 +00004898 if (size < 0)
4899 return posix_error();
Christian Heimes217cfd12007-12-02 14:31:20 +00004900 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004901}
4902
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004904PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004905"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004906Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004907
Barry Warsaw53699e91996-12-10 23:23:01 +00004908static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004909posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004910{
4911 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004912 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004913 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004914 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004915 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004916#ifdef __VMS
4917 /* on OpenVMS we must ensure that all bytes are written to the file */
4918 fsync(fd);
4919#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004920 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004921 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004922 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004923 if (res != 0) {
4924#ifdef MS_WINDOWS
4925 return win32_error("fstat", NULL);
4926#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004927 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004928#endif
4929 }
Tim Peters5aa91602002-01-30 05:46:57 +00004930
Martin v. Löwis14694662006-02-03 12:54:16 +00004931 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004932}
4933
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004934PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004935"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004936Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004937connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004938
4939static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004940posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004941{
4942 int fd;
4943 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4944 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004945 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004946}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004947
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004948#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004949PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004950"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004951Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004952
Barry Warsaw53699e91996-12-10 23:23:01 +00004953static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004954posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004955{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004956#if defined(PYOS_OS2)
4957 HFILE read, write;
4958 APIRET rc;
4959
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004960 Py_BEGIN_ALLOW_THREADS
4961 rc = DosCreatePipe( &read, &write, 4096);
4962 Py_END_ALLOW_THREADS
4963 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004964 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004965
4966 return Py_BuildValue("(ii)", read, write);
4967#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004968#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004969 int fds[2];
4970 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004971 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004972 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004973 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004974 if (res != 0)
4975 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004976 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004977#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004978 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004979 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004980 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004981 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004982 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004983 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004984 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004985 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004986 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4987 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004988 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004989#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004990#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004991}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004992#endif /* HAVE_PIPE */
4993
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004994
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004995#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004996PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004997"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004998Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004999
Barry Warsaw53699e91996-12-10 23:23:01 +00005000static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005001posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005002{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005003 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005004 int mode = 0666;
5005 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005006 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005007 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005008 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005009 res = mkfifo(filename, mode);
5010 Py_END_ALLOW_THREADS
5011 if (res < 0)
5012 return posix_error();
5013 Py_INCREF(Py_None);
5014 return Py_None;
5015}
5016#endif
5017
5018
Neal Norwitz11690112002-07-30 01:08:28 +00005019#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005020PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005021"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005022Create a filesystem node (file, device special file or named pipe)\n\
5023named filename. mode specifies both the permissions to use and the\n\
5024type of node to be created, being combined (bitwise OR) with one of\n\
5025S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005026device defines the newly created device special file (probably using\n\
5027os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005028
5029
5030static PyObject *
5031posix_mknod(PyObject *self, PyObject *args)
5032{
5033 char *filename;
5034 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005035 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005036 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005037 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005038 return NULL;
5039 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005040 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005041 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005042 if (res < 0)
5043 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005044 Py_INCREF(Py_None);
5045 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005046}
5047#endif
5048
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005049#ifdef HAVE_DEVICE_MACROS
5050PyDoc_STRVAR(posix_major__doc__,
5051"major(device) -> major number\n\
5052Extracts a device major number from a raw device number.");
5053
5054static PyObject *
5055posix_major(PyObject *self, PyObject *args)
5056{
5057 int device;
5058 if (!PyArg_ParseTuple(args, "i:major", &device))
5059 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005060 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005061}
5062
5063PyDoc_STRVAR(posix_minor__doc__,
5064"minor(device) -> minor number\n\
5065Extracts a device minor number from a raw device number.");
5066
5067static PyObject *
5068posix_minor(PyObject *self, PyObject *args)
5069{
5070 int device;
5071 if (!PyArg_ParseTuple(args, "i:minor", &device))
5072 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005073 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005074}
5075
5076PyDoc_STRVAR(posix_makedev__doc__,
5077"makedev(major, minor) -> device number\n\
5078Composes a raw device number from the major and minor device numbers.");
5079
5080static PyObject *
5081posix_makedev(PyObject *self, PyObject *args)
5082{
5083 int major, minor;
5084 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5085 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00005086 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005087}
5088#endif /* device macros */
5089
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005090
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005091#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005092PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005093"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005094Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005095
Barry Warsaw53699e91996-12-10 23:23:01 +00005096static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005097posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005098{
5099 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005100 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005101 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005102 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005103
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005104 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005105 return NULL;
5106
5107#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005108 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005109#else
5110 length = PyLong_Check(lenobj) ?
Christian Heimes217cfd12007-12-02 14:31:20 +00005111 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005112#endif
5113 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005114 return NULL;
5115
Barry Warsaw53699e91996-12-10 23:23:01 +00005116 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005117 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005118 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005119 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005120 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005121 return NULL;
5122 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005123 Py_INCREF(Py_None);
5124 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005125}
5126#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005127
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005128#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005129PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005130"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005131Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005132
Fred Drake762e2061999-08-26 17:23:54 +00005133/* Save putenv() parameters as values here, so we can collect them when they
5134 * get re-set with another call for the same key. */
5135static PyObject *posix_putenv_garbage;
5136
Tim Peters5aa91602002-01-30 05:46:57 +00005137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005138posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005139{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005140#ifdef MS_WINDOWS
5141 wchar_t *s1, *s2;
5142 wchar_t *newenv;
5143#else
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005144 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005145 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005146#endif
Fred Drake762e2061999-08-26 17:23:54 +00005147 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005148 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005149
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005150 if (!PyArg_ParseTuple(args,
5151#ifdef MS_WINDOWS
5152 "uu:putenv",
5153#else
5154 "ss:putenv",
5155#endif
5156 &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005157 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005158
5159#if defined(PYOS_OS2)
5160 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5161 APIRET rc;
5162
Guido van Rossumd48f2521997-12-05 22:19:34 +00005163 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5164 if (rc != NO_ERROR)
5165 return os2_error(rc);
5166
5167 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5168 APIRET rc;
5169
Guido van Rossumd48f2521997-12-05 22:19:34 +00005170 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5171 if (rc != NO_ERROR)
5172 return os2_error(rc);
5173 } else {
5174#endif
Fred Drake762e2061999-08-26 17:23:54 +00005175 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005176 /* len includes space for a trailing \0; the size arg to
Christian Heimes72b710a2008-05-26 13:28:38 +00005177 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005178#ifdef MS_WINDOWS
5179 len = wcslen(s1) + wcslen(s2) + 2;
5180 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
5181#else
5182 len = strlen(s1) + strlen(s2) + 2;
Christian Heimes72b710a2008-05-26 13:28:38 +00005183 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005184#endif
Fred Drake762e2061999-08-26 17:23:54 +00005185 if (newstr == NULL)
5186 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005187#ifdef MS_WINDOWS
5188 newenv = PyUnicode_AsUnicode(newstr);
5189 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5190 if (_wputenv(newenv)) {
5191 Py_DECREF(newstr);
5192 posix_error();
5193 return NULL;
5194 }
5195#else
Christian Heimes72b710a2008-05-26 13:28:38 +00005196 newenv = PyBytes_AS_STRING(newstr);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005197 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5198 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005199 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005200 posix_error();
5201 return NULL;
5202 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005203#endif
Fred Drake762e2061999-08-26 17:23:54 +00005204 /* Install the first arg and newstr in posix_putenv_garbage;
5205 * this will cause previous value to be collected. This has to
5206 * happen after the real putenv() call because the old value
5207 * was still accessible until then. */
5208 if (PyDict_SetItem(posix_putenv_garbage,
5209 PyTuple_GET_ITEM(args, 0), newstr)) {
5210 /* really not much we can do; just leak */
5211 PyErr_Clear();
5212 }
5213 else {
5214 Py_DECREF(newstr);
5215 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005216
5217#if defined(PYOS_OS2)
5218 }
5219#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005220 Py_INCREF(Py_None);
5221 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005222}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005223#endif /* putenv */
5224
Guido van Rossumc524d952001-10-19 01:31:59 +00005225#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005226PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005227"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005228Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005229
5230static PyObject *
5231posix_unsetenv(PyObject *self, PyObject *args)
5232{
5233 char *s1;
5234
5235 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5236 return NULL;
5237
5238 unsetenv(s1);
5239
5240 /* Remove the key from posix_putenv_garbage;
5241 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005242 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005243 * old value was still accessible until then.
5244 */
5245 if (PyDict_DelItem(posix_putenv_garbage,
5246 PyTuple_GET_ITEM(args, 0))) {
5247 /* really not much we can do; just leak */
5248 PyErr_Clear();
5249 }
5250
5251 Py_INCREF(Py_None);
5252 return Py_None;
5253}
5254#endif /* unsetenv */
5255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005256PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005257"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005258Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005259
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005260static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005261posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005262{
5263 int code;
5264 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005265 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005266 return NULL;
5267 message = strerror(code);
5268 if (message == NULL) {
5269 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005270 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005271 return NULL;
5272 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005273 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005274}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005275
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005276
Guido van Rossumc9641791998-08-04 15:26:23 +00005277#ifdef HAVE_SYS_WAIT_H
5278
Fred Drake106c1a02002-04-23 15:58:02 +00005279#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005280PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005281"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005282Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005283
5284static PyObject *
5285posix_WCOREDUMP(PyObject *self, PyObject *args)
5286{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005287 WAIT_TYPE status;
5288 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005289
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005290 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005291 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005292
5293 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005294}
5295#endif /* WCOREDUMP */
5296
5297#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005298PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005299"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005300Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005301job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005302
5303static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005304posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005305{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005306 WAIT_TYPE status;
5307 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005308
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005309 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005310 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005311
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005312 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005313}
5314#endif /* WIFCONTINUED */
5315
Guido van Rossumc9641791998-08-04 15:26:23 +00005316#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005317PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005318"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005319Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005320
5321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005322posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005323{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005324 WAIT_TYPE status;
5325 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005326
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005327 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005328 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005329
Fred Drake106c1a02002-04-23 15:58:02 +00005330 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005331}
5332#endif /* WIFSTOPPED */
5333
5334#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005335PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005336"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005337Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005338
5339static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005340posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005341{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005342 WAIT_TYPE status;
5343 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005344
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005345 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005346 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005347
Fred Drake106c1a02002-04-23 15:58:02 +00005348 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005349}
5350#endif /* WIFSIGNALED */
5351
5352#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005353PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005354"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005355Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005356system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005357
5358static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005359posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005360{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005361 WAIT_TYPE status;
5362 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005363
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005364 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005365 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005366
Fred Drake106c1a02002-04-23 15:58:02 +00005367 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005368}
5369#endif /* WIFEXITED */
5370
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005371#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005372PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005373"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005374Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005375
5376static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005377posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005378{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005379 WAIT_TYPE status;
5380 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005381
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005382 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005383 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005384
Guido van Rossumc9641791998-08-04 15:26:23 +00005385 return Py_BuildValue("i", WEXITSTATUS(status));
5386}
5387#endif /* WEXITSTATUS */
5388
5389#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005390PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005391"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005392Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005393value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005394
5395static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005396posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005397{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005398 WAIT_TYPE status;
5399 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005400
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005401 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005402 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005403
Guido van Rossumc9641791998-08-04 15:26:23 +00005404 return Py_BuildValue("i", WTERMSIG(status));
5405}
5406#endif /* WTERMSIG */
5407
5408#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005409PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005410"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411Return the signal that stopped the process that provided\n\
5412the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005413
5414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005415posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005416{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005417 WAIT_TYPE status;
5418 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005419
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005420 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005421 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005422
Guido van Rossumc9641791998-08-04 15:26:23 +00005423 return Py_BuildValue("i", WSTOPSIG(status));
5424}
5425#endif /* WSTOPSIG */
5426
5427#endif /* HAVE_SYS_WAIT_H */
5428
5429
Thomas Wouters477c8d52006-05-27 19:21:47 +00005430#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005431#ifdef _SCO_DS
5432/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5433 needed definitions in sys/statvfs.h */
5434#define _SVID3
5435#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005436#include <sys/statvfs.h>
5437
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005438static PyObject*
5439_pystatvfs_fromstructstatvfs(struct statvfs st) {
5440 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5441 if (v == NULL)
5442 return NULL;
5443
5444#if !defined(HAVE_LARGEFILE_SUPPORT)
Christian Heimes217cfd12007-12-02 14:31:20 +00005445 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5446 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5447 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5448 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5449 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5450 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5451 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5452 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5453 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5454 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005455#else
Christian Heimes217cfd12007-12-02 14:31:20 +00005456 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5457 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005458 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005459 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005460 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005461 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005462 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005463 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005464 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005465 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005466 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005467 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005468 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005469 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Christian Heimes217cfd12007-12-02 14:31:20 +00005470 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5471 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005472#endif
5473
5474 return v;
5475}
5476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005477PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005478"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005479Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005480
5481static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005482posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005483{
5484 int fd, res;
5485 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005486
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005487 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005488 return NULL;
5489 Py_BEGIN_ALLOW_THREADS
5490 res = fstatvfs(fd, &st);
5491 Py_END_ALLOW_THREADS
5492 if (res != 0)
5493 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005494
5495 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005496}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005497#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005498
5499
Thomas Wouters477c8d52006-05-27 19:21:47 +00005500#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005501#include <sys/statvfs.h>
5502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005504"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005505Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005506
5507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005508posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005509{
5510 char *path;
5511 int res;
5512 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005513 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005514 return NULL;
5515 Py_BEGIN_ALLOW_THREADS
5516 res = statvfs(path, &st);
5517 Py_END_ALLOW_THREADS
5518 if (res != 0)
5519 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005520
5521 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005522}
5523#endif /* HAVE_STATVFS */
5524
Fred Drakec9680921999-12-13 16:37:25 +00005525/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5526 * It maps strings representing configuration variable names to
5527 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005528 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005529 * rarely-used constants. There are three separate tables that use
5530 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005531 *
5532 * This code is always included, even if none of the interfaces that
5533 * need it are included. The #if hackery needed to avoid it would be
5534 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005535 */
5536struct constdef {
5537 char *name;
5538 long value;
5539};
5540
Fred Drake12c6e2d1999-12-14 21:25:03 +00005541static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005542conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005543 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005544{
Christian Heimes217cfd12007-12-02 14:31:20 +00005545 if (PyLong_Check(arg)) {
5546 *valuep = PyLong_AS_LONG(arg);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005547 return 1;
5548 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005549 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005550 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005551 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005552 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005553 size_t hi = tablesize;
5554 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005555 const char *confname;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005556 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005557 PyErr_SetString(PyExc_TypeError,
5558 "configuration names must be strings or integers");
5559 return 0;
5560 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005561 confname = _PyUnicode_AsString(arg);
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005562 if (confname == NULL)
5563 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005564 while (lo < hi) {
5565 mid = (lo + hi) / 2;
5566 cmp = strcmp(confname, table[mid].name);
5567 if (cmp < 0)
5568 hi = mid;
5569 else if (cmp > 0)
5570 lo = mid + 1;
5571 else {
5572 *valuep = table[mid].value;
5573 return 1;
5574 }
5575 }
5576 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005577 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005578 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005579}
5580
5581
5582#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5583static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005584#ifdef _PC_ABI_AIO_XFER_MAX
5585 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5586#endif
5587#ifdef _PC_ABI_ASYNC_IO
5588 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5589#endif
Fred Drakec9680921999-12-13 16:37:25 +00005590#ifdef _PC_ASYNC_IO
5591 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5592#endif
5593#ifdef _PC_CHOWN_RESTRICTED
5594 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5595#endif
5596#ifdef _PC_FILESIZEBITS
5597 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5598#endif
5599#ifdef _PC_LAST
5600 {"PC_LAST", _PC_LAST},
5601#endif
5602#ifdef _PC_LINK_MAX
5603 {"PC_LINK_MAX", _PC_LINK_MAX},
5604#endif
5605#ifdef _PC_MAX_CANON
5606 {"PC_MAX_CANON", _PC_MAX_CANON},
5607#endif
5608#ifdef _PC_MAX_INPUT
5609 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5610#endif
5611#ifdef _PC_NAME_MAX
5612 {"PC_NAME_MAX", _PC_NAME_MAX},
5613#endif
5614#ifdef _PC_NO_TRUNC
5615 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5616#endif
5617#ifdef _PC_PATH_MAX
5618 {"PC_PATH_MAX", _PC_PATH_MAX},
5619#endif
5620#ifdef _PC_PIPE_BUF
5621 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5622#endif
5623#ifdef _PC_PRIO_IO
5624 {"PC_PRIO_IO", _PC_PRIO_IO},
5625#endif
5626#ifdef _PC_SOCK_MAXBUF
5627 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5628#endif
5629#ifdef _PC_SYNC_IO
5630 {"PC_SYNC_IO", _PC_SYNC_IO},
5631#endif
5632#ifdef _PC_VDISABLE
5633 {"PC_VDISABLE", _PC_VDISABLE},
5634#endif
5635};
5636
Fred Drakec9680921999-12-13 16:37:25 +00005637static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005638conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005639{
5640 return conv_confname(arg, valuep, posix_constants_pathconf,
5641 sizeof(posix_constants_pathconf)
5642 / sizeof(struct constdef));
5643}
5644#endif
5645
5646#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005647PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005648"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005649Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005650If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005651
5652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005653posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005654{
5655 PyObject *result = NULL;
5656 int name, fd;
5657
Fred Drake12c6e2d1999-12-14 21:25:03 +00005658 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5659 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005660 long limit;
5661
5662 errno = 0;
5663 limit = fpathconf(fd, name);
5664 if (limit == -1 && errno != 0)
5665 posix_error();
5666 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005667 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005668 }
5669 return result;
5670}
5671#endif
5672
5673
5674#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005676"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005677Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005678If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005679
5680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005681posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005682{
5683 PyObject *result = NULL;
5684 int name;
5685 char *path;
5686
5687 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5688 conv_path_confname, &name)) {
5689 long limit;
5690
5691 errno = 0;
5692 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005693 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005694 if (errno == EINVAL)
5695 /* could be a path or name problem */
5696 posix_error();
5697 else
5698 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005699 }
Fred Drakec9680921999-12-13 16:37:25 +00005700 else
Christian Heimes217cfd12007-12-02 14:31:20 +00005701 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005702 }
5703 return result;
5704}
5705#endif
5706
5707#ifdef HAVE_CONFSTR
5708static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005709#ifdef _CS_ARCHITECTURE
5710 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5711#endif
5712#ifdef _CS_HOSTNAME
5713 {"CS_HOSTNAME", _CS_HOSTNAME},
5714#endif
5715#ifdef _CS_HW_PROVIDER
5716 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5717#endif
5718#ifdef _CS_HW_SERIAL
5719 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5720#endif
5721#ifdef _CS_INITTAB_NAME
5722 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5723#endif
Fred Drakec9680921999-12-13 16:37:25 +00005724#ifdef _CS_LFS64_CFLAGS
5725 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5726#endif
5727#ifdef _CS_LFS64_LDFLAGS
5728 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5729#endif
5730#ifdef _CS_LFS64_LIBS
5731 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5732#endif
5733#ifdef _CS_LFS64_LINTFLAGS
5734 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5735#endif
5736#ifdef _CS_LFS_CFLAGS
5737 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5738#endif
5739#ifdef _CS_LFS_LDFLAGS
5740 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5741#endif
5742#ifdef _CS_LFS_LIBS
5743 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5744#endif
5745#ifdef _CS_LFS_LINTFLAGS
5746 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5747#endif
Fred Draked86ed291999-12-15 15:34:33 +00005748#ifdef _CS_MACHINE
5749 {"CS_MACHINE", _CS_MACHINE},
5750#endif
Fred Drakec9680921999-12-13 16:37:25 +00005751#ifdef _CS_PATH
5752 {"CS_PATH", _CS_PATH},
5753#endif
Fred Draked86ed291999-12-15 15:34:33 +00005754#ifdef _CS_RELEASE
5755 {"CS_RELEASE", _CS_RELEASE},
5756#endif
5757#ifdef _CS_SRPC_DOMAIN
5758 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5759#endif
5760#ifdef _CS_SYSNAME
5761 {"CS_SYSNAME", _CS_SYSNAME},
5762#endif
5763#ifdef _CS_VERSION
5764 {"CS_VERSION", _CS_VERSION},
5765#endif
Fred Drakec9680921999-12-13 16:37:25 +00005766#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5767 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5768#endif
5769#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5770 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5771#endif
5772#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5773 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5774#endif
5775#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5776 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5777#endif
5778#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5779 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5780#endif
5781#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5782 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5783#endif
5784#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5785 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5786#endif
5787#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5788 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5789#endif
5790#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5791 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5792#endif
5793#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5794 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5795#endif
5796#ifdef _CS_XBS5_LP64_OFF64_LIBS
5797 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5798#endif
5799#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5800 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5801#endif
5802#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5803 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5804#endif
5805#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5806 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5807#endif
5808#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5809 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5810#endif
5811#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5812 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5813#endif
Fred Draked86ed291999-12-15 15:34:33 +00005814#ifdef _MIPS_CS_AVAIL_PROCESSORS
5815 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5816#endif
5817#ifdef _MIPS_CS_BASE
5818 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5819#endif
5820#ifdef _MIPS_CS_HOSTID
5821 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5822#endif
5823#ifdef _MIPS_CS_HW_NAME
5824 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5825#endif
5826#ifdef _MIPS_CS_NUM_PROCESSORS
5827 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5828#endif
5829#ifdef _MIPS_CS_OSREL_MAJ
5830 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5831#endif
5832#ifdef _MIPS_CS_OSREL_MIN
5833 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5834#endif
5835#ifdef _MIPS_CS_OSREL_PATCH
5836 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5837#endif
5838#ifdef _MIPS_CS_OS_NAME
5839 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5840#endif
5841#ifdef _MIPS_CS_OS_PROVIDER
5842 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5843#endif
5844#ifdef _MIPS_CS_PROCESSORS
5845 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5846#endif
5847#ifdef _MIPS_CS_SERIAL
5848 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5849#endif
5850#ifdef _MIPS_CS_VENDOR
5851 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5852#endif
Fred Drakec9680921999-12-13 16:37:25 +00005853};
5854
5855static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005856conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005857{
5858 return conv_confname(arg, valuep, posix_constants_confstr,
5859 sizeof(posix_constants_confstr)
5860 / sizeof(struct constdef));
5861}
5862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005863PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005864"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005865Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005866
5867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005868posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005869{
5870 PyObject *result = NULL;
5871 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005872 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005873
5874 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005875 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005876
Fred Drakec9680921999-12-13 16:37:25 +00005877 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005878 len = confstr(name, buffer, sizeof(buffer));
5879 if (len == 0) {
5880 if (errno) {
5881 posix_error();
5882 }
5883 else {
5884 result = Py_None;
5885 Py_INCREF(Py_None);
5886 }
Fred Drakec9680921999-12-13 16:37:25 +00005887 }
5888 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005889 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005890 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005891 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00005892 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005893 }
5894 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005895 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005896 }
5897 }
5898 return result;
5899}
5900#endif
5901
5902
5903#ifdef HAVE_SYSCONF
5904static struct constdef posix_constants_sysconf[] = {
5905#ifdef _SC_2_CHAR_TERM
5906 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5907#endif
5908#ifdef _SC_2_C_BIND
5909 {"SC_2_C_BIND", _SC_2_C_BIND},
5910#endif
5911#ifdef _SC_2_C_DEV
5912 {"SC_2_C_DEV", _SC_2_C_DEV},
5913#endif
5914#ifdef _SC_2_C_VERSION
5915 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5916#endif
5917#ifdef _SC_2_FORT_DEV
5918 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5919#endif
5920#ifdef _SC_2_FORT_RUN
5921 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5922#endif
5923#ifdef _SC_2_LOCALEDEF
5924 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5925#endif
5926#ifdef _SC_2_SW_DEV
5927 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5928#endif
5929#ifdef _SC_2_UPE
5930 {"SC_2_UPE", _SC_2_UPE},
5931#endif
5932#ifdef _SC_2_VERSION
5933 {"SC_2_VERSION", _SC_2_VERSION},
5934#endif
Fred Draked86ed291999-12-15 15:34:33 +00005935#ifdef _SC_ABI_ASYNCHRONOUS_IO
5936 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5937#endif
5938#ifdef _SC_ACL
5939 {"SC_ACL", _SC_ACL},
5940#endif
Fred Drakec9680921999-12-13 16:37:25 +00005941#ifdef _SC_AIO_LISTIO_MAX
5942 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5943#endif
Fred Drakec9680921999-12-13 16:37:25 +00005944#ifdef _SC_AIO_MAX
5945 {"SC_AIO_MAX", _SC_AIO_MAX},
5946#endif
5947#ifdef _SC_AIO_PRIO_DELTA_MAX
5948 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5949#endif
5950#ifdef _SC_ARG_MAX
5951 {"SC_ARG_MAX", _SC_ARG_MAX},
5952#endif
5953#ifdef _SC_ASYNCHRONOUS_IO
5954 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5955#endif
5956#ifdef _SC_ATEXIT_MAX
5957 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5958#endif
Fred Draked86ed291999-12-15 15:34:33 +00005959#ifdef _SC_AUDIT
5960 {"SC_AUDIT", _SC_AUDIT},
5961#endif
Fred Drakec9680921999-12-13 16:37:25 +00005962#ifdef _SC_AVPHYS_PAGES
5963 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5964#endif
5965#ifdef _SC_BC_BASE_MAX
5966 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5967#endif
5968#ifdef _SC_BC_DIM_MAX
5969 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5970#endif
5971#ifdef _SC_BC_SCALE_MAX
5972 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5973#endif
5974#ifdef _SC_BC_STRING_MAX
5975 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5976#endif
Fred Draked86ed291999-12-15 15:34:33 +00005977#ifdef _SC_CAP
5978 {"SC_CAP", _SC_CAP},
5979#endif
Fred Drakec9680921999-12-13 16:37:25 +00005980#ifdef _SC_CHARCLASS_NAME_MAX
5981 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5982#endif
5983#ifdef _SC_CHAR_BIT
5984 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5985#endif
5986#ifdef _SC_CHAR_MAX
5987 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5988#endif
5989#ifdef _SC_CHAR_MIN
5990 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5991#endif
5992#ifdef _SC_CHILD_MAX
5993 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5994#endif
5995#ifdef _SC_CLK_TCK
5996 {"SC_CLK_TCK", _SC_CLK_TCK},
5997#endif
5998#ifdef _SC_COHER_BLKSZ
5999 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6000#endif
6001#ifdef _SC_COLL_WEIGHTS_MAX
6002 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6003#endif
6004#ifdef _SC_DCACHE_ASSOC
6005 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6006#endif
6007#ifdef _SC_DCACHE_BLKSZ
6008 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6009#endif
6010#ifdef _SC_DCACHE_LINESZ
6011 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6012#endif
6013#ifdef _SC_DCACHE_SZ
6014 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6015#endif
6016#ifdef _SC_DCACHE_TBLKSZ
6017 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6018#endif
6019#ifdef _SC_DELAYTIMER_MAX
6020 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6021#endif
6022#ifdef _SC_EQUIV_CLASS_MAX
6023 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6024#endif
6025#ifdef _SC_EXPR_NEST_MAX
6026 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6027#endif
6028#ifdef _SC_FSYNC
6029 {"SC_FSYNC", _SC_FSYNC},
6030#endif
6031#ifdef _SC_GETGR_R_SIZE_MAX
6032 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6033#endif
6034#ifdef _SC_GETPW_R_SIZE_MAX
6035 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6036#endif
6037#ifdef _SC_ICACHE_ASSOC
6038 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6039#endif
6040#ifdef _SC_ICACHE_BLKSZ
6041 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6042#endif
6043#ifdef _SC_ICACHE_LINESZ
6044 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6045#endif
6046#ifdef _SC_ICACHE_SZ
6047 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6048#endif
Fred Draked86ed291999-12-15 15:34:33 +00006049#ifdef _SC_INF
6050 {"SC_INF", _SC_INF},
6051#endif
Fred Drakec9680921999-12-13 16:37:25 +00006052#ifdef _SC_INT_MAX
6053 {"SC_INT_MAX", _SC_INT_MAX},
6054#endif
6055#ifdef _SC_INT_MIN
6056 {"SC_INT_MIN", _SC_INT_MIN},
6057#endif
6058#ifdef _SC_IOV_MAX
6059 {"SC_IOV_MAX", _SC_IOV_MAX},
6060#endif
Fred Draked86ed291999-12-15 15:34:33 +00006061#ifdef _SC_IP_SECOPTS
6062 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6063#endif
Fred Drakec9680921999-12-13 16:37:25 +00006064#ifdef _SC_JOB_CONTROL
6065 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6066#endif
Fred Draked86ed291999-12-15 15:34:33 +00006067#ifdef _SC_KERN_POINTERS
6068 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6069#endif
6070#ifdef _SC_KERN_SIM
6071 {"SC_KERN_SIM", _SC_KERN_SIM},
6072#endif
Fred Drakec9680921999-12-13 16:37:25 +00006073#ifdef _SC_LINE_MAX
6074 {"SC_LINE_MAX", _SC_LINE_MAX},
6075#endif
6076#ifdef _SC_LOGIN_NAME_MAX
6077 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6078#endif
6079#ifdef _SC_LOGNAME_MAX
6080 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6081#endif
6082#ifdef _SC_LONG_BIT
6083 {"SC_LONG_BIT", _SC_LONG_BIT},
6084#endif
Fred Draked86ed291999-12-15 15:34:33 +00006085#ifdef _SC_MAC
6086 {"SC_MAC", _SC_MAC},
6087#endif
Fred Drakec9680921999-12-13 16:37:25 +00006088#ifdef _SC_MAPPED_FILES
6089 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6090#endif
6091#ifdef _SC_MAXPID
6092 {"SC_MAXPID", _SC_MAXPID},
6093#endif
6094#ifdef _SC_MB_LEN_MAX
6095 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6096#endif
6097#ifdef _SC_MEMLOCK
6098 {"SC_MEMLOCK", _SC_MEMLOCK},
6099#endif
6100#ifdef _SC_MEMLOCK_RANGE
6101 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6102#endif
6103#ifdef _SC_MEMORY_PROTECTION
6104 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6105#endif
6106#ifdef _SC_MESSAGE_PASSING
6107 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6108#endif
Fred Draked86ed291999-12-15 15:34:33 +00006109#ifdef _SC_MMAP_FIXED_ALIGNMENT
6110 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6111#endif
Fred Drakec9680921999-12-13 16:37:25 +00006112#ifdef _SC_MQ_OPEN_MAX
6113 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6114#endif
6115#ifdef _SC_MQ_PRIO_MAX
6116 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6117#endif
Fred Draked86ed291999-12-15 15:34:33 +00006118#ifdef _SC_NACLS_MAX
6119 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6120#endif
Fred Drakec9680921999-12-13 16:37:25 +00006121#ifdef _SC_NGROUPS_MAX
6122 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6123#endif
6124#ifdef _SC_NL_ARGMAX
6125 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6126#endif
6127#ifdef _SC_NL_LANGMAX
6128 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6129#endif
6130#ifdef _SC_NL_MSGMAX
6131 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6132#endif
6133#ifdef _SC_NL_NMAX
6134 {"SC_NL_NMAX", _SC_NL_NMAX},
6135#endif
6136#ifdef _SC_NL_SETMAX
6137 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6138#endif
6139#ifdef _SC_NL_TEXTMAX
6140 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6141#endif
6142#ifdef _SC_NPROCESSORS_CONF
6143 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6144#endif
6145#ifdef _SC_NPROCESSORS_ONLN
6146 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6147#endif
Fred Draked86ed291999-12-15 15:34:33 +00006148#ifdef _SC_NPROC_CONF
6149 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6150#endif
6151#ifdef _SC_NPROC_ONLN
6152 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6153#endif
Fred Drakec9680921999-12-13 16:37:25 +00006154#ifdef _SC_NZERO
6155 {"SC_NZERO", _SC_NZERO},
6156#endif
6157#ifdef _SC_OPEN_MAX
6158 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6159#endif
6160#ifdef _SC_PAGESIZE
6161 {"SC_PAGESIZE", _SC_PAGESIZE},
6162#endif
6163#ifdef _SC_PAGE_SIZE
6164 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6165#endif
6166#ifdef _SC_PASS_MAX
6167 {"SC_PASS_MAX", _SC_PASS_MAX},
6168#endif
6169#ifdef _SC_PHYS_PAGES
6170 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6171#endif
6172#ifdef _SC_PII
6173 {"SC_PII", _SC_PII},
6174#endif
6175#ifdef _SC_PII_INTERNET
6176 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6177#endif
6178#ifdef _SC_PII_INTERNET_DGRAM
6179 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6180#endif
6181#ifdef _SC_PII_INTERNET_STREAM
6182 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6183#endif
6184#ifdef _SC_PII_OSI
6185 {"SC_PII_OSI", _SC_PII_OSI},
6186#endif
6187#ifdef _SC_PII_OSI_CLTS
6188 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6189#endif
6190#ifdef _SC_PII_OSI_COTS
6191 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6192#endif
6193#ifdef _SC_PII_OSI_M
6194 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6195#endif
6196#ifdef _SC_PII_SOCKET
6197 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6198#endif
6199#ifdef _SC_PII_XTI
6200 {"SC_PII_XTI", _SC_PII_XTI},
6201#endif
6202#ifdef _SC_POLL
6203 {"SC_POLL", _SC_POLL},
6204#endif
6205#ifdef _SC_PRIORITIZED_IO
6206 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6207#endif
6208#ifdef _SC_PRIORITY_SCHEDULING
6209 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6210#endif
6211#ifdef _SC_REALTIME_SIGNALS
6212 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6213#endif
6214#ifdef _SC_RE_DUP_MAX
6215 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6216#endif
6217#ifdef _SC_RTSIG_MAX
6218 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6219#endif
6220#ifdef _SC_SAVED_IDS
6221 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6222#endif
6223#ifdef _SC_SCHAR_MAX
6224 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6225#endif
6226#ifdef _SC_SCHAR_MIN
6227 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6228#endif
6229#ifdef _SC_SELECT
6230 {"SC_SELECT", _SC_SELECT},
6231#endif
6232#ifdef _SC_SEMAPHORES
6233 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6234#endif
6235#ifdef _SC_SEM_NSEMS_MAX
6236 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6237#endif
6238#ifdef _SC_SEM_VALUE_MAX
6239 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6240#endif
6241#ifdef _SC_SHARED_MEMORY_OBJECTS
6242 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6243#endif
6244#ifdef _SC_SHRT_MAX
6245 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6246#endif
6247#ifdef _SC_SHRT_MIN
6248 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6249#endif
6250#ifdef _SC_SIGQUEUE_MAX
6251 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6252#endif
6253#ifdef _SC_SIGRT_MAX
6254 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6255#endif
6256#ifdef _SC_SIGRT_MIN
6257 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6258#endif
Fred Draked86ed291999-12-15 15:34:33 +00006259#ifdef _SC_SOFTPOWER
6260 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6261#endif
Fred Drakec9680921999-12-13 16:37:25 +00006262#ifdef _SC_SPLIT_CACHE
6263 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6264#endif
6265#ifdef _SC_SSIZE_MAX
6266 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6267#endif
6268#ifdef _SC_STACK_PROT
6269 {"SC_STACK_PROT", _SC_STACK_PROT},
6270#endif
6271#ifdef _SC_STREAM_MAX
6272 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6273#endif
6274#ifdef _SC_SYNCHRONIZED_IO
6275 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6276#endif
6277#ifdef _SC_THREADS
6278 {"SC_THREADS", _SC_THREADS},
6279#endif
6280#ifdef _SC_THREAD_ATTR_STACKADDR
6281 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6282#endif
6283#ifdef _SC_THREAD_ATTR_STACKSIZE
6284 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6285#endif
6286#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6287 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6288#endif
6289#ifdef _SC_THREAD_KEYS_MAX
6290 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6291#endif
6292#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6293 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6294#endif
6295#ifdef _SC_THREAD_PRIO_INHERIT
6296 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6297#endif
6298#ifdef _SC_THREAD_PRIO_PROTECT
6299 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6300#endif
6301#ifdef _SC_THREAD_PROCESS_SHARED
6302 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6303#endif
6304#ifdef _SC_THREAD_SAFE_FUNCTIONS
6305 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6306#endif
6307#ifdef _SC_THREAD_STACK_MIN
6308 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6309#endif
6310#ifdef _SC_THREAD_THREADS_MAX
6311 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6312#endif
6313#ifdef _SC_TIMERS
6314 {"SC_TIMERS", _SC_TIMERS},
6315#endif
6316#ifdef _SC_TIMER_MAX
6317 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6318#endif
6319#ifdef _SC_TTY_NAME_MAX
6320 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6321#endif
6322#ifdef _SC_TZNAME_MAX
6323 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6324#endif
6325#ifdef _SC_T_IOV_MAX
6326 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6327#endif
6328#ifdef _SC_UCHAR_MAX
6329 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6330#endif
6331#ifdef _SC_UINT_MAX
6332 {"SC_UINT_MAX", _SC_UINT_MAX},
6333#endif
6334#ifdef _SC_UIO_MAXIOV
6335 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6336#endif
6337#ifdef _SC_ULONG_MAX
6338 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6339#endif
6340#ifdef _SC_USHRT_MAX
6341 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6342#endif
6343#ifdef _SC_VERSION
6344 {"SC_VERSION", _SC_VERSION},
6345#endif
6346#ifdef _SC_WORD_BIT
6347 {"SC_WORD_BIT", _SC_WORD_BIT},
6348#endif
6349#ifdef _SC_XBS5_ILP32_OFF32
6350 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6351#endif
6352#ifdef _SC_XBS5_ILP32_OFFBIG
6353 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6354#endif
6355#ifdef _SC_XBS5_LP64_OFF64
6356 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6357#endif
6358#ifdef _SC_XBS5_LPBIG_OFFBIG
6359 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6360#endif
6361#ifdef _SC_XOPEN_CRYPT
6362 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6363#endif
6364#ifdef _SC_XOPEN_ENH_I18N
6365 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6366#endif
6367#ifdef _SC_XOPEN_LEGACY
6368 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6369#endif
6370#ifdef _SC_XOPEN_REALTIME
6371 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6372#endif
6373#ifdef _SC_XOPEN_REALTIME_THREADS
6374 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6375#endif
6376#ifdef _SC_XOPEN_SHM
6377 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6378#endif
6379#ifdef _SC_XOPEN_UNIX
6380 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6381#endif
6382#ifdef _SC_XOPEN_VERSION
6383 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6384#endif
6385#ifdef _SC_XOPEN_XCU_VERSION
6386 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6387#endif
6388#ifdef _SC_XOPEN_XPG2
6389 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6390#endif
6391#ifdef _SC_XOPEN_XPG3
6392 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6393#endif
6394#ifdef _SC_XOPEN_XPG4
6395 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6396#endif
6397};
6398
6399static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006400conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006401{
6402 return conv_confname(arg, valuep, posix_constants_sysconf,
6403 sizeof(posix_constants_sysconf)
6404 / sizeof(struct constdef));
6405}
6406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006407PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006408"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006409Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006410
6411static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006412posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006413{
6414 PyObject *result = NULL;
6415 int name;
6416
6417 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6418 int value;
6419
6420 errno = 0;
6421 value = sysconf(name);
6422 if (value == -1 && errno != 0)
6423 posix_error();
6424 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006425 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006426 }
6427 return result;
6428}
6429#endif
6430
6431
Fred Drakebec628d1999-12-15 18:31:10 +00006432/* This code is used to ensure that the tables of configuration value names
6433 * are in sorted order as required by conv_confname(), and also to build the
6434 * the exported dictionaries that are used to publish information about the
6435 * names available on the host platform.
6436 *
6437 * Sorting the table at runtime ensures that the table is properly ordered
6438 * when used, even for platforms we're not able to test on. It also makes
6439 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006440 */
Fred Drakebec628d1999-12-15 18:31:10 +00006441
6442static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006443cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006444{
6445 const struct constdef *c1 =
6446 (const struct constdef *) v1;
6447 const struct constdef *c2 =
6448 (const struct constdef *) v2;
6449
6450 return strcmp(c1->name, c2->name);
6451}
6452
6453static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006454setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006455 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006456{
Fred Drakebec628d1999-12-15 18:31:10 +00006457 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006458 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006459
6460 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6461 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006462 if (d == NULL)
6463 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006464
Barry Warsaw3155db32000-04-13 15:20:40 +00006465 for (i=0; i < tablesize; ++i) {
Christian Heimes217cfd12007-12-02 14:31:20 +00006466 PyObject *o = PyLong_FromLong(table[i].value);
Barry Warsaw3155db32000-04-13 15:20:40 +00006467 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6468 Py_XDECREF(o);
6469 Py_DECREF(d);
6470 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006471 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006472 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006473 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006474 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006475}
6476
Fred Drakebec628d1999-12-15 18:31:10 +00006477/* Return -1 on failure, 0 on success. */
6478static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006479setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006480{
6481#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006482 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006483 sizeof(posix_constants_pathconf)
6484 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006485 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006486 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006487#endif
6488#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006489 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006490 sizeof(posix_constants_confstr)
6491 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006492 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006493 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006494#endif
6495#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006496 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006497 sizeof(posix_constants_sysconf)
6498 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006499 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006500 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006501#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006502 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006503}
Fred Draked86ed291999-12-15 15:34:33 +00006504
6505
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006506PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006507"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006508Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006509in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006510
6511static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006512posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006513{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006514 abort();
6515 /*NOTREACHED*/
6516 Py_FatalError("abort() called from Python code didn't abort!");
6517 return NULL;
6518}
Fred Drakebec628d1999-12-15 18:31:10 +00006519
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006520#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006521PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006522"startfile(filepath [, operation]) - Start a file with its associated\n\
6523application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006524\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006525When \"operation\" is not specified or \"open\", this acts like\n\
6526double-clicking the file in Explorer, or giving the file name as an\n\
6527argument to the DOS \"start\" command: the file is opened with whatever\n\
6528application (if any) its extension is associated.\n\
6529When another \"operation\" is given, it specifies what should be done with\n\
6530the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006531\n\
6532startfile returns as soon as the associated application is launched.\n\
6533There is no option to wait for the application to close, and no way\n\
6534to retrieve the application's exit status.\n\
6535\n\
6536The filepath is relative to the current directory. If you want to use\n\
6537an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006538the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006539
6540static PyObject *
6541win32_startfile(PyObject *self, PyObject *args)
6542{
6543 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006544 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006545 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006546#ifdef Py_WIN_WIDE_FILENAMES
6547 if (unicode_file_names()) {
6548 PyObject *unipath, *woperation = NULL;
6549 if (!PyArg_ParseTuple(args, "U|s:startfile",
6550 &unipath, &operation)) {
6551 PyErr_Clear();
6552 goto normal;
6553 }
6554
6555
6556 if (operation) {
6557 woperation = PyUnicode_DecodeASCII(operation,
6558 strlen(operation), NULL);
6559 if (!woperation) {
6560 PyErr_Clear();
6561 operation = NULL;
6562 goto normal;
6563 }
6564 }
6565
6566 Py_BEGIN_ALLOW_THREADS
6567 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6568 PyUnicode_AS_UNICODE(unipath),
6569 NULL, NULL, SW_SHOWNORMAL);
6570 Py_END_ALLOW_THREADS
6571
6572 Py_XDECREF(woperation);
6573 if (rc <= (HINSTANCE)32) {
6574 PyObject *errval = win32_error_unicode("startfile",
6575 PyUnicode_AS_UNICODE(unipath));
6576 return errval;
6577 }
6578 Py_INCREF(Py_None);
6579 return Py_None;
6580 }
6581#endif
6582
6583normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006584 if (!PyArg_ParseTuple(args, "et|s:startfile",
6585 Py_FileSystemDefaultEncoding, &filepath,
6586 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006587 return NULL;
6588 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006589 rc = ShellExecute((HWND)0, operation, filepath,
6590 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006591 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006592 if (rc <= (HINSTANCE)32) {
6593 PyObject *errval = win32_error("startfile", filepath);
6594 PyMem_Free(filepath);
6595 return errval;
6596 }
6597 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006598 Py_INCREF(Py_None);
6599 return Py_None;
6600}
6601#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006602
Martin v. Löwis438b5342002-12-27 10:16:42 +00006603#ifdef HAVE_GETLOADAVG
6604PyDoc_STRVAR(posix_getloadavg__doc__,
6605"getloadavg() -> (float, float, float)\n\n\
6606Return the number of processes in the system run queue averaged over\n\
6607the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6608was unobtainable");
6609
6610static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006611posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006612{
6613 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006614 if (getloadavg(loadavg, 3)!=3) {
6615 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6616 return NULL;
6617 } else
6618 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6619}
6620#endif
6621
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006622#ifdef MS_WINDOWS
6623
6624PyDoc_STRVAR(win32_urandom__doc__,
6625"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006626Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006627
6628typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6629 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6630 DWORD dwFlags );
6631typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6632 BYTE *pbBuffer );
6633
6634static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006635/* This handle is never explicitly released. Instead, the operating
6636 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006637static HCRYPTPROV hCryptProv = 0;
6638
Tim Peters4ad82172004-08-30 17:02:04 +00006639static PyObject*
6640win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006641{
Tim Petersd3115382004-08-30 17:36:46 +00006642 int howMany;
6643 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006644
Tim Peters4ad82172004-08-30 17:02:04 +00006645 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006646 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006647 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006648 if (howMany < 0)
6649 return PyErr_Format(PyExc_ValueError,
6650 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006651
Tim Peters4ad82172004-08-30 17:02:04 +00006652 if (hCryptProv == 0) {
6653 HINSTANCE hAdvAPI32 = NULL;
6654 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006655
Tim Peters4ad82172004-08-30 17:02:04 +00006656 /* Obtain handle to the DLL containing CryptoAPI
6657 This should not fail */
6658 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6659 if(hAdvAPI32 == NULL)
6660 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006661
Tim Peters4ad82172004-08-30 17:02:04 +00006662 /* Obtain pointers to the CryptoAPI functions
6663 This will fail on some early versions of Win95 */
6664 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6665 hAdvAPI32,
6666 "CryptAcquireContextA");
6667 if (pCryptAcquireContext == NULL)
6668 return PyErr_Format(PyExc_NotImplementedError,
6669 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006670
Tim Peters4ad82172004-08-30 17:02:04 +00006671 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6672 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006673 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006674 return PyErr_Format(PyExc_NotImplementedError,
6675 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006676
Tim Peters4ad82172004-08-30 17:02:04 +00006677 /* Acquire context */
6678 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6679 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6680 return win32_error("CryptAcquireContext", NULL);
6681 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006682
Tim Peters4ad82172004-08-30 17:02:04 +00006683 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006684 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006685 if (result != NULL) {
6686 /* Get random data */
Amaury Forgeot d'Arca05ada32008-07-21 21:13:14 +00006687 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
Tim Petersd3115382004-08-30 17:36:46 +00006688 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006689 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006690 Py_DECREF(result);
6691 return win32_error("CryptGenRandom", NULL);
6692 }
Tim Peters4ad82172004-08-30 17:02:04 +00006693 }
Tim Petersd3115382004-08-30 17:36:46 +00006694 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006695}
6696#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006697
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006698PyDoc_STRVAR(device_encoding__doc__,
6699"device_encoding(fd) -> str\n\n\
6700Return a string describing the encoding of the device\n\
6701if the output is a terminal; else return None.");
6702
6703static PyObject *
6704device_encoding(PyObject *self, PyObject *args)
6705{
6706 int fd;
6707 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6708 return NULL;
6709 if (!isatty(fd)) {
6710 Py_INCREF(Py_None);
6711 return Py_None;
6712 }
6713#if defined(MS_WINDOWS) || defined(MS_WIN64)
6714 if (fd == 0) {
6715 char buf[100];
6716 sprintf(buf, "cp%d", GetConsoleCP());
6717 return PyUnicode_FromString(buf);
6718 }
6719 if (fd == 1 || fd == 2) {
6720 char buf[100];
6721 sprintf(buf, "cp%d", GetConsoleOutputCP());
6722 return PyUnicode_FromString(buf);
6723 }
6724#elif defined(CODESET)
6725 {
6726 char *codeset = nl_langinfo(CODESET);
Mark Dickinsonda2706b2008-12-11 18:03:03 +00006727 if (codeset != NULL && codeset[0] != 0)
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006728 return PyUnicode_FromString(codeset);
6729 }
6730#endif
6731 Py_INCREF(Py_None);
6732 return Py_None;
6733}
6734
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006735#ifdef __VMS
6736/* Use openssl random routine */
6737#include <openssl/rand.h>
6738PyDoc_STRVAR(vms_urandom__doc__,
6739"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006740Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006741
6742static PyObject*
6743vms_urandom(PyObject *self, PyObject *args)
6744{
6745 int howMany;
6746 PyObject* result;
6747
6748 /* Read arguments */
6749 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6750 return NULL;
6751 if (howMany < 0)
6752 return PyErr_Format(PyExc_ValueError,
6753 "negative argument not allowed");
6754
6755 /* Allocate bytes */
Christian Heimes72b710a2008-05-26 13:28:38 +00006756 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006757 if (result != NULL) {
6758 /* Get random data */
6759 if (RAND_pseudo_bytes((unsigned char*)
Christian Heimes72b710a2008-05-26 13:28:38 +00006760 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006761 howMany) < 0) {
6762 Py_DECREF(result);
6763 return PyErr_Format(PyExc_ValueError,
6764 "RAND_pseudo_bytes");
6765 }
6766 }
6767 return result;
6768}
6769#endif
6770
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006771static PyMethodDef posix_methods[] = {
6772 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6773#ifdef HAVE_TTYNAME
6774 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6775#endif
6776 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006777#ifdef HAVE_CHFLAGS
6778 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6779#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006780 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00006781#ifdef HAVE_FCHMOD
6782 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
6783#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006784#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006785 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006786#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00006787#ifdef HAVE_LCHMOD
6788 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
6789#endif /* HAVE_LCHMOD */
6790#ifdef HAVE_FCHOWN
6791 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
6792#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006793#ifdef HAVE_LCHFLAGS
6794 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6795#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006796#ifdef HAVE_LCHOWN
6797 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6798#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006799#ifdef HAVE_CHROOT
6800 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6801#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006802#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006803 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006804#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006805#ifdef HAVE_GETCWD
Guido van Rossumf0af3e32008-10-02 18:55:37 +00006806 {"getcwd", (PyCFunction)posix_getcwd_unicode,
6807 METH_NOARGS, posix_getcwd__doc__},
6808 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
6809 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006810#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006811#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006812 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006813#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006814 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6815 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6816 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006817#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006818 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006819#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006820#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006821 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006822#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006823 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6824 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6825 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006826 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006827#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006828 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006829#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006830#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006831 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006832#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006833 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006834#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006835 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006836#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006837 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6838 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6839 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006840#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006841 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006842#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006843 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006844#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006845 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6846 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006847#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006848#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006849 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6850 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006851#if defined(PYOS_OS2)
6852 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6853 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6854#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006855#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006856#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006857 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006858#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006859#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006860 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006861#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006862#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006863 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006864#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006865#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006866 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006867#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006868#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006869 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006870#endif /* HAVE_GETEGID */
6871#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006872 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006873#endif /* HAVE_GETEUID */
6874#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006875 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006876#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006877#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006878 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006879#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006880 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006881#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006882 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006883#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006884#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006885 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006886#endif /* HAVE_GETPPID */
6887#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006888 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006889#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006890#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006891 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006892#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006893#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006894 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006895#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006896#ifdef HAVE_KILLPG
6897 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6898#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006899#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006900 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006901#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006902#ifdef MS_WINDOWS
6903 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6904#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006905#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006906 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006907#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006908#ifdef HAVE_SETEUID
6909 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6910#endif /* HAVE_SETEUID */
6911#ifdef HAVE_SETEGID
6912 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6913#endif /* HAVE_SETEGID */
6914#ifdef HAVE_SETREUID
6915 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6916#endif /* HAVE_SETREUID */
6917#ifdef HAVE_SETREGID
6918 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6919#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006920#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006921 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006922#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006923#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006924 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006925#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006926#ifdef HAVE_GETPGID
6927 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6928#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006929#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006930 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006931#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006932#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006933 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006934#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006935#ifdef HAVE_WAIT3
6936 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6937#endif /* HAVE_WAIT3 */
6938#ifdef HAVE_WAIT4
6939 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6940#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006941#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006942 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006943#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006944#ifdef HAVE_GETSID
6945 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6946#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006947#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006948 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006949#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006950#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006951 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006952#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006953#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006954 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006955#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006956#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006957 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006958#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006959 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6960 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Christian Heimesfdab48e2008-01-20 09:06:41 +00006961 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006962 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006963 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6964 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6965 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6966 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6967 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6968 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006969 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006970#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006971 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006972#endif
6973#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006974 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006975#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006976#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006977 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6978#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006979#ifdef HAVE_DEVICE_MACROS
6980 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6981 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6982 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6983#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006984#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006985 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006986#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006987#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006988 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006989#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006990#ifdef HAVE_UNSETENV
6991 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6992#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006993 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00006994#ifdef HAVE_FCHDIR
6995 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6996#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006997#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006998 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006999#endif
7000#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007001 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007002#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007003#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007004#ifdef WCOREDUMP
7005 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7006#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007007#ifdef WIFCONTINUED
7008 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7009#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007010#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007011 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007012#endif /* WIFSTOPPED */
7013#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007014 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007015#endif /* WIFSIGNALED */
7016#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007017 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007018#endif /* WIFEXITED */
7019#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007020 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007021#endif /* WEXITSTATUS */
7022#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007023 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007024#endif /* WTERMSIG */
7025#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007026 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007027#endif /* WSTOPSIG */
7028#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007029#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007030 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007031#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007032#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007033 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007034#endif
Fred Drakec9680921999-12-13 16:37:25 +00007035#ifdef HAVE_CONFSTR
7036 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7037#endif
7038#ifdef HAVE_SYSCONF
7039 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7040#endif
7041#ifdef HAVE_FPATHCONF
7042 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7043#endif
7044#ifdef HAVE_PATHCONF
7045 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7046#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007047 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007048#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007049 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7050#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007051#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007052 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007053#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007054 #ifdef MS_WINDOWS
7055 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7056 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007057 #ifdef __VMS
7058 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7059 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007060 {NULL, NULL} /* Sentinel */
7061};
7062
7063
Barry Warsaw4a342091996-12-19 23:50:02 +00007064static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007065ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007066{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007067 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007068}
7069
Guido van Rossumd48f2521997-12-05 22:19:34 +00007070#if defined(PYOS_OS2)
7071/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007072static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007073{
7074 APIRET rc;
7075 ULONG values[QSV_MAX+1];
7076 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007077 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007078
7079 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007080 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007081 Py_END_ALLOW_THREADS
7082
7083 if (rc != NO_ERROR) {
7084 os2_error(rc);
7085 return -1;
7086 }
7087
Fred Drake4d1e64b2002-04-15 19:40:07 +00007088 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7089 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7090 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7091 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7092 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7093 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7094 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007095
7096 switch (values[QSV_VERSION_MINOR]) {
7097 case 0: ver = "2.00"; break;
7098 case 10: ver = "2.10"; break;
7099 case 11: ver = "2.11"; break;
7100 case 30: ver = "3.00"; break;
7101 case 40: ver = "4.00"; break;
7102 case 50: ver = "5.00"; break;
7103 default:
Tim Peters885d4572001-11-28 20:27:42 +00007104 PyOS_snprintf(tmp, sizeof(tmp),
7105 "%d-%d", values[QSV_VERSION_MAJOR],
7106 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007107 ver = &tmp[0];
7108 }
7109
7110 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007111 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007112 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007113
7114 /* Add Indicator of Which Drive was Used to Boot the System */
7115 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7116 tmp[1] = ':';
7117 tmp[2] = '\0';
7118
Fred Drake4d1e64b2002-04-15 19:40:07 +00007119 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007120}
7121#endif
7122
Barry Warsaw4a342091996-12-19 23:50:02 +00007123static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007124all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007125{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007126#ifdef F_OK
7127 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007128#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007129#ifdef R_OK
7130 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007131#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007132#ifdef W_OK
7133 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007134#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007135#ifdef X_OK
7136 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007137#endif
Fred Drakec9680921999-12-13 16:37:25 +00007138#ifdef NGROUPS_MAX
7139 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7140#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007141#ifdef TMP_MAX
7142 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7143#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007144#ifdef WCONTINUED
7145 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7146#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007147#ifdef WNOHANG
7148 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007149#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007150#ifdef WUNTRACED
7151 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7152#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007153#ifdef O_RDONLY
7154 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7155#endif
7156#ifdef O_WRONLY
7157 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7158#endif
7159#ifdef O_RDWR
7160 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7161#endif
7162#ifdef O_NDELAY
7163 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7164#endif
7165#ifdef O_NONBLOCK
7166 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7167#endif
7168#ifdef O_APPEND
7169 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7170#endif
7171#ifdef O_DSYNC
7172 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7173#endif
7174#ifdef O_RSYNC
7175 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7176#endif
7177#ifdef O_SYNC
7178 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7179#endif
7180#ifdef O_NOCTTY
7181 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7182#endif
7183#ifdef O_CREAT
7184 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7185#endif
7186#ifdef O_EXCL
7187 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7188#endif
7189#ifdef O_TRUNC
7190 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7191#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007192#ifdef O_BINARY
7193 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7194#endif
7195#ifdef O_TEXT
7196 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7197#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007198#ifdef O_LARGEFILE
7199 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7200#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007201#ifdef O_SHLOCK
7202 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7203#endif
7204#ifdef O_EXLOCK
7205 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7206#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007207
Tim Peters5aa91602002-01-30 05:46:57 +00007208/* MS Windows */
7209#ifdef O_NOINHERIT
7210 /* Don't inherit in child processes. */
7211 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7212#endif
7213#ifdef _O_SHORT_LIVED
7214 /* Optimize for short life (keep in memory). */
7215 /* MS forgot to define this one with a non-underscore form too. */
7216 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7217#endif
7218#ifdef O_TEMPORARY
7219 /* Automatically delete when last handle is closed. */
7220 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7221#endif
7222#ifdef O_RANDOM
7223 /* Optimize for random access. */
7224 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7225#endif
7226#ifdef O_SEQUENTIAL
7227 /* Optimize for sequential access. */
7228 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7229#endif
7230
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007231/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007232#ifdef O_ASYNC
7233 /* Send a SIGIO signal whenever input or output
7234 becomes available on file descriptor */
7235 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
7236#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007237#ifdef O_DIRECT
7238 /* Direct disk access. */
7239 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7240#endif
7241#ifdef O_DIRECTORY
7242 /* Must be a directory. */
7243 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7244#endif
7245#ifdef O_NOFOLLOW
7246 /* Do not follow links. */
7247 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7248#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007249#ifdef O_NOATIME
7250 /* Do not update the access time. */
7251 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
7252#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007253
Barry Warsaw5676bd12003-01-07 20:57:09 +00007254 /* These come from sysexits.h */
7255#ifdef EX_OK
7256 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007257#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007258#ifdef EX_USAGE
7259 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007260#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007261#ifdef EX_DATAERR
7262 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007263#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007264#ifdef EX_NOINPUT
7265 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007266#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007267#ifdef EX_NOUSER
7268 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007269#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007270#ifdef EX_NOHOST
7271 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007272#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007273#ifdef EX_UNAVAILABLE
7274 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007275#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007276#ifdef EX_SOFTWARE
7277 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007278#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007279#ifdef EX_OSERR
7280 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007281#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007282#ifdef EX_OSFILE
7283 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007284#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007285#ifdef EX_CANTCREAT
7286 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007287#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007288#ifdef EX_IOERR
7289 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007290#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007291#ifdef EX_TEMPFAIL
7292 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007293#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007294#ifdef EX_PROTOCOL
7295 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007296#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007297#ifdef EX_NOPERM
7298 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007299#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007300#ifdef EX_CONFIG
7301 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007302#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007303#ifdef EX_NOTFOUND
7304 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007305#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007306
Guido van Rossum246bc171999-02-01 23:54:31 +00007307#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007308#if defined(PYOS_OS2) && defined(PYCC_GCC)
7309 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7310 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7311 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7312 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7313 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7314 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7315 if (ins(d, "P_PM", (long)P_PM)) return -1;
7316 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7317 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7318 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7319 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7320 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7321 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7322 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7323 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7324 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7325 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7326 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7327 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7328 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7329#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007330 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7331 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7332 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7333 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7334 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007335#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007336#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007337
Guido van Rossumd48f2521997-12-05 22:19:34 +00007338#if defined(PYOS_OS2)
7339 if (insertvalues(d)) return -1;
7340#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007341 return 0;
7342}
7343
7344
Tim Peters5aa91602002-01-30 05:46:57 +00007345#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007346#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007347#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007348
7349#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007350#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007351#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007352
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007353#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007354#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007355#define MODNAME "posix"
7356#endif
7357
Martin v. Löwis1a214512008-06-11 05:26:20 +00007358static struct PyModuleDef posixmodule = {
7359 PyModuleDef_HEAD_INIT,
7360 MODNAME,
7361 posix__doc__,
7362 -1,
7363 posix_methods,
7364 NULL,
7365 NULL,
7366 NULL,
7367 NULL
7368};
7369
7370
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007371PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007372INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007373{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007374 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007375
Martin v. Löwis1a214512008-06-11 05:26:20 +00007376 m = PyModule_Create(&posixmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007377 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007378 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007379
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007380 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007381 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007382 Py_XINCREF(v);
7383 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007384 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00007385 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007386
Fred Drake4d1e64b2002-04-15 19:40:07 +00007387 if (all_ins(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007388 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007389
Fred Drake4d1e64b2002-04-15 19:40:07 +00007390 if (setup_confname_tables(m))
Martin v. Löwis1a214512008-06-11 05:26:20 +00007391 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007392
Fred Drake4d1e64b2002-04-15 19:40:07 +00007393 Py_INCREF(PyExc_OSError);
7394 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007395
Guido van Rossumb3d39562000-01-31 18:41:26 +00007396#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007397 if (posix_putenv_garbage == NULL)
7398 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007399#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007400
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007401 if (!initialized) {
7402 stat_result_desc.name = MODNAME ".stat_result";
7403 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7404 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7405 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7406 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7407 structseq_new = StatResultType.tp_new;
7408 StatResultType.tp_new = statresult_new;
7409
7410 statvfs_result_desc.name = MODNAME ".statvfs_result";
7411 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7412 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007413 Py_INCREF((PyObject*) &StatResultType);
7414 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007415 Py_INCREF((PyObject*) &StatVFSResultType);
7416 PyModule_AddObject(m, "statvfs_result",
7417 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007418 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007419
7420#ifdef __APPLE__
7421 /*
7422 * Step 2 of weak-linking support on Mac OS X.
7423 *
7424 * The code below removes functions that are not available on the
7425 * currently active platform.
7426 *
7427 * This block allow one to use a python binary that was build on
7428 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7429 * OSX 10.4.
7430 */
7431#ifdef HAVE_FSTATVFS
7432 if (fstatvfs == NULL) {
7433 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007434 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007435 }
7436 }
7437#endif /* HAVE_FSTATVFS */
7438
7439#ifdef HAVE_STATVFS
7440 if (statvfs == NULL) {
7441 if (PyObject_DelAttrString(m, "statvfs") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007442 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007443 }
7444 }
7445#endif /* HAVE_STATVFS */
7446
7447# ifdef HAVE_LCHOWN
7448 if (lchown == NULL) {
7449 if (PyObject_DelAttrString(m, "lchown") == -1) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00007450 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007451 }
7452 }
7453#endif /* HAVE_LCHOWN */
7454
7455
7456#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00007457 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007458
Guido van Rossumb6775db1994-08-01 11:34:53 +00007459}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007460
7461#ifdef __cplusplus
7462}
7463#endif