blob: acd01da8eba31f75138162d2b882acdfed9fd0dd [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
Fred Drake699f3522000-06-29 21:12:41 +0000305/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000306#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000307#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000308# define STAT win32_stat
309# define FSTAT win32_fstat
310# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000311#else
312# define STAT stat
313# define FSTAT fstat
314# define STRUCT_STAT struct stat
315#endif
316
Tim Peters11b23062003-04-23 02:39:17 +0000317#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000318#include <sys/mkdev.h>
319#else
320#if defined(MAJOR_IN_SYSMACROS)
321#include <sys/sysmacros.h>
322#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000323#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
324#include <sys/mkdev.h>
325#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000326#endif
Fred Drake699f3522000-06-29 21:12:41 +0000327
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000329#ifdef WITH_NEXT_FRAMEWORK
330/* On Darwin/MacOSX a shared library or framework has no access to
331** environ directly, we must obtain it with _NSGetEnviron().
332*/
333#include <crt_externs.h>
334static char **environ;
335#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000337#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338
Barry Warsaw53699e91996-12-10 23:23:01 +0000339static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000340convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341{
Barry Warsaw53699e91996-12-10 23:23:01 +0000342 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000344 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 if (d == NULL)
346 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000347#ifdef WITH_NEXT_FRAMEWORK
348 if (environ == NULL)
349 environ = *_NSGetEnviron();
350#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351 if (environ == NULL)
352 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000353 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000355 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000356 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 char *p = strchr(*e, '=');
358 if (p == NULL)
359 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000360 k = PyString_FromStringAndSize(*e, (int)(p-*e));
361 if (k == NULL) {
362 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000364 }
365 v = PyString_FromString(p+1);
366 if (v == NULL) {
367 PyErr_Clear();
368 Py_DECREF(k);
369 continue;
370 }
371 if (PyDict_GetItem(d, k) == NULL) {
372 if (PyDict_SetItem(d, k, v) != 0)
373 PyErr_Clear();
374 }
375 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000376 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000378#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000379 {
380 APIRET rc;
381 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
382
383 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000384 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000385 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000386 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000387 Py_DECREF(v);
388 }
389 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
390 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
391 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000392 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000393 Py_DECREF(v);
394 }
395 }
396#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397 return d;
398}
399
400
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401/* Set a POSIX-specific error from errno, and return NULL */
402
Barry Warsawd58d7641998-07-23 16:14:40 +0000403static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000404posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000405{
Barry Warsawca74da41999-02-09 19:31:45 +0000406 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407}
Barry Warsawd58d7641998-07-23 16:14:40 +0000408static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000409posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000410{
Barry Warsawca74da41999-02-09 19:31:45 +0000411 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000412}
413
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000414#ifdef Py_WIN_WIDE_FILENAMES
415static PyObject *
416posix_error_with_unicode_filename(Py_UNICODE* name)
417{
418 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
419}
420#endif /* Py_WIN_WIDE_FILENAMES */
421
422
Mark Hammondef8b6542001-05-13 08:04:26 +0000423static PyObject *
424posix_error_with_allocated_filename(char* name)
425{
426 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
427 PyMem_Free(name);
428 return rc;
429}
430
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000431#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000432static PyObject *
433win32_error(char* function, char* filename)
434{
Mark Hammond33a6da92000-08-15 00:46:38 +0000435 /* XXX We should pass the function name along in the future.
436 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000437 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000438 Windows error object, which is non-trivial.
439 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000440 errno = GetLastError();
441 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000442 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000443 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000444 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000445}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000446
447#ifdef Py_WIN_WIDE_FILENAMES
448static PyObject *
449win32_error_unicode(char* function, Py_UNICODE* filename)
450{
451 /* XXX - see win32_error for comments on 'function' */
452 errno = GetLastError();
453 if (filename)
454 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
455 else
456 return PyErr_SetFromWindowsErr(errno);
457}
458
459static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
460{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000461}
462
463/* Function suitable for O& conversion */
464static int
465convert_to_unicode(PyObject *arg, void* _param)
466{
467 PyObject **param = (PyObject**)_param;
468 if (PyUnicode_CheckExact(arg)) {
469 Py_INCREF(arg);
470 *param = arg;
471 }
472 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000473 /* For a Unicode subtype that's not a Unicode object,
474 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000475 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
476 PyUnicode_GET_SIZE(arg));
477 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000478 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479 else
480 *param = PyUnicode_FromEncodedObject(arg,
481 Py_FileSystemDefaultEncoding,
482 "strict");
483 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000484}
485
486#endif /* Py_WIN_WIDE_FILENAMES */
487
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000488#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489
Guido van Rossumd48f2521997-12-05 22:19:34 +0000490#if defined(PYOS_OS2)
491/**********************************************************************
492 * Helper Function to Trim and Format OS/2 Messages
493 **********************************************************************/
494 static void
495os2_formatmsg(char *msgbuf, int msglen, char *reason)
496{
497 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
498
499 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
500 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
501
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000502 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000503 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
504 }
505
506 /* Add Optional Reason Text */
507 if (reason) {
508 strcat(msgbuf, " : ");
509 strcat(msgbuf, reason);
510 }
511}
512
513/**********************************************************************
514 * Decode an OS/2 Operating System Error Code
515 *
516 * A convenience function to lookup an OS/2 error code and return a
517 * text message we can use to raise a Python exception.
518 *
519 * Notes:
520 * The messages for errors returned from the OS/2 kernel reside in
521 * the file OSO001.MSG in the \OS2 directory hierarchy.
522 *
523 **********************************************************************/
524 static char *
525os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
526{
527 APIRET rc;
528 ULONG msglen;
529
530 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
531 Py_BEGIN_ALLOW_THREADS
532 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
533 errorcode, "oso001.msg", &msglen);
534 Py_END_ALLOW_THREADS
535
536 if (rc == NO_ERROR)
537 os2_formatmsg(msgbuf, msglen, reason);
538 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000539 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000540 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000541
542 return msgbuf;
543}
544
545/* Set an OS/2-specific error and return NULL. OS/2 kernel
546 errors are not in a global variable e.g. 'errno' nor are
547 they congruent with posix error numbers. */
548
549static PyObject * os2_error(int code)
550{
551 char text[1024];
552 PyObject *v;
553
554 os2_strerror(text, sizeof(text), code, "");
555
556 v = Py_BuildValue("(is)", code, text);
557 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000558 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000559 Py_DECREF(v);
560 }
561 return NULL; /* Signal to Python that an Exception is Pending */
562}
563
564#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565
566/* POSIX generic methods */
567
Barry Warsaw53699e91996-12-10 23:23:01 +0000568static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000569posix_fildes(PyObject *fdobj, int (*func)(int))
570{
571 int fd;
572 int res;
573 fd = PyObject_AsFileDescriptor(fdobj);
574 if (fd < 0)
575 return NULL;
576 Py_BEGIN_ALLOW_THREADS
577 res = (*func)(fd);
578 Py_END_ALLOW_THREADS
579 if (res < 0)
580 return posix_error();
581 Py_INCREF(Py_None);
582 return Py_None;
583}
Guido van Rossum21142a01999-01-08 21:05:37 +0000584
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000585#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000586static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000587unicode_file_names(void)
588{
589 static int canusewide = -1;
590 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000591 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000592 the Windows NT family. */
593 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
594 }
595 return canusewide;
596}
597#endif
Tim Peters11b23062003-04-23 02:39:17 +0000598
Guido van Rossum21142a01999-01-08 21:05:37 +0000599static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601{
Mark Hammondef8b6542001-05-13 08:04:26 +0000602 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000603 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000604 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000605 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000606 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000607 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000608 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000609 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000610 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000611 return posix_error_with_allocated_filename(path1);
612 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_INCREF(Py_None);
614 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615}
616
Barry Warsaw53699e91996-12-10 23:23:01 +0000617static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000618posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000619 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000620 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621{
Mark Hammondef8b6542001-05-13 08:04:26 +0000622 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000623 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000624 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000625 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000626 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000628 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000629 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000630 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000631 PyMem_Free(path1);
632 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000633 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000634 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000636 Py_INCREF(Py_None);
637 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000638}
639
Thomas Wouters477c8d52006-05-27 19:21:47 +0000640#ifdef Py_WIN_WIDE_FILENAMES
641static PyObject*
642win32_1str(PyObject* args, char* func,
643 char* format, BOOL (__stdcall *funcA)(LPCSTR),
644 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
645{
646 PyObject *uni;
647 char *ansi;
648 BOOL result;
649 if (unicode_file_names()) {
650 if (!PyArg_ParseTuple(args, wformat, &uni))
651 PyErr_Clear();
652 else {
653 Py_BEGIN_ALLOW_THREADS
654 result = funcW(PyUnicode_AsUnicode(uni));
655 Py_END_ALLOW_THREADS
656 if (!result)
657 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
658 Py_INCREF(Py_None);
659 return Py_None;
660 }
661 }
662 if (!PyArg_ParseTuple(args, format, &ansi))
663 return NULL;
664 Py_BEGIN_ALLOW_THREADS
665 result = funcA(ansi);
666 Py_END_ALLOW_THREADS
667 if (!result)
668 return win32_error(func, ansi);
669 Py_INCREF(Py_None);
670 return Py_None;
671
672}
673
674/* This is a reimplementation of the C library's chdir function,
675 but one that produces Win32 errors instead of DOS error codes.
676 chdir is essentially a wrapper around SetCurrentDirectory; however,
677 it also needs to set "magic" environment variables indicating
678 the per-drive current directory, which are of the form =<drive>: */
679BOOL __stdcall
680win32_chdir(LPCSTR path)
681{
682 char new_path[MAX_PATH+1];
683 int result;
684 char env[4] = "=x:";
685
686 if(!SetCurrentDirectoryA(path))
687 return FALSE;
688 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
689 if (!result)
690 return FALSE;
691 /* In the ANSI API, there should not be any paths longer
692 than MAX_PATH. */
693 assert(result <= MAX_PATH+1);
694 if (strncmp(new_path, "\\\\", 2) == 0 ||
695 strncmp(new_path, "//", 2) == 0)
696 /* UNC path, nothing to do. */
697 return TRUE;
698 env[1] = new_path[0];
699 return SetEnvironmentVariableA(env, new_path);
700}
701
702/* The Unicode version differs from the ANSI version
703 since the current directory might exceed MAX_PATH characters */
704BOOL __stdcall
705win32_wchdir(LPCWSTR path)
706{
707 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
708 int result;
709 wchar_t env[4] = L"=x:";
710
711 if(!SetCurrentDirectoryW(path))
712 return FALSE;
713 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
714 if (!result)
715 return FALSE;
716 if (result > MAX_PATH+1) {
717 new_path = malloc(result);
718 if (!new_path) {
719 SetLastError(ERROR_OUTOFMEMORY);
720 return FALSE;
721 }
722 }
723 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
724 wcsncmp(new_path, L"//", 2) == 0)
725 /* UNC path, nothing to do. */
726 return TRUE;
727 env[1] = new_path[0];
728 result = SetEnvironmentVariableW(env, new_path);
729 if (new_path != _new_path)
730 free(new_path);
731 return result;
732}
733#endif
734
Martin v. Löwis14694662006-02-03 12:54:16 +0000735#ifdef MS_WINDOWS
736/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
737 - time stamps are restricted to second resolution
738 - file modification times suffer from forth-and-back conversions between
739 UTC and local time
740 Therefore, we implement our own stat, based on the Win32 API directly.
741*/
742#define HAVE_STAT_NSEC 1
743
744struct win32_stat{
745 int st_dev;
746 __int64 st_ino;
747 unsigned short st_mode;
748 int st_nlink;
749 int st_uid;
750 int st_gid;
751 int st_rdev;
752 __int64 st_size;
753 int st_atime;
754 int st_atime_nsec;
755 int st_mtime;
756 int st_mtime_nsec;
757 int st_ctime;
758 int st_ctime_nsec;
759};
760
761static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
762
763static void
764FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
765{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
767 /* Cannot simply cast and dereference in_ptr,
768 since it might not be aligned properly */
769 __int64 in;
770 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000771 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
772 /* XXX Win32 supports time stamps past 2038; we currently don't */
773 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
774}
775
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776static void
777time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
778{
779 /* XXX endianness */
780 __int64 out;
781 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000782 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783 memcpy(out_ptr, &out, sizeof(out));
784}
785
Martin v. Löwis14694662006-02-03 12:54:16 +0000786/* Below, we *know* that ugo+r is 0444 */
787#if _S_IREAD != 0400
788#error Unsupported C library
789#endif
790static int
791attributes_to_mode(DWORD attr)
792{
793 int m = 0;
794 if (attr & FILE_ATTRIBUTE_DIRECTORY)
795 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
796 else
797 m |= _S_IFREG;
798 if (attr & FILE_ATTRIBUTE_READONLY)
799 m |= 0444;
800 else
801 m |= 0666;
802 return m;
803}
804
805static int
806attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
807{
808 memset(result, 0, sizeof(*result));
809 result->st_mode = attributes_to_mode(info->dwFileAttributes);
810 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
811 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
812 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
813 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
814
815 return 0;
816}
817
Thomas Wouters89f507f2006-12-13 04:49:30 +0000818/* Emulate GetFileAttributesEx[AW] on Windows 95 */
819static int checked = 0;
820static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
821static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
822static void
823check_gfax()
824{
825 HINSTANCE hKernel32;
826 if (checked)
827 return;
828 checked = 1;
829 hKernel32 = GetModuleHandle("KERNEL32");
830 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
831 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
832}
833
Guido van Rossumd8faa362007-04-27 19:54:29 +0000834static BOOL
835attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
836{
837 HANDLE hFindFile;
838 WIN32_FIND_DATAA FileData;
839 hFindFile = FindFirstFileA(pszFile, &FileData);
840 if (hFindFile == INVALID_HANDLE_VALUE)
841 return FALSE;
842 FindClose(hFindFile);
843 pfad->dwFileAttributes = FileData.dwFileAttributes;
844 pfad->ftCreationTime = FileData.ftCreationTime;
845 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
846 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
847 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
848 pfad->nFileSizeLow = FileData.nFileSizeLow;
849 return TRUE;
850}
851
852static BOOL
853attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
854{
855 HANDLE hFindFile;
856 WIN32_FIND_DATAW FileData;
857 hFindFile = FindFirstFileW(pszFile, &FileData);
858 if (hFindFile == INVALID_HANDLE_VALUE)
859 return FALSE;
860 FindClose(hFindFile);
861 pfad->dwFileAttributes = FileData.dwFileAttributes;
862 pfad->ftCreationTime = FileData.ftCreationTime;
863 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
864 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
865 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
866 pfad->nFileSizeLow = FileData.nFileSizeLow;
867 return TRUE;
868}
869
Thomas Wouters89f507f2006-12-13 04:49:30 +0000870static BOOL WINAPI
871Py_GetFileAttributesExA(LPCSTR pszFile,
872 GET_FILEEX_INFO_LEVELS level,
873 LPVOID pv)
874{
875 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000876 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
877 /* First try to use the system's implementation, if that is
878 available and either succeeds to gives an error other than
879 that it isn't implemented. */
880 check_gfax();
881 if (gfaxa) {
882 result = gfaxa(pszFile, level, pv);
883 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
884 return result;
885 }
886 /* It's either not present, or not implemented.
887 Emulate using FindFirstFile. */
888 if (level != GetFileExInfoStandard) {
889 SetLastError(ERROR_INVALID_PARAMETER);
890 return FALSE;
891 }
892 /* Use GetFileAttributes to validate that the file name
893 does not contain wildcards (which FindFirstFile would
894 accept). */
895 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
896 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000897 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000898}
899
900static BOOL WINAPI
901Py_GetFileAttributesExW(LPCWSTR pszFile,
902 GET_FILEEX_INFO_LEVELS level,
903 LPVOID pv)
904{
905 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000906 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
907 /* First try to use the system's implementation, if that is
908 available and either succeeds to gives an error other than
909 that it isn't implemented. */
910 check_gfax();
911 if (gfaxa) {
912 result = gfaxw(pszFile, level, pv);
913 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
914 return result;
915 }
916 /* It's either not present, or not implemented.
917 Emulate using FindFirstFile. */
918 if (level != GetFileExInfoStandard) {
919 SetLastError(ERROR_INVALID_PARAMETER);
920 return FALSE;
921 }
922 /* Use GetFileAttributes to validate that the file name
923 does not contain wildcards (which FindFirstFile would
924 accept). */
925 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
926 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000927 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000928}
929
Martin v. Löwis14694662006-02-03 12:54:16 +0000930static int
931win32_stat(const char* path, struct win32_stat *result)
932{
933 WIN32_FILE_ATTRIBUTE_DATA info;
934 int code;
935 char *dot;
936 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000937 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000938 if (GetLastError() != ERROR_SHARING_VIOLATION) {
939 /* Protocol violation: we explicitly clear errno, instead of
940 setting it to a POSIX error. Callers should use GetLastError. */
941 errno = 0;
942 return -1;
943 } else {
944 /* Could not get attributes on open file. Fall back to
945 reading the directory. */
946 if (!attributes_from_dir(path, &info)) {
947 /* Very strange. This should not fail now */
948 errno = 0;
949 return -1;
950 }
951 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000952 }
953 code = attribute_data_to_stat(&info, result);
954 if (code != 0)
955 return code;
956 /* Set S_IFEXEC if it is an .exe, .bat, ... */
957 dot = strrchr(path, '.');
958 if (dot) {
959 if (stricmp(dot, ".bat") == 0 ||
960 stricmp(dot, ".cmd") == 0 ||
961 stricmp(dot, ".exe") == 0 ||
962 stricmp(dot, ".com") == 0)
963 result->st_mode |= 0111;
964 }
965 return code;
966}
967
968static int
969win32_wstat(const wchar_t* path, struct win32_stat *result)
970{
971 int code;
972 const wchar_t *dot;
973 WIN32_FILE_ATTRIBUTE_DATA info;
974 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000975 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976 if (GetLastError() != ERROR_SHARING_VIOLATION) {
977 /* Protocol violation: we explicitly clear errno, instead of
978 setting it to a POSIX error. Callers should use GetLastError. */
979 errno = 0;
980 return -1;
981 } else {
982 /* Could not get attributes on open file. Fall back to
983 reading the directory. */
984 if (!attributes_from_dir_w(path, &info)) {
985 /* Very strange. This should not fail now */
986 errno = 0;
987 return -1;
988 }
989 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000990 }
991 code = attribute_data_to_stat(&info, result);
992 if (code < 0)
993 return code;
994 /* Set IFEXEC if it is an .exe, .bat, ... */
995 dot = wcsrchr(path, '.');
996 if (dot) {
997 if (_wcsicmp(dot, L".bat") == 0 ||
998 _wcsicmp(dot, L".cmd") == 0 ||
999 _wcsicmp(dot, L".exe") == 0 ||
1000 _wcsicmp(dot, L".com") == 0)
1001 result->st_mode |= 0111;
1002 }
1003 return code;
1004}
1005
1006static int
1007win32_fstat(int file_number, struct win32_stat *result)
1008{
1009 BY_HANDLE_FILE_INFORMATION info;
1010 HANDLE h;
1011 int type;
1012
1013 h = (HANDLE)_get_osfhandle(file_number);
1014
1015 /* Protocol violation: we explicitly clear errno, instead of
1016 setting it to a POSIX error. Callers should use GetLastError. */
1017 errno = 0;
1018
1019 if (h == INVALID_HANDLE_VALUE) {
1020 /* This is really a C library error (invalid file handle).
1021 We set the Win32 error to the closes one matching. */
1022 SetLastError(ERROR_INVALID_HANDLE);
1023 return -1;
1024 }
1025 memset(result, 0, sizeof(*result));
1026
1027 type = GetFileType(h);
1028 if (type == FILE_TYPE_UNKNOWN) {
1029 DWORD error = GetLastError();
1030 if (error != 0) {
1031 return -1;
1032 }
1033 /* else: valid but unknown file */
1034 }
1035
1036 if (type != FILE_TYPE_DISK) {
1037 if (type == FILE_TYPE_CHAR)
1038 result->st_mode = _S_IFCHR;
1039 else if (type == FILE_TYPE_PIPE)
1040 result->st_mode = _S_IFIFO;
1041 return 0;
1042 }
1043
1044 if (!GetFileInformationByHandle(h, &info)) {
1045 return -1;
1046 }
1047
1048 /* similar to stat() */
1049 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1050 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1051 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1052 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1053 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1054 /* specific to fstat() */
1055 result->st_nlink = info.nNumberOfLinks;
1056 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1057 return 0;
1058}
1059
1060#endif /* MS_WINDOWS */
1061
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001062PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001063"stat_result: Result from stat or lstat.\n\n\
1064This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001065 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001066or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1067\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001068Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1069or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001072
1073static PyStructSequence_Field stat_result_fields[] = {
1074 {"st_mode", "protection bits"},
1075 {"st_ino", "inode"},
1076 {"st_dev", "device"},
1077 {"st_nlink", "number of hard links"},
1078 {"st_uid", "user ID of owner"},
1079 {"st_gid", "group ID of owner"},
1080 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001081 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1082 {NULL, "integer time of last access"},
1083 {NULL, "integer time of last modification"},
1084 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001085 {"st_atime", "time of last access"},
1086 {"st_mtime", "time of last modification"},
1087 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001088#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001089 {"st_blksize", "blocksize for filesystem I/O"},
1090#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001091#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001092 {"st_blocks", "number of blocks allocated"},
1093#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001094#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001095 {"st_rdev", "device type (if inode device)"},
1096#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001097#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1098 {"st_flags", "user defined flags for file"},
1099#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001100#ifdef HAVE_STRUCT_STAT_ST_GEN
1101 {"st_gen", "generation number"},
1102#endif
1103#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1104 {"st_birthtime", "time of creation"},
1105#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001106 {0}
1107};
1108
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001109#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001110#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001111#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001112#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001113#endif
1114
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001115#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001116#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1117#else
1118#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1119#endif
1120
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001121#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001122#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1123#else
1124#define ST_RDEV_IDX ST_BLOCKS_IDX
1125#endif
1126
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001127#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1128#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1129#else
1130#define ST_FLAGS_IDX ST_RDEV_IDX
1131#endif
1132
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001133#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001134#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001135#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001136#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001137#endif
1138
1139#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1140#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1141#else
1142#define ST_BIRTHTIME_IDX ST_GEN_IDX
1143#endif
1144
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001145static PyStructSequence_Desc stat_result_desc = {
1146 "stat_result", /* name */
1147 stat_result__doc__, /* doc */
1148 stat_result_fields,
1149 10
1150};
1151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001152PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001153"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1154This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001155 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001156or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001158See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001159
1160static PyStructSequence_Field statvfs_result_fields[] = {
1161 {"f_bsize", },
1162 {"f_frsize", },
1163 {"f_blocks", },
1164 {"f_bfree", },
1165 {"f_bavail", },
1166 {"f_files", },
1167 {"f_ffree", },
1168 {"f_favail", },
1169 {"f_flag", },
1170 {"f_namemax",},
1171 {0}
1172};
1173
1174static PyStructSequence_Desc statvfs_result_desc = {
1175 "statvfs_result", /* name */
1176 statvfs_result__doc__, /* doc */
1177 statvfs_result_fields,
1178 10
1179};
1180
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001182static PyTypeObject StatResultType;
1183static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001184static newfunc structseq_new;
1185
1186static PyObject *
1187statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1188{
1189 PyStructSequence *result;
1190 int i;
1191
1192 result = (PyStructSequence*)structseq_new(type, args, kwds);
1193 if (!result)
1194 return NULL;
1195 /* If we have been initialized from a tuple,
1196 st_?time might be set to None. Initialize it
1197 from the int slots. */
1198 for (i = 7; i <= 9; i++) {
1199 if (result->ob_item[i+3] == Py_None) {
1200 Py_DECREF(Py_None);
1201 Py_INCREF(result->ob_item[i]);
1202 result->ob_item[i+3] = result->ob_item[i];
1203 }
1204 }
1205 return (PyObject*)result;
1206}
1207
1208
1209
1210/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001211static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001212
1213PyDoc_STRVAR(stat_float_times__doc__,
1214"stat_float_times([newval]) -> oldval\n\n\
1215Determine whether os.[lf]stat represents time stamps as float objects.\n\
1216If newval is True, future calls to stat() return floats, if it is False,\n\
1217future calls return ints. \n\
1218If newval is omitted, return the current setting.\n");
1219
1220static PyObject*
1221stat_float_times(PyObject* self, PyObject *args)
1222{
1223 int newval = -1;
1224 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1225 return NULL;
1226 if (newval == -1)
1227 /* Return old value */
1228 return PyBool_FromLong(_stat_float_times);
1229 _stat_float_times = newval;
1230 Py_INCREF(Py_None);
1231 return Py_None;
1232}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001233
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001234static void
1235fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1236{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001237 PyObject *fval,*ival;
1238#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001239 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001240#else
1241 ival = PyInt_FromLong((long)sec);
1242#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001243 if (!ival)
1244 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001245 if (_stat_float_times) {
1246 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1247 } else {
1248 fval = ival;
1249 Py_INCREF(fval);
1250 }
1251 PyStructSequence_SET_ITEM(v, index, ival);
1252 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001253}
1254
Tim Peters5aa91602002-01-30 05:46:57 +00001255/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001256 (used by posix_stat() and posix_fstat()) */
1257static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001258_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001259{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001260 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001261 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001262 if (v == NULL)
1263 return NULL;
1264
Martin v. Löwis14694662006-02-03 12:54:16 +00001265 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001266#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001267 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001268 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001269#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001270 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001271#endif
1272#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001273 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001274 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001275#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001276 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001277#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001278 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1279 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1280 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001281#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001282 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001283 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001284#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001285 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001286#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001287
Martin v. Löwis14694662006-02-03 12:54:16 +00001288#if defined(HAVE_STAT_TV_NSEC)
1289 ansec = st->st_atim.tv_nsec;
1290 mnsec = st->st_mtim.tv_nsec;
1291 cnsec = st->st_ctim.tv_nsec;
1292#elif defined(HAVE_STAT_TV_NSEC2)
1293 ansec = st->st_atimespec.tv_nsec;
1294 mnsec = st->st_mtimespec.tv_nsec;
1295 cnsec = st->st_ctimespec.tv_nsec;
1296#elif defined(HAVE_STAT_NSEC)
1297 ansec = st->st_atime_nsec;
1298 mnsec = st->st_mtime_nsec;
1299 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001300#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001301 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001302#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001303 fill_time(v, 7, st->st_atime, ansec);
1304 fill_time(v, 8, st->st_mtime, mnsec);
1305 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001306
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001307#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001308 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001309 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001311#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001312 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001313 PyInt_FromLong((long)st->st_blocks));
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_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001316 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001318#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001319#ifdef HAVE_STRUCT_STAT_ST_GEN
1320 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001322#endif
1323#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1324 {
1325 PyObject *val;
1326 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001327 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001328#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001329 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001330#else
1331 bnsec = 0;
1332#endif
1333 if (_stat_float_times) {
1334 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1335 } else {
1336 val = PyInt_FromLong((long)bsec);
1337 }
1338 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1339 val);
1340 }
1341#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001342#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1343 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001344 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001345#endif
Fred Drake699f3522000-06-29 21:12:41 +00001346
1347 if (PyErr_Occurred()) {
1348 Py_DECREF(v);
1349 return NULL;
1350 }
1351
1352 return v;
1353}
1354
Martin v. Löwisd8948722004-06-02 09:57:56 +00001355#ifdef MS_WINDOWS
1356
1357/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1358 where / can be used in place of \ and the trailing slash is optional.
1359 Both SERVER and SHARE must have at least one character.
1360*/
1361
1362#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1363#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001364#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001365#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001366#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001367
Tim Peters4ad82172004-08-30 17:02:04 +00001368static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001369IsUNCRootA(char *path, int pathlen)
1370{
1371 #define ISSLASH ISSLASHA
1372
1373 int i, share;
1374
1375 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1376 /* minimum UNCRoot is \\x\y */
1377 return FALSE;
1378 for (i = 2; i < pathlen ; i++)
1379 if (ISSLASH(path[i])) break;
1380 if (i == 2 || i == pathlen)
1381 /* do not allow \\\SHARE or \\SERVER */
1382 return FALSE;
1383 share = i+1;
1384 for (i = share; i < pathlen; i++)
1385 if (ISSLASH(path[i])) break;
1386 return (i != share && (i == pathlen || i == pathlen-1));
1387
1388 #undef ISSLASH
1389}
1390
1391#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001392static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001393IsUNCRootW(Py_UNICODE *path, int pathlen)
1394{
1395 #define ISSLASH ISSLASHW
1396
1397 int i, share;
1398
1399 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1400 /* minimum UNCRoot is \\x\y */
1401 return FALSE;
1402 for (i = 2; i < pathlen ; i++)
1403 if (ISSLASH(path[i])) break;
1404 if (i == 2 || i == pathlen)
1405 /* do not allow \\\SHARE or \\SERVER */
1406 return FALSE;
1407 share = i+1;
1408 for (i = share; i < pathlen; i++)
1409 if (ISSLASH(path[i])) break;
1410 return (i != share && (i == pathlen || i == pathlen-1));
1411
1412 #undef ISSLASH
1413}
1414#endif /* Py_WIN_WIDE_FILENAMES */
1415#endif /* MS_WINDOWS */
1416
Barry Warsaw53699e91996-12-10 23:23:01 +00001417static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001418posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001419 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001420#ifdef __VMS
1421 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1422#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001423 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001424#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001425 char *wformat,
1426 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001427{
Fred Drake699f3522000-06-29 21:12:41 +00001428 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001429 char *path = NULL; /* pass this to stat; do not free() it */
1430 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001431 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001432 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001433
1434#ifdef Py_WIN_WIDE_FILENAMES
1435 /* If on wide-character-capable OS see if argument
1436 is Unicode and if so use wide API. */
1437 if (unicode_file_names()) {
1438 PyUnicodeObject *po;
1439 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001440 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1441
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001442 Py_BEGIN_ALLOW_THREADS
1443 /* PyUnicode_AS_UNICODE result OK without
1444 thread lock as it is a simple dereference. */
1445 res = wstatfunc(wpath, &st);
1446 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001447
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001448 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001449 return win32_error_unicode("stat", wpath);
1450 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001451 }
1452 /* Drop the argument parsing error as narrow strings
1453 are also valid. */
1454 PyErr_Clear();
1455 }
1456#endif
1457
Tim Peters5aa91602002-01-30 05:46:57 +00001458 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001459 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001461 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001462
Barry Warsaw53699e91996-12-10 23:23:01 +00001463 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001464 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001465 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001466
1467 if (res != 0) {
1468#ifdef MS_WINDOWS
1469 result = win32_error("stat", pathfree);
1470#else
1471 result = posix_error_with_filename(pathfree);
1472#endif
1473 }
1474 else
1475 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001476
Tim Peters500bd032001-12-19 19:05:01 +00001477 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001478 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001479}
1480
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001481/* POSIX methods */
1482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001484"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001485Use the real uid/gid to test for access to a path. Note that most\n\
1486operations will use the effective uid/gid, therefore this routine can\n\
1487be used in a suid/sgid environment to test if the invoking user has the\n\
1488specified access to the path. The mode argument can be F_OK to test\n\
1489existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001490
1491static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001492posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001493{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001494 char *path;
1495 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001496
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001497#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001498 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001499 if (unicode_file_names()) {
1500 PyUnicodeObject *po;
1501 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1502 Py_BEGIN_ALLOW_THREADS
1503 /* PyUnicode_AS_UNICODE OK without thread lock as
1504 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001505 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001506 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001507 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001508 }
1509 /* Drop the argument parsing error as narrow strings
1510 are also valid. */
1511 PyErr_Clear();
1512 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001513 if (!PyArg_ParseTuple(args, "eti:access",
1514 Py_FileSystemDefaultEncoding, &path, &mode))
1515 return 0;
1516 Py_BEGIN_ALLOW_THREADS
1517 attr = GetFileAttributesA(path);
1518 Py_END_ALLOW_THREADS
1519 PyMem_Free(path);
1520finish:
1521 if (attr == 0xFFFFFFFF)
1522 /* File does not exist, or cannot read attributes */
1523 return PyBool_FromLong(0);
1524 /* Access is possible if either write access wasn't requested, or
1525 the file isn't read-only. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001526 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001527#else
1528 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001529 if (!PyArg_ParseTuple(args, "eti:access",
1530 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001531 return NULL;
1532 Py_BEGIN_ALLOW_THREADS
1533 res = access(path, mode);
1534 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001535 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001536 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001537#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001538}
1539
Guido van Rossumd371ff11999-01-25 16:12:23 +00001540#ifndef F_OK
1541#define F_OK 0
1542#endif
1543#ifndef R_OK
1544#define R_OK 4
1545#endif
1546#ifndef W_OK
1547#define W_OK 2
1548#endif
1549#ifndef X_OK
1550#define X_OK 1
1551#endif
1552
1553#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001554PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001555"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001556Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001557
1558static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001559posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001560{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001561 int id;
1562 char *ret;
1563
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001564 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001565 return NULL;
1566
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001567#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001568 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001569 if (id == 0) {
1570 ret = ttyname();
1571 }
1572 else {
1573 ret = NULL;
1574 }
1575#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001576 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001577#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001578 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001579 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001580 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001581}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001582#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001583
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001584#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001585PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001586"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001587Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001588
1589static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001590posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001591{
1592 char *ret;
1593 char buffer[L_ctermid];
1594
Greg Wardb48bc172000-03-01 21:51:56 +00001595#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001596 ret = ctermid_r(buffer);
1597#else
1598 ret = ctermid(buffer);
1599#endif
1600 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001601 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001602 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001603}
1604#endif
1605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001606PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001607"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001608Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001609
Barry Warsaw53699e91996-12-10 23:23:01 +00001610static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001611posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001612{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001613#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001614 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001615#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001616 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001617#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001619#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001621#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001622}
1623
Fred Drake4d1e64b2002-04-15 19:40:07 +00001624#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001625PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001626"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001627Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001629
1630static PyObject *
1631posix_fchdir(PyObject *self, PyObject *fdobj)
1632{
1633 return posix_fildes(fdobj, fchdir);
1634}
1635#endif /* HAVE_FCHDIR */
1636
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001638PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001639"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001640Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001641
Barry Warsaw53699e91996-12-10 23:23:01 +00001642static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001643posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001644{
Mark Hammondef8b6542001-05-13 08:04:26 +00001645 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001646 int i;
1647 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001648#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001649 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001650 if (unicode_file_names()) {
1651 PyUnicodeObject *po;
1652 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1653 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001654 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1655 if (attr != 0xFFFFFFFF) {
1656 if (i & _S_IWRITE)
1657 attr &= ~FILE_ATTRIBUTE_READONLY;
1658 else
1659 attr |= FILE_ATTRIBUTE_READONLY;
1660 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1661 }
1662 else
1663 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001664 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001665 if (!res)
1666 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001667 PyUnicode_AS_UNICODE(po));
1668 Py_INCREF(Py_None);
1669 return Py_None;
1670 }
1671 /* Drop the argument parsing error as narrow strings
1672 are also valid. */
1673 PyErr_Clear();
1674 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001675 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1676 &path, &i))
1677 return NULL;
1678 Py_BEGIN_ALLOW_THREADS
1679 attr = GetFileAttributesA(path);
1680 if (attr != 0xFFFFFFFF) {
1681 if (i & _S_IWRITE)
1682 attr &= ~FILE_ATTRIBUTE_READONLY;
1683 else
1684 attr |= FILE_ATTRIBUTE_READONLY;
1685 res = SetFileAttributesA(path, attr);
1686 }
1687 else
1688 res = 0;
1689 Py_END_ALLOW_THREADS
1690 if (!res) {
1691 win32_error("chmod", path);
1692 PyMem_Free(path);
1693 return NULL;
1694 }
1695 PyMem_Free(path);
1696 Py_INCREF(Py_None);
1697 return Py_None;
1698#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001699 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001700 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001701 return NULL;
1702 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001703 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001704 Py_END_ALLOW_THREADS
1705 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001706 return posix_error_with_allocated_filename(path);
1707 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001708 Py_INCREF(Py_None);
1709 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001710#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001711}
1712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001713
Thomas Wouterscf297e42007-02-23 15:07:44 +00001714#ifdef HAVE_CHFLAGS
1715PyDoc_STRVAR(posix_chflags__doc__,
1716"chflags(path, flags)\n\n\
1717Set file flags.");
1718
1719static PyObject *
1720posix_chflags(PyObject *self, PyObject *args)
1721{
1722 char *path;
1723 unsigned long flags;
1724 int res;
1725 if (!PyArg_ParseTuple(args, "etk:chflags",
1726 Py_FileSystemDefaultEncoding, &path, &flags))
1727 return NULL;
1728 Py_BEGIN_ALLOW_THREADS
1729 res = chflags(path, flags);
1730 Py_END_ALLOW_THREADS
1731 if (res < 0)
1732 return posix_error_with_allocated_filename(path);
1733 PyMem_Free(path);
1734 Py_INCREF(Py_None);
1735 return Py_None;
1736}
1737#endif /* HAVE_CHFLAGS */
1738
1739#ifdef HAVE_LCHFLAGS
1740PyDoc_STRVAR(posix_lchflags__doc__,
1741"lchflags(path, flags)\n\n\
1742Set file flags.\n\
1743This function will not follow symbolic links.");
1744
1745static PyObject *
1746posix_lchflags(PyObject *self, PyObject *args)
1747{
1748 char *path;
1749 unsigned long flags;
1750 int res;
1751 if (!PyArg_ParseTuple(args, "etk:lchflags",
1752 Py_FileSystemDefaultEncoding, &path, &flags))
1753 return NULL;
1754 Py_BEGIN_ALLOW_THREADS
1755 res = lchflags(path, flags);
1756 Py_END_ALLOW_THREADS
1757 if (res < 0)
1758 return posix_error_with_allocated_filename(path);
1759 PyMem_Free(path);
1760 Py_INCREF(Py_None);
1761 return Py_None;
1762}
1763#endif /* HAVE_LCHFLAGS */
1764
Martin v. Löwis244edc82001-10-04 22:44:26 +00001765#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001766PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001767"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001769
1770static PyObject *
1771posix_chroot(PyObject *self, PyObject *args)
1772{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001773 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001774}
1775#endif
1776
Guido van Rossum21142a01999-01-08 21:05:37 +00001777#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001778PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001779"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001780force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001781
1782static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001783posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001784{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001785 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001786}
1787#endif /* HAVE_FSYNC */
1788
1789#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001790
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001791#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001792extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1793#endif
1794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001796"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001797force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001798 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001799
1800static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001801posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001802{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001803 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001804}
1805#endif /* HAVE_FDATASYNC */
1806
1807
Fredrik Lundh10723342000-07-10 16:38:09 +00001808#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001809PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001810"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001811Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001812
Barry Warsaw53699e91996-12-10 23:23:01 +00001813static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001814posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001815{
Mark Hammondef8b6542001-05-13 08:04:26 +00001816 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001817 int uid, gid;
1818 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001819 if (!PyArg_ParseTuple(args, "etii:chown",
1820 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001821 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001822 return NULL;
1823 Py_BEGIN_ALLOW_THREADS
1824 res = chown(path, (uid_t) uid, (gid_t) gid);
1825 Py_END_ALLOW_THREADS
1826 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001827 return posix_error_with_allocated_filename(path);
1828 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001829 Py_INCREF(Py_None);
1830 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001831}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001832#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001833
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001834#ifdef HAVE_LCHOWN
1835PyDoc_STRVAR(posix_lchown__doc__,
1836"lchown(path, uid, gid)\n\n\
1837Change the owner and group id of path to the numeric uid and gid.\n\
1838This function will not follow symbolic links.");
1839
1840static PyObject *
1841posix_lchown(PyObject *self, PyObject *args)
1842{
1843 char *path = NULL;
1844 int uid, gid;
1845 int res;
1846 if (!PyArg_ParseTuple(args, "etii:lchown",
1847 Py_FileSystemDefaultEncoding, &path,
1848 &uid, &gid))
1849 return NULL;
1850 Py_BEGIN_ALLOW_THREADS
1851 res = lchown(path, (uid_t) uid, (gid_t) gid);
1852 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001853 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001854 return posix_error_with_allocated_filename(path);
1855 PyMem_Free(path);
1856 Py_INCREF(Py_None);
1857 return Py_None;
1858}
1859#endif /* HAVE_LCHOWN */
1860
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001861
Guido van Rossum36bc6801995-06-14 22:54:23 +00001862#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001864"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001866
Barry Warsaw53699e91996-12-10 23:23:01 +00001867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001868posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001869{
1870 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001871 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001872
Barry Warsaw53699e91996-12-10 23:23:01 +00001873 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001874#if defined(PYOS_OS2) && defined(PYCC_GCC)
1875 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001876#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001877 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001878#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001879 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001880 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001882 return PyUnicode_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001883}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001884
1885PyDoc_STRVAR(posix_getcwdu__doc__,
1886"getcwdu() -> path\n\n\
1887Return a unicode string representing the current working directory.");
1888
1889static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001890posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001891{
1892 char buf[1026];
1893 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001894
1895#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001896 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001897 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001898 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001899 wchar_t *wbuf2 = wbuf;
1900 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001901 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001902 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1903 /* If the buffer is large enough, len does not include the
1904 terminating \0. If the buffer is too small, len includes
1905 the space needed for the terminator. */
1906 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1907 wbuf2 = malloc(len * sizeof(wchar_t));
1908 if (wbuf2)
1909 len = GetCurrentDirectoryW(len, wbuf2);
1910 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001911 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001912 if (!wbuf2) {
1913 PyErr_NoMemory();
1914 return NULL;
1915 }
1916 if (!len) {
1917 if (wbuf2 != wbuf) free(wbuf2);
1918 return win32_error("getcwdu", NULL);
1919 }
1920 resobj = PyUnicode_FromWideChar(wbuf2, len);
1921 if (wbuf2 != wbuf) free(wbuf2);
1922 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001923 }
1924#endif
1925
1926 Py_BEGIN_ALLOW_THREADS
1927#if defined(PYOS_OS2) && defined(PYCC_GCC)
1928 res = _getcwd2(buf, sizeof buf);
1929#else
1930 res = getcwd(buf, sizeof buf);
1931#endif
1932 Py_END_ALLOW_THREADS
1933 if (res == NULL)
1934 return posix_error();
1935 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1936}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001937#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001938
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001939
Guido van Rossumb6775db1994-08-01 11:34:53 +00001940#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001941PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001942"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001944
Barry Warsaw53699e91996-12-10 23:23:01 +00001945static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001946posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001947{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001948 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001949}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001950#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001951
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001952
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001954"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001955Return a list containing the names of the entries in the directory.\n\
1956\n\
1957 path: path of directory to list\n\
1958\n\
1959The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001960entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001961
Barry Warsaw53699e91996-12-10 23:23:01 +00001962static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001963posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001964{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001965 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001966 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001967#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001968
Barry Warsaw53699e91996-12-10 23:23:01 +00001969 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001970 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001971 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001972 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001973 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001974 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001975 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001977#ifdef Py_WIN_WIDE_FILENAMES
1978 /* If on wide-character-capable OS see if argument
1979 is Unicode and if so use wide API. */
1980 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001981 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001982 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1983 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001984 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001985 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001986 /* Overallocate for \\*.*\0 */
1987 len = PyUnicode_GET_SIZE(po);
1988 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1989 if (!wnamebuf) {
1990 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001991 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001992 }
1993 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1994 wch = len > 0 ? wnamebuf[len-1] : '\0';
1995 if (wch != L'/' && wch != L'\\' && wch != L':')
1996 wnamebuf[len++] = L'\\';
1997 wcscpy(wnamebuf + len, L"*.*");
1998 if ((d = PyList_New(0)) == NULL) {
1999 free(wnamebuf);
2000 return NULL;
2001 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002002 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2003 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002004 int error = GetLastError();
2005 if (error == ERROR_FILE_NOT_FOUND) {
2006 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002007 return d;
2008 }
2009 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002010 win32_error_unicode("FindFirstFileW", wnamebuf);
2011 free(wnamebuf);
2012 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002013 }
2014 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002015 /* Skip over . and .. */
2016 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2017 wcscmp(wFileData.cFileName, L"..") != 0) {
2018 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2019 if (v == NULL) {
2020 Py_DECREF(d);
2021 d = NULL;
2022 break;
2023 }
2024 if (PyList_Append(d, v) != 0) {
2025 Py_DECREF(v);
2026 Py_DECREF(d);
2027 d = NULL;
2028 break;
2029 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002030 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002031 }
Georg Brandl622927b2006-03-07 12:48:03 +00002032 Py_BEGIN_ALLOW_THREADS
2033 result = FindNextFileW(hFindFile, &wFileData);
2034 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002035 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2036 it got to the end of the directory. */
2037 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2038 Py_DECREF(d);
2039 win32_error_unicode("FindNextFileW", wnamebuf);
2040 FindClose(hFindFile);
2041 free(wnamebuf);
2042 return NULL;
2043 }
Georg Brandl622927b2006-03-07 12:48:03 +00002044 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002045
2046 if (FindClose(hFindFile) == FALSE) {
2047 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002048 win32_error_unicode("FindClose", wnamebuf);
2049 free(wnamebuf);
2050 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002051 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002052 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002053 return d;
2054 }
2055 /* Drop the argument parsing error as narrow strings
2056 are also valid. */
2057 PyErr_Clear();
2058 }
2059#endif
2060
Tim Peters5aa91602002-01-30 05:46:57 +00002061 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002062 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002063 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002064 if (len > 0) {
2065 char ch = namebuf[len-1];
2066 if (ch != SEP && ch != ALTSEP && ch != ':')
2067 namebuf[len++] = '/';
2068 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002069 strcpy(namebuf + len, "*.*");
2070
Barry Warsaw53699e91996-12-10 23:23:01 +00002071 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002072 return NULL;
2073
2074 hFindFile = FindFirstFile(namebuf, &FileData);
2075 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002076 int error = GetLastError();
2077 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002078 return d;
2079 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002080 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081 }
2082 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002083 /* Skip over . and .. */
2084 if (strcmp(FileData.cFileName, ".") != 0 &&
2085 strcmp(FileData.cFileName, "..") != 0) {
2086 v = PyString_FromString(FileData.cFileName);
2087 if (v == NULL) {
2088 Py_DECREF(d);
2089 d = NULL;
2090 break;
2091 }
2092 if (PyList_Append(d, v) != 0) {
2093 Py_DECREF(v);
2094 Py_DECREF(d);
2095 d = NULL;
2096 break;
2097 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002098 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002099 }
Georg Brandl622927b2006-03-07 12:48:03 +00002100 Py_BEGIN_ALLOW_THREADS
2101 result = FindNextFile(hFindFile, &FileData);
2102 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002103 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2104 it got to the end of the directory. */
2105 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2106 Py_DECREF(d);
2107 win32_error("FindNextFile", namebuf);
2108 FindClose(hFindFile);
2109 return NULL;
2110 }
Georg Brandl622927b2006-03-07 12:48:03 +00002111 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002112
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002113 if (FindClose(hFindFile) == FALSE) {
2114 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002115 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002116 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002117
2118 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002119
Tim Peters0bb44a42000-09-15 07:44:49 +00002120#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002121
2122#ifndef MAX_PATH
2123#define MAX_PATH CCHMAXPATH
2124#endif
2125 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002126 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002127 PyObject *d, *v;
2128 char namebuf[MAX_PATH+5];
2129 HDIR hdir = 1;
2130 ULONG srchcnt = 1;
2131 FILEFINDBUF3 ep;
2132 APIRET rc;
2133
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002134 if (!PyArg_ParseTuple(args, "et#:listdir",
2135 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002136 return NULL;
2137 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002138 PyMem_Free(name);
2139 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002140 return NULL;
2141 }
2142 strcpy(namebuf, name);
2143 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002144 if (*pt == ALTSEP)
2145 *pt = SEP;
2146 if (namebuf[len-1] != SEP)
2147 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002148 strcpy(namebuf + len, "*.*");
2149
Neal Norwitz6c913782007-10-14 03:23:09 +00002150 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002151 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002152 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002153 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002154
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;
Neal Norwitz6c913782007-10-14 03:23:09 +00002164 return posix_error_with_allocated_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
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002194 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002195 return d;
2196#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002197
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002198 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002199 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002200 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002201 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002202 int arg_is_unicode = 1;
2203
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002204 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002205 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2206 arg_is_unicode = 0;
2207 PyErr_Clear();
2208 }
2209 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002211 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002212 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002213 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002214 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002215 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002216 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002217 return NULL;
2218 }
Georg Brandl622927b2006-03-07 12:48:03 +00002219 for (;;) {
2220 Py_BEGIN_ALLOW_THREADS
2221 ep = readdir(dirp);
2222 Py_END_ALLOW_THREADS
2223 if (ep == NULL)
2224 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002225 if (ep->d_name[0] == '.' &&
2226 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002227 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002228 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002229 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002230 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002231 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002232 d = NULL;
2233 break;
2234 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002235 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002236 PyObject *w;
2237
2238 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002239 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002240 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002241 if (w != NULL) {
2242 Py_DECREF(v);
2243 v = w;
2244 }
2245 else {
2246 /* fall back to the original byte string, as
2247 discussed in patch #683592 */
2248 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002249 }
Just van Rossum46c97842003-02-25 21:42:15 +00002250 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002251 if (PyList_Append(d, v) != 0) {
2252 Py_DECREF(v);
2253 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002254 d = NULL;
2255 break;
2256 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002257 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002258 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002259 if (errno != 0 && d != NULL) {
2260 /* readdir() returned NULL and set errno */
2261 closedir(dirp);
2262 Py_DECREF(d);
2263 return posix_error_with_allocated_filename(name);
2264 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002265 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002266 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002267
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002268 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002269
Tim Peters0bb44a42000-09-15 07:44:49 +00002270#endif /* which OS */
2271} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002272
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002273#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002274/* A helper function for abspath on win32 */
2275static PyObject *
2276posix__getfullpathname(PyObject *self, PyObject *args)
2277{
2278 /* assume encoded strings wont more than double no of chars */
2279 char inbuf[MAX_PATH*2];
2280 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002281 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002282 char outbuf[MAX_PATH*2];
2283 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002284#ifdef Py_WIN_WIDE_FILENAMES
2285 if (unicode_file_names()) {
2286 PyUnicodeObject *po;
2287 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2288 Py_UNICODE woutbuf[MAX_PATH*2];
2289 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002290 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002291 sizeof(woutbuf)/sizeof(woutbuf[0]),
2292 woutbuf, &wtemp))
2293 return win32_error("GetFullPathName", "");
2294 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2295 }
2296 /* Drop the argument parsing error as narrow strings
2297 are also valid. */
2298 PyErr_Clear();
2299 }
2300#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002301 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2302 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002303 &insize))
2304 return NULL;
2305 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2306 outbuf, &temp))
2307 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002308 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2309 return PyUnicode_Decode(outbuf, strlen(outbuf),
2310 Py_FileSystemDefaultEncoding, NULL);
2311 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002312 return PyString_FromString(outbuf);
2313} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002314#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002316PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002317"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002318Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002319
Barry Warsaw53699e91996-12-10 23:23:01 +00002320static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002321posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002322{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002323 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002324 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002325 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002326
2327#ifdef Py_WIN_WIDE_FILENAMES
2328 if (unicode_file_names()) {
2329 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002330 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002331 Py_BEGIN_ALLOW_THREADS
2332 /* PyUnicode_AS_UNICODE OK without thread lock as
2333 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002334 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002335 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002336 if (!res)
2337 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002338 Py_INCREF(Py_None);
2339 return Py_None;
2340 }
2341 /* Drop the argument parsing error as narrow strings
2342 are also valid. */
2343 PyErr_Clear();
2344 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002345 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2346 Py_FileSystemDefaultEncoding, &path, &mode))
2347 return NULL;
2348 Py_BEGIN_ALLOW_THREADS
2349 /* PyUnicode_AS_UNICODE OK without thread lock as
2350 it is a simple dereference. */
2351 res = CreateDirectoryA(path, NULL);
2352 Py_END_ALLOW_THREADS
2353 if (!res) {
2354 win32_error("mkdir", path);
2355 PyMem_Free(path);
2356 return NULL;
2357 }
2358 PyMem_Free(path);
2359 Py_INCREF(Py_None);
2360 return Py_None;
2361#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002362
Tim Peters5aa91602002-01-30 05:46:57 +00002363 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002364 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002365 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002366 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002367#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002368 res = mkdir(path);
2369#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002370 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002371#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002372 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002373 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002374 return posix_error_with_allocated_filename(path);
2375 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002376 Py_INCREF(Py_None);
2377 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002378#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002379}
2380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002381
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002382/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2383#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002384#include <sys/resource.h>
2385#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002386
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002387
2388#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002389PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002390"nice(inc) -> new_priority\n\n\
2391Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002392
Barry Warsaw53699e91996-12-10 23:23:01 +00002393static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002394posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002395{
2396 int increment, value;
2397
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002398 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002399 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002400
2401 /* There are two flavours of 'nice': one that returns the new
2402 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002403 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2404 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002405
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002406 If we are of the nice family that returns the new priority, we
2407 need to clear errno before the call, and check if errno is filled
2408 before calling posix_error() on a returnvalue of -1, because the
2409 -1 may be the actual new priority! */
2410
2411 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002412 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002413#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002414 if (value == 0)
2415 value = getpriority(PRIO_PROCESS, 0);
2416#endif
2417 if (value == -1 && errno != 0)
2418 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002419 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002420 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002421}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002422#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002423
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002424PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002425"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002426Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002427
Barry Warsaw53699e91996-12-10 23:23:01 +00002428static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002429posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002430{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002431#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002432 PyObject *o1, *o2;
2433 char *p1, *p2;
2434 BOOL result;
2435 if (unicode_file_names()) {
2436 if (!PyArg_ParseTuple(args, "O&O&:rename",
2437 convert_to_unicode, &o1,
2438 convert_to_unicode, &o2))
2439 PyErr_Clear();
2440 else {
2441 Py_BEGIN_ALLOW_THREADS
2442 result = MoveFileW(PyUnicode_AsUnicode(o1),
2443 PyUnicode_AsUnicode(o2));
2444 Py_END_ALLOW_THREADS
2445 Py_DECREF(o1);
2446 Py_DECREF(o2);
2447 if (!result)
2448 return win32_error("rename", NULL);
2449 Py_INCREF(Py_None);
2450 return Py_None;
2451 }
2452 }
2453 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2454 return NULL;
2455 Py_BEGIN_ALLOW_THREADS
2456 result = MoveFileA(p1, p2);
2457 Py_END_ALLOW_THREADS
2458 if (!result)
2459 return win32_error("rename", NULL);
2460 Py_INCREF(Py_None);
2461 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002462#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002463 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002464#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002465}
2466
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002468PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002469"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002471
Barry Warsaw53699e91996-12-10 23:23:01 +00002472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002473posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002474{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002475#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002476 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002477#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002478 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002479#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002480}
2481
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002483PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002484"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002485Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002486
Barry Warsaw53699e91996-12-10 23:23:01 +00002487static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002488posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002489{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002490#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002491 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002492#else
2493 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2494#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002495}
2496
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002497
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002498#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002499PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002500"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002501Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002502
Barry Warsaw53699e91996-12-10 23:23:01 +00002503static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002504posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002505{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002506 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002507 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002508 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002509 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002510 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002511 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002512 Py_END_ALLOW_THREADS
2513 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002514}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002515#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002516
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002518PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002519"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002520Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002521
Barry Warsaw53699e91996-12-10 23:23:01 +00002522static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002523posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002524{
2525 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002526 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002527 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002528 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002529 if (i < 0)
2530 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002531 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002532}
2533
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002534
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002536"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002537Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002540"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002541Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002542
Barry Warsaw53699e91996-12-10 23:23:01 +00002543static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002544posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002545{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002546#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002547 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002548#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002549 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002550#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002551}
2552
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002553
Guido van Rossumb6775db1994-08-01 11:34:53 +00002554#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002555PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002556"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002557Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002558
Barry Warsaw53699e91996-12-10 23:23:01 +00002559static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002560posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002561{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002562 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002563 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002564
Barry Warsaw53699e91996-12-10 23:23:01 +00002565 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002566 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002567 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002568 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002569 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002570 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002571 u.sysname,
2572 u.nodename,
2573 u.release,
2574 u.version,
2575 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002576}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002577#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002578
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002579static int
2580extract_time(PyObject *t, long* sec, long* usec)
2581{
2582 long intval;
2583 if (PyFloat_Check(t)) {
2584 double tval = PyFloat_AsDouble(t);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002585 PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002586 if (!intobj)
2587 return -1;
2588 intval = PyInt_AsLong(intobj);
2589 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002590 if (intval == -1 && PyErr_Occurred())
2591 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002592 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002593 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002594 if (*usec < 0)
2595 /* If rounding gave us a negative number,
2596 truncate. */
2597 *usec = 0;
2598 return 0;
2599 }
2600 intval = PyInt_AsLong(t);
2601 if (intval == -1 && PyErr_Occurred())
2602 return -1;
2603 *sec = intval;
2604 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002605 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002606}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002608PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002609"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002610utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002611Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002612second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002613
Barry Warsaw53699e91996-12-10 23:23:01 +00002614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002615posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002616{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002617#ifdef Py_WIN_WIDE_FILENAMES
2618 PyObject *arg;
2619 PyUnicodeObject *obwpath;
2620 wchar_t *wpath = NULL;
2621 char *apath = NULL;
2622 HANDLE hFile;
2623 long atimesec, mtimesec, ausec, musec;
2624 FILETIME atime, mtime;
2625 PyObject *result = NULL;
2626
2627 if (unicode_file_names()) {
2628 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2629 wpath = PyUnicode_AS_UNICODE(obwpath);
2630 Py_BEGIN_ALLOW_THREADS
2631 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 NULL, OPEN_EXISTING,
2633 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002634 Py_END_ALLOW_THREADS
2635 if (hFile == INVALID_HANDLE_VALUE)
2636 return win32_error_unicode("utime", wpath);
2637 } else
2638 /* Drop the argument parsing error as narrow strings
2639 are also valid. */
2640 PyErr_Clear();
2641 }
2642 if (!wpath) {
2643 if (!PyArg_ParseTuple(args, "etO:utime",
2644 Py_FileSystemDefaultEncoding, &apath, &arg))
2645 return NULL;
2646 Py_BEGIN_ALLOW_THREADS
2647 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002648 NULL, OPEN_EXISTING,
2649 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002650 Py_END_ALLOW_THREADS
2651 if (hFile == INVALID_HANDLE_VALUE) {
2652 win32_error("utime", apath);
2653 PyMem_Free(apath);
2654 return NULL;
2655 }
2656 PyMem_Free(apath);
2657 }
2658
2659 if (arg == Py_None) {
2660 SYSTEMTIME now;
2661 GetSystemTime(&now);
2662 if (!SystemTimeToFileTime(&now, &mtime) ||
2663 !SystemTimeToFileTime(&now, &atime)) {
2664 win32_error("utime", NULL);
2665 goto done;
2666 }
2667 }
2668 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2669 PyErr_SetString(PyExc_TypeError,
2670 "utime() arg 2 must be a tuple (atime, mtime)");
2671 goto done;
2672 }
2673 else {
2674 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2675 &atimesec, &ausec) == -1)
2676 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002677 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002678 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2679 &mtimesec, &musec) == -1)
2680 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002681 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002682 }
2683 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2684 /* Avoid putting the file name into the error here,
2685 as that may confuse the user into believing that
2686 something is wrong with the file, when it also
2687 could be the time stamp that gives a problem. */
2688 win32_error("utime", NULL);
2689 }
2690 Py_INCREF(Py_None);
2691 result = Py_None;
2692done:
2693 CloseHandle(hFile);
2694 return result;
2695#else /* Py_WIN_WIDE_FILENAMES */
2696
Neal Norwitz2adf2102004-06-09 01:46:02 +00002697 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002698 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002699 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002700 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002701
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002702#if defined(HAVE_UTIMES)
2703 struct timeval buf[2];
2704#define ATIME buf[0].tv_sec
2705#define MTIME buf[1].tv_sec
2706#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002707/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002708 struct utimbuf buf;
2709#define ATIME buf.actime
2710#define MTIME buf.modtime
2711#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002712#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002713 time_t buf[2];
2714#define ATIME buf[0]
2715#define MTIME buf[1]
2716#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002717#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002718
Mark Hammond817c9292003-12-03 01:22:38 +00002719
Thomas Wouters477c8d52006-05-27 19:21:47 +00002720 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002721 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002722 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002723 if (arg == Py_None) {
2724 /* optional time values not given */
2725 Py_BEGIN_ALLOW_THREADS
2726 res = utime(path, NULL);
2727 Py_END_ALLOW_THREADS
2728 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002729 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002730 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002731 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002732 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002733 return NULL;
2734 }
2735 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002736 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002737 &atime, &ausec) == -1) {
2738 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002739 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002740 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002741 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002742 &mtime, &musec) == -1) {
2743 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002744 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002745 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002746 ATIME = atime;
2747 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002748#ifdef HAVE_UTIMES
2749 buf[0].tv_usec = ausec;
2750 buf[1].tv_usec = musec;
2751 Py_BEGIN_ALLOW_THREADS
2752 res = utimes(path, buf);
2753 Py_END_ALLOW_THREADS
2754#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002755 Py_BEGIN_ALLOW_THREADS
2756 res = utime(path, UTIME_ARG);
2757 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002758#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002759 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002760 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002761 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002762 }
Neal Norwitz96652712004-06-06 20:40:27 +00002763 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002764 Py_INCREF(Py_None);
2765 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002766#undef UTIME_ARG
2767#undef ATIME
2768#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002769#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002770}
2771
Guido van Rossum85e3b011991-06-03 12:42:10 +00002772
Guido van Rossum3b066191991-06-04 19:40:25 +00002773/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002775PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002776"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002777Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002778
Barry Warsaw53699e91996-12-10 23:23:01 +00002779static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002780posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002781{
2782 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002783 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002784 return NULL;
2785 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002786 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002787}
2788
Martin v. Löwis114619e2002-10-07 06:44:21 +00002789#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2790static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002791free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002792{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002793 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002794 for (i = 0; i < count; i++)
2795 PyMem_Free(array[i]);
2796 PyMem_DEL(array);
2797}
2798#endif
2799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002800
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002801#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002802PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002803"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002804Execute an executable path with arguments, replacing current process.\n\
2805\n\
2806 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002807 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808
Barry Warsaw53699e91996-12-10 23:23:01 +00002809static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002810posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002811{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002812 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002813 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002814 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002815 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002816 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002817
Guido van Rossum89b33251993-10-22 14:26:06 +00002818 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002819 argv is a list or tuple of strings. */
2820
Martin v. Löwis114619e2002-10-07 06:44:21 +00002821 if (!PyArg_ParseTuple(args, "etO:execv",
2822 Py_FileSystemDefaultEncoding,
2823 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002824 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002825 if (PyList_Check(argv)) {
2826 argc = PyList_Size(argv);
2827 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002828 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002829 else if (PyTuple_Check(argv)) {
2830 argc = PyTuple_Size(argv);
2831 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002832 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002833 else {
Fred Drake661ea262000-10-24 19:57:45 +00002834 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002835 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002836 return NULL;
2837 }
Thomas Heller6790d602007-08-30 17:15:14 +00002838 if (argc < 1) {
2839 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2840 PyMem_Free(path);
2841 return NULL;
2842 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002843
Barry Warsaw53699e91996-12-10 23:23:01 +00002844 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002845 if (argvlist == NULL) {
2846 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002847 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002848 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002849 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002850 if (!PyArg_Parse((*getitem)(argv, i), "et",
2851 Py_FileSystemDefaultEncoding,
2852 &argvlist[i])) {
2853 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002854 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002855 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002856 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002857 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002858
Guido van Rossum85e3b011991-06-03 12:42:10 +00002859 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002860 }
2861 argvlist[argc] = NULL;
2862
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002863 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002864
Guido van Rossum85e3b011991-06-03 12:42:10 +00002865 /* If we get here it's definitely an error */
2866
Martin v. Löwis114619e2002-10-07 06:44:21 +00002867 free_string_array(argvlist, argc);
2868 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002869 return posix_error();
2870}
2871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002872
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002873PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002874"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002875Execute a path with arguments and environment, replacing current process.\n\
2876\n\
2877 path: path of executable file\n\
2878 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002879 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002880
Barry Warsaw53699e91996-12-10 23:23:01 +00002881static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002882posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002883{
2884 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002885 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002886 char **argvlist;
2887 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002888 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002889 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002890 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002891 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002892
2893 /* execve has three arguments: (path, argv, env), where
2894 argv is a list or tuple of strings and env is a dictionary
2895 like posix.environ. */
2896
Martin v. Löwis114619e2002-10-07 06:44:21 +00002897 if (!PyArg_ParseTuple(args, "etOO:execve",
2898 Py_FileSystemDefaultEncoding,
2899 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002900 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002901 if (PyList_Check(argv)) {
2902 argc = PyList_Size(argv);
2903 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002904 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002905 else if (PyTuple_Check(argv)) {
2906 argc = PyTuple_Size(argv);
2907 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002908 }
2909 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002910 PyErr_SetString(PyExc_TypeError,
2911 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002912 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002913 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002914 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002915 PyErr_SetString(PyExc_TypeError,
2916 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002917 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002918 }
2919
Barry Warsaw53699e91996-12-10 23:23:01 +00002920 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002921 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002922 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002923 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002924 }
2925 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002926 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002927 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002928 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002929 &argvlist[i]))
2930 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002931 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002932 goto fail_1;
2933 }
2934 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002935 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002936 argvlist[argc] = NULL;
2937
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002938 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002939 if (i < 0)
2940 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002941 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002942 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002943 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002944 goto fail_1;
2945 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002946 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002947 keys = PyMapping_Keys(env);
2948 vals = PyMapping_Values(env);
2949 if (!keys || !vals)
2950 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002951 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2952 PyErr_SetString(PyExc_TypeError,
2953 "execve(): env.keys() or env.values() is not a list");
2954 goto fail_2;
2955 }
Tim Peters5aa91602002-01-30 05:46:57 +00002956
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002957 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002958 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002959 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002960
2961 key = PyList_GetItem(keys, pos);
2962 val = PyList_GetItem(vals, pos);
2963 if (!key || !val)
2964 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002965
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002966 if (!PyArg_Parse(
2967 key,
2968 "s;execve() arg 3 contains a non-string key",
2969 &k) ||
2970 !PyArg_Parse(
2971 val,
2972 "s;execve() arg 3 contains a non-string value",
2973 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002974 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002975 goto fail_2;
2976 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002977
2978#if defined(PYOS_OS2)
2979 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2980 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2981#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002982 len = PyString_Size(key) + PyString_Size(val) + 2;
2983 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002984 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002985 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002986 goto fail_2;
2987 }
Tim Petersc8996f52001-12-03 20:41:00 +00002988 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002989 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002990#if defined(PYOS_OS2)
2991 }
2992#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002993 }
2994 envlist[envc] = 0;
2995
2996 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002997
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002998 /* If we get here it's definitely an error */
2999
3000 (void) posix_error();
3001
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003002 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003003 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003004 PyMem_DEL(envlist[envc]);
3005 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003006 fail_1:
3007 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003008 Py_XDECREF(vals);
3009 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003010 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003011 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003012 return NULL;
3013}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003014#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003015
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003016
Guido van Rossuma1065681999-01-25 23:20:23 +00003017#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003018PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003019"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003020Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003021\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003022 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003023 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003024 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003025
3026static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003027posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003028{
3029 char *path;
3030 PyObject *argv;
3031 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003032 int mode, i;
3033 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003034 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003035 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003036
3037 /* spawnv has three arguments: (mode, path, argv), where
3038 argv is a list or tuple of strings. */
3039
Martin v. Löwis114619e2002-10-07 06:44:21 +00003040 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3041 Py_FileSystemDefaultEncoding,
3042 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003043 return NULL;
3044 if (PyList_Check(argv)) {
3045 argc = PyList_Size(argv);
3046 getitem = PyList_GetItem;
3047 }
3048 else if (PyTuple_Check(argv)) {
3049 argc = PyTuple_Size(argv);
3050 getitem = PyTuple_GetItem;
3051 }
3052 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003053 PyErr_SetString(PyExc_TypeError,
3054 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003055 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003056 return NULL;
3057 }
3058
3059 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003060 if (argvlist == NULL) {
3061 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003062 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003063 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003064 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003065 if (!PyArg_Parse((*getitem)(argv, i), "et",
3066 Py_FileSystemDefaultEncoding,
3067 &argvlist[i])) {
3068 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003069 PyErr_SetString(
3070 PyExc_TypeError,
3071 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003072 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003073 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003074 }
3075 }
3076 argvlist[argc] = NULL;
3077
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003078#if defined(PYOS_OS2) && defined(PYCC_GCC)
3079 Py_BEGIN_ALLOW_THREADS
3080 spawnval = spawnv(mode, path, argvlist);
3081 Py_END_ALLOW_THREADS
3082#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003083 if (mode == _OLD_P_OVERLAY)
3084 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003085
Tim Peters25059d32001-12-07 20:35:43 +00003086 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003087 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003088 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003089#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003090
Martin v. Löwis114619e2002-10-07 06:44:21 +00003091 free_string_array(argvlist, argc);
3092 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003093
Fred Drake699f3522000-06-29 21:12:41 +00003094 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003095 return posix_error();
3096 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003097#if SIZEOF_LONG == SIZEOF_VOID_P
3098 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003099#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003100 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003101#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003102}
3103
3104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003105PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003106"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003107Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003108\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003109 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003110 path: path of executable file\n\
3111 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003112 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003113
3114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003115posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003116{
3117 char *path;
3118 PyObject *argv, *env;
3119 char **argvlist;
3120 char **envlist;
3121 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003122 int mode, pos, envc;
3123 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003124 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003125 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003126 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003127
3128 /* spawnve has four arguments: (mode, path, argv, env), where
3129 argv is a list or tuple of strings and env is a dictionary
3130 like posix.environ. */
3131
Martin v. Löwis114619e2002-10-07 06:44:21 +00003132 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3133 Py_FileSystemDefaultEncoding,
3134 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003135 return NULL;
3136 if (PyList_Check(argv)) {
3137 argc = PyList_Size(argv);
3138 getitem = PyList_GetItem;
3139 }
3140 else if (PyTuple_Check(argv)) {
3141 argc = PyTuple_Size(argv);
3142 getitem = PyTuple_GetItem;
3143 }
3144 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003145 PyErr_SetString(PyExc_TypeError,
3146 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003147 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003148 }
3149 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003150 PyErr_SetString(PyExc_TypeError,
3151 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003152 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003153 }
3154
3155 argvlist = PyMem_NEW(char *, argc+1);
3156 if (argvlist == NULL) {
3157 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003158 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003159 }
3160 for (i = 0; i < argc; i++) {
3161 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003162 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003163 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003164 &argvlist[i]))
3165 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003166 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003167 goto fail_1;
3168 }
3169 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003170 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003171 argvlist[argc] = NULL;
3172
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003173 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003174 if (i < 0)
3175 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003176 envlist = PyMem_NEW(char *, i + 1);
3177 if (envlist == NULL) {
3178 PyErr_NoMemory();
3179 goto fail_1;
3180 }
3181 envc = 0;
3182 keys = PyMapping_Keys(env);
3183 vals = PyMapping_Values(env);
3184 if (!keys || !vals)
3185 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003186 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3187 PyErr_SetString(PyExc_TypeError,
3188 "spawnve(): env.keys() or env.values() is not a list");
3189 goto fail_2;
3190 }
Tim Peters5aa91602002-01-30 05:46:57 +00003191
Guido van Rossuma1065681999-01-25 23:20:23 +00003192 for (pos = 0; pos < i; pos++) {
3193 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003194 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003195
3196 key = PyList_GetItem(keys, pos);
3197 val = PyList_GetItem(vals, pos);
3198 if (!key || !val)
3199 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003200
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003201 if (!PyArg_Parse(
3202 key,
3203 "s;spawnve() arg 3 contains a non-string key",
3204 &k) ||
3205 !PyArg_Parse(
3206 val,
3207 "s;spawnve() arg 3 contains a non-string value",
3208 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003209 {
3210 goto fail_2;
3211 }
Tim Petersc8996f52001-12-03 20:41:00 +00003212 len = PyString_Size(key) + PyString_Size(val) + 2;
3213 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003214 if (p == NULL) {
3215 PyErr_NoMemory();
3216 goto fail_2;
3217 }
Tim Petersc8996f52001-12-03 20:41:00 +00003218 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003219 envlist[envc++] = p;
3220 }
3221 envlist[envc] = 0;
3222
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003223#if defined(PYOS_OS2) && defined(PYCC_GCC)
3224 Py_BEGIN_ALLOW_THREADS
3225 spawnval = spawnve(mode, path, argvlist, envlist);
3226 Py_END_ALLOW_THREADS
3227#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003228 if (mode == _OLD_P_OVERLAY)
3229 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003230
3231 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003232 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003233 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003234#endif
Tim Peters25059d32001-12-07 20:35:43 +00003235
Fred Drake699f3522000-06-29 21:12:41 +00003236 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003237 (void) posix_error();
3238 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003239#if SIZEOF_LONG == SIZEOF_VOID_P
3240 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003241#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003242 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003243#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003244
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003245 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003246 while (--envc >= 0)
3247 PyMem_DEL(envlist[envc]);
3248 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003249 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003250 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003251 Py_XDECREF(vals);
3252 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003253 fail_0:
3254 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003255 return res;
3256}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003257
3258/* OS/2 supports spawnvp & spawnvpe natively */
3259#if defined(PYOS_OS2)
3260PyDoc_STRVAR(posix_spawnvp__doc__,
3261"spawnvp(mode, file, args)\n\n\
3262Execute the program 'file' in a new process, using the environment\n\
3263search path to find the file.\n\
3264\n\
3265 mode: mode of process creation\n\
3266 file: executable file name\n\
3267 args: tuple or list of strings");
3268
3269static PyObject *
3270posix_spawnvp(PyObject *self, PyObject *args)
3271{
3272 char *path;
3273 PyObject *argv;
3274 char **argvlist;
3275 int mode, i, argc;
3276 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003277 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003278
3279 /* spawnvp has three arguments: (mode, path, argv), where
3280 argv is a list or tuple of strings. */
3281
3282 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3283 Py_FileSystemDefaultEncoding,
3284 &path, &argv))
3285 return NULL;
3286 if (PyList_Check(argv)) {
3287 argc = PyList_Size(argv);
3288 getitem = PyList_GetItem;
3289 }
3290 else if (PyTuple_Check(argv)) {
3291 argc = PyTuple_Size(argv);
3292 getitem = PyTuple_GetItem;
3293 }
3294 else {
3295 PyErr_SetString(PyExc_TypeError,
3296 "spawnvp() arg 2 must be a tuple or list");
3297 PyMem_Free(path);
3298 return NULL;
3299 }
3300
3301 argvlist = PyMem_NEW(char *, argc+1);
3302 if (argvlist == NULL) {
3303 PyMem_Free(path);
3304 return PyErr_NoMemory();
3305 }
3306 for (i = 0; i < argc; i++) {
3307 if (!PyArg_Parse((*getitem)(argv, i), "et",
3308 Py_FileSystemDefaultEncoding,
3309 &argvlist[i])) {
3310 free_string_array(argvlist, i);
3311 PyErr_SetString(
3312 PyExc_TypeError,
3313 "spawnvp() arg 2 must contain only strings");
3314 PyMem_Free(path);
3315 return NULL;
3316 }
3317 }
3318 argvlist[argc] = NULL;
3319
3320 Py_BEGIN_ALLOW_THREADS
3321#if defined(PYCC_GCC)
3322 spawnval = spawnvp(mode, path, argvlist);
3323#else
3324 spawnval = _spawnvp(mode, path, argvlist);
3325#endif
3326 Py_END_ALLOW_THREADS
3327
3328 free_string_array(argvlist, argc);
3329 PyMem_Free(path);
3330
3331 if (spawnval == -1)
3332 return posix_error();
3333 else
3334 return Py_BuildValue("l", (long) spawnval);
3335}
3336
3337
3338PyDoc_STRVAR(posix_spawnvpe__doc__,
3339"spawnvpe(mode, file, args, env)\n\n\
3340Execute the program 'file' in a new process, using the environment\n\
3341search path to find the file.\n\
3342\n\
3343 mode: mode of process creation\n\
3344 file: executable file name\n\
3345 args: tuple or list of arguments\n\
3346 env: dictionary of strings mapping to strings");
3347
3348static PyObject *
3349posix_spawnvpe(PyObject *self, PyObject *args)
3350{
3351 char *path;
3352 PyObject *argv, *env;
3353 char **argvlist;
3354 char **envlist;
3355 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3356 int mode, i, pos, argc, envc;
3357 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003358 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003359 int lastarg = 0;
3360
3361 /* spawnvpe has four arguments: (mode, path, argv, env), where
3362 argv is a list or tuple of strings and env is a dictionary
3363 like posix.environ. */
3364
3365 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3366 Py_FileSystemDefaultEncoding,
3367 &path, &argv, &env))
3368 return NULL;
3369 if (PyList_Check(argv)) {
3370 argc = PyList_Size(argv);
3371 getitem = PyList_GetItem;
3372 }
3373 else if (PyTuple_Check(argv)) {
3374 argc = PyTuple_Size(argv);
3375 getitem = PyTuple_GetItem;
3376 }
3377 else {
3378 PyErr_SetString(PyExc_TypeError,
3379 "spawnvpe() arg 2 must be a tuple or list");
3380 goto fail_0;
3381 }
3382 if (!PyMapping_Check(env)) {
3383 PyErr_SetString(PyExc_TypeError,
3384 "spawnvpe() arg 3 must be a mapping object");
3385 goto fail_0;
3386 }
3387
3388 argvlist = PyMem_NEW(char *, argc+1);
3389 if (argvlist == NULL) {
3390 PyErr_NoMemory();
3391 goto fail_0;
3392 }
3393 for (i = 0; i < argc; i++) {
3394 if (!PyArg_Parse((*getitem)(argv, i),
3395 "et;spawnvpe() arg 2 must contain only strings",
3396 Py_FileSystemDefaultEncoding,
3397 &argvlist[i]))
3398 {
3399 lastarg = i;
3400 goto fail_1;
3401 }
3402 }
3403 lastarg = argc;
3404 argvlist[argc] = NULL;
3405
3406 i = PyMapping_Size(env);
3407 if (i < 0)
3408 goto fail_1;
3409 envlist = PyMem_NEW(char *, i + 1);
3410 if (envlist == NULL) {
3411 PyErr_NoMemory();
3412 goto fail_1;
3413 }
3414 envc = 0;
3415 keys = PyMapping_Keys(env);
3416 vals = PyMapping_Values(env);
3417 if (!keys || !vals)
3418 goto fail_2;
3419 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3420 PyErr_SetString(PyExc_TypeError,
3421 "spawnvpe(): env.keys() or env.values() is not a list");
3422 goto fail_2;
3423 }
3424
3425 for (pos = 0; pos < i; pos++) {
3426 char *p, *k, *v;
3427 size_t len;
3428
3429 key = PyList_GetItem(keys, pos);
3430 val = PyList_GetItem(vals, pos);
3431 if (!key || !val)
3432 goto fail_2;
3433
3434 if (!PyArg_Parse(
3435 key,
3436 "s;spawnvpe() arg 3 contains a non-string key",
3437 &k) ||
3438 !PyArg_Parse(
3439 val,
3440 "s;spawnvpe() arg 3 contains a non-string value",
3441 &v))
3442 {
3443 goto fail_2;
3444 }
3445 len = PyString_Size(key) + PyString_Size(val) + 2;
3446 p = PyMem_NEW(char, len);
3447 if (p == NULL) {
3448 PyErr_NoMemory();
3449 goto fail_2;
3450 }
3451 PyOS_snprintf(p, len, "%s=%s", k, v);
3452 envlist[envc++] = p;
3453 }
3454 envlist[envc] = 0;
3455
3456 Py_BEGIN_ALLOW_THREADS
3457#if defined(PYCC_GCC)
3458 spawnval = spawnve(mode, path, argvlist, envlist);
3459#else
3460 spawnval = _spawnve(mode, path, argvlist, envlist);
3461#endif
3462 Py_END_ALLOW_THREADS
3463
3464 if (spawnval == -1)
3465 (void) posix_error();
3466 else
3467 res = Py_BuildValue("l", (long) spawnval);
3468
3469 fail_2:
3470 while (--envc >= 0)
3471 PyMem_DEL(envlist[envc]);
3472 PyMem_DEL(envlist);
3473 fail_1:
3474 free_string_array(argvlist, lastarg);
3475 Py_XDECREF(vals);
3476 Py_XDECREF(keys);
3477 fail_0:
3478 PyMem_Free(path);
3479 return res;
3480}
3481#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003482#endif /* HAVE_SPAWNV */
3483
3484
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003485#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003486PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003487"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003488Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3489\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003490Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003491
3492static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003493posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003494{
Neal Norwitze241ce82003-02-17 18:17:05 +00003495 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003496 if (pid == -1)
3497 return posix_error();
3498 PyOS_AfterFork();
3499 return PyInt_FromLong((long)pid);
3500}
3501#endif
3502
3503
Guido van Rossumad0ee831995-03-01 10:34:45 +00003504#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003505PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003506"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003507Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003508Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003509
Barry Warsaw53699e91996-12-10 23:23:01 +00003510static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003511posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003512{
Neal Norwitze241ce82003-02-17 18:17:05 +00003513 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003514 if (pid == -1)
3515 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003516 if (pid == 0)
3517 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003518 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003519}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003520#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003521
Neal Norwitzb59798b2003-03-21 01:43:31 +00003522/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003523/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3524#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003525#define DEV_PTY_FILE "/dev/ptc"
3526#define HAVE_DEV_PTMX
3527#else
3528#define DEV_PTY_FILE "/dev/ptmx"
3529#endif
3530
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003531#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003532#ifdef HAVE_PTY_H
3533#include <pty.h>
3534#else
3535#ifdef HAVE_LIBUTIL_H
3536#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003537#endif /* HAVE_LIBUTIL_H */
3538#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003539#ifdef HAVE_STROPTS_H
3540#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003541#endif
3542#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003543
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003544#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003545PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003546"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003547Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003548
3549static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003550posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003551{
3552 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003553#ifndef HAVE_OPENPTY
3554 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003555#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003556#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003557 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003558#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003559 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003560#endif
3561#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003562
Thomas Wouters70c21a12000-07-14 14:28:33 +00003563#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003564 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3565 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003566#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003567 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3568 if (slave_name == NULL)
3569 return posix_error();
3570
3571 slave_fd = open(slave_name, O_RDWR);
3572 if (slave_fd < 0)
3573 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003574#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003575 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003576 if (master_fd < 0)
3577 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003578 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003579 /* change permission of slave */
3580 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003581 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003582 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003583 }
3584 /* unlock slave */
3585 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003586 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003587 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003588 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003589 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003590 slave_name = ptsname(master_fd); /* get name of slave */
3591 if (slave_name == NULL)
3592 return posix_error();
3593 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3594 if (slave_fd < 0)
3595 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003596#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003597 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3598 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003599#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003600 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003601#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003602#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003603#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003604
Fred Drake8cef4cf2000-06-28 16:40:38 +00003605 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003606
Fred Drake8cef4cf2000-06-28 16:40:38 +00003607}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003608#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003609
3610#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003611PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003612"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003613Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3614Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003615To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003616
3617static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003618posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003619{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003620 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003621
Fred Drake8cef4cf2000-06-28 16:40:38 +00003622 pid = forkpty(&master_fd, NULL, NULL, NULL);
3623 if (pid == -1)
3624 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003625 if (pid == 0)
3626 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003627 return Py_BuildValue("(ii)", pid, master_fd);
3628}
3629#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003630
Guido van Rossumad0ee831995-03-01 10:34:45 +00003631#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003632PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003633"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003634Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003635
Barry Warsaw53699e91996-12-10 23:23:01 +00003636static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003637posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003638{
Barry Warsaw53699e91996-12-10 23:23:01 +00003639 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003640}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003641#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003643
Guido van Rossumad0ee831995-03-01 10:34:45 +00003644#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003645PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003646"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003647Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003648
Barry Warsaw53699e91996-12-10 23:23:01 +00003649static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003650posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003651{
Barry Warsaw53699e91996-12-10 23:23:01 +00003652 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003653}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003654#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003655
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003656
Guido van Rossumad0ee831995-03-01 10:34:45 +00003657#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003658PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003659"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003660Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003661
Barry Warsaw53699e91996-12-10 23:23:01 +00003662static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003663posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003664{
Barry Warsaw53699e91996-12-10 23:23:01 +00003665 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003666}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003667#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003668
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003670PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003671"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003672Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003673
Barry Warsaw53699e91996-12-10 23:23:01 +00003674static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003675posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003676{
Barry Warsaw53699e91996-12-10 23:23:01 +00003677 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003678}
3679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003680
Fred Drakec9680921999-12-13 16:37:25 +00003681#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003682PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003683"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003684Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003685
3686static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003687posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003688{
3689 PyObject *result = NULL;
3690
Fred Drakec9680921999-12-13 16:37:25 +00003691#ifdef NGROUPS_MAX
3692#define MAX_GROUPS NGROUPS_MAX
3693#else
3694 /* defined to be 16 on Solaris7, so this should be a small number */
3695#define MAX_GROUPS 64
3696#endif
3697 gid_t grouplist[MAX_GROUPS];
3698 int n;
3699
3700 n = getgroups(MAX_GROUPS, grouplist);
3701 if (n < 0)
3702 posix_error();
3703 else {
3704 result = PyList_New(n);
3705 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003706 int i;
3707 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003708 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003709 if (o == NULL) {
3710 Py_DECREF(result);
3711 result = NULL;
3712 break;
3713 }
3714 PyList_SET_ITEM(result, i, o);
3715 }
3716 }
3717 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003718
Fred Drakec9680921999-12-13 16:37:25 +00003719 return result;
3720}
3721#endif
3722
Martin v. Löwis606edc12002-06-13 21:09:11 +00003723#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003724PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003725"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003726Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003727
3728static PyObject *
3729posix_getpgid(PyObject *self, PyObject *args)
3730{
3731 int pid, pgid;
3732 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3733 return NULL;
3734 pgid = getpgid(pid);
3735 if (pgid < 0)
3736 return posix_error();
3737 return PyInt_FromLong((long)pgid);
3738}
3739#endif /* HAVE_GETPGID */
3740
3741
Guido van Rossumb6775db1994-08-01 11:34:53 +00003742#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003743PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003744"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003745Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003746
Barry Warsaw53699e91996-12-10 23:23:01 +00003747static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003748posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003749{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003750#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003751 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003752#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003753 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003754#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003755}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003756#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003757
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003758
Guido van Rossumb6775db1994-08-01 11:34:53 +00003759#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003760PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003761"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003762Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003763
Barry Warsaw53699e91996-12-10 23:23:01 +00003764static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003765posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003766{
Guido van Rossum64933891994-10-20 21:56:42 +00003767#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003768 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003769#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003770 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003771#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003772 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003773 Py_INCREF(Py_None);
3774 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003775}
3776
Guido van Rossumb6775db1994-08-01 11:34:53 +00003777#endif /* HAVE_SETPGRP */
3778
Guido van Rossumad0ee831995-03-01 10:34:45 +00003779#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003780PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003781"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003782Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003783
Barry Warsaw53699e91996-12-10 23:23:01 +00003784static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003785posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003786{
Barry Warsaw53699e91996-12-10 23:23:01 +00003787 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003788}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003789#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003791
Fred Drake12c6e2d1999-12-14 21:25:03 +00003792#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003793PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003794"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003795Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003796
3797static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003798posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003799{
Neal Norwitze241ce82003-02-17 18:17:05 +00003800 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003801 char *name;
3802 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003803
Fred Drakea30680b2000-12-06 21:24:28 +00003804 errno = 0;
3805 name = getlogin();
3806 if (name == NULL) {
3807 if (errno)
3808 posix_error();
3809 else
3810 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003811 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003812 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003813 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003814 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003815 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003816
Fred Drake12c6e2d1999-12-14 21:25:03 +00003817 return result;
3818}
3819#endif
3820
Guido van Rossumad0ee831995-03-01 10:34:45 +00003821#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003822PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003823"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003825
Barry Warsaw53699e91996-12-10 23:23:01 +00003826static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003827posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003828{
Barry Warsaw53699e91996-12-10 23:23:01 +00003829 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003830}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003831#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003832
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003833
Guido van Rossumad0ee831995-03-01 10:34:45 +00003834#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003835PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003836"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003837Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003838
Barry Warsaw53699e91996-12-10 23:23:01 +00003839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003840posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003841{
3842 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003843 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003844 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003845#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003846 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3847 APIRET rc;
3848 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003849 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003850
3851 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3852 APIRET rc;
3853 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003854 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003855
3856 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003857 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003858#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003859 if (kill(pid, sig) == -1)
3860 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003861#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003862 Py_INCREF(Py_None);
3863 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003864}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003865#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003866
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003867#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003868PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003869"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003870Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003871
3872static PyObject *
3873posix_killpg(PyObject *self, PyObject *args)
3874{
3875 int pgid, sig;
3876 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3877 return NULL;
3878 if (killpg(pgid, sig) == -1)
3879 return posix_error();
3880 Py_INCREF(Py_None);
3881 return Py_None;
3882}
3883#endif
3884
Guido van Rossumc0125471996-06-28 18:55:32 +00003885#ifdef HAVE_PLOCK
3886
3887#ifdef HAVE_SYS_LOCK_H
3888#include <sys/lock.h>
3889#endif
3890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003891PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003892"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003893Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003894
Barry Warsaw53699e91996-12-10 23:23:01 +00003895static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003896posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003897{
3898 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003899 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003900 return NULL;
3901 if (plock(op) == -1)
3902 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003903 Py_INCREF(Py_None);
3904 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003905}
3906#endif
3907
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003908
Guido van Rossum3b066191991-06-04 19:40:25 +00003909
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003910
Guido van Rossumb6775db1994-08-01 11:34:53 +00003911#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003912PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003913"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003914Set the current process's user id.");
3915
Barry Warsaw53699e91996-12-10 23:23:01 +00003916static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003917posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003918{
3919 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003920 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003921 return NULL;
3922 if (setuid(uid) < 0)
3923 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003924 Py_INCREF(Py_None);
3925 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003926}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003927#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003929
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003930#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003931PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003932"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003933Set the current process's effective user id.");
3934
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003935static PyObject *
3936posix_seteuid (PyObject *self, PyObject *args)
3937{
3938 int euid;
3939 if (!PyArg_ParseTuple(args, "i", &euid)) {
3940 return NULL;
3941 } else if (seteuid(euid) < 0) {
3942 return posix_error();
3943 } else {
3944 Py_INCREF(Py_None);
3945 return Py_None;
3946 }
3947}
3948#endif /* HAVE_SETEUID */
3949
3950#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003951PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003952"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003953Set the current process's effective group id.");
3954
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003955static PyObject *
3956posix_setegid (PyObject *self, PyObject *args)
3957{
3958 int egid;
3959 if (!PyArg_ParseTuple(args, "i", &egid)) {
3960 return NULL;
3961 } else if (setegid(egid) < 0) {
3962 return posix_error();
3963 } else {
3964 Py_INCREF(Py_None);
3965 return Py_None;
3966 }
3967}
3968#endif /* HAVE_SETEGID */
3969
3970#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003971PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003972"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003973Set the current process's real and effective user ids.");
3974
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003975static PyObject *
3976posix_setreuid (PyObject *self, PyObject *args)
3977{
3978 int ruid, euid;
3979 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
3980 return NULL;
3981 } else if (setreuid(ruid, euid) < 0) {
3982 return posix_error();
3983 } else {
3984 Py_INCREF(Py_None);
3985 return Py_None;
3986 }
3987}
3988#endif /* HAVE_SETREUID */
3989
3990#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003991PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003992"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003993Set the current process's real and effective group ids.");
3994
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003995static PyObject *
3996posix_setregid (PyObject *self, PyObject *args)
3997{
3998 int rgid, egid;
3999 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4000 return NULL;
4001 } else if (setregid(rgid, egid) < 0) {
4002 return posix_error();
4003 } else {
4004 Py_INCREF(Py_None);
4005 return Py_None;
4006 }
4007}
4008#endif /* HAVE_SETREGID */
4009
Guido van Rossumb6775db1994-08-01 11:34:53 +00004010#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004011PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004012"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004013Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004014
Barry Warsaw53699e91996-12-10 23:23:01 +00004015static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004016posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004017{
4018 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004019 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004020 return NULL;
4021 if (setgid(gid) < 0)
4022 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004023 Py_INCREF(Py_None);
4024 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004025}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004026#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004027
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004028#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004029PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004030"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004031Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004032
4033static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004034posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004035{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004036 int i, len;
4037 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004038
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004039 if (!PySequence_Check(groups)) {
4040 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4041 return NULL;
4042 }
4043 len = PySequence_Size(groups);
4044 if (len > MAX_GROUPS) {
4045 PyErr_SetString(PyExc_ValueError, "too many groups");
4046 return NULL;
4047 }
4048 for(i = 0; i < len; i++) {
4049 PyObject *elem;
4050 elem = PySequence_GetItem(groups, i);
4051 if (!elem)
4052 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004053 if (!PyLong_Check(elem)) {
4054 PyErr_SetString(PyExc_TypeError,
4055 "groups must be integers");
4056 Py_DECREF(elem);
4057 return NULL;
4058 } else {
4059 unsigned long x = PyLong_AsUnsignedLong(elem);
4060 if (PyErr_Occurred()) {
4061 PyErr_SetString(PyExc_TypeError,
4062 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004063 Py_DECREF(elem);
4064 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004065 }
Georg Brandla13c2442005-11-22 19:30:31 +00004066 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004067 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004068 if (grouplist[i] != x) {
4069 PyErr_SetString(PyExc_TypeError,
4070 "group id too big");
4071 Py_DECREF(elem);
4072 return NULL;
4073 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004074 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004075 Py_DECREF(elem);
4076 }
4077
4078 if (setgroups(len, grouplist) < 0)
4079 return posix_error();
4080 Py_INCREF(Py_None);
4081 return Py_None;
4082}
4083#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004084
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004085#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4086static PyObject *
4087wait_helper(int pid, int status, struct rusage *ru)
4088{
4089 PyObject *result;
4090 static PyObject *struct_rusage;
4091
4092 if (pid == -1)
4093 return posix_error();
4094
4095 if (struct_rusage == NULL) {
4096 PyObject *m = PyImport_ImportModule("resource");
4097 if (m == NULL)
4098 return NULL;
4099 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4100 Py_DECREF(m);
4101 if (struct_rusage == NULL)
4102 return NULL;
4103 }
4104
4105 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4106 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4107 if (!result)
4108 return NULL;
4109
4110#ifndef doubletime
4111#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4112#endif
4113
4114 PyStructSequence_SET_ITEM(result, 0,
4115 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4116 PyStructSequence_SET_ITEM(result, 1,
4117 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4118#define SET_INT(result, index, value)\
4119 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4120 SET_INT(result, 2, ru->ru_maxrss);
4121 SET_INT(result, 3, ru->ru_ixrss);
4122 SET_INT(result, 4, ru->ru_idrss);
4123 SET_INT(result, 5, ru->ru_isrss);
4124 SET_INT(result, 6, ru->ru_minflt);
4125 SET_INT(result, 7, ru->ru_majflt);
4126 SET_INT(result, 8, ru->ru_nswap);
4127 SET_INT(result, 9, ru->ru_inblock);
4128 SET_INT(result, 10, ru->ru_oublock);
4129 SET_INT(result, 11, ru->ru_msgsnd);
4130 SET_INT(result, 12, ru->ru_msgrcv);
4131 SET_INT(result, 13, ru->ru_nsignals);
4132 SET_INT(result, 14, ru->ru_nvcsw);
4133 SET_INT(result, 15, ru->ru_nivcsw);
4134#undef SET_INT
4135
4136 if (PyErr_Occurred()) {
4137 Py_DECREF(result);
4138 return NULL;
4139 }
4140
4141 return Py_BuildValue("iiN", pid, status, result);
4142}
4143#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4144
4145#ifdef HAVE_WAIT3
4146PyDoc_STRVAR(posix_wait3__doc__,
4147"wait3(options) -> (pid, status, rusage)\n\n\
4148Wait for completion of a child process.");
4149
4150static PyObject *
4151posix_wait3(PyObject *self, PyObject *args)
4152{
4153 int pid, options;
4154 struct rusage ru;
4155 WAIT_TYPE status;
4156 WAIT_STATUS_INT(status) = 0;
4157
4158 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4159 return NULL;
4160
4161 Py_BEGIN_ALLOW_THREADS
4162 pid = wait3(&status, options, &ru);
4163 Py_END_ALLOW_THREADS
4164
4165 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4166}
4167#endif /* HAVE_WAIT3 */
4168
4169#ifdef HAVE_WAIT4
4170PyDoc_STRVAR(posix_wait4__doc__,
4171"wait4(pid, options) -> (pid, status, rusage)\n\n\
4172Wait for completion of a given child process.");
4173
4174static PyObject *
4175posix_wait4(PyObject *self, PyObject *args)
4176{
4177 int pid, options;
4178 struct rusage ru;
4179 WAIT_TYPE status;
4180 WAIT_STATUS_INT(status) = 0;
4181
4182 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4183 return NULL;
4184
4185 Py_BEGIN_ALLOW_THREADS
4186 pid = wait4(pid, &status, options, &ru);
4187 Py_END_ALLOW_THREADS
4188
4189 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4190}
4191#endif /* HAVE_WAIT4 */
4192
Guido van Rossumb6775db1994-08-01 11:34:53 +00004193#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004194PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004195"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004196Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004197
Barry Warsaw53699e91996-12-10 23:23:01 +00004198static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004199posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004200{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004201 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004202 WAIT_TYPE status;
4203 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004204
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004205 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004206 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004207 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004208 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004209 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004210 if (pid == -1)
4211 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004212
4213 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004214}
4215
Tim Petersab034fa2002-02-01 11:27:43 +00004216#elif defined(HAVE_CWAIT)
4217
4218/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004219PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004220"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004221"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004222
4223static PyObject *
4224posix_waitpid(PyObject *self, PyObject *args)
4225{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004226 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004227 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004228
4229 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4230 return NULL;
4231 Py_BEGIN_ALLOW_THREADS
4232 pid = _cwait(&status, pid, options);
4233 Py_END_ALLOW_THREADS
4234 if (pid == -1)
4235 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004236
4237 /* shift the status left a byte so this is more like the POSIX waitpid */
4238 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004239}
4240#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004241
Guido van Rossumad0ee831995-03-01 10:34:45 +00004242#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004243PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004244"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004245Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004246
Barry Warsaw53699e91996-12-10 23:23:01 +00004247static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004248posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004249{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004250 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004251 WAIT_TYPE status;
4252 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004253
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004254 Py_BEGIN_ALLOW_THREADS
4255 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004256 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004257 if (pid == -1)
4258 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004259
4260 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004261}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004262#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004263
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004264
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004265PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004266"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004267Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004268
Barry Warsaw53699e91996-12-10 23:23:01 +00004269static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004270posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004271{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004272#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004273 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004274#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004275#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004276 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004277#else
4278 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4279#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004280#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004281}
4282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004283
Guido van Rossumb6775db1994-08-01 11:34:53 +00004284#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004285PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004286"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004287Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004288
Barry Warsaw53699e91996-12-10 23:23:01 +00004289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004290posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004291{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004292 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004293 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004294 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004295 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004296 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004297
4298 if (!PyArg_ParseTuple(args, "et:readlink",
4299 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004300 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004301 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004302 if (v == NULL) {
4303 PyMem_Free(path);
4304 return NULL;
4305 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004306
4307 if (PyUnicode_Check(v)) {
4308 arg_is_unicode = 1;
4309 }
4310 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004311
Barry Warsaw53699e91996-12-10 23:23:01 +00004312 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004313 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004314 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004315 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004316 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004317
Neal Norwitzfca70052007-08-12 16:56:02 +00004318 PyMem_Free(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004319 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004320 if (arg_is_unicode) {
4321 PyObject *w;
4322
4323 w = PyUnicode_FromEncodedObject(v,
4324 Py_FileSystemDefaultEncoding,
4325 "strict");
4326 if (w != NULL) {
4327 Py_DECREF(v);
4328 v = w;
4329 }
4330 else {
4331 /* fall back to the original byte string, as
4332 discussed in patch #683592 */
4333 PyErr_Clear();
4334 }
4335 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004336 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004337}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004338#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004340
Guido van Rossumb6775db1994-08-01 11:34:53 +00004341#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004342PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004343"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004344Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004345
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004346static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004347posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004348{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004349 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004350}
4351#endif /* HAVE_SYMLINK */
4352
4353
4354#ifdef HAVE_TIMES
4355#ifndef HZ
4356#define HZ 60 /* Universal constant :-) */
4357#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004358
Guido van Rossumd48f2521997-12-05 22:19:34 +00004359#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4360static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004361system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004362{
4363 ULONG value = 0;
4364
4365 Py_BEGIN_ALLOW_THREADS
4366 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4367 Py_END_ALLOW_THREADS
4368
4369 return value;
4370}
4371
4372static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004373posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004374{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004375 /* Currently Only Uptime is Provided -- Others Later */
4376 return Py_BuildValue("ddddd",
4377 (double)0 /* t.tms_utime / HZ */,
4378 (double)0 /* t.tms_stime / HZ */,
4379 (double)0 /* t.tms_cutime / HZ */,
4380 (double)0 /* t.tms_cstime / HZ */,
4381 (double)system_uptime() / 1000);
4382}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004383#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004384static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004385posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004386{
4387 struct tms t;
4388 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004389 errno = 0;
4390 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004391 if (c == (clock_t) -1)
4392 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004393 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004394 (double)t.tms_utime / HZ,
4395 (double)t.tms_stime / HZ,
4396 (double)t.tms_cutime / HZ,
4397 (double)t.tms_cstime / HZ,
4398 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004399}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004400#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004401#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004402
4403
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004404#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004405#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004406static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004407posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004408{
4409 FILETIME create, exit, kernel, user;
4410 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004411 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004412 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4413 /* The fields of a FILETIME structure are the hi and lo part
4414 of a 64-bit value expressed in 100 nanosecond units.
4415 1e7 is one second in such units; 1e-7 the inverse.
4416 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4417 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004418 return Py_BuildValue(
4419 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004420 (double)(kernel.dwHighDateTime*429.4967296 +
4421 kernel.dwLowDateTime*1e-7),
4422 (double)(user.dwHighDateTime*429.4967296 +
4423 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004424 (double)0,
4425 (double)0,
4426 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004427}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004428#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004429
4430#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004432"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004433Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004434#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004435
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004436
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004437#ifdef HAVE_GETSID
4438PyDoc_STRVAR(posix_getsid__doc__,
4439"getsid(pid) -> sid\n\n\
4440Call the system call getsid().");
4441
4442static PyObject *
4443posix_getsid(PyObject *self, PyObject *args)
4444{
4445 int pid, sid;
4446 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4447 return NULL;
4448 sid = getsid(pid);
4449 if (sid < 0)
4450 return posix_error();
4451 return PyInt_FromLong((long)sid);
4452}
4453#endif /* HAVE_GETSID */
4454
4455
Guido van Rossumb6775db1994-08-01 11:34:53 +00004456#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004457PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004458"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004459Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004460
Barry Warsaw53699e91996-12-10 23:23:01 +00004461static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004462posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004463{
Guido van Rossum687dd131993-05-17 08:34:16 +00004464 if (setsid() < 0)
4465 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004466 Py_INCREF(Py_None);
4467 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004468}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004469#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004470
Guido van Rossumb6775db1994-08-01 11:34:53 +00004471#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004472PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004473"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004474Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004475
Barry Warsaw53699e91996-12-10 23:23:01 +00004476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004477posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004478{
4479 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004480 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004481 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004482 if (setpgid(pid, pgrp) < 0)
4483 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004484 Py_INCREF(Py_None);
4485 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004486}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004487#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004488
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004489
Guido van Rossumb6775db1994-08-01 11:34:53 +00004490#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004491PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004492"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004493Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004494
Barry Warsaw53699e91996-12-10 23:23:01 +00004495static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004496posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004497{
4498 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004499 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004500 return NULL;
4501 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004502 if (pgid < 0)
4503 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004504 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004505}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004506#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004507
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004508
Guido van Rossumb6775db1994-08-01 11:34:53 +00004509#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004510PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004511"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004512Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004513
Barry Warsaw53699e91996-12-10 23:23:01 +00004514static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004515posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004516{
4517 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004518 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004519 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004520 if (tcsetpgrp(fd, pgid) < 0)
4521 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004522 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004523 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004524}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004525#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004526
Guido van Rossum687dd131993-05-17 08:34:16 +00004527/* Functions acting on file descriptors */
4528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004529PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004530"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004531Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004532
Barry Warsaw53699e91996-12-10 23:23:01 +00004533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004534posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004535{
Mark Hammondef8b6542001-05-13 08:04:26 +00004536 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004537 int flag;
4538 int mode = 0777;
4539 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004540
4541#ifdef MS_WINDOWS
4542 if (unicode_file_names()) {
4543 PyUnicodeObject *po;
4544 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4545 Py_BEGIN_ALLOW_THREADS
4546 /* PyUnicode_AS_UNICODE OK without thread
4547 lock as it is a simple dereference. */
4548 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4549 Py_END_ALLOW_THREADS
4550 if (fd < 0)
4551 return posix_error();
4552 return PyInt_FromLong((long)fd);
4553 }
4554 /* Drop the argument parsing error as narrow strings
4555 are also valid. */
4556 PyErr_Clear();
4557 }
4558#endif
4559
Tim Peters5aa91602002-01-30 05:46:57 +00004560 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004561 Py_FileSystemDefaultEncoding, &file,
4562 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004563 return NULL;
4564
Barry Warsaw53699e91996-12-10 23:23:01 +00004565 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004566 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004567 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004568 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004569 return posix_error_with_allocated_filename(file);
4570 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004571 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004572}
4573
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004575PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004576"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004577Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004578
Barry Warsaw53699e91996-12-10 23:23:01 +00004579static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004580posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004581{
4582 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004583 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004584 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004585 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004586 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004587 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004588 if (res < 0)
4589 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004590 Py_INCREF(Py_None);
4591 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004592}
4593
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004595PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004596"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004597Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004598
Barry Warsaw53699e91996-12-10 23:23:01 +00004599static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004600posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004601{
4602 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004603 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004604 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004605 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004606 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004607 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004608 if (fd < 0)
4609 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004610 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004611}
4612
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004614PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004615"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004616Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004617
Barry Warsaw53699e91996-12-10 23:23:01 +00004618static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004619posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004620{
4621 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004622 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004623 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004624 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004625 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004626 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004627 if (res < 0)
4628 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004629 Py_INCREF(Py_None);
4630 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004631}
4632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004634PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004635"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004636Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004637
Barry Warsaw53699e91996-12-10 23:23:01 +00004638static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004639posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004640{
4641 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004642#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004643 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004644#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004645 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004646#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004647 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004648 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004649 return NULL;
4650#ifdef SEEK_SET
4651 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4652 switch (how) {
4653 case 0: how = SEEK_SET; break;
4654 case 1: how = SEEK_CUR; break;
4655 case 2: how = SEEK_END; break;
4656 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004657#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004658
4659#if !defined(HAVE_LARGEFILE_SUPPORT)
4660 pos = PyInt_AsLong(posobj);
4661#else
4662 pos = PyLong_Check(posobj) ?
4663 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4664#endif
4665 if (PyErr_Occurred())
4666 return NULL;
4667
Barry Warsaw53699e91996-12-10 23:23:01 +00004668 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004669#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004670 res = _lseeki64(fd, pos, how);
4671#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004672 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004673#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004674 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004675 if (res < 0)
4676 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004677
4678#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004679 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004680#else
4681 return PyLong_FromLongLong(res);
4682#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004683}
4684
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004686PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004687"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004688Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004689
Barry Warsaw53699e91996-12-10 23:23:01 +00004690static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004691posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004692{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004693 int fd, size;
4694 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004695 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004696 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004697 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004698 if (size < 0) {
4699 errno = EINVAL;
4700 return posix_error();
4701 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00004702 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004703 if (buffer == NULL)
4704 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004705 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00004706 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004707 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004708 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004709 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004710 return posix_error();
4711 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004712 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00004713 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004714 return buffer;
4715}
4716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004718PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004719"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004720Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004721
Barry Warsaw53699e91996-12-10 23:23:01 +00004722static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004723posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004724{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004725 int fd;
4726 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004727 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004728
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004729 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004730 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004731 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004732 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004733 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004734 if (size < 0)
4735 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004736 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004737}
4738
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004740PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004741"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004742Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004743
Barry Warsaw53699e91996-12-10 23:23:01 +00004744static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004745posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004746{
4747 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004748 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004749 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004750 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004751 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004752#ifdef __VMS
4753 /* on OpenVMS we must ensure that all bytes are written to the file */
4754 fsync(fd);
4755#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004756 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004757 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004758 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004759 if (res != 0) {
4760#ifdef MS_WINDOWS
4761 return win32_error("fstat", NULL);
4762#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004763 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004764#endif
4765 }
Tim Peters5aa91602002-01-30 05:46:57 +00004766
Martin v. Löwis14694662006-02-03 12:54:16 +00004767 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004768}
4769
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004770PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004771"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004772Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004773connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004774
4775static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004776posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004777{
4778 int fd;
4779 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4780 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004781 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004782}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004783
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004784#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004785PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004786"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004787Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004788
Barry Warsaw53699e91996-12-10 23:23:01 +00004789static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004790posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004791{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004792#if defined(PYOS_OS2)
4793 HFILE read, write;
4794 APIRET rc;
4795
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004796 Py_BEGIN_ALLOW_THREADS
4797 rc = DosCreatePipe( &read, &write, 4096);
4798 Py_END_ALLOW_THREADS
4799 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004800 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004801
4802 return Py_BuildValue("(ii)", read, write);
4803#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004804#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004805 int fds[2];
4806 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004807 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004808 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004809 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004810 if (res != 0)
4811 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004812 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004813#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004814 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004815 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004816 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004817 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004818 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004819 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004820 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004821 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004822 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4823 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004824 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004825#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004826#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004827}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004828#endif /* HAVE_PIPE */
4829
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004830
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004831#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004832PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004833"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004834Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004835
Barry Warsaw53699e91996-12-10 23:23:01 +00004836static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004837posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004838{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004839 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004840 int mode = 0666;
4841 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004842 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004843 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004844 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004845 res = mkfifo(filename, mode);
4846 Py_END_ALLOW_THREADS
4847 if (res < 0)
4848 return posix_error();
4849 Py_INCREF(Py_None);
4850 return Py_None;
4851}
4852#endif
4853
4854
Neal Norwitz11690112002-07-30 01:08:28 +00004855#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004856PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004857"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004858Create a filesystem node (file, device special file or named pipe)\n\
4859named filename. mode specifies both the permissions to use and the\n\
4860type of node to be created, being combined (bitwise OR) with one of\n\
4861S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004862device defines the newly created device special file (probably using\n\
4863os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004864
4865
4866static PyObject *
4867posix_mknod(PyObject *self, PyObject *args)
4868{
4869 char *filename;
4870 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004871 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004872 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00004873 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004874 return NULL;
4875 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004876 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00004877 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004878 if (res < 0)
4879 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004880 Py_INCREF(Py_None);
4881 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004882}
4883#endif
4884
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004885#ifdef HAVE_DEVICE_MACROS
4886PyDoc_STRVAR(posix_major__doc__,
4887"major(device) -> major number\n\
4888Extracts a device major number from a raw device number.");
4889
4890static PyObject *
4891posix_major(PyObject *self, PyObject *args)
4892{
4893 int device;
4894 if (!PyArg_ParseTuple(args, "i:major", &device))
4895 return NULL;
4896 return PyInt_FromLong((long)major(device));
4897}
4898
4899PyDoc_STRVAR(posix_minor__doc__,
4900"minor(device) -> minor number\n\
4901Extracts a device minor number from a raw device number.");
4902
4903static PyObject *
4904posix_minor(PyObject *self, PyObject *args)
4905{
4906 int device;
4907 if (!PyArg_ParseTuple(args, "i:minor", &device))
4908 return NULL;
4909 return PyInt_FromLong((long)minor(device));
4910}
4911
4912PyDoc_STRVAR(posix_makedev__doc__,
4913"makedev(major, minor) -> device number\n\
4914Composes a raw device number from the major and minor device numbers.");
4915
4916static PyObject *
4917posix_makedev(PyObject *self, PyObject *args)
4918{
4919 int major, minor;
4920 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
4921 return NULL;
4922 return PyInt_FromLong((long)makedev(major, minor));
4923}
4924#endif /* device macros */
4925
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004926
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004927#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004928PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004929"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004930Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004931
Barry Warsaw53699e91996-12-10 23:23:01 +00004932static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004933posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004934{
4935 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004936 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004937 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004938 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004939
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004940 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004941 return NULL;
4942
4943#if !defined(HAVE_LARGEFILE_SUPPORT)
4944 length = PyInt_AsLong(lenobj);
4945#else
4946 length = PyLong_Check(lenobj) ?
4947 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
4948#endif
4949 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004950 return NULL;
4951
Barry Warsaw53699e91996-12-10 23:23:01 +00004952 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004953 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00004954 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004955 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004956 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004957 return NULL;
4958 }
Barry Warsaw53699e91996-12-10 23:23:01 +00004959 Py_INCREF(Py_None);
4960 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004961}
4962#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004963
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004964#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004965PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004966"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004967Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004968
Fred Drake762e2061999-08-26 17:23:54 +00004969/* Save putenv() parameters as values here, so we can collect them when they
4970 * get re-set with another call for the same key. */
4971static PyObject *posix_putenv_garbage;
4972
Tim Peters5aa91602002-01-30 05:46:57 +00004973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004974posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004975{
4976 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004977 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00004978 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00004979 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004980
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004981 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004982 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00004983
4984#if defined(PYOS_OS2)
4985 if (stricmp(s1, "BEGINLIBPATH") == 0) {
4986 APIRET rc;
4987
Guido van Rossumd48f2521997-12-05 22:19:34 +00004988 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
4989 if (rc != NO_ERROR)
4990 return os2_error(rc);
4991
4992 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
4993 APIRET rc;
4994
Guido van Rossumd48f2521997-12-05 22:19:34 +00004995 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
4996 if (rc != NO_ERROR)
4997 return os2_error(rc);
4998 } else {
4999#endif
5000
Fred Drake762e2061999-08-26 17:23:54 +00005001 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005002 len = strlen(s1) + strlen(s2) + 2;
5003 /* len includes space for a trailing \0; the size arg to
5004 PyString_FromStringAndSize does not count that */
5005 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005006 if (newstr == NULL)
5007 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005008 newenv = PyString_AS_STRING(newstr);
5009 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5010 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005011 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005012 posix_error();
5013 return NULL;
5014 }
Fred Drake762e2061999-08-26 17:23:54 +00005015 /* Install the first arg and newstr in posix_putenv_garbage;
5016 * this will cause previous value to be collected. This has to
5017 * happen after the real putenv() call because the old value
5018 * was still accessible until then. */
5019 if (PyDict_SetItem(posix_putenv_garbage,
5020 PyTuple_GET_ITEM(args, 0), newstr)) {
5021 /* really not much we can do; just leak */
5022 PyErr_Clear();
5023 }
5024 else {
5025 Py_DECREF(newstr);
5026 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005027
5028#if defined(PYOS_OS2)
5029 }
5030#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005031 Py_INCREF(Py_None);
5032 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005033}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005034#endif /* putenv */
5035
Guido van Rossumc524d952001-10-19 01:31:59 +00005036#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005037PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005038"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005039Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005040
5041static PyObject *
5042posix_unsetenv(PyObject *self, PyObject *args)
5043{
5044 char *s1;
5045
5046 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5047 return NULL;
5048
5049 unsetenv(s1);
5050
5051 /* Remove the key from posix_putenv_garbage;
5052 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005053 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005054 * old value was still accessible until then.
5055 */
5056 if (PyDict_DelItem(posix_putenv_garbage,
5057 PyTuple_GET_ITEM(args, 0))) {
5058 /* really not much we can do; just leak */
5059 PyErr_Clear();
5060 }
5061
5062 Py_INCREF(Py_None);
5063 return Py_None;
5064}
5065#endif /* unsetenv */
5066
Guido van Rossumb6a47161997-09-15 22:54:34 +00005067#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005068PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005069"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005070Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005071
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005072static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005073posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005074{
5075 int code;
5076 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005077 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005078 return NULL;
5079 message = strerror(code);
5080 if (message == NULL) {
5081 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005082 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005083 return NULL;
5084 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005085 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005086}
5087#endif /* strerror */
5088
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005089
Guido van Rossumc9641791998-08-04 15:26:23 +00005090#ifdef HAVE_SYS_WAIT_H
5091
Fred Drake106c1a02002-04-23 15:58:02 +00005092#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005093PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005094"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005095Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005096
5097static PyObject *
5098posix_WCOREDUMP(PyObject *self, PyObject *args)
5099{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005100 WAIT_TYPE status;
5101 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005102
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005103 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005104 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005105
5106 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005107}
5108#endif /* WCOREDUMP */
5109
5110#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005111PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005112"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005113Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005114job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005115
5116static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005117posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005118{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005119 WAIT_TYPE status;
5120 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005121
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005122 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005123 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005124
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005125 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005126}
5127#endif /* WIFCONTINUED */
5128
Guido van Rossumc9641791998-08-04 15:26:23 +00005129#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005130PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005131"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005132Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005133
5134static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005135posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005136{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005137 WAIT_TYPE status;
5138 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005139
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005140 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005141 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005142
Fred Drake106c1a02002-04-23 15:58:02 +00005143 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005144}
5145#endif /* WIFSTOPPED */
5146
5147#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005148PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005149"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005150Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005151
5152static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005153posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005154{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005155 WAIT_TYPE status;
5156 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005157
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005158 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005159 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005160
Fred Drake106c1a02002-04-23 15:58:02 +00005161 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005162}
5163#endif /* WIFSIGNALED */
5164
5165#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005166PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005167"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005168Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005169system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005170
5171static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005172posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005173{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005174 WAIT_TYPE status;
5175 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005176
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005177 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005178 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005179
Fred Drake106c1a02002-04-23 15:58:02 +00005180 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005181}
5182#endif /* WIFEXITED */
5183
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005184#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005185PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005186"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005187Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005188
5189static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005190posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005191{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005192 WAIT_TYPE status;
5193 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005194
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005195 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005196 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005197
Guido van Rossumc9641791998-08-04 15:26:23 +00005198 return Py_BuildValue("i", WEXITSTATUS(status));
5199}
5200#endif /* WEXITSTATUS */
5201
5202#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005203PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005204"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005205Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005206value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005207
5208static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005209posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005210{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005211 WAIT_TYPE status;
5212 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005213
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005214 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005215 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005216
Guido van Rossumc9641791998-08-04 15:26:23 +00005217 return Py_BuildValue("i", WTERMSIG(status));
5218}
5219#endif /* WTERMSIG */
5220
5221#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005222PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005223"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224Return the signal that stopped the process that provided\n\
5225the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005226
5227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005228posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005229{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005230 WAIT_TYPE status;
5231 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005232
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005233 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005234 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005235
Guido van Rossumc9641791998-08-04 15:26:23 +00005236 return Py_BuildValue("i", WSTOPSIG(status));
5237}
5238#endif /* WSTOPSIG */
5239
5240#endif /* HAVE_SYS_WAIT_H */
5241
5242
Thomas Wouters477c8d52006-05-27 19:21:47 +00005243#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005244#ifdef _SCO_DS
5245/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5246 needed definitions in sys/statvfs.h */
5247#define _SVID3
5248#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005249#include <sys/statvfs.h>
5250
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005251static PyObject*
5252_pystatvfs_fromstructstatvfs(struct statvfs st) {
5253 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5254 if (v == NULL)
5255 return NULL;
5256
5257#if !defined(HAVE_LARGEFILE_SUPPORT)
5258 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5259 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5260 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5261 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5262 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5263 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5264 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5265 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5266 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5267 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5268#else
5269 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5270 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005271 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005272 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005273 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005274 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005275 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005276 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005277 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005278 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005279 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005280 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005281 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005282 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005283 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5284 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5285#endif
5286
5287 return v;
5288}
5289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005290PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005291"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005292Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005293
5294static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005295posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005296{
5297 int fd, res;
5298 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005299
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005300 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005301 return NULL;
5302 Py_BEGIN_ALLOW_THREADS
5303 res = fstatvfs(fd, &st);
5304 Py_END_ALLOW_THREADS
5305 if (res != 0)
5306 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005307
5308 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005309}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005310#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005311
5312
Thomas Wouters477c8d52006-05-27 19:21:47 +00005313#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005314#include <sys/statvfs.h>
5315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005316PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005317"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005318Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005319
5320static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005321posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005322{
5323 char *path;
5324 int res;
5325 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005326 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005327 return NULL;
5328 Py_BEGIN_ALLOW_THREADS
5329 res = statvfs(path, &st);
5330 Py_END_ALLOW_THREADS
5331 if (res != 0)
5332 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005333
5334 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005335}
5336#endif /* HAVE_STATVFS */
5337
Fred Drakec9680921999-12-13 16:37:25 +00005338/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5339 * It maps strings representing configuration variable names to
5340 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005341 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005342 * rarely-used constants. There are three separate tables that use
5343 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005344 *
5345 * This code is always included, even if none of the interfaces that
5346 * need it are included. The #if hackery needed to avoid it would be
5347 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005348 */
5349struct constdef {
5350 char *name;
5351 long value;
5352};
5353
Fred Drake12c6e2d1999-12-14 21:25:03 +00005354static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005355conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005356 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005357{
5358 if (PyInt_Check(arg)) {
5359 *valuep = PyInt_AS_LONG(arg);
5360 return 1;
5361 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005362 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005363 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005364 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005365 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005366 size_t hi = tablesize;
5367 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005368 const char *confname;
5369 Py_ssize_t namelen;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005370 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005371 PyErr_SetString(PyExc_TypeError,
5372 "configuration names must be strings or integers");
5373 return 0;
5374 }
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005375 confname = PyUnicode_AsString(arg);
5376 if (confname == NULL)
5377 return 0;
5378 namelen = strlen(confname);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005379 while (lo < hi) {
5380 mid = (lo + hi) / 2;
5381 cmp = strcmp(confname, table[mid].name);
5382 if (cmp < 0)
5383 hi = mid;
5384 else if (cmp > 0)
5385 lo = mid + 1;
5386 else {
5387 *valuep = table[mid].value;
5388 return 1;
5389 }
5390 }
5391 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005392 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005393 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005394}
5395
5396
5397#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5398static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005399#ifdef _PC_ABI_AIO_XFER_MAX
5400 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5401#endif
5402#ifdef _PC_ABI_ASYNC_IO
5403 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5404#endif
Fred Drakec9680921999-12-13 16:37:25 +00005405#ifdef _PC_ASYNC_IO
5406 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5407#endif
5408#ifdef _PC_CHOWN_RESTRICTED
5409 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5410#endif
5411#ifdef _PC_FILESIZEBITS
5412 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5413#endif
5414#ifdef _PC_LAST
5415 {"PC_LAST", _PC_LAST},
5416#endif
5417#ifdef _PC_LINK_MAX
5418 {"PC_LINK_MAX", _PC_LINK_MAX},
5419#endif
5420#ifdef _PC_MAX_CANON
5421 {"PC_MAX_CANON", _PC_MAX_CANON},
5422#endif
5423#ifdef _PC_MAX_INPUT
5424 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5425#endif
5426#ifdef _PC_NAME_MAX
5427 {"PC_NAME_MAX", _PC_NAME_MAX},
5428#endif
5429#ifdef _PC_NO_TRUNC
5430 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5431#endif
5432#ifdef _PC_PATH_MAX
5433 {"PC_PATH_MAX", _PC_PATH_MAX},
5434#endif
5435#ifdef _PC_PIPE_BUF
5436 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5437#endif
5438#ifdef _PC_PRIO_IO
5439 {"PC_PRIO_IO", _PC_PRIO_IO},
5440#endif
5441#ifdef _PC_SOCK_MAXBUF
5442 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5443#endif
5444#ifdef _PC_SYNC_IO
5445 {"PC_SYNC_IO", _PC_SYNC_IO},
5446#endif
5447#ifdef _PC_VDISABLE
5448 {"PC_VDISABLE", _PC_VDISABLE},
5449#endif
5450};
5451
Fred Drakec9680921999-12-13 16:37:25 +00005452static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005453conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005454{
5455 return conv_confname(arg, valuep, posix_constants_pathconf,
5456 sizeof(posix_constants_pathconf)
5457 / sizeof(struct constdef));
5458}
5459#endif
5460
5461#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005462PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005463"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005464Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005465If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005466
5467static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005468posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005469{
5470 PyObject *result = NULL;
5471 int name, fd;
5472
Fred Drake12c6e2d1999-12-14 21:25:03 +00005473 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5474 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005475 long limit;
5476
5477 errno = 0;
5478 limit = fpathconf(fd, name);
5479 if (limit == -1 && errno != 0)
5480 posix_error();
5481 else
5482 result = PyInt_FromLong(limit);
5483 }
5484 return result;
5485}
5486#endif
5487
5488
5489#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005490PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005491"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005492Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005493If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005494
5495static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005496posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005497{
5498 PyObject *result = NULL;
5499 int name;
5500 char *path;
5501
5502 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5503 conv_path_confname, &name)) {
5504 long limit;
5505
5506 errno = 0;
5507 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005508 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005509 if (errno == EINVAL)
5510 /* could be a path or name problem */
5511 posix_error();
5512 else
5513 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005514 }
Fred Drakec9680921999-12-13 16:37:25 +00005515 else
5516 result = PyInt_FromLong(limit);
5517 }
5518 return result;
5519}
5520#endif
5521
5522#ifdef HAVE_CONFSTR
5523static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005524#ifdef _CS_ARCHITECTURE
5525 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5526#endif
5527#ifdef _CS_HOSTNAME
5528 {"CS_HOSTNAME", _CS_HOSTNAME},
5529#endif
5530#ifdef _CS_HW_PROVIDER
5531 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5532#endif
5533#ifdef _CS_HW_SERIAL
5534 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5535#endif
5536#ifdef _CS_INITTAB_NAME
5537 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5538#endif
Fred Drakec9680921999-12-13 16:37:25 +00005539#ifdef _CS_LFS64_CFLAGS
5540 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5541#endif
5542#ifdef _CS_LFS64_LDFLAGS
5543 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5544#endif
5545#ifdef _CS_LFS64_LIBS
5546 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5547#endif
5548#ifdef _CS_LFS64_LINTFLAGS
5549 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5550#endif
5551#ifdef _CS_LFS_CFLAGS
5552 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5553#endif
5554#ifdef _CS_LFS_LDFLAGS
5555 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5556#endif
5557#ifdef _CS_LFS_LIBS
5558 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5559#endif
5560#ifdef _CS_LFS_LINTFLAGS
5561 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5562#endif
Fred Draked86ed291999-12-15 15:34:33 +00005563#ifdef _CS_MACHINE
5564 {"CS_MACHINE", _CS_MACHINE},
5565#endif
Fred Drakec9680921999-12-13 16:37:25 +00005566#ifdef _CS_PATH
5567 {"CS_PATH", _CS_PATH},
5568#endif
Fred Draked86ed291999-12-15 15:34:33 +00005569#ifdef _CS_RELEASE
5570 {"CS_RELEASE", _CS_RELEASE},
5571#endif
5572#ifdef _CS_SRPC_DOMAIN
5573 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5574#endif
5575#ifdef _CS_SYSNAME
5576 {"CS_SYSNAME", _CS_SYSNAME},
5577#endif
5578#ifdef _CS_VERSION
5579 {"CS_VERSION", _CS_VERSION},
5580#endif
Fred Drakec9680921999-12-13 16:37:25 +00005581#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5582 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5583#endif
5584#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5585 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5586#endif
5587#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5588 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5589#endif
5590#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5591 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5592#endif
5593#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5594 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5595#endif
5596#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5597 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5598#endif
5599#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5600 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5601#endif
5602#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5603 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5604#endif
5605#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5606 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5607#endif
5608#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5609 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5610#endif
5611#ifdef _CS_XBS5_LP64_OFF64_LIBS
5612 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5613#endif
5614#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5615 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5616#endif
5617#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5618 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5619#endif
5620#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5621 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5622#endif
5623#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5624 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5625#endif
5626#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5627 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5628#endif
Fred Draked86ed291999-12-15 15:34:33 +00005629#ifdef _MIPS_CS_AVAIL_PROCESSORS
5630 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5631#endif
5632#ifdef _MIPS_CS_BASE
5633 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5634#endif
5635#ifdef _MIPS_CS_HOSTID
5636 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5637#endif
5638#ifdef _MIPS_CS_HW_NAME
5639 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5640#endif
5641#ifdef _MIPS_CS_NUM_PROCESSORS
5642 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5643#endif
5644#ifdef _MIPS_CS_OSREL_MAJ
5645 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5646#endif
5647#ifdef _MIPS_CS_OSREL_MIN
5648 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5649#endif
5650#ifdef _MIPS_CS_OSREL_PATCH
5651 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5652#endif
5653#ifdef _MIPS_CS_OS_NAME
5654 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5655#endif
5656#ifdef _MIPS_CS_OS_PROVIDER
5657 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5658#endif
5659#ifdef _MIPS_CS_PROCESSORS
5660 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5661#endif
5662#ifdef _MIPS_CS_SERIAL
5663 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5664#endif
5665#ifdef _MIPS_CS_VENDOR
5666 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5667#endif
Fred Drakec9680921999-12-13 16:37:25 +00005668};
5669
5670static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005671conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005672{
5673 return conv_confname(arg, valuep, posix_constants_confstr,
5674 sizeof(posix_constants_confstr)
5675 / sizeof(struct constdef));
5676}
5677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005678PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005679"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005680Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005681
5682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005683posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005684{
5685 PyObject *result = NULL;
5686 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005687 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005688
5689 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005690 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005691
Fred Drakec9680921999-12-13 16:37:25 +00005692 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005693 len = confstr(name, buffer, sizeof(buffer));
5694 if (len == 0) {
5695 if (errno) {
5696 posix_error();
5697 }
5698 else {
5699 result = Py_None;
5700 Py_INCREF(Py_None);
5701 }
Fred Drakec9680921999-12-13 16:37:25 +00005702 }
5703 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005704 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005705 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005706 if (result != NULL)
Neal Norwitz93c56822007-08-26 07:10:06 +00005707 confstr(name, PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005708 }
5709 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005710 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005711 }
5712 }
5713 return result;
5714}
5715#endif
5716
5717
5718#ifdef HAVE_SYSCONF
5719static struct constdef posix_constants_sysconf[] = {
5720#ifdef _SC_2_CHAR_TERM
5721 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5722#endif
5723#ifdef _SC_2_C_BIND
5724 {"SC_2_C_BIND", _SC_2_C_BIND},
5725#endif
5726#ifdef _SC_2_C_DEV
5727 {"SC_2_C_DEV", _SC_2_C_DEV},
5728#endif
5729#ifdef _SC_2_C_VERSION
5730 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5731#endif
5732#ifdef _SC_2_FORT_DEV
5733 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5734#endif
5735#ifdef _SC_2_FORT_RUN
5736 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5737#endif
5738#ifdef _SC_2_LOCALEDEF
5739 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5740#endif
5741#ifdef _SC_2_SW_DEV
5742 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5743#endif
5744#ifdef _SC_2_UPE
5745 {"SC_2_UPE", _SC_2_UPE},
5746#endif
5747#ifdef _SC_2_VERSION
5748 {"SC_2_VERSION", _SC_2_VERSION},
5749#endif
Fred Draked86ed291999-12-15 15:34:33 +00005750#ifdef _SC_ABI_ASYNCHRONOUS_IO
5751 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5752#endif
5753#ifdef _SC_ACL
5754 {"SC_ACL", _SC_ACL},
5755#endif
Fred Drakec9680921999-12-13 16:37:25 +00005756#ifdef _SC_AIO_LISTIO_MAX
5757 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5758#endif
Fred Drakec9680921999-12-13 16:37:25 +00005759#ifdef _SC_AIO_MAX
5760 {"SC_AIO_MAX", _SC_AIO_MAX},
5761#endif
5762#ifdef _SC_AIO_PRIO_DELTA_MAX
5763 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5764#endif
5765#ifdef _SC_ARG_MAX
5766 {"SC_ARG_MAX", _SC_ARG_MAX},
5767#endif
5768#ifdef _SC_ASYNCHRONOUS_IO
5769 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5770#endif
5771#ifdef _SC_ATEXIT_MAX
5772 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5773#endif
Fred Draked86ed291999-12-15 15:34:33 +00005774#ifdef _SC_AUDIT
5775 {"SC_AUDIT", _SC_AUDIT},
5776#endif
Fred Drakec9680921999-12-13 16:37:25 +00005777#ifdef _SC_AVPHYS_PAGES
5778 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5779#endif
5780#ifdef _SC_BC_BASE_MAX
5781 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5782#endif
5783#ifdef _SC_BC_DIM_MAX
5784 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5785#endif
5786#ifdef _SC_BC_SCALE_MAX
5787 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5788#endif
5789#ifdef _SC_BC_STRING_MAX
5790 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5791#endif
Fred Draked86ed291999-12-15 15:34:33 +00005792#ifdef _SC_CAP
5793 {"SC_CAP", _SC_CAP},
5794#endif
Fred Drakec9680921999-12-13 16:37:25 +00005795#ifdef _SC_CHARCLASS_NAME_MAX
5796 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5797#endif
5798#ifdef _SC_CHAR_BIT
5799 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5800#endif
5801#ifdef _SC_CHAR_MAX
5802 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5803#endif
5804#ifdef _SC_CHAR_MIN
5805 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5806#endif
5807#ifdef _SC_CHILD_MAX
5808 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5809#endif
5810#ifdef _SC_CLK_TCK
5811 {"SC_CLK_TCK", _SC_CLK_TCK},
5812#endif
5813#ifdef _SC_COHER_BLKSZ
5814 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5815#endif
5816#ifdef _SC_COLL_WEIGHTS_MAX
5817 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5818#endif
5819#ifdef _SC_DCACHE_ASSOC
5820 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5821#endif
5822#ifdef _SC_DCACHE_BLKSZ
5823 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5824#endif
5825#ifdef _SC_DCACHE_LINESZ
5826 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
5827#endif
5828#ifdef _SC_DCACHE_SZ
5829 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
5830#endif
5831#ifdef _SC_DCACHE_TBLKSZ
5832 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
5833#endif
5834#ifdef _SC_DELAYTIMER_MAX
5835 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
5836#endif
5837#ifdef _SC_EQUIV_CLASS_MAX
5838 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
5839#endif
5840#ifdef _SC_EXPR_NEST_MAX
5841 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
5842#endif
5843#ifdef _SC_FSYNC
5844 {"SC_FSYNC", _SC_FSYNC},
5845#endif
5846#ifdef _SC_GETGR_R_SIZE_MAX
5847 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
5848#endif
5849#ifdef _SC_GETPW_R_SIZE_MAX
5850 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
5851#endif
5852#ifdef _SC_ICACHE_ASSOC
5853 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
5854#endif
5855#ifdef _SC_ICACHE_BLKSZ
5856 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
5857#endif
5858#ifdef _SC_ICACHE_LINESZ
5859 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
5860#endif
5861#ifdef _SC_ICACHE_SZ
5862 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
5863#endif
Fred Draked86ed291999-12-15 15:34:33 +00005864#ifdef _SC_INF
5865 {"SC_INF", _SC_INF},
5866#endif
Fred Drakec9680921999-12-13 16:37:25 +00005867#ifdef _SC_INT_MAX
5868 {"SC_INT_MAX", _SC_INT_MAX},
5869#endif
5870#ifdef _SC_INT_MIN
5871 {"SC_INT_MIN", _SC_INT_MIN},
5872#endif
5873#ifdef _SC_IOV_MAX
5874 {"SC_IOV_MAX", _SC_IOV_MAX},
5875#endif
Fred Draked86ed291999-12-15 15:34:33 +00005876#ifdef _SC_IP_SECOPTS
5877 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
5878#endif
Fred Drakec9680921999-12-13 16:37:25 +00005879#ifdef _SC_JOB_CONTROL
5880 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
5881#endif
Fred Draked86ed291999-12-15 15:34:33 +00005882#ifdef _SC_KERN_POINTERS
5883 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
5884#endif
5885#ifdef _SC_KERN_SIM
5886 {"SC_KERN_SIM", _SC_KERN_SIM},
5887#endif
Fred Drakec9680921999-12-13 16:37:25 +00005888#ifdef _SC_LINE_MAX
5889 {"SC_LINE_MAX", _SC_LINE_MAX},
5890#endif
5891#ifdef _SC_LOGIN_NAME_MAX
5892 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
5893#endif
5894#ifdef _SC_LOGNAME_MAX
5895 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
5896#endif
5897#ifdef _SC_LONG_BIT
5898 {"SC_LONG_BIT", _SC_LONG_BIT},
5899#endif
Fred Draked86ed291999-12-15 15:34:33 +00005900#ifdef _SC_MAC
5901 {"SC_MAC", _SC_MAC},
5902#endif
Fred Drakec9680921999-12-13 16:37:25 +00005903#ifdef _SC_MAPPED_FILES
5904 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
5905#endif
5906#ifdef _SC_MAXPID
5907 {"SC_MAXPID", _SC_MAXPID},
5908#endif
5909#ifdef _SC_MB_LEN_MAX
5910 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
5911#endif
5912#ifdef _SC_MEMLOCK
5913 {"SC_MEMLOCK", _SC_MEMLOCK},
5914#endif
5915#ifdef _SC_MEMLOCK_RANGE
5916 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
5917#endif
5918#ifdef _SC_MEMORY_PROTECTION
5919 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
5920#endif
5921#ifdef _SC_MESSAGE_PASSING
5922 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
5923#endif
Fred Draked86ed291999-12-15 15:34:33 +00005924#ifdef _SC_MMAP_FIXED_ALIGNMENT
5925 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
5926#endif
Fred Drakec9680921999-12-13 16:37:25 +00005927#ifdef _SC_MQ_OPEN_MAX
5928 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
5929#endif
5930#ifdef _SC_MQ_PRIO_MAX
5931 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
5932#endif
Fred Draked86ed291999-12-15 15:34:33 +00005933#ifdef _SC_NACLS_MAX
5934 {"SC_NACLS_MAX", _SC_NACLS_MAX},
5935#endif
Fred Drakec9680921999-12-13 16:37:25 +00005936#ifdef _SC_NGROUPS_MAX
5937 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
5938#endif
5939#ifdef _SC_NL_ARGMAX
5940 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
5941#endif
5942#ifdef _SC_NL_LANGMAX
5943 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
5944#endif
5945#ifdef _SC_NL_MSGMAX
5946 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
5947#endif
5948#ifdef _SC_NL_NMAX
5949 {"SC_NL_NMAX", _SC_NL_NMAX},
5950#endif
5951#ifdef _SC_NL_SETMAX
5952 {"SC_NL_SETMAX", _SC_NL_SETMAX},
5953#endif
5954#ifdef _SC_NL_TEXTMAX
5955 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
5956#endif
5957#ifdef _SC_NPROCESSORS_CONF
5958 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
5959#endif
5960#ifdef _SC_NPROCESSORS_ONLN
5961 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
5962#endif
Fred Draked86ed291999-12-15 15:34:33 +00005963#ifdef _SC_NPROC_CONF
5964 {"SC_NPROC_CONF", _SC_NPROC_CONF},
5965#endif
5966#ifdef _SC_NPROC_ONLN
5967 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
5968#endif
Fred Drakec9680921999-12-13 16:37:25 +00005969#ifdef _SC_NZERO
5970 {"SC_NZERO", _SC_NZERO},
5971#endif
5972#ifdef _SC_OPEN_MAX
5973 {"SC_OPEN_MAX", _SC_OPEN_MAX},
5974#endif
5975#ifdef _SC_PAGESIZE
5976 {"SC_PAGESIZE", _SC_PAGESIZE},
5977#endif
5978#ifdef _SC_PAGE_SIZE
5979 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
5980#endif
5981#ifdef _SC_PASS_MAX
5982 {"SC_PASS_MAX", _SC_PASS_MAX},
5983#endif
5984#ifdef _SC_PHYS_PAGES
5985 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
5986#endif
5987#ifdef _SC_PII
5988 {"SC_PII", _SC_PII},
5989#endif
5990#ifdef _SC_PII_INTERNET
5991 {"SC_PII_INTERNET", _SC_PII_INTERNET},
5992#endif
5993#ifdef _SC_PII_INTERNET_DGRAM
5994 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
5995#endif
5996#ifdef _SC_PII_INTERNET_STREAM
5997 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
5998#endif
5999#ifdef _SC_PII_OSI
6000 {"SC_PII_OSI", _SC_PII_OSI},
6001#endif
6002#ifdef _SC_PII_OSI_CLTS
6003 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6004#endif
6005#ifdef _SC_PII_OSI_COTS
6006 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6007#endif
6008#ifdef _SC_PII_OSI_M
6009 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6010#endif
6011#ifdef _SC_PII_SOCKET
6012 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6013#endif
6014#ifdef _SC_PII_XTI
6015 {"SC_PII_XTI", _SC_PII_XTI},
6016#endif
6017#ifdef _SC_POLL
6018 {"SC_POLL", _SC_POLL},
6019#endif
6020#ifdef _SC_PRIORITIZED_IO
6021 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6022#endif
6023#ifdef _SC_PRIORITY_SCHEDULING
6024 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6025#endif
6026#ifdef _SC_REALTIME_SIGNALS
6027 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6028#endif
6029#ifdef _SC_RE_DUP_MAX
6030 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6031#endif
6032#ifdef _SC_RTSIG_MAX
6033 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6034#endif
6035#ifdef _SC_SAVED_IDS
6036 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6037#endif
6038#ifdef _SC_SCHAR_MAX
6039 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6040#endif
6041#ifdef _SC_SCHAR_MIN
6042 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6043#endif
6044#ifdef _SC_SELECT
6045 {"SC_SELECT", _SC_SELECT},
6046#endif
6047#ifdef _SC_SEMAPHORES
6048 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6049#endif
6050#ifdef _SC_SEM_NSEMS_MAX
6051 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6052#endif
6053#ifdef _SC_SEM_VALUE_MAX
6054 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6055#endif
6056#ifdef _SC_SHARED_MEMORY_OBJECTS
6057 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6058#endif
6059#ifdef _SC_SHRT_MAX
6060 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6061#endif
6062#ifdef _SC_SHRT_MIN
6063 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6064#endif
6065#ifdef _SC_SIGQUEUE_MAX
6066 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6067#endif
6068#ifdef _SC_SIGRT_MAX
6069 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6070#endif
6071#ifdef _SC_SIGRT_MIN
6072 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6073#endif
Fred Draked86ed291999-12-15 15:34:33 +00006074#ifdef _SC_SOFTPOWER
6075 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6076#endif
Fred Drakec9680921999-12-13 16:37:25 +00006077#ifdef _SC_SPLIT_CACHE
6078 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6079#endif
6080#ifdef _SC_SSIZE_MAX
6081 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6082#endif
6083#ifdef _SC_STACK_PROT
6084 {"SC_STACK_PROT", _SC_STACK_PROT},
6085#endif
6086#ifdef _SC_STREAM_MAX
6087 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6088#endif
6089#ifdef _SC_SYNCHRONIZED_IO
6090 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6091#endif
6092#ifdef _SC_THREADS
6093 {"SC_THREADS", _SC_THREADS},
6094#endif
6095#ifdef _SC_THREAD_ATTR_STACKADDR
6096 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6097#endif
6098#ifdef _SC_THREAD_ATTR_STACKSIZE
6099 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6100#endif
6101#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6102 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6103#endif
6104#ifdef _SC_THREAD_KEYS_MAX
6105 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6106#endif
6107#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6108 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6109#endif
6110#ifdef _SC_THREAD_PRIO_INHERIT
6111 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6112#endif
6113#ifdef _SC_THREAD_PRIO_PROTECT
6114 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6115#endif
6116#ifdef _SC_THREAD_PROCESS_SHARED
6117 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6118#endif
6119#ifdef _SC_THREAD_SAFE_FUNCTIONS
6120 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6121#endif
6122#ifdef _SC_THREAD_STACK_MIN
6123 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6124#endif
6125#ifdef _SC_THREAD_THREADS_MAX
6126 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6127#endif
6128#ifdef _SC_TIMERS
6129 {"SC_TIMERS", _SC_TIMERS},
6130#endif
6131#ifdef _SC_TIMER_MAX
6132 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6133#endif
6134#ifdef _SC_TTY_NAME_MAX
6135 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6136#endif
6137#ifdef _SC_TZNAME_MAX
6138 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6139#endif
6140#ifdef _SC_T_IOV_MAX
6141 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6142#endif
6143#ifdef _SC_UCHAR_MAX
6144 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6145#endif
6146#ifdef _SC_UINT_MAX
6147 {"SC_UINT_MAX", _SC_UINT_MAX},
6148#endif
6149#ifdef _SC_UIO_MAXIOV
6150 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6151#endif
6152#ifdef _SC_ULONG_MAX
6153 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6154#endif
6155#ifdef _SC_USHRT_MAX
6156 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6157#endif
6158#ifdef _SC_VERSION
6159 {"SC_VERSION", _SC_VERSION},
6160#endif
6161#ifdef _SC_WORD_BIT
6162 {"SC_WORD_BIT", _SC_WORD_BIT},
6163#endif
6164#ifdef _SC_XBS5_ILP32_OFF32
6165 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6166#endif
6167#ifdef _SC_XBS5_ILP32_OFFBIG
6168 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6169#endif
6170#ifdef _SC_XBS5_LP64_OFF64
6171 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6172#endif
6173#ifdef _SC_XBS5_LPBIG_OFFBIG
6174 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6175#endif
6176#ifdef _SC_XOPEN_CRYPT
6177 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6178#endif
6179#ifdef _SC_XOPEN_ENH_I18N
6180 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6181#endif
6182#ifdef _SC_XOPEN_LEGACY
6183 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6184#endif
6185#ifdef _SC_XOPEN_REALTIME
6186 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6187#endif
6188#ifdef _SC_XOPEN_REALTIME_THREADS
6189 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6190#endif
6191#ifdef _SC_XOPEN_SHM
6192 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6193#endif
6194#ifdef _SC_XOPEN_UNIX
6195 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6196#endif
6197#ifdef _SC_XOPEN_VERSION
6198 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6199#endif
6200#ifdef _SC_XOPEN_XCU_VERSION
6201 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6202#endif
6203#ifdef _SC_XOPEN_XPG2
6204 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6205#endif
6206#ifdef _SC_XOPEN_XPG3
6207 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6208#endif
6209#ifdef _SC_XOPEN_XPG4
6210 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6211#endif
6212};
6213
6214static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006215conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006216{
6217 return conv_confname(arg, valuep, posix_constants_sysconf,
6218 sizeof(posix_constants_sysconf)
6219 / sizeof(struct constdef));
6220}
6221
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006222PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006223"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006224Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006225
6226static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006227posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006228{
6229 PyObject *result = NULL;
6230 int name;
6231
6232 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6233 int value;
6234
6235 errno = 0;
6236 value = sysconf(name);
6237 if (value == -1 && errno != 0)
6238 posix_error();
6239 else
6240 result = PyInt_FromLong(value);
6241 }
6242 return result;
6243}
6244#endif
6245
6246
Fred Drakebec628d1999-12-15 18:31:10 +00006247/* This code is used to ensure that the tables of configuration value names
6248 * are in sorted order as required by conv_confname(), and also to build the
6249 * the exported dictionaries that are used to publish information about the
6250 * names available on the host platform.
6251 *
6252 * Sorting the table at runtime ensures that the table is properly ordered
6253 * when used, even for platforms we're not able to test on. It also makes
6254 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006255 */
Fred Drakebec628d1999-12-15 18:31:10 +00006256
6257static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006258cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006259{
6260 const struct constdef *c1 =
6261 (const struct constdef *) v1;
6262 const struct constdef *c2 =
6263 (const struct constdef *) v2;
6264
6265 return strcmp(c1->name, c2->name);
6266}
6267
6268static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006269setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006270 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006271{
Fred Drakebec628d1999-12-15 18:31:10 +00006272 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006273 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006274
6275 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6276 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006277 if (d == NULL)
6278 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006279
Barry Warsaw3155db32000-04-13 15:20:40 +00006280 for (i=0; i < tablesize; ++i) {
6281 PyObject *o = PyInt_FromLong(table[i].value);
6282 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6283 Py_XDECREF(o);
6284 Py_DECREF(d);
6285 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006286 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006287 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006288 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006289 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006290}
6291
Fred Drakebec628d1999-12-15 18:31:10 +00006292/* Return -1 on failure, 0 on success. */
6293static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006294setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006295{
6296#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006297 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006298 sizeof(posix_constants_pathconf)
6299 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006300 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006301 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006302#endif
6303#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006304 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006305 sizeof(posix_constants_confstr)
6306 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006307 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006308 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006309#endif
6310#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006311 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006312 sizeof(posix_constants_sysconf)
6313 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006314 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006315 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006316#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006317 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006318}
Fred Draked86ed291999-12-15 15:34:33 +00006319
6320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006321PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006322"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006323Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006324in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006325
6326static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006327posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006328{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006329 abort();
6330 /*NOTREACHED*/
6331 Py_FatalError("abort() called from Python code didn't abort!");
6332 return NULL;
6333}
Fred Drakebec628d1999-12-15 18:31:10 +00006334
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006335#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006336PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006337"startfile(filepath [, operation]) - Start a file with its associated\n\
6338application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006339\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006340When \"operation\" is not specified or \"open\", this acts like\n\
6341double-clicking the file in Explorer, or giving the file name as an\n\
6342argument to the DOS \"start\" command: the file is opened with whatever\n\
6343application (if any) its extension is associated.\n\
6344When another \"operation\" is given, it specifies what should be done with\n\
6345the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006346\n\
6347startfile returns as soon as the associated application is launched.\n\
6348There is no option to wait for the application to close, and no way\n\
6349to retrieve the application's exit status.\n\
6350\n\
6351The filepath is relative to the current directory. If you want to use\n\
6352an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006353the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006354
6355static PyObject *
6356win32_startfile(PyObject *self, PyObject *args)
6357{
6358 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006359 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006360 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006361#ifdef Py_WIN_WIDE_FILENAMES
6362 if (unicode_file_names()) {
6363 PyObject *unipath, *woperation = NULL;
6364 if (!PyArg_ParseTuple(args, "U|s:startfile",
6365 &unipath, &operation)) {
6366 PyErr_Clear();
6367 goto normal;
6368 }
6369
6370
6371 if (operation) {
6372 woperation = PyUnicode_DecodeASCII(operation,
6373 strlen(operation), NULL);
6374 if (!woperation) {
6375 PyErr_Clear();
6376 operation = NULL;
6377 goto normal;
6378 }
6379 }
6380
6381 Py_BEGIN_ALLOW_THREADS
6382 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6383 PyUnicode_AS_UNICODE(unipath),
6384 NULL, NULL, SW_SHOWNORMAL);
6385 Py_END_ALLOW_THREADS
6386
6387 Py_XDECREF(woperation);
6388 if (rc <= (HINSTANCE)32) {
6389 PyObject *errval = win32_error_unicode("startfile",
6390 PyUnicode_AS_UNICODE(unipath));
6391 return errval;
6392 }
6393 Py_INCREF(Py_None);
6394 return Py_None;
6395 }
6396#endif
6397
6398normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006399 if (!PyArg_ParseTuple(args, "et|s:startfile",
6400 Py_FileSystemDefaultEncoding, &filepath,
6401 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006402 return NULL;
6403 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006404 rc = ShellExecute((HWND)0, operation, filepath,
6405 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006406 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006407 if (rc <= (HINSTANCE)32) {
6408 PyObject *errval = win32_error("startfile", filepath);
6409 PyMem_Free(filepath);
6410 return errval;
6411 }
6412 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006413 Py_INCREF(Py_None);
6414 return Py_None;
6415}
6416#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006417
Martin v. Löwis438b5342002-12-27 10:16:42 +00006418#ifdef HAVE_GETLOADAVG
6419PyDoc_STRVAR(posix_getloadavg__doc__,
6420"getloadavg() -> (float, float, float)\n\n\
6421Return the number of processes in the system run queue averaged over\n\
6422the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6423was unobtainable");
6424
6425static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006426posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006427{
6428 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006429 if (getloadavg(loadavg, 3)!=3) {
6430 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6431 return NULL;
6432 } else
6433 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6434}
6435#endif
6436
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006437#ifdef MS_WINDOWS
6438
6439PyDoc_STRVAR(win32_urandom__doc__,
6440"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006441Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006442
6443typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6444 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6445 DWORD dwFlags );
6446typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6447 BYTE *pbBuffer );
6448
6449static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006450/* This handle is never explicitly released. Instead, the operating
6451 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006452static HCRYPTPROV hCryptProv = 0;
6453
Tim Peters4ad82172004-08-30 17:02:04 +00006454static PyObject*
6455win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006456{
Tim Petersd3115382004-08-30 17:36:46 +00006457 int howMany;
6458 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006459
Tim Peters4ad82172004-08-30 17:02:04 +00006460 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006461 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006462 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006463 if (howMany < 0)
6464 return PyErr_Format(PyExc_ValueError,
6465 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006466
Tim Peters4ad82172004-08-30 17:02:04 +00006467 if (hCryptProv == 0) {
6468 HINSTANCE hAdvAPI32 = NULL;
6469 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006470
Tim Peters4ad82172004-08-30 17:02:04 +00006471 /* Obtain handle to the DLL containing CryptoAPI
6472 This should not fail */
6473 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6474 if(hAdvAPI32 == NULL)
6475 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006476
Tim Peters4ad82172004-08-30 17:02:04 +00006477 /* Obtain pointers to the CryptoAPI functions
6478 This will fail on some early versions of Win95 */
6479 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6480 hAdvAPI32,
6481 "CryptAcquireContextA");
6482 if (pCryptAcquireContext == NULL)
6483 return PyErr_Format(PyExc_NotImplementedError,
6484 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006485
Tim Peters4ad82172004-08-30 17:02:04 +00006486 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6487 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006488 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006489 return PyErr_Format(PyExc_NotImplementedError,
6490 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006491
Tim Peters4ad82172004-08-30 17:02:04 +00006492 /* Acquire context */
6493 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6494 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6495 return win32_error("CryptAcquireContext", NULL);
6496 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006497
Tim Peters4ad82172004-08-30 17:02:04 +00006498 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006499 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006500 if (result != NULL) {
6501 /* Get random data */
6502 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006503 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006504 Py_DECREF(result);
6505 return win32_error("CryptGenRandom", NULL);
6506 }
Tim Peters4ad82172004-08-30 17:02:04 +00006507 }
Tim Petersd3115382004-08-30 17:36:46 +00006508 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006509}
6510#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006511
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006512PyDoc_STRVAR(device_encoding__doc__,
6513"device_encoding(fd) -> str\n\n\
6514Return a string describing the encoding of the device\n\
6515if the output is a terminal; else return None.");
6516
6517static PyObject *
6518device_encoding(PyObject *self, PyObject *args)
6519{
6520 int fd;
6521 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6522 return NULL;
6523 if (!isatty(fd)) {
6524 Py_INCREF(Py_None);
6525 return Py_None;
6526 }
6527#if defined(MS_WINDOWS) || defined(MS_WIN64)
6528 if (fd == 0) {
6529 char buf[100];
6530 sprintf(buf, "cp%d", GetConsoleCP());
6531 return PyUnicode_FromString(buf);
6532 }
6533 if (fd == 1 || fd == 2) {
6534 char buf[100];
6535 sprintf(buf, "cp%d", GetConsoleOutputCP());
6536 return PyUnicode_FromString(buf);
6537 }
6538#elif defined(CODESET)
6539 {
6540 char *codeset = nl_langinfo(CODESET);
6541 if (codeset)
6542 return PyUnicode_FromString(codeset);
6543 }
6544#endif
6545 Py_INCREF(Py_None);
6546 return Py_None;
6547}
6548
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006549#ifdef __VMS
6550/* Use openssl random routine */
6551#include <openssl/rand.h>
6552PyDoc_STRVAR(vms_urandom__doc__,
6553"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006554Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006555
6556static PyObject*
6557vms_urandom(PyObject *self, PyObject *args)
6558{
6559 int howMany;
6560 PyObject* result;
6561
6562 /* Read arguments */
6563 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6564 return NULL;
6565 if (howMany < 0)
6566 return PyErr_Format(PyExc_ValueError,
6567 "negative argument not allowed");
6568
6569 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006570 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006571 if (result != NULL) {
6572 /* Get random data */
6573 if (RAND_pseudo_bytes((unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006574 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006575 howMany) < 0) {
6576 Py_DECREF(result);
6577 return PyErr_Format(PyExc_ValueError,
6578 "RAND_pseudo_bytes");
6579 }
6580 }
6581 return result;
6582}
6583#endif
6584
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006585static PyMethodDef posix_methods[] = {
6586 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6587#ifdef HAVE_TTYNAME
6588 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6589#endif
6590 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006591#ifdef HAVE_CHFLAGS
6592 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6593#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006594 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006595#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006596 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006597#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006598#ifdef HAVE_LCHFLAGS
6599 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6600#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006601#ifdef HAVE_LCHOWN
6602 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6603#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006604#ifdef HAVE_CHROOT
6605 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6606#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006607#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006608 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006609#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006610#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006611 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006612 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006613#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006614#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006615 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006616#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006617 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6618 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6619 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006620#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006621 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006622#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006623#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006624 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006625#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006626 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6627 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6628 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006629 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006630#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006631 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006632#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006633#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006634 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006635#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006636 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006637#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006638 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006639#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006640 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6641 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6642 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006643#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006644 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006645#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006646 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006647#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006648 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6649 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006650#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006651#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006652 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6653 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006654#if defined(PYOS_OS2)
6655 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6656 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6657#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006658#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006659#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006660 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006661#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006662#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006663 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006664#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006665#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006666 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006667#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006668#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006669 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006670#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006671#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006672 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006673#endif /* HAVE_GETEGID */
6674#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006675 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006676#endif /* HAVE_GETEUID */
6677#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006678 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006679#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006680#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006681 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006682#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006683 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006684#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006685 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006686#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006687#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006688 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006689#endif /* HAVE_GETPPID */
6690#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006691 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006692#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006693#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006694 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006695#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006696#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006697 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006698#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006699#ifdef HAVE_KILLPG
6700 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6701#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006702#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006703 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006704#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006705#ifdef MS_WINDOWS
6706 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6707#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006708#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006709 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006710#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006711#ifdef HAVE_SETEUID
6712 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6713#endif /* HAVE_SETEUID */
6714#ifdef HAVE_SETEGID
6715 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6716#endif /* HAVE_SETEGID */
6717#ifdef HAVE_SETREUID
6718 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6719#endif /* HAVE_SETREUID */
6720#ifdef HAVE_SETREGID
6721 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6722#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006723#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006724 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006725#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006726#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006727 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006728#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006729#ifdef HAVE_GETPGID
6730 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6731#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006732#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006733 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006734#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006735#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006736 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006737#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006738#ifdef HAVE_WAIT3
6739 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6740#endif /* HAVE_WAIT3 */
6741#ifdef HAVE_WAIT4
6742 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6743#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006744#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006745 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006746#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006747#ifdef HAVE_GETSID
6748 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6749#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006750#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006751 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006752#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006753#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006754 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006755#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006756#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006757 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006758#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006759#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006760 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006761#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006762 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6763 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006764 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006765 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6766 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6767 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6768 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6769 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6770 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006771 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006772#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006773 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006774#endif
6775#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006776 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006777#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006778#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006779 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6780#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006781#ifdef HAVE_DEVICE_MACROS
6782 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6783 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6784 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6785#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006786#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006787 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006788#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006789#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006790 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006791#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006792#ifdef HAVE_UNSETENV
6793 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6794#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00006795#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006796 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00006797#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00006798#ifdef HAVE_FCHDIR
6799 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6800#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006801#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006802 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006803#endif
6804#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006805 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006806#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006807#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006808#ifdef WCOREDUMP
6809 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6810#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006811#ifdef WIFCONTINUED
6812 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6813#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006814#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006815 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006816#endif /* WIFSTOPPED */
6817#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006818 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006819#endif /* WIFSIGNALED */
6820#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006821 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006822#endif /* WIFEXITED */
6823#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006824 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006825#endif /* WEXITSTATUS */
6826#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006827 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006828#endif /* WTERMSIG */
6829#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006830 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006831#endif /* WSTOPSIG */
6832#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00006833#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006834 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006835#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00006836#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006837 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006838#endif
Fred Drakec9680921999-12-13 16:37:25 +00006839#ifdef HAVE_CONFSTR
6840 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
6841#endif
6842#ifdef HAVE_SYSCONF
6843 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
6844#endif
6845#ifdef HAVE_FPATHCONF
6846 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
6847#endif
6848#ifdef HAVE_PATHCONF
6849 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
6850#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006851 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006852#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00006853 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
6854#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006855#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00006856 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00006857#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006858 #ifdef MS_WINDOWS
6859 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
6860 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006861 #ifdef __VMS
6862 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
6863 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006864 {NULL, NULL} /* Sentinel */
6865};
6866
6867
Barry Warsaw4a342091996-12-19 23:50:02 +00006868static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006869ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00006870{
Fred Drake4d1e64b2002-04-15 19:40:07 +00006871 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00006872}
6873
Guido van Rossumd48f2521997-12-05 22:19:34 +00006874#if defined(PYOS_OS2)
6875/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006876static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006877{
6878 APIRET rc;
6879 ULONG values[QSV_MAX+1];
6880 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00006881 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00006882
6883 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00006884 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006885 Py_END_ALLOW_THREADS
6886
6887 if (rc != NO_ERROR) {
6888 os2_error(rc);
6889 return -1;
6890 }
6891
Fred Drake4d1e64b2002-04-15 19:40:07 +00006892 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6893 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
6894 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
6895 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
6896 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
6897 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
6898 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006899
6900 switch (values[QSV_VERSION_MINOR]) {
6901 case 0: ver = "2.00"; break;
6902 case 10: ver = "2.10"; break;
6903 case 11: ver = "2.11"; break;
6904 case 30: ver = "3.00"; break;
6905 case 40: ver = "4.00"; break;
6906 case 50: ver = "5.00"; break;
6907 default:
Tim Peters885d4572001-11-28 20:27:42 +00006908 PyOS_snprintf(tmp, sizeof(tmp),
6909 "%d-%d", values[QSV_VERSION_MAJOR],
6910 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006911 ver = &tmp[0];
6912 }
6913
6914 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006915 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006916 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006917
6918 /* Add Indicator of Which Drive was Used to Boot the System */
6919 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
6920 tmp[1] = ':';
6921 tmp[2] = '\0';
6922
Fred Drake4d1e64b2002-04-15 19:40:07 +00006923 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006924}
6925#endif
6926
Barry Warsaw4a342091996-12-19 23:50:02 +00006927static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006928all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00006929{
Guido van Rossum94f6f721999-01-06 18:42:14 +00006930#ifdef F_OK
6931 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00006932#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006933#ifdef R_OK
6934 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00006935#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006936#ifdef W_OK
6937 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00006938#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006939#ifdef X_OK
6940 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00006941#endif
Fred Drakec9680921999-12-13 16:37:25 +00006942#ifdef NGROUPS_MAX
6943 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
6944#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006945#ifdef TMP_MAX
6946 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
6947#endif
Fred Drake106c1a02002-04-23 15:58:02 +00006948#ifdef WCONTINUED
6949 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
6950#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00006951#ifdef WNOHANG
6952 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00006953#endif
Fred Drake106c1a02002-04-23 15:58:02 +00006954#ifdef WUNTRACED
6955 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
6956#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00006957#ifdef O_RDONLY
6958 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
6959#endif
6960#ifdef O_WRONLY
6961 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
6962#endif
6963#ifdef O_RDWR
6964 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
6965#endif
6966#ifdef O_NDELAY
6967 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
6968#endif
6969#ifdef O_NONBLOCK
6970 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
6971#endif
6972#ifdef O_APPEND
6973 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
6974#endif
6975#ifdef O_DSYNC
6976 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
6977#endif
6978#ifdef O_RSYNC
6979 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
6980#endif
6981#ifdef O_SYNC
6982 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
6983#endif
6984#ifdef O_NOCTTY
6985 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
6986#endif
6987#ifdef O_CREAT
6988 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
6989#endif
6990#ifdef O_EXCL
6991 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
6992#endif
6993#ifdef O_TRUNC
6994 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
6995#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00006996#ifdef O_BINARY
6997 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
6998#endif
6999#ifdef O_TEXT
7000 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7001#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007002#ifdef O_LARGEFILE
7003 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7004#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007005#ifdef O_SHLOCK
7006 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7007#endif
7008#ifdef O_EXLOCK
7009 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7010#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007011
Tim Peters5aa91602002-01-30 05:46:57 +00007012/* MS Windows */
7013#ifdef O_NOINHERIT
7014 /* Don't inherit in child processes. */
7015 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7016#endif
7017#ifdef _O_SHORT_LIVED
7018 /* Optimize for short life (keep in memory). */
7019 /* MS forgot to define this one with a non-underscore form too. */
7020 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7021#endif
7022#ifdef O_TEMPORARY
7023 /* Automatically delete when last handle is closed. */
7024 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7025#endif
7026#ifdef O_RANDOM
7027 /* Optimize for random access. */
7028 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7029#endif
7030#ifdef O_SEQUENTIAL
7031 /* Optimize for sequential access. */
7032 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7033#endif
7034
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007035/* GNU extensions. */
7036#ifdef O_DIRECT
7037 /* Direct disk access. */
7038 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7039#endif
7040#ifdef O_DIRECTORY
7041 /* Must be a directory. */
7042 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7043#endif
7044#ifdef O_NOFOLLOW
7045 /* Do not follow links. */
7046 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7047#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007048
Barry Warsaw5676bd12003-01-07 20:57:09 +00007049 /* These come from sysexits.h */
7050#ifdef EX_OK
7051 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007052#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007053#ifdef EX_USAGE
7054 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007055#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007056#ifdef EX_DATAERR
7057 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007058#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007059#ifdef EX_NOINPUT
7060 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007061#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007062#ifdef EX_NOUSER
7063 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007064#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007065#ifdef EX_NOHOST
7066 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007067#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007068#ifdef EX_UNAVAILABLE
7069 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007070#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007071#ifdef EX_SOFTWARE
7072 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007073#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007074#ifdef EX_OSERR
7075 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007076#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007077#ifdef EX_OSFILE
7078 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007079#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007080#ifdef EX_CANTCREAT
7081 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007082#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007083#ifdef EX_IOERR
7084 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007085#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007086#ifdef EX_TEMPFAIL
7087 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007088#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007089#ifdef EX_PROTOCOL
7090 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007091#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007092#ifdef EX_NOPERM
7093 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007094#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007095#ifdef EX_CONFIG
7096 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007097#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007098#ifdef EX_NOTFOUND
7099 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007100#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007101
Guido van Rossum246bc171999-02-01 23:54:31 +00007102#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007103#if defined(PYOS_OS2) && defined(PYCC_GCC)
7104 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7105 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7106 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7107 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7108 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7109 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7110 if (ins(d, "P_PM", (long)P_PM)) return -1;
7111 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7112 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7113 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7114 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7115 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7116 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7117 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7118 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7119 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7120 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7121 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7122 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7123 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7124#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007125 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7126 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7127 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7128 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7129 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007130#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007131#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007132
Guido van Rossumd48f2521997-12-05 22:19:34 +00007133#if defined(PYOS_OS2)
7134 if (insertvalues(d)) return -1;
7135#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007136 return 0;
7137}
7138
7139
Tim Peters5aa91602002-01-30 05:46:57 +00007140#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007141#define INITFUNC initnt
7142#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007143
7144#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007145#define INITFUNC initos2
7146#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007147
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007148#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007149#define INITFUNC initposix
7150#define MODNAME "posix"
7151#endif
7152
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007153PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007154INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007155{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007156 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007157
Fred Drake4d1e64b2002-04-15 19:40:07 +00007158 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007159 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007160 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007161 if (m == NULL)
7162 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007163
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007164 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007165 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007166 Py_XINCREF(v);
7167 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007168 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007169 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007170
Fred Drake4d1e64b2002-04-15 19:40:07 +00007171 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007172 return;
7173
Fred Drake4d1e64b2002-04-15 19:40:07 +00007174 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007175 return;
7176
Fred Drake4d1e64b2002-04-15 19:40:07 +00007177 Py_INCREF(PyExc_OSError);
7178 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007179
Guido van Rossumb3d39562000-01-31 18:41:26 +00007180#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007181 if (posix_putenv_garbage == NULL)
7182 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007183#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007184
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007185 if (!initialized) {
7186 stat_result_desc.name = MODNAME ".stat_result";
7187 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7188 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7189 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7190 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7191 structseq_new = StatResultType.tp_new;
7192 StatResultType.tp_new = statresult_new;
7193
7194 statvfs_result_desc.name = MODNAME ".statvfs_result";
7195 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7196 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007197 Py_INCREF((PyObject*) &StatResultType);
7198 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007199 Py_INCREF((PyObject*) &StatVFSResultType);
7200 PyModule_AddObject(m, "statvfs_result",
7201 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007202 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007203
7204#ifdef __APPLE__
7205 /*
7206 * Step 2 of weak-linking support on Mac OS X.
7207 *
7208 * The code below removes functions that are not available on the
7209 * currently active platform.
7210 *
7211 * This block allow one to use a python binary that was build on
7212 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7213 * OSX 10.4.
7214 */
7215#ifdef HAVE_FSTATVFS
7216 if (fstatvfs == NULL) {
7217 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7218 return;
7219 }
7220 }
7221#endif /* HAVE_FSTATVFS */
7222
7223#ifdef HAVE_STATVFS
7224 if (statvfs == NULL) {
7225 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7226 return;
7227 }
7228 }
7229#endif /* HAVE_STATVFS */
7230
7231# ifdef HAVE_LCHOWN
7232 if (lchown == NULL) {
7233 if (PyObject_DelAttrString(m, "lchown") == -1) {
7234 return;
7235 }
7236 }
7237#endif /* HAVE_LCHOWN */
7238
7239
7240#endif /* __APPLE__ */
7241
Guido van Rossumb6775db1994-08-01 11:34:53 +00007242}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007243
7244#ifdef __cplusplus
7245}
7246#endif