blob: 000f6a2ca3453b4e2faf7b65a98fe9bf27c5a99c [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);
390 PyDict_SetItemString(d, "BEGINLIBPATH", v);
391 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);
396 PyDict_SetItemString(d, "ENDLIBPATH", v);
397 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();
1584 return PyString_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();
1606 return PyString_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();
Barry Warsaw53699e91996-12-10 23:23:01 +00001886 return PyString_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 }
2837
Barry Warsaw53699e91996-12-10 23:23:01 +00002838 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002839 if (argvlist == NULL) {
2840 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002841 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002842 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002843 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002844 if (!PyArg_Parse((*getitem)(argv, i), "et",
2845 Py_FileSystemDefaultEncoding,
2846 &argvlist[i])) {
2847 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002848 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002849 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002850 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002851 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002852
Guido van Rossum85e3b011991-06-03 12:42:10 +00002853 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002854 }
2855 argvlist[argc] = NULL;
2856
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002857 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002858
Guido van Rossum85e3b011991-06-03 12:42:10 +00002859 /* If we get here it's definitely an error */
2860
Martin v. Löwis114619e2002-10-07 06:44:21 +00002861 free_string_array(argvlist, argc);
2862 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002863 return posix_error();
2864}
2865
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002867PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002868"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002869Execute a path with arguments and environment, replacing current process.\n\
2870\n\
2871 path: path of executable file\n\
2872 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002873 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874
Barry Warsaw53699e91996-12-10 23:23:01 +00002875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002876posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002877{
2878 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002879 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002880 char **argvlist;
2881 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002882 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002883 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002884 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002885 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002886
2887 /* execve has three arguments: (path, argv, env), where
2888 argv is a list or tuple of strings and env is a dictionary
2889 like posix.environ. */
2890
Martin v. Löwis114619e2002-10-07 06:44:21 +00002891 if (!PyArg_ParseTuple(args, "etOO:execve",
2892 Py_FileSystemDefaultEncoding,
2893 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002894 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002895 if (PyList_Check(argv)) {
2896 argc = PyList_Size(argv);
2897 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002898 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002899 else if (PyTuple_Check(argv)) {
2900 argc = PyTuple_Size(argv);
2901 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002902 }
2903 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002904 PyErr_SetString(PyExc_TypeError,
2905 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002906 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002907 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002908 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002909 PyErr_SetString(PyExc_TypeError,
2910 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002911 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002912 }
2913
Barry Warsaw53699e91996-12-10 23:23:01 +00002914 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002915 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002916 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002917 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002918 }
2919 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002920 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002921 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002922 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002923 &argvlist[i]))
2924 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002925 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002926 goto fail_1;
2927 }
2928 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002929 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002930 argvlist[argc] = NULL;
2931
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002932 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002933 if (i < 0)
2934 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002935 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002936 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002937 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002938 goto fail_1;
2939 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002940 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002941 keys = PyMapping_Keys(env);
2942 vals = PyMapping_Values(env);
2943 if (!keys || !vals)
2944 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002945 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2946 PyErr_SetString(PyExc_TypeError,
2947 "execve(): env.keys() or env.values() is not a list");
2948 goto fail_2;
2949 }
Tim Peters5aa91602002-01-30 05:46:57 +00002950
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002951 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002952 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002953 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002954
2955 key = PyList_GetItem(keys, pos);
2956 val = PyList_GetItem(vals, pos);
2957 if (!key || !val)
2958 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002959
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002960 if (!PyArg_Parse(
2961 key,
2962 "s;execve() arg 3 contains a non-string key",
2963 &k) ||
2964 !PyArg_Parse(
2965 val,
2966 "s;execve() arg 3 contains a non-string value",
2967 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002968 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002969 goto fail_2;
2970 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002971
2972#if defined(PYOS_OS2)
2973 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2974 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2975#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002976 len = PyString_Size(key) + PyString_Size(val) + 2;
2977 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002978 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002979 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002980 goto fail_2;
2981 }
Tim Petersc8996f52001-12-03 20:41:00 +00002982 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002983 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002984#if defined(PYOS_OS2)
2985 }
2986#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002987 }
2988 envlist[envc] = 0;
2989
2990 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002991
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002992 /* If we get here it's definitely an error */
2993
2994 (void) posix_error();
2995
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002996 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002997 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002998 PyMem_DEL(envlist[envc]);
2999 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003000 fail_1:
3001 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003002 Py_XDECREF(vals);
3003 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003004 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003005 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003006 return NULL;
3007}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003008#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003009
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003010
Guido van Rossuma1065681999-01-25 23:20:23 +00003011#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003013"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003014Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003015\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003016 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003017 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003018 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003019
3020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003021posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003022{
3023 char *path;
3024 PyObject *argv;
3025 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003026 int mode, i;
3027 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003028 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003029 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003030
3031 /* spawnv has three arguments: (mode, path, argv), where
3032 argv is a list or tuple of strings. */
3033
Martin v. Löwis114619e2002-10-07 06:44:21 +00003034 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3035 Py_FileSystemDefaultEncoding,
3036 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003037 return NULL;
3038 if (PyList_Check(argv)) {
3039 argc = PyList_Size(argv);
3040 getitem = PyList_GetItem;
3041 }
3042 else if (PyTuple_Check(argv)) {
3043 argc = PyTuple_Size(argv);
3044 getitem = PyTuple_GetItem;
3045 }
3046 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003047 PyErr_SetString(PyExc_TypeError,
3048 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003049 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003050 return NULL;
3051 }
3052
3053 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003054 if (argvlist == NULL) {
3055 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003056 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003057 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003058 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003059 if (!PyArg_Parse((*getitem)(argv, i), "et",
3060 Py_FileSystemDefaultEncoding,
3061 &argvlist[i])) {
3062 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003063 PyErr_SetString(
3064 PyExc_TypeError,
3065 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003066 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003067 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003068 }
3069 }
3070 argvlist[argc] = NULL;
3071
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003072#if defined(PYOS_OS2) && defined(PYCC_GCC)
3073 Py_BEGIN_ALLOW_THREADS
3074 spawnval = spawnv(mode, path, argvlist);
3075 Py_END_ALLOW_THREADS
3076#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003077 if (mode == _OLD_P_OVERLAY)
3078 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003079
Tim Peters25059d32001-12-07 20:35:43 +00003080 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003081 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003082 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003083#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003084
Martin v. Löwis114619e2002-10-07 06:44:21 +00003085 free_string_array(argvlist, argc);
3086 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003087
Fred Drake699f3522000-06-29 21:12:41 +00003088 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003089 return posix_error();
3090 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003091#if SIZEOF_LONG == SIZEOF_VOID_P
3092 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003093#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003094 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003095#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003096}
3097
3098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003099PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003100"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003101Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003102\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003103 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003104 path: path of executable file\n\
3105 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003107
3108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003109posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003110{
3111 char *path;
3112 PyObject *argv, *env;
3113 char **argvlist;
3114 char **envlist;
3115 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003116 int mode, pos, envc;
3117 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003118 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003119 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003121
3122 /* spawnve has four arguments: (mode, path, argv, env), where
3123 argv is a list or tuple of strings and env is a dictionary
3124 like posix.environ. */
3125
Martin v. Löwis114619e2002-10-07 06:44:21 +00003126 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3127 Py_FileSystemDefaultEncoding,
3128 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003129 return NULL;
3130 if (PyList_Check(argv)) {
3131 argc = PyList_Size(argv);
3132 getitem = PyList_GetItem;
3133 }
3134 else if (PyTuple_Check(argv)) {
3135 argc = PyTuple_Size(argv);
3136 getitem = PyTuple_GetItem;
3137 }
3138 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003139 PyErr_SetString(PyExc_TypeError,
3140 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003141 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003142 }
3143 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 PyErr_SetString(PyExc_TypeError,
3145 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003146 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003147 }
3148
3149 argvlist = PyMem_NEW(char *, argc+1);
3150 if (argvlist == NULL) {
3151 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003152 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003153 }
3154 for (i = 0; i < argc; i++) {
3155 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003156 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003157 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003158 &argvlist[i]))
3159 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003160 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003161 goto fail_1;
3162 }
3163 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003164 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003165 argvlist[argc] = NULL;
3166
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003167 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003168 if (i < 0)
3169 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003170 envlist = PyMem_NEW(char *, i + 1);
3171 if (envlist == NULL) {
3172 PyErr_NoMemory();
3173 goto fail_1;
3174 }
3175 envc = 0;
3176 keys = PyMapping_Keys(env);
3177 vals = PyMapping_Values(env);
3178 if (!keys || !vals)
3179 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003180 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3181 PyErr_SetString(PyExc_TypeError,
3182 "spawnve(): env.keys() or env.values() is not a list");
3183 goto fail_2;
3184 }
Tim Peters5aa91602002-01-30 05:46:57 +00003185
Guido van Rossuma1065681999-01-25 23:20:23 +00003186 for (pos = 0; pos < i; pos++) {
3187 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003188 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003189
3190 key = PyList_GetItem(keys, pos);
3191 val = PyList_GetItem(vals, pos);
3192 if (!key || !val)
3193 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003194
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003195 if (!PyArg_Parse(
3196 key,
3197 "s;spawnve() arg 3 contains a non-string key",
3198 &k) ||
3199 !PyArg_Parse(
3200 val,
3201 "s;spawnve() arg 3 contains a non-string value",
3202 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003203 {
3204 goto fail_2;
3205 }
Tim Petersc8996f52001-12-03 20:41:00 +00003206 len = PyString_Size(key) + PyString_Size(val) + 2;
3207 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003208 if (p == NULL) {
3209 PyErr_NoMemory();
3210 goto fail_2;
3211 }
Tim Petersc8996f52001-12-03 20:41:00 +00003212 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003213 envlist[envc++] = p;
3214 }
3215 envlist[envc] = 0;
3216
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003217#if defined(PYOS_OS2) && defined(PYCC_GCC)
3218 Py_BEGIN_ALLOW_THREADS
3219 spawnval = spawnve(mode, path, argvlist, envlist);
3220 Py_END_ALLOW_THREADS
3221#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003222 if (mode == _OLD_P_OVERLAY)
3223 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003224
3225 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003226 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003227 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003228#endif
Tim Peters25059d32001-12-07 20:35:43 +00003229
Fred Drake699f3522000-06-29 21:12:41 +00003230 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003231 (void) posix_error();
3232 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003233#if SIZEOF_LONG == SIZEOF_VOID_P
3234 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003235#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003236 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003237#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003238
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003239 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003240 while (--envc >= 0)
3241 PyMem_DEL(envlist[envc]);
3242 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003243 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003244 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003245 Py_XDECREF(vals);
3246 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003247 fail_0:
3248 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003249 return res;
3250}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003251
3252/* OS/2 supports spawnvp & spawnvpe natively */
3253#if defined(PYOS_OS2)
3254PyDoc_STRVAR(posix_spawnvp__doc__,
3255"spawnvp(mode, file, args)\n\n\
3256Execute the program 'file' in a new process, using the environment\n\
3257search path to find the file.\n\
3258\n\
3259 mode: mode of process creation\n\
3260 file: executable file name\n\
3261 args: tuple or list of strings");
3262
3263static PyObject *
3264posix_spawnvp(PyObject *self, PyObject *args)
3265{
3266 char *path;
3267 PyObject *argv;
3268 char **argvlist;
3269 int mode, i, argc;
3270 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003271 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003272
3273 /* spawnvp has three arguments: (mode, path, argv), where
3274 argv is a list or tuple of strings. */
3275
3276 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3277 Py_FileSystemDefaultEncoding,
3278 &path, &argv))
3279 return NULL;
3280 if (PyList_Check(argv)) {
3281 argc = PyList_Size(argv);
3282 getitem = PyList_GetItem;
3283 }
3284 else if (PyTuple_Check(argv)) {
3285 argc = PyTuple_Size(argv);
3286 getitem = PyTuple_GetItem;
3287 }
3288 else {
3289 PyErr_SetString(PyExc_TypeError,
3290 "spawnvp() arg 2 must be a tuple or list");
3291 PyMem_Free(path);
3292 return NULL;
3293 }
3294
3295 argvlist = PyMem_NEW(char *, argc+1);
3296 if (argvlist == NULL) {
3297 PyMem_Free(path);
3298 return PyErr_NoMemory();
3299 }
3300 for (i = 0; i < argc; i++) {
3301 if (!PyArg_Parse((*getitem)(argv, i), "et",
3302 Py_FileSystemDefaultEncoding,
3303 &argvlist[i])) {
3304 free_string_array(argvlist, i);
3305 PyErr_SetString(
3306 PyExc_TypeError,
3307 "spawnvp() arg 2 must contain only strings");
3308 PyMem_Free(path);
3309 return NULL;
3310 }
3311 }
3312 argvlist[argc] = NULL;
3313
3314 Py_BEGIN_ALLOW_THREADS
3315#if defined(PYCC_GCC)
3316 spawnval = spawnvp(mode, path, argvlist);
3317#else
3318 spawnval = _spawnvp(mode, path, argvlist);
3319#endif
3320 Py_END_ALLOW_THREADS
3321
3322 free_string_array(argvlist, argc);
3323 PyMem_Free(path);
3324
3325 if (spawnval == -1)
3326 return posix_error();
3327 else
3328 return Py_BuildValue("l", (long) spawnval);
3329}
3330
3331
3332PyDoc_STRVAR(posix_spawnvpe__doc__,
3333"spawnvpe(mode, file, args, env)\n\n\
3334Execute the program 'file' in a new process, using the environment\n\
3335search path to find the file.\n\
3336\n\
3337 mode: mode of process creation\n\
3338 file: executable file name\n\
3339 args: tuple or list of arguments\n\
3340 env: dictionary of strings mapping to strings");
3341
3342static PyObject *
3343posix_spawnvpe(PyObject *self, PyObject *args)
3344{
3345 char *path;
3346 PyObject *argv, *env;
3347 char **argvlist;
3348 char **envlist;
3349 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3350 int mode, i, pos, argc, envc;
3351 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003352 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003353 int lastarg = 0;
3354
3355 /* spawnvpe has four arguments: (mode, path, argv, env), where
3356 argv is a list or tuple of strings and env is a dictionary
3357 like posix.environ. */
3358
3359 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3360 Py_FileSystemDefaultEncoding,
3361 &path, &argv, &env))
3362 return NULL;
3363 if (PyList_Check(argv)) {
3364 argc = PyList_Size(argv);
3365 getitem = PyList_GetItem;
3366 }
3367 else if (PyTuple_Check(argv)) {
3368 argc = PyTuple_Size(argv);
3369 getitem = PyTuple_GetItem;
3370 }
3371 else {
3372 PyErr_SetString(PyExc_TypeError,
3373 "spawnvpe() arg 2 must be a tuple or list");
3374 goto fail_0;
3375 }
3376 if (!PyMapping_Check(env)) {
3377 PyErr_SetString(PyExc_TypeError,
3378 "spawnvpe() arg 3 must be a mapping object");
3379 goto fail_0;
3380 }
3381
3382 argvlist = PyMem_NEW(char *, argc+1);
3383 if (argvlist == NULL) {
3384 PyErr_NoMemory();
3385 goto fail_0;
3386 }
3387 for (i = 0; i < argc; i++) {
3388 if (!PyArg_Parse((*getitem)(argv, i),
3389 "et;spawnvpe() arg 2 must contain only strings",
3390 Py_FileSystemDefaultEncoding,
3391 &argvlist[i]))
3392 {
3393 lastarg = i;
3394 goto fail_1;
3395 }
3396 }
3397 lastarg = argc;
3398 argvlist[argc] = NULL;
3399
3400 i = PyMapping_Size(env);
3401 if (i < 0)
3402 goto fail_1;
3403 envlist = PyMem_NEW(char *, i + 1);
3404 if (envlist == NULL) {
3405 PyErr_NoMemory();
3406 goto fail_1;
3407 }
3408 envc = 0;
3409 keys = PyMapping_Keys(env);
3410 vals = PyMapping_Values(env);
3411 if (!keys || !vals)
3412 goto fail_2;
3413 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3414 PyErr_SetString(PyExc_TypeError,
3415 "spawnvpe(): env.keys() or env.values() is not a list");
3416 goto fail_2;
3417 }
3418
3419 for (pos = 0; pos < i; pos++) {
3420 char *p, *k, *v;
3421 size_t len;
3422
3423 key = PyList_GetItem(keys, pos);
3424 val = PyList_GetItem(vals, pos);
3425 if (!key || !val)
3426 goto fail_2;
3427
3428 if (!PyArg_Parse(
3429 key,
3430 "s;spawnvpe() arg 3 contains a non-string key",
3431 &k) ||
3432 !PyArg_Parse(
3433 val,
3434 "s;spawnvpe() arg 3 contains a non-string value",
3435 &v))
3436 {
3437 goto fail_2;
3438 }
3439 len = PyString_Size(key) + PyString_Size(val) + 2;
3440 p = PyMem_NEW(char, len);
3441 if (p == NULL) {
3442 PyErr_NoMemory();
3443 goto fail_2;
3444 }
3445 PyOS_snprintf(p, len, "%s=%s", k, v);
3446 envlist[envc++] = p;
3447 }
3448 envlist[envc] = 0;
3449
3450 Py_BEGIN_ALLOW_THREADS
3451#if defined(PYCC_GCC)
3452 spawnval = spawnve(mode, path, argvlist, envlist);
3453#else
3454 spawnval = _spawnve(mode, path, argvlist, envlist);
3455#endif
3456 Py_END_ALLOW_THREADS
3457
3458 if (spawnval == -1)
3459 (void) posix_error();
3460 else
3461 res = Py_BuildValue("l", (long) spawnval);
3462
3463 fail_2:
3464 while (--envc >= 0)
3465 PyMem_DEL(envlist[envc]);
3466 PyMem_DEL(envlist);
3467 fail_1:
3468 free_string_array(argvlist, lastarg);
3469 Py_XDECREF(vals);
3470 Py_XDECREF(keys);
3471 fail_0:
3472 PyMem_Free(path);
3473 return res;
3474}
3475#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003476#endif /* HAVE_SPAWNV */
3477
3478
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003479#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003480PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003481"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003482Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3483\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003484Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003485
3486static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003487posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003488{
Neal Norwitze241ce82003-02-17 18:17:05 +00003489 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003490 if (pid == -1)
3491 return posix_error();
3492 PyOS_AfterFork();
3493 return PyInt_FromLong((long)pid);
3494}
3495#endif
3496
3497
Guido van Rossumad0ee831995-03-01 10:34:45 +00003498#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003499PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003500"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003501Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003502Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003503
Barry Warsaw53699e91996-12-10 23:23:01 +00003504static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003505posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003506{
Neal Norwitze241ce82003-02-17 18:17:05 +00003507 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003508 if (pid == -1)
3509 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003510 if (pid == 0)
3511 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003512 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003513}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003514#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003515
Neal Norwitzb59798b2003-03-21 01:43:31 +00003516/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003517/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3518#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003519#define DEV_PTY_FILE "/dev/ptc"
3520#define HAVE_DEV_PTMX
3521#else
3522#define DEV_PTY_FILE "/dev/ptmx"
3523#endif
3524
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003525#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003526#ifdef HAVE_PTY_H
3527#include <pty.h>
3528#else
3529#ifdef HAVE_LIBUTIL_H
3530#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003531#endif /* HAVE_LIBUTIL_H */
3532#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003533#ifdef HAVE_STROPTS_H
3534#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003535#endif
3536#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003537
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003538#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003539PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003540"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003541Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003542
3543static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003544posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003545{
3546 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003547#ifndef HAVE_OPENPTY
3548 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003549#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003550#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003551 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003552#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003553 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003554#endif
3555#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003556
Thomas Wouters70c21a12000-07-14 14:28:33 +00003557#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003558 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3559 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003560#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003561 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3562 if (slave_name == NULL)
3563 return posix_error();
3564
3565 slave_fd = open(slave_name, O_RDWR);
3566 if (slave_fd < 0)
3567 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003568#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003569 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003570 if (master_fd < 0)
3571 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003572 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003573 /* change permission of slave */
3574 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003575 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003576 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003577 }
3578 /* unlock slave */
3579 if (unlockpt(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 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003583 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003584 slave_name = ptsname(master_fd); /* get name of slave */
3585 if (slave_name == NULL)
3586 return posix_error();
3587 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3588 if (slave_fd < 0)
3589 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003590#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003591 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3592 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003593#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003594 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003595#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003596#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003597#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003598
Fred Drake8cef4cf2000-06-28 16:40:38 +00003599 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003600
Fred Drake8cef4cf2000-06-28 16:40:38 +00003601}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003602#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003603
3604#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003605PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003606"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003607Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3608Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003609To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003610
3611static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003612posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003613{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003614 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003615
Fred Drake8cef4cf2000-06-28 16:40:38 +00003616 pid = forkpty(&master_fd, NULL, NULL, NULL);
3617 if (pid == -1)
3618 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003619 if (pid == 0)
3620 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003621 return Py_BuildValue("(ii)", pid, master_fd);
3622}
3623#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003624
Guido van Rossumad0ee831995-03-01 10:34:45 +00003625#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003626PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003627"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003628Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003629
Barry Warsaw53699e91996-12-10 23:23:01 +00003630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003631posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003632{
Barry Warsaw53699e91996-12-10 23:23:01 +00003633 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003634}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003635#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003636
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003637
Guido van Rossumad0ee831995-03-01 10:34:45 +00003638#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003639PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003640"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003641Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003642
Barry Warsaw53699e91996-12-10 23:23:01 +00003643static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003644posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003645{
Barry Warsaw53699e91996-12-10 23:23:01 +00003646 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003647}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003648#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003649
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003650
Guido van Rossumad0ee831995-03-01 10:34:45 +00003651#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003652PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003653"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003654Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003655
Barry Warsaw53699e91996-12-10 23:23:01 +00003656static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003657posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003658{
Barry Warsaw53699e91996-12-10 23:23:01 +00003659 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003660}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003661#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003662
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003664PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003665"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003666Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003667
Barry Warsaw53699e91996-12-10 23:23:01 +00003668static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003669posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003670{
Barry Warsaw53699e91996-12-10 23:23:01 +00003671 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003672}
3673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003674
Fred Drakec9680921999-12-13 16:37:25 +00003675#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003676PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003677"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003678Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003679
3680static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003681posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003682{
3683 PyObject *result = NULL;
3684
Fred Drakec9680921999-12-13 16:37:25 +00003685#ifdef NGROUPS_MAX
3686#define MAX_GROUPS NGROUPS_MAX
3687#else
3688 /* defined to be 16 on Solaris7, so this should be a small number */
3689#define MAX_GROUPS 64
3690#endif
3691 gid_t grouplist[MAX_GROUPS];
3692 int n;
3693
3694 n = getgroups(MAX_GROUPS, grouplist);
3695 if (n < 0)
3696 posix_error();
3697 else {
3698 result = PyList_New(n);
3699 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003700 int i;
3701 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003702 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003703 if (o == NULL) {
3704 Py_DECREF(result);
3705 result = NULL;
3706 break;
3707 }
3708 PyList_SET_ITEM(result, i, o);
3709 }
3710 }
3711 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003712
Fred Drakec9680921999-12-13 16:37:25 +00003713 return result;
3714}
3715#endif
3716
Martin v. Löwis606edc12002-06-13 21:09:11 +00003717#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003718PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003719"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003720Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003721
3722static PyObject *
3723posix_getpgid(PyObject *self, PyObject *args)
3724{
3725 int pid, pgid;
3726 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3727 return NULL;
3728 pgid = getpgid(pid);
3729 if (pgid < 0)
3730 return posix_error();
3731 return PyInt_FromLong((long)pgid);
3732}
3733#endif /* HAVE_GETPGID */
3734
3735
Guido van Rossumb6775db1994-08-01 11:34:53 +00003736#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003737PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003738"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003739Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003740
Barry Warsaw53699e91996-12-10 23:23:01 +00003741static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003742posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003743{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003744#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003745 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003746#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003747 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003748#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003749}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003750#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003752
Guido van Rossumb6775db1994-08-01 11:34:53 +00003753#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003754PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003755"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003756Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003757
Barry Warsaw53699e91996-12-10 23:23:01 +00003758static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003759posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003760{
Guido van Rossum64933891994-10-20 21:56:42 +00003761#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003762 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003763#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003764 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003765#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003766 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003767 Py_INCREF(Py_None);
3768 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003769}
3770
Guido van Rossumb6775db1994-08-01 11:34:53 +00003771#endif /* HAVE_SETPGRP */
3772
Guido van Rossumad0ee831995-03-01 10:34:45 +00003773#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003774PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003775"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003776Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003777
Barry Warsaw53699e91996-12-10 23:23:01 +00003778static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003779posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003780{
Barry Warsaw53699e91996-12-10 23:23:01 +00003781 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003782}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003783#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003785
Fred Drake12c6e2d1999-12-14 21:25:03 +00003786#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003790
3791static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003792posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003793{
Neal Norwitze241ce82003-02-17 18:17:05 +00003794 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003795 char *name;
3796 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003797
Fred Drakea30680b2000-12-06 21:24:28 +00003798 errno = 0;
3799 name = getlogin();
3800 if (name == NULL) {
3801 if (errno)
3802 posix_error();
3803 else
3804 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003805 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003806 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003807 else
3808 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003809 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003810
Fred Drake12c6e2d1999-12-14 21:25:03 +00003811 return result;
3812}
3813#endif
3814
Guido van Rossumad0ee831995-03-01 10:34:45 +00003815#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003816PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003817"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003818Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003819
Barry Warsaw53699e91996-12-10 23:23:01 +00003820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003821posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003822{
Barry Warsaw53699e91996-12-10 23:23:01 +00003823 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003824}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003825#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003827
Guido van Rossumad0ee831995-03-01 10:34:45 +00003828#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003829PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003830"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003831Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003832
Barry Warsaw53699e91996-12-10 23:23:01 +00003833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003834posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003835{
3836 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003837 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003838 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003839#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003840 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3841 APIRET rc;
3842 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003843 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003844
3845 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3846 APIRET rc;
3847 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != 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
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003851 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003852#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003853 if (kill(pid, sig) == -1)
3854 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003855#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003856 Py_INCREF(Py_None);
3857 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003858}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003859#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003860
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003861#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003862PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003863"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003864Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003865
3866static PyObject *
3867posix_killpg(PyObject *self, PyObject *args)
3868{
3869 int pgid, sig;
3870 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3871 return NULL;
3872 if (killpg(pgid, sig) == -1)
3873 return posix_error();
3874 Py_INCREF(Py_None);
3875 return Py_None;
3876}
3877#endif
3878
Guido van Rossumc0125471996-06-28 18:55:32 +00003879#ifdef HAVE_PLOCK
3880
3881#ifdef HAVE_SYS_LOCK_H
3882#include <sys/lock.h>
3883#endif
3884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003885PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003886"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003887Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003888
Barry Warsaw53699e91996-12-10 23:23:01 +00003889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003890posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003891{
3892 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003893 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003894 return NULL;
3895 if (plock(op) == -1)
3896 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003897 Py_INCREF(Py_None);
3898 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003899}
3900#endif
3901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003902
Guido van Rossum3b066191991-06-04 19:40:25 +00003903
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003904
Guido van Rossumb6775db1994-08-01 11:34:53 +00003905#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003906PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003907"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003908Set the current process's user id.");
3909
Barry Warsaw53699e91996-12-10 23:23:01 +00003910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003911posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003912{
3913 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003914 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003915 return NULL;
3916 if (setuid(uid) < 0)
3917 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003918 Py_INCREF(Py_None);
3919 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003920}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003921#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003923
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003924#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003925PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003926"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003927Set the current process's effective user id.");
3928
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003929static PyObject *
3930posix_seteuid (PyObject *self, PyObject *args)
3931{
3932 int euid;
3933 if (!PyArg_ParseTuple(args, "i", &euid)) {
3934 return NULL;
3935 } else if (seteuid(euid) < 0) {
3936 return posix_error();
3937 } else {
3938 Py_INCREF(Py_None);
3939 return Py_None;
3940 }
3941}
3942#endif /* HAVE_SETEUID */
3943
3944#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003945PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003946"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003947Set the current process's effective group id.");
3948
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003949static PyObject *
3950posix_setegid (PyObject *self, PyObject *args)
3951{
3952 int egid;
3953 if (!PyArg_ParseTuple(args, "i", &egid)) {
3954 return NULL;
3955 } else if (setegid(egid) < 0) {
3956 return posix_error();
3957 } else {
3958 Py_INCREF(Py_None);
3959 return Py_None;
3960 }
3961}
3962#endif /* HAVE_SETEGID */
3963
3964#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003965PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003966"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003967Set the current process's real and effective user ids.");
3968
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003969static PyObject *
3970posix_setreuid (PyObject *self, PyObject *args)
3971{
3972 int ruid, euid;
3973 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
3974 return NULL;
3975 } else if (setreuid(ruid, euid) < 0) {
3976 return posix_error();
3977 } else {
3978 Py_INCREF(Py_None);
3979 return Py_None;
3980 }
3981}
3982#endif /* HAVE_SETREUID */
3983
3984#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003985PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003986"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003987Set the current process's real and effective group ids.");
3988
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003989static PyObject *
3990posix_setregid (PyObject *self, PyObject *args)
3991{
3992 int rgid, egid;
3993 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
3994 return NULL;
3995 } else if (setregid(rgid, egid) < 0) {
3996 return posix_error();
3997 } else {
3998 Py_INCREF(Py_None);
3999 return Py_None;
4000 }
4001}
4002#endif /* HAVE_SETREGID */
4003
Guido van Rossumb6775db1994-08-01 11:34:53 +00004004#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004006"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004007Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004008
Barry Warsaw53699e91996-12-10 23:23:01 +00004009static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004010posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004011{
4012 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004013 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004014 return NULL;
4015 if (setgid(gid) < 0)
4016 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004017 Py_INCREF(Py_None);
4018 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004019}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004020#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004021
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004022#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004023PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004024"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004025Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004026
4027static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004028posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004029{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004030 int i, len;
4031 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004032
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004033 if (!PySequence_Check(groups)) {
4034 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4035 return NULL;
4036 }
4037 len = PySequence_Size(groups);
4038 if (len > MAX_GROUPS) {
4039 PyErr_SetString(PyExc_ValueError, "too many groups");
4040 return NULL;
4041 }
4042 for(i = 0; i < len; i++) {
4043 PyObject *elem;
4044 elem = PySequence_GetItem(groups, i);
4045 if (!elem)
4046 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004047 if (!PyLong_Check(elem)) {
4048 PyErr_SetString(PyExc_TypeError,
4049 "groups must be integers");
4050 Py_DECREF(elem);
4051 return NULL;
4052 } else {
4053 unsigned long x = PyLong_AsUnsignedLong(elem);
4054 if (PyErr_Occurred()) {
4055 PyErr_SetString(PyExc_TypeError,
4056 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004057 Py_DECREF(elem);
4058 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004059 }
Georg Brandla13c2442005-11-22 19:30:31 +00004060 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004061 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004062 if (grouplist[i] != x) {
4063 PyErr_SetString(PyExc_TypeError,
4064 "group id too big");
4065 Py_DECREF(elem);
4066 return NULL;
4067 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004068 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004069 Py_DECREF(elem);
4070 }
4071
4072 if (setgroups(len, grouplist) < 0)
4073 return posix_error();
4074 Py_INCREF(Py_None);
4075 return Py_None;
4076}
4077#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004078
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004079#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4080static PyObject *
4081wait_helper(int pid, int status, struct rusage *ru)
4082{
4083 PyObject *result;
4084 static PyObject *struct_rusage;
4085
4086 if (pid == -1)
4087 return posix_error();
4088
4089 if (struct_rusage == NULL) {
4090 PyObject *m = PyImport_ImportModule("resource");
4091 if (m == NULL)
4092 return NULL;
4093 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4094 Py_DECREF(m);
4095 if (struct_rusage == NULL)
4096 return NULL;
4097 }
4098
4099 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4100 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4101 if (!result)
4102 return NULL;
4103
4104#ifndef doubletime
4105#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4106#endif
4107
4108 PyStructSequence_SET_ITEM(result, 0,
4109 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4110 PyStructSequence_SET_ITEM(result, 1,
4111 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4112#define SET_INT(result, index, value)\
4113 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4114 SET_INT(result, 2, ru->ru_maxrss);
4115 SET_INT(result, 3, ru->ru_ixrss);
4116 SET_INT(result, 4, ru->ru_idrss);
4117 SET_INT(result, 5, ru->ru_isrss);
4118 SET_INT(result, 6, ru->ru_minflt);
4119 SET_INT(result, 7, ru->ru_majflt);
4120 SET_INT(result, 8, ru->ru_nswap);
4121 SET_INT(result, 9, ru->ru_inblock);
4122 SET_INT(result, 10, ru->ru_oublock);
4123 SET_INT(result, 11, ru->ru_msgsnd);
4124 SET_INT(result, 12, ru->ru_msgrcv);
4125 SET_INT(result, 13, ru->ru_nsignals);
4126 SET_INT(result, 14, ru->ru_nvcsw);
4127 SET_INT(result, 15, ru->ru_nivcsw);
4128#undef SET_INT
4129
4130 if (PyErr_Occurred()) {
4131 Py_DECREF(result);
4132 return NULL;
4133 }
4134
4135 return Py_BuildValue("iiN", pid, status, result);
4136}
4137#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4138
4139#ifdef HAVE_WAIT3
4140PyDoc_STRVAR(posix_wait3__doc__,
4141"wait3(options) -> (pid, status, rusage)\n\n\
4142Wait for completion of a child process.");
4143
4144static PyObject *
4145posix_wait3(PyObject *self, PyObject *args)
4146{
4147 int pid, options;
4148 struct rusage ru;
4149 WAIT_TYPE status;
4150 WAIT_STATUS_INT(status) = 0;
4151
4152 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4153 return NULL;
4154
4155 Py_BEGIN_ALLOW_THREADS
4156 pid = wait3(&status, options, &ru);
4157 Py_END_ALLOW_THREADS
4158
4159 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4160}
4161#endif /* HAVE_WAIT3 */
4162
4163#ifdef HAVE_WAIT4
4164PyDoc_STRVAR(posix_wait4__doc__,
4165"wait4(pid, options) -> (pid, status, rusage)\n\n\
4166Wait for completion of a given child process.");
4167
4168static PyObject *
4169posix_wait4(PyObject *self, PyObject *args)
4170{
4171 int pid, options;
4172 struct rusage ru;
4173 WAIT_TYPE status;
4174 WAIT_STATUS_INT(status) = 0;
4175
4176 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4177 return NULL;
4178
4179 Py_BEGIN_ALLOW_THREADS
4180 pid = wait4(pid, &status, options, &ru);
4181 Py_END_ALLOW_THREADS
4182
4183 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4184}
4185#endif /* HAVE_WAIT4 */
4186
Guido van Rossumb6775db1994-08-01 11:34:53 +00004187#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004188PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004189"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004190Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004191
Barry Warsaw53699e91996-12-10 23:23:01 +00004192static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004193posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004194{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004195 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004196 WAIT_TYPE status;
4197 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004198
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004199 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004200 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004201 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004202 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004203 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004204 if (pid == -1)
4205 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004206
4207 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004208}
4209
Tim Petersab034fa2002-02-01 11:27:43 +00004210#elif defined(HAVE_CWAIT)
4211
4212/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004214"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004215"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004216
4217static PyObject *
4218posix_waitpid(PyObject *self, PyObject *args)
4219{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004220 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004221 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004222
4223 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4224 return NULL;
4225 Py_BEGIN_ALLOW_THREADS
4226 pid = _cwait(&status, pid, options);
4227 Py_END_ALLOW_THREADS
4228 if (pid == -1)
4229 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004230
4231 /* shift the status left a byte so this is more like the POSIX waitpid */
4232 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004233}
4234#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004235
Guido van Rossumad0ee831995-03-01 10:34:45 +00004236#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004237PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004238"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004240
Barry Warsaw53699e91996-12-10 23:23:01 +00004241static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004242posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004243{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004244 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004245 WAIT_TYPE status;
4246 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004247
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004248 Py_BEGIN_ALLOW_THREADS
4249 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004250 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004251 if (pid == -1)
4252 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004253
4254 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004255}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004256#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004257
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004259PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004260"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004261Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004262
Barry Warsaw53699e91996-12-10 23:23:01 +00004263static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004264posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004265{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004266#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004267 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004268#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004269#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004270 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004271#else
4272 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4273#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004274#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004275}
4276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004277
Guido van Rossumb6775db1994-08-01 11:34:53 +00004278#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004279PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004280"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004281Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004282
Barry Warsaw53699e91996-12-10 23:23:01 +00004283static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004284posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004285{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004286 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004287 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004288 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004289 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004290 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004291
4292 if (!PyArg_ParseTuple(args, "et:readlink",
4293 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004294 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004295 v = PySequence_GetItem(args, 0);
4296 if (v == NULL) return NULL;
4297
4298 if (PyUnicode_Check(v)) {
4299 arg_is_unicode = 1;
4300 }
4301 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004302
Barry Warsaw53699e91996-12-10 23:23:01 +00004303 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004304 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004305 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004306 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004307 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004308
Neal Norwitzfca70052007-08-12 16:56:02 +00004309 PyMem_Free(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004310 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004311 if (arg_is_unicode) {
4312 PyObject *w;
4313
4314 w = PyUnicode_FromEncodedObject(v,
4315 Py_FileSystemDefaultEncoding,
4316 "strict");
4317 if (w != NULL) {
4318 Py_DECREF(v);
4319 v = w;
4320 }
4321 else {
4322 /* fall back to the original byte string, as
4323 discussed in patch #683592 */
4324 PyErr_Clear();
4325 }
4326 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004327 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004328}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004329#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004330
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004331
Guido van Rossumb6775db1994-08-01 11:34:53 +00004332#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004333PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004334"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004335Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004336
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004338posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004339{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004340 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004341}
4342#endif /* HAVE_SYMLINK */
4343
4344
4345#ifdef HAVE_TIMES
4346#ifndef HZ
4347#define HZ 60 /* Universal constant :-) */
4348#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004349
Guido van Rossumd48f2521997-12-05 22:19:34 +00004350#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4351static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004352system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004353{
4354 ULONG value = 0;
4355
4356 Py_BEGIN_ALLOW_THREADS
4357 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4358 Py_END_ALLOW_THREADS
4359
4360 return value;
4361}
4362
4363static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004364posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004365{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004366 /* Currently Only Uptime is Provided -- Others Later */
4367 return Py_BuildValue("ddddd",
4368 (double)0 /* t.tms_utime / HZ */,
4369 (double)0 /* t.tms_stime / HZ */,
4370 (double)0 /* t.tms_cutime / HZ */,
4371 (double)0 /* t.tms_cstime / HZ */,
4372 (double)system_uptime() / 1000);
4373}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004374#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004375static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004376posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004377{
4378 struct tms t;
4379 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004380 errno = 0;
4381 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004382 if (c == (clock_t) -1)
4383 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004384 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004385 (double)t.tms_utime / HZ,
4386 (double)t.tms_stime / HZ,
4387 (double)t.tms_cutime / HZ,
4388 (double)t.tms_cstime / HZ,
4389 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004390}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004391#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004392#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004393
4394
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004395#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004396#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004397static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004398posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004399{
4400 FILETIME create, exit, kernel, user;
4401 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004402 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004403 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4404 /* The fields of a FILETIME structure are the hi and lo part
4405 of a 64-bit value expressed in 100 nanosecond units.
4406 1e7 is one second in such units; 1e-7 the inverse.
4407 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4408 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004409 return Py_BuildValue(
4410 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004411 (double)(kernel.dwHighDateTime*429.4967296 +
4412 kernel.dwLowDateTime*1e-7),
4413 (double)(user.dwHighDateTime*429.4967296 +
4414 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004415 (double)0,
4416 (double)0,
4417 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004418}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004419#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004420
4421#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004422PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004423"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004424Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004425#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004426
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004427
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004428#ifdef HAVE_GETSID
4429PyDoc_STRVAR(posix_getsid__doc__,
4430"getsid(pid) -> sid\n\n\
4431Call the system call getsid().");
4432
4433static PyObject *
4434posix_getsid(PyObject *self, PyObject *args)
4435{
4436 int pid, sid;
4437 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4438 return NULL;
4439 sid = getsid(pid);
4440 if (sid < 0)
4441 return posix_error();
4442 return PyInt_FromLong((long)sid);
4443}
4444#endif /* HAVE_GETSID */
4445
4446
Guido van Rossumb6775db1994-08-01 11:34:53 +00004447#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004448PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004449"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004450Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004451
Barry Warsaw53699e91996-12-10 23:23:01 +00004452static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004453posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004454{
Guido van Rossum687dd131993-05-17 08:34:16 +00004455 if (setsid() < 0)
4456 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004457 Py_INCREF(Py_None);
4458 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004459}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004460#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004461
Guido van Rossumb6775db1994-08-01 11:34:53 +00004462#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004463PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004464"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004465Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004466
Barry Warsaw53699e91996-12-10 23:23:01 +00004467static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004468posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004469{
4470 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004471 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004472 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004473 if (setpgid(pid, pgrp) < 0)
4474 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004475 Py_INCREF(Py_None);
4476 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004477}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004478#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004479
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004480
Guido van Rossumb6775db1994-08-01 11:34:53 +00004481#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004482PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004483"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004484Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004485
Barry Warsaw53699e91996-12-10 23:23:01 +00004486static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004487posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004488{
4489 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004490 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004491 return NULL;
4492 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004493 if (pgid < 0)
4494 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004495 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004496}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004497#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004498
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004499
Guido van Rossumb6775db1994-08-01 11:34:53 +00004500#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004501PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004502"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004503Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004504
Barry Warsaw53699e91996-12-10 23:23:01 +00004505static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004506posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004507{
4508 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004509 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004510 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004511 if (tcsetpgrp(fd, pgid) < 0)
4512 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004513 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004514 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004515}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004516#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004517
Guido van Rossum687dd131993-05-17 08:34:16 +00004518/* Functions acting on file descriptors */
4519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004520PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004521"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004522Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004523
Barry Warsaw53699e91996-12-10 23:23:01 +00004524static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004525posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004526{
Mark Hammondef8b6542001-05-13 08:04:26 +00004527 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004528 int flag;
4529 int mode = 0777;
4530 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004531
4532#ifdef MS_WINDOWS
4533 if (unicode_file_names()) {
4534 PyUnicodeObject *po;
4535 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4536 Py_BEGIN_ALLOW_THREADS
4537 /* PyUnicode_AS_UNICODE OK without thread
4538 lock as it is a simple dereference. */
4539 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4540 Py_END_ALLOW_THREADS
4541 if (fd < 0)
4542 return posix_error();
4543 return PyInt_FromLong((long)fd);
4544 }
4545 /* Drop the argument parsing error as narrow strings
4546 are also valid. */
4547 PyErr_Clear();
4548 }
4549#endif
4550
Tim Peters5aa91602002-01-30 05:46:57 +00004551 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004552 Py_FileSystemDefaultEncoding, &file,
4553 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004554 return NULL;
4555
Barry Warsaw53699e91996-12-10 23:23:01 +00004556 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004557 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004558 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004559 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004560 return posix_error_with_allocated_filename(file);
4561 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004562 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004563}
4564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004565
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004566PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004567"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004568Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004569
Barry Warsaw53699e91996-12-10 23:23:01 +00004570static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004571posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004572{
4573 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004574 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004575 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004576 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004577 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004578 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004579 if (res < 0)
4580 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004581 Py_INCREF(Py_None);
4582 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004583}
4584
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004586PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004587"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004588Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004589
Barry Warsaw53699e91996-12-10 23:23:01 +00004590static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004591posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004592{
4593 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004594 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004595 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004596 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004597 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004598 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004599 if (fd < 0)
4600 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004601 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004602}
4603
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004605PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004606"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004607Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004608
Barry Warsaw53699e91996-12-10 23:23:01 +00004609static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004610posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004611{
4612 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004613 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004614 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004615 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004616 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004617 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004618 if (res < 0)
4619 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004620 Py_INCREF(Py_None);
4621 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004622}
4623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004626"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004628
Barry Warsaw53699e91996-12-10 23:23:01 +00004629static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004630posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004631{
4632 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004633#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004634 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004635#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004636 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004637#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004638 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004639 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004640 return NULL;
4641#ifdef SEEK_SET
4642 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4643 switch (how) {
4644 case 0: how = SEEK_SET; break;
4645 case 1: how = SEEK_CUR; break;
4646 case 2: how = SEEK_END; break;
4647 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004648#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004649
4650#if !defined(HAVE_LARGEFILE_SUPPORT)
4651 pos = PyInt_AsLong(posobj);
4652#else
4653 pos = PyLong_Check(posobj) ?
4654 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4655#endif
4656 if (PyErr_Occurred())
4657 return NULL;
4658
Barry Warsaw53699e91996-12-10 23:23:01 +00004659 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004660#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004661 res = _lseeki64(fd, pos, how);
4662#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004663 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004664#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004665 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004666 if (res < 0)
4667 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004668
4669#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004670 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004671#else
4672 return PyLong_FromLongLong(res);
4673#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004674}
4675
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004677PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004678"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004679Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004680
Barry Warsaw53699e91996-12-10 23:23:01 +00004681static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004682posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004683{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004684 int fd, size;
4685 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004686 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004687 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004688 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004689 if (size < 0) {
4690 errno = EINVAL;
4691 return posix_error();
4692 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00004693 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004694 if (buffer == NULL)
4695 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004696 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00004697 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004698 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004699 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004700 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004701 return posix_error();
4702 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004703 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00004704 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004705 return buffer;
4706}
4707
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004709PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004710"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004711Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004712
Barry Warsaw53699e91996-12-10 23:23:01 +00004713static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004714posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004715{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004716 int fd;
4717 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004718 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004719
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004720 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004721 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004722 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004723 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004724 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004725 if (size < 0)
4726 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004727 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004728}
4729
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004731PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004732"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004733Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004734
Barry Warsaw53699e91996-12-10 23:23:01 +00004735static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004736posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004737{
4738 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004739 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004740 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004741 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004742 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004743#ifdef __VMS
4744 /* on OpenVMS we must ensure that all bytes are written to the file */
4745 fsync(fd);
4746#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004747 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004748 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004749 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004750 if (res != 0) {
4751#ifdef MS_WINDOWS
4752 return win32_error("fstat", NULL);
4753#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004754 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004755#endif
4756 }
Tim Peters5aa91602002-01-30 05:46:57 +00004757
Martin v. Löwis14694662006-02-03 12:54:16 +00004758 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004759}
4760
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004761PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004762"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004763Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004764connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004765
4766static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004767posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004768{
4769 int fd;
4770 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4771 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004772 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004773}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004774
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004775#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004776PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004777"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004778Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004779
Barry Warsaw53699e91996-12-10 23:23:01 +00004780static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004781posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004782{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004783#if defined(PYOS_OS2)
4784 HFILE read, write;
4785 APIRET rc;
4786
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004787 Py_BEGIN_ALLOW_THREADS
4788 rc = DosCreatePipe( &read, &write, 4096);
4789 Py_END_ALLOW_THREADS
4790 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004791 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004792
4793 return Py_BuildValue("(ii)", read, write);
4794#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004795#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004796 int fds[2];
4797 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004798 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004799 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004800 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004801 if (res != 0)
4802 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004803 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004804#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004805 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004806 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004807 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004808 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004809 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004810 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004811 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004812 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004813 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4814 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004815 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004816#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004817#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004818}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004819#endif /* HAVE_PIPE */
4820
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004821
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004822#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004823PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004824"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004825Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004826
Barry Warsaw53699e91996-12-10 23:23:01 +00004827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004828posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004829{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004830 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004831 int mode = 0666;
4832 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004833 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004834 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004835 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004836 res = mkfifo(filename, mode);
4837 Py_END_ALLOW_THREADS
4838 if (res < 0)
4839 return posix_error();
4840 Py_INCREF(Py_None);
4841 return Py_None;
4842}
4843#endif
4844
4845
Neal Norwitz11690112002-07-30 01:08:28 +00004846#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004847PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004848"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004849Create a filesystem node (file, device special file or named pipe)\n\
4850named filename. mode specifies both the permissions to use and the\n\
4851type of node to be created, being combined (bitwise OR) with one of\n\
4852S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004853device defines the newly created device special file (probably using\n\
4854os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004855
4856
4857static PyObject *
4858posix_mknod(PyObject *self, PyObject *args)
4859{
4860 char *filename;
4861 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004862 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004863 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00004864 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004865 return NULL;
4866 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004867 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00004868 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004869 if (res < 0)
4870 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004871 Py_INCREF(Py_None);
4872 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004873}
4874#endif
4875
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004876#ifdef HAVE_DEVICE_MACROS
4877PyDoc_STRVAR(posix_major__doc__,
4878"major(device) -> major number\n\
4879Extracts a device major number from a raw device number.");
4880
4881static PyObject *
4882posix_major(PyObject *self, PyObject *args)
4883{
4884 int device;
4885 if (!PyArg_ParseTuple(args, "i:major", &device))
4886 return NULL;
4887 return PyInt_FromLong((long)major(device));
4888}
4889
4890PyDoc_STRVAR(posix_minor__doc__,
4891"minor(device) -> minor number\n\
4892Extracts a device minor number from a raw device number.");
4893
4894static PyObject *
4895posix_minor(PyObject *self, PyObject *args)
4896{
4897 int device;
4898 if (!PyArg_ParseTuple(args, "i:minor", &device))
4899 return NULL;
4900 return PyInt_FromLong((long)minor(device));
4901}
4902
4903PyDoc_STRVAR(posix_makedev__doc__,
4904"makedev(major, minor) -> device number\n\
4905Composes a raw device number from the major and minor device numbers.");
4906
4907static PyObject *
4908posix_makedev(PyObject *self, PyObject *args)
4909{
4910 int major, minor;
4911 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
4912 return NULL;
4913 return PyInt_FromLong((long)makedev(major, minor));
4914}
4915#endif /* device macros */
4916
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004917
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004918#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004919PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004920"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004921Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004922
Barry Warsaw53699e91996-12-10 23:23:01 +00004923static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004924posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004925{
4926 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004927 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004928 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004929 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004930
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004931 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004932 return NULL;
4933
4934#if !defined(HAVE_LARGEFILE_SUPPORT)
4935 length = PyInt_AsLong(lenobj);
4936#else
4937 length = PyLong_Check(lenobj) ?
4938 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
4939#endif
4940 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004941 return NULL;
4942
Barry Warsaw53699e91996-12-10 23:23:01 +00004943 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004944 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00004945 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004946 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004947 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004948 return NULL;
4949 }
Barry Warsaw53699e91996-12-10 23:23:01 +00004950 Py_INCREF(Py_None);
4951 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004952}
4953#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004954
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004955#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004956PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004957"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004958Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004959
Fred Drake762e2061999-08-26 17:23:54 +00004960/* Save putenv() parameters as values here, so we can collect them when they
4961 * get re-set with another call for the same key. */
4962static PyObject *posix_putenv_garbage;
4963
Tim Peters5aa91602002-01-30 05:46:57 +00004964static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004965posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004966{
4967 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004968 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00004969 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00004970 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004971
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004972 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004973 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00004974
4975#if defined(PYOS_OS2)
4976 if (stricmp(s1, "BEGINLIBPATH") == 0) {
4977 APIRET rc;
4978
Guido van Rossumd48f2521997-12-05 22:19:34 +00004979 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
4980 if (rc != NO_ERROR)
4981 return os2_error(rc);
4982
4983 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
4984 APIRET rc;
4985
Guido van Rossumd48f2521997-12-05 22:19:34 +00004986 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
4987 if (rc != NO_ERROR)
4988 return os2_error(rc);
4989 } else {
4990#endif
4991
Fred Drake762e2061999-08-26 17:23:54 +00004992 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00004993 len = strlen(s1) + strlen(s2) + 2;
4994 /* len includes space for a trailing \0; the size arg to
4995 PyString_FromStringAndSize does not count that */
4996 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00004997 if (newstr == NULL)
4998 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004999 newenv = PyString_AS_STRING(newstr);
5000 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5001 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005002 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005003 posix_error();
5004 return NULL;
5005 }
Fred Drake762e2061999-08-26 17:23:54 +00005006 /* Install the first arg and newstr in posix_putenv_garbage;
5007 * this will cause previous value to be collected. This has to
5008 * happen after the real putenv() call because the old value
5009 * was still accessible until then. */
5010 if (PyDict_SetItem(posix_putenv_garbage,
5011 PyTuple_GET_ITEM(args, 0), newstr)) {
5012 /* really not much we can do; just leak */
5013 PyErr_Clear();
5014 }
5015 else {
5016 Py_DECREF(newstr);
5017 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005018
5019#if defined(PYOS_OS2)
5020 }
5021#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005022 Py_INCREF(Py_None);
5023 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005024}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005025#endif /* putenv */
5026
Guido van Rossumc524d952001-10-19 01:31:59 +00005027#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005028PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005029"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005030Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005031
5032static PyObject *
5033posix_unsetenv(PyObject *self, PyObject *args)
5034{
5035 char *s1;
5036
5037 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5038 return NULL;
5039
5040 unsetenv(s1);
5041
5042 /* Remove the key from posix_putenv_garbage;
5043 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005044 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005045 * old value was still accessible until then.
5046 */
5047 if (PyDict_DelItem(posix_putenv_garbage,
5048 PyTuple_GET_ITEM(args, 0))) {
5049 /* really not much we can do; just leak */
5050 PyErr_Clear();
5051 }
5052
5053 Py_INCREF(Py_None);
5054 return Py_None;
5055}
5056#endif /* unsetenv */
5057
Guido van Rossumb6a47161997-09-15 22:54:34 +00005058#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005059PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005060"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005061Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005062
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005063static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005064posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005065{
5066 int code;
5067 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005068 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005069 return NULL;
5070 message = strerror(code);
5071 if (message == NULL) {
5072 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005073 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005074 return NULL;
5075 }
5076 return PyString_FromString(message);
5077}
5078#endif /* strerror */
5079
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005080
Guido van Rossumc9641791998-08-04 15:26:23 +00005081#ifdef HAVE_SYS_WAIT_H
5082
Fred Drake106c1a02002-04-23 15:58:02 +00005083#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005084PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005085"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005086Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005087
5088static PyObject *
5089posix_WCOREDUMP(PyObject *self, PyObject *args)
5090{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005091 WAIT_TYPE status;
5092 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005093
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005094 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005095 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005096
5097 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005098}
5099#endif /* WCOREDUMP */
5100
5101#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005102PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005103"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005104Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005105job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005106
5107static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005108posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005109{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005110 WAIT_TYPE status;
5111 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005112
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005113 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005114 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005115
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005116 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005117}
5118#endif /* WIFCONTINUED */
5119
Guido van Rossumc9641791998-08-04 15:26:23 +00005120#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005121PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005122"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005123Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005124
5125static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005126posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005127{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005128 WAIT_TYPE status;
5129 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005130
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005131 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005132 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005133
Fred Drake106c1a02002-04-23 15:58:02 +00005134 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005135}
5136#endif /* WIFSTOPPED */
5137
5138#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005139PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005140"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005141Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005142
5143static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005144posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005145{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005146 WAIT_TYPE status;
5147 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005148
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005149 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005150 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005151
Fred Drake106c1a02002-04-23 15:58:02 +00005152 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005153}
5154#endif /* WIFSIGNALED */
5155
5156#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005157PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005158"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005159Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005160system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005161
5162static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005163posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005164{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005165 WAIT_TYPE status;
5166 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005167
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005168 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005169 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005170
Fred Drake106c1a02002-04-23 15:58:02 +00005171 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005172}
5173#endif /* WIFEXITED */
5174
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005175#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005176PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005177"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005178Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005179
5180static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005181posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005182{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005183 WAIT_TYPE status;
5184 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005185
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005186 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005187 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005188
Guido van Rossumc9641791998-08-04 15:26:23 +00005189 return Py_BuildValue("i", WEXITSTATUS(status));
5190}
5191#endif /* WEXITSTATUS */
5192
5193#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005194PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005195"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005196Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005197value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005198
5199static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005200posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005201{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005202 WAIT_TYPE status;
5203 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005204
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005205 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005206 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005207
Guido van Rossumc9641791998-08-04 15:26:23 +00005208 return Py_BuildValue("i", WTERMSIG(status));
5209}
5210#endif /* WTERMSIG */
5211
5212#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005213PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005214"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005215Return the signal that stopped the process that provided\n\
5216the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005217
5218static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005219posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005220{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005221 WAIT_TYPE status;
5222 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005223
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005224 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005225 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005226
Guido van Rossumc9641791998-08-04 15:26:23 +00005227 return Py_BuildValue("i", WSTOPSIG(status));
5228}
5229#endif /* WSTOPSIG */
5230
5231#endif /* HAVE_SYS_WAIT_H */
5232
5233
Thomas Wouters477c8d52006-05-27 19:21:47 +00005234#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005235#ifdef _SCO_DS
5236/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5237 needed definitions in sys/statvfs.h */
5238#define _SVID3
5239#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005240#include <sys/statvfs.h>
5241
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005242static PyObject*
5243_pystatvfs_fromstructstatvfs(struct statvfs st) {
5244 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5245 if (v == NULL)
5246 return NULL;
5247
5248#if !defined(HAVE_LARGEFILE_SUPPORT)
5249 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5250 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5251 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5252 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5253 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5254 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5255 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5256 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5257 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5258 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5259#else
5260 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5261 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005262 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005263 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005264 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005265 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005266 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005267 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005268 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005269 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005270 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005271 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005272 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005273 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005274 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5275 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5276#endif
5277
5278 return v;
5279}
5280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005281PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005282"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005283Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005284
5285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005286posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005287{
5288 int fd, res;
5289 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005290
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005291 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005292 return NULL;
5293 Py_BEGIN_ALLOW_THREADS
5294 res = fstatvfs(fd, &st);
5295 Py_END_ALLOW_THREADS
5296 if (res != 0)
5297 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005298
5299 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005300}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005301#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005302
5303
Thomas Wouters477c8d52006-05-27 19:21:47 +00005304#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005305#include <sys/statvfs.h>
5306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005307PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005308"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005309Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005310
5311static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005312posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005313{
5314 char *path;
5315 int res;
5316 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005317 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005318 return NULL;
5319 Py_BEGIN_ALLOW_THREADS
5320 res = statvfs(path, &st);
5321 Py_END_ALLOW_THREADS
5322 if (res != 0)
5323 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005324
5325 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005326}
5327#endif /* HAVE_STATVFS */
5328
5329
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005330#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005331PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005332"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005333Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005334The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005335or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005336
5337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005338posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005339{
5340 PyObject *result = NULL;
5341 char *dir = NULL;
5342 char *pfx = NULL;
5343 char *name;
5344
5345 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5346 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005347
Skip Montanaro46fc3372007-08-12 11:44:53 +00005348 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5349 "tempnam is a potential security risk to your program",
5350 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005351 return NULL;
5352
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005353#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005354 name = _tempnam(dir, pfx);
5355#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005356 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005357#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005358 if (name == NULL)
5359 return PyErr_NoMemory();
5360 result = PyString_FromString(name);
5361 free(name);
5362 return result;
5363}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005364#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005365
5366
5367#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005368PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005369"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005370Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005371
5372static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005373posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005374{
5375 FILE *fp;
5376
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005377 fp = tmpfile();
5378 if (fp == NULL)
5379 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005380 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005381}
5382#endif
5383
5384
5385#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005387"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005388Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005389
5390static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005391posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005392{
5393 char buffer[L_tmpnam];
5394 char *name;
5395
Skip Montanaro46fc3372007-08-12 11:44:53 +00005396 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5397 "tmpnam is a potential security risk to your program",
5398 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005399 return NULL;
5400
Greg Wardb48bc172000-03-01 21:51:56 +00005401#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005402 name = tmpnam_r(buffer);
5403#else
5404 name = tmpnam(buffer);
5405#endif
5406 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005407 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005408#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005409 "unexpected NULL from tmpnam_r"
5410#else
5411 "unexpected NULL from tmpnam"
5412#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005413 );
5414 PyErr_SetObject(PyExc_OSError, err);
5415 Py_XDECREF(err);
5416 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005417 }
5418 return PyString_FromString(buffer);
5419}
5420#endif
5421
5422
Fred Drakec9680921999-12-13 16:37:25 +00005423/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5424 * It maps strings representing configuration variable names to
5425 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005426 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005427 * rarely-used constants. There are three separate tables that use
5428 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005429 *
5430 * This code is always included, even if none of the interfaces that
5431 * need it are included. The #if hackery needed to avoid it would be
5432 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005433 */
5434struct constdef {
5435 char *name;
5436 long value;
5437};
5438
Fred Drake12c6e2d1999-12-14 21:25:03 +00005439static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005440conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5441 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005442{
5443 if (PyInt_Check(arg)) {
5444 *valuep = PyInt_AS_LONG(arg);
5445 return 1;
5446 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005447 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005448 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005449 size_t lo = 0;
5450 size_t mid;
5451 size_t hi = tablesize;
5452 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005453 const char *confname;
5454 Py_ssize_t namelen;
5455 if (PyObject_AsCharBuffer(arg, &confname, &namelen) < 0) {
5456 PyErr_SetString(PyExc_TypeError,
5457 "configuration names must be strings or integers");
5458 return 0;
5459 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005460 while (lo < hi) {
5461 mid = (lo + hi) / 2;
5462 cmp = strcmp(confname, table[mid].name);
5463 if (cmp < 0)
5464 hi = mid;
5465 else if (cmp > 0)
5466 lo = mid + 1;
5467 else {
5468 *valuep = table[mid].value;
5469 return 1;
5470 }
5471 }
5472 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005473 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005474 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005475}
5476
5477
5478#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5479static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005480#ifdef _PC_ABI_AIO_XFER_MAX
5481 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5482#endif
5483#ifdef _PC_ABI_ASYNC_IO
5484 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5485#endif
Fred Drakec9680921999-12-13 16:37:25 +00005486#ifdef _PC_ASYNC_IO
5487 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5488#endif
5489#ifdef _PC_CHOWN_RESTRICTED
5490 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5491#endif
5492#ifdef _PC_FILESIZEBITS
5493 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5494#endif
5495#ifdef _PC_LAST
5496 {"PC_LAST", _PC_LAST},
5497#endif
5498#ifdef _PC_LINK_MAX
5499 {"PC_LINK_MAX", _PC_LINK_MAX},
5500#endif
5501#ifdef _PC_MAX_CANON
5502 {"PC_MAX_CANON", _PC_MAX_CANON},
5503#endif
5504#ifdef _PC_MAX_INPUT
5505 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5506#endif
5507#ifdef _PC_NAME_MAX
5508 {"PC_NAME_MAX", _PC_NAME_MAX},
5509#endif
5510#ifdef _PC_NO_TRUNC
5511 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5512#endif
5513#ifdef _PC_PATH_MAX
5514 {"PC_PATH_MAX", _PC_PATH_MAX},
5515#endif
5516#ifdef _PC_PIPE_BUF
5517 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5518#endif
5519#ifdef _PC_PRIO_IO
5520 {"PC_PRIO_IO", _PC_PRIO_IO},
5521#endif
5522#ifdef _PC_SOCK_MAXBUF
5523 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5524#endif
5525#ifdef _PC_SYNC_IO
5526 {"PC_SYNC_IO", _PC_SYNC_IO},
5527#endif
5528#ifdef _PC_VDISABLE
5529 {"PC_VDISABLE", _PC_VDISABLE},
5530#endif
5531};
5532
Fred Drakec9680921999-12-13 16:37:25 +00005533static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005534conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005535{
5536 return conv_confname(arg, valuep, posix_constants_pathconf,
5537 sizeof(posix_constants_pathconf)
5538 / sizeof(struct constdef));
5539}
5540#endif
5541
5542#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005543PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005544"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005545Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005546If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005547
5548static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005549posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005550{
5551 PyObject *result = NULL;
5552 int name, fd;
5553
Fred Drake12c6e2d1999-12-14 21:25:03 +00005554 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5555 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005556 long limit;
5557
5558 errno = 0;
5559 limit = fpathconf(fd, name);
5560 if (limit == -1 && errno != 0)
5561 posix_error();
5562 else
5563 result = PyInt_FromLong(limit);
5564 }
5565 return result;
5566}
5567#endif
5568
5569
5570#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005571PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005572"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005573Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005574If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005575
5576static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005577posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005578{
5579 PyObject *result = NULL;
5580 int name;
5581 char *path;
5582
5583 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5584 conv_path_confname, &name)) {
5585 long limit;
5586
5587 errno = 0;
5588 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005589 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005590 if (errno == EINVAL)
5591 /* could be a path or name problem */
5592 posix_error();
5593 else
5594 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005595 }
Fred Drakec9680921999-12-13 16:37:25 +00005596 else
5597 result = PyInt_FromLong(limit);
5598 }
5599 return result;
5600}
5601#endif
5602
5603#ifdef HAVE_CONFSTR
5604static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005605#ifdef _CS_ARCHITECTURE
5606 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5607#endif
5608#ifdef _CS_HOSTNAME
5609 {"CS_HOSTNAME", _CS_HOSTNAME},
5610#endif
5611#ifdef _CS_HW_PROVIDER
5612 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5613#endif
5614#ifdef _CS_HW_SERIAL
5615 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5616#endif
5617#ifdef _CS_INITTAB_NAME
5618 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5619#endif
Fred Drakec9680921999-12-13 16:37:25 +00005620#ifdef _CS_LFS64_CFLAGS
5621 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5622#endif
5623#ifdef _CS_LFS64_LDFLAGS
5624 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5625#endif
5626#ifdef _CS_LFS64_LIBS
5627 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5628#endif
5629#ifdef _CS_LFS64_LINTFLAGS
5630 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5631#endif
5632#ifdef _CS_LFS_CFLAGS
5633 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5634#endif
5635#ifdef _CS_LFS_LDFLAGS
5636 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5637#endif
5638#ifdef _CS_LFS_LIBS
5639 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5640#endif
5641#ifdef _CS_LFS_LINTFLAGS
5642 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5643#endif
Fred Draked86ed291999-12-15 15:34:33 +00005644#ifdef _CS_MACHINE
5645 {"CS_MACHINE", _CS_MACHINE},
5646#endif
Fred Drakec9680921999-12-13 16:37:25 +00005647#ifdef _CS_PATH
5648 {"CS_PATH", _CS_PATH},
5649#endif
Fred Draked86ed291999-12-15 15:34:33 +00005650#ifdef _CS_RELEASE
5651 {"CS_RELEASE", _CS_RELEASE},
5652#endif
5653#ifdef _CS_SRPC_DOMAIN
5654 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5655#endif
5656#ifdef _CS_SYSNAME
5657 {"CS_SYSNAME", _CS_SYSNAME},
5658#endif
5659#ifdef _CS_VERSION
5660 {"CS_VERSION", _CS_VERSION},
5661#endif
Fred Drakec9680921999-12-13 16:37:25 +00005662#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5663 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5664#endif
5665#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5666 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5667#endif
5668#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5669 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5670#endif
5671#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5672 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5673#endif
5674#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5675 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5676#endif
5677#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5678 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5679#endif
5680#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5681 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5682#endif
5683#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5684 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5685#endif
5686#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5687 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5688#endif
5689#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5690 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5691#endif
5692#ifdef _CS_XBS5_LP64_OFF64_LIBS
5693 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5694#endif
5695#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5696 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5697#endif
5698#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5699 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5700#endif
5701#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5702 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5703#endif
5704#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5705 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5706#endif
5707#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5708 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5709#endif
Fred Draked86ed291999-12-15 15:34:33 +00005710#ifdef _MIPS_CS_AVAIL_PROCESSORS
5711 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5712#endif
5713#ifdef _MIPS_CS_BASE
5714 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5715#endif
5716#ifdef _MIPS_CS_HOSTID
5717 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5718#endif
5719#ifdef _MIPS_CS_HW_NAME
5720 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5721#endif
5722#ifdef _MIPS_CS_NUM_PROCESSORS
5723 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5724#endif
5725#ifdef _MIPS_CS_OSREL_MAJ
5726 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5727#endif
5728#ifdef _MIPS_CS_OSREL_MIN
5729 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5730#endif
5731#ifdef _MIPS_CS_OSREL_PATCH
5732 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5733#endif
5734#ifdef _MIPS_CS_OS_NAME
5735 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5736#endif
5737#ifdef _MIPS_CS_OS_PROVIDER
5738 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5739#endif
5740#ifdef _MIPS_CS_PROCESSORS
5741 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5742#endif
5743#ifdef _MIPS_CS_SERIAL
5744 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5745#endif
5746#ifdef _MIPS_CS_VENDOR
5747 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5748#endif
Fred Drakec9680921999-12-13 16:37:25 +00005749};
5750
5751static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005752conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005753{
5754 return conv_confname(arg, valuep, posix_constants_confstr,
5755 sizeof(posix_constants_confstr)
5756 / sizeof(struct constdef));
5757}
5758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005759PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005760"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005761Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005762
5763static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005764posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005765{
5766 PyObject *result = NULL;
5767 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005768 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005769
5770 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005771 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005772
Fred Drakec9680921999-12-13 16:37:25 +00005773 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005774 len = confstr(name, buffer, sizeof(buffer));
5775 if (len == 0) {
5776 if (errno) {
5777 posix_error();
5778 }
5779 else {
5780 result = Py_None;
5781 Py_INCREF(Py_None);
5782 }
Fred Drakec9680921999-12-13 16:37:25 +00005783 }
5784 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005785 if ((unsigned int)len >= sizeof(buffer)) {
5786 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005787 if (result != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005788 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005789 }
5790 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005791 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005792 }
5793 }
5794 return result;
5795}
5796#endif
5797
5798
5799#ifdef HAVE_SYSCONF
5800static struct constdef posix_constants_sysconf[] = {
5801#ifdef _SC_2_CHAR_TERM
5802 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5803#endif
5804#ifdef _SC_2_C_BIND
5805 {"SC_2_C_BIND", _SC_2_C_BIND},
5806#endif
5807#ifdef _SC_2_C_DEV
5808 {"SC_2_C_DEV", _SC_2_C_DEV},
5809#endif
5810#ifdef _SC_2_C_VERSION
5811 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5812#endif
5813#ifdef _SC_2_FORT_DEV
5814 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5815#endif
5816#ifdef _SC_2_FORT_RUN
5817 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5818#endif
5819#ifdef _SC_2_LOCALEDEF
5820 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5821#endif
5822#ifdef _SC_2_SW_DEV
5823 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5824#endif
5825#ifdef _SC_2_UPE
5826 {"SC_2_UPE", _SC_2_UPE},
5827#endif
5828#ifdef _SC_2_VERSION
5829 {"SC_2_VERSION", _SC_2_VERSION},
5830#endif
Fred Draked86ed291999-12-15 15:34:33 +00005831#ifdef _SC_ABI_ASYNCHRONOUS_IO
5832 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5833#endif
5834#ifdef _SC_ACL
5835 {"SC_ACL", _SC_ACL},
5836#endif
Fred Drakec9680921999-12-13 16:37:25 +00005837#ifdef _SC_AIO_LISTIO_MAX
5838 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5839#endif
Fred Drakec9680921999-12-13 16:37:25 +00005840#ifdef _SC_AIO_MAX
5841 {"SC_AIO_MAX", _SC_AIO_MAX},
5842#endif
5843#ifdef _SC_AIO_PRIO_DELTA_MAX
5844 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5845#endif
5846#ifdef _SC_ARG_MAX
5847 {"SC_ARG_MAX", _SC_ARG_MAX},
5848#endif
5849#ifdef _SC_ASYNCHRONOUS_IO
5850 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5851#endif
5852#ifdef _SC_ATEXIT_MAX
5853 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5854#endif
Fred Draked86ed291999-12-15 15:34:33 +00005855#ifdef _SC_AUDIT
5856 {"SC_AUDIT", _SC_AUDIT},
5857#endif
Fred Drakec9680921999-12-13 16:37:25 +00005858#ifdef _SC_AVPHYS_PAGES
5859 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5860#endif
5861#ifdef _SC_BC_BASE_MAX
5862 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5863#endif
5864#ifdef _SC_BC_DIM_MAX
5865 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5866#endif
5867#ifdef _SC_BC_SCALE_MAX
5868 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5869#endif
5870#ifdef _SC_BC_STRING_MAX
5871 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5872#endif
Fred Draked86ed291999-12-15 15:34:33 +00005873#ifdef _SC_CAP
5874 {"SC_CAP", _SC_CAP},
5875#endif
Fred Drakec9680921999-12-13 16:37:25 +00005876#ifdef _SC_CHARCLASS_NAME_MAX
5877 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5878#endif
5879#ifdef _SC_CHAR_BIT
5880 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5881#endif
5882#ifdef _SC_CHAR_MAX
5883 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5884#endif
5885#ifdef _SC_CHAR_MIN
5886 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5887#endif
5888#ifdef _SC_CHILD_MAX
5889 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5890#endif
5891#ifdef _SC_CLK_TCK
5892 {"SC_CLK_TCK", _SC_CLK_TCK},
5893#endif
5894#ifdef _SC_COHER_BLKSZ
5895 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5896#endif
5897#ifdef _SC_COLL_WEIGHTS_MAX
5898 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5899#endif
5900#ifdef _SC_DCACHE_ASSOC
5901 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5902#endif
5903#ifdef _SC_DCACHE_BLKSZ
5904 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5905#endif
5906#ifdef _SC_DCACHE_LINESZ
5907 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
5908#endif
5909#ifdef _SC_DCACHE_SZ
5910 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
5911#endif
5912#ifdef _SC_DCACHE_TBLKSZ
5913 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
5914#endif
5915#ifdef _SC_DELAYTIMER_MAX
5916 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
5917#endif
5918#ifdef _SC_EQUIV_CLASS_MAX
5919 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
5920#endif
5921#ifdef _SC_EXPR_NEST_MAX
5922 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
5923#endif
5924#ifdef _SC_FSYNC
5925 {"SC_FSYNC", _SC_FSYNC},
5926#endif
5927#ifdef _SC_GETGR_R_SIZE_MAX
5928 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
5929#endif
5930#ifdef _SC_GETPW_R_SIZE_MAX
5931 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
5932#endif
5933#ifdef _SC_ICACHE_ASSOC
5934 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
5935#endif
5936#ifdef _SC_ICACHE_BLKSZ
5937 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
5938#endif
5939#ifdef _SC_ICACHE_LINESZ
5940 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
5941#endif
5942#ifdef _SC_ICACHE_SZ
5943 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
5944#endif
Fred Draked86ed291999-12-15 15:34:33 +00005945#ifdef _SC_INF
5946 {"SC_INF", _SC_INF},
5947#endif
Fred Drakec9680921999-12-13 16:37:25 +00005948#ifdef _SC_INT_MAX
5949 {"SC_INT_MAX", _SC_INT_MAX},
5950#endif
5951#ifdef _SC_INT_MIN
5952 {"SC_INT_MIN", _SC_INT_MIN},
5953#endif
5954#ifdef _SC_IOV_MAX
5955 {"SC_IOV_MAX", _SC_IOV_MAX},
5956#endif
Fred Draked86ed291999-12-15 15:34:33 +00005957#ifdef _SC_IP_SECOPTS
5958 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
5959#endif
Fred Drakec9680921999-12-13 16:37:25 +00005960#ifdef _SC_JOB_CONTROL
5961 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
5962#endif
Fred Draked86ed291999-12-15 15:34:33 +00005963#ifdef _SC_KERN_POINTERS
5964 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
5965#endif
5966#ifdef _SC_KERN_SIM
5967 {"SC_KERN_SIM", _SC_KERN_SIM},
5968#endif
Fred Drakec9680921999-12-13 16:37:25 +00005969#ifdef _SC_LINE_MAX
5970 {"SC_LINE_MAX", _SC_LINE_MAX},
5971#endif
5972#ifdef _SC_LOGIN_NAME_MAX
5973 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
5974#endif
5975#ifdef _SC_LOGNAME_MAX
5976 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
5977#endif
5978#ifdef _SC_LONG_BIT
5979 {"SC_LONG_BIT", _SC_LONG_BIT},
5980#endif
Fred Draked86ed291999-12-15 15:34:33 +00005981#ifdef _SC_MAC
5982 {"SC_MAC", _SC_MAC},
5983#endif
Fred Drakec9680921999-12-13 16:37:25 +00005984#ifdef _SC_MAPPED_FILES
5985 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
5986#endif
5987#ifdef _SC_MAXPID
5988 {"SC_MAXPID", _SC_MAXPID},
5989#endif
5990#ifdef _SC_MB_LEN_MAX
5991 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
5992#endif
5993#ifdef _SC_MEMLOCK
5994 {"SC_MEMLOCK", _SC_MEMLOCK},
5995#endif
5996#ifdef _SC_MEMLOCK_RANGE
5997 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
5998#endif
5999#ifdef _SC_MEMORY_PROTECTION
6000 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6001#endif
6002#ifdef _SC_MESSAGE_PASSING
6003 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6004#endif
Fred Draked86ed291999-12-15 15:34:33 +00006005#ifdef _SC_MMAP_FIXED_ALIGNMENT
6006 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6007#endif
Fred Drakec9680921999-12-13 16:37:25 +00006008#ifdef _SC_MQ_OPEN_MAX
6009 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6010#endif
6011#ifdef _SC_MQ_PRIO_MAX
6012 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6013#endif
Fred Draked86ed291999-12-15 15:34:33 +00006014#ifdef _SC_NACLS_MAX
6015 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6016#endif
Fred Drakec9680921999-12-13 16:37:25 +00006017#ifdef _SC_NGROUPS_MAX
6018 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6019#endif
6020#ifdef _SC_NL_ARGMAX
6021 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6022#endif
6023#ifdef _SC_NL_LANGMAX
6024 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6025#endif
6026#ifdef _SC_NL_MSGMAX
6027 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6028#endif
6029#ifdef _SC_NL_NMAX
6030 {"SC_NL_NMAX", _SC_NL_NMAX},
6031#endif
6032#ifdef _SC_NL_SETMAX
6033 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6034#endif
6035#ifdef _SC_NL_TEXTMAX
6036 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6037#endif
6038#ifdef _SC_NPROCESSORS_CONF
6039 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6040#endif
6041#ifdef _SC_NPROCESSORS_ONLN
6042 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6043#endif
Fred Draked86ed291999-12-15 15:34:33 +00006044#ifdef _SC_NPROC_CONF
6045 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6046#endif
6047#ifdef _SC_NPROC_ONLN
6048 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6049#endif
Fred Drakec9680921999-12-13 16:37:25 +00006050#ifdef _SC_NZERO
6051 {"SC_NZERO", _SC_NZERO},
6052#endif
6053#ifdef _SC_OPEN_MAX
6054 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6055#endif
6056#ifdef _SC_PAGESIZE
6057 {"SC_PAGESIZE", _SC_PAGESIZE},
6058#endif
6059#ifdef _SC_PAGE_SIZE
6060 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6061#endif
6062#ifdef _SC_PASS_MAX
6063 {"SC_PASS_MAX", _SC_PASS_MAX},
6064#endif
6065#ifdef _SC_PHYS_PAGES
6066 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6067#endif
6068#ifdef _SC_PII
6069 {"SC_PII", _SC_PII},
6070#endif
6071#ifdef _SC_PII_INTERNET
6072 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6073#endif
6074#ifdef _SC_PII_INTERNET_DGRAM
6075 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6076#endif
6077#ifdef _SC_PII_INTERNET_STREAM
6078 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6079#endif
6080#ifdef _SC_PII_OSI
6081 {"SC_PII_OSI", _SC_PII_OSI},
6082#endif
6083#ifdef _SC_PII_OSI_CLTS
6084 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6085#endif
6086#ifdef _SC_PII_OSI_COTS
6087 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6088#endif
6089#ifdef _SC_PII_OSI_M
6090 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6091#endif
6092#ifdef _SC_PII_SOCKET
6093 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6094#endif
6095#ifdef _SC_PII_XTI
6096 {"SC_PII_XTI", _SC_PII_XTI},
6097#endif
6098#ifdef _SC_POLL
6099 {"SC_POLL", _SC_POLL},
6100#endif
6101#ifdef _SC_PRIORITIZED_IO
6102 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6103#endif
6104#ifdef _SC_PRIORITY_SCHEDULING
6105 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6106#endif
6107#ifdef _SC_REALTIME_SIGNALS
6108 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6109#endif
6110#ifdef _SC_RE_DUP_MAX
6111 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6112#endif
6113#ifdef _SC_RTSIG_MAX
6114 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6115#endif
6116#ifdef _SC_SAVED_IDS
6117 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6118#endif
6119#ifdef _SC_SCHAR_MAX
6120 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6121#endif
6122#ifdef _SC_SCHAR_MIN
6123 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6124#endif
6125#ifdef _SC_SELECT
6126 {"SC_SELECT", _SC_SELECT},
6127#endif
6128#ifdef _SC_SEMAPHORES
6129 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6130#endif
6131#ifdef _SC_SEM_NSEMS_MAX
6132 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6133#endif
6134#ifdef _SC_SEM_VALUE_MAX
6135 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6136#endif
6137#ifdef _SC_SHARED_MEMORY_OBJECTS
6138 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6139#endif
6140#ifdef _SC_SHRT_MAX
6141 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6142#endif
6143#ifdef _SC_SHRT_MIN
6144 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6145#endif
6146#ifdef _SC_SIGQUEUE_MAX
6147 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6148#endif
6149#ifdef _SC_SIGRT_MAX
6150 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6151#endif
6152#ifdef _SC_SIGRT_MIN
6153 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6154#endif
Fred Draked86ed291999-12-15 15:34:33 +00006155#ifdef _SC_SOFTPOWER
6156 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6157#endif
Fred Drakec9680921999-12-13 16:37:25 +00006158#ifdef _SC_SPLIT_CACHE
6159 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6160#endif
6161#ifdef _SC_SSIZE_MAX
6162 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6163#endif
6164#ifdef _SC_STACK_PROT
6165 {"SC_STACK_PROT", _SC_STACK_PROT},
6166#endif
6167#ifdef _SC_STREAM_MAX
6168 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6169#endif
6170#ifdef _SC_SYNCHRONIZED_IO
6171 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6172#endif
6173#ifdef _SC_THREADS
6174 {"SC_THREADS", _SC_THREADS},
6175#endif
6176#ifdef _SC_THREAD_ATTR_STACKADDR
6177 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6178#endif
6179#ifdef _SC_THREAD_ATTR_STACKSIZE
6180 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6181#endif
6182#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6183 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6184#endif
6185#ifdef _SC_THREAD_KEYS_MAX
6186 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6187#endif
6188#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6189 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6190#endif
6191#ifdef _SC_THREAD_PRIO_INHERIT
6192 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6193#endif
6194#ifdef _SC_THREAD_PRIO_PROTECT
6195 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6196#endif
6197#ifdef _SC_THREAD_PROCESS_SHARED
6198 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6199#endif
6200#ifdef _SC_THREAD_SAFE_FUNCTIONS
6201 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6202#endif
6203#ifdef _SC_THREAD_STACK_MIN
6204 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6205#endif
6206#ifdef _SC_THREAD_THREADS_MAX
6207 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6208#endif
6209#ifdef _SC_TIMERS
6210 {"SC_TIMERS", _SC_TIMERS},
6211#endif
6212#ifdef _SC_TIMER_MAX
6213 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6214#endif
6215#ifdef _SC_TTY_NAME_MAX
6216 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6217#endif
6218#ifdef _SC_TZNAME_MAX
6219 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6220#endif
6221#ifdef _SC_T_IOV_MAX
6222 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6223#endif
6224#ifdef _SC_UCHAR_MAX
6225 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6226#endif
6227#ifdef _SC_UINT_MAX
6228 {"SC_UINT_MAX", _SC_UINT_MAX},
6229#endif
6230#ifdef _SC_UIO_MAXIOV
6231 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6232#endif
6233#ifdef _SC_ULONG_MAX
6234 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6235#endif
6236#ifdef _SC_USHRT_MAX
6237 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6238#endif
6239#ifdef _SC_VERSION
6240 {"SC_VERSION", _SC_VERSION},
6241#endif
6242#ifdef _SC_WORD_BIT
6243 {"SC_WORD_BIT", _SC_WORD_BIT},
6244#endif
6245#ifdef _SC_XBS5_ILP32_OFF32
6246 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6247#endif
6248#ifdef _SC_XBS5_ILP32_OFFBIG
6249 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6250#endif
6251#ifdef _SC_XBS5_LP64_OFF64
6252 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6253#endif
6254#ifdef _SC_XBS5_LPBIG_OFFBIG
6255 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6256#endif
6257#ifdef _SC_XOPEN_CRYPT
6258 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6259#endif
6260#ifdef _SC_XOPEN_ENH_I18N
6261 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6262#endif
6263#ifdef _SC_XOPEN_LEGACY
6264 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6265#endif
6266#ifdef _SC_XOPEN_REALTIME
6267 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6268#endif
6269#ifdef _SC_XOPEN_REALTIME_THREADS
6270 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6271#endif
6272#ifdef _SC_XOPEN_SHM
6273 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6274#endif
6275#ifdef _SC_XOPEN_UNIX
6276 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6277#endif
6278#ifdef _SC_XOPEN_VERSION
6279 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6280#endif
6281#ifdef _SC_XOPEN_XCU_VERSION
6282 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6283#endif
6284#ifdef _SC_XOPEN_XPG2
6285 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6286#endif
6287#ifdef _SC_XOPEN_XPG3
6288 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6289#endif
6290#ifdef _SC_XOPEN_XPG4
6291 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6292#endif
6293};
6294
6295static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006296conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006297{
6298 return conv_confname(arg, valuep, posix_constants_sysconf,
6299 sizeof(posix_constants_sysconf)
6300 / sizeof(struct constdef));
6301}
6302
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006303PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006304"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006305Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006306
6307static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006308posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006309{
6310 PyObject *result = NULL;
6311 int name;
6312
6313 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6314 int value;
6315
6316 errno = 0;
6317 value = sysconf(name);
6318 if (value == -1 && errno != 0)
6319 posix_error();
6320 else
6321 result = PyInt_FromLong(value);
6322 }
6323 return result;
6324}
6325#endif
6326
6327
Fred Drakebec628d1999-12-15 18:31:10 +00006328/* This code is used to ensure that the tables of configuration value names
6329 * are in sorted order as required by conv_confname(), and also to build the
6330 * the exported dictionaries that are used to publish information about the
6331 * names available on the host platform.
6332 *
6333 * Sorting the table at runtime ensures that the table is properly ordered
6334 * when used, even for platforms we're not able to test on. It also makes
6335 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006336 */
Fred Drakebec628d1999-12-15 18:31:10 +00006337
6338static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006339cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006340{
6341 const struct constdef *c1 =
6342 (const struct constdef *) v1;
6343 const struct constdef *c2 =
6344 (const struct constdef *) v2;
6345
6346 return strcmp(c1->name, c2->name);
6347}
6348
6349static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006350setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006351 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006352{
Fred Drakebec628d1999-12-15 18:31:10 +00006353 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006354 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006355
6356 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6357 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006358 if (d == NULL)
6359 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006360
Barry Warsaw3155db32000-04-13 15:20:40 +00006361 for (i=0; i < tablesize; ++i) {
6362 PyObject *o = PyInt_FromLong(table[i].value);
6363 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6364 Py_XDECREF(o);
6365 Py_DECREF(d);
6366 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006367 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006368 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006369 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006370 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006371}
6372
Fred Drakebec628d1999-12-15 18:31:10 +00006373/* Return -1 on failure, 0 on success. */
6374static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006375setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006376{
6377#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006378 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006379 sizeof(posix_constants_pathconf)
6380 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006381 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006382 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006383#endif
6384#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006385 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006386 sizeof(posix_constants_confstr)
6387 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006388 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006389 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006390#endif
6391#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006392 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006393 sizeof(posix_constants_sysconf)
6394 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006395 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006396 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006397#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006398 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006399}
Fred Draked86ed291999-12-15 15:34:33 +00006400
6401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006402PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006403"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006404Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006406
6407static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006408posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006409{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006410 abort();
6411 /*NOTREACHED*/
6412 Py_FatalError("abort() called from Python code didn't abort!");
6413 return NULL;
6414}
Fred Drakebec628d1999-12-15 18:31:10 +00006415
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006416#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006417PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006418"startfile(filepath [, operation]) - Start a file with its associated\n\
6419application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006420\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006421When \"operation\" is not specified or \"open\", this acts like\n\
6422double-clicking the file in Explorer, or giving the file name as an\n\
6423argument to the DOS \"start\" command: the file is opened with whatever\n\
6424application (if any) its extension is associated.\n\
6425When another \"operation\" is given, it specifies what should be done with\n\
6426the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006427\n\
6428startfile returns as soon as the associated application is launched.\n\
6429There is no option to wait for the application to close, and no way\n\
6430to retrieve the application's exit status.\n\
6431\n\
6432The filepath is relative to the current directory. If you want to use\n\
6433an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006435
6436static PyObject *
6437win32_startfile(PyObject *self, PyObject *args)
6438{
6439 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006440 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006441 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006442#ifdef Py_WIN_WIDE_FILENAMES
6443 if (unicode_file_names()) {
6444 PyObject *unipath, *woperation = NULL;
6445 if (!PyArg_ParseTuple(args, "U|s:startfile",
6446 &unipath, &operation)) {
6447 PyErr_Clear();
6448 goto normal;
6449 }
6450
6451
6452 if (operation) {
6453 woperation = PyUnicode_DecodeASCII(operation,
6454 strlen(operation), NULL);
6455 if (!woperation) {
6456 PyErr_Clear();
6457 operation = NULL;
6458 goto normal;
6459 }
6460 }
6461
6462 Py_BEGIN_ALLOW_THREADS
6463 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6464 PyUnicode_AS_UNICODE(unipath),
6465 NULL, NULL, SW_SHOWNORMAL);
6466 Py_END_ALLOW_THREADS
6467
6468 Py_XDECREF(woperation);
6469 if (rc <= (HINSTANCE)32) {
6470 PyObject *errval = win32_error_unicode("startfile",
6471 PyUnicode_AS_UNICODE(unipath));
6472 return errval;
6473 }
6474 Py_INCREF(Py_None);
6475 return Py_None;
6476 }
6477#endif
6478
6479normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006480 if (!PyArg_ParseTuple(args, "et|s:startfile",
6481 Py_FileSystemDefaultEncoding, &filepath,
6482 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006483 return NULL;
6484 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006485 rc = ShellExecute((HWND)0, operation, filepath,
6486 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006487 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006488 if (rc <= (HINSTANCE)32) {
6489 PyObject *errval = win32_error("startfile", filepath);
6490 PyMem_Free(filepath);
6491 return errval;
6492 }
6493 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006494 Py_INCREF(Py_None);
6495 return Py_None;
6496}
6497#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006498
Martin v. Löwis438b5342002-12-27 10:16:42 +00006499#ifdef HAVE_GETLOADAVG
6500PyDoc_STRVAR(posix_getloadavg__doc__,
6501"getloadavg() -> (float, float, float)\n\n\
6502Return the number of processes in the system run queue averaged over\n\
6503the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6504was unobtainable");
6505
6506static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006507posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006508{
6509 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006510 if (getloadavg(loadavg, 3)!=3) {
6511 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6512 return NULL;
6513 } else
6514 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6515}
6516#endif
6517
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006518#ifdef MS_WINDOWS
6519
6520PyDoc_STRVAR(win32_urandom__doc__,
6521"urandom(n) -> str\n\n\
6522Return a string of n random bytes suitable for cryptographic use.");
6523
6524typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6525 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6526 DWORD dwFlags );
6527typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6528 BYTE *pbBuffer );
6529
6530static CRYPTGENRANDOM pCryptGenRandom = NULL;
6531static HCRYPTPROV hCryptProv = 0;
6532
Tim Peters4ad82172004-08-30 17:02:04 +00006533static PyObject*
6534win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006535{
Tim Petersd3115382004-08-30 17:36:46 +00006536 int howMany;
6537 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006538
Tim Peters4ad82172004-08-30 17:02:04 +00006539 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006540 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006541 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006542 if (howMany < 0)
6543 return PyErr_Format(PyExc_ValueError,
6544 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006545
Tim Peters4ad82172004-08-30 17:02:04 +00006546 if (hCryptProv == 0) {
6547 HINSTANCE hAdvAPI32 = NULL;
6548 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006549
Tim Peters4ad82172004-08-30 17:02:04 +00006550 /* Obtain handle to the DLL containing CryptoAPI
6551 This should not fail */
6552 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6553 if(hAdvAPI32 == NULL)
6554 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006555
Tim Peters4ad82172004-08-30 17:02:04 +00006556 /* Obtain pointers to the CryptoAPI functions
6557 This will fail on some early versions of Win95 */
6558 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6559 hAdvAPI32,
6560 "CryptAcquireContextA");
6561 if (pCryptAcquireContext == NULL)
6562 return PyErr_Format(PyExc_NotImplementedError,
6563 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006564
Tim Peters4ad82172004-08-30 17:02:04 +00006565 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6566 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006567 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006568 return PyErr_Format(PyExc_NotImplementedError,
6569 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006570
Tim Peters4ad82172004-08-30 17:02:04 +00006571 /* Acquire context */
6572 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6573 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6574 return win32_error("CryptAcquireContext", NULL);
6575 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006576
Tim Peters4ad82172004-08-30 17:02:04 +00006577 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00006578 result = PyString_FromStringAndSize(NULL, howMany);
6579 if (result != NULL) {
6580 /* Get random data */
6581 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6582 PyString_AS_STRING(result))) {
6583 Py_DECREF(result);
6584 return win32_error("CryptGenRandom", NULL);
6585 }
Tim Peters4ad82172004-08-30 17:02:04 +00006586 }
Tim Petersd3115382004-08-30 17:36:46 +00006587 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006588}
6589#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006590
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006591PyDoc_STRVAR(device_encoding__doc__,
6592"device_encoding(fd) -> str\n\n\
6593Return a string describing the encoding of the device\n\
6594if the output is a terminal; else return None.");
6595
6596static PyObject *
6597device_encoding(PyObject *self, PyObject *args)
6598{
6599 int fd;
6600 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6601 return NULL;
6602 if (!isatty(fd)) {
6603 Py_INCREF(Py_None);
6604 return Py_None;
6605 }
6606#if defined(MS_WINDOWS) || defined(MS_WIN64)
6607 if (fd == 0) {
6608 char buf[100];
6609 sprintf(buf, "cp%d", GetConsoleCP());
6610 return PyUnicode_FromString(buf);
6611 }
6612 if (fd == 1 || fd == 2) {
6613 char buf[100];
6614 sprintf(buf, "cp%d", GetConsoleOutputCP());
6615 return PyUnicode_FromString(buf);
6616 }
6617#elif defined(CODESET)
6618 {
6619 char *codeset = nl_langinfo(CODESET);
6620 if (codeset)
6621 return PyUnicode_FromString(codeset);
6622 }
6623#endif
6624 Py_INCREF(Py_None);
6625 return Py_None;
6626}
6627
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006628#ifdef __VMS
6629/* Use openssl random routine */
6630#include <openssl/rand.h>
6631PyDoc_STRVAR(vms_urandom__doc__,
6632"urandom(n) -> str\n\n\
6633Return a string of n random bytes suitable for cryptographic use.");
6634
6635static PyObject*
6636vms_urandom(PyObject *self, PyObject *args)
6637{
6638 int howMany;
6639 PyObject* result;
6640
6641 /* Read arguments */
6642 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6643 return NULL;
6644 if (howMany < 0)
6645 return PyErr_Format(PyExc_ValueError,
6646 "negative argument not allowed");
6647
6648 /* Allocate bytes */
6649 result = PyString_FromStringAndSize(NULL, howMany);
6650 if (result != NULL) {
6651 /* Get random data */
6652 if (RAND_pseudo_bytes((unsigned char*)
6653 PyString_AS_STRING(result),
6654 howMany) < 0) {
6655 Py_DECREF(result);
6656 return PyErr_Format(PyExc_ValueError,
6657 "RAND_pseudo_bytes");
6658 }
6659 }
6660 return result;
6661}
6662#endif
6663
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006664static PyMethodDef posix_methods[] = {
6665 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6666#ifdef HAVE_TTYNAME
6667 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6668#endif
6669 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006670#ifdef HAVE_CHFLAGS
6671 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6672#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006673 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006674#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006675 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006676#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006677#ifdef HAVE_LCHFLAGS
6678 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6679#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006680#ifdef HAVE_LCHOWN
6681 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6682#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006683#ifdef HAVE_CHROOT
6684 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6685#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006686#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006687 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006688#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006689#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006690 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006691 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006692#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006693#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006694 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006695#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006696 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6697 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6698 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006699#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006700 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006701#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006702#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006703 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006704#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006705 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6706 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6707 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006708 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006709#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006710 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006711#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006712#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006713 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006714#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006715 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006716#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006717 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006718#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006719 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6720 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6721 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006722#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006723 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006724#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006725 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006726#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006727 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6728 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006729#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006730#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006731 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6732 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006733#if defined(PYOS_OS2)
6734 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6735 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6736#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006737#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006738#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006739 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006740#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006741#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006742 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006743#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006744#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006745 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006746#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006747#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006748 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006749#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006750#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006751 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006752#endif /* HAVE_GETEGID */
6753#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006754 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006755#endif /* HAVE_GETEUID */
6756#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006757 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006758#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006759#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006760 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006761#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006762 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006763#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006764 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006765#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006766#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006767 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006768#endif /* HAVE_GETPPID */
6769#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006770 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006771#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006772#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006773 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006774#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006775#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006776 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006777#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006778#ifdef HAVE_KILLPG
6779 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6780#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006781#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006782 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006783#endif /* HAVE_PLOCK */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006784#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006785 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006786#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006787#ifdef HAVE_SETEUID
6788 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6789#endif /* HAVE_SETEUID */
6790#ifdef HAVE_SETEGID
6791 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6792#endif /* HAVE_SETEGID */
6793#ifdef HAVE_SETREUID
6794 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6795#endif /* HAVE_SETREUID */
6796#ifdef HAVE_SETREGID
6797 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6798#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006799#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006800 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006801#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006802#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006803 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006804#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006805#ifdef HAVE_GETPGID
6806 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6807#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006808#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006809 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006810#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006811#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006812 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006813#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006814#ifdef HAVE_WAIT3
6815 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6816#endif /* HAVE_WAIT3 */
6817#ifdef HAVE_WAIT4
6818 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6819#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006820#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006821 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006822#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006823#ifdef HAVE_GETSID
6824 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6825#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006826#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006827 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006828#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006829#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006830 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006831#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006832#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006833 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006834#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006835#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006836 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006837#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006838 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6839 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006840 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006841 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6842 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6843 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6844 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6845 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6846 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006847 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006848#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006849 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006850#endif
6851#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006852 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006853#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006854#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006855 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6856#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006857#ifdef HAVE_DEVICE_MACROS
6858 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6859 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6860 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6861#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006862#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006863 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006864#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006865#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006866 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006867#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006868#ifdef HAVE_UNSETENV
6869 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6870#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00006871#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006872 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00006873#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00006874#ifdef HAVE_FCHDIR
6875 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6876#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006877#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006878 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006879#endif
6880#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006881 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006882#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006883#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006884#ifdef WCOREDUMP
6885 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6886#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006887#ifdef WIFCONTINUED
6888 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6889#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006890#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006891 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006892#endif /* WIFSTOPPED */
6893#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006894 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006895#endif /* WIFSIGNALED */
6896#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006897 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006898#endif /* WIFEXITED */
6899#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006900 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006901#endif /* WEXITSTATUS */
6902#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006903 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006904#endif /* WTERMSIG */
6905#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006906 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006907#endif /* WSTOPSIG */
6908#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00006909#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006910 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006911#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00006912#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006913 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006914#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00006915#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00006916 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006917#endif
6918#ifdef HAVE_TEMPNAM
6919 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
6920#endif
6921#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00006922 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006923#endif
Fred Drakec9680921999-12-13 16:37:25 +00006924#ifdef HAVE_CONFSTR
6925 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
6926#endif
6927#ifdef HAVE_SYSCONF
6928 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
6929#endif
6930#ifdef HAVE_FPATHCONF
6931 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
6932#endif
6933#ifdef HAVE_PATHCONF
6934 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
6935#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006936 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006937#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00006938 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
6939#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006940#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00006941 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00006942#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006943 #ifdef MS_WINDOWS
6944 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
6945 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006946 #ifdef __VMS
6947 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
6948 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006949 {NULL, NULL} /* Sentinel */
6950};
6951
6952
Barry Warsaw4a342091996-12-19 23:50:02 +00006953static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006954ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00006955{
Fred Drake4d1e64b2002-04-15 19:40:07 +00006956 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00006957}
6958
Guido van Rossumd48f2521997-12-05 22:19:34 +00006959#if defined(PYOS_OS2)
6960/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006961static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006962{
6963 APIRET rc;
6964 ULONG values[QSV_MAX+1];
6965 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00006966 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00006967
6968 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00006969 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006970 Py_END_ALLOW_THREADS
6971
6972 if (rc != NO_ERROR) {
6973 os2_error(rc);
6974 return -1;
6975 }
6976
Fred Drake4d1e64b2002-04-15 19:40:07 +00006977 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6978 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
6979 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
6980 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
6981 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
6982 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
6983 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006984
6985 switch (values[QSV_VERSION_MINOR]) {
6986 case 0: ver = "2.00"; break;
6987 case 10: ver = "2.10"; break;
6988 case 11: ver = "2.11"; break;
6989 case 30: ver = "3.00"; break;
6990 case 40: ver = "4.00"; break;
6991 case 50: ver = "5.00"; break;
6992 default:
Tim Peters885d4572001-11-28 20:27:42 +00006993 PyOS_snprintf(tmp, sizeof(tmp),
6994 "%d-%d", values[QSV_VERSION_MAJOR],
6995 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006996 ver = &tmp[0];
6997 }
6998
6999 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007000 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007001 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007002
7003 /* Add Indicator of Which Drive was Used to Boot the System */
7004 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7005 tmp[1] = ':';
7006 tmp[2] = '\0';
7007
Fred Drake4d1e64b2002-04-15 19:40:07 +00007008 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007009}
7010#endif
7011
Barry Warsaw4a342091996-12-19 23:50:02 +00007012static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007013all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007014{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007015#ifdef F_OK
7016 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007017#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007018#ifdef R_OK
7019 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007020#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007021#ifdef W_OK
7022 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007023#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007024#ifdef X_OK
7025 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007026#endif
Fred Drakec9680921999-12-13 16:37:25 +00007027#ifdef NGROUPS_MAX
7028 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7029#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007030#ifdef TMP_MAX
7031 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7032#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007033#ifdef WCONTINUED
7034 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7035#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007036#ifdef WNOHANG
7037 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007038#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007039#ifdef WUNTRACED
7040 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7041#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007042#ifdef O_RDONLY
7043 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7044#endif
7045#ifdef O_WRONLY
7046 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7047#endif
7048#ifdef O_RDWR
7049 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7050#endif
7051#ifdef O_NDELAY
7052 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7053#endif
7054#ifdef O_NONBLOCK
7055 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7056#endif
7057#ifdef O_APPEND
7058 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7059#endif
7060#ifdef O_DSYNC
7061 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7062#endif
7063#ifdef O_RSYNC
7064 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7065#endif
7066#ifdef O_SYNC
7067 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7068#endif
7069#ifdef O_NOCTTY
7070 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7071#endif
7072#ifdef O_CREAT
7073 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7074#endif
7075#ifdef O_EXCL
7076 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7077#endif
7078#ifdef O_TRUNC
7079 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7080#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007081#ifdef O_BINARY
7082 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7083#endif
7084#ifdef O_TEXT
7085 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7086#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007087#ifdef O_LARGEFILE
7088 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7089#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007090#ifdef O_SHLOCK
7091 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7092#endif
7093#ifdef O_EXLOCK
7094 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7095#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007096
Tim Peters5aa91602002-01-30 05:46:57 +00007097/* MS Windows */
7098#ifdef O_NOINHERIT
7099 /* Don't inherit in child processes. */
7100 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7101#endif
7102#ifdef _O_SHORT_LIVED
7103 /* Optimize for short life (keep in memory). */
7104 /* MS forgot to define this one with a non-underscore form too. */
7105 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7106#endif
7107#ifdef O_TEMPORARY
7108 /* Automatically delete when last handle is closed. */
7109 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7110#endif
7111#ifdef O_RANDOM
7112 /* Optimize for random access. */
7113 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7114#endif
7115#ifdef O_SEQUENTIAL
7116 /* Optimize for sequential access. */
7117 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7118#endif
7119
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007120/* GNU extensions. */
7121#ifdef O_DIRECT
7122 /* Direct disk access. */
7123 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7124#endif
7125#ifdef O_DIRECTORY
7126 /* Must be a directory. */
7127 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7128#endif
7129#ifdef O_NOFOLLOW
7130 /* Do not follow links. */
7131 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7132#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007133
Barry Warsaw5676bd12003-01-07 20:57:09 +00007134 /* These come from sysexits.h */
7135#ifdef EX_OK
7136 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007137#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007138#ifdef EX_USAGE
7139 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007140#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007141#ifdef EX_DATAERR
7142 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007143#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007144#ifdef EX_NOINPUT
7145 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007146#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007147#ifdef EX_NOUSER
7148 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007149#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007150#ifdef EX_NOHOST
7151 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007152#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007153#ifdef EX_UNAVAILABLE
7154 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007155#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007156#ifdef EX_SOFTWARE
7157 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007158#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007159#ifdef EX_OSERR
7160 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007161#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007162#ifdef EX_OSFILE
7163 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007164#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007165#ifdef EX_CANTCREAT
7166 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007167#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007168#ifdef EX_IOERR
7169 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007170#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007171#ifdef EX_TEMPFAIL
7172 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007173#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007174#ifdef EX_PROTOCOL
7175 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007176#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007177#ifdef EX_NOPERM
7178 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007179#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007180#ifdef EX_CONFIG
7181 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007182#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007183#ifdef EX_NOTFOUND
7184 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007185#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007186
Guido van Rossum246bc171999-02-01 23:54:31 +00007187#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007188#if defined(PYOS_OS2) && defined(PYCC_GCC)
7189 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7190 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7191 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7192 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7193 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7194 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7195 if (ins(d, "P_PM", (long)P_PM)) return -1;
7196 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7197 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7198 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7199 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7200 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7201 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7202 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7203 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7204 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7205 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7206 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7207 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7208 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7209#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007210 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7211 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7212 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7213 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7214 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007215#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007216#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007217
Guido van Rossumd48f2521997-12-05 22:19:34 +00007218#if defined(PYOS_OS2)
7219 if (insertvalues(d)) return -1;
7220#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007221 return 0;
7222}
7223
7224
Tim Peters5aa91602002-01-30 05:46:57 +00007225#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007226#define INITFUNC initnt
7227#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007228
7229#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007230#define INITFUNC initos2
7231#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007232
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007233#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007234#define INITFUNC initposix
7235#define MODNAME "posix"
7236#endif
7237
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007238PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007239INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007240{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007241 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007242
Fred Drake4d1e64b2002-04-15 19:40:07 +00007243 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007244 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007245 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007246 if (m == NULL)
7247 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007248
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007249 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007250 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007251 Py_XINCREF(v);
7252 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007253 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007254 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007255
Fred Drake4d1e64b2002-04-15 19:40:07 +00007256 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007257 return;
7258
Fred Drake4d1e64b2002-04-15 19:40:07 +00007259 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007260 return;
7261
Fred Drake4d1e64b2002-04-15 19:40:07 +00007262 Py_INCREF(PyExc_OSError);
7263 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007264
Guido van Rossumb3d39562000-01-31 18:41:26 +00007265#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007266 if (posix_putenv_garbage == NULL)
7267 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007268#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007269
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007270 if (!initialized) {
7271 stat_result_desc.name = MODNAME ".stat_result";
7272 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7273 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7274 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7275 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7276 structseq_new = StatResultType.tp_new;
7277 StatResultType.tp_new = statresult_new;
7278
7279 statvfs_result_desc.name = MODNAME ".statvfs_result";
7280 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7281 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007282 Py_INCREF((PyObject*) &StatResultType);
7283 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007284 Py_INCREF((PyObject*) &StatVFSResultType);
7285 PyModule_AddObject(m, "statvfs_result",
7286 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007287 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007288
7289#ifdef __APPLE__
7290 /*
7291 * Step 2 of weak-linking support on Mac OS X.
7292 *
7293 * The code below removes functions that are not available on the
7294 * currently active platform.
7295 *
7296 * This block allow one to use a python binary that was build on
7297 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7298 * OSX 10.4.
7299 */
7300#ifdef HAVE_FSTATVFS
7301 if (fstatvfs == NULL) {
7302 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7303 return;
7304 }
7305 }
7306#endif /* HAVE_FSTATVFS */
7307
7308#ifdef HAVE_STATVFS
7309 if (statvfs == NULL) {
7310 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7311 return;
7312 }
7313 }
7314#endif /* HAVE_STATVFS */
7315
7316# ifdef HAVE_LCHOWN
7317 if (lchown == NULL) {
7318 if (PyObject_DelAttrString(m, "lchown") == -1) {
7319 return;
7320 }
7321 }
7322#endif /* HAVE_LCHOWN */
7323
7324
7325#endif /* __APPLE__ */
7326
Guido van Rossumb6775db1994-08-01 11:34:53 +00007327}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007328
7329#ifdef __cplusplus
7330}
7331#endif