blob: d710a7366e449606d2c22e732353570c1cd07158 [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)
Barry Warsawd58d7641998-07-23 16:14:40 +00004307 return posix_error_with_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004308
4309 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004310 if (arg_is_unicode) {
4311 PyObject *w;
4312
4313 w = PyUnicode_FromEncodedObject(v,
4314 Py_FileSystemDefaultEncoding,
4315 "strict");
4316 if (w != NULL) {
4317 Py_DECREF(v);
4318 v = w;
4319 }
4320 else {
4321 /* fall back to the original byte string, as
4322 discussed in patch #683592 */
4323 PyErr_Clear();
4324 }
4325 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004326 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004327}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004328#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004329
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004330
Guido van Rossumb6775db1994-08-01 11:34:53 +00004331#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004332PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004333"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004334Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004335
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004336static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004337posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004338{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004339 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004340}
4341#endif /* HAVE_SYMLINK */
4342
4343
4344#ifdef HAVE_TIMES
4345#ifndef HZ
4346#define HZ 60 /* Universal constant :-) */
4347#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004348
Guido van Rossumd48f2521997-12-05 22:19:34 +00004349#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4350static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004351system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004352{
4353 ULONG value = 0;
4354
4355 Py_BEGIN_ALLOW_THREADS
4356 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4357 Py_END_ALLOW_THREADS
4358
4359 return value;
4360}
4361
4362static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004363posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004364{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004365 /* Currently Only Uptime is Provided -- Others Later */
4366 return Py_BuildValue("ddddd",
4367 (double)0 /* t.tms_utime / HZ */,
4368 (double)0 /* t.tms_stime / HZ */,
4369 (double)0 /* t.tms_cutime / HZ */,
4370 (double)0 /* t.tms_cstime / HZ */,
4371 (double)system_uptime() / 1000);
4372}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004373#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004374static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004375posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004376{
4377 struct tms t;
4378 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004379 errno = 0;
4380 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004381 if (c == (clock_t) -1)
4382 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004383 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004384 (double)t.tms_utime / HZ,
4385 (double)t.tms_stime / HZ,
4386 (double)t.tms_cutime / HZ,
4387 (double)t.tms_cstime / HZ,
4388 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004389}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004390#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004391#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004392
4393
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004394#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004395#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004396static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004397posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004398{
4399 FILETIME create, exit, kernel, user;
4400 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004401 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004402 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4403 /* The fields of a FILETIME structure are the hi and lo part
4404 of a 64-bit value expressed in 100 nanosecond units.
4405 1e7 is one second in such units; 1e-7 the inverse.
4406 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4407 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004408 return Py_BuildValue(
4409 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004410 (double)(kernel.dwHighDateTime*429.4967296 +
4411 kernel.dwLowDateTime*1e-7),
4412 (double)(user.dwHighDateTime*429.4967296 +
4413 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004414 (double)0,
4415 (double)0,
4416 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004417}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004418#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004419
4420#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004421PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004422"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004423Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004424#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004425
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004426
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004427#ifdef HAVE_GETSID
4428PyDoc_STRVAR(posix_getsid__doc__,
4429"getsid(pid) -> sid\n\n\
4430Call the system call getsid().");
4431
4432static PyObject *
4433posix_getsid(PyObject *self, PyObject *args)
4434{
4435 int pid, sid;
4436 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4437 return NULL;
4438 sid = getsid(pid);
4439 if (sid < 0)
4440 return posix_error();
4441 return PyInt_FromLong((long)sid);
4442}
4443#endif /* HAVE_GETSID */
4444
4445
Guido van Rossumb6775db1994-08-01 11:34:53 +00004446#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004447PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004448"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004449Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004450
Barry Warsaw53699e91996-12-10 23:23:01 +00004451static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004452posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004453{
Guido van Rossum687dd131993-05-17 08:34:16 +00004454 if (setsid() < 0)
4455 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004456 Py_INCREF(Py_None);
4457 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004458}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004459#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004460
Guido van Rossumb6775db1994-08-01 11:34:53 +00004461#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004462PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004463"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004464Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004465
Barry Warsaw53699e91996-12-10 23:23:01 +00004466static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004467posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004468{
4469 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004470 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004471 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004472 if (setpgid(pid, pgrp) < 0)
4473 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004474 Py_INCREF(Py_None);
4475 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004476}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004477#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004478
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004479
Guido van Rossumb6775db1994-08-01 11:34:53 +00004480#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004481PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004482"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004483Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004484
Barry Warsaw53699e91996-12-10 23:23:01 +00004485static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004486posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004487{
4488 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004489 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004490 return NULL;
4491 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004492 if (pgid < 0)
4493 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004494 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004495}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004496#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004497
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004498
Guido van Rossumb6775db1994-08-01 11:34:53 +00004499#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004500PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004501"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004502Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004503
Barry Warsaw53699e91996-12-10 23:23:01 +00004504static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004505posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004506{
4507 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004508 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004509 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004510 if (tcsetpgrp(fd, pgid) < 0)
4511 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004512 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004513 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004514}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004515#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004516
Guido van Rossum687dd131993-05-17 08:34:16 +00004517/* Functions acting on file descriptors */
4518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004519PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004520"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004521Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004522
Barry Warsaw53699e91996-12-10 23:23:01 +00004523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004524posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004525{
Mark Hammondef8b6542001-05-13 08:04:26 +00004526 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004527 int flag;
4528 int mode = 0777;
4529 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004530
4531#ifdef MS_WINDOWS
4532 if (unicode_file_names()) {
4533 PyUnicodeObject *po;
4534 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4535 Py_BEGIN_ALLOW_THREADS
4536 /* PyUnicode_AS_UNICODE OK without thread
4537 lock as it is a simple dereference. */
4538 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4539 Py_END_ALLOW_THREADS
4540 if (fd < 0)
4541 return posix_error();
4542 return PyInt_FromLong((long)fd);
4543 }
4544 /* Drop the argument parsing error as narrow strings
4545 are also valid. */
4546 PyErr_Clear();
4547 }
4548#endif
4549
Tim Peters5aa91602002-01-30 05:46:57 +00004550 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004551 Py_FileSystemDefaultEncoding, &file,
4552 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004553 return NULL;
4554
Barry Warsaw53699e91996-12-10 23:23:01 +00004555 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004556 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004557 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004558 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004559 return posix_error_with_allocated_filename(file);
4560 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004561 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004562}
4563
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004565PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004566"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004567Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004568
Barry Warsaw53699e91996-12-10 23:23:01 +00004569static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004570posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004571{
4572 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004573 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004574 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004575 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004576 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004577 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004578 if (res < 0)
4579 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004580 Py_INCREF(Py_None);
4581 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004582}
4583
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004585PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004586"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004587Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004588
Barry Warsaw53699e91996-12-10 23:23:01 +00004589static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004590posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004591{
4592 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004593 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004594 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004595 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004596 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004597 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004598 if (fd < 0)
4599 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004600 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004601}
4602
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004604PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004605"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004606Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004607
Barry Warsaw53699e91996-12-10 23:23:01 +00004608static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004609posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004610{
4611 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004612 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004613 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004614 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004615 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004616 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004617 if (res < 0)
4618 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004619 Py_INCREF(Py_None);
4620 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004621}
4622
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004624PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004625"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004626Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004627
Barry Warsaw53699e91996-12-10 23:23:01 +00004628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004629posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004630{
4631 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004632#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004633 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004634#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004635 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004636#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004637 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004638 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004639 return NULL;
4640#ifdef SEEK_SET
4641 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4642 switch (how) {
4643 case 0: how = SEEK_SET; break;
4644 case 1: how = SEEK_CUR; break;
4645 case 2: how = SEEK_END; break;
4646 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004647#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004648
4649#if !defined(HAVE_LARGEFILE_SUPPORT)
4650 pos = PyInt_AsLong(posobj);
4651#else
4652 pos = PyLong_Check(posobj) ?
4653 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4654#endif
4655 if (PyErr_Occurred())
4656 return NULL;
4657
Barry Warsaw53699e91996-12-10 23:23:01 +00004658 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004659#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004660 res = _lseeki64(fd, pos, how);
4661#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004662 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004663#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004664 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004665 if (res < 0)
4666 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004667
4668#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004669 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004670#else
4671 return PyLong_FromLongLong(res);
4672#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004673}
4674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004676PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004677"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004678Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004679
Barry Warsaw53699e91996-12-10 23:23:01 +00004680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004681posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004682{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004683 int fd, size;
4684 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004685 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004686 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004687 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004688 if (size < 0) {
4689 errno = EINVAL;
4690 return posix_error();
4691 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00004692 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004693 if (buffer == NULL)
4694 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004695 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00004696 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004697 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004698 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004700 return posix_error();
4701 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004702 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00004703 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004704 return buffer;
4705}
4706
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004708PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004709"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004710Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004711
Barry Warsaw53699e91996-12-10 23:23:01 +00004712static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004713posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004714{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004715 int fd;
4716 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004717 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004718
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004719 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004720 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004721 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004722 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004723 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004724 if (size < 0)
4725 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004726 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004727}
4728
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004730PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004731"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004732Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004733
Barry Warsaw53699e91996-12-10 23:23:01 +00004734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004735posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004736{
4737 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004738 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004739 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004740 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004741 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004742#ifdef __VMS
4743 /* on OpenVMS we must ensure that all bytes are written to the file */
4744 fsync(fd);
4745#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004746 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004747 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004748 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004749 if (res != 0) {
4750#ifdef MS_WINDOWS
4751 return win32_error("fstat", NULL);
4752#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004753 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004754#endif
4755 }
Tim Peters5aa91602002-01-30 05:46:57 +00004756
Martin v. Löwis14694662006-02-03 12:54:16 +00004757 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004758}
4759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004760PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004761"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004762Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004763connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004764
4765static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004766posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004767{
4768 int fd;
4769 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4770 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004771 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004772}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004773
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004774#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004775PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004776"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004778
Barry Warsaw53699e91996-12-10 23:23:01 +00004779static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004780posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004781{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004782#if defined(PYOS_OS2)
4783 HFILE read, write;
4784 APIRET rc;
4785
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004786 Py_BEGIN_ALLOW_THREADS
4787 rc = DosCreatePipe( &read, &write, 4096);
4788 Py_END_ALLOW_THREADS
4789 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004790 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004791
4792 return Py_BuildValue("(ii)", read, write);
4793#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004794#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004795 int fds[2];
4796 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004797 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004798 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004799 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004800 if (res != 0)
4801 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004802 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004803#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004804 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004805 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004806 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004807 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004808 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004809 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004810 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004811 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004812 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4813 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004814 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004815#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004816#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004817}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004818#endif /* HAVE_PIPE */
4819
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004820
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004821#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004822PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004823"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004824Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004825
Barry Warsaw53699e91996-12-10 23:23:01 +00004826static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004827posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004828{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004829 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004830 int mode = 0666;
4831 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004832 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004833 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004834 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004835 res = mkfifo(filename, mode);
4836 Py_END_ALLOW_THREADS
4837 if (res < 0)
4838 return posix_error();
4839 Py_INCREF(Py_None);
4840 return Py_None;
4841}
4842#endif
4843
4844
Neal Norwitz11690112002-07-30 01:08:28 +00004845#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004846PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004847"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004848Create a filesystem node (file, device special file or named pipe)\n\
4849named filename. mode specifies both the permissions to use and the\n\
4850type of node to be created, being combined (bitwise OR) with one of\n\
4851S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004852device defines the newly created device special file (probably using\n\
4853os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004854
4855
4856static PyObject *
4857posix_mknod(PyObject *self, PyObject *args)
4858{
4859 char *filename;
4860 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004861 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004862 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00004863 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004864 return NULL;
4865 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004866 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00004867 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004868 if (res < 0)
4869 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004870 Py_INCREF(Py_None);
4871 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004872}
4873#endif
4874
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004875#ifdef HAVE_DEVICE_MACROS
4876PyDoc_STRVAR(posix_major__doc__,
4877"major(device) -> major number\n\
4878Extracts a device major number from a raw device number.");
4879
4880static PyObject *
4881posix_major(PyObject *self, PyObject *args)
4882{
4883 int device;
4884 if (!PyArg_ParseTuple(args, "i:major", &device))
4885 return NULL;
4886 return PyInt_FromLong((long)major(device));
4887}
4888
4889PyDoc_STRVAR(posix_minor__doc__,
4890"minor(device) -> minor number\n\
4891Extracts a device minor number from a raw device number.");
4892
4893static PyObject *
4894posix_minor(PyObject *self, PyObject *args)
4895{
4896 int device;
4897 if (!PyArg_ParseTuple(args, "i:minor", &device))
4898 return NULL;
4899 return PyInt_FromLong((long)minor(device));
4900}
4901
4902PyDoc_STRVAR(posix_makedev__doc__,
4903"makedev(major, minor) -> device number\n\
4904Composes a raw device number from the major and minor device numbers.");
4905
4906static PyObject *
4907posix_makedev(PyObject *self, PyObject *args)
4908{
4909 int major, minor;
4910 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
4911 return NULL;
4912 return PyInt_FromLong((long)makedev(major, minor));
4913}
4914#endif /* device macros */
4915
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004916
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004917#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004918PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004919"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004920Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004921
Barry Warsaw53699e91996-12-10 23:23:01 +00004922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004923posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004924{
4925 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004926 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004927 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004928 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004929
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004930 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004931 return NULL;
4932
4933#if !defined(HAVE_LARGEFILE_SUPPORT)
4934 length = PyInt_AsLong(lenobj);
4935#else
4936 length = PyLong_Check(lenobj) ?
4937 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
4938#endif
4939 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004940 return NULL;
4941
Barry Warsaw53699e91996-12-10 23:23:01 +00004942 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004943 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00004944 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004945 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004946 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004947 return NULL;
4948 }
Barry Warsaw53699e91996-12-10 23:23:01 +00004949 Py_INCREF(Py_None);
4950 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004951}
4952#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004953
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004954#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004955PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004956"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004957Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004958
Fred Drake762e2061999-08-26 17:23:54 +00004959/* Save putenv() parameters as values here, so we can collect them when they
4960 * get re-set with another call for the same key. */
4961static PyObject *posix_putenv_garbage;
4962
Tim Peters5aa91602002-01-30 05:46:57 +00004963static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004964posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004965{
4966 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004967 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00004968 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00004969 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004970
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004971 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004972 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00004973
4974#if defined(PYOS_OS2)
4975 if (stricmp(s1, "BEGINLIBPATH") == 0) {
4976 APIRET rc;
4977
Guido van Rossumd48f2521997-12-05 22:19:34 +00004978 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
4979 if (rc != NO_ERROR)
4980 return os2_error(rc);
4981
4982 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
4983 APIRET rc;
4984
Guido van Rossumd48f2521997-12-05 22:19:34 +00004985 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
4986 if (rc != NO_ERROR)
4987 return os2_error(rc);
4988 } else {
4989#endif
4990
Fred Drake762e2061999-08-26 17:23:54 +00004991 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00004992 len = strlen(s1) + strlen(s2) + 2;
4993 /* len includes space for a trailing \0; the size arg to
4994 PyString_FromStringAndSize does not count that */
4995 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00004996 if (newstr == NULL)
4997 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004998 newenv = PyString_AS_STRING(newstr);
4999 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5000 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005001 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005002 posix_error();
5003 return NULL;
5004 }
Fred Drake762e2061999-08-26 17:23:54 +00005005 /* Install the first arg and newstr in posix_putenv_garbage;
5006 * this will cause previous value to be collected. This has to
5007 * happen after the real putenv() call because the old value
5008 * was still accessible until then. */
5009 if (PyDict_SetItem(posix_putenv_garbage,
5010 PyTuple_GET_ITEM(args, 0), newstr)) {
5011 /* really not much we can do; just leak */
5012 PyErr_Clear();
5013 }
5014 else {
5015 Py_DECREF(newstr);
5016 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005017
5018#if defined(PYOS_OS2)
5019 }
5020#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005021 Py_INCREF(Py_None);
5022 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005023}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005024#endif /* putenv */
5025
Guido van Rossumc524d952001-10-19 01:31:59 +00005026#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005027PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005028"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005029Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005030
5031static PyObject *
5032posix_unsetenv(PyObject *self, PyObject *args)
5033{
5034 char *s1;
5035
5036 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5037 return NULL;
5038
5039 unsetenv(s1);
5040
5041 /* Remove the key from posix_putenv_garbage;
5042 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005043 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005044 * old value was still accessible until then.
5045 */
5046 if (PyDict_DelItem(posix_putenv_garbage,
5047 PyTuple_GET_ITEM(args, 0))) {
5048 /* really not much we can do; just leak */
5049 PyErr_Clear();
5050 }
5051
5052 Py_INCREF(Py_None);
5053 return Py_None;
5054}
5055#endif /* unsetenv */
5056
Guido van Rossumb6a47161997-09-15 22:54:34 +00005057#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005058PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005059"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005060Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005061
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005062static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005063posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005064{
5065 int code;
5066 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005067 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005068 return NULL;
5069 message = strerror(code);
5070 if (message == NULL) {
5071 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005072 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005073 return NULL;
5074 }
5075 return PyString_FromString(message);
5076}
5077#endif /* strerror */
5078
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005079
Guido van Rossumc9641791998-08-04 15:26:23 +00005080#ifdef HAVE_SYS_WAIT_H
5081
Fred Drake106c1a02002-04-23 15:58:02 +00005082#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005083PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005084"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005085Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005086
5087static PyObject *
5088posix_WCOREDUMP(PyObject *self, PyObject *args)
5089{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005090 WAIT_TYPE status;
5091 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005092
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005093 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005094 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005095
5096 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005097}
5098#endif /* WCOREDUMP */
5099
5100#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005101PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005102"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005103Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005104job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005105
5106static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005107posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005108{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005109 WAIT_TYPE status;
5110 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005111
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005112 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005113 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005114
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005115 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005116}
5117#endif /* WIFCONTINUED */
5118
Guido van Rossumc9641791998-08-04 15:26:23 +00005119#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005120PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005121"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005122Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005123
5124static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005125posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005126{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005127 WAIT_TYPE status;
5128 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005130 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005131 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005132
Fred Drake106c1a02002-04-23 15:58:02 +00005133 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005134}
5135#endif /* WIFSTOPPED */
5136
5137#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005138PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005139"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005140Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005141
5142static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005143posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005144{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005145 WAIT_TYPE status;
5146 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005147
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005148 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005149 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005150
Fred Drake106c1a02002-04-23 15:58:02 +00005151 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005152}
5153#endif /* WIFSIGNALED */
5154
5155#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005156PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005157"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005158Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005159system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005160
5161static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005162posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005163{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005164 WAIT_TYPE status;
5165 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005166
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005167 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005168 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005169
Fred Drake106c1a02002-04-23 15:58:02 +00005170 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005171}
5172#endif /* WIFEXITED */
5173
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005174#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005175PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005176"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005177Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005178
5179static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005180posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005181{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005182 WAIT_TYPE status;
5183 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005184
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005185 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005186 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005187
Guido van Rossumc9641791998-08-04 15:26:23 +00005188 return Py_BuildValue("i", WEXITSTATUS(status));
5189}
5190#endif /* WEXITSTATUS */
5191
5192#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005193PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005194"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005195Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005196value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005197
5198static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005199posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005200{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005201 WAIT_TYPE status;
5202 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005203
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005204 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005205 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005206
Guido van Rossumc9641791998-08-04 15:26:23 +00005207 return Py_BuildValue("i", WTERMSIG(status));
5208}
5209#endif /* WTERMSIG */
5210
5211#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005213"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005214Return the signal that stopped the process that provided\n\
5215the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005216
5217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005218posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005219{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005220 WAIT_TYPE status;
5221 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005222
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005223 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005224 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005225
Guido van Rossumc9641791998-08-04 15:26:23 +00005226 return Py_BuildValue("i", WSTOPSIG(status));
5227}
5228#endif /* WSTOPSIG */
5229
5230#endif /* HAVE_SYS_WAIT_H */
5231
5232
Thomas Wouters477c8d52006-05-27 19:21:47 +00005233#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005234#ifdef _SCO_DS
5235/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5236 needed definitions in sys/statvfs.h */
5237#define _SVID3
5238#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005239#include <sys/statvfs.h>
5240
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005241static PyObject*
5242_pystatvfs_fromstructstatvfs(struct statvfs st) {
5243 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5244 if (v == NULL)
5245 return NULL;
5246
5247#if !defined(HAVE_LARGEFILE_SUPPORT)
5248 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5249 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5250 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5251 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5252 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5253 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5254 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5255 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5256 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5257 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5258#else
5259 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5260 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005261 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005262 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005263 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005264 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005265 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005266 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005267 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005268 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005269 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005270 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005271 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005272 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005273 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5274 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5275#endif
5276
5277 return v;
5278}
5279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005280PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005281"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005282Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005283
5284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005285posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005286{
5287 int fd, res;
5288 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005289
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005290 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005291 return NULL;
5292 Py_BEGIN_ALLOW_THREADS
5293 res = fstatvfs(fd, &st);
5294 Py_END_ALLOW_THREADS
5295 if (res != 0)
5296 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005297
5298 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005299}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005300#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005301
5302
Thomas Wouters477c8d52006-05-27 19:21:47 +00005303#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005304#include <sys/statvfs.h>
5305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005306PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005307"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005308Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005309
5310static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005311posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005312{
5313 char *path;
5314 int res;
5315 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005316 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005317 return NULL;
5318 Py_BEGIN_ALLOW_THREADS
5319 res = statvfs(path, &st);
5320 Py_END_ALLOW_THREADS
5321 if (res != 0)
5322 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005323
5324 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005325}
5326#endif /* HAVE_STATVFS */
5327
5328
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005329#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005330PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005331"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005332Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005333The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005334or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005335
5336static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005337posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005338{
5339 PyObject *result = NULL;
5340 char *dir = NULL;
5341 char *pfx = NULL;
5342 char *name;
5343
5344 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5345 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005346
Skip Montanaro46fc3372007-08-12 11:44:53 +00005347 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5348 "tempnam is a potential security risk to your program",
5349 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005350 return NULL;
5351
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005352#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005353 name = _tempnam(dir, pfx);
5354#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005355 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005356#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005357 if (name == NULL)
5358 return PyErr_NoMemory();
5359 result = PyString_FromString(name);
5360 free(name);
5361 return result;
5362}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005363#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005364
5365
5366#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005367PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005368"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005369Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005370
5371static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005372posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005373{
5374 FILE *fp;
5375
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005376 fp = tmpfile();
5377 if (fp == NULL)
5378 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005379 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005380}
5381#endif
5382
5383
5384#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005385PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005386"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005387Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005388
5389static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005390posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005391{
5392 char buffer[L_tmpnam];
5393 char *name;
5394
Skip Montanaro46fc3372007-08-12 11:44:53 +00005395 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5396 "tmpnam is a potential security risk to your program",
5397 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005398 return NULL;
5399
Greg Wardb48bc172000-03-01 21:51:56 +00005400#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005401 name = tmpnam_r(buffer);
5402#else
5403 name = tmpnam(buffer);
5404#endif
5405 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005406 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005407#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005408 "unexpected NULL from tmpnam_r"
5409#else
5410 "unexpected NULL from tmpnam"
5411#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005412 );
5413 PyErr_SetObject(PyExc_OSError, err);
5414 Py_XDECREF(err);
5415 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005416 }
5417 return PyString_FromString(buffer);
5418}
5419#endif
5420
5421
Fred Drakec9680921999-12-13 16:37:25 +00005422/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5423 * It maps strings representing configuration variable names to
5424 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005425 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005426 * rarely-used constants. There are three separate tables that use
5427 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005428 *
5429 * This code is always included, even if none of the interfaces that
5430 * need it are included. The #if hackery needed to avoid it would be
5431 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005432 */
5433struct constdef {
5434 char *name;
5435 long value;
5436};
5437
Fred Drake12c6e2d1999-12-14 21:25:03 +00005438static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005439conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5440 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005441{
5442 if (PyInt_Check(arg)) {
5443 *valuep = PyInt_AS_LONG(arg);
5444 return 1;
5445 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005446 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005447 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005448 size_t lo = 0;
5449 size_t mid;
5450 size_t hi = tablesize;
5451 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005452 const char *confname;
5453 Py_ssize_t namelen;
5454 if (PyObject_AsCharBuffer(arg, &confname, &namelen) < 0) {
5455 PyErr_SetString(PyExc_TypeError,
5456 "configuration names must be strings or integers");
5457 return 0;
5458 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005459 while (lo < hi) {
5460 mid = (lo + hi) / 2;
5461 cmp = strcmp(confname, table[mid].name);
5462 if (cmp < 0)
5463 hi = mid;
5464 else if (cmp > 0)
5465 lo = mid + 1;
5466 else {
5467 *valuep = table[mid].value;
5468 return 1;
5469 }
5470 }
5471 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005472 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005473 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005474}
5475
5476
5477#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5478static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005479#ifdef _PC_ABI_AIO_XFER_MAX
5480 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5481#endif
5482#ifdef _PC_ABI_ASYNC_IO
5483 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5484#endif
Fred Drakec9680921999-12-13 16:37:25 +00005485#ifdef _PC_ASYNC_IO
5486 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5487#endif
5488#ifdef _PC_CHOWN_RESTRICTED
5489 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5490#endif
5491#ifdef _PC_FILESIZEBITS
5492 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5493#endif
5494#ifdef _PC_LAST
5495 {"PC_LAST", _PC_LAST},
5496#endif
5497#ifdef _PC_LINK_MAX
5498 {"PC_LINK_MAX", _PC_LINK_MAX},
5499#endif
5500#ifdef _PC_MAX_CANON
5501 {"PC_MAX_CANON", _PC_MAX_CANON},
5502#endif
5503#ifdef _PC_MAX_INPUT
5504 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5505#endif
5506#ifdef _PC_NAME_MAX
5507 {"PC_NAME_MAX", _PC_NAME_MAX},
5508#endif
5509#ifdef _PC_NO_TRUNC
5510 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5511#endif
5512#ifdef _PC_PATH_MAX
5513 {"PC_PATH_MAX", _PC_PATH_MAX},
5514#endif
5515#ifdef _PC_PIPE_BUF
5516 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5517#endif
5518#ifdef _PC_PRIO_IO
5519 {"PC_PRIO_IO", _PC_PRIO_IO},
5520#endif
5521#ifdef _PC_SOCK_MAXBUF
5522 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5523#endif
5524#ifdef _PC_SYNC_IO
5525 {"PC_SYNC_IO", _PC_SYNC_IO},
5526#endif
5527#ifdef _PC_VDISABLE
5528 {"PC_VDISABLE", _PC_VDISABLE},
5529#endif
5530};
5531
Fred Drakec9680921999-12-13 16:37:25 +00005532static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005533conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005534{
5535 return conv_confname(arg, valuep, posix_constants_pathconf,
5536 sizeof(posix_constants_pathconf)
5537 / sizeof(struct constdef));
5538}
5539#endif
5540
5541#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005542PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005543"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005544Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005545If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005546
5547static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005548posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005549{
5550 PyObject *result = NULL;
5551 int name, fd;
5552
Fred Drake12c6e2d1999-12-14 21:25:03 +00005553 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5554 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005555 long limit;
5556
5557 errno = 0;
5558 limit = fpathconf(fd, name);
5559 if (limit == -1 && errno != 0)
5560 posix_error();
5561 else
5562 result = PyInt_FromLong(limit);
5563 }
5564 return result;
5565}
5566#endif
5567
5568
5569#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005570PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005571"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005572Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005573If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005574
5575static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005576posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005577{
5578 PyObject *result = NULL;
5579 int name;
5580 char *path;
5581
5582 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5583 conv_path_confname, &name)) {
5584 long limit;
5585
5586 errno = 0;
5587 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005588 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005589 if (errno == EINVAL)
5590 /* could be a path or name problem */
5591 posix_error();
5592 else
5593 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005594 }
Fred Drakec9680921999-12-13 16:37:25 +00005595 else
5596 result = PyInt_FromLong(limit);
5597 }
5598 return result;
5599}
5600#endif
5601
5602#ifdef HAVE_CONFSTR
5603static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005604#ifdef _CS_ARCHITECTURE
5605 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5606#endif
5607#ifdef _CS_HOSTNAME
5608 {"CS_HOSTNAME", _CS_HOSTNAME},
5609#endif
5610#ifdef _CS_HW_PROVIDER
5611 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5612#endif
5613#ifdef _CS_HW_SERIAL
5614 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5615#endif
5616#ifdef _CS_INITTAB_NAME
5617 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5618#endif
Fred Drakec9680921999-12-13 16:37:25 +00005619#ifdef _CS_LFS64_CFLAGS
5620 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5621#endif
5622#ifdef _CS_LFS64_LDFLAGS
5623 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5624#endif
5625#ifdef _CS_LFS64_LIBS
5626 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5627#endif
5628#ifdef _CS_LFS64_LINTFLAGS
5629 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5630#endif
5631#ifdef _CS_LFS_CFLAGS
5632 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5633#endif
5634#ifdef _CS_LFS_LDFLAGS
5635 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5636#endif
5637#ifdef _CS_LFS_LIBS
5638 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5639#endif
5640#ifdef _CS_LFS_LINTFLAGS
5641 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5642#endif
Fred Draked86ed291999-12-15 15:34:33 +00005643#ifdef _CS_MACHINE
5644 {"CS_MACHINE", _CS_MACHINE},
5645#endif
Fred Drakec9680921999-12-13 16:37:25 +00005646#ifdef _CS_PATH
5647 {"CS_PATH", _CS_PATH},
5648#endif
Fred Draked86ed291999-12-15 15:34:33 +00005649#ifdef _CS_RELEASE
5650 {"CS_RELEASE", _CS_RELEASE},
5651#endif
5652#ifdef _CS_SRPC_DOMAIN
5653 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5654#endif
5655#ifdef _CS_SYSNAME
5656 {"CS_SYSNAME", _CS_SYSNAME},
5657#endif
5658#ifdef _CS_VERSION
5659 {"CS_VERSION", _CS_VERSION},
5660#endif
Fred Drakec9680921999-12-13 16:37:25 +00005661#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5662 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5663#endif
5664#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5665 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5666#endif
5667#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5668 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5669#endif
5670#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5671 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5672#endif
5673#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5674 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5675#endif
5676#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5677 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5678#endif
5679#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5680 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5681#endif
5682#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5683 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5684#endif
5685#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5686 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5687#endif
5688#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5689 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5690#endif
5691#ifdef _CS_XBS5_LP64_OFF64_LIBS
5692 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5693#endif
5694#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5695 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5696#endif
5697#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5698 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5699#endif
5700#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5701 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5702#endif
5703#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5704 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5705#endif
5706#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5707 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5708#endif
Fred Draked86ed291999-12-15 15:34:33 +00005709#ifdef _MIPS_CS_AVAIL_PROCESSORS
5710 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5711#endif
5712#ifdef _MIPS_CS_BASE
5713 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5714#endif
5715#ifdef _MIPS_CS_HOSTID
5716 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5717#endif
5718#ifdef _MIPS_CS_HW_NAME
5719 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5720#endif
5721#ifdef _MIPS_CS_NUM_PROCESSORS
5722 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5723#endif
5724#ifdef _MIPS_CS_OSREL_MAJ
5725 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5726#endif
5727#ifdef _MIPS_CS_OSREL_MIN
5728 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5729#endif
5730#ifdef _MIPS_CS_OSREL_PATCH
5731 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5732#endif
5733#ifdef _MIPS_CS_OS_NAME
5734 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5735#endif
5736#ifdef _MIPS_CS_OS_PROVIDER
5737 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5738#endif
5739#ifdef _MIPS_CS_PROCESSORS
5740 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5741#endif
5742#ifdef _MIPS_CS_SERIAL
5743 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5744#endif
5745#ifdef _MIPS_CS_VENDOR
5746 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5747#endif
Fred Drakec9680921999-12-13 16:37:25 +00005748};
5749
5750static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005751conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005752{
5753 return conv_confname(arg, valuep, posix_constants_confstr,
5754 sizeof(posix_constants_confstr)
5755 / sizeof(struct constdef));
5756}
5757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005758PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005759"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005760Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005761
5762static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005763posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005764{
5765 PyObject *result = NULL;
5766 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005767 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005768
5769 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005770 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005771
Fred Drakec9680921999-12-13 16:37:25 +00005772 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005773 len = confstr(name, buffer, sizeof(buffer));
5774 if (len == 0) {
5775 if (errno) {
5776 posix_error();
5777 }
5778 else {
5779 result = Py_None;
5780 Py_INCREF(Py_None);
5781 }
Fred Drakec9680921999-12-13 16:37:25 +00005782 }
5783 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005784 if ((unsigned int)len >= sizeof(buffer)) {
5785 result = PyString_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005786 if (result != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005787 confstr(name, PyString_AS_STRING(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005788 }
5789 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005790 result = PyString_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005791 }
5792 }
5793 return result;
5794}
5795#endif
5796
5797
5798#ifdef HAVE_SYSCONF
5799static struct constdef posix_constants_sysconf[] = {
5800#ifdef _SC_2_CHAR_TERM
5801 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5802#endif
5803#ifdef _SC_2_C_BIND
5804 {"SC_2_C_BIND", _SC_2_C_BIND},
5805#endif
5806#ifdef _SC_2_C_DEV
5807 {"SC_2_C_DEV", _SC_2_C_DEV},
5808#endif
5809#ifdef _SC_2_C_VERSION
5810 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5811#endif
5812#ifdef _SC_2_FORT_DEV
5813 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5814#endif
5815#ifdef _SC_2_FORT_RUN
5816 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5817#endif
5818#ifdef _SC_2_LOCALEDEF
5819 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5820#endif
5821#ifdef _SC_2_SW_DEV
5822 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5823#endif
5824#ifdef _SC_2_UPE
5825 {"SC_2_UPE", _SC_2_UPE},
5826#endif
5827#ifdef _SC_2_VERSION
5828 {"SC_2_VERSION", _SC_2_VERSION},
5829#endif
Fred Draked86ed291999-12-15 15:34:33 +00005830#ifdef _SC_ABI_ASYNCHRONOUS_IO
5831 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5832#endif
5833#ifdef _SC_ACL
5834 {"SC_ACL", _SC_ACL},
5835#endif
Fred Drakec9680921999-12-13 16:37:25 +00005836#ifdef _SC_AIO_LISTIO_MAX
5837 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5838#endif
Fred Drakec9680921999-12-13 16:37:25 +00005839#ifdef _SC_AIO_MAX
5840 {"SC_AIO_MAX", _SC_AIO_MAX},
5841#endif
5842#ifdef _SC_AIO_PRIO_DELTA_MAX
5843 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5844#endif
5845#ifdef _SC_ARG_MAX
5846 {"SC_ARG_MAX", _SC_ARG_MAX},
5847#endif
5848#ifdef _SC_ASYNCHRONOUS_IO
5849 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5850#endif
5851#ifdef _SC_ATEXIT_MAX
5852 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5853#endif
Fred Draked86ed291999-12-15 15:34:33 +00005854#ifdef _SC_AUDIT
5855 {"SC_AUDIT", _SC_AUDIT},
5856#endif
Fred Drakec9680921999-12-13 16:37:25 +00005857#ifdef _SC_AVPHYS_PAGES
5858 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5859#endif
5860#ifdef _SC_BC_BASE_MAX
5861 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5862#endif
5863#ifdef _SC_BC_DIM_MAX
5864 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5865#endif
5866#ifdef _SC_BC_SCALE_MAX
5867 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5868#endif
5869#ifdef _SC_BC_STRING_MAX
5870 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5871#endif
Fred Draked86ed291999-12-15 15:34:33 +00005872#ifdef _SC_CAP
5873 {"SC_CAP", _SC_CAP},
5874#endif
Fred Drakec9680921999-12-13 16:37:25 +00005875#ifdef _SC_CHARCLASS_NAME_MAX
5876 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5877#endif
5878#ifdef _SC_CHAR_BIT
5879 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5880#endif
5881#ifdef _SC_CHAR_MAX
5882 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5883#endif
5884#ifdef _SC_CHAR_MIN
5885 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5886#endif
5887#ifdef _SC_CHILD_MAX
5888 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5889#endif
5890#ifdef _SC_CLK_TCK
5891 {"SC_CLK_TCK", _SC_CLK_TCK},
5892#endif
5893#ifdef _SC_COHER_BLKSZ
5894 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5895#endif
5896#ifdef _SC_COLL_WEIGHTS_MAX
5897 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5898#endif
5899#ifdef _SC_DCACHE_ASSOC
5900 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5901#endif
5902#ifdef _SC_DCACHE_BLKSZ
5903 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5904#endif
5905#ifdef _SC_DCACHE_LINESZ
5906 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
5907#endif
5908#ifdef _SC_DCACHE_SZ
5909 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
5910#endif
5911#ifdef _SC_DCACHE_TBLKSZ
5912 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
5913#endif
5914#ifdef _SC_DELAYTIMER_MAX
5915 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
5916#endif
5917#ifdef _SC_EQUIV_CLASS_MAX
5918 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
5919#endif
5920#ifdef _SC_EXPR_NEST_MAX
5921 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
5922#endif
5923#ifdef _SC_FSYNC
5924 {"SC_FSYNC", _SC_FSYNC},
5925#endif
5926#ifdef _SC_GETGR_R_SIZE_MAX
5927 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
5928#endif
5929#ifdef _SC_GETPW_R_SIZE_MAX
5930 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
5931#endif
5932#ifdef _SC_ICACHE_ASSOC
5933 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
5934#endif
5935#ifdef _SC_ICACHE_BLKSZ
5936 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
5937#endif
5938#ifdef _SC_ICACHE_LINESZ
5939 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
5940#endif
5941#ifdef _SC_ICACHE_SZ
5942 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
5943#endif
Fred Draked86ed291999-12-15 15:34:33 +00005944#ifdef _SC_INF
5945 {"SC_INF", _SC_INF},
5946#endif
Fred Drakec9680921999-12-13 16:37:25 +00005947#ifdef _SC_INT_MAX
5948 {"SC_INT_MAX", _SC_INT_MAX},
5949#endif
5950#ifdef _SC_INT_MIN
5951 {"SC_INT_MIN", _SC_INT_MIN},
5952#endif
5953#ifdef _SC_IOV_MAX
5954 {"SC_IOV_MAX", _SC_IOV_MAX},
5955#endif
Fred Draked86ed291999-12-15 15:34:33 +00005956#ifdef _SC_IP_SECOPTS
5957 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
5958#endif
Fred Drakec9680921999-12-13 16:37:25 +00005959#ifdef _SC_JOB_CONTROL
5960 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
5961#endif
Fred Draked86ed291999-12-15 15:34:33 +00005962#ifdef _SC_KERN_POINTERS
5963 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
5964#endif
5965#ifdef _SC_KERN_SIM
5966 {"SC_KERN_SIM", _SC_KERN_SIM},
5967#endif
Fred Drakec9680921999-12-13 16:37:25 +00005968#ifdef _SC_LINE_MAX
5969 {"SC_LINE_MAX", _SC_LINE_MAX},
5970#endif
5971#ifdef _SC_LOGIN_NAME_MAX
5972 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
5973#endif
5974#ifdef _SC_LOGNAME_MAX
5975 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
5976#endif
5977#ifdef _SC_LONG_BIT
5978 {"SC_LONG_BIT", _SC_LONG_BIT},
5979#endif
Fred Draked86ed291999-12-15 15:34:33 +00005980#ifdef _SC_MAC
5981 {"SC_MAC", _SC_MAC},
5982#endif
Fred Drakec9680921999-12-13 16:37:25 +00005983#ifdef _SC_MAPPED_FILES
5984 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
5985#endif
5986#ifdef _SC_MAXPID
5987 {"SC_MAXPID", _SC_MAXPID},
5988#endif
5989#ifdef _SC_MB_LEN_MAX
5990 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
5991#endif
5992#ifdef _SC_MEMLOCK
5993 {"SC_MEMLOCK", _SC_MEMLOCK},
5994#endif
5995#ifdef _SC_MEMLOCK_RANGE
5996 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
5997#endif
5998#ifdef _SC_MEMORY_PROTECTION
5999 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6000#endif
6001#ifdef _SC_MESSAGE_PASSING
6002 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6003#endif
Fred Draked86ed291999-12-15 15:34:33 +00006004#ifdef _SC_MMAP_FIXED_ALIGNMENT
6005 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6006#endif
Fred Drakec9680921999-12-13 16:37:25 +00006007#ifdef _SC_MQ_OPEN_MAX
6008 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6009#endif
6010#ifdef _SC_MQ_PRIO_MAX
6011 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6012#endif
Fred Draked86ed291999-12-15 15:34:33 +00006013#ifdef _SC_NACLS_MAX
6014 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6015#endif
Fred Drakec9680921999-12-13 16:37:25 +00006016#ifdef _SC_NGROUPS_MAX
6017 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6018#endif
6019#ifdef _SC_NL_ARGMAX
6020 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6021#endif
6022#ifdef _SC_NL_LANGMAX
6023 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6024#endif
6025#ifdef _SC_NL_MSGMAX
6026 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6027#endif
6028#ifdef _SC_NL_NMAX
6029 {"SC_NL_NMAX", _SC_NL_NMAX},
6030#endif
6031#ifdef _SC_NL_SETMAX
6032 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6033#endif
6034#ifdef _SC_NL_TEXTMAX
6035 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6036#endif
6037#ifdef _SC_NPROCESSORS_CONF
6038 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6039#endif
6040#ifdef _SC_NPROCESSORS_ONLN
6041 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6042#endif
Fred Draked86ed291999-12-15 15:34:33 +00006043#ifdef _SC_NPROC_CONF
6044 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6045#endif
6046#ifdef _SC_NPROC_ONLN
6047 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6048#endif
Fred Drakec9680921999-12-13 16:37:25 +00006049#ifdef _SC_NZERO
6050 {"SC_NZERO", _SC_NZERO},
6051#endif
6052#ifdef _SC_OPEN_MAX
6053 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6054#endif
6055#ifdef _SC_PAGESIZE
6056 {"SC_PAGESIZE", _SC_PAGESIZE},
6057#endif
6058#ifdef _SC_PAGE_SIZE
6059 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6060#endif
6061#ifdef _SC_PASS_MAX
6062 {"SC_PASS_MAX", _SC_PASS_MAX},
6063#endif
6064#ifdef _SC_PHYS_PAGES
6065 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6066#endif
6067#ifdef _SC_PII
6068 {"SC_PII", _SC_PII},
6069#endif
6070#ifdef _SC_PII_INTERNET
6071 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6072#endif
6073#ifdef _SC_PII_INTERNET_DGRAM
6074 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6075#endif
6076#ifdef _SC_PII_INTERNET_STREAM
6077 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6078#endif
6079#ifdef _SC_PII_OSI
6080 {"SC_PII_OSI", _SC_PII_OSI},
6081#endif
6082#ifdef _SC_PII_OSI_CLTS
6083 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6084#endif
6085#ifdef _SC_PII_OSI_COTS
6086 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6087#endif
6088#ifdef _SC_PII_OSI_M
6089 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6090#endif
6091#ifdef _SC_PII_SOCKET
6092 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6093#endif
6094#ifdef _SC_PII_XTI
6095 {"SC_PII_XTI", _SC_PII_XTI},
6096#endif
6097#ifdef _SC_POLL
6098 {"SC_POLL", _SC_POLL},
6099#endif
6100#ifdef _SC_PRIORITIZED_IO
6101 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6102#endif
6103#ifdef _SC_PRIORITY_SCHEDULING
6104 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6105#endif
6106#ifdef _SC_REALTIME_SIGNALS
6107 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6108#endif
6109#ifdef _SC_RE_DUP_MAX
6110 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6111#endif
6112#ifdef _SC_RTSIG_MAX
6113 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6114#endif
6115#ifdef _SC_SAVED_IDS
6116 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6117#endif
6118#ifdef _SC_SCHAR_MAX
6119 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6120#endif
6121#ifdef _SC_SCHAR_MIN
6122 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6123#endif
6124#ifdef _SC_SELECT
6125 {"SC_SELECT", _SC_SELECT},
6126#endif
6127#ifdef _SC_SEMAPHORES
6128 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6129#endif
6130#ifdef _SC_SEM_NSEMS_MAX
6131 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6132#endif
6133#ifdef _SC_SEM_VALUE_MAX
6134 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6135#endif
6136#ifdef _SC_SHARED_MEMORY_OBJECTS
6137 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6138#endif
6139#ifdef _SC_SHRT_MAX
6140 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6141#endif
6142#ifdef _SC_SHRT_MIN
6143 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6144#endif
6145#ifdef _SC_SIGQUEUE_MAX
6146 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6147#endif
6148#ifdef _SC_SIGRT_MAX
6149 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6150#endif
6151#ifdef _SC_SIGRT_MIN
6152 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6153#endif
Fred Draked86ed291999-12-15 15:34:33 +00006154#ifdef _SC_SOFTPOWER
6155 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6156#endif
Fred Drakec9680921999-12-13 16:37:25 +00006157#ifdef _SC_SPLIT_CACHE
6158 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6159#endif
6160#ifdef _SC_SSIZE_MAX
6161 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6162#endif
6163#ifdef _SC_STACK_PROT
6164 {"SC_STACK_PROT", _SC_STACK_PROT},
6165#endif
6166#ifdef _SC_STREAM_MAX
6167 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6168#endif
6169#ifdef _SC_SYNCHRONIZED_IO
6170 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6171#endif
6172#ifdef _SC_THREADS
6173 {"SC_THREADS", _SC_THREADS},
6174#endif
6175#ifdef _SC_THREAD_ATTR_STACKADDR
6176 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6177#endif
6178#ifdef _SC_THREAD_ATTR_STACKSIZE
6179 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6180#endif
6181#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6182 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6183#endif
6184#ifdef _SC_THREAD_KEYS_MAX
6185 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6186#endif
6187#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6188 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6189#endif
6190#ifdef _SC_THREAD_PRIO_INHERIT
6191 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6192#endif
6193#ifdef _SC_THREAD_PRIO_PROTECT
6194 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6195#endif
6196#ifdef _SC_THREAD_PROCESS_SHARED
6197 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6198#endif
6199#ifdef _SC_THREAD_SAFE_FUNCTIONS
6200 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6201#endif
6202#ifdef _SC_THREAD_STACK_MIN
6203 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6204#endif
6205#ifdef _SC_THREAD_THREADS_MAX
6206 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6207#endif
6208#ifdef _SC_TIMERS
6209 {"SC_TIMERS", _SC_TIMERS},
6210#endif
6211#ifdef _SC_TIMER_MAX
6212 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6213#endif
6214#ifdef _SC_TTY_NAME_MAX
6215 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6216#endif
6217#ifdef _SC_TZNAME_MAX
6218 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6219#endif
6220#ifdef _SC_T_IOV_MAX
6221 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6222#endif
6223#ifdef _SC_UCHAR_MAX
6224 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6225#endif
6226#ifdef _SC_UINT_MAX
6227 {"SC_UINT_MAX", _SC_UINT_MAX},
6228#endif
6229#ifdef _SC_UIO_MAXIOV
6230 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6231#endif
6232#ifdef _SC_ULONG_MAX
6233 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6234#endif
6235#ifdef _SC_USHRT_MAX
6236 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6237#endif
6238#ifdef _SC_VERSION
6239 {"SC_VERSION", _SC_VERSION},
6240#endif
6241#ifdef _SC_WORD_BIT
6242 {"SC_WORD_BIT", _SC_WORD_BIT},
6243#endif
6244#ifdef _SC_XBS5_ILP32_OFF32
6245 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6246#endif
6247#ifdef _SC_XBS5_ILP32_OFFBIG
6248 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6249#endif
6250#ifdef _SC_XBS5_LP64_OFF64
6251 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6252#endif
6253#ifdef _SC_XBS5_LPBIG_OFFBIG
6254 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6255#endif
6256#ifdef _SC_XOPEN_CRYPT
6257 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6258#endif
6259#ifdef _SC_XOPEN_ENH_I18N
6260 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6261#endif
6262#ifdef _SC_XOPEN_LEGACY
6263 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6264#endif
6265#ifdef _SC_XOPEN_REALTIME
6266 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6267#endif
6268#ifdef _SC_XOPEN_REALTIME_THREADS
6269 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6270#endif
6271#ifdef _SC_XOPEN_SHM
6272 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6273#endif
6274#ifdef _SC_XOPEN_UNIX
6275 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6276#endif
6277#ifdef _SC_XOPEN_VERSION
6278 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6279#endif
6280#ifdef _SC_XOPEN_XCU_VERSION
6281 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6282#endif
6283#ifdef _SC_XOPEN_XPG2
6284 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6285#endif
6286#ifdef _SC_XOPEN_XPG3
6287 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6288#endif
6289#ifdef _SC_XOPEN_XPG4
6290 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6291#endif
6292};
6293
6294static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006295conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006296{
6297 return conv_confname(arg, valuep, posix_constants_sysconf,
6298 sizeof(posix_constants_sysconf)
6299 / sizeof(struct constdef));
6300}
6301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006302PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006303"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006304Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006305
6306static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006307posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006308{
6309 PyObject *result = NULL;
6310 int name;
6311
6312 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6313 int value;
6314
6315 errno = 0;
6316 value = sysconf(name);
6317 if (value == -1 && errno != 0)
6318 posix_error();
6319 else
6320 result = PyInt_FromLong(value);
6321 }
6322 return result;
6323}
6324#endif
6325
6326
Fred Drakebec628d1999-12-15 18:31:10 +00006327/* This code is used to ensure that the tables of configuration value names
6328 * are in sorted order as required by conv_confname(), and also to build the
6329 * the exported dictionaries that are used to publish information about the
6330 * names available on the host platform.
6331 *
6332 * Sorting the table at runtime ensures that the table is properly ordered
6333 * when used, even for platforms we're not able to test on. It also makes
6334 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006335 */
Fred Drakebec628d1999-12-15 18:31:10 +00006336
6337static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006338cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006339{
6340 const struct constdef *c1 =
6341 (const struct constdef *) v1;
6342 const struct constdef *c2 =
6343 (const struct constdef *) v2;
6344
6345 return strcmp(c1->name, c2->name);
6346}
6347
6348static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006349setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006350 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006351{
Fred Drakebec628d1999-12-15 18:31:10 +00006352 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006353 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006354
6355 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6356 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006357 if (d == NULL)
6358 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006359
Barry Warsaw3155db32000-04-13 15:20:40 +00006360 for (i=0; i < tablesize; ++i) {
6361 PyObject *o = PyInt_FromLong(table[i].value);
6362 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6363 Py_XDECREF(o);
6364 Py_DECREF(d);
6365 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006366 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006367 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006368 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006369 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006370}
6371
Fred Drakebec628d1999-12-15 18:31:10 +00006372/* Return -1 on failure, 0 on success. */
6373static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006374setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006375{
6376#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006377 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006378 sizeof(posix_constants_pathconf)
6379 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006380 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006381 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006382#endif
6383#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006384 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006385 sizeof(posix_constants_confstr)
6386 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006387 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006388 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006389#endif
6390#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006391 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006392 sizeof(posix_constants_sysconf)
6393 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006394 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006395 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006396#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006397 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006398}
Fred Draked86ed291999-12-15 15:34:33 +00006399
6400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006401PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006402"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006403Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006404in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006405
6406static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006407posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006408{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006409 abort();
6410 /*NOTREACHED*/
6411 Py_FatalError("abort() called from Python code didn't abort!");
6412 return NULL;
6413}
Fred Drakebec628d1999-12-15 18:31:10 +00006414
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006415#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006416PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006417"startfile(filepath [, operation]) - Start a file with its associated\n\
6418application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006419\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006420When \"operation\" is not specified or \"open\", this acts like\n\
6421double-clicking the file in Explorer, or giving the file name as an\n\
6422argument to the DOS \"start\" command: the file is opened with whatever\n\
6423application (if any) its extension is associated.\n\
6424When another \"operation\" is given, it specifies what should be done with\n\
6425the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006426\n\
6427startfile returns as soon as the associated application is launched.\n\
6428There is no option to wait for the application to close, and no way\n\
6429to retrieve the application's exit status.\n\
6430\n\
6431The filepath is relative to the current directory. If you want to use\n\
6432an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006433the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006434
6435static PyObject *
6436win32_startfile(PyObject *self, PyObject *args)
6437{
6438 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006439 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006440 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006441#ifdef Py_WIN_WIDE_FILENAMES
6442 if (unicode_file_names()) {
6443 PyObject *unipath, *woperation = NULL;
6444 if (!PyArg_ParseTuple(args, "U|s:startfile",
6445 &unipath, &operation)) {
6446 PyErr_Clear();
6447 goto normal;
6448 }
6449
6450
6451 if (operation) {
6452 woperation = PyUnicode_DecodeASCII(operation,
6453 strlen(operation), NULL);
6454 if (!woperation) {
6455 PyErr_Clear();
6456 operation = NULL;
6457 goto normal;
6458 }
6459 }
6460
6461 Py_BEGIN_ALLOW_THREADS
6462 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6463 PyUnicode_AS_UNICODE(unipath),
6464 NULL, NULL, SW_SHOWNORMAL);
6465 Py_END_ALLOW_THREADS
6466
6467 Py_XDECREF(woperation);
6468 if (rc <= (HINSTANCE)32) {
6469 PyObject *errval = win32_error_unicode("startfile",
6470 PyUnicode_AS_UNICODE(unipath));
6471 return errval;
6472 }
6473 Py_INCREF(Py_None);
6474 return Py_None;
6475 }
6476#endif
6477
6478normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006479 if (!PyArg_ParseTuple(args, "et|s:startfile",
6480 Py_FileSystemDefaultEncoding, &filepath,
6481 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006482 return NULL;
6483 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006484 rc = ShellExecute((HWND)0, operation, filepath,
6485 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006486 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006487 if (rc <= (HINSTANCE)32) {
6488 PyObject *errval = win32_error("startfile", filepath);
6489 PyMem_Free(filepath);
6490 return errval;
6491 }
6492 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006493 Py_INCREF(Py_None);
6494 return Py_None;
6495}
6496#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006497
Martin v. Löwis438b5342002-12-27 10:16:42 +00006498#ifdef HAVE_GETLOADAVG
6499PyDoc_STRVAR(posix_getloadavg__doc__,
6500"getloadavg() -> (float, float, float)\n\n\
6501Return the number of processes in the system run queue averaged over\n\
6502the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6503was unobtainable");
6504
6505static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006506posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006507{
6508 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006509 if (getloadavg(loadavg, 3)!=3) {
6510 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6511 return NULL;
6512 } else
6513 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6514}
6515#endif
6516
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006517#ifdef MS_WINDOWS
6518
6519PyDoc_STRVAR(win32_urandom__doc__,
6520"urandom(n) -> str\n\n\
6521Return a string of n random bytes suitable for cryptographic use.");
6522
6523typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6524 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6525 DWORD dwFlags );
6526typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6527 BYTE *pbBuffer );
6528
6529static CRYPTGENRANDOM pCryptGenRandom = NULL;
6530static HCRYPTPROV hCryptProv = 0;
6531
Tim Peters4ad82172004-08-30 17:02:04 +00006532static PyObject*
6533win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006534{
Tim Petersd3115382004-08-30 17:36:46 +00006535 int howMany;
6536 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006537
Tim Peters4ad82172004-08-30 17:02:04 +00006538 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006539 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006540 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006541 if (howMany < 0)
6542 return PyErr_Format(PyExc_ValueError,
6543 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006544
Tim Peters4ad82172004-08-30 17:02:04 +00006545 if (hCryptProv == 0) {
6546 HINSTANCE hAdvAPI32 = NULL;
6547 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006548
Tim Peters4ad82172004-08-30 17:02:04 +00006549 /* Obtain handle to the DLL containing CryptoAPI
6550 This should not fail */
6551 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6552 if(hAdvAPI32 == NULL)
6553 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006554
Tim Peters4ad82172004-08-30 17:02:04 +00006555 /* Obtain pointers to the CryptoAPI functions
6556 This will fail on some early versions of Win95 */
6557 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6558 hAdvAPI32,
6559 "CryptAcquireContextA");
6560 if (pCryptAcquireContext == NULL)
6561 return PyErr_Format(PyExc_NotImplementedError,
6562 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006563
Tim Peters4ad82172004-08-30 17:02:04 +00006564 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6565 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006566 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006567 return PyErr_Format(PyExc_NotImplementedError,
6568 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006569
Tim Peters4ad82172004-08-30 17:02:04 +00006570 /* Acquire context */
6571 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6572 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6573 return win32_error("CryptAcquireContext", NULL);
6574 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006575
Tim Peters4ad82172004-08-30 17:02:04 +00006576 /* Allocate bytes */
Tim Petersd3115382004-08-30 17:36:46 +00006577 result = PyString_FromStringAndSize(NULL, howMany);
6578 if (result != NULL) {
6579 /* Get random data */
6580 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6581 PyString_AS_STRING(result))) {
6582 Py_DECREF(result);
6583 return win32_error("CryptGenRandom", NULL);
6584 }
Tim Peters4ad82172004-08-30 17:02:04 +00006585 }
Tim Petersd3115382004-08-30 17:36:46 +00006586 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006587}
6588#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006589
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006590PyDoc_STRVAR(device_encoding__doc__,
6591"device_encoding(fd) -> str\n\n\
6592Return a string describing the encoding of the device\n\
6593if the output is a terminal; else return None.");
6594
6595static PyObject *
6596device_encoding(PyObject *self, PyObject *args)
6597{
6598 int fd;
6599 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6600 return NULL;
6601 if (!isatty(fd)) {
6602 Py_INCREF(Py_None);
6603 return Py_None;
6604 }
6605#if defined(MS_WINDOWS) || defined(MS_WIN64)
6606 if (fd == 0) {
6607 char buf[100];
6608 sprintf(buf, "cp%d", GetConsoleCP());
6609 return PyUnicode_FromString(buf);
6610 }
6611 if (fd == 1 || fd == 2) {
6612 char buf[100];
6613 sprintf(buf, "cp%d", GetConsoleOutputCP());
6614 return PyUnicode_FromString(buf);
6615 }
6616#elif defined(CODESET)
6617 {
6618 char *codeset = nl_langinfo(CODESET);
6619 if (codeset)
6620 return PyUnicode_FromString(codeset);
6621 }
6622#endif
6623 Py_INCREF(Py_None);
6624 return Py_None;
6625}
6626
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006627#ifdef __VMS
6628/* Use openssl random routine */
6629#include <openssl/rand.h>
6630PyDoc_STRVAR(vms_urandom__doc__,
6631"urandom(n) -> str\n\n\
6632Return a string of n random bytes suitable for cryptographic use.");
6633
6634static PyObject*
6635vms_urandom(PyObject *self, PyObject *args)
6636{
6637 int howMany;
6638 PyObject* result;
6639
6640 /* Read arguments */
6641 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6642 return NULL;
6643 if (howMany < 0)
6644 return PyErr_Format(PyExc_ValueError,
6645 "negative argument not allowed");
6646
6647 /* Allocate bytes */
6648 result = PyString_FromStringAndSize(NULL, howMany);
6649 if (result != NULL) {
6650 /* Get random data */
6651 if (RAND_pseudo_bytes((unsigned char*)
6652 PyString_AS_STRING(result),
6653 howMany) < 0) {
6654 Py_DECREF(result);
6655 return PyErr_Format(PyExc_ValueError,
6656 "RAND_pseudo_bytes");
6657 }
6658 }
6659 return result;
6660}
6661#endif
6662
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006663static PyMethodDef posix_methods[] = {
6664 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6665#ifdef HAVE_TTYNAME
6666 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6667#endif
6668 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006669#ifdef HAVE_CHFLAGS
6670 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6671#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006672 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006673#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006674 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006675#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006676#ifdef HAVE_LCHFLAGS
6677 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6678#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006679#ifdef HAVE_LCHOWN
6680 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6681#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006682#ifdef HAVE_CHROOT
6683 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6684#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006685#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006686 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006687#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006688#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006689 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006690 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006691#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006692#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006693 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006694#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006695 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6696 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6697 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006698#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006699 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006700#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006701#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006702 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006703#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006704 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6705 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6706 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006707 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006708#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006709 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006710#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006711#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006712 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006713#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006714 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006715#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006716 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006717#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006718 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6719 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6720 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006721#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006722 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006723#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006724 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006725#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006726 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6727 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006728#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006729#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006730 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6731 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006732#if defined(PYOS_OS2)
6733 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6734 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6735#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006736#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006737#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006738 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006739#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006740#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006741 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006742#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006743#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006744 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006745#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006746#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006747 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006748#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006749#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006750 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006751#endif /* HAVE_GETEGID */
6752#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006753 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006754#endif /* HAVE_GETEUID */
6755#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006756 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006757#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006758#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006759 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006760#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006761 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006762#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006763 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006764#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006765#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006766 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006767#endif /* HAVE_GETPPID */
6768#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006769 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006770#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006771#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006772 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006773#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006774#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006775 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006776#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006777#ifdef HAVE_KILLPG
6778 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6779#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006780#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006781 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006782#endif /* HAVE_PLOCK */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006783#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006784 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006785#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006786#ifdef HAVE_SETEUID
6787 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6788#endif /* HAVE_SETEUID */
6789#ifdef HAVE_SETEGID
6790 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6791#endif /* HAVE_SETEGID */
6792#ifdef HAVE_SETREUID
6793 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6794#endif /* HAVE_SETREUID */
6795#ifdef HAVE_SETREGID
6796 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6797#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006798#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006799 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006800#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006801#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006802 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006803#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006804#ifdef HAVE_GETPGID
6805 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6806#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006807#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006808 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006809#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006810#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006811 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006812#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006813#ifdef HAVE_WAIT3
6814 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6815#endif /* HAVE_WAIT3 */
6816#ifdef HAVE_WAIT4
6817 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6818#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006819#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006820 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006821#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006822#ifdef HAVE_GETSID
6823 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6824#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006825#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006826 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006827#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006828#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006829 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006830#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006831#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006832 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006833#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006834#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006835 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006836#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006837 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6838 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006839 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006840 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6841 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6842 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6843 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6844 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6845 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006846 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006847#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006848 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006849#endif
6850#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006851 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006852#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006853#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006854 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6855#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006856#ifdef HAVE_DEVICE_MACROS
6857 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6858 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6859 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6860#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006861#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006862 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006863#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006864#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006865 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006866#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006867#ifdef HAVE_UNSETENV
6868 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6869#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00006870#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006871 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00006872#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00006873#ifdef HAVE_FCHDIR
6874 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6875#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006876#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006877 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006878#endif
6879#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006880 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006881#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006882#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006883#ifdef WCOREDUMP
6884 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6885#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006886#ifdef WIFCONTINUED
6887 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6888#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006889#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006890 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006891#endif /* WIFSTOPPED */
6892#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006893 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006894#endif /* WIFSIGNALED */
6895#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006896 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006897#endif /* WIFEXITED */
6898#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006899 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006900#endif /* WEXITSTATUS */
6901#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006902 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006903#endif /* WTERMSIG */
6904#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006905 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006906#endif /* WSTOPSIG */
6907#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00006908#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006909 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006910#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00006911#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006912 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006913#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00006914#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00006915 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006916#endif
6917#ifdef HAVE_TEMPNAM
6918 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
6919#endif
6920#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00006921 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006922#endif
Fred Drakec9680921999-12-13 16:37:25 +00006923#ifdef HAVE_CONFSTR
6924 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
6925#endif
6926#ifdef HAVE_SYSCONF
6927 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
6928#endif
6929#ifdef HAVE_FPATHCONF
6930 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
6931#endif
6932#ifdef HAVE_PATHCONF
6933 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
6934#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006935 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006936#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00006937 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
6938#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006939#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00006940 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00006941#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006942 #ifdef MS_WINDOWS
6943 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
6944 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006945 #ifdef __VMS
6946 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
6947 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006948 {NULL, NULL} /* Sentinel */
6949};
6950
6951
Barry Warsaw4a342091996-12-19 23:50:02 +00006952static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006953ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00006954{
Fred Drake4d1e64b2002-04-15 19:40:07 +00006955 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00006956}
6957
Guido van Rossumd48f2521997-12-05 22:19:34 +00006958#if defined(PYOS_OS2)
6959/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006960static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006961{
6962 APIRET rc;
6963 ULONG values[QSV_MAX+1];
6964 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00006965 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00006966
6967 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00006968 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006969 Py_END_ALLOW_THREADS
6970
6971 if (rc != NO_ERROR) {
6972 os2_error(rc);
6973 return -1;
6974 }
6975
Fred Drake4d1e64b2002-04-15 19:40:07 +00006976 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6977 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
6978 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
6979 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
6980 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
6981 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
6982 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006983
6984 switch (values[QSV_VERSION_MINOR]) {
6985 case 0: ver = "2.00"; break;
6986 case 10: ver = "2.10"; break;
6987 case 11: ver = "2.11"; break;
6988 case 30: ver = "3.00"; break;
6989 case 40: ver = "4.00"; break;
6990 case 50: ver = "5.00"; break;
6991 default:
Tim Peters885d4572001-11-28 20:27:42 +00006992 PyOS_snprintf(tmp, sizeof(tmp),
6993 "%d-%d", values[QSV_VERSION_MAJOR],
6994 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006995 ver = &tmp[0];
6996 }
6997
6998 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006999 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007000 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007001
7002 /* Add Indicator of Which Drive was Used to Boot the System */
7003 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7004 tmp[1] = ':';
7005 tmp[2] = '\0';
7006
Fred Drake4d1e64b2002-04-15 19:40:07 +00007007 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007008}
7009#endif
7010
Barry Warsaw4a342091996-12-19 23:50:02 +00007011static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007012all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007013{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007014#ifdef F_OK
7015 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007016#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007017#ifdef R_OK
7018 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007019#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007020#ifdef W_OK
7021 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007022#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007023#ifdef X_OK
7024 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007025#endif
Fred Drakec9680921999-12-13 16:37:25 +00007026#ifdef NGROUPS_MAX
7027 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7028#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007029#ifdef TMP_MAX
7030 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7031#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007032#ifdef WCONTINUED
7033 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7034#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007035#ifdef WNOHANG
7036 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007037#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007038#ifdef WUNTRACED
7039 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7040#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007041#ifdef O_RDONLY
7042 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7043#endif
7044#ifdef O_WRONLY
7045 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7046#endif
7047#ifdef O_RDWR
7048 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7049#endif
7050#ifdef O_NDELAY
7051 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7052#endif
7053#ifdef O_NONBLOCK
7054 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7055#endif
7056#ifdef O_APPEND
7057 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7058#endif
7059#ifdef O_DSYNC
7060 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7061#endif
7062#ifdef O_RSYNC
7063 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7064#endif
7065#ifdef O_SYNC
7066 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7067#endif
7068#ifdef O_NOCTTY
7069 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7070#endif
7071#ifdef O_CREAT
7072 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7073#endif
7074#ifdef O_EXCL
7075 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7076#endif
7077#ifdef O_TRUNC
7078 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7079#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007080#ifdef O_BINARY
7081 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7082#endif
7083#ifdef O_TEXT
7084 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7085#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007086#ifdef O_LARGEFILE
7087 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7088#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007089#ifdef O_SHLOCK
7090 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7091#endif
7092#ifdef O_EXLOCK
7093 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7094#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007095
Tim Peters5aa91602002-01-30 05:46:57 +00007096/* MS Windows */
7097#ifdef O_NOINHERIT
7098 /* Don't inherit in child processes. */
7099 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7100#endif
7101#ifdef _O_SHORT_LIVED
7102 /* Optimize for short life (keep in memory). */
7103 /* MS forgot to define this one with a non-underscore form too. */
7104 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7105#endif
7106#ifdef O_TEMPORARY
7107 /* Automatically delete when last handle is closed. */
7108 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7109#endif
7110#ifdef O_RANDOM
7111 /* Optimize for random access. */
7112 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7113#endif
7114#ifdef O_SEQUENTIAL
7115 /* Optimize for sequential access. */
7116 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7117#endif
7118
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007119/* GNU extensions. */
7120#ifdef O_DIRECT
7121 /* Direct disk access. */
7122 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7123#endif
7124#ifdef O_DIRECTORY
7125 /* Must be a directory. */
7126 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7127#endif
7128#ifdef O_NOFOLLOW
7129 /* Do not follow links. */
7130 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7131#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007132
Barry Warsaw5676bd12003-01-07 20:57:09 +00007133 /* These come from sysexits.h */
7134#ifdef EX_OK
7135 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007136#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007137#ifdef EX_USAGE
7138 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007139#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007140#ifdef EX_DATAERR
7141 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007142#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007143#ifdef EX_NOINPUT
7144 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007145#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007146#ifdef EX_NOUSER
7147 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007148#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007149#ifdef EX_NOHOST
7150 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007151#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007152#ifdef EX_UNAVAILABLE
7153 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007154#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007155#ifdef EX_SOFTWARE
7156 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007157#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007158#ifdef EX_OSERR
7159 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007160#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007161#ifdef EX_OSFILE
7162 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007163#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007164#ifdef EX_CANTCREAT
7165 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007166#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007167#ifdef EX_IOERR
7168 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007169#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007170#ifdef EX_TEMPFAIL
7171 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007172#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007173#ifdef EX_PROTOCOL
7174 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007175#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007176#ifdef EX_NOPERM
7177 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007178#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007179#ifdef EX_CONFIG
7180 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007181#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007182#ifdef EX_NOTFOUND
7183 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007184#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007185
Guido van Rossum246bc171999-02-01 23:54:31 +00007186#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007187#if defined(PYOS_OS2) && defined(PYCC_GCC)
7188 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7189 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7190 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7191 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7192 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7193 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7194 if (ins(d, "P_PM", (long)P_PM)) return -1;
7195 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7196 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7197 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7198 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7199 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7200 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7201 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7202 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7203 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7204 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7205 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7206 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7207 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7208#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007209 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7210 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7211 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7212 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7213 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007214#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007215#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007216
Guido van Rossumd48f2521997-12-05 22:19:34 +00007217#if defined(PYOS_OS2)
7218 if (insertvalues(d)) return -1;
7219#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007220 return 0;
7221}
7222
7223
Tim Peters5aa91602002-01-30 05:46:57 +00007224#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007225#define INITFUNC initnt
7226#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007227
7228#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007229#define INITFUNC initos2
7230#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007231
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007232#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007233#define INITFUNC initposix
7234#define MODNAME "posix"
7235#endif
7236
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007237PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007238INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007239{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007240 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007241
Fred Drake4d1e64b2002-04-15 19:40:07 +00007242 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007243 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007244 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007245 if (m == NULL)
7246 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007247
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007248 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007249 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007250 Py_XINCREF(v);
7251 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007252 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007253 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007254
Fred Drake4d1e64b2002-04-15 19:40:07 +00007255 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007256 return;
7257
Fred Drake4d1e64b2002-04-15 19:40:07 +00007258 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007259 return;
7260
Fred Drake4d1e64b2002-04-15 19:40:07 +00007261 Py_INCREF(PyExc_OSError);
7262 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007263
Guido van Rossumb3d39562000-01-31 18:41:26 +00007264#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007265 if (posix_putenv_garbage == NULL)
7266 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007267#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007268
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007269 if (!initialized) {
7270 stat_result_desc.name = MODNAME ".stat_result";
7271 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7272 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7273 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7274 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7275 structseq_new = StatResultType.tp_new;
7276 StatResultType.tp_new = statresult_new;
7277
7278 statvfs_result_desc.name = MODNAME ".statvfs_result";
7279 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7280 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007281 Py_INCREF((PyObject*) &StatResultType);
7282 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007283 Py_INCREF((PyObject*) &StatVFSResultType);
7284 PyModule_AddObject(m, "statvfs_result",
7285 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007286 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007287
7288#ifdef __APPLE__
7289 /*
7290 * Step 2 of weak-linking support on Mac OS X.
7291 *
7292 * The code below removes functions that are not available on the
7293 * currently active platform.
7294 *
7295 * This block allow one to use a python binary that was build on
7296 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7297 * OSX 10.4.
7298 */
7299#ifdef HAVE_FSTATVFS
7300 if (fstatvfs == NULL) {
7301 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7302 return;
7303 }
7304 }
7305#endif /* HAVE_FSTATVFS */
7306
7307#ifdef HAVE_STATVFS
7308 if (statvfs == NULL) {
7309 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7310 return;
7311 }
7312 }
7313#endif /* HAVE_STATVFS */
7314
7315# ifdef HAVE_LCHOWN
7316 if (lchown == NULL) {
7317 if (PyObject_DelAttrString(m, "lchown") == -1) {
7318 return;
7319 }
7320 }
7321#endif /* HAVE_LCHOWN */
7322
7323
7324#endif /* __APPLE__ */
7325
Guido van Rossumb6775db1994-08-01 11:34:53 +00007326}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007327
7328#ifdef __cplusplus
7329}
7330#endif