blob: 75ce9914ac6b219a392a7dc285c05024f9db1fd0 [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
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int chown(const char *, uid_t, gid_t);
190extern char *getcwd(char *, int);
191extern char *strerror(int);
192extern int link(const char *, const char *);
193extern int rename(const char *, const char *);
194extern int stat(const char *, struct stat *);
195extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000198#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000201#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000203
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_UTIME_H
207#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000210#ifdef HAVE_SYS_UTIME_H
211#include <sys/utime.h>
212#define HAVE_UTIME_H /* pretend we do for the rest of this file */
213#endif /* HAVE_SYS_UTIME_H */
214
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#ifdef HAVE_SYS_TIMES_H
216#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000217#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
219#ifdef HAVE_SYS_PARAM_H
220#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
223#ifdef HAVE_SYS_UTSNAME_H
224#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#define NAMLEN(dirent) strlen((dirent)->d_name)
230#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000231#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000232#include <direct.h>
233#define NAMLEN(dirent) strlen((dirent)->d_name)
234#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000237#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000238#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#endif
241#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#endif
244#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000252#endif
253#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255#endif
256#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000259#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000261#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
Guido van Rossumd48f2521997-12-05 22:19:34 +0000264#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000266#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267
Tim Petersbc2e10e2002-03-03 23:17:02 +0000268#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269#if defined(PATH_MAX) && PATH_MAX > 1024
270#define MAXPATHLEN PATH_MAX
271#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000272#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000273#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#endif /* MAXPATHLEN */
275
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000276#ifdef UNION_WAIT
277/* Emulate some macros on systems that have a union instead of macros */
278
279#ifndef WIFEXITED
280#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
281#endif
282
283#ifndef WEXITSTATUS
284#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
285#endif
286
287#ifndef WTERMSIG
288#define WTERMSIG(u_wait) ((u_wait).w_termsig)
289#endif
290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291#define WAIT_TYPE union wait
292#define WAIT_STATUS_INT(s) (s.w_status)
293
294#else /* !UNION_WAIT */
295#define WAIT_TYPE int
296#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000297#endif /* UNION_WAIT */
298
Greg Wardb48bc172000-03-01 21:51:56 +0000299/* Don't use the "_r" form if we don't need it (also, won't have a
300 prototype for it, at least on Solaris -- maybe others as well?). */
301#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
302#define USE_CTERMID_R
303#endif
304
305#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
306#define USE_TMPNAM_R
307#endif
308
Fred Drake699f3522000-06-29 21:12:41 +0000309/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000310#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000311#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000312# define STAT win32_stat
313# define FSTAT win32_fstat
314# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000315#else
316# define STAT stat
317# define FSTAT fstat
318# define STRUCT_STAT struct stat
319#endif
320
Tim Peters11b23062003-04-23 02:39:17 +0000321#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000322#include <sys/mkdev.h>
323#else
324#if defined(MAJOR_IN_SYSMACROS)
325#include <sys/sysmacros.h>
326#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000327#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
328#include <sys/mkdev.h>
329#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000330#endif
Fred Drake699f3522000-06-29 21:12:41 +0000331
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000333#ifdef WITH_NEXT_FRAMEWORK
334/* On Darwin/MacOSX a shared library or framework has no access to
335** environ directly, we must obtain it with _NSGetEnviron().
336*/
337#include <crt_externs.h>
338static char **environ;
339#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000341#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342
Barry Warsaw53699e91996-12-10 23:23:01 +0000343static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000344convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345{
Barry Warsaw53699e91996-12-10 23:23:01 +0000346 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 if (d == NULL)
350 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000351#ifdef WITH_NEXT_FRAMEWORK
352 if (environ == NULL)
353 environ = *_NSGetEnviron();
354#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 if (environ == NULL)
356 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000357 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000359 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 char *p = strchr(*e, '=');
362 if (p == NULL)
363 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000364 k = PyString_FromStringAndSize(*e, (int)(p-*e));
365 if (k == NULL) {
366 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000368 }
369 v = PyString_FromString(p+1);
370 if (v == NULL) {
371 PyErr_Clear();
372 Py_DECREF(k);
373 continue;
374 }
375 if (PyDict_GetItem(d, k) == NULL) {
376 if (PyDict_SetItem(d, k, v) != 0)
377 PyErr_Clear();
378 }
379 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000380 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000382#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000383 {
384 APIRET rc;
385 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
386
387 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000388 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000389 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000390 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000391 Py_DECREF(v);
392 }
393 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
394 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
395 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000396 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000397 Py_DECREF(v);
398 }
399 }
400#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 return d;
402}
403
404
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405/* Set a POSIX-specific error from errno, and return NULL */
406
Barry Warsawd58d7641998-07-23 16:14:40 +0000407static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000408posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000409{
Barry Warsawca74da41999-02-09 19:31:45 +0000410 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411}
Barry Warsawd58d7641998-07-23 16:14:40 +0000412static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000413posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000414{
Barry Warsawca74da41999-02-09 19:31:45 +0000415 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000416}
417
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000418#ifdef Py_WIN_WIDE_FILENAMES
419static PyObject *
420posix_error_with_unicode_filename(Py_UNICODE* name)
421{
422 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
423}
424#endif /* Py_WIN_WIDE_FILENAMES */
425
426
Mark Hammondef8b6542001-05-13 08:04:26 +0000427static PyObject *
428posix_error_with_allocated_filename(char* name)
429{
430 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
431 PyMem_Free(name);
432 return rc;
433}
434
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000435#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000436static PyObject *
437win32_error(char* function, char* filename)
438{
Mark Hammond33a6da92000-08-15 00:46:38 +0000439 /* XXX We should pass the function name along in the future.
440 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000441 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000442 Windows error object, which is non-trivial.
443 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000444 errno = GetLastError();
445 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000446 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000447 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000448 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000449}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450
451#ifdef Py_WIN_WIDE_FILENAMES
452static PyObject *
453win32_error_unicode(char* function, Py_UNICODE* filename)
454{
455 /* XXX - see win32_error for comments on 'function' */
456 errno = GetLastError();
457 if (filename)
458 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
459 else
460 return PyErr_SetFromWindowsErr(errno);
461}
462
463static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
464{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465}
466
467/* Function suitable for O& conversion */
468static int
469convert_to_unicode(PyObject *arg, void* _param)
470{
471 PyObject **param = (PyObject**)_param;
472 if (PyUnicode_CheckExact(arg)) {
473 Py_INCREF(arg);
474 *param = arg;
475 }
476 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000477 /* For a Unicode subtype that's not a Unicode object,
478 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
480 PyUnicode_GET_SIZE(arg));
481 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000482 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 else
484 *param = PyUnicode_FromEncodedObject(arg,
485 Py_FileSystemDefaultEncoding,
486 "strict");
487 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000488}
489
490#endif /* Py_WIN_WIDE_FILENAMES */
491
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000492#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493
Guido van Rossumd48f2521997-12-05 22:19:34 +0000494#if defined(PYOS_OS2)
495/**********************************************************************
496 * Helper Function to Trim and Format OS/2 Messages
497 **********************************************************************/
498 static void
499os2_formatmsg(char *msgbuf, int msglen, char *reason)
500{
501 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
502
503 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
504 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
505
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000506 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000507 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
508 }
509
510 /* Add Optional Reason Text */
511 if (reason) {
512 strcat(msgbuf, " : ");
513 strcat(msgbuf, reason);
514 }
515}
516
517/**********************************************************************
518 * Decode an OS/2 Operating System Error Code
519 *
520 * A convenience function to lookup an OS/2 error code and return a
521 * text message we can use to raise a Python exception.
522 *
523 * Notes:
524 * The messages for errors returned from the OS/2 kernel reside in
525 * the file OSO001.MSG in the \OS2 directory hierarchy.
526 *
527 **********************************************************************/
528 static char *
529os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
530{
531 APIRET rc;
532 ULONG msglen;
533
534 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
535 Py_BEGIN_ALLOW_THREADS
536 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
537 errorcode, "oso001.msg", &msglen);
538 Py_END_ALLOW_THREADS
539
540 if (rc == NO_ERROR)
541 os2_formatmsg(msgbuf, msglen, reason);
542 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000543 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000544 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000545
546 return msgbuf;
547}
548
549/* Set an OS/2-specific error and return NULL. OS/2 kernel
550 errors are not in a global variable e.g. 'errno' nor are
551 they congruent with posix error numbers. */
552
553static PyObject * os2_error(int code)
554{
555 char text[1024];
556 PyObject *v;
557
558 os2_strerror(text, sizeof(text), code, "");
559
560 v = Py_BuildValue("(is)", code, text);
561 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000562 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000563 Py_DECREF(v);
564 }
565 return NULL; /* Signal to Python that an Exception is Pending */
566}
567
568#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569
570/* POSIX generic methods */
571
Barry Warsaw53699e91996-12-10 23:23:01 +0000572static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000573posix_fildes(PyObject *fdobj, int (*func)(int))
574{
575 int fd;
576 int res;
577 fd = PyObject_AsFileDescriptor(fdobj);
578 if (fd < 0)
579 return NULL;
580 Py_BEGIN_ALLOW_THREADS
581 res = (*func)(fd);
582 Py_END_ALLOW_THREADS
583 if (res < 0)
584 return posix_error();
585 Py_INCREF(Py_None);
586 return Py_None;
587}
Guido van Rossum21142a01999-01-08 21:05:37 +0000588
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000589#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000590static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000591unicode_file_names(void)
592{
593 static int canusewide = -1;
594 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000595 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000596 the Windows NT family. */
597 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
598 }
599 return canusewide;
600}
601#endif
Tim Peters11b23062003-04-23 02:39:17 +0000602
Guido van Rossum21142a01999-01-08 21:05:37 +0000603static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605{
Mark Hammondef8b6542001-05-13 08:04:26 +0000606 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000608 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000611 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000612 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000614 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000615 return posix_error_with_allocated_filename(path1);
616 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000617 Py_INCREF(Py_None);
618 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Barry Warsaw53699e91996-12-10 23:23:01 +0000621static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000622posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625{
Mark Hammondef8b6542001-05-13 08:04:26 +0000626 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000627 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000628 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000629 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000630 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000632 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000633 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000634 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 PyMem_Free(path1);
636 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000637 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000638 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_INCREF(Py_None);
641 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644#ifdef Py_WIN_WIDE_FILENAMES
645static PyObject*
646win32_1str(PyObject* args, char* func,
647 char* format, BOOL (__stdcall *funcA)(LPCSTR),
648 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
649{
650 PyObject *uni;
651 char *ansi;
652 BOOL result;
653 if (unicode_file_names()) {
654 if (!PyArg_ParseTuple(args, wformat, &uni))
655 PyErr_Clear();
656 else {
657 Py_BEGIN_ALLOW_THREADS
658 result = funcW(PyUnicode_AsUnicode(uni));
659 Py_END_ALLOW_THREADS
660 if (!result)
661 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
662 Py_INCREF(Py_None);
663 return Py_None;
664 }
665 }
666 if (!PyArg_ParseTuple(args, format, &ansi))
667 return NULL;
668 Py_BEGIN_ALLOW_THREADS
669 result = funcA(ansi);
670 Py_END_ALLOW_THREADS
671 if (!result)
672 return win32_error(func, ansi);
673 Py_INCREF(Py_None);
674 return Py_None;
675
676}
677
678/* This is a reimplementation of the C library's chdir function,
679 but one that produces Win32 errors instead of DOS error codes.
680 chdir is essentially a wrapper around SetCurrentDirectory; however,
681 it also needs to set "magic" environment variables indicating
682 the per-drive current directory, which are of the form =<drive>: */
683BOOL __stdcall
684win32_chdir(LPCSTR path)
685{
686 char new_path[MAX_PATH+1];
687 int result;
688 char env[4] = "=x:";
689
690 if(!SetCurrentDirectoryA(path))
691 return FALSE;
692 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
693 if (!result)
694 return FALSE;
695 /* In the ANSI API, there should not be any paths longer
696 than MAX_PATH. */
697 assert(result <= MAX_PATH+1);
698 if (strncmp(new_path, "\\\\", 2) == 0 ||
699 strncmp(new_path, "//", 2) == 0)
700 /* UNC path, nothing to do. */
701 return TRUE;
702 env[1] = new_path[0];
703 return SetEnvironmentVariableA(env, new_path);
704}
705
706/* The Unicode version differs from the ANSI version
707 since the current directory might exceed MAX_PATH characters */
708BOOL __stdcall
709win32_wchdir(LPCWSTR path)
710{
711 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
712 int result;
713 wchar_t env[4] = L"=x:";
714
715 if(!SetCurrentDirectoryW(path))
716 return FALSE;
717 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
718 if (!result)
719 return FALSE;
720 if (result > MAX_PATH+1) {
721 new_path = malloc(result);
722 if (!new_path) {
723 SetLastError(ERROR_OUTOFMEMORY);
724 return FALSE;
725 }
726 }
727 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
728 wcsncmp(new_path, L"//", 2) == 0)
729 /* UNC path, nothing to do. */
730 return TRUE;
731 env[1] = new_path[0];
732 result = SetEnvironmentVariableW(env, new_path);
733 if (new_path != _new_path)
734 free(new_path);
735 return result;
736}
737#endif
738
Martin v. Löwis14694662006-02-03 12:54:16 +0000739#ifdef MS_WINDOWS
740/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
741 - time stamps are restricted to second resolution
742 - file modification times suffer from forth-and-back conversions between
743 UTC and local time
744 Therefore, we implement our own stat, based on the Win32 API directly.
745*/
746#define HAVE_STAT_NSEC 1
747
748struct win32_stat{
749 int st_dev;
750 __int64 st_ino;
751 unsigned short st_mode;
752 int st_nlink;
753 int st_uid;
754 int st_gid;
755 int st_rdev;
756 __int64 st_size;
757 int st_atime;
758 int st_atime_nsec;
759 int st_mtime;
760 int st_mtime_nsec;
761 int st_ctime;
762 int st_ctime_nsec;
763};
764
765static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
766
767static void
768FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
769{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000770 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
771 /* Cannot simply cast and dereference in_ptr,
772 since it might not be aligned properly */
773 __int64 in;
774 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000775 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
776 /* XXX Win32 supports time stamps past 2038; we currently don't */
777 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
778}
779
Thomas Wouters477c8d52006-05-27 19:21:47 +0000780static void
781time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
782{
783 /* XXX endianness */
784 __int64 out;
785 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000786 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 memcpy(out_ptr, &out, sizeof(out));
788}
789
Martin v. Löwis14694662006-02-03 12:54:16 +0000790/* Below, we *know* that ugo+r is 0444 */
791#if _S_IREAD != 0400
792#error Unsupported C library
793#endif
794static int
795attributes_to_mode(DWORD attr)
796{
797 int m = 0;
798 if (attr & FILE_ATTRIBUTE_DIRECTORY)
799 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
800 else
801 m |= _S_IFREG;
802 if (attr & FILE_ATTRIBUTE_READONLY)
803 m |= 0444;
804 else
805 m |= 0666;
806 return m;
807}
808
809static int
810attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
811{
812 memset(result, 0, sizeof(*result));
813 result->st_mode = attributes_to_mode(info->dwFileAttributes);
814 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
815 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
816 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
817 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
818
819 return 0;
820}
821
Thomas Wouters89f507f2006-12-13 04:49:30 +0000822/* Emulate GetFileAttributesEx[AW] on Windows 95 */
823static int checked = 0;
824static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
825static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
826static void
827check_gfax()
828{
829 HINSTANCE hKernel32;
830 if (checked)
831 return;
832 checked = 1;
833 hKernel32 = GetModuleHandle("KERNEL32");
834 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
835 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
836}
837
Guido van Rossumd8faa362007-04-27 19:54:29 +0000838static BOOL
839attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
840{
841 HANDLE hFindFile;
842 WIN32_FIND_DATAA FileData;
843 hFindFile = FindFirstFileA(pszFile, &FileData);
844 if (hFindFile == INVALID_HANDLE_VALUE)
845 return FALSE;
846 FindClose(hFindFile);
847 pfad->dwFileAttributes = FileData.dwFileAttributes;
848 pfad->ftCreationTime = FileData.ftCreationTime;
849 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
850 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
851 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
852 pfad->nFileSizeLow = FileData.nFileSizeLow;
853 return TRUE;
854}
855
856static BOOL
857attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
858{
859 HANDLE hFindFile;
860 WIN32_FIND_DATAW FileData;
861 hFindFile = FindFirstFileW(pszFile, &FileData);
862 if (hFindFile == INVALID_HANDLE_VALUE)
863 return FALSE;
864 FindClose(hFindFile);
865 pfad->dwFileAttributes = FileData.dwFileAttributes;
866 pfad->ftCreationTime = FileData.ftCreationTime;
867 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
868 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
869 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
870 pfad->nFileSizeLow = FileData.nFileSizeLow;
871 return TRUE;
872}
873
Thomas Wouters89f507f2006-12-13 04:49:30 +0000874static BOOL WINAPI
875Py_GetFileAttributesExA(LPCSTR pszFile,
876 GET_FILEEX_INFO_LEVELS level,
877 LPVOID pv)
878{
879 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000880 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
881 /* First try to use the system's implementation, if that is
882 available and either succeeds to gives an error other than
883 that it isn't implemented. */
884 check_gfax();
885 if (gfaxa) {
886 result = gfaxa(pszFile, level, pv);
887 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
888 return result;
889 }
890 /* It's either not present, or not implemented.
891 Emulate using FindFirstFile. */
892 if (level != GetFileExInfoStandard) {
893 SetLastError(ERROR_INVALID_PARAMETER);
894 return FALSE;
895 }
896 /* Use GetFileAttributes to validate that the file name
897 does not contain wildcards (which FindFirstFile would
898 accept). */
899 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
900 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000901 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000902}
903
904static BOOL WINAPI
905Py_GetFileAttributesExW(LPCWSTR pszFile,
906 GET_FILEEX_INFO_LEVELS level,
907 LPVOID pv)
908{
909 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
911 /* First try to use the system's implementation, if that is
912 available and either succeeds to gives an error other than
913 that it isn't implemented. */
914 check_gfax();
915 if (gfaxa) {
916 result = gfaxw(pszFile, level, pv);
917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
918 return result;
919 }
920 /* It's either not present, or not implemented.
921 Emulate using FindFirstFile. */
922 if (level != GetFileExInfoStandard) {
923 SetLastError(ERROR_INVALID_PARAMETER);
924 return FALSE;
925 }
926 /* Use GetFileAttributes to validate that the file name
927 does not contain wildcards (which FindFirstFile would
928 accept). */
929 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
930 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000931 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000932}
933
Martin v. Löwis14694662006-02-03 12:54:16 +0000934static int
935win32_stat(const char* path, struct win32_stat *result)
936{
937 WIN32_FILE_ATTRIBUTE_DATA info;
938 int code;
939 char *dot;
940 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000941 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942 if (GetLastError() != ERROR_SHARING_VIOLATION) {
943 /* Protocol violation: we explicitly clear errno, instead of
944 setting it to a POSIX error. Callers should use GetLastError. */
945 errno = 0;
946 return -1;
947 } else {
948 /* Could not get attributes on open file. Fall back to
949 reading the directory. */
950 if (!attributes_from_dir(path, &info)) {
951 /* Very strange. This should not fail now */
952 errno = 0;
953 return -1;
954 }
955 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000956 }
957 code = attribute_data_to_stat(&info, result);
958 if (code != 0)
959 return code;
960 /* Set S_IFEXEC if it is an .exe, .bat, ... */
961 dot = strrchr(path, '.');
962 if (dot) {
963 if (stricmp(dot, ".bat") == 0 ||
964 stricmp(dot, ".cmd") == 0 ||
965 stricmp(dot, ".exe") == 0 ||
966 stricmp(dot, ".com") == 0)
967 result->st_mode |= 0111;
968 }
969 return code;
970}
971
972static int
973win32_wstat(const wchar_t* path, struct win32_stat *result)
974{
975 int code;
976 const wchar_t *dot;
977 WIN32_FILE_ATTRIBUTE_DATA info;
978 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000979 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000980 if (GetLastError() != ERROR_SHARING_VIOLATION) {
981 /* Protocol violation: we explicitly clear errno, instead of
982 setting it to a POSIX error. Callers should use GetLastError. */
983 errno = 0;
984 return -1;
985 } else {
986 /* Could not get attributes on open file. Fall back to
987 reading the directory. */
988 if (!attributes_from_dir_w(path, &info)) {
989 /* Very strange. This should not fail now */
990 errno = 0;
991 return -1;
992 }
993 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 }
995 code = attribute_data_to_stat(&info, result);
996 if (code < 0)
997 return code;
998 /* Set IFEXEC if it is an .exe, .bat, ... */
999 dot = wcsrchr(path, '.');
1000 if (dot) {
1001 if (_wcsicmp(dot, L".bat") == 0 ||
1002 _wcsicmp(dot, L".cmd") == 0 ||
1003 _wcsicmp(dot, L".exe") == 0 ||
1004 _wcsicmp(dot, L".com") == 0)
1005 result->st_mode |= 0111;
1006 }
1007 return code;
1008}
1009
1010static int
1011win32_fstat(int file_number, struct win32_stat *result)
1012{
1013 BY_HANDLE_FILE_INFORMATION info;
1014 HANDLE h;
1015 int type;
1016
1017 h = (HANDLE)_get_osfhandle(file_number);
1018
1019 /* Protocol violation: we explicitly clear errno, instead of
1020 setting it to a POSIX error. Callers should use GetLastError. */
1021 errno = 0;
1022
1023 if (h == INVALID_HANDLE_VALUE) {
1024 /* This is really a C library error (invalid file handle).
1025 We set the Win32 error to the closes one matching. */
1026 SetLastError(ERROR_INVALID_HANDLE);
1027 return -1;
1028 }
1029 memset(result, 0, sizeof(*result));
1030
1031 type = GetFileType(h);
1032 if (type == FILE_TYPE_UNKNOWN) {
1033 DWORD error = GetLastError();
1034 if (error != 0) {
1035 return -1;
1036 }
1037 /* else: valid but unknown file */
1038 }
1039
1040 if (type != FILE_TYPE_DISK) {
1041 if (type == FILE_TYPE_CHAR)
1042 result->st_mode = _S_IFCHR;
1043 else if (type == FILE_TYPE_PIPE)
1044 result->st_mode = _S_IFIFO;
1045 return 0;
1046 }
1047
1048 if (!GetFileInformationByHandle(h, &info)) {
1049 return -1;
1050 }
1051
1052 /* similar to stat() */
1053 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1054 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1055 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1056 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1057 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1058 /* specific to fstat() */
1059 result->st_nlink = info.nNumberOfLinks;
1060 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1061 return 0;
1062}
1063
1064#endif /* MS_WINDOWS */
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001067"stat_result: Result from stat or lstat.\n\n\
1068This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001069 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1071\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001072Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1073or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001074\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076
1077static PyStructSequence_Field stat_result_fields[] = {
1078 {"st_mode", "protection bits"},
1079 {"st_ino", "inode"},
1080 {"st_dev", "device"},
1081 {"st_nlink", "number of hard links"},
1082 {"st_uid", "user ID of owner"},
1083 {"st_gid", "group ID of owner"},
1084 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001085 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1086 {NULL, "integer time of last access"},
1087 {NULL, "integer time of last modification"},
1088 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001089 {"st_atime", "time of last access"},
1090 {"st_mtime", "time of last modification"},
1091 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001092#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001093 {"st_blksize", "blocksize for filesystem I/O"},
1094#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001095#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001096 {"st_blocks", "number of blocks allocated"},
1097#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001098#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001099 {"st_rdev", "device type (if inode device)"},
1100#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001101#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1102 {"st_flags", "user defined flags for file"},
1103#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001104#ifdef HAVE_STRUCT_STAT_ST_GEN
1105 {"st_gen", "generation number"},
1106#endif
1107#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1108 {"st_birthtime", "time of creation"},
1109#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110 {0}
1111};
1112
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001113#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001114#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001116#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001117#endif
1118
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001119#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001120#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1121#else
1122#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1123#endif
1124
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001125#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1127#else
1128#define ST_RDEV_IDX ST_BLOCKS_IDX
1129#endif
1130
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001131#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1132#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1133#else
1134#define ST_FLAGS_IDX ST_RDEV_IDX
1135#endif
1136
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001137#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001138#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001139#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001140#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001141#endif
1142
1143#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1144#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1145#else
1146#define ST_BIRTHTIME_IDX ST_GEN_IDX
1147#endif
1148
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149static PyStructSequence_Desc stat_result_desc = {
1150 "stat_result", /* name */
1151 stat_result__doc__, /* doc */
1152 stat_result_fields,
1153 10
1154};
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1158This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001159 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001160or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001161\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001162See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163
1164static PyStructSequence_Field statvfs_result_fields[] = {
1165 {"f_bsize", },
1166 {"f_frsize", },
1167 {"f_blocks", },
1168 {"f_bfree", },
1169 {"f_bavail", },
1170 {"f_files", },
1171 {"f_ffree", },
1172 {"f_favail", },
1173 {"f_flag", },
1174 {"f_namemax",},
1175 {0}
1176};
1177
1178static PyStructSequence_Desc statvfs_result_desc = {
1179 "statvfs_result", /* name */
1180 statvfs_result__doc__, /* doc */
1181 statvfs_result_fields,
1182 10
1183};
1184
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001186static PyTypeObject StatResultType;
1187static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001188static newfunc structseq_new;
1189
1190static PyObject *
1191statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1192{
1193 PyStructSequence *result;
1194 int i;
1195
1196 result = (PyStructSequence*)structseq_new(type, args, kwds);
1197 if (!result)
1198 return NULL;
1199 /* If we have been initialized from a tuple,
1200 st_?time might be set to None. Initialize it
1201 from the int slots. */
1202 for (i = 7; i <= 9; i++) {
1203 if (result->ob_item[i+3] == Py_None) {
1204 Py_DECREF(Py_None);
1205 Py_INCREF(result->ob_item[i]);
1206 result->ob_item[i+3] = result->ob_item[i];
1207 }
1208 }
1209 return (PyObject*)result;
1210}
1211
1212
1213
1214/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001215static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001216
1217PyDoc_STRVAR(stat_float_times__doc__,
1218"stat_float_times([newval]) -> oldval\n\n\
1219Determine whether os.[lf]stat represents time stamps as float objects.\n\
1220If newval is True, future calls to stat() return floats, if it is False,\n\
1221future calls return ints. \n\
1222If newval is omitted, return the current setting.\n");
1223
1224static PyObject*
1225stat_float_times(PyObject* self, PyObject *args)
1226{
1227 int newval = -1;
1228 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1229 return NULL;
1230 if (newval == -1)
1231 /* Return old value */
1232 return PyBool_FromLong(_stat_float_times);
1233 _stat_float_times = newval;
1234 Py_INCREF(Py_None);
1235 return Py_None;
1236}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001237
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001238static void
1239fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1240{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001241 PyObject *fval,*ival;
1242#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001243 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001244#else
1245 ival = PyInt_FromLong((long)sec);
1246#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001247 if (!ival)
1248 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001249 if (_stat_float_times) {
1250 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1251 } else {
1252 fval = ival;
1253 Py_INCREF(fval);
1254 }
1255 PyStructSequence_SET_ITEM(v, index, ival);
1256 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001257}
1258
Tim Peters5aa91602002-01-30 05:46:57 +00001259/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001260 (used by posix_stat() and posix_fstat()) */
1261static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001262_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001263{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001264 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001265 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001266 if (v == NULL)
1267 return NULL;
1268
Martin v. Löwis14694662006-02-03 12:54:16 +00001269 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001270#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001271 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001272 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001273#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001274 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001275#endif
1276#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001277 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001278 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001279#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001280 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001281#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001282 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1283 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1284 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001285#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001286 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001287 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001288#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001289 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001290#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001291
Martin v. Löwis14694662006-02-03 12:54:16 +00001292#if defined(HAVE_STAT_TV_NSEC)
1293 ansec = st->st_atim.tv_nsec;
1294 mnsec = st->st_mtim.tv_nsec;
1295 cnsec = st->st_ctim.tv_nsec;
1296#elif defined(HAVE_STAT_TV_NSEC2)
1297 ansec = st->st_atimespec.tv_nsec;
1298 mnsec = st->st_mtimespec.tv_nsec;
1299 cnsec = st->st_ctimespec.tv_nsec;
1300#elif defined(HAVE_STAT_NSEC)
1301 ansec = st->st_atime_nsec;
1302 mnsec = st->st_mtime_nsec;
1303 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001304#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001305 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001306#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001307 fill_time(v, 7, st->st_atime, ansec);
1308 fill_time(v, 8, st->st_mtime, mnsec);
1309 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001311#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001312 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001313 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001315#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001316 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001318#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001319#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001320 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001322#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001323#ifdef HAVE_STRUCT_STAT_ST_GEN
1324 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001325 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001326#endif
1327#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1328 {
1329 PyObject *val;
1330 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001331 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001332#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001333 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#else
1335 bnsec = 0;
1336#endif
1337 if (_stat_float_times) {
1338 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1339 } else {
1340 val = PyInt_FromLong((long)bsec);
1341 }
1342 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1343 val);
1344 }
1345#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001346#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1347 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001348 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001349#endif
Fred Drake699f3522000-06-29 21:12:41 +00001350
1351 if (PyErr_Occurred()) {
1352 Py_DECREF(v);
1353 return NULL;
1354 }
1355
1356 return v;
1357}
1358
Martin v. Löwisd8948722004-06-02 09:57:56 +00001359#ifdef MS_WINDOWS
1360
1361/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1362 where / can be used in place of \ and the trailing slash is optional.
1363 Both SERVER and SHARE must have at least one character.
1364*/
1365
1366#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1367#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001368#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001369#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001370#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001371
Tim Peters4ad82172004-08-30 17:02:04 +00001372static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001373IsUNCRootA(char *path, int pathlen)
1374{
1375 #define ISSLASH ISSLASHA
1376
1377 int i, share;
1378
1379 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1380 /* minimum UNCRoot is \\x\y */
1381 return FALSE;
1382 for (i = 2; i < pathlen ; i++)
1383 if (ISSLASH(path[i])) break;
1384 if (i == 2 || i == pathlen)
1385 /* do not allow \\\SHARE or \\SERVER */
1386 return FALSE;
1387 share = i+1;
1388 for (i = share; i < pathlen; i++)
1389 if (ISSLASH(path[i])) break;
1390 return (i != share && (i == pathlen || i == pathlen-1));
1391
1392 #undef ISSLASH
1393}
1394
1395#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001396static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001397IsUNCRootW(Py_UNICODE *path, int pathlen)
1398{
1399 #define ISSLASH ISSLASHW
1400
1401 int i, share;
1402
1403 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1404 /* minimum UNCRoot is \\x\y */
1405 return FALSE;
1406 for (i = 2; i < pathlen ; i++)
1407 if (ISSLASH(path[i])) break;
1408 if (i == 2 || i == pathlen)
1409 /* do not allow \\\SHARE or \\SERVER */
1410 return FALSE;
1411 share = i+1;
1412 for (i = share; i < pathlen; i++)
1413 if (ISSLASH(path[i])) break;
1414 return (i != share && (i == pathlen || i == pathlen-1));
1415
1416 #undef ISSLASH
1417}
1418#endif /* Py_WIN_WIDE_FILENAMES */
1419#endif /* MS_WINDOWS */
1420
Barry Warsaw53699e91996-12-10 23:23:01 +00001421static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001422posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001423 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001424#ifdef __VMS
1425 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1426#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001427 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001428#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001429 char *wformat,
1430 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431{
Fred Drake699f3522000-06-29 21:12:41 +00001432 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001433 char *path = NULL; /* pass this to stat; do not free() it */
1434 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001435 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001436 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001437
1438#ifdef Py_WIN_WIDE_FILENAMES
1439 /* If on wide-character-capable OS see if argument
1440 is Unicode and if so use wide API. */
1441 if (unicode_file_names()) {
1442 PyUnicodeObject *po;
1443 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001444 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1445
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001446 Py_BEGIN_ALLOW_THREADS
1447 /* PyUnicode_AS_UNICODE result OK without
1448 thread lock as it is a simple dereference. */
1449 res = wstatfunc(wpath, &st);
1450 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001451
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001452 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001453 return win32_error_unicode("stat", wpath);
1454 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001455 }
1456 /* Drop the argument parsing error as narrow strings
1457 are also valid. */
1458 PyErr_Clear();
1459 }
1460#endif
1461
Tim Peters5aa91602002-01-30 05:46:57 +00001462 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001463 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001464 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001465 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001466
Barry Warsaw53699e91996-12-10 23:23:01 +00001467 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001468 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001469 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001470
1471 if (res != 0) {
1472#ifdef MS_WINDOWS
1473 result = win32_error("stat", pathfree);
1474#else
1475 result = posix_error_with_filename(pathfree);
1476#endif
1477 }
1478 else
1479 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001480
Tim Peters500bd032001-12-19 19:05:01 +00001481 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001482 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001483}
1484
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001485/* POSIX methods */
1486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001488"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001489Use the real uid/gid to test for access to a path. Note that most\n\
1490operations will use the effective uid/gid, therefore this routine can\n\
1491be used in a suid/sgid environment to test if the invoking user has the\n\
1492specified access to the path. The mode argument can be F_OK to test\n\
1493existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001494
1495static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001496posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001497{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001498 char *path;
1499 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001500
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001501#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001503 if (unicode_file_names()) {
1504 PyUnicodeObject *po;
1505 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1506 Py_BEGIN_ALLOW_THREADS
1507 /* PyUnicode_AS_UNICODE OK without thread lock as
1508 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001509 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001510 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001511 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001512 }
1513 /* Drop the argument parsing error as narrow strings
1514 are also valid. */
1515 PyErr_Clear();
1516 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001517 if (!PyArg_ParseTuple(args, "eti:access",
1518 Py_FileSystemDefaultEncoding, &path, &mode))
1519 return 0;
1520 Py_BEGIN_ALLOW_THREADS
1521 attr = GetFileAttributesA(path);
1522 Py_END_ALLOW_THREADS
1523 PyMem_Free(path);
1524finish:
1525 if (attr == 0xFFFFFFFF)
1526 /* File does not exist, or cannot read attributes */
1527 return PyBool_FromLong(0);
1528 /* Access is possible if either write access wasn't requested, or
1529 the file isn't read-only. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001530 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001531#else
1532 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001533 if (!PyArg_ParseTuple(args, "eti:access",
1534 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001535 return NULL;
1536 Py_BEGIN_ALLOW_THREADS
1537 res = access(path, mode);
1538 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001539 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001540 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001542}
1543
Guido van Rossumd371ff11999-01-25 16:12:23 +00001544#ifndef F_OK
1545#define F_OK 0
1546#endif
1547#ifndef R_OK
1548#define R_OK 4
1549#endif
1550#ifndef W_OK
1551#define W_OK 2
1552#endif
1553#ifndef X_OK
1554#define X_OK 1
1555#endif
1556
1557#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001558PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001559"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001561
1562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001563posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001564{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001565 int id;
1566 char *ret;
1567
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001568 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001569 return NULL;
1570
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001571#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001572 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001573 if (id == 0) {
1574 ret = ttyname();
1575 }
1576 else {
1577 ret = NULL;
1578 }
1579#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001580 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001581#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001582 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001583 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001584 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001585}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001587
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001588#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001590"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001592
1593static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001594posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001595{
1596 char *ret;
1597 char buffer[L_ctermid];
1598
Greg Wardb48bc172000-03-01 21:51:56 +00001599#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600 ret = ctermid_r(buffer);
1601#else
1602 ret = ctermid(buffer);
1603#endif
1604 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001605 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001606 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607}
1608#endif
1609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001610PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001611"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001613
Barry Warsaw53699e91996-12-10 23:23:01 +00001614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001615posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001616{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001617#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001619#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001621#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001625#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626}
1627
Fred Drake4d1e64b2002-04-15 19:40:07 +00001628#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001630"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001631Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001632opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001633
1634static PyObject *
1635posix_fchdir(PyObject *self, PyObject *fdobj)
1636{
1637 return posix_fildes(fdobj, fchdir);
1638}
1639#endif /* HAVE_FCHDIR */
1640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001642PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001643"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001644Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001645
Barry Warsaw53699e91996-12-10 23:23:01 +00001646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001647posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648{
Mark Hammondef8b6542001-05-13 08:04:26 +00001649 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001650 int i;
1651 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001652#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001654 if (unicode_file_names()) {
1655 PyUnicodeObject *po;
1656 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1657 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001658 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1659 if (attr != 0xFFFFFFFF) {
1660 if (i & _S_IWRITE)
1661 attr &= ~FILE_ATTRIBUTE_READONLY;
1662 else
1663 attr |= FILE_ATTRIBUTE_READONLY;
1664 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1665 }
1666 else
1667 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001668 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001669 if (!res)
1670 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001671 PyUnicode_AS_UNICODE(po));
1672 Py_INCREF(Py_None);
1673 return Py_None;
1674 }
1675 /* Drop the argument parsing error as narrow strings
1676 are also valid. */
1677 PyErr_Clear();
1678 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001679 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1680 &path, &i))
1681 return NULL;
1682 Py_BEGIN_ALLOW_THREADS
1683 attr = GetFileAttributesA(path);
1684 if (attr != 0xFFFFFFFF) {
1685 if (i & _S_IWRITE)
1686 attr &= ~FILE_ATTRIBUTE_READONLY;
1687 else
1688 attr |= FILE_ATTRIBUTE_READONLY;
1689 res = SetFileAttributesA(path, attr);
1690 }
1691 else
1692 res = 0;
1693 Py_END_ALLOW_THREADS
1694 if (!res) {
1695 win32_error("chmod", path);
1696 PyMem_Free(path);
1697 return NULL;
1698 }
1699 PyMem_Free(path);
1700 Py_INCREF(Py_None);
1701 return Py_None;
1702#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001703 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001704 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001705 return NULL;
1706 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001707 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001708 Py_END_ALLOW_THREADS
1709 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001710 return posix_error_with_allocated_filename(path);
1711 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001712 Py_INCREF(Py_None);
1713 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001714#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001715}
1716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001717
Thomas Wouterscf297e42007-02-23 15:07:44 +00001718#ifdef HAVE_CHFLAGS
1719PyDoc_STRVAR(posix_chflags__doc__,
1720"chflags(path, flags)\n\n\
1721Set file flags.");
1722
1723static PyObject *
1724posix_chflags(PyObject *self, PyObject *args)
1725{
1726 char *path;
1727 unsigned long flags;
1728 int res;
1729 if (!PyArg_ParseTuple(args, "etk:chflags",
1730 Py_FileSystemDefaultEncoding, &path, &flags))
1731 return NULL;
1732 Py_BEGIN_ALLOW_THREADS
1733 res = chflags(path, flags);
1734 Py_END_ALLOW_THREADS
1735 if (res < 0)
1736 return posix_error_with_allocated_filename(path);
1737 PyMem_Free(path);
1738 Py_INCREF(Py_None);
1739 return Py_None;
1740}
1741#endif /* HAVE_CHFLAGS */
1742
1743#ifdef HAVE_LCHFLAGS
1744PyDoc_STRVAR(posix_lchflags__doc__,
1745"lchflags(path, flags)\n\n\
1746Set file flags.\n\
1747This function will not follow symbolic links.");
1748
1749static PyObject *
1750posix_lchflags(PyObject *self, PyObject *args)
1751{
1752 char *path;
1753 unsigned long flags;
1754 int res;
1755 if (!PyArg_ParseTuple(args, "etk:lchflags",
1756 Py_FileSystemDefaultEncoding, &path, &flags))
1757 return NULL;
1758 Py_BEGIN_ALLOW_THREADS
1759 res = lchflags(path, flags);
1760 Py_END_ALLOW_THREADS
1761 if (res < 0)
1762 return posix_error_with_allocated_filename(path);
1763 PyMem_Free(path);
1764 Py_INCREF(Py_None);
1765 return Py_None;
1766}
1767#endif /* HAVE_LCHFLAGS */
1768
Martin v. Löwis244edc82001-10-04 22:44:26 +00001769#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001771"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001773
1774static PyObject *
1775posix_chroot(PyObject *self, PyObject *args)
1776{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001777 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001778}
1779#endif
1780
Guido van Rossum21142a01999-01-08 21:05:37 +00001781#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001783"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001784force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001785
1786static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001787posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001788{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001789 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001790}
1791#endif /* HAVE_FSYNC */
1792
1793#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001794
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001795#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001796extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1797#endif
1798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001801force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001803
1804static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001805posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001806{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001807 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001808}
1809#endif /* HAVE_FDATASYNC */
1810
1811
Fredrik Lundh10723342000-07-10 16:38:09 +00001812#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001813PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001814"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001816
Barry Warsaw53699e91996-12-10 23:23:01 +00001817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001818posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001819{
Mark Hammondef8b6542001-05-13 08:04:26 +00001820 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001821 int uid, gid;
1822 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001823 if (!PyArg_ParseTuple(args, "etii:chown",
1824 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001825 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001826 return NULL;
1827 Py_BEGIN_ALLOW_THREADS
1828 res = chown(path, (uid_t) uid, (gid_t) gid);
1829 Py_END_ALLOW_THREADS
1830 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001831 return posix_error_with_allocated_filename(path);
1832 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001833 Py_INCREF(Py_None);
1834 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001835}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001836#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001837
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001838#ifdef HAVE_LCHOWN
1839PyDoc_STRVAR(posix_lchown__doc__,
1840"lchown(path, uid, gid)\n\n\
1841Change the owner and group id of path to the numeric uid and gid.\n\
1842This function will not follow symbolic links.");
1843
1844static PyObject *
1845posix_lchown(PyObject *self, PyObject *args)
1846{
1847 char *path = NULL;
1848 int uid, gid;
1849 int res;
1850 if (!PyArg_ParseTuple(args, "etii:lchown",
1851 Py_FileSystemDefaultEncoding, &path,
1852 &uid, &gid))
1853 return NULL;
1854 Py_BEGIN_ALLOW_THREADS
1855 res = lchown(path, (uid_t) uid, (gid_t) gid);
1856 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001857 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001858 return posix_error_with_allocated_filename(path);
1859 PyMem_Free(path);
1860 Py_INCREF(Py_None);
1861 return Py_None;
1862}
1863#endif /* HAVE_LCHOWN */
1864
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001865
Guido van Rossum36bc6801995-06-14 22:54:23 +00001866#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001868"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Barry Warsaw53699e91996-12-10 23:23:01 +00001871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001872posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001873{
1874 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001875 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001878#if defined(PYOS_OS2) && defined(PYCC_GCC)
1879 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001880#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001881 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001882#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001883 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001884 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001886 return PyUnicode_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001887}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001888
1889PyDoc_STRVAR(posix_getcwdu__doc__,
1890"getcwdu() -> path\n\n\
1891Return a unicode string representing the current working directory.");
1892
1893static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001894posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001895{
1896 char buf[1026];
1897 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001898
1899#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001900 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001901 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001902 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903 wchar_t *wbuf2 = wbuf;
1904 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001905 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1907 /* If the buffer is large enough, len does not include the
1908 terminating \0. If the buffer is too small, len includes
1909 the space needed for the terminator. */
1910 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1911 wbuf2 = malloc(len * sizeof(wchar_t));
1912 if (wbuf2)
1913 len = GetCurrentDirectoryW(len, wbuf2);
1914 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001915 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916 if (!wbuf2) {
1917 PyErr_NoMemory();
1918 return NULL;
1919 }
1920 if (!len) {
1921 if (wbuf2 != wbuf) free(wbuf2);
1922 return win32_error("getcwdu", NULL);
1923 }
1924 resobj = PyUnicode_FromWideChar(wbuf2, len);
1925 if (wbuf2 != wbuf) free(wbuf2);
1926 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001927 }
1928#endif
1929
1930 Py_BEGIN_ALLOW_THREADS
1931#if defined(PYOS_OS2) && defined(PYCC_GCC)
1932 res = _getcwd2(buf, sizeof buf);
1933#else
1934 res = getcwd(buf, sizeof buf);
1935#endif
1936 Py_END_ALLOW_THREADS
1937 if (res == NULL)
1938 return posix_error();
1939 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1940}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001941#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001943
Guido van Rossumb6775db1994-08-01 11:34:53 +00001944#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001946"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001948
Barry Warsaw53699e91996-12-10 23:23:01 +00001949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001950posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001951{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001952 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001953}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001954#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001958"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959Return a list containing the names of the entries in the directory.\n\
1960\n\
1961 path: path of directory to list\n\
1962\n\
1963The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001964entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001965
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001967posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001968{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001969 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001970 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001971#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001972
Barry Warsaw53699e91996-12-10 23:23:01 +00001973 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001974 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001975 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001978 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001979 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001980
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#ifdef Py_WIN_WIDE_FILENAMES
1982 /* If on wide-character-capable OS see if argument
1983 is Unicode and if so use wide API. */
1984 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001985 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001986 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1987 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001988 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990 /* Overallocate for \\*.*\0 */
1991 len = PyUnicode_GET_SIZE(po);
1992 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1993 if (!wnamebuf) {
1994 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001996 }
1997 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1998 wch = len > 0 ? wnamebuf[len-1] : '\0';
1999 if (wch != L'/' && wch != L'\\' && wch != L':')
2000 wnamebuf[len++] = L'\\';
2001 wcscpy(wnamebuf + len, L"*.*");
2002 if ((d = PyList_New(0)) == NULL) {
2003 free(wnamebuf);
2004 return NULL;
2005 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2007 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002008 int error = GetLastError();
2009 if (error == ERROR_FILE_NOT_FOUND) {
2010 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011 return d;
2012 }
2013 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002014 win32_error_unicode("FindFirstFileW", wnamebuf);
2015 free(wnamebuf);
2016 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017 }
2018 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002019 /* Skip over . and .. */
2020 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2021 wcscmp(wFileData.cFileName, L"..") != 0) {
2022 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2023 if (v == NULL) {
2024 Py_DECREF(d);
2025 d = NULL;
2026 break;
2027 }
2028 if (PyList_Append(d, v) != 0) {
2029 Py_DECREF(v);
2030 Py_DECREF(d);
2031 d = NULL;
2032 break;
2033 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002035 }
Georg Brandl622927b2006-03-07 12:48:03 +00002036 Py_BEGIN_ALLOW_THREADS
2037 result = FindNextFileW(hFindFile, &wFileData);
2038 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002039 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2040 it got to the end of the directory. */
2041 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2042 Py_DECREF(d);
2043 win32_error_unicode("FindNextFileW", wnamebuf);
2044 FindClose(hFindFile);
2045 free(wnamebuf);
2046 return NULL;
2047 }
Georg Brandl622927b2006-03-07 12:48:03 +00002048 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002049
2050 if (FindClose(hFindFile) == FALSE) {
2051 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002052 win32_error_unicode("FindClose", wnamebuf);
2053 free(wnamebuf);
2054 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002055 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002056 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002057 return d;
2058 }
2059 /* Drop the argument parsing error as narrow strings
2060 are also valid. */
2061 PyErr_Clear();
2062 }
2063#endif
2064
Tim Peters5aa91602002-01-30 05:46:57 +00002065 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002066 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002067 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002068 if (len > 0) {
2069 char ch = namebuf[len-1];
2070 if (ch != SEP && ch != ALTSEP && ch != ':')
2071 namebuf[len++] = '/';
2072 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002073 strcpy(namebuf + len, "*.*");
2074
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002076 return NULL;
2077
2078 hFindFile = FindFirstFile(namebuf, &FileData);
2079 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080 int error = GetLastError();
2081 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002082 return d;
2083 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002084 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085 }
2086 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002087 /* Skip over . and .. */
2088 if (strcmp(FileData.cFileName, ".") != 0 &&
2089 strcmp(FileData.cFileName, "..") != 0) {
2090 v = PyString_FromString(FileData.cFileName);
2091 if (v == NULL) {
2092 Py_DECREF(d);
2093 d = NULL;
2094 break;
2095 }
2096 if (PyList_Append(d, v) != 0) {
2097 Py_DECREF(v);
2098 Py_DECREF(d);
2099 d = NULL;
2100 break;
2101 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103 }
Georg Brandl622927b2006-03-07 12:48:03 +00002104 Py_BEGIN_ALLOW_THREADS
2105 result = FindNextFile(hFindFile, &FileData);
2106 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002107 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2108 it got to the end of the directory. */
2109 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2110 Py_DECREF(d);
2111 win32_error("FindNextFile", namebuf);
2112 FindClose(hFindFile);
2113 return NULL;
2114 }
Georg Brandl622927b2006-03-07 12:48:03 +00002115 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002116
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002117 if (FindClose(hFindFile) == FALSE) {
2118 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002119 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002120 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002121
2122 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002123
Tim Peters0bb44a42000-09-15 07:44:49 +00002124#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002125
2126#ifndef MAX_PATH
2127#define MAX_PATH CCHMAXPATH
2128#endif
2129 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002130 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002131 PyObject *d, *v;
2132 char namebuf[MAX_PATH+5];
2133 HDIR hdir = 1;
2134 ULONG srchcnt = 1;
2135 FILEFINDBUF3 ep;
2136 APIRET rc;
2137
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002138 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002139 return NULL;
2140 if (len >= MAX_PATH) {
2141 PyErr_SetString(PyExc_ValueError, "path too long");
2142 return NULL;
2143 }
2144 strcpy(namebuf, name);
2145 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002146 if (*pt == ALTSEP)
2147 *pt = SEP;
2148 if (namebuf[len-1] != SEP)
2149 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002150 strcpy(namebuf + len, "*.*");
2151
2152 if ((d = PyList_New(0)) == NULL)
2153 return NULL;
2154
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002155 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2156 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002157 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002158 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2159 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2160 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002161
2162 if (rc != NO_ERROR) {
2163 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002164 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002165 }
2166
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002167 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002168 do {
2169 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002170 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002171 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002172
2173 strcpy(namebuf, ep.achName);
2174
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002175 /* Leave Case of Name Alone -- In Native Form */
2176 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002177
2178 v = PyString_FromString(namebuf);
2179 if (v == NULL) {
2180 Py_DECREF(d);
2181 d = NULL;
2182 break;
2183 }
2184 if (PyList_Append(d, v) != 0) {
2185 Py_DECREF(v);
2186 Py_DECREF(d);
2187 d = NULL;
2188 break;
2189 }
2190 Py_DECREF(v);
2191 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2192 }
2193
2194 return d;
2195#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002196
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002197 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002198 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002199 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002200 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002201 int arg_is_unicode = 1;
2202
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002203 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002204 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2205 arg_is_unicode = 0;
2206 PyErr_Clear();
2207 }
2208 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002209 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002210 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002211 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002212 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002213 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002214 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002215 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002216 return NULL;
2217 }
Georg Brandl622927b2006-03-07 12:48:03 +00002218 for (;;) {
2219 Py_BEGIN_ALLOW_THREADS
2220 ep = readdir(dirp);
2221 Py_END_ALLOW_THREADS
2222 if (ep == NULL)
2223 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002224 if (ep->d_name[0] == '.' &&
2225 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002226 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002227 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002229 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002230 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002231 d = NULL;
2232 break;
2233 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002234 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002235 PyObject *w;
2236
2237 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002238 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002239 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002240 if (w != NULL) {
2241 Py_DECREF(v);
2242 v = w;
2243 }
2244 else {
2245 /* fall back to the original byte string, as
2246 discussed in patch #683592 */
2247 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002248 }
Just van Rossum46c97842003-02-25 21:42:15 +00002249 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002250 if (PyList_Append(d, v) != 0) {
2251 Py_DECREF(v);
2252 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002253 d = NULL;
2254 break;
2255 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002256 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002257 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002258 if (errno != 0 && d != NULL) {
2259 /* readdir() returned NULL and set errno */
2260 closedir(dirp);
2261 Py_DECREF(d);
2262 return posix_error_with_allocated_filename(name);
2263 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002264 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002265 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002266
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002267 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002268
Tim Peters0bb44a42000-09-15 07:44:49 +00002269#endif /* which OS */
2270} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002271
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002272#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002273/* A helper function for abspath on win32 */
2274static PyObject *
2275posix__getfullpathname(PyObject *self, PyObject *args)
2276{
2277 /* assume encoded strings wont more than double no of chars */
2278 char inbuf[MAX_PATH*2];
2279 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002280 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002281 char outbuf[MAX_PATH*2];
2282 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002283#ifdef Py_WIN_WIDE_FILENAMES
2284 if (unicode_file_names()) {
2285 PyUnicodeObject *po;
2286 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2287 Py_UNICODE woutbuf[MAX_PATH*2];
2288 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002289 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002290 sizeof(woutbuf)/sizeof(woutbuf[0]),
2291 woutbuf, &wtemp))
2292 return win32_error("GetFullPathName", "");
2293 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2294 }
2295 /* Drop the argument parsing error as narrow strings
2296 are also valid. */
2297 PyErr_Clear();
2298 }
2299#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002300 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2301 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002302 &insize))
2303 return NULL;
2304 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2305 outbuf, &temp))
2306 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002307 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2308 return PyUnicode_Decode(outbuf, strlen(outbuf),
2309 Py_FileSystemDefaultEncoding, NULL);
2310 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002311 return PyString_FromString(outbuf);
2312} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002313#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002314
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002315PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002316"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002317Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002318
Barry Warsaw53699e91996-12-10 23:23:01 +00002319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002320posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002321{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002322 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002323 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002324 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002325
2326#ifdef Py_WIN_WIDE_FILENAMES
2327 if (unicode_file_names()) {
2328 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002329 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330 Py_BEGIN_ALLOW_THREADS
2331 /* PyUnicode_AS_UNICODE OK without thread lock as
2332 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002333 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002334 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002335 if (!res)
2336 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002337 Py_INCREF(Py_None);
2338 return Py_None;
2339 }
2340 /* Drop the argument parsing error as narrow strings
2341 are also valid. */
2342 PyErr_Clear();
2343 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002344 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2345 Py_FileSystemDefaultEncoding, &path, &mode))
2346 return NULL;
2347 Py_BEGIN_ALLOW_THREADS
2348 /* PyUnicode_AS_UNICODE OK without thread lock as
2349 it is a simple dereference. */
2350 res = CreateDirectoryA(path, NULL);
2351 Py_END_ALLOW_THREADS
2352 if (!res) {
2353 win32_error("mkdir", path);
2354 PyMem_Free(path);
2355 return NULL;
2356 }
2357 PyMem_Free(path);
2358 Py_INCREF(Py_None);
2359 return Py_None;
2360#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002361
Tim Peters5aa91602002-01-30 05:46:57 +00002362 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002363 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002364 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002365 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002366#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002367 res = mkdir(path);
2368#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002369 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002370#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002371 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002372 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002373 return posix_error_with_allocated_filename(path);
2374 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002375 Py_INCREF(Py_None);
2376 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002377#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378}
2379
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002380
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002381/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2382#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002383#include <sys/resource.h>
2384#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002385
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386
2387#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002388PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002389"nice(inc) -> new_priority\n\n\
2390Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002391
Barry Warsaw53699e91996-12-10 23:23:01 +00002392static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002393posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002394{
2395 int increment, value;
2396
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002397 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002398 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002399
2400 /* There are two flavours of 'nice': one that returns the new
2401 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002402 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2403 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002404
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002405 If we are of the nice family that returns the new priority, we
2406 need to clear errno before the call, and check if errno is filled
2407 before calling posix_error() on a returnvalue of -1, because the
2408 -1 may be the actual new priority! */
2409
2410 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002411 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002412#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002413 if (value == 0)
2414 value = getpriority(PRIO_PROCESS, 0);
2415#endif
2416 if (value == -1 && errno != 0)
2417 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002418 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002419 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002420}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002421#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002423PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002424"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002425Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002426
Barry Warsaw53699e91996-12-10 23:23:01 +00002427static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002428posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002429{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002430#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002431 PyObject *o1, *o2;
2432 char *p1, *p2;
2433 BOOL result;
2434 if (unicode_file_names()) {
2435 if (!PyArg_ParseTuple(args, "O&O&:rename",
2436 convert_to_unicode, &o1,
2437 convert_to_unicode, &o2))
2438 PyErr_Clear();
2439 else {
2440 Py_BEGIN_ALLOW_THREADS
2441 result = MoveFileW(PyUnicode_AsUnicode(o1),
2442 PyUnicode_AsUnicode(o2));
2443 Py_END_ALLOW_THREADS
2444 Py_DECREF(o1);
2445 Py_DECREF(o2);
2446 if (!result)
2447 return win32_error("rename", NULL);
2448 Py_INCREF(Py_None);
2449 return Py_None;
2450 }
2451 }
2452 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2453 return NULL;
2454 Py_BEGIN_ALLOW_THREADS
2455 result = MoveFileA(p1, p2);
2456 Py_END_ALLOW_THREADS
2457 if (!result)
2458 return win32_error("rename", NULL);
2459 Py_INCREF(Py_None);
2460 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002461#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002462 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002463#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002464}
2465
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002467PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002468"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002469Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002470
Barry Warsaw53699e91996-12-10 23:23:01 +00002471static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002472posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002473{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002474#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002475 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002476#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002477 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002478#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002479}
2480
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002482PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002483"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002484Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002485
Barry Warsaw53699e91996-12-10 23:23:01 +00002486static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002487posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002488{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002489#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002490 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002491#else
2492 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2493#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002494}
2495
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002496
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002497#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002498PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002499"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002500Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501
Barry Warsaw53699e91996-12-10 23:23:01 +00002502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002503posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002504{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002505 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002506 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002507 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002508 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002509 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002510 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002511 Py_END_ALLOW_THREADS
2512 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002513}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002514#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002517PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002518"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002519Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002520
Barry Warsaw53699e91996-12-10 23:23:01 +00002521static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002522posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002523{
2524 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002525 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002526 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002527 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002528 if (i < 0)
2529 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002530 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002531}
2532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002535"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002539"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002540Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541
Barry Warsaw53699e91996-12-10 23:23:01 +00002542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002543posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002544{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002545#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002546 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002547#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002548 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002549#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002550}
2551
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002552
Guido van Rossumb6775db1994-08-01 11:34:53 +00002553#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002554PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002555"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002556Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002557
Barry Warsaw53699e91996-12-10 23:23:01 +00002558static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002559posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002560{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002561 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002562 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002563
Barry Warsaw53699e91996-12-10 23:23:01 +00002564 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002565 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002566 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002567 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002568 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002569 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002570 u.sysname,
2571 u.nodename,
2572 u.release,
2573 u.version,
2574 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002575}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002576#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002577
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002578static int
2579extract_time(PyObject *t, long* sec, long* usec)
2580{
2581 long intval;
2582 if (PyFloat_Check(t)) {
2583 double tval = PyFloat_AsDouble(t);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002584 PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002585 if (!intobj)
2586 return -1;
2587 intval = PyInt_AsLong(intobj);
2588 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002589 if (intval == -1 && PyErr_Occurred())
2590 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002591 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002592 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002593 if (*usec < 0)
2594 /* If rounding gave us a negative number,
2595 truncate. */
2596 *usec = 0;
2597 return 0;
2598 }
2599 intval = PyInt_AsLong(t);
2600 if (intval == -1 && PyErr_Occurred())
2601 return -1;
2602 *sec = intval;
2603 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002604 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002605}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002607PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002608"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002609utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002610Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002612
Barry Warsaw53699e91996-12-10 23:23:01 +00002613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002614posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002615{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002616#ifdef Py_WIN_WIDE_FILENAMES
2617 PyObject *arg;
2618 PyUnicodeObject *obwpath;
2619 wchar_t *wpath = NULL;
2620 char *apath = NULL;
2621 HANDLE hFile;
2622 long atimesec, mtimesec, ausec, musec;
2623 FILETIME atime, mtime;
2624 PyObject *result = NULL;
2625
2626 if (unicode_file_names()) {
2627 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2628 wpath = PyUnicode_AS_UNICODE(obwpath);
2629 Py_BEGIN_ALLOW_THREADS
2630 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002631 NULL, OPEN_EXISTING,
2632 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002633 Py_END_ALLOW_THREADS
2634 if (hFile == INVALID_HANDLE_VALUE)
2635 return win32_error_unicode("utime", wpath);
2636 } else
2637 /* Drop the argument parsing error as narrow strings
2638 are also valid. */
2639 PyErr_Clear();
2640 }
2641 if (!wpath) {
2642 if (!PyArg_ParseTuple(args, "etO:utime",
2643 Py_FileSystemDefaultEncoding, &apath, &arg))
2644 return NULL;
2645 Py_BEGIN_ALLOW_THREADS
2646 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002647 NULL, OPEN_EXISTING,
2648 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002649 Py_END_ALLOW_THREADS
2650 if (hFile == INVALID_HANDLE_VALUE) {
2651 win32_error("utime", apath);
2652 PyMem_Free(apath);
2653 return NULL;
2654 }
2655 PyMem_Free(apath);
2656 }
2657
2658 if (arg == Py_None) {
2659 SYSTEMTIME now;
2660 GetSystemTime(&now);
2661 if (!SystemTimeToFileTime(&now, &mtime) ||
2662 !SystemTimeToFileTime(&now, &atime)) {
2663 win32_error("utime", NULL);
2664 goto done;
2665 }
2666 }
2667 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2668 PyErr_SetString(PyExc_TypeError,
2669 "utime() arg 2 must be a tuple (atime, mtime)");
2670 goto done;
2671 }
2672 else {
2673 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2674 &atimesec, &ausec) == -1)
2675 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002676 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002677 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2678 &mtimesec, &musec) == -1)
2679 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002680 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002681 }
2682 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2683 /* Avoid putting the file name into the error here,
2684 as that may confuse the user into believing that
2685 something is wrong with the file, when it also
2686 could be the time stamp that gives a problem. */
2687 win32_error("utime", NULL);
2688 }
2689 Py_INCREF(Py_None);
2690 result = Py_None;
2691done:
2692 CloseHandle(hFile);
2693 return result;
2694#else /* Py_WIN_WIDE_FILENAMES */
2695
Neal Norwitz2adf2102004-06-09 01:46:02 +00002696 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002697 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002698 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002699 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002700
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002701#if defined(HAVE_UTIMES)
2702 struct timeval buf[2];
2703#define ATIME buf[0].tv_sec
2704#define MTIME buf[1].tv_sec
2705#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002706/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002707 struct utimbuf buf;
2708#define ATIME buf.actime
2709#define MTIME buf.modtime
2710#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002711#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002712 time_t buf[2];
2713#define ATIME buf[0]
2714#define MTIME buf[1]
2715#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002717
Mark Hammond817c9292003-12-03 01:22:38 +00002718
Thomas Wouters477c8d52006-05-27 19:21:47 +00002719 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002720 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002721 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002722 if (arg == Py_None) {
2723 /* optional time values not given */
2724 Py_BEGIN_ALLOW_THREADS
2725 res = utime(path, NULL);
2726 Py_END_ALLOW_THREADS
2727 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002728 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002729 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002730 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002731 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002732 return NULL;
2733 }
2734 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002735 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002736 &atime, &ausec) == -1) {
2737 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002738 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002739 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002740 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002741 &mtime, &musec) == -1) {
2742 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002743 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002744 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002745 ATIME = atime;
2746 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002747#ifdef HAVE_UTIMES
2748 buf[0].tv_usec = ausec;
2749 buf[1].tv_usec = musec;
2750 Py_BEGIN_ALLOW_THREADS
2751 res = utimes(path, buf);
2752 Py_END_ALLOW_THREADS
2753#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002754 Py_BEGIN_ALLOW_THREADS
2755 res = utime(path, UTIME_ARG);
2756 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002757#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002758 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002759 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002760 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002761 }
Neal Norwitz96652712004-06-06 20:40:27 +00002762 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002763 Py_INCREF(Py_None);
2764 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002765#undef UTIME_ARG
2766#undef ATIME
2767#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002768#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002769}
2770
Guido van Rossum85e3b011991-06-03 12:42:10 +00002771
Guido van Rossum3b066191991-06-04 19:40:25 +00002772/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002775"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002779posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002780{
2781 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002782 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002783 return NULL;
2784 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002785 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002786}
2787
Martin v. Löwis114619e2002-10-07 06:44:21 +00002788#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2789static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002790free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002791{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002792 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002793 for (i = 0; i < count; i++)
2794 PyMem_Free(array[i]);
2795 PyMem_DEL(array);
2796}
2797#endif
2798
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002799
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002800#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002801PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002802"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002803Execute an executable path with arguments, replacing current process.\n\
2804\n\
2805 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002807
Barry Warsaw53699e91996-12-10 23:23:01 +00002808static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002809posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002810{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002811 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002812 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002813 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002814 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002815 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002816
Guido van Rossum89b33251993-10-22 14:26:06 +00002817 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002818 argv is a list or tuple of strings. */
2819
Martin v. Löwis114619e2002-10-07 06:44:21 +00002820 if (!PyArg_ParseTuple(args, "etO:execv",
2821 Py_FileSystemDefaultEncoding,
2822 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002823 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002824 if (PyList_Check(argv)) {
2825 argc = PyList_Size(argv);
2826 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002827 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002828 else if (PyTuple_Check(argv)) {
2829 argc = PyTuple_Size(argv);
2830 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002831 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002832 else {
Fred Drake661ea262000-10-24 19:57:45 +00002833 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002834 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002835 return NULL;
2836 }
Thomas Heller6790d602007-08-30 17:15:14 +00002837 if (argc < 1) {
2838 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2839 PyMem_Free(path);
2840 return NULL;
2841 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002842
Barry Warsaw53699e91996-12-10 23:23:01 +00002843 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002844 if (argvlist == NULL) {
2845 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002846 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002847 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002848 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002849 if (!PyArg_Parse((*getitem)(argv, i), "et",
2850 Py_FileSystemDefaultEncoding,
2851 &argvlist[i])) {
2852 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002853 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002854 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002855 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002856 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002857
Guido van Rossum85e3b011991-06-03 12:42:10 +00002858 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002859 }
2860 argvlist[argc] = NULL;
2861
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002862 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002863
Guido van Rossum85e3b011991-06-03 12:42:10 +00002864 /* If we get here it's definitely an error */
2865
Martin v. Löwis114619e2002-10-07 06:44:21 +00002866 free_string_array(argvlist, argc);
2867 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002868 return posix_error();
2869}
2870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002872PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002873"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874Execute a path with arguments and environment, replacing current process.\n\
2875\n\
2876 path: path of executable file\n\
2877 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002878 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002879
Barry Warsaw53699e91996-12-10 23:23:01 +00002880static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002881posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002882{
2883 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002884 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002885 char **argvlist;
2886 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002887 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002888 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002889 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002890 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002891
2892 /* execve has three arguments: (path, argv, env), where
2893 argv is a list or tuple of strings and env is a dictionary
2894 like posix.environ. */
2895
Martin v. Löwis114619e2002-10-07 06:44:21 +00002896 if (!PyArg_ParseTuple(args, "etOO:execve",
2897 Py_FileSystemDefaultEncoding,
2898 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002899 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002900 if (PyList_Check(argv)) {
2901 argc = PyList_Size(argv);
2902 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002903 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002904 else if (PyTuple_Check(argv)) {
2905 argc = PyTuple_Size(argv);
2906 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002907 }
2908 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002909 PyErr_SetString(PyExc_TypeError,
2910 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002911 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002912 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002913 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002914 PyErr_SetString(PyExc_TypeError,
2915 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002916 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002917 }
2918
Barry Warsaw53699e91996-12-10 23:23:01 +00002919 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002920 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002921 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002922 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002923 }
2924 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002925 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002926 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002927 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002928 &argvlist[i]))
2929 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002930 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002931 goto fail_1;
2932 }
2933 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002934 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002935 argvlist[argc] = NULL;
2936
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002937 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002938 if (i < 0)
2939 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002940 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002941 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002942 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002943 goto fail_1;
2944 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002945 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002946 keys = PyMapping_Keys(env);
2947 vals = PyMapping_Values(env);
2948 if (!keys || !vals)
2949 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002950 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2951 PyErr_SetString(PyExc_TypeError,
2952 "execve(): env.keys() or env.values() is not a list");
2953 goto fail_2;
2954 }
Tim Peters5aa91602002-01-30 05:46:57 +00002955
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002956 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002957 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002958 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002959
2960 key = PyList_GetItem(keys, pos);
2961 val = PyList_GetItem(vals, pos);
2962 if (!key || !val)
2963 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002964
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002965 if (!PyArg_Parse(
2966 key,
2967 "s;execve() arg 3 contains a non-string key",
2968 &k) ||
2969 !PyArg_Parse(
2970 val,
2971 "s;execve() arg 3 contains a non-string value",
2972 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002973 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002974 goto fail_2;
2975 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002976
2977#if defined(PYOS_OS2)
2978 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2979 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2980#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002981 len = PyString_Size(key) + PyString_Size(val) + 2;
2982 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002983 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002984 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002985 goto fail_2;
2986 }
Tim Petersc8996f52001-12-03 20:41:00 +00002987 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002988 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002989#if defined(PYOS_OS2)
2990 }
2991#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002992 }
2993 envlist[envc] = 0;
2994
2995 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002996
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002997 /* If we get here it's definitely an error */
2998
2999 (void) posix_error();
3000
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003001 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003002 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003003 PyMem_DEL(envlist[envc]);
3004 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003005 fail_1:
3006 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003007 Py_XDECREF(vals);
3008 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003009 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003010 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003011 return NULL;
3012}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003013#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003015
Guido van Rossuma1065681999-01-25 23:20:23 +00003016#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003018"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003019Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003020\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003021 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003022 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003023 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003024
3025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003026posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003027{
3028 char *path;
3029 PyObject *argv;
3030 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003031 int mode, i;
3032 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003033 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003034 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003035
3036 /* spawnv has three arguments: (mode, path, argv), where
3037 argv is a list or tuple of strings. */
3038
Martin v. Löwis114619e2002-10-07 06:44:21 +00003039 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3040 Py_FileSystemDefaultEncoding,
3041 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003042 return NULL;
3043 if (PyList_Check(argv)) {
3044 argc = PyList_Size(argv);
3045 getitem = PyList_GetItem;
3046 }
3047 else if (PyTuple_Check(argv)) {
3048 argc = PyTuple_Size(argv);
3049 getitem = PyTuple_GetItem;
3050 }
3051 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003052 PyErr_SetString(PyExc_TypeError,
3053 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003054 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003055 return NULL;
3056 }
3057
3058 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003059 if (argvlist == NULL) {
3060 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003061 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003062 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003063 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003064 if (!PyArg_Parse((*getitem)(argv, i), "et",
3065 Py_FileSystemDefaultEncoding,
3066 &argvlist[i])) {
3067 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003068 PyErr_SetString(
3069 PyExc_TypeError,
3070 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003071 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003072 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003073 }
3074 }
3075 argvlist[argc] = NULL;
3076
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003077#if defined(PYOS_OS2) && defined(PYCC_GCC)
3078 Py_BEGIN_ALLOW_THREADS
3079 spawnval = spawnv(mode, path, argvlist);
3080 Py_END_ALLOW_THREADS
3081#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003082 if (mode == _OLD_P_OVERLAY)
3083 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003084
Tim Peters25059d32001-12-07 20:35:43 +00003085 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003086 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003087 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003088#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003089
Martin v. Löwis114619e2002-10-07 06:44:21 +00003090 free_string_array(argvlist, argc);
3091 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003092
Fred Drake699f3522000-06-29 21:12:41 +00003093 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003094 return posix_error();
3095 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003096#if SIZEOF_LONG == SIZEOF_VOID_P
3097 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003098#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003099 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003100#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003101}
3102
3103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003104PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003105"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003106Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003107\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003108 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003109 path: path of executable file\n\
3110 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003111 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003112
3113static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003114posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003115{
3116 char *path;
3117 PyObject *argv, *env;
3118 char **argvlist;
3119 char **envlist;
3120 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003121 int mode, pos, envc;
3122 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003123 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003124 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003125 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003126
3127 /* spawnve has four arguments: (mode, path, argv, env), where
3128 argv is a list or tuple of strings and env is a dictionary
3129 like posix.environ. */
3130
Martin v. Löwis114619e2002-10-07 06:44:21 +00003131 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3132 Py_FileSystemDefaultEncoding,
3133 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003134 return NULL;
3135 if (PyList_Check(argv)) {
3136 argc = PyList_Size(argv);
3137 getitem = PyList_GetItem;
3138 }
3139 else if (PyTuple_Check(argv)) {
3140 argc = PyTuple_Size(argv);
3141 getitem = PyTuple_GetItem;
3142 }
3143 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 PyErr_SetString(PyExc_TypeError,
3145 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003146 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003147 }
3148 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 PyErr_SetString(PyExc_TypeError,
3150 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003151 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003152 }
3153
3154 argvlist = PyMem_NEW(char *, argc+1);
3155 if (argvlist == NULL) {
3156 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003157 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003158 }
3159 for (i = 0; i < argc; i++) {
3160 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003161 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003162 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003163 &argvlist[i]))
3164 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003165 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003166 goto fail_1;
3167 }
3168 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003169 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003170 argvlist[argc] = NULL;
3171
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003172 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003173 if (i < 0)
3174 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003175 envlist = PyMem_NEW(char *, i + 1);
3176 if (envlist == NULL) {
3177 PyErr_NoMemory();
3178 goto fail_1;
3179 }
3180 envc = 0;
3181 keys = PyMapping_Keys(env);
3182 vals = PyMapping_Values(env);
3183 if (!keys || !vals)
3184 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003185 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3186 PyErr_SetString(PyExc_TypeError,
3187 "spawnve(): env.keys() or env.values() is not a list");
3188 goto fail_2;
3189 }
Tim Peters5aa91602002-01-30 05:46:57 +00003190
Guido van Rossuma1065681999-01-25 23:20:23 +00003191 for (pos = 0; pos < i; pos++) {
3192 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003193 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003194
3195 key = PyList_GetItem(keys, pos);
3196 val = PyList_GetItem(vals, pos);
3197 if (!key || !val)
3198 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003199
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003200 if (!PyArg_Parse(
3201 key,
3202 "s;spawnve() arg 3 contains a non-string key",
3203 &k) ||
3204 !PyArg_Parse(
3205 val,
3206 "s;spawnve() arg 3 contains a non-string value",
3207 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003208 {
3209 goto fail_2;
3210 }
Tim Petersc8996f52001-12-03 20:41:00 +00003211 len = PyString_Size(key) + PyString_Size(val) + 2;
3212 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003213 if (p == NULL) {
3214 PyErr_NoMemory();
3215 goto fail_2;
3216 }
Tim Petersc8996f52001-12-03 20:41:00 +00003217 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003218 envlist[envc++] = p;
3219 }
3220 envlist[envc] = 0;
3221
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003222#if defined(PYOS_OS2) && defined(PYCC_GCC)
3223 Py_BEGIN_ALLOW_THREADS
3224 spawnval = spawnve(mode, path, argvlist, envlist);
3225 Py_END_ALLOW_THREADS
3226#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003227 if (mode == _OLD_P_OVERLAY)
3228 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003229
3230 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003231 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003232 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003233#endif
Tim Peters25059d32001-12-07 20:35:43 +00003234
Fred Drake699f3522000-06-29 21:12:41 +00003235 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003236 (void) posix_error();
3237 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003238#if SIZEOF_LONG == SIZEOF_VOID_P
3239 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003240#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003241 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003242#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003243
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003244 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003245 while (--envc >= 0)
3246 PyMem_DEL(envlist[envc]);
3247 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003248 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003249 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003250 Py_XDECREF(vals);
3251 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003252 fail_0:
3253 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003254 return res;
3255}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003256
3257/* OS/2 supports spawnvp & spawnvpe natively */
3258#if defined(PYOS_OS2)
3259PyDoc_STRVAR(posix_spawnvp__doc__,
3260"spawnvp(mode, file, args)\n\n\
3261Execute the program 'file' in a new process, using the environment\n\
3262search path to find the file.\n\
3263\n\
3264 mode: mode of process creation\n\
3265 file: executable file name\n\
3266 args: tuple or list of strings");
3267
3268static PyObject *
3269posix_spawnvp(PyObject *self, PyObject *args)
3270{
3271 char *path;
3272 PyObject *argv;
3273 char **argvlist;
3274 int mode, i, argc;
3275 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003276 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003277
3278 /* spawnvp has three arguments: (mode, path, argv), where
3279 argv is a list or tuple of strings. */
3280
3281 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3282 Py_FileSystemDefaultEncoding,
3283 &path, &argv))
3284 return NULL;
3285 if (PyList_Check(argv)) {
3286 argc = PyList_Size(argv);
3287 getitem = PyList_GetItem;
3288 }
3289 else if (PyTuple_Check(argv)) {
3290 argc = PyTuple_Size(argv);
3291 getitem = PyTuple_GetItem;
3292 }
3293 else {
3294 PyErr_SetString(PyExc_TypeError,
3295 "spawnvp() arg 2 must be a tuple or list");
3296 PyMem_Free(path);
3297 return NULL;
3298 }
3299
3300 argvlist = PyMem_NEW(char *, argc+1);
3301 if (argvlist == NULL) {
3302 PyMem_Free(path);
3303 return PyErr_NoMemory();
3304 }
3305 for (i = 0; i < argc; i++) {
3306 if (!PyArg_Parse((*getitem)(argv, i), "et",
3307 Py_FileSystemDefaultEncoding,
3308 &argvlist[i])) {
3309 free_string_array(argvlist, i);
3310 PyErr_SetString(
3311 PyExc_TypeError,
3312 "spawnvp() arg 2 must contain only strings");
3313 PyMem_Free(path);
3314 return NULL;
3315 }
3316 }
3317 argvlist[argc] = NULL;
3318
3319 Py_BEGIN_ALLOW_THREADS
3320#if defined(PYCC_GCC)
3321 spawnval = spawnvp(mode, path, argvlist);
3322#else
3323 spawnval = _spawnvp(mode, path, argvlist);
3324#endif
3325 Py_END_ALLOW_THREADS
3326
3327 free_string_array(argvlist, argc);
3328 PyMem_Free(path);
3329
3330 if (spawnval == -1)
3331 return posix_error();
3332 else
3333 return Py_BuildValue("l", (long) spawnval);
3334}
3335
3336
3337PyDoc_STRVAR(posix_spawnvpe__doc__,
3338"spawnvpe(mode, file, args, env)\n\n\
3339Execute the program 'file' in a new process, using the environment\n\
3340search path to find the file.\n\
3341\n\
3342 mode: mode of process creation\n\
3343 file: executable file name\n\
3344 args: tuple or list of arguments\n\
3345 env: dictionary of strings mapping to strings");
3346
3347static PyObject *
3348posix_spawnvpe(PyObject *self, PyObject *args)
3349{
3350 char *path;
3351 PyObject *argv, *env;
3352 char **argvlist;
3353 char **envlist;
3354 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3355 int mode, i, pos, argc, envc;
3356 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003357 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003358 int lastarg = 0;
3359
3360 /* spawnvpe has four arguments: (mode, path, argv, env), where
3361 argv is a list or tuple of strings and env is a dictionary
3362 like posix.environ. */
3363
3364 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3365 Py_FileSystemDefaultEncoding,
3366 &path, &argv, &env))
3367 return NULL;
3368 if (PyList_Check(argv)) {
3369 argc = PyList_Size(argv);
3370 getitem = PyList_GetItem;
3371 }
3372 else if (PyTuple_Check(argv)) {
3373 argc = PyTuple_Size(argv);
3374 getitem = PyTuple_GetItem;
3375 }
3376 else {
3377 PyErr_SetString(PyExc_TypeError,
3378 "spawnvpe() arg 2 must be a tuple or list");
3379 goto fail_0;
3380 }
3381 if (!PyMapping_Check(env)) {
3382 PyErr_SetString(PyExc_TypeError,
3383 "spawnvpe() arg 3 must be a mapping object");
3384 goto fail_0;
3385 }
3386
3387 argvlist = PyMem_NEW(char *, argc+1);
3388 if (argvlist == NULL) {
3389 PyErr_NoMemory();
3390 goto fail_0;
3391 }
3392 for (i = 0; i < argc; i++) {
3393 if (!PyArg_Parse((*getitem)(argv, i),
3394 "et;spawnvpe() arg 2 must contain only strings",
3395 Py_FileSystemDefaultEncoding,
3396 &argvlist[i]))
3397 {
3398 lastarg = i;
3399 goto fail_1;
3400 }
3401 }
3402 lastarg = argc;
3403 argvlist[argc] = NULL;
3404
3405 i = PyMapping_Size(env);
3406 if (i < 0)
3407 goto fail_1;
3408 envlist = PyMem_NEW(char *, i + 1);
3409 if (envlist == NULL) {
3410 PyErr_NoMemory();
3411 goto fail_1;
3412 }
3413 envc = 0;
3414 keys = PyMapping_Keys(env);
3415 vals = PyMapping_Values(env);
3416 if (!keys || !vals)
3417 goto fail_2;
3418 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3419 PyErr_SetString(PyExc_TypeError,
3420 "spawnvpe(): env.keys() or env.values() is not a list");
3421 goto fail_2;
3422 }
3423
3424 for (pos = 0; pos < i; pos++) {
3425 char *p, *k, *v;
3426 size_t len;
3427
3428 key = PyList_GetItem(keys, pos);
3429 val = PyList_GetItem(vals, pos);
3430 if (!key || !val)
3431 goto fail_2;
3432
3433 if (!PyArg_Parse(
3434 key,
3435 "s;spawnvpe() arg 3 contains a non-string key",
3436 &k) ||
3437 !PyArg_Parse(
3438 val,
3439 "s;spawnvpe() arg 3 contains a non-string value",
3440 &v))
3441 {
3442 goto fail_2;
3443 }
3444 len = PyString_Size(key) + PyString_Size(val) + 2;
3445 p = PyMem_NEW(char, len);
3446 if (p == NULL) {
3447 PyErr_NoMemory();
3448 goto fail_2;
3449 }
3450 PyOS_snprintf(p, len, "%s=%s", k, v);
3451 envlist[envc++] = p;
3452 }
3453 envlist[envc] = 0;
3454
3455 Py_BEGIN_ALLOW_THREADS
3456#if defined(PYCC_GCC)
3457 spawnval = spawnve(mode, path, argvlist, envlist);
3458#else
3459 spawnval = _spawnve(mode, path, argvlist, envlist);
3460#endif
3461 Py_END_ALLOW_THREADS
3462
3463 if (spawnval == -1)
3464 (void) posix_error();
3465 else
3466 res = Py_BuildValue("l", (long) spawnval);
3467
3468 fail_2:
3469 while (--envc >= 0)
3470 PyMem_DEL(envlist[envc]);
3471 PyMem_DEL(envlist);
3472 fail_1:
3473 free_string_array(argvlist, lastarg);
3474 Py_XDECREF(vals);
3475 Py_XDECREF(keys);
3476 fail_0:
3477 PyMem_Free(path);
3478 return res;
3479}
3480#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003481#endif /* HAVE_SPAWNV */
3482
3483
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003484#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003485PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003486"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003487Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3488\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003489Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003490
3491static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003492posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003493{
Neal Norwitze241ce82003-02-17 18:17:05 +00003494 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003495 if (pid == -1)
3496 return posix_error();
3497 PyOS_AfterFork();
3498 return PyInt_FromLong((long)pid);
3499}
3500#endif
3501
3502
Guido van Rossumad0ee831995-03-01 10:34:45 +00003503#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003504PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003505"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003506Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003507Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003508
Barry Warsaw53699e91996-12-10 23:23:01 +00003509static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003510posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003511{
Neal Norwitze241ce82003-02-17 18:17:05 +00003512 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003513 if (pid == -1)
3514 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003515 if (pid == 0)
3516 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003517 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003518}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003519#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003520
Neal Norwitzb59798b2003-03-21 01:43:31 +00003521/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003522/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3523#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003524#define DEV_PTY_FILE "/dev/ptc"
3525#define HAVE_DEV_PTMX
3526#else
3527#define DEV_PTY_FILE "/dev/ptmx"
3528#endif
3529
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003530#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003531#ifdef HAVE_PTY_H
3532#include <pty.h>
3533#else
3534#ifdef HAVE_LIBUTIL_H
3535#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003536#endif /* HAVE_LIBUTIL_H */
3537#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003538#ifdef HAVE_STROPTS_H
3539#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003540#endif
3541#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003542
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003543#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003544PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003545"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003546Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003547
3548static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003549posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003550{
3551 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003552#ifndef HAVE_OPENPTY
3553 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003554#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003555#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003556 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003557#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003558 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003559#endif
3560#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003561
Thomas Wouters70c21a12000-07-14 14:28:33 +00003562#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003563 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3564 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003565#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003566 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3567 if (slave_name == NULL)
3568 return posix_error();
3569
3570 slave_fd = open(slave_name, O_RDWR);
3571 if (slave_fd < 0)
3572 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003573#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003574 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003575 if (master_fd < 0)
3576 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003577 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003578 /* change permission of slave */
3579 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003580 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003581 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003582 }
3583 /* unlock slave */
3584 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003585 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003586 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003587 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003588 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003589 slave_name = ptsname(master_fd); /* get name of slave */
3590 if (slave_name == NULL)
3591 return posix_error();
3592 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3593 if (slave_fd < 0)
3594 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003595#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003596 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3597 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003598#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003599 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003600#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003601#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003602#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003603
Fred Drake8cef4cf2000-06-28 16:40:38 +00003604 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003605
Fred Drake8cef4cf2000-06-28 16:40:38 +00003606}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003607#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003608
3609#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003610PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003611"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003612Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3613Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003614To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003615
3616static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003617posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003618{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003619 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003620
Fred Drake8cef4cf2000-06-28 16:40:38 +00003621 pid = forkpty(&master_fd, NULL, NULL, NULL);
3622 if (pid == -1)
3623 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003624 if (pid == 0)
3625 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003626 return Py_BuildValue("(ii)", pid, master_fd);
3627}
3628#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003629
Guido van Rossumad0ee831995-03-01 10:34:45 +00003630#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003631PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003632"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003633Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003634
Barry Warsaw53699e91996-12-10 23:23:01 +00003635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003636posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003637{
Barry Warsaw53699e91996-12-10 23:23:01 +00003638 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003639}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003640#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003641
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003642
Guido van Rossumad0ee831995-03-01 10:34:45 +00003643#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003644PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003645"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003646Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003647
Barry Warsaw53699e91996-12-10 23:23:01 +00003648static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003649posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003650{
Barry Warsaw53699e91996-12-10 23:23:01 +00003651 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003652}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003653#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003654
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003655
Guido van Rossumad0ee831995-03-01 10:34:45 +00003656#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003657PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003658"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003659Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003660
Barry Warsaw53699e91996-12-10 23:23:01 +00003661static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003662posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003663{
Barry Warsaw53699e91996-12-10 23:23:01 +00003664 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003665}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003666#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003667
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003669PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003670"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003671Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003672
Barry Warsaw53699e91996-12-10 23:23:01 +00003673static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003674posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003675{
Barry Warsaw53699e91996-12-10 23:23:01 +00003676 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003677}
3678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003679
Fred Drakec9680921999-12-13 16:37:25 +00003680#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003681PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003682"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003683Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003684
3685static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003686posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003687{
3688 PyObject *result = NULL;
3689
Fred Drakec9680921999-12-13 16:37:25 +00003690#ifdef NGROUPS_MAX
3691#define MAX_GROUPS NGROUPS_MAX
3692#else
3693 /* defined to be 16 on Solaris7, so this should be a small number */
3694#define MAX_GROUPS 64
3695#endif
3696 gid_t grouplist[MAX_GROUPS];
3697 int n;
3698
3699 n = getgroups(MAX_GROUPS, grouplist);
3700 if (n < 0)
3701 posix_error();
3702 else {
3703 result = PyList_New(n);
3704 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003705 int i;
3706 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003707 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003708 if (o == NULL) {
3709 Py_DECREF(result);
3710 result = NULL;
3711 break;
3712 }
3713 PyList_SET_ITEM(result, i, o);
3714 }
3715 }
3716 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003717
Fred Drakec9680921999-12-13 16:37:25 +00003718 return result;
3719}
3720#endif
3721
Martin v. Löwis606edc12002-06-13 21:09:11 +00003722#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003723PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003724"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003725Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003726
3727static PyObject *
3728posix_getpgid(PyObject *self, PyObject *args)
3729{
3730 int pid, pgid;
3731 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3732 return NULL;
3733 pgid = getpgid(pid);
3734 if (pgid < 0)
3735 return posix_error();
3736 return PyInt_FromLong((long)pgid);
3737}
3738#endif /* HAVE_GETPGID */
3739
3740
Guido van Rossumb6775db1994-08-01 11:34:53 +00003741#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003742PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003743"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003744Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003745
Barry Warsaw53699e91996-12-10 23:23:01 +00003746static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003747posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003748{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003749#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003750 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003751#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003752 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003753#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003754}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003755#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003757
Guido van Rossumb6775db1994-08-01 11:34:53 +00003758#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003759PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003760"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003761Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003762
Barry Warsaw53699e91996-12-10 23:23:01 +00003763static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003764posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003765{
Guido van Rossum64933891994-10-20 21:56:42 +00003766#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003767 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003768#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003769 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003770#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003771 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003772 Py_INCREF(Py_None);
3773 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003774}
3775
Guido van Rossumb6775db1994-08-01 11:34:53 +00003776#endif /* HAVE_SETPGRP */
3777
Guido van Rossumad0ee831995-03-01 10:34:45 +00003778#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003780"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003781Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003782
Barry Warsaw53699e91996-12-10 23:23:01 +00003783static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003784posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003785{
Barry Warsaw53699e91996-12-10 23:23:01 +00003786 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003787}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003788#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003789
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Fred Drake12c6e2d1999-12-14 21:25:03 +00003791#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003792PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003793"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003794Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003795
3796static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003797posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003798{
Neal Norwitze241ce82003-02-17 18:17:05 +00003799 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003800 char *name;
3801 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003802
Fred Drakea30680b2000-12-06 21:24:28 +00003803 errno = 0;
3804 name = getlogin();
3805 if (name == NULL) {
3806 if (errno)
3807 posix_error();
3808 else
3809 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003810 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003811 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003812 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003813 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003814 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003815
Fred Drake12c6e2d1999-12-14 21:25:03 +00003816 return result;
3817}
3818#endif
3819
Guido van Rossumad0ee831995-03-01 10:34:45 +00003820#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003821PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003822"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003823Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003824
Barry Warsaw53699e91996-12-10 23:23:01 +00003825static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003826posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003827{
Barry Warsaw53699e91996-12-10 23:23:01 +00003828 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003829}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003830#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003832
Guido van Rossumad0ee831995-03-01 10:34:45 +00003833#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003834PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003835"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003836Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003837
Barry Warsaw53699e91996-12-10 23:23:01 +00003838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003839posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003840{
3841 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003842 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003843 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003844#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003845 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3846 APIRET rc;
3847 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003848 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003849
3850 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3851 APIRET rc;
3852 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003853 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003854
3855 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003856 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003857#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003858 if (kill(pid, sig) == -1)
3859 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003860#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003861 Py_INCREF(Py_None);
3862 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003863}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003864#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003865
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003866#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003867PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003868"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003869Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003870
3871static PyObject *
3872posix_killpg(PyObject *self, PyObject *args)
3873{
3874 int pgid, sig;
3875 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3876 return NULL;
3877 if (killpg(pgid, sig) == -1)
3878 return posix_error();
3879 Py_INCREF(Py_None);
3880 return Py_None;
3881}
3882#endif
3883
Guido van Rossumc0125471996-06-28 18:55:32 +00003884#ifdef HAVE_PLOCK
3885
3886#ifdef HAVE_SYS_LOCK_H
3887#include <sys/lock.h>
3888#endif
3889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003890PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003891"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003892Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003893
Barry Warsaw53699e91996-12-10 23:23:01 +00003894static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003895posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003896{
3897 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003898 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003899 return NULL;
3900 if (plock(op) == -1)
3901 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003902 Py_INCREF(Py_None);
3903 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003904}
3905#endif
3906
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003907
Guido van Rossum3b066191991-06-04 19:40:25 +00003908
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003909
Guido van Rossumb6775db1994-08-01 11:34:53 +00003910#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003911PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003912"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003913Set the current process's user id.");
3914
Barry Warsaw53699e91996-12-10 23:23:01 +00003915static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003916posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003917{
3918 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003919 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003920 return NULL;
3921 if (setuid(uid) < 0)
3922 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003923 Py_INCREF(Py_None);
3924 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003925}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003926#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003927
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003928
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003929#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003930PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003931"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003932Set the current process's effective user id.");
3933
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003934static PyObject *
3935posix_seteuid (PyObject *self, PyObject *args)
3936{
3937 int euid;
3938 if (!PyArg_ParseTuple(args, "i", &euid)) {
3939 return NULL;
3940 } else if (seteuid(euid) < 0) {
3941 return posix_error();
3942 } else {
3943 Py_INCREF(Py_None);
3944 return Py_None;
3945 }
3946}
3947#endif /* HAVE_SETEUID */
3948
3949#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003950PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003951"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003952Set the current process's effective group id.");
3953
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003954static PyObject *
3955posix_setegid (PyObject *self, PyObject *args)
3956{
3957 int egid;
3958 if (!PyArg_ParseTuple(args, "i", &egid)) {
3959 return NULL;
3960 } else if (setegid(egid) < 0) {
3961 return posix_error();
3962 } else {
3963 Py_INCREF(Py_None);
3964 return Py_None;
3965 }
3966}
3967#endif /* HAVE_SETEGID */
3968
3969#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003970PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003971"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003972Set the current process's real and effective user ids.");
3973
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003974static PyObject *
3975posix_setreuid (PyObject *self, PyObject *args)
3976{
3977 int ruid, euid;
3978 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
3979 return NULL;
3980 } else if (setreuid(ruid, euid) < 0) {
3981 return posix_error();
3982 } else {
3983 Py_INCREF(Py_None);
3984 return Py_None;
3985 }
3986}
3987#endif /* HAVE_SETREUID */
3988
3989#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003990PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003991"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003992Set the current process's real and effective group ids.");
3993
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003994static PyObject *
3995posix_setregid (PyObject *self, PyObject *args)
3996{
3997 int rgid, egid;
3998 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
3999 return NULL;
4000 } else if (setregid(rgid, egid) < 0) {
4001 return posix_error();
4002 } else {
4003 Py_INCREF(Py_None);
4004 return Py_None;
4005 }
4006}
4007#endif /* HAVE_SETREGID */
4008
Guido van Rossumb6775db1994-08-01 11:34:53 +00004009#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004010PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004011"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004012Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004013
Barry Warsaw53699e91996-12-10 23:23:01 +00004014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004015posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004016{
4017 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004018 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004019 return NULL;
4020 if (setgid(gid) < 0)
4021 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004022 Py_INCREF(Py_None);
4023 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004024}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004025#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004026
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004027#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004029"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004030Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004031
4032static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004033posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004034{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004035 int i, len;
4036 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004037
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004038 if (!PySequence_Check(groups)) {
4039 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4040 return NULL;
4041 }
4042 len = PySequence_Size(groups);
4043 if (len > MAX_GROUPS) {
4044 PyErr_SetString(PyExc_ValueError, "too many groups");
4045 return NULL;
4046 }
4047 for(i = 0; i < len; i++) {
4048 PyObject *elem;
4049 elem = PySequence_GetItem(groups, i);
4050 if (!elem)
4051 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004052 if (!PyLong_Check(elem)) {
4053 PyErr_SetString(PyExc_TypeError,
4054 "groups must be integers");
4055 Py_DECREF(elem);
4056 return NULL;
4057 } else {
4058 unsigned long x = PyLong_AsUnsignedLong(elem);
4059 if (PyErr_Occurred()) {
4060 PyErr_SetString(PyExc_TypeError,
4061 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004062 Py_DECREF(elem);
4063 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004064 }
Georg Brandla13c2442005-11-22 19:30:31 +00004065 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004066 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004067 if (grouplist[i] != x) {
4068 PyErr_SetString(PyExc_TypeError,
4069 "group id too big");
4070 Py_DECREF(elem);
4071 return NULL;
4072 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004073 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004074 Py_DECREF(elem);
4075 }
4076
4077 if (setgroups(len, grouplist) < 0)
4078 return posix_error();
4079 Py_INCREF(Py_None);
4080 return Py_None;
4081}
4082#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004083
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004084#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4085static PyObject *
4086wait_helper(int pid, int status, struct rusage *ru)
4087{
4088 PyObject *result;
4089 static PyObject *struct_rusage;
4090
4091 if (pid == -1)
4092 return posix_error();
4093
4094 if (struct_rusage == NULL) {
4095 PyObject *m = PyImport_ImportModule("resource");
4096 if (m == NULL)
4097 return NULL;
4098 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4099 Py_DECREF(m);
4100 if (struct_rusage == NULL)
4101 return NULL;
4102 }
4103
4104 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4105 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4106 if (!result)
4107 return NULL;
4108
4109#ifndef doubletime
4110#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4111#endif
4112
4113 PyStructSequence_SET_ITEM(result, 0,
4114 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4115 PyStructSequence_SET_ITEM(result, 1,
4116 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4117#define SET_INT(result, index, value)\
4118 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4119 SET_INT(result, 2, ru->ru_maxrss);
4120 SET_INT(result, 3, ru->ru_ixrss);
4121 SET_INT(result, 4, ru->ru_idrss);
4122 SET_INT(result, 5, ru->ru_isrss);
4123 SET_INT(result, 6, ru->ru_minflt);
4124 SET_INT(result, 7, ru->ru_majflt);
4125 SET_INT(result, 8, ru->ru_nswap);
4126 SET_INT(result, 9, ru->ru_inblock);
4127 SET_INT(result, 10, ru->ru_oublock);
4128 SET_INT(result, 11, ru->ru_msgsnd);
4129 SET_INT(result, 12, ru->ru_msgrcv);
4130 SET_INT(result, 13, ru->ru_nsignals);
4131 SET_INT(result, 14, ru->ru_nvcsw);
4132 SET_INT(result, 15, ru->ru_nivcsw);
4133#undef SET_INT
4134
4135 if (PyErr_Occurred()) {
4136 Py_DECREF(result);
4137 return NULL;
4138 }
4139
4140 return Py_BuildValue("iiN", pid, status, result);
4141}
4142#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4143
4144#ifdef HAVE_WAIT3
4145PyDoc_STRVAR(posix_wait3__doc__,
4146"wait3(options) -> (pid, status, rusage)\n\n\
4147Wait for completion of a child process.");
4148
4149static PyObject *
4150posix_wait3(PyObject *self, PyObject *args)
4151{
4152 int pid, options;
4153 struct rusage ru;
4154 WAIT_TYPE status;
4155 WAIT_STATUS_INT(status) = 0;
4156
4157 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4158 return NULL;
4159
4160 Py_BEGIN_ALLOW_THREADS
4161 pid = wait3(&status, options, &ru);
4162 Py_END_ALLOW_THREADS
4163
4164 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4165}
4166#endif /* HAVE_WAIT3 */
4167
4168#ifdef HAVE_WAIT4
4169PyDoc_STRVAR(posix_wait4__doc__,
4170"wait4(pid, options) -> (pid, status, rusage)\n\n\
4171Wait for completion of a given child process.");
4172
4173static PyObject *
4174posix_wait4(PyObject *self, PyObject *args)
4175{
4176 int pid, options;
4177 struct rusage ru;
4178 WAIT_TYPE status;
4179 WAIT_STATUS_INT(status) = 0;
4180
4181 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4182 return NULL;
4183
4184 Py_BEGIN_ALLOW_THREADS
4185 pid = wait4(pid, &status, options, &ru);
4186 Py_END_ALLOW_THREADS
4187
4188 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4189}
4190#endif /* HAVE_WAIT4 */
4191
Guido van Rossumb6775db1994-08-01 11:34:53 +00004192#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004193PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004194"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004195Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004196
Barry Warsaw53699e91996-12-10 23:23:01 +00004197static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004198posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004199{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004200 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004201 WAIT_TYPE status;
4202 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004203
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004204 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004205 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004206 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004207 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004208 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004209 if (pid == -1)
4210 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004211
4212 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004213}
4214
Tim Petersab034fa2002-02-01 11:27:43 +00004215#elif defined(HAVE_CWAIT)
4216
4217/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004218PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004219"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004220"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004221
4222static PyObject *
4223posix_waitpid(PyObject *self, PyObject *args)
4224{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004225 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004226 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004227
4228 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4229 return NULL;
4230 Py_BEGIN_ALLOW_THREADS
4231 pid = _cwait(&status, pid, options);
4232 Py_END_ALLOW_THREADS
4233 if (pid == -1)
4234 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004235
4236 /* shift the status left a byte so this is more like the POSIX waitpid */
4237 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004238}
4239#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004240
Guido van Rossumad0ee831995-03-01 10:34:45 +00004241#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004242PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004243"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004244Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004245
Barry Warsaw53699e91996-12-10 23:23:01 +00004246static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004247posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004248{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004249 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004250 WAIT_TYPE status;
4251 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004252
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004253 Py_BEGIN_ALLOW_THREADS
4254 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004255 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004256 if (pid == -1)
4257 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004258
4259 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004260}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004261#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004262
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004264PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004265"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004266Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004267
Barry Warsaw53699e91996-12-10 23:23:01 +00004268static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004269posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004270{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004271#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004272 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004273#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004274#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004275 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004276#else
4277 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4278#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004279#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004280}
4281
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004282
Guido van Rossumb6775db1994-08-01 11:34:53 +00004283#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004284PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004285"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004286Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004287
Barry Warsaw53699e91996-12-10 23:23:01 +00004288static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004289posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004290{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004291 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004292 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004293 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004294 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004295 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004296
4297 if (!PyArg_ParseTuple(args, "et:readlink",
4298 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004299 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004300 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004301 if (v == NULL) {
4302 PyMem_Free(path);
4303 return NULL;
4304 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004305
4306 if (PyUnicode_Check(v)) {
4307 arg_is_unicode = 1;
4308 }
4309 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004310
Barry Warsaw53699e91996-12-10 23:23:01 +00004311 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004312 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004313 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004314 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004315 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004316
Neal Norwitzfca70052007-08-12 16:56:02 +00004317 PyMem_Free(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004318 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004319 if (arg_is_unicode) {
4320 PyObject *w;
4321
4322 w = PyUnicode_FromEncodedObject(v,
4323 Py_FileSystemDefaultEncoding,
4324 "strict");
4325 if (w != NULL) {
4326 Py_DECREF(v);
4327 v = w;
4328 }
4329 else {
4330 /* fall back to the original byte string, as
4331 discussed in patch #683592 */
4332 PyErr_Clear();
4333 }
4334 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004335 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004336}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004337#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004338
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004339
Guido van Rossumb6775db1994-08-01 11:34:53 +00004340#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004341PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004342"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004343Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004344
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004345static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004346posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004347{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004348 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004349}
4350#endif /* HAVE_SYMLINK */
4351
4352
4353#ifdef HAVE_TIMES
4354#ifndef HZ
4355#define HZ 60 /* Universal constant :-) */
4356#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004357
Guido van Rossumd48f2521997-12-05 22:19:34 +00004358#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4359static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004360system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004361{
4362 ULONG value = 0;
4363
4364 Py_BEGIN_ALLOW_THREADS
4365 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4366 Py_END_ALLOW_THREADS
4367
4368 return value;
4369}
4370
4371static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004372posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004373{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004374 /* Currently Only Uptime is Provided -- Others Later */
4375 return Py_BuildValue("ddddd",
4376 (double)0 /* t.tms_utime / HZ */,
4377 (double)0 /* t.tms_stime / HZ */,
4378 (double)0 /* t.tms_cutime / HZ */,
4379 (double)0 /* t.tms_cstime / HZ */,
4380 (double)system_uptime() / 1000);
4381}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004382#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004383static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004384posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004385{
4386 struct tms t;
4387 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004388 errno = 0;
4389 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004390 if (c == (clock_t) -1)
4391 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004392 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004393 (double)t.tms_utime / HZ,
4394 (double)t.tms_stime / HZ,
4395 (double)t.tms_cutime / HZ,
4396 (double)t.tms_cstime / HZ,
4397 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004398}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004399#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004400#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004401
4402
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004403#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004404#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004405static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004406posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004407{
4408 FILETIME create, exit, kernel, user;
4409 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004410 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004411 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4412 /* The fields of a FILETIME structure are the hi and lo part
4413 of a 64-bit value expressed in 100 nanosecond units.
4414 1e7 is one second in such units; 1e-7 the inverse.
4415 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4416 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004417 return Py_BuildValue(
4418 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004419 (double)(kernel.dwHighDateTime*429.4967296 +
4420 kernel.dwLowDateTime*1e-7),
4421 (double)(user.dwHighDateTime*429.4967296 +
4422 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004423 (double)0,
4424 (double)0,
4425 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004426}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004427#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004428
4429#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004430PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004431"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004432Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004433#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004434
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004435
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004436#ifdef HAVE_GETSID
4437PyDoc_STRVAR(posix_getsid__doc__,
4438"getsid(pid) -> sid\n\n\
4439Call the system call getsid().");
4440
4441static PyObject *
4442posix_getsid(PyObject *self, PyObject *args)
4443{
4444 int pid, sid;
4445 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4446 return NULL;
4447 sid = getsid(pid);
4448 if (sid < 0)
4449 return posix_error();
4450 return PyInt_FromLong((long)sid);
4451}
4452#endif /* HAVE_GETSID */
4453
4454
Guido van Rossumb6775db1994-08-01 11:34:53 +00004455#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004456PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004457"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004458Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004459
Barry Warsaw53699e91996-12-10 23:23:01 +00004460static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004461posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004462{
Guido van Rossum687dd131993-05-17 08:34:16 +00004463 if (setsid() < 0)
4464 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004465 Py_INCREF(Py_None);
4466 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004467}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004468#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004469
Guido van Rossumb6775db1994-08-01 11:34:53 +00004470#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004471PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004472"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004473Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004474
Barry Warsaw53699e91996-12-10 23:23:01 +00004475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004476posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004477{
4478 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004479 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004480 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004481 if (setpgid(pid, pgrp) < 0)
4482 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004483 Py_INCREF(Py_None);
4484 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004485}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004486#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004487
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004488
Guido van Rossumb6775db1994-08-01 11:34:53 +00004489#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004490PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004491"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004492Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004493
Barry Warsaw53699e91996-12-10 23:23:01 +00004494static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004495posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004496{
4497 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004498 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004499 return NULL;
4500 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004501 if (pgid < 0)
4502 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004503 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004504}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004505#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004506
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004507
Guido van Rossumb6775db1994-08-01 11:34:53 +00004508#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004509PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004510"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004511Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004512
Barry Warsaw53699e91996-12-10 23:23:01 +00004513static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004514posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004515{
4516 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004517 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004518 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004519 if (tcsetpgrp(fd, pgid) < 0)
4520 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004521 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004522 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004523}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004524#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004525
Guido van Rossum687dd131993-05-17 08:34:16 +00004526/* Functions acting on file descriptors */
4527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004528PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004529"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004530Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004531
Barry Warsaw53699e91996-12-10 23:23:01 +00004532static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004533posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004534{
Mark Hammondef8b6542001-05-13 08:04:26 +00004535 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004536 int flag;
4537 int mode = 0777;
4538 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004539
4540#ifdef MS_WINDOWS
4541 if (unicode_file_names()) {
4542 PyUnicodeObject *po;
4543 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4544 Py_BEGIN_ALLOW_THREADS
4545 /* PyUnicode_AS_UNICODE OK without thread
4546 lock as it is a simple dereference. */
4547 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4548 Py_END_ALLOW_THREADS
4549 if (fd < 0)
4550 return posix_error();
4551 return PyInt_FromLong((long)fd);
4552 }
4553 /* Drop the argument parsing error as narrow strings
4554 are also valid. */
4555 PyErr_Clear();
4556 }
4557#endif
4558
Tim Peters5aa91602002-01-30 05:46:57 +00004559 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004560 Py_FileSystemDefaultEncoding, &file,
4561 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004562 return NULL;
4563
Barry Warsaw53699e91996-12-10 23:23:01 +00004564 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004565 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004566 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004567 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004568 return posix_error_with_allocated_filename(file);
4569 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004570 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004571}
4572
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004573
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004574PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004575"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004576Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004577
Barry Warsaw53699e91996-12-10 23:23:01 +00004578static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004579posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004580{
4581 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004582 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004583 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004584 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004585 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004586 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004587 if (res < 0)
4588 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004589 Py_INCREF(Py_None);
4590 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004591}
4592
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004594PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004595"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004596Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004597
Barry Warsaw53699e91996-12-10 23:23:01 +00004598static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004599posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004600{
4601 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004602 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004603 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004604 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004605 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004606 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004607 if (fd < 0)
4608 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004609 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004610}
4611
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004613PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004614"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004615Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004616
Barry Warsaw53699e91996-12-10 23:23:01 +00004617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004618posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004619{
4620 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004621 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004622 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004623 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004624 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004625 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004626 if (res < 0)
4627 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004628 Py_INCREF(Py_None);
4629 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004630}
4631
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004633PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004634"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004635Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004636
Barry Warsaw53699e91996-12-10 23:23:01 +00004637static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004638posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004639{
4640 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004641#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004642 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004643#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004644 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004645#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004646 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004647 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004648 return NULL;
4649#ifdef SEEK_SET
4650 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4651 switch (how) {
4652 case 0: how = SEEK_SET; break;
4653 case 1: how = SEEK_CUR; break;
4654 case 2: how = SEEK_END; break;
4655 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004656#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004657
4658#if !defined(HAVE_LARGEFILE_SUPPORT)
4659 pos = PyInt_AsLong(posobj);
4660#else
4661 pos = PyLong_Check(posobj) ?
4662 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4663#endif
4664 if (PyErr_Occurred())
4665 return NULL;
4666
Barry Warsaw53699e91996-12-10 23:23:01 +00004667 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004668#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004669 res = _lseeki64(fd, pos, how);
4670#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004671 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004672#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004673 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004674 if (res < 0)
4675 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004676
4677#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004678 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004679#else
4680 return PyLong_FromLongLong(res);
4681#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004682}
4683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004684
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004685PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004686"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004687Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004688
Barry Warsaw53699e91996-12-10 23:23:01 +00004689static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004690posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004691{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004692 int fd, size;
4693 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004694 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004695 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004696 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004697 if (size < 0) {
4698 errno = EINVAL;
4699 return posix_error();
4700 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00004701 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004702 if (buffer == NULL)
4703 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004704 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00004705 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004706 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004707 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004708 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004709 return posix_error();
4710 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004711 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00004712 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004713 return buffer;
4714}
4715
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004717PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004718"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004719Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004720
Barry Warsaw53699e91996-12-10 23:23:01 +00004721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004722posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004723{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004724 int fd;
4725 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004726 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004727
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004728 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004729 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004730 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004731 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004732 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004733 if (size < 0)
4734 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004735 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004736}
4737
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004739PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004740"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004741Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004742
Barry Warsaw53699e91996-12-10 23:23:01 +00004743static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004744posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004745{
4746 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004747 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004748 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004749 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004750 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004751#ifdef __VMS
4752 /* on OpenVMS we must ensure that all bytes are written to the file */
4753 fsync(fd);
4754#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004755 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004756 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004757 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004758 if (res != 0) {
4759#ifdef MS_WINDOWS
4760 return win32_error("fstat", NULL);
4761#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004762 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004763#endif
4764 }
Tim Peters5aa91602002-01-30 05:46:57 +00004765
Martin v. Löwis14694662006-02-03 12:54:16 +00004766 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004767}
4768
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004769PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004770"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004771Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004772connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004773
4774static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004775posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004776{
4777 int fd;
4778 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4779 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004780 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004781}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004782
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004783#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004784PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004785"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004786Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004787
Barry Warsaw53699e91996-12-10 23:23:01 +00004788static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004789posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004790{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004791#if defined(PYOS_OS2)
4792 HFILE read, write;
4793 APIRET rc;
4794
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004795 Py_BEGIN_ALLOW_THREADS
4796 rc = DosCreatePipe( &read, &write, 4096);
4797 Py_END_ALLOW_THREADS
4798 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004799 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004800
4801 return Py_BuildValue("(ii)", read, write);
4802#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004803#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004804 int fds[2];
4805 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004806 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004807 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004808 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004809 if (res != 0)
4810 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004811 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004812#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004813 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004814 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004815 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004816 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004817 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004818 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004819 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004820 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004821 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4822 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004823 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004824#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004825#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004826}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004827#endif /* HAVE_PIPE */
4828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004829
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004830#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004831PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004832"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004833Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004834
Barry Warsaw53699e91996-12-10 23:23:01 +00004835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004836posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004837{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004838 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004839 int mode = 0666;
4840 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004841 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004842 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004843 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004844 res = mkfifo(filename, mode);
4845 Py_END_ALLOW_THREADS
4846 if (res < 0)
4847 return posix_error();
4848 Py_INCREF(Py_None);
4849 return Py_None;
4850}
4851#endif
4852
4853
Neal Norwitz11690112002-07-30 01:08:28 +00004854#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004855PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004856"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004857Create a filesystem node (file, device special file or named pipe)\n\
4858named filename. mode specifies both the permissions to use and the\n\
4859type of node to be created, being combined (bitwise OR) with one of\n\
4860S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004861device defines the newly created device special file (probably using\n\
4862os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004863
4864
4865static PyObject *
4866posix_mknod(PyObject *self, PyObject *args)
4867{
4868 char *filename;
4869 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004870 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004871 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00004872 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004873 return NULL;
4874 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004875 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00004876 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004877 if (res < 0)
4878 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004879 Py_INCREF(Py_None);
4880 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004881}
4882#endif
4883
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004884#ifdef HAVE_DEVICE_MACROS
4885PyDoc_STRVAR(posix_major__doc__,
4886"major(device) -> major number\n\
4887Extracts a device major number from a raw device number.");
4888
4889static PyObject *
4890posix_major(PyObject *self, PyObject *args)
4891{
4892 int device;
4893 if (!PyArg_ParseTuple(args, "i:major", &device))
4894 return NULL;
4895 return PyInt_FromLong((long)major(device));
4896}
4897
4898PyDoc_STRVAR(posix_minor__doc__,
4899"minor(device) -> minor number\n\
4900Extracts a device minor number from a raw device number.");
4901
4902static PyObject *
4903posix_minor(PyObject *self, PyObject *args)
4904{
4905 int device;
4906 if (!PyArg_ParseTuple(args, "i:minor", &device))
4907 return NULL;
4908 return PyInt_FromLong((long)minor(device));
4909}
4910
4911PyDoc_STRVAR(posix_makedev__doc__,
4912"makedev(major, minor) -> device number\n\
4913Composes a raw device number from the major and minor device numbers.");
4914
4915static PyObject *
4916posix_makedev(PyObject *self, PyObject *args)
4917{
4918 int major, minor;
4919 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
4920 return NULL;
4921 return PyInt_FromLong((long)makedev(major, minor));
4922}
4923#endif /* device macros */
4924
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004925
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004926#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004927PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004928"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004929Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004930
Barry Warsaw53699e91996-12-10 23:23:01 +00004931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004932posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004933{
4934 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004935 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004936 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004937 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004938
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004939 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004940 return NULL;
4941
4942#if !defined(HAVE_LARGEFILE_SUPPORT)
4943 length = PyInt_AsLong(lenobj);
4944#else
4945 length = PyLong_Check(lenobj) ?
4946 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
4947#endif
4948 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004949 return NULL;
4950
Barry Warsaw53699e91996-12-10 23:23:01 +00004951 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004952 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00004953 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004954 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004955 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004956 return NULL;
4957 }
Barry Warsaw53699e91996-12-10 23:23:01 +00004958 Py_INCREF(Py_None);
4959 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004960}
4961#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004962
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004963#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004964PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004965"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004966Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004967
Fred Drake762e2061999-08-26 17:23:54 +00004968/* Save putenv() parameters as values here, so we can collect them when they
4969 * get re-set with another call for the same key. */
4970static PyObject *posix_putenv_garbage;
4971
Tim Peters5aa91602002-01-30 05:46:57 +00004972static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004973posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004974{
4975 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004976 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00004977 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00004978 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004979
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004980 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004981 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00004982
4983#if defined(PYOS_OS2)
4984 if (stricmp(s1, "BEGINLIBPATH") == 0) {
4985 APIRET rc;
4986
Guido van Rossumd48f2521997-12-05 22:19:34 +00004987 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
4988 if (rc != NO_ERROR)
4989 return os2_error(rc);
4990
4991 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
4992 APIRET rc;
4993
Guido van Rossumd48f2521997-12-05 22:19:34 +00004994 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
4995 if (rc != NO_ERROR)
4996 return os2_error(rc);
4997 } else {
4998#endif
4999
Fred Drake762e2061999-08-26 17:23:54 +00005000 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005001 len = strlen(s1) + strlen(s2) + 2;
5002 /* len includes space for a trailing \0; the size arg to
5003 PyString_FromStringAndSize does not count that */
5004 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005005 if (newstr == NULL)
5006 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005007 newenv = PyString_AS_STRING(newstr);
5008 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5009 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005010 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005011 posix_error();
5012 return NULL;
5013 }
Fred Drake762e2061999-08-26 17:23:54 +00005014 /* Install the first arg and newstr in posix_putenv_garbage;
5015 * this will cause previous value to be collected. This has to
5016 * happen after the real putenv() call because the old value
5017 * was still accessible until then. */
5018 if (PyDict_SetItem(posix_putenv_garbage,
5019 PyTuple_GET_ITEM(args, 0), newstr)) {
5020 /* really not much we can do; just leak */
5021 PyErr_Clear();
5022 }
5023 else {
5024 Py_DECREF(newstr);
5025 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005026
5027#if defined(PYOS_OS2)
5028 }
5029#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005030 Py_INCREF(Py_None);
5031 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005032}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005033#endif /* putenv */
5034
Guido van Rossumc524d952001-10-19 01:31:59 +00005035#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005036PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005037"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005038Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005039
5040static PyObject *
5041posix_unsetenv(PyObject *self, PyObject *args)
5042{
5043 char *s1;
5044
5045 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5046 return NULL;
5047
5048 unsetenv(s1);
5049
5050 /* Remove the key from posix_putenv_garbage;
5051 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005052 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005053 * old value was still accessible until then.
5054 */
5055 if (PyDict_DelItem(posix_putenv_garbage,
5056 PyTuple_GET_ITEM(args, 0))) {
5057 /* really not much we can do; just leak */
5058 PyErr_Clear();
5059 }
5060
5061 Py_INCREF(Py_None);
5062 return Py_None;
5063}
5064#endif /* unsetenv */
5065
Guido van Rossumb6a47161997-09-15 22:54:34 +00005066#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005067PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005068"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005069Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005070
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005071static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005072posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005073{
5074 int code;
5075 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005076 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005077 return NULL;
5078 message = strerror(code);
5079 if (message == NULL) {
5080 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005081 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005082 return NULL;
5083 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005084 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005085}
5086#endif /* strerror */
5087
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005088
Guido van Rossumc9641791998-08-04 15:26:23 +00005089#ifdef HAVE_SYS_WAIT_H
5090
Fred Drake106c1a02002-04-23 15:58:02 +00005091#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005092PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005093"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005094Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005095
5096static PyObject *
5097posix_WCOREDUMP(PyObject *self, PyObject *args)
5098{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005099 WAIT_TYPE status;
5100 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005101
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005102 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005103 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005104
5105 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005106}
5107#endif /* WCOREDUMP */
5108
5109#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005110PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005111"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005112Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005113job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005114
5115static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005116posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005117{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005118 WAIT_TYPE status;
5119 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005120
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005121 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005122 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005123
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005124 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005125}
5126#endif /* WIFCONTINUED */
5127
Guido van Rossumc9641791998-08-04 15:26:23 +00005128#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005129PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005130"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005131Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005132
5133static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005134posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005135{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005136 WAIT_TYPE status;
5137 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005138
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005139 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005140 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005141
Fred Drake106c1a02002-04-23 15:58:02 +00005142 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005143}
5144#endif /* WIFSTOPPED */
5145
5146#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005147PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005148"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005149Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005150
5151static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005152posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005153{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005154 WAIT_TYPE status;
5155 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005156
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005157 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005158 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005159
Fred Drake106c1a02002-04-23 15:58:02 +00005160 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005161}
5162#endif /* WIFSIGNALED */
5163
5164#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005165PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005166"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005167Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005168system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005169
5170static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005171posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005172{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005173 WAIT_TYPE status;
5174 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005175
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005176 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005177 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005178
Fred Drake106c1a02002-04-23 15:58:02 +00005179 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005180}
5181#endif /* WIFEXITED */
5182
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005183#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005184PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005185"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005186Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005187
5188static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005189posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005190{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005191 WAIT_TYPE status;
5192 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005193
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005194 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005195 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005196
Guido van Rossumc9641791998-08-04 15:26:23 +00005197 return Py_BuildValue("i", WEXITSTATUS(status));
5198}
5199#endif /* WEXITSTATUS */
5200
5201#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005202PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005203"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005204Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005205value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005206
5207static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005208posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005209{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005210 WAIT_TYPE status;
5211 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005212
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005213 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005214 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005215
Guido van Rossumc9641791998-08-04 15:26:23 +00005216 return Py_BuildValue("i", WTERMSIG(status));
5217}
5218#endif /* WTERMSIG */
5219
5220#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005221PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005222"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005223Return the signal that stopped the process that provided\n\
5224the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005225
5226static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005227posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005228{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005229 WAIT_TYPE status;
5230 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005231
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005232 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005233 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005234
Guido van Rossumc9641791998-08-04 15:26:23 +00005235 return Py_BuildValue("i", WSTOPSIG(status));
5236}
5237#endif /* WSTOPSIG */
5238
5239#endif /* HAVE_SYS_WAIT_H */
5240
5241
Thomas Wouters477c8d52006-05-27 19:21:47 +00005242#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005243#ifdef _SCO_DS
5244/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5245 needed definitions in sys/statvfs.h */
5246#define _SVID3
5247#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005248#include <sys/statvfs.h>
5249
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005250static PyObject*
5251_pystatvfs_fromstructstatvfs(struct statvfs st) {
5252 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5253 if (v == NULL)
5254 return NULL;
5255
5256#if !defined(HAVE_LARGEFILE_SUPPORT)
5257 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5258 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5259 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5260 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5261 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5262 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5263 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5264 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5265 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5266 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5267#else
5268 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5269 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005270 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005271 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005272 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005273 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005274 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005275 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005276 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005277 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005278 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005279 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005280 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005281 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005282 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5283 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5284#endif
5285
5286 return v;
5287}
5288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005289PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005290"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005291Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005292
5293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005294posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005295{
5296 int fd, res;
5297 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005298
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005299 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005300 return NULL;
5301 Py_BEGIN_ALLOW_THREADS
5302 res = fstatvfs(fd, &st);
5303 Py_END_ALLOW_THREADS
5304 if (res != 0)
5305 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005306
5307 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005308}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005309#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005310
5311
Thomas Wouters477c8d52006-05-27 19:21:47 +00005312#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005313#include <sys/statvfs.h>
5314
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005315PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005316"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005317Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005318
5319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005320posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005321{
5322 char *path;
5323 int res;
5324 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005325 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005326 return NULL;
5327 Py_BEGIN_ALLOW_THREADS
5328 res = statvfs(path, &st);
5329 Py_END_ALLOW_THREADS
5330 if (res != 0)
5331 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005332
5333 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005334}
5335#endif /* HAVE_STATVFS */
5336
5337
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005338#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005339PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005340"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005341Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005342The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005343or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005344
5345static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005346posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005347{
5348 PyObject *result = NULL;
5349 char *dir = NULL;
5350 char *pfx = NULL;
5351 char *name;
5352
5353 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5354 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005355
Skip Montanaro46fc3372007-08-12 11:44:53 +00005356 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5357 "tempnam is a potential security risk to your program",
5358 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005359 return NULL;
5360
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005361#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005362 name = _tempnam(dir, pfx);
5363#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005364 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005365#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005366 if (name == NULL)
5367 return PyErr_NoMemory();
5368 result = PyString_FromString(name);
5369 free(name);
5370 return result;
5371}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005372#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005373
5374
5375#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005376PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005377"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005378Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005379
5380static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005381posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005382{
5383 FILE *fp;
5384
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005385 fp = tmpfile();
5386 if (fp == NULL)
5387 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005388 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005389}
5390#endif
5391
5392
5393#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005394PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005395"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005396Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005397
5398static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005399posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005400{
5401 char buffer[L_tmpnam];
5402 char *name;
5403
Skip Montanaro46fc3372007-08-12 11:44:53 +00005404 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5405 "tmpnam is a potential security risk to your program",
5406 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005407 return NULL;
5408
Greg Wardb48bc172000-03-01 21:51:56 +00005409#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005410 name = tmpnam_r(buffer);
5411#else
5412 name = tmpnam(buffer);
5413#endif
5414 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005415 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005416#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005417 "unexpected NULL from tmpnam_r"
5418#else
5419 "unexpected NULL from tmpnam"
5420#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005421 );
5422 PyErr_SetObject(PyExc_OSError, err);
5423 Py_XDECREF(err);
5424 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005425 }
5426 return PyString_FromString(buffer);
5427}
5428#endif
5429
5430
Fred Drakec9680921999-12-13 16:37:25 +00005431/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5432 * It maps strings representing configuration variable names to
5433 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005434 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005435 * rarely-used constants. There are three separate tables that use
5436 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005437 *
5438 * This code is always included, even if none of the interfaces that
5439 * need it are included. The #if hackery needed to avoid it would be
5440 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005441 */
5442struct constdef {
5443 char *name;
5444 long value;
5445};
5446
Fred Drake12c6e2d1999-12-14 21:25:03 +00005447static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005448conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005449 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005450{
5451 if (PyInt_Check(arg)) {
5452 *valuep = PyInt_AS_LONG(arg);
5453 return 1;
5454 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005455 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005456 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005457 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005458 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005459 size_t hi = tablesize;
5460 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005461 const char *confname;
5462 Py_ssize_t namelen;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005463 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005464 PyErr_SetString(PyExc_TypeError,
5465 "configuration names must be strings or integers");
5466 return 0;
5467 }
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005468 confname = PyUnicode_AsString(arg);
5469 if (confname == NULL)
5470 return 0;
5471 namelen = strlen(confname);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005472 while (lo < hi) {
5473 mid = (lo + hi) / 2;
5474 cmp = strcmp(confname, table[mid].name);
5475 if (cmp < 0)
5476 hi = mid;
5477 else if (cmp > 0)
5478 lo = mid + 1;
5479 else {
5480 *valuep = table[mid].value;
5481 return 1;
5482 }
5483 }
5484 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005485 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005486 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005487}
5488
5489
5490#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5491static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005492#ifdef _PC_ABI_AIO_XFER_MAX
5493 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5494#endif
5495#ifdef _PC_ABI_ASYNC_IO
5496 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5497#endif
Fred Drakec9680921999-12-13 16:37:25 +00005498#ifdef _PC_ASYNC_IO
5499 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5500#endif
5501#ifdef _PC_CHOWN_RESTRICTED
5502 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5503#endif
5504#ifdef _PC_FILESIZEBITS
5505 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5506#endif
5507#ifdef _PC_LAST
5508 {"PC_LAST", _PC_LAST},
5509#endif
5510#ifdef _PC_LINK_MAX
5511 {"PC_LINK_MAX", _PC_LINK_MAX},
5512#endif
5513#ifdef _PC_MAX_CANON
5514 {"PC_MAX_CANON", _PC_MAX_CANON},
5515#endif
5516#ifdef _PC_MAX_INPUT
5517 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5518#endif
5519#ifdef _PC_NAME_MAX
5520 {"PC_NAME_MAX", _PC_NAME_MAX},
5521#endif
5522#ifdef _PC_NO_TRUNC
5523 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5524#endif
5525#ifdef _PC_PATH_MAX
5526 {"PC_PATH_MAX", _PC_PATH_MAX},
5527#endif
5528#ifdef _PC_PIPE_BUF
5529 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5530#endif
5531#ifdef _PC_PRIO_IO
5532 {"PC_PRIO_IO", _PC_PRIO_IO},
5533#endif
5534#ifdef _PC_SOCK_MAXBUF
5535 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5536#endif
5537#ifdef _PC_SYNC_IO
5538 {"PC_SYNC_IO", _PC_SYNC_IO},
5539#endif
5540#ifdef _PC_VDISABLE
5541 {"PC_VDISABLE", _PC_VDISABLE},
5542#endif
5543};
5544
Fred Drakec9680921999-12-13 16:37:25 +00005545static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005546conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005547{
5548 return conv_confname(arg, valuep, posix_constants_pathconf,
5549 sizeof(posix_constants_pathconf)
5550 / sizeof(struct constdef));
5551}
5552#endif
5553
5554#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005555PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005556"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005557Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005559
5560static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005561posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005562{
5563 PyObject *result = NULL;
5564 int name, fd;
5565
Fred Drake12c6e2d1999-12-14 21:25:03 +00005566 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5567 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005568 long limit;
5569
5570 errno = 0;
5571 limit = fpathconf(fd, name);
5572 if (limit == -1 && errno != 0)
5573 posix_error();
5574 else
5575 result = PyInt_FromLong(limit);
5576 }
5577 return result;
5578}
5579#endif
5580
5581
5582#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005583PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005584"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005585Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005586If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005587
5588static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005589posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005590{
5591 PyObject *result = NULL;
5592 int name;
5593 char *path;
5594
5595 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5596 conv_path_confname, &name)) {
5597 long limit;
5598
5599 errno = 0;
5600 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005601 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005602 if (errno == EINVAL)
5603 /* could be a path or name problem */
5604 posix_error();
5605 else
5606 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005607 }
Fred Drakec9680921999-12-13 16:37:25 +00005608 else
5609 result = PyInt_FromLong(limit);
5610 }
5611 return result;
5612}
5613#endif
5614
5615#ifdef HAVE_CONFSTR
5616static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005617#ifdef _CS_ARCHITECTURE
5618 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5619#endif
5620#ifdef _CS_HOSTNAME
5621 {"CS_HOSTNAME", _CS_HOSTNAME},
5622#endif
5623#ifdef _CS_HW_PROVIDER
5624 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5625#endif
5626#ifdef _CS_HW_SERIAL
5627 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5628#endif
5629#ifdef _CS_INITTAB_NAME
5630 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5631#endif
Fred Drakec9680921999-12-13 16:37:25 +00005632#ifdef _CS_LFS64_CFLAGS
5633 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5634#endif
5635#ifdef _CS_LFS64_LDFLAGS
5636 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5637#endif
5638#ifdef _CS_LFS64_LIBS
5639 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5640#endif
5641#ifdef _CS_LFS64_LINTFLAGS
5642 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5643#endif
5644#ifdef _CS_LFS_CFLAGS
5645 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5646#endif
5647#ifdef _CS_LFS_LDFLAGS
5648 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5649#endif
5650#ifdef _CS_LFS_LIBS
5651 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5652#endif
5653#ifdef _CS_LFS_LINTFLAGS
5654 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5655#endif
Fred Draked86ed291999-12-15 15:34:33 +00005656#ifdef _CS_MACHINE
5657 {"CS_MACHINE", _CS_MACHINE},
5658#endif
Fred Drakec9680921999-12-13 16:37:25 +00005659#ifdef _CS_PATH
5660 {"CS_PATH", _CS_PATH},
5661#endif
Fred Draked86ed291999-12-15 15:34:33 +00005662#ifdef _CS_RELEASE
5663 {"CS_RELEASE", _CS_RELEASE},
5664#endif
5665#ifdef _CS_SRPC_DOMAIN
5666 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5667#endif
5668#ifdef _CS_SYSNAME
5669 {"CS_SYSNAME", _CS_SYSNAME},
5670#endif
5671#ifdef _CS_VERSION
5672 {"CS_VERSION", _CS_VERSION},
5673#endif
Fred Drakec9680921999-12-13 16:37:25 +00005674#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5675 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5676#endif
5677#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5678 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5679#endif
5680#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5681 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5682#endif
5683#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5684 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5685#endif
5686#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5687 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5688#endif
5689#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5690 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5691#endif
5692#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5693 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5694#endif
5695#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5696 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5697#endif
5698#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5699 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5700#endif
5701#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5702 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5703#endif
5704#ifdef _CS_XBS5_LP64_OFF64_LIBS
5705 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5706#endif
5707#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5708 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5709#endif
5710#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5711 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5712#endif
5713#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5714 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5715#endif
5716#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5717 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5718#endif
5719#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5720 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5721#endif
Fred Draked86ed291999-12-15 15:34:33 +00005722#ifdef _MIPS_CS_AVAIL_PROCESSORS
5723 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5724#endif
5725#ifdef _MIPS_CS_BASE
5726 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5727#endif
5728#ifdef _MIPS_CS_HOSTID
5729 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5730#endif
5731#ifdef _MIPS_CS_HW_NAME
5732 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5733#endif
5734#ifdef _MIPS_CS_NUM_PROCESSORS
5735 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5736#endif
5737#ifdef _MIPS_CS_OSREL_MAJ
5738 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5739#endif
5740#ifdef _MIPS_CS_OSREL_MIN
5741 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5742#endif
5743#ifdef _MIPS_CS_OSREL_PATCH
5744 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5745#endif
5746#ifdef _MIPS_CS_OS_NAME
5747 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5748#endif
5749#ifdef _MIPS_CS_OS_PROVIDER
5750 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5751#endif
5752#ifdef _MIPS_CS_PROCESSORS
5753 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5754#endif
5755#ifdef _MIPS_CS_SERIAL
5756 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5757#endif
5758#ifdef _MIPS_CS_VENDOR
5759 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5760#endif
Fred Drakec9680921999-12-13 16:37:25 +00005761};
5762
5763static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005764conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005765{
5766 return conv_confname(arg, valuep, posix_constants_confstr,
5767 sizeof(posix_constants_confstr)
5768 / sizeof(struct constdef));
5769}
5770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005771PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005772"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005773Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005774
5775static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005776posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005777{
5778 PyObject *result = NULL;
5779 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005780 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005781
5782 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005783 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005784
Fred Drakec9680921999-12-13 16:37:25 +00005785 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005786 len = confstr(name, buffer, sizeof(buffer));
5787 if (len == 0) {
5788 if (errno) {
5789 posix_error();
5790 }
5791 else {
5792 result = Py_None;
5793 Py_INCREF(Py_None);
5794 }
Fred Drakec9680921999-12-13 16:37:25 +00005795 }
5796 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005797 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005798 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005799 if (result != NULL)
Neal Norwitz93c56822007-08-26 07:10:06 +00005800 confstr(name, PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005801 }
5802 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005803 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005804 }
5805 }
5806 return result;
5807}
5808#endif
5809
5810
5811#ifdef HAVE_SYSCONF
5812static struct constdef posix_constants_sysconf[] = {
5813#ifdef _SC_2_CHAR_TERM
5814 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5815#endif
5816#ifdef _SC_2_C_BIND
5817 {"SC_2_C_BIND", _SC_2_C_BIND},
5818#endif
5819#ifdef _SC_2_C_DEV
5820 {"SC_2_C_DEV", _SC_2_C_DEV},
5821#endif
5822#ifdef _SC_2_C_VERSION
5823 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5824#endif
5825#ifdef _SC_2_FORT_DEV
5826 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5827#endif
5828#ifdef _SC_2_FORT_RUN
5829 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5830#endif
5831#ifdef _SC_2_LOCALEDEF
5832 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5833#endif
5834#ifdef _SC_2_SW_DEV
5835 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5836#endif
5837#ifdef _SC_2_UPE
5838 {"SC_2_UPE", _SC_2_UPE},
5839#endif
5840#ifdef _SC_2_VERSION
5841 {"SC_2_VERSION", _SC_2_VERSION},
5842#endif
Fred Draked86ed291999-12-15 15:34:33 +00005843#ifdef _SC_ABI_ASYNCHRONOUS_IO
5844 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5845#endif
5846#ifdef _SC_ACL
5847 {"SC_ACL", _SC_ACL},
5848#endif
Fred Drakec9680921999-12-13 16:37:25 +00005849#ifdef _SC_AIO_LISTIO_MAX
5850 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5851#endif
Fred Drakec9680921999-12-13 16:37:25 +00005852#ifdef _SC_AIO_MAX
5853 {"SC_AIO_MAX", _SC_AIO_MAX},
5854#endif
5855#ifdef _SC_AIO_PRIO_DELTA_MAX
5856 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5857#endif
5858#ifdef _SC_ARG_MAX
5859 {"SC_ARG_MAX", _SC_ARG_MAX},
5860#endif
5861#ifdef _SC_ASYNCHRONOUS_IO
5862 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5863#endif
5864#ifdef _SC_ATEXIT_MAX
5865 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5866#endif
Fred Draked86ed291999-12-15 15:34:33 +00005867#ifdef _SC_AUDIT
5868 {"SC_AUDIT", _SC_AUDIT},
5869#endif
Fred Drakec9680921999-12-13 16:37:25 +00005870#ifdef _SC_AVPHYS_PAGES
5871 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5872#endif
5873#ifdef _SC_BC_BASE_MAX
5874 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5875#endif
5876#ifdef _SC_BC_DIM_MAX
5877 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5878#endif
5879#ifdef _SC_BC_SCALE_MAX
5880 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5881#endif
5882#ifdef _SC_BC_STRING_MAX
5883 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5884#endif
Fred Draked86ed291999-12-15 15:34:33 +00005885#ifdef _SC_CAP
5886 {"SC_CAP", _SC_CAP},
5887#endif
Fred Drakec9680921999-12-13 16:37:25 +00005888#ifdef _SC_CHARCLASS_NAME_MAX
5889 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5890#endif
5891#ifdef _SC_CHAR_BIT
5892 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5893#endif
5894#ifdef _SC_CHAR_MAX
5895 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5896#endif
5897#ifdef _SC_CHAR_MIN
5898 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5899#endif
5900#ifdef _SC_CHILD_MAX
5901 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5902#endif
5903#ifdef _SC_CLK_TCK
5904 {"SC_CLK_TCK", _SC_CLK_TCK},
5905#endif
5906#ifdef _SC_COHER_BLKSZ
5907 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5908#endif
5909#ifdef _SC_COLL_WEIGHTS_MAX
5910 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5911#endif
5912#ifdef _SC_DCACHE_ASSOC
5913 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5914#endif
5915#ifdef _SC_DCACHE_BLKSZ
5916 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5917#endif
5918#ifdef _SC_DCACHE_LINESZ
5919 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
5920#endif
5921#ifdef _SC_DCACHE_SZ
5922 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
5923#endif
5924#ifdef _SC_DCACHE_TBLKSZ
5925 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
5926#endif
5927#ifdef _SC_DELAYTIMER_MAX
5928 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
5929#endif
5930#ifdef _SC_EQUIV_CLASS_MAX
5931 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
5932#endif
5933#ifdef _SC_EXPR_NEST_MAX
5934 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
5935#endif
5936#ifdef _SC_FSYNC
5937 {"SC_FSYNC", _SC_FSYNC},
5938#endif
5939#ifdef _SC_GETGR_R_SIZE_MAX
5940 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
5941#endif
5942#ifdef _SC_GETPW_R_SIZE_MAX
5943 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
5944#endif
5945#ifdef _SC_ICACHE_ASSOC
5946 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
5947#endif
5948#ifdef _SC_ICACHE_BLKSZ
5949 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
5950#endif
5951#ifdef _SC_ICACHE_LINESZ
5952 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
5953#endif
5954#ifdef _SC_ICACHE_SZ
5955 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
5956#endif
Fred Draked86ed291999-12-15 15:34:33 +00005957#ifdef _SC_INF
5958 {"SC_INF", _SC_INF},
5959#endif
Fred Drakec9680921999-12-13 16:37:25 +00005960#ifdef _SC_INT_MAX
5961 {"SC_INT_MAX", _SC_INT_MAX},
5962#endif
5963#ifdef _SC_INT_MIN
5964 {"SC_INT_MIN", _SC_INT_MIN},
5965#endif
5966#ifdef _SC_IOV_MAX
5967 {"SC_IOV_MAX", _SC_IOV_MAX},
5968#endif
Fred Draked86ed291999-12-15 15:34:33 +00005969#ifdef _SC_IP_SECOPTS
5970 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
5971#endif
Fred Drakec9680921999-12-13 16:37:25 +00005972#ifdef _SC_JOB_CONTROL
5973 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
5974#endif
Fred Draked86ed291999-12-15 15:34:33 +00005975#ifdef _SC_KERN_POINTERS
5976 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
5977#endif
5978#ifdef _SC_KERN_SIM
5979 {"SC_KERN_SIM", _SC_KERN_SIM},
5980#endif
Fred Drakec9680921999-12-13 16:37:25 +00005981#ifdef _SC_LINE_MAX
5982 {"SC_LINE_MAX", _SC_LINE_MAX},
5983#endif
5984#ifdef _SC_LOGIN_NAME_MAX
5985 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
5986#endif
5987#ifdef _SC_LOGNAME_MAX
5988 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
5989#endif
5990#ifdef _SC_LONG_BIT
5991 {"SC_LONG_BIT", _SC_LONG_BIT},
5992#endif
Fred Draked86ed291999-12-15 15:34:33 +00005993#ifdef _SC_MAC
5994 {"SC_MAC", _SC_MAC},
5995#endif
Fred Drakec9680921999-12-13 16:37:25 +00005996#ifdef _SC_MAPPED_FILES
5997 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
5998#endif
5999#ifdef _SC_MAXPID
6000 {"SC_MAXPID", _SC_MAXPID},
6001#endif
6002#ifdef _SC_MB_LEN_MAX
6003 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6004#endif
6005#ifdef _SC_MEMLOCK
6006 {"SC_MEMLOCK", _SC_MEMLOCK},
6007#endif
6008#ifdef _SC_MEMLOCK_RANGE
6009 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6010#endif
6011#ifdef _SC_MEMORY_PROTECTION
6012 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6013#endif
6014#ifdef _SC_MESSAGE_PASSING
6015 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6016#endif
Fred Draked86ed291999-12-15 15:34:33 +00006017#ifdef _SC_MMAP_FIXED_ALIGNMENT
6018 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6019#endif
Fred Drakec9680921999-12-13 16:37:25 +00006020#ifdef _SC_MQ_OPEN_MAX
6021 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6022#endif
6023#ifdef _SC_MQ_PRIO_MAX
6024 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6025#endif
Fred Draked86ed291999-12-15 15:34:33 +00006026#ifdef _SC_NACLS_MAX
6027 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6028#endif
Fred Drakec9680921999-12-13 16:37:25 +00006029#ifdef _SC_NGROUPS_MAX
6030 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6031#endif
6032#ifdef _SC_NL_ARGMAX
6033 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6034#endif
6035#ifdef _SC_NL_LANGMAX
6036 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6037#endif
6038#ifdef _SC_NL_MSGMAX
6039 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6040#endif
6041#ifdef _SC_NL_NMAX
6042 {"SC_NL_NMAX", _SC_NL_NMAX},
6043#endif
6044#ifdef _SC_NL_SETMAX
6045 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6046#endif
6047#ifdef _SC_NL_TEXTMAX
6048 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6049#endif
6050#ifdef _SC_NPROCESSORS_CONF
6051 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6052#endif
6053#ifdef _SC_NPROCESSORS_ONLN
6054 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6055#endif
Fred Draked86ed291999-12-15 15:34:33 +00006056#ifdef _SC_NPROC_CONF
6057 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6058#endif
6059#ifdef _SC_NPROC_ONLN
6060 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6061#endif
Fred Drakec9680921999-12-13 16:37:25 +00006062#ifdef _SC_NZERO
6063 {"SC_NZERO", _SC_NZERO},
6064#endif
6065#ifdef _SC_OPEN_MAX
6066 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6067#endif
6068#ifdef _SC_PAGESIZE
6069 {"SC_PAGESIZE", _SC_PAGESIZE},
6070#endif
6071#ifdef _SC_PAGE_SIZE
6072 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6073#endif
6074#ifdef _SC_PASS_MAX
6075 {"SC_PASS_MAX", _SC_PASS_MAX},
6076#endif
6077#ifdef _SC_PHYS_PAGES
6078 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6079#endif
6080#ifdef _SC_PII
6081 {"SC_PII", _SC_PII},
6082#endif
6083#ifdef _SC_PII_INTERNET
6084 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6085#endif
6086#ifdef _SC_PII_INTERNET_DGRAM
6087 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6088#endif
6089#ifdef _SC_PII_INTERNET_STREAM
6090 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6091#endif
6092#ifdef _SC_PII_OSI
6093 {"SC_PII_OSI", _SC_PII_OSI},
6094#endif
6095#ifdef _SC_PII_OSI_CLTS
6096 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6097#endif
6098#ifdef _SC_PII_OSI_COTS
6099 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6100#endif
6101#ifdef _SC_PII_OSI_M
6102 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6103#endif
6104#ifdef _SC_PII_SOCKET
6105 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6106#endif
6107#ifdef _SC_PII_XTI
6108 {"SC_PII_XTI", _SC_PII_XTI},
6109#endif
6110#ifdef _SC_POLL
6111 {"SC_POLL", _SC_POLL},
6112#endif
6113#ifdef _SC_PRIORITIZED_IO
6114 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6115#endif
6116#ifdef _SC_PRIORITY_SCHEDULING
6117 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6118#endif
6119#ifdef _SC_REALTIME_SIGNALS
6120 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6121#endif
6122#ifdef _SC_RE_DUP_MAX
6123 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6124#endif
6125#ifdef _SC_RTSIG_MAX
6126 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6127#endif
6128#ifdef _SC_SAVED_IDS
6129 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6130#endif
6131#ifdef _SC_SCHAR_MAX
6132 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6133#endif
6134#ifdef _SC_SCHAR_MIN
6135 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6136#endif
6137#ifdef _SC_SELECT
6138 {"SC_SELECT", _SC_SELECT},
6139#endif
6140#ifdef _SC_SEMAPHORES
6141 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6142#endif
6143#ifdef _SC_SEM_NSEMS_MAX
6144 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6145#endif
6146#ifdef _SC_SEM_VALUE_MAX
6147 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6148#endif
6149#ifdef _SC_SHARED_MEMORY_OBJECTS
6150 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6151#endif
6152#ifdef _SC_SHRT_MAX
6153 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6154#endif
6155#ifdef _SC_SHRT_MIN
6156 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6157#endif
6158#ifdef _SC_SIGQUEUE_MAX
6159 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6160#endif
6161#ifdef _SC_SIGRT_MAX
6162 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6163#endif
6164#ifdef _SC_SIGRT_MIN
6165 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6166#endif
Fred Draked86ed291999-12-15 15:34:33 +00006167#ifdef _SC_SOFTPOWER
6168 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6169#endif
Fred Drakec9680921999-12-13 16:37:25 +00006170#ifdef _SC_SPLIT_CACHE
6171 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6172#endif
6173#ifdef _SC_SSIZE_MAX
6174 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6175#endif
6176#ifdef _SC_STACK_PROT
6177 {"SC_STACK_PROT", _SC_STACK_PROT},
6178#endif
6179#ifdef _SC_STREAM_MAX
6180 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6181#endif
6182#ifdef _SC_SYNCHRONIZED_IO
6183 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6184#endif
6185#ifdef _SC_THREADS
6186 {"SC_THREADS", _SC_THREADS},
6187#endif
6188#ifdef _SC_THREAD_ATTR_STACKADDR
6189 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6190#endif
6191#ifdef _SC_THREAD_ATTR_STACKSIZE
6192 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6193#endif
6194#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6195 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6196#endif
6197#ifdef _SC_THREAD_KEYS_MAX
6198 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6199#endif
6200#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6201 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6202#endif
6203#ifdef _SC_THREAD_PRIO_INHERIT
6204 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6205#endif
6206#ifdef _SC_THREAD_PRIO_PROTECT
6207 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6208#endif
6209#ifdef _SC_THREAD_PROCESS_SHARED
6210 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6211#endif
6212#ifdef _SC_THREAD_SAFE_FUNCTIONS
6213 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6214#endif
6215#ifdef _SC_THREAD_STACK_MIN
6216 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6217#endif
6218#ifdef _SC_THREAD_THREADS_MAX
6219 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6220#endif
6221#ifdef _SC_TIMERS
6222 {"SC_TIMERS", _SC_TIMERS},
6223#endif
6224#ifdef _SC_TIMER_MAX
6225 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6226#endif
6227#ifdef _SC_TTY_NAME_MAX
6228 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6229#endif
6230#ifdef _SC_TZNAME_MAX
6231 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6232#endif
6233#ifdef _SC_T_IOV_MAX
6234 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6235#endif
6236#ifdef _SC_UCHAR_MAX
6237 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6238#endif
6239#ifdef _SC_UINT_MAX
6240 {"SC_UINT_MAX", _SC_UINT_MAX},
6241#endif
6242#ifdef _SC_UIO_MAXIOV
6243 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6244#endif
6245#ifdef _SC_ULONG_MAX
6246 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6247#endif
6248#ifdef _SC_USHRT_MAX
6249 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6250#endif
6251#ifdef _SC_VERSION
6252 {"SC_VERSION", _SC_VERSION},
6253#endif
6254#ifdef _SC_WORD_BIT
6255 {"SC_WORD_BIT", _SC_WORD_BIT},
6256#endif
6257#ifdef _SC_XBS5_ILP32_OFF32
6258 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6259#endif
6260#ifdef _SC_XBS5_ILP32_OFFBIG
6261 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6262#endif
6263#ifdef _SC_XBS5_LP64_OFF64
6264 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6265#endif
6266#ifdef _SC_XBS5_LPBIG_OFFBIG
6267 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6268#endif
6269#ifdef _SC_XOPEN_CRYPT
6270 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6271#endif
6272#ifdef _SC_XOPEN_ENH_I18N
6273 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6274#endif
6275#ifdef _SC_XOPEN_LEGACY
6276 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6277#endif
6278#ifdef _SC_XOPEN_REALTIME
6279 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6280#endif
6281#ifdef _SC_XOPEN_REALTIME_THREADS
6282 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6283#endif
6284#ifdef _SC_XOPEN_SHM
6285 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6286#endif
6287#ifdef _SC_XOPEN_UNIX
6288 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6289#endif
6290#ifdef _SC_XOPEN_VERSION
6291 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6292#endif
6293#ifdef _SC_XOPEN_XCU_VERSION
6294 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6295#endif
6296#ifdef _SC_XOPEN_XPG2
6297 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6298#endif
6299#ifdef _SC_XOPEN_XPG3
6300 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6301#endif
6302#ifdef _SC_XOPEN_XPG4
6303 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6304#endif
6305};
6306
6307static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006308conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006309{
6310 return conv_confname(arg, valuep, posix_constants_sysconf,
6311 sizeof(posix_constants_sysconf)
6312 / sizeof(struct constdef));
6313}
6314
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006315PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006316"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006317Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006318
6319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006320posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006321{
6322 PyObject *result = NULL;
6323 int name;
6324
6325 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6326 int value;
6327
6328 errno = 0;
6329 value = sysconf(name);
6330 if (value == -1 && errno != 0)
6331 posix_error();
6332 else
6333 result = PyInt_FromLong(value);
6334 }
6335 return result;
6336}
6337#endif
6338
6339
Fred Drakebec628d1999-12-15 18:31:10 +00006340/* This code is used to ensure that the tables of configuration value names
6341 * are in sorted order as required by conv_confname(), and also to build the
6342 * the exported dictionaries that are used to publish information about the
6343 * names available on the host platform.
6344 *
6345 * Sorting the table at runtime ensures that the table is properly ordered
6346 * when used, even for platforms we're not able to test on. It also makes
6347 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006348 */
Fred Drakebec628d1999-12-15 18:31:10 +00006349
6350static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006351cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006352{
6353 const struct constdef *c1 =
6354 (const struct constdef *) v1;
6355 const struct constdef *c2 =
6356 (const struct constdef *) v2;
6357
6358 return strcmp(c1->name, c2->name);
6359}
6360
6361static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006362setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006363 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006364{
Fred Drakebec628d1999-12-15 18:31:10 +00006365 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006366 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006367
6368 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6369 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006370 if (d == NULL)
6371 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006372
Barry Warsaw3155db32000-04-13 15:20:40 +00006373 for (i=0; i < tablesize; ++i) {
6374 PyObject *o = PyInt_FromLong(table[i].value);
6375 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6376 Py_XDECREF(o);
6377 Py_DECREF(d);
6378 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006379 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006380 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006381 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006382 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006383}
6384
Fred Drakebec628d1999-12-15 18:31:10 +00006385/* Return -1 on failure, 0 on success. */
6386static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006387setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006388{
6389#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006390 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006391 sizeof(posix_constants_pathconf)
6392 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006393 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006394 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006395#endif
6396#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006397 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006398 sizeof(posix_constants_confstr)
6399 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006400 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006401 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006402#endif
6403#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006404 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006405 sizeof(posix_constants_sysconf)
6406 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006407 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006408 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006409#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006410 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006411}
Fred Draked86ed291999-12-15 15:34:33 +00006412
6413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006414PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006415"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006416Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006417in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006418
6419static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006420posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006421{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006422 abort();
6423 /*NOTREACHED*/
6424 Py_FatalError("abort() called from Python code didn't abort!");
6425 return NULL;
6426}
Fred Drakebec628d1999-12-15 18:31:10 +00006427
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006428#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006429PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006430"startfile(filepath [, operation]) - Start a file with its associated\n\
6431application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006432\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006433When \"operation\" is not specified or \"open\", this acts like\n\
6434double-clicking the file in Explorer, or giving the file name as an\n\
6435argument to the DOS \"start\" command: the file is opened with whatever\n\
6436application (if any) its extension is associated.\n\
6437When another \"operation\" is given, it specifies what should be done with\n\
6438the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006439\n\
6440startfile returns as soon as the associated application is launched.\n\
6441There is no option to wait for the application to close, and no way\n\
6442to retrieve the application's exit status.\n\
6443\n\
6444The filepath is relative to the current directory. If you want to use\n\
6445an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006446the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006447
6448static PyObject *
6449win32_startfile(PyObject *self, PyObject *args)
6450{
6451 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006452 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006453 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006454#ifdef Py_WIN_WIDE_FILENAMES
6455 if (unicode_file_names()) {
6456 PyObject *unipath, *woperation = NULL;
6457 if (!PyArg_ParseTuple(args, "U|s:startfile",
6458 &unipath, &operation)) {
6459 PyErr_Clear();
6460 goto normal;
6461 }
6462
6463
6464 if (operation) {
6465 woperation = PyUnicode_DecodeASCII(operation,
6466 strlen(operation), NULL);
6467 if (!woperation) {
6468 PyErr_Clear();
6469 operation = NULL;
6470 goto normal;
6471 }
6472 }
6473
6474 Py_BEGIN_ALLOW_THREADS
6475 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6476 PyUnicode_AS_UNICODE(unipath),
6477 NULL, NULL, SW_SHOWNORMAL);
6478 Py_END_ALLOW_THREADS
6479
6480 Py_XDECREF(woperation);
6481 if (rc <= (HINSTANCE)32) {
6482 PyObject *errval = win32_error_unicode("startfile",
6483 PyUnicode_AS_UNICODE(unipath));
6484 return errval;
6485 }
6486 Py_INCREF(Py_None);
6487 return Py_None;
6488 }
6489#endif
6490
6491normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006492 if (!PyArg_ParseTuple(args, "et|s:startfile",
6493 Py_FileSystemDefaultEncoding, &filepath,
6494 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006495 return NULL;
6496 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006497 rc = ShellExecute((HWND)0, operation, filepath,
6498 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006499 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006500 if (rc <= (HINSTANCE)32) {
6501 PyObject *errval = win32_error("startfile", filepath);
6502 PyMem_Free(filepath);
6503 return errval;
6504 }
6505 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006506 Py_INCREF(Py_None);
6507 return Py_None;
6508}
6509#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006510
Martin v. Löwis438b5342002-12-27 10:16:42 +00006511#ifdef HAVE_GETLOADAVG
6512PyDoc_STRVAR(posix_getloadavg__doc__,
6513"getloadavg() -> (float, float, float)\n\n\
6514Return the number of processes in the system run queue averaged over\n\
6515the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6516was unobtainable");
6517
6518static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006519posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006520{
6521 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006522 if (getloadavg(loadavg, 3)!=3) {
6523 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6524 return NULL;
6525 } else
6526 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6527}
6528#endif
6529
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006530#ifdef MS_WINDOWS
6531
6532PyDoc_STRVAR(win32_urandom__doc__,
6533"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006534Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006535
6536typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6537 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6538 DWORD dwFlags );
6539typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6540 BYTE *pbBuffer );
6541
6542static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006543/* This handle is never explicitly released. Instead, the operating
6544 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006545static HCRYPTPROV hCryptProv = 0;
6546
Tim Peters4ad82172004-08-30 17:02:04 +00006547static PyObject*
6548win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006549{
Tim Petersd3115382004-08-30 17:36:46 +00006550 int howMany;
6551 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006552
Tim Peters4ad82172004-08-30 17:02:04 +00006553 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006554 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006555 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006556 if (howMany < 0)
6557 return PyErr_Format(PyExc_ValueError,
6558 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006559
Tim Peters4ad82172004-08-30 17:02:04 +00006560 if (hCryptProv == 0) {
6561 HINSTANCE hAdvAPI32 = NULL;
6562 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006563
Tim Peters4ad82172004-08-30 17:02:04 +00006564 /* Obtain handle to the DLL containing CryptoAPI
6565 This should not fail */
6566 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6567 if(hAdvAPI32 == NULL)
6568 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006569
Tim Peters4ad82172004-08-30 17:02:04 +00006570 /* Obtain pointers to the CryptoAPI functions
6571 This will fail on some early versions of Win95 */
6572 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6573 hAdvAPI32,
6574 "CryptAcquireContextA");
6575 if (pCryptAcquireContext == NULL)
6576 return PyErr_Format(PyExc_NotImplementedError,
6577 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006578
Tim Peters4ad82172004-08-30 17:02:04 +00006579 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6580 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006581 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006582 return PyErr_Format(PyExc_NotImplementedError,
6583 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006584
Tim Peters4ad82172004-08-30 17:02:04 +00006585 /* Acquire context */
6586 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6587 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6588 return win32_error("CryptAcquireContext", NULL);
6589 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006590
Tim Peters4ad82172004-08-30 17:02:04 +00006591 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006592 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006593 if (result != NULL) {
6594 /* Get random data */
6595 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006596 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006597 Py_DECREF(result);
6598 return win32_error("CryptGenRandom", NULL);
6599 }
Tim Peters4ad82172004-08-30 17:02:04 +00006600 }
Tim Petersd3115382004-08-30 17:36:46 +00006601 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006602}
6603#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006604
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006605PyDoc_STRVAR(device_encoding__doc__,
6606"device_encoding(fd) -> str\n\n\
6607Return a string describing the encoding of the device\n\
6608if the output is a terminal; else return None.");
6609
6610static PyObject *
6611device_encoding(PyObject *self, PyObject *args)
6612{
6613 int fd;
6614 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6615 return NULL;
6616 if (!isatty(fd)) {
6617 Py_INCREF(Py_None);
6618 return Py_None;
6619 }
6620#if defined(MS_WINDOWS) || defined(MS_WIN64)
6621 if (fd == 0) {
6622 char buf[100];
6623 sprintf(buf, "cp%d", GetConsoleCP());
6624 return PyUnicode_FromString(buf);
6625 }
6626 if (fd == 1 || fd == 2) {
6627 char buf[100];
6628 sprintf(buf, "cp%d", GetConsoleOutputCP());
6629 return PyUnicode_FromString(buf);
6630 }
6631#elif defined(CODESET)
6632 {
6633 char *codeset = nl_langinfo(CODESET);
6634 if (codeset)
6635 return PyUnicode_FromString(codeset);
6636 }
6637#endif
6638 Py_INCREF(Py_None);
6639 return Py_None;
6640}
6641
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006642#ifdef __VMS
6643/* Use openssl random routine */
6644#include <openssl/rand.h>
6645PyDoc_STRVAR(vms_urandom__doc__,
6646"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006647Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006648
6649static PyObject*
6650vms_urandom(PyObject *self, PyObject *args)
6651{
6652 int howMany;
6653 PyObject* result;
6654
6655 /* Read arguments */
6656 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6657 return NULL;
6658 if (howMany < 0)
6659 return PyErr_Format(PyExc_ValueError,
6660 "negative argument not allowed");
6661
6662 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006663 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006664 if (result != NULL) {
6665 /* Get random data */
6666 if (RAND_pseudo_bytes((unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006667 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006668 howMany) < 0) {
6669 Py_DECREF(result);
6670 return PyErr_Format(PyExc_ValueError,
6671 "RAND_pseudo_bytes");
6672 }
6673 }
6674 return result;
6675}
6676#endif
6677
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006678static PyMethodDef posix_methods[] = {
6679 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6680#ifdef HAVE_TTYNAME
6681 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6682#endif
6683 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006684#ifdef HAVE_CHFLAGS
6685 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6686#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006687 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006688#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006689 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006690#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006691#ifdef HAVE_LCHFLAGS
6692 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6693#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006694#ifdef HAVE_LCHOWN
6695 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6696#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006697#ifdef HAVE_CHROOT
6698 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6699#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006700#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006701 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006702#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006703#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006704 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006705 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006706#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006707#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006708 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006709#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006710 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6711 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6712 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006713#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006714 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006715#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006716#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006717 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006718#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006719 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6720 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6721 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006722 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006723#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006724 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006725#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006726#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006727 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006728#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006729 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006730#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006731 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006732#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006733 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6734 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6735 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006736#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006737 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006738#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006739 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006740#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006741 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6742 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006743#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006744#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006745 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6746 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006747#if defined(PYOS_OS2)
6748 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6749 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6750#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006751#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006752#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006753 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006754#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006755#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006756 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006757#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006758#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006759 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006760#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006761#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006762 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006763#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006764#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006765 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006766#endif /* HAVE_GETEGID */
6767#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006768 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006769#endif /* HAVE_GETEUID */
6770#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006771 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006772#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006773#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006774 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006775#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006776 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006777#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006778 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006779#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006780#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006781 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006782#endif /* HAVE_GETPPID */
6783#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006784 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006785#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006786#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006787 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006788#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006789#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006790 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006791#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006792#ifdef HAVE_KILLPG
6793 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6794#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006795#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006796 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006797#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006798#ifdef MS_WINDOWS
6799 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6800#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006801#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006802 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006803#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006804#ifdef HAVE_SETEUID
6805 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6806#endif /* HAVE_SETEUID */
6807#ifdef HAVE_SETEGID
6808 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6809#endif /* HAVE_SETEGID */
6810#ifdef HAVE_SETREUID
6811 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6812#endif /* HAVE_SETREUID */
6813#ifdef HAVE_SETREGID
6814 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6815#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006816#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006817 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006818#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006819#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006820 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006821#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006822#ifdef HAVE_GETPGID
6823 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6824#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006825#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006826 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006827#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006828#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006829 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006830#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006831#ifdef HAVE_WAIT3
6832 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6833#endif /* HAVE_WAIT3 */
6834#ifdef HAVE_WAIT4
6835 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6836#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006837#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006838 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006839#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006840#ifdef HAVE_GETSID
6841 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6842#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006843#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006844 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006845#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006846#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006847 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006848#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006849#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006850 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006851#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006852#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006853 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006854#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006855 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6856 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006857 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006858 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6859 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6860 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6861 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6862 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6863 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006864 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006865#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006866 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006867#endif
6868#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006869 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006870#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006871#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006872 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6873#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006874#ifdef HAVE_DEVICE_MACROS
6875 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6876 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6877 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6878#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006879#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006880 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006881#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006882#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006883 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006884#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006885#ifdef HAVE_UNSETENV
6886 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6887#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00006888#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006889 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00006890#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00006891#ifdef HAVE_FCHDIR
6892 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6893#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006894#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006895 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006896#endif
6897#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006898 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006899#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006900#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006901#ifdef WCOREDUMP
6902 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6903#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006904#ifdef WIFCONTINUED
6905 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6906#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006907#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006908 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006909#endif /* WIFSTOPPED */
6910#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006911 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006912#endif /* WIFSIGNALED */
6913#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006914 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006915#endif /* WIFEXITED */
6916#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006917 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006918#endif /* WEXITSTATUS */
6919#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006920 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006921#endif /* WTERMSIG */
6922#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006923 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006924#endif /* WSTOPSIG */
6925#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00006926#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006927 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006928#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00006929#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006930 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006931#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00006932#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00006933 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006934#endif
6935#ifdef HAVE_TEMPNAM
6936 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
6937#endif
6938#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00006939 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006940#endif
Fred Drakec9680921999-12-13 16:37:25 +00006941#ifdef HAVE_CONFSTR
6942 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
6943#endif
6944#ifdef HAVE_SYSCONF
6945 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
6946#endif
6947#ifdef HAVE_FPATHCONF
6948 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
6949#endif
6950#ifdef HAVE_PATHCONF
6951 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
6952#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006953 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006954#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00006955 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
6956#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006957#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00006958 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00006959#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006960 #ifdef MS_WINDOWS
6961 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
6962 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006963 #ifdef __VMS
6964 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
6965 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006966 {NULL, NULL} /* Sentinel */
6967};
6968
6969
Barry Warsaw4a342091996-12-19 23:50:02 +00006970static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006971ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00006972{
Fred Drake4d1e64b2002-04-15 19:40:07 +00006973 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00006974}
6975
Guido van Rossumd48f2521997-12-05 22:19:34 +00006976#if defined(PYOS_OS2)
6977/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006978static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006979{
6980 APIRET rc;
6981 ULONG values[QSV_MAX+1];
6982 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00006983 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00006984
6985 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00006986 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006987 Py_END_ALLOW_THREADS
6988
6989 if (rc != NO_ERROR) {
6990 os2_error(rc);
6991 return -1;
6992 }
6993
Fred Drake4d1e64b2002-04-15 19:40:07 +00006994 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6995 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
6996 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
6997 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
6998 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
6999 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7000 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007001
7002 switch (values[QSV_VERSION_MINOR]) {
7003 case 0: ver = "2.00"; break;
7004 case 10: ver = "2.10"; break;
7005 case 11: ver = "2.11"; break;
7006 case 30: ver = "3.00"; break;
7007 case 40: ver = "4.00"; break;
7008 case 50: ver = "5.00"; break;
7009 default:
Tim Peters885d4572001-11-28 20:27:42 +00007010 PyOS_snprintf(tmp, sizeof(tmp),
7011 "%d-%d", values[QSV_VERSION_MAJOR],
7012 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007013 ver = &tmp[0];
7014 }
7015
7016 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007017 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007018 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007019
7020 /* Add Indicator of Which Drive was Used to Boot the System */
7021 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7022 tmp[1] = ':';
7023 tmp[2] = '\0';
7024
Fred Drake4d1e64b2002-04-15 19:40:07 +00007025 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007026}
7027#endif
7028
Barry Warsaw4a342091996-12-19 23:50:02 +00007029static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007030all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007031{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007032#ifdef F_OK
7033 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007034#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007035#ifdef R_OK
7036 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007037#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007038#ifdef W_OK
7039 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007040#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007041#ifdef X_OK
7042 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007043#endif
Fred Drakec9680921999-12-13 16:37:25 +00007044#ifdef NGROUPS_MAX
7045 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7046#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007047#ifdef TMP_MAX
7048 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7049#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007050#ifdef WCONTINUED
7051 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7052#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007053#ifdef WNOHANG
7054 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007055#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007056#ifdef WUNTRACED
7057 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7058#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007059#ifdef O_RDONLY
7060 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7061#endif
7062#ifdef O_WRONLY
7063 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7064#endif
7065#ifdef O_RDWR
7066 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7067#endif
7068#ifdef O_NDELAY
7069 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7070#endif
7071#ifdef O_NONBLOCK
7072 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7073#endif
7074#ifdef O_APPEND
7075 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7076#endif
7077#ifdef O_DSYNC
7078 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7079#endif
7080#ifdef O_RSYNC
7081 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7082#endif
7083#ifdef O_SYNC
7084 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7085#endif
7086#ifdef O_NOCTTY
7087 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7088#endif
7089#ifdef O_CREAT
7090 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7091#endif
7092#ifdef O_EXCL
7093 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7094#endif
7095#ifdef O_TRUNC
7096 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7097#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007098#ifdef O_BINARY
7099 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7100#endif
7101#ifdef O_TEXT
7102 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7103#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007104#ifdef O_LARGEFILE
7105 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7106#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007107#ifdef O_SHLOCK
7108 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7109#endif
7110#ifdef O_EXLOCK
7111 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7112#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007113
Tim Peters5aa91602002-01-30 05:46:57 +00007114/* MS Windows */
7115#ifdef O_NOINHERIT
7116 /* Don't inherit in child processes. */
7117 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7118#endif
7119#ifdef _O_SHORT_LIVED
7120 /* Optimize for short life (keep in memory). */
7121 /* MS forgot to define this one with a non-underscore form too. */
7122 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7123#endif
7124#ifdef O_TEMPORARY
7125 /* Automatically delete when last handle is closed. */
7126 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7127#endif
7128#ifdef O_RANDOM
7129 /* Optimize for random access. */
7130 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7131#endif
7132#ifdef O_SEQUENTIAL
7133 /* Optimize for sequential access. */
7134 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7135#endif
7136
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007137/* GNU extensions. */
7138#ifdef O_DIRECT
7139 /* Direct disk access. */
7140 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7141#endif
7142#ifdef O_DIRECTORY
7143 /* Must be a directory. */
7144 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7145#endif
7146#ifdef O_NOFOLLOW
7147 /* Do not follow links. */
7148 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7149#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007150
Barry Warsaw5676bd12003-01-07 20:57:09 +00007151 /* These come from sysexits.h */
7152#ifdef EX_OK
7153 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007154#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007155#ifdef EX_USAGE
7156 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007157#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007158#ifdef EX_DATAERR
7159 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007160#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007161#ifdef EX_NOINPUT
7162 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007163#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007164#ifdef EX_NOUSER
7165 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007166#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007167#ifdef EX_NOHOST
7168 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007169#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007170#ifdef EX_UNAVAILABLE
7171 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007172#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007173#ifdef EX_SOFTWARE
7174 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007175#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007176#ifdef EX_OSERR
7177 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007178#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007179#ifdef EX_OSFILE
7180 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007181#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007182#ifdef EX_CANTCREAT
7183 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007184#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007185#ifdef EX_IOERR
7186 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007187#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007188#ifdef EX_TEMPFAIL
7189 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007190#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007191#ifdef EX_PROTOCOL
7192 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007193#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007194#ifdef EX_NOPERM
7195 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007196#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007197#ifdef EX_CONFIG
7198 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007199#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007200#ifdef EX_NOTFOUND
7201 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007202#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007203
Guido van Rossum246bc171999-02-01 23:54:31 +00007204#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007205#if defined(PYOS_OS2) && defined(PYCC_GCC)
7206 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7207 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7208 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7209 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7210 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7211 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7212 if (ins(d, "P_PM", (long)P_PM)) return -1;
7213 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7214 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7215 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7216 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7217 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7218 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7219 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7220 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7221 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7222 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7223 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7224 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7225 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7226#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007227 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7228 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7229 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7230 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7231 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007232#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007233#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007234
Guido van Rossumd48f2521997-12-05 22:19:34 +00007235#if defined(PYOS_OS2)
7236 if (insertvalues(d)) return -1;
7237#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007238 return 0;
7239}
7240
7241
Tim Peters5aa91602002-01-30 05:46:57 +00007242#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007243#define INITFUNC initnt
7244#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007245
7246#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007247#define INITFUNC initos2
7248#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007249
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007250#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007251#define INITFUNC initposix
7252#define MODNAME "posix"
7253#endif
7254
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007255PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007256INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007257{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007258 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007259
Fred Drake4d1e64b2002-04-15 19:40:07 +00007260 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007261 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007262 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007263 if (m == NULL)
7264 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007265
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007266 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007267 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007268 Py_XINCREF(v);
7269 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007270 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007271 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007272
Fred Drake4d1e64b2002-04-15 19:40:07 +00007273 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007274 return;
7275
Fred Drake4d1e64b2002-04-15 19:40:07 +00007276 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007277 return;
7278
Fred Drake4d1e64b2002-04-15 19:40:07 +00007279 Py_INCREF(PyExc_OSError);
7280 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007281
Guido van Rossumb3d39562000-01-31 18:41:26 +00007282#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007283 if (posix_putenv_garbage == NULL)
7284 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007285#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007286
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007287 if (!initialized) {
7288 stat_result_desc.name = MODNAME ".stat_result";
7289 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7290 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7291 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7292 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7293 structseq_new = StatResultType.tp_new;
7294 StatResultType.tp_new = statresult_new;
7295
7296 statvfs_result_desc.name = MODNAME ".statvfs_result";
7297 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7298 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007299 Py_INCREF((PyObject*) &StatResultType);
7300 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007301 Py_INCREF((PyObject*) &StatVFSResultType);
7302 PyModule_AddObject(m, "statvfs_result",
7303 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007304 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007305
7306#ifdef __APPLE__
7307 /*
7308 * Step 2 of weak-linking support on Mac OS X.
7309 *
7310 * The code below removes functions that are not available on the
7311 * currently active platform.
7312 *
7313 * This block allow one to use a python binary that was build on
7314 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7315 * OSX 10.4.
7316 */
7317#ifdef HAVE_FSTATVFS
7318 if (fstatvfs == NULL) {
7319 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7320 return;
7321 }
7322 }
7323#endif /* HAVE_FSTATVFS */
7324
7325#ifdef HAVE_STATVFS
7326 if (statvfs == NULL) {
7327 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7328 return;
7329 }
7330 }
7331#endif /* HAVE_STATVFS */
7332
7333# ifdef HAVE_LCHOWN
7334 if (lchown == NULL) {
7335 if (PyObject_DelAttrString(m, "lchown") == -1) {
7336 return;
7337 }
7338 }
7339#endif /* HAVE_LCHOWN */
7340
7341
7342#endif /* __APPLE__ */
7343
Guido van Rossumb6775db1994-08-01 11:34:53 +00007344}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007345
7346#ifdef __cplusplus
7347}
7348#endif