blob: 55af338e0a065484e11ca59afcf2b57a13ea970f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int chown(const char *, uid_t, gid_t);
190extern char *getcwd(char *, int);
191extern char *strerror(int);
192extern int link(const char *, const char *);
193extern int rename(const char *, const char *);
194extern int stat(const char *, struct stat *);
195extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000198#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000201#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000203
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_UTIME_H
207#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000210#ifdef HAVE_SYS_UTIME_H
211#include <sys/utime.h>
212#define HAVE_UTIME_H /* pretend we do for the rest of this file */
213#endif /* HAVE_SYS_UTIME_H */
214
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#ifdef HAVE_SYS_TIMES_H
216#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000217#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
219#ifdef HAVE_SYS_PARAM_H
220#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
223#ifdef HAVE_SYS_UTSNAME_H
224#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#define NAMLEN(dirent) strlen((dirent)->d_name)
230#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000231#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000232#include <direct.h>
233#define NAMLEN(dirent) strlen((dirent)->d_name)
234#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000237#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000238#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#endif
241#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#endif
244#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000252#endif
253#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255#endif
256#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000259#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000261#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
Guido van Rossumd48f2521997-12-05 22:19:34 +0000264#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000266#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267
Tim Petersbc2e10e2002-03-03 23:17:02 +0000268#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269#if defined(PATH_MAX) && PATH_MAX > 1024
270#define MAXPATHLEN PATH_MAX
271#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000272#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000273#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#endif /* MAXPATHLEN */
275
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000276#ifdef UNION_WAIT
277/* Emulate some macros on systems that have a union instead of macros */
278
279#ifndef WIFEXITED
280#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
281#endif
282
283#ifndef WEXITSTATUS
284#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
285#endif
286
287#ifndef WTERMSIG
288#define WTERMSIG(u_wait) ((u_wait).w_termsig)
289#endif
290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291#define WAIT_TYPE union wait
292#define WAIT_STATUS_INT(s) (s.w_status)
293
294#else /* !UNION_WAIT */
295#define WAIT_TYPE int
296#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000297#endif /* UNION_WAIT */
298
Greg Wardb48bc172000-03-01 21:51:56 +0000299/* Don't use the "_r" form if we don't need it (also, won't have a
300 prototype for it, at least on Solaris -- maybe others as well?). */
301#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
302#define USE_CTERMID_R
303#endif
304
305#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
306#define USE_TMPNAM_R
307#endif
308
Fred Drake699f3522000-06-29 21:12:41 +0000309/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000310#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000311#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000312# define STAT win32_stat
313# define FSTAT win32_fstat
314# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000315#else
316# define STAT stat
317# define FSTAT fstat
318# define STRUCT_STAT struct stat
319#endif
320
Tim Peters11b23062003-04-23 02:39:17 +0000321#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000322#include <sys/mkdev.h>
323#else
324#if defined(MAJOR_IN_SYSMACROS)
325#include <sys/sysmacros.h>
326#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000327#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
328#include <sys/mkdev.h>
329#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000330#endif
Fred Drake699f3522000-06-29 21:12:41 +0000331
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000333#ifdef WITH_NEXT_FRAMEWORK
334/* On Darwin/MacOSX a shared library or framework has no access to
335** environ directly, we must obtain it with _NSGetEnviron().
336*/
337#include <crt_externs.h>
338static char **environ;
339#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000341#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342
Barry Warsaw53699e91996-12-10 23:23:01 +0000343static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000344convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345{
Barry Warsaw53699e91996-12-10 23:23:01 +0000346 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 if (d == NULL)
350 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000351#ifdef WITH_NEXT_FRAMEWORK
352 if (environ == NULL)
353 environ = *_NSGetEnviron();
354#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 if (environ == NULL)
356 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000357 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000359 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 char *p = strchr(*e, '=');
362 if (p == NULL)
363 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000364 k = PyString_FromStringAndSize(*e, (int)(p-*e));
365 if (k == NULL) {
366 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000368 }
369 v = PyString_FromString(p+1);
370 if (v == NULL) {
371 PyErr_Clear();
372 Py_DECREF(k);
373 continue;
374 }
375 if (PyDict_GetItem(d, k) == NULL) {
376 if (PyDict_SetItem(d, k, v) != 0)
377 PyErr_Clear();
378 }
379 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000380 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000382#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000383 {
384 APIRET rc;
385 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
386
387 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000388 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000389 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000390 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000391 Py_DECREF(v);
392 }
393 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
394 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
395 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000396 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000397 Py_DECREF(v);
398 }
399 }
400#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 return d;
402}
403
404
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405/* Set a POSIX-specific error from errno, and return NULL */
406
Barry Warsawd58d7641998-07-23 16:14:40 +0000407static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000408posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000409{
Barry Warsawca74da41999-02-09 19:31:45 +0000410 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411}
Barry Warsawd58d7641998-07-23 16:14:40 +0000412static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000413posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000414{
Barry Warsawca74da41999-02-09 19:31:45 +0000415 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000416}
417
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000418#ifdef Py_WIN_WIDE_FILENAMES
419static PyObject *
420posix_error_with_unicode_filename(Py_UNICODE* name)
421{
422 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
423}
424#endif /* Py_WIN_WIDE_FILENAMES */
425
426
Mark Hammondef8b6542001-05-13 08:04:26 +0000427static PyObject *
428posix_error_with_allocated_filename(char* name)
429{
430 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
431 PyMem_Free(name);
432 return rc;
433}
434
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000435#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000436static PyObject *
437win32_error(char* function, char* filename)
438{
Mark Hammond33a6da92000-08-15 00:46:38 +0000439 /* XXX We should pass the function name along in the future.
440 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000441 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000442 Windows error object, which is non-trivial.
443 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000444 errno = GetLastError();
445 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000446 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000447 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000448 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000449}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450
451#ifdef Py_WIN_WIDE_FILENAMES
452static PyObject *
453win32_error_unicode(char* function, Py_UNICODE* filename)
454{
455 /* XXX - see win32_error for comments on 'function' */
456 errno = GetLastError();
457 if (filename)
458 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
459 else
460 return PyErr_SetFromWindowsErr(errno);
461}
462
463static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
464{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465}
466
467/* Function suitable for O& conversion */
468static int
469convert_to_unicode(PyObject *arg, void* _param)
470{
471 PyObject **param = (PyObject**)_param;
472 if (PyUnicode_CheckExact(arg)) {
473 Py_INCREF(arg);
474 *param = arg;
475 }
476 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000477 /* For a Unicode subtype that's not a Unicode object,
478 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
480 PyUnicode_GET_SIZE(arg));
481 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000482 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 else
484 *param = PyUnicode_FromEncodedObject(arg,
485 Py_FileSystemDefaultEncoding,
486 "strict");
487 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000488}
489
490#endif /* Py_WIN_WIDE_FILENAMES */
491
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000492#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493
Guido van Rossumd48f2521997-12-05 22:19:34 +0000494#if defined(PYOS_OS2)
495/**********************************************************************
496 * Helper Function to Trim and Format OS/2 Messages
497 **********************************************************************/
498 static void
499os2_formatmsg(char *msgbuf, int msglen, char *reason)
500{
501 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
502
503 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
504 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
505
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000506 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000507 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
508 }
509
510 /* Add Optional Reason Text */
511 if (reason) {
512 strcat(msgbuf, " : ");
513 strcat(msgbuf, reason);
514 }
515}
516
517/**********************************************************************
518 * Decode an OS/2 Operating System Error Code
519 *
520 * A convenience function to lookup an OS/2 error code and return a
521 * text message we can use to raise a Python exception.
522 *
523 * Notes:
524 * The messages for errors returned from the OS/2 kernel reside in
525 * the file OSO001.MSG in the \OS2 directory hierarchy.
526 *
527 **********************************************************************/
528 static char *
529os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
530{
531 APIRET rc;
532 ULONG msglen;
533
534 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
535 Py_BEGIN_ALLOW_THREADS
536 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
537 errorcode, "oso001.msg", &msglen);
538 Py_END_ALLOW_THREADS
539
540 if (rc == NO_ERROR)
541 os2_formatmsg(msgbuf, msglen, reason);
542 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000543 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000544 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000545
546 return msgbuf;
547}
548
549/* Set an OS/2-specific error and return NULL. OS/2 kernel
550 errors are not in a global variable e.g. 'errno' nor are
551 they congruent with posix error numbers. */
552
553static PyObject * os2_error(int code)
554{
555 char text[1024];
556 PyObject *v;
557
558 os2_strerror(text, sizeof(text), code, "");
559
560 v = Py_BuildValue("(is)", code, text);
561 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000562 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000563 Py_DECREF(v);
564 }
565 return NULL; /* Signal to Python that an Exception is Pending */
566}
567
568#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569
570/* POSIX generic methods */
571
Barry Warsaw53699e91996-12-10 23:23:01 +0000572static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000573posix_fildes(PyObject *fdobj, int (*func)(int))
574{
575 int fd;
576 int res;
577 fd = PyObject_AsFileDescriptor(fdobj);
578 if (fd < 0)
579 return NULL;
580 Py_BEGIN_ALLOW_THREADS
581 res = (*func)(fd);
582 Py_END_ALLOW_THREADS
583 if (res < 0)
584 return posix_error();
585 Py_INCREF(Py_None);
586 return Py_None;
587}
Guido van Rossum21142a01999-01-08 21:05:37 +0000588
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000589#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000590static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000591unicode_file_names(void)
592{
593 static int canusewide = -1;
594 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000595 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000596 the Windows NT family. */
597 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
598 }
599 return canusewide;
600}
601#endif
Tim Peters11b23062003-04-23 02:39:17 +0000602
Guido van Rossum21142a01999-01-08 21:05:37 +0000603static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605{
Mark Hammondef8b6542001-05-13 08:04:26 +0000606 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000608 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000611 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000612 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000614 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000615 return posix_error_with_allocated_filename(path1);
616 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000617 Py_INCREF(Py_None);
618 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Barry Warsaw53699e91996-12-10 23:23:01 +0000621static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000622posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625{
Mark Hammondef8b6542001-05-13 08:04:26 +0000626 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000627 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000628 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000629 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000630 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000632 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000633 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000634 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 PyMem_Free(path1);
636 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000637 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000638 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_INCREF(Py_None);
641 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644#ifdef Py_WIN_WIDE_FILENAMES
645static PyObject*
646win32_1str(PyObject* args, char* func,
647 char* format, BOOL (__stdcall *funcA)(LPCSTR),
648 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
649{
650 PyObject *uni;
651 char *ansi;
652 BOOL result;
653 if (unicode_file_names()) {
654 if (!PyArg_ParseTuple(args, wformat, &uni))
655 PyErr_Clear();
656 else {
657 Py_BEGIN_ALLOW_THREADS
658 result = funcW(PyUnicode_AsUnicode(uni));
659 Py_END_ALLOW_THREADS
660 if (!result)
661 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
662 Py_INCREF(Py_None);
663 return Py_None;
664 }
665 }
666 if (!PyArg_ParseTuple(args, format, &ansi))
667 return NULL;
668 Py_BEGIN_ALLOW_THREADS
669 result = funcA(ansi);
670 Py_END_ALLOW_THREADS
671 if (!result)
672 return win32_error(func, ansi);
673 Py_INCREF(Py_None);
674 return Py_None;
675
676}
677
678/* This is a reimplementation of the C library's chdir function,
679 but one that produces Win32 errors instead of DOS error codes.
680 chdir is essentially a wrapper around SetCurrentDirectory; however,
681 it also needs to set "magic" environment variables indicating
682 the per-drive current directory, which are of the form =<drive>: */
683BOOL __stdcall
684win32_chdir(LPCSTR path)
685{
686 char new_path[MAX_PATH+1];
687 int result;
688 char env[4] = "=x:";
689
690 if(!SetCurrentDirectoryA(path))
691 return FALSE;
692 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
693 if (!result)
694 return FALSE;
695 /* In the ANSI API, there should not be any paths longer
696 than MAX_PATH. */
697 assert(result <= MAX_PATH+1);
698 if (strncmp(new_path, "\\\\", 2) == 0 ||
699 strncmp(new_path, "//", 2) == 0)
700 /* UNC path, nothing to do. */
701 return TRUE;
702 env[1] = new_path[0];
703 return SetEnvironmentVariableA(env, new_path);
704}
705
706/* The Unicode version differs from the ANSI version
707 since the current directory might exceed MAX_PATH characters */
708BOOL __stdcall
709win32_wchdir(LPCWSTR path)
710{
711 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
712 int result;
713 wchar_t env[4] = L"=x:";
714
715 if(!SetCurrentDirectoryW(path))
716 return FALSE;
717 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
718 if (!result)
719 return FALSE;
720 if (result > MAX_PATH+1) {
721 new_path = malloc(result);
722 if (!new_path) {
723 SetLastError(ERROR_OUTOFMEMORY);
724 return FALSE;
725 }
726 }
727 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
728 wcsncmp(new_path, L"//", 2) == 0)
729 /* UNC path, nothing to do. */
730 return TRUE;
731 env[1] = new_path[0];
732 result = SetEnvironmentVariableW(env, new_path);
733 if (new_path != _new_path)
734 free(new_path);
735 return result;
736}
737#endif
738
Martin v. Löwis14694662006-02-03 12:54:16 +0000739#ifdef MS_WINDOWS
740/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
741 - time stamps are restricted to second resolution
742 - file modification times suffer from forth-and-back conversions between
743 UTC and local time
744 Therefore, we implement our own stat, based on the Win32 API directly.
745*/
746#define HAVE_STAT_NSEC 1
747
748struct win32_stat{
749 int st_dev;
750 __int64 st_ino;
751 unsigned short st_mode;
752 int st_nlink;
753 int st_uid;
754 int st_gid;
755 int st_rdev;
756 __int64 st_size;
757 int st_atime;
758 int st_atime_nsec;
759 int st_mtime;
760 int st_mtime_nsec;
761 int st_ctime;
762 int st_ctime_nsec;
763};
764
765static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
766
767static void
768FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
769{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000770 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
771 /* Cannot simply cast and dereference in_ptr,
772 since it might not be aligned properly */
773 __int64 in;
774 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000775 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
776 /* XXX Win32 supports time stamps past 2038; we currently don't */
777 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
778}
779
Thomas Wouters477c8d52006-05-27 19:21:47 +0000780static void
781time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
782{
783 /* XXX endianness */
784 __int64 out;
785 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000786 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 memcpy(out_ptr, &out, sizeof(out));
788}
789
Martin v. Löwis14694662006-02-03 12:54:16 +0000790/* Below, we *know* that ugo+r is 0444 */
791#if _S_IREAD != 0400
792#error Unsupported C library
793#endif
794static int
795attributes_to_mode(DWORD attr)
796{
797 int m = 0;
798 if (attr & FILE_ATTRIBUTE_DIRECTORY)
799 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
800 else
801 m |= _S_IFREG;
802 if (attr & FILE_ATTRIBUTE_READONLY)
803 m |= 0444;
804 else
805 m |= 0666;
806 return m;
807}
808
809static int
810attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
811{
812 memset(result, 0, sizeof(*result));
813 result->st_mode = attributes_to_mode(info->dwFileAttributes);
814 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
815 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
816 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
817 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
818
819 return 0;
820}
821
Thomas Wouters89f507f2006-12-13 04:49:30 +0000822/* Emulate GetFileAttributesEx[AW] on Windows 95 */
823static int checked = 0;
824static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
825static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
826static void
827check_gfax()
828{
829 HINSTANCE hKernel32;
830 if (checked)
831 return;
832 checked = 1;
833 hKernel32 = GetModuleHandle("KERNEL32");
834 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
835 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
836}
837
Guido van Rossumd8faa362007-04-27 19:54:29 +0000838static BOOL
839attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
840{
841 HANDLE hFindFile;
842 WIN32_FIND_DATAA FileData;
843 hFindFile = FindFirstFileA(pszFile, &FileData);
844 if (hFindFile == INVALID_HANDLE_VALUE)
845 return FALSE;
846 FindClose(hFindFile);
847 pfad->dwFileAttributes = FileData.dwFileAttributes;
848 pfad->ftCreationTime = FileData.ftCreationTime;
849 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
850 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
851 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
852 pfad->nFileSizeLow = FileData.nFileSizeLow;
853 return TRUE;
854}
855
856static BOOL
857attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
858{
859 HANDLE hFindFile;
860 WIN32_FIND_DATAW FileData;
861 hFindFile = FindFirstFileW(pszFile, &FileData);
862 if (hFindFile == INVALID_HANDLE_VALUE)
863 return FALSE;
864 FindClose(hFindFile);
865 pfad->dwFileAttributes = FileData.dwFileAttributes;
866 pfad->ftCreationTime = FileData.ftCreationTime;
867 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
868 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
869 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
870 pfad->nFileSizeLow = FileData.nFileSizeLow;
871 return TRUE;
872}
873
Thomas Wouters89f507f2006-12-13 04:49:30 +0000874static BOOL WINAPI
875Py_GetFileAttributesExA(LPCSTR pszFile,
876 GET_FILEEX_INFO_LEVELS level,
877 LPVOID pv)
878{
879 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000880 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
881 /* First try to use the system's implementation, if that is
882 available and either succeeds to gives an error other than
883 that it isn't implemented. */
884 check_gfax();
885 if (gfaxa) {
886 result = gfaxa(pszFile, level, pv);
887 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
888 return result;
889 }
890 /* It's either not present, or not implemented.
891 Emulate using FindFirstFile. */
892 if (level != GetFileExInfoStandard) {
893 SetLastError(ERROR_INVALID_PARAMETER);
894 return FALSE;
895 }
896 /* Use GetFileAttributes to validate that the file name
897 does not contain wildcards (which FindFirstFile would
898 accept). */
899 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
900 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000901 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000902}
903
904static BOOL WINAPI
905Py_GetFileAttributesExW(LPCWSTR pszFile,
906 GET_FILEEX_INFO_LEVELS level,
907 LPVOID pv)
908{
909 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
911 /* First try to use the system's implementation, if that is
912 available and either succeeds to gives an error other than
913 that it isn't implemented. */
914 check_gfax();
915 if (gfaxa) {
916 result = gfaxw(pszFile, level, pv);
917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
918 return result;
919 }
920 /* It's either not present, or not implemented.
921 Emulate using FindFirstFile. */
922 if (level != GetFileExInfoStandard) {
923 SetLastError(ERROR_INVALID_PARAMETER);
924 return FALSE;
925 }
926 /* Use GetFileAttributes to validate that the file name
927 does not contain wildcards (which FindFirstFile would
928 accept). */
929 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
930 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000931 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000932}
933
Martin v. Löwis14694662006-02-03 12:54:16 +0000934static int
935win32_stat(const char* path, struct win32_stat *result)
936{
937 WIN32_FILE_ATTRIBUTE_DATA info;
938 int code;
939 char *dot;
940 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000941 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942 if (GetLastError() != ERROR_SHARING_VIOLATION) {
943 /* Protocol violation: we explicitly clear errno, instead of
944 setting it to a POSIX error. Callers should use GetLastError. */
945 errno = 0;
946 return -1;
947 } else {
948 /* Could not get attributes on open file. Fall back to
949 reading the directory. */
950 if (!attributes_from_dir(path, &info)) {
951 /* Very strange. This should not fail now */
952 errno = 0;
953 return -1;
954 }
955 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000956 }
957 code = attribute_data_to_stat(&info, result);
958 if (code != 0)
959 return code;
960 /* Set S_IFEXEC if it is an .exe, .bat, ... */
961 dot = strrchr(path, '.');
962 if (dot) {
963 if (stricmp(dot, ".bat") == 0 ||
964 stricmp(dot, ".cmd") == 0 ||
965 stricmp(dot, ".exe") == 0 ||
966 stricmp(dot, ".com") == 0)
967 result->st_mode |= 0111;
968 }
969 return code;
970}
971
972static int
973win32_wstat(const wchar_t* path, struct win32_stat *result)
974{
975 int code;
976 const wchar_t *dot;
977 WIN32_FILE_ATTRIBUTE_DATA info;
978 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000979 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000980 if (GetLastError() != ERROR_SHARING_VIOLATION) {
981 /* Protocol violation: we explicitly clear errno, instead of
982 setting it to a POSIX error. Callers should use GetLastError. */
983 errno = 0;
984 return -1;
985 } else {
986 /* Could not get attributes on open file. Fall back to
987 reading the directory. */
988 if (!attributes_from_dir_w(path, &info)) {
989 /* Very strange. This should not fail now */
990 errno = 0;
991 return -1;
992 }
993 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 }
995 code = attribute_data_to_stat(&info, result);
996 if (code < 0)
997 return code;
998 /* Set IFEXEC if it is an .exe, .bat, ... */
999 dot = wcsrchr(path, '.');
1000 if (dot) {
1001 if (_wcsicmp(dot, L".bat") == 0 ||
1002 _wcsicmp(dot, L".cmd") == 0 ||
1003 _wcsicmp(dot, L".exe") == 0 ||
1004 _wcsicmp(dot, L".com") == 0)
1005 result->st_mode |= 0111;
1006 }
1007 return code;
1008}
1009
1010static int
1011win32_fstat(int file_number, struct win32_stat *result)
1012{
1013 BY_HANDLE_FILE_INFORMATION info;
1014 HANDLE h;
1015 int type;
1016
1017 h = (HANDLE)_get_osfhandle(file_number);
1018
1019 /* Protocol violation: we explicitly clear errno, instead of
1020 setting it to a POSIX error. Callers should use GetLastError. */
1021 errno = 0;
1022
1023 if (h == INVALID_HANDLE_VALUE) {
1024 /* This is really a C library error (invalid file handle).
1025 We set the Win32 error to the closes one matching. */
1026 SetLastError(ERROR_INVALID_HANDLE);
1027 return -1;
1028 }
1029 memset(result, 0, sizeof(*result));
1030
1031 type = GetFileType(h);
1032 if (type == FILE_TYPE_UNKNOWN) {
1033 DWORD error = GetLastError();
1034 if (error != 0) {
1035 return -1;
1036 }
1037 /* else: valid but unknown file */
1038 }
1039
1040 if (type != FILE_TYPE_DISK) {
1041 if (type == FILE_TYPE_CHAR)
1042 result->st_mode = _S_IFCHR;
1043 else if (type == FILE_TYPE_PIPE)
1044 result->st_mode = _S_IFIFO;
1045 return 0;
1046 }
1047
1048 if (!GetFileInformationByHandle(h, &info)) {
1049 return -1;
1050 }
1051
1052 /* similar to stat() */
1053 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1054 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1055 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1056 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1057 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1058 /* specific to fstat() */
1059 result->st_nlink = info.nNumberOfLinks;
1060 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1061 return 0;
1062}
1063
1064#endif /* MS_WINDOWS */
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001067"stat_result: Result from stat or lstat.\n\n\
1068This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001069 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1071\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001072Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1073or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001074\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076
1077static PyStructSequence_Field stat_result_fields[] = {
1078 {"st_mode", "protection bits"},
1079 {"st_ino", "inode"},
1080 {"st_dev", "device"},
1081 {"st_nlink", "number of hard links"},
1082 {"st_uid", "user ID of owner"},
1083 {"st_gid", "group ID of owner"},
1084 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001085 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1086 {NULL, "integer time of last access"},
1087 {NULL, "integer time of last modification"},
1088 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001089 {"st_atime", "time of last access"},
1090 {"st_mtime", "time of last modification"},
1091 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001092#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001093 {"st_blksize", "blocksize for filesystem I/O"},
1094#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001095#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001096 {"st_blocks", "number of blocks allocated"},
1097#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001098#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001099 {"st_rdev", "device type (if inode device)"},
1100#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001101#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1102 {"st_flags", "user defined flags for file"},
1103#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001104#ifdef HAVE_STRUCT_STAT_ST_GEN
1105 {"st_gen", "generation number"},
1106#endif
1107#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1108 {"st_birthtime", "time of creation"},
1109#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110 {0}
1111};
1112
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001113#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001114#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001116#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001117#endif
1118
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001119#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001120#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1121#else
1122#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1123#endif
1124
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001125#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1127#else
1128#define ST_RDEV_IDX ST_BLOCKS_IDX
1129#endif
1130
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001131#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1132#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1133#else
1134#define ST_FLAGS_IDX ST_RDEV_IDX
1135#endif
1136
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001137#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001138#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001139#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001140#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001141#endif
1142
1143#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1144#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1145#else
1146#define ST_BIRTHTIME_IDX ST_GEN_IDX
1147#endif
1148
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149static PyStructSequence_Desc stat_result_desc = {
1150 "stat_result", /* name */
1151 stat_result__doc__, /* doc */
1152 stat_result_fields,
1153 10
1154};
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1158This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001159 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001160or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001161\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001162See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163
1164static PyStructSequence_Field statvfs_result_fields[] = {
1165 {"f_bsize", },
1166 {"f_frsize", },
1167 {"f_blocks", },
1168 {"f_bfree", },
1169 {"f_bavail", },
1170 {"f_files", },
1171 {"f_ffree", },
1172 {"f_favail", },
1173 {"f_flag", },
1174 {"f_namemax",},
1175 {0}
1176};
1177
1178static PyStructSequence_Desc statvfs_result_desc = {
1179 "statvfs_result", /* name */
1180 statvfs_result__doc__, /* doc */
1181 statvfs_result_fields,
1182 10
1183};
1184
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001186static PyTypeObject StatResultType;
1187static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001188static newfunc structseq_new;
1189
1190static PyObject *
1191statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1192{
1193 PyStructSequence *result;
1194 int i;
1195
1196 result = (PyStructSequence*)structseq_new(type, args, kwds);
1197 if (!result)
1198 return NULL;
1199 /* If we have been initialized from a tuple,
1200 st_?time might be set to None. Initialize it
1201 from the int slots. */
1202 for (i = 7; i <= 9; i++) {
1203 if (result->ob_item[i+3] == Py_None) {
1204 Py_DECREF(Py_None);
1205 Py_INCREF(result->ob_item[i]);
1206 result->ob_item[i+3] = result->ob_item[i];
1207 }
1208 }
1209 return (PyObject*)result;
1210}
1211
1212
1213
1214/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001215static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001216
1217PyDoc_STRVAR(stat_float_times__doc__,
1218"stat_float_times([newval]) -> oldval\n\n\
1219Determine whether os.[lf]stat represents time stamps as float objects.\n\
1220If newval is True, future calls to stat() return floats, if it is False,\n\
1221future calls return ints. \n\
1222If newval is omitted, return the current setting.\n");
1223
1224static PyObject*
1225stat_float_times(PyObject* self, PyObject *args)
1226{
1227 int newval = -1;
1228 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1229 return NULL;
1230 if (newval == -1)
1231 /* Return old value */
1232 return PyBool_FromLong(_stat_float_times);
1233 _stat_float_times = newval;
1234 Py_INCREF(Py_None);
1235 return Py_None;
1236}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001237
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001238static void
1239fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1240{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001241 PyObject *fval,*ival;
1242#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001243 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001244#else
1245 ival = PyInt_FromLong((long)sec);
1246#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001247 if (!ival)
1248 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001249 if (_stat_float_times) {
1250 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1251 } else {
1252 fval = ival;
1253 Py_INCREF(fval);
1254 }
1255 PyStructSequence_SET_ITEM(v, index, ival);
1256 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001257}
1258
Tim Peters5aa91602002-01-30 05:46:57 +00001259/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001260 (used by posix_stat() and posix_fstat()) */
1261static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001262_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001263{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001264 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001265 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001266 if (v == NULL)
1267 return NULL;
1268
Martin v. Löwis14694662006-02-03 12:54:16 +00001269 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001270#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001271 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001272 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001273#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001274 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001275#endif
1276#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001277 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001278 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001279#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001280 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001281#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001282 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1283 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1284 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001285#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001286 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001287 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001288#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001289 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001290#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001291
Martin v. Löwis14694662006-02-03 12:54:16 +00001292#if defined(HAVE_STAT_TV_NSEC)
1293 ansec = st->st_atim.tv_nsec;
1294 mnsec = st->st_mtim.tv_nsec;
1295 cnsec = st->st_ctim.tv_nsec;
1296#elif defined(HAVE_STAT_TV_NSEC2)
1297 ansec = st->st_atimespec.tv_nsec;
1298 mnsec = st->st_mtimespec.tv_nsec;
1299 cnsec = st->st_ctimespec.tv_nsec;
1300#elif defined(HAVE_STAT_NSEC)
1301 ansec = st->st_atime_nsec;
1302 mnsec = st->st_mtime_nsec;
1303 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001304#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001305 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001306#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001307 fill_time(v, 7, st->st_atime, ansec);
1308 fill_time(v, 8, st->st_mtime, mnsec);
1309 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001311#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001312 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001313 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001315#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001316 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001318#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001319#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001320 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001322#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001323#ifdef HAVE_STRUCT_STAT_ST_GEN
1324 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001325 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001326#endif
1327#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1328 {
1329 PyObject *val;
1330 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001331 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001332#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001333 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#else
1335 bnsec = 0;
1336#endif
1337 if (_stat_float_times) {
1338 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1339 } else {
1340 val = PyInt_FromLong((long)bsec);
1341 }
1342 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1343 val);
1344 }
1345#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001346#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1347 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001348 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001349#endif
Fred Drake699f3522000-06-29 21:12:41 +00001350
1351 if (PyErr_Occurred()) {
1352 Py_DECREF(v);
1353 return NULL;
1354 }
1355
1356 return v;
1357}
1358
Martin v. Löwisd8948722004-06-02 09:57:56 +00001359#ifdef MS_WINDOWS
1360
1361/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1362 where / can be used in place of \ and the trailing slash is optional.
1363 Both SERVER and SHARE must have at least one character.
1364*/
1365
1366#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1367#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001368#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001369#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001370#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001371
Tim Peters4ad82172004-08-30 17:02:04 +00001372static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001373IsUNCRootA(char *path, int pathlen)
1374{
1375 #define ISSLASH ISSLASHA
1376
1377 int i, share;
1378
1379 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1380 /* minimum UNCRoot is \\x\y */
1381 return FALSE;
1382 for (i = 2; i < pathlen ; i++)
1383 if (ISSLASH(path[i])) break;
1384 if (i == 2 || i == pathlen)
1385 /* do not allow \\\SHARE or \\SERVER */
1386 return FALSE;
1387 share = i+1;
1388 for (i = share; i < pathlen; i++)
1389 if (ISSLASH(path[i])) break;
1390 return (i != share && (i == pathlen || i == pathlen-1));
1391
1392 #undef ISSLASH
1393}
1394
1395#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001396static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001397IsUNCRootW(Py_UNICODE *path, int pathlen)
1398{
1399 #define ISSLASH ISSLASHW
1400
1401 int i, share;
1402
1403 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1404 /* minimum UNCRoot is \\x\y */
1405 return FALSE;
1406 for (i = 2; i < pathlen ; i++)
1407 if (ISSLASH(path[i])) break;
1408 if (i == 2 || i == pathlen)
1409 /* do not allow \\\SHARE or \\SERVER */
1410 return FALSE;
1411 share = i+1;
1412 for (i = share; i < pathlen; i++)
1413 if (ISSLASH(path[i])) break;
1414 return (i != share && (i == pathlen || i == pathlen-1));
1415
1416 #undef ISSLASH
1417}
1418#endif /* Py_WIN_WIDE_FILENAMES */
1419#endif /* MS_WINDOWS */
1420
Barry Warsaw53699e91996-12-10 23:23:01 +00001421static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001422posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001423 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001424#ifdef __VMS
1425 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1426#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001427 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001428#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001429 char *wformat,
1430 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431{
Fred Drake699f3522000-06-29 21:12:41 +00001432 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001433 char *path = NULL; /* pass this to stat; do not free() it */
1434 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001435 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001436 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001437
1438#ifdef Py_WIN_WIDE_FILENAMES
1439 /* If on wide-character-capable OS see if argument
1440 is Unicode and if so use wide API. */
1441 if (unicode_file_names()) {
1442 PyUnicodeObject *po;
1443 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001444 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1445
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001446 Py_BEGIN_ALLOW_THREADS
1447 /* PyUnicode_AS_UNICODE result OK without
1448 thread lock as it is a simple dereference. */
1449 res = wstatfunc(wpath, &st);
1450 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001451
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001452 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001453 return win32_error_unicode("stat", wpath);
1454 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001455 }
1456 /* Drop the argument parsing error as narrow strings
1457 are also valid. */
1458 PyErr_Clear();
1459 }
1460#endif
1461
Tim Peters5aa91602002-01-30 05:46:57 +00001462 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001463 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001464 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001465 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001466
Barry Warsaw53699e91996-12-10 23:23:01 +00001467 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001468 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001469 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001470
1471 if (res != 0) {
1472#ifdef MS_WINDOWS
1473 result = win32_error("stat", pathfree);
1474#else
1475 result = posix_error_with_filename(pathfree);
1476#endif
1477 }
1478 else
1479 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001480
Tim Peters500bd032001-12-19 19:05:01 +00001481 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001482 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001483}
1484
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001485/* POSIX methods */
1486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001488"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001489Use the real uid/gid to test for access to a path. Note that most\n\
1490operations will use the effective uid/gid, therefore this routine can\n\
1491be used in a suid/sgid environment to test if the invoking user has the\n\
1492specified access to the path. The mode argument can be F_OK to test\n\
1493existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001494
1495static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001496posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001497{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001498 char *path;
1499 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001500
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001501#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001503 if (unicode_file_names()) {
1504 PyUnicodeObject *po;
1505 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1506 Py_BEGIN_ALLOW_THREADS
1507 /* PyUnicode_AS_UNICODE OK without thread lock as
1508 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001509 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001510 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001511 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001512 }
1513 /* Drop the argument parsing error as narrow strings
1514 are also valid. */
1515 PyErr_Clear();
1516 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001517 if (!PyArg_ParseTuple(args, "eti:access",
1518 Py_FileSystemDefaultEncoding, &path, &mode))
1519 return 0;
1520 Py_BEGIN_ALLOW_THREADS
1521 attr = GetFileAttributesA(path);
1522 Py_END_ALLOW_THREADS
1523 PyMem_Free(path);
1524finish:
1525 if (attr == 0xFFFFFFFF)
1526 /* File does not exist, or cannot read attributes */
1527 return PyBool_FromLong(0);
1528 /* Access is possible if either write access wasn't requested, or
1529 the file isn't read-only. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001530 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001531#else
1532 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001533 if (!PyArg_ParseTuple(args, "eti:access",
1534 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001535 return NULL;
1536 Py_BEGIN_ALLOW_THREADS
1537 res = access(path, mode);
1538 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001539 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001540 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001542}
1543
Guido van Rossumd371ff11999-01-25 16:12:23 +00001544#ifndef F_OK
1545#define F_OK 0
1546#endif
1547#ifndef R_OK
1548#define R_OK 4
1549#endif
1550#ifndef W_OK
1551#define W_OK 2
1552#endif
1553#ifndef X_OK
1554#define X_OK 1
1555#endif
1556
1557#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001558PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001559"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001561
1562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001563posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001564{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001565 int id;
1566 char *ret;
1567
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001568 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001569 return NULL;
1570
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001571#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001572 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001573 if (id == 0) {
1574 ret = ttyname();
1575 }
1576 else {
1577 ret = NULL;
1578 }
1579#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001580 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001581#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001582 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001583 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001584 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001585}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001587
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001588#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001590"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001592
1593static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001594posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001595{
1596 char *ret;
1597 char buffer[L_ctermid];
1598
Greg Wardb48bc172000-03-01 21:51:56 +00001599#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600 ret = ctermid_r(buffer);
1601#else
1602 ret = ctermid(buffer);
1603#endif
1604 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001605 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001606 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607}
1608#endif
1609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001610PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001611"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001613
Barry Warsaw53699e91996-12-10 23:23:01 +00001614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001615posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001616{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001617#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001619#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001621#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001625#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626}
1627
Fred Drake4d1e64b2002-04-15 19:40:07 +00001628#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001630"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001631Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001632opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001633
1634static PyObject *
1635posix_fchdir(PyObject *self, PyObject *fdobj)
1636{
1637 return posix_fildes(fdobj, fchdir);
1638}
1639#endif /* HAVE_FCHDIR */
1640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001642PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001643"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001644Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001645
Barry Warsaw53699e91996-12-10 23:23:01 +00001646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001647posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648{
Mark Hammondef8b6542001-05-13 08:04:26 +00001649 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001650 int i;
1651 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001652#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001654 if (unicode_file_names()) {
1655 PyUnicodeObject *po;
1656 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1657 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001658 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1659 if (attr != 0xFFFFFFFF) {
1660 if (i & _S_IWRITE)
1661 attr &= ~FILE_ATTRIBUTE_READONLY;
1662 else
1663 attr |= FILE_ATTRIBUTE_READONLY;
1664 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1665 }
1666 else
1667 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001668 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001669 if (!res)
1670 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001671 PyUnicode_AS_UNICODE(po));
1672 Py_INCREF(Py_None);
1673 return Py_None;
1674 }
1675 /* Drop the argument parsing error as narrow strings
1676 are also valid. */
1677 PyErr_Clear();
1678 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001679 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1680 &path, &i))
1681 return NULL;
1682 Py_BEGIN_ALLOW_THREADS
1683 attr = GetFileAttributesA(path);
1684 if (attr != 0xFFFFFFFF) {
1685 if (i & _S_IWRITE)
1686 attr &= ~FILE_ATTRIBUTE_READONLY;
1687 else
1688 attr |= FILE_ATTRIBUTE_READONLY;
1689 res = SetFileAttributesA(path, attr);
1690 }
1691 else
1692 res = 0;
1693 Py_END_ALLOW_THREADS
1694 if (!res) {
1695 win32_error("chmod", path);
1696 PyMem_Free(path);
1697 return NULL;
1698 }
1699 PyMem_Free(path);
1700 Py_INCREF(Py_None);
1701 return Py_None;
1702#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001703 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001704 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001705 return NULL;
1706 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001707 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001708 Py_END_ALLOW_THREADS
1709 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001710 return posix_error_with_allocated_filename(path);
1711 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001712 Py_INCREF(Py_None);
1713 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001714#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001715}
1716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001717
Thomas Wouterscf297e42007-02-23 15:07:44 +00001718#ifdef HAVE_CHFLAGS
1719PyDoc_STRVAR(posix_chflags__doc__,
1720"chflags(path, flags)\n\n\
1721Set file flags.");
1722
1723static PyObject *
1724posix_chflags(PyObject *self, PyObject *args)
1725{
1726 char *path;
1727 unsigned long flags;
1728 int res;
1729 if (!PyArg_ParseTuple(args, "etk:chflags",
1730 Py_FileSystemDefaultEncoding, &path, &flags))
1731 return NULL;
1732 Py_BEGIN_ALLOW_THREADS
1733 res = chflags(path, flags);
1734 Py_END_ALLOW_THREADS
1735 if (res < 0)
1736 return posix_error_with_allocated_filename(path);
1737 PyMem_Free(path);
1738 Py_INCREF(Py_None);
1739 return Py_None;
1740}
1741#endif /* HAVE_CHFLAGS */
1742
1743#ifdef HAVE_LCHFLAGS
1744PyDoc_STRVAR(posix_lchflags__doc__,
1745"lchflags(path, flags)\n\n\
1746Set file flags.\n\
1747This function will not follow symbolic links.");
1748
1749static PyObject *
1750posix_lchflags(PyObject *self, PyObject *args)
1751{
1752 char *path;
1753 unsigned long flags;
1754 int res;
1755 if (!PyArg_ParseTuple(args, "etk:lchflags",
1756 Py_FileSystemDefaultEncoding, &path, &flags))
1757 return NULL;
1758 Py_BEGIN_ALLOW_THREADS
1759 res = lchflags(path, flags);
1760 Py_END_ALLOW_THREADS
1761 if (res < 0)
1762 return posix_error_with_allocated_filename(path);
1763 PyMem_Free(path);
1764 Py_INCREF(Py_None);
1765 return Py_None;
1766}
1767#endif /* HAVE_LCHFLAGS */
1768
Martin v. Löwis244edc82001-10-04 22:44:26 +00001769#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001771"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001773
1774static PyObject *
1775posix_chroot(PyObject *self, PyObject *args)
1776{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001777 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001778}
1779#endif
1780
Guido van Rossum21142a01999-01-08 21:05:37 +00001781#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001783"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001784force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001785
1786static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001787posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001788{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001789 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001790}
1791#endif /* HAVE_FSYNC */
1792
1793#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001794
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001795#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001796extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1797#endif
1798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001801force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001803
1804static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001805posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001806{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001807 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001808}
1809#endif /* HAVE_FDATASYNC */
1810
1811
Fredrik Lundh10723342000-07-10 16:38:09 +00001812#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001813PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001814"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001816
Barry Warsaw53699e91996-12-10 23:23:01 +00001817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001818posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001819{
Mark Hammondef8b6542001-05-13 08:04:26 +00001820 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001821 int uid, gid;
1822 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001823 if (!PyArg_ParseTuple(args, "etii:chown",
1824 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001825 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001826 return NULL;
1827 Py_BEGIN_ALLOW_THREADS
1828 res = chown(path, (uid_t) uid, (gid_t) gid);
1829 Py_END_ALLOW_THREADS
1830 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001831 return posix_error_with_allocated_filename(path);
1832 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001833 Py_INCREF(Py_None);
1834 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001835}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001836#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001837
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001838#ifdef HAVE_LCHOWN
1839PyDoc_STRVAR(posix_lchown__doc__,
1840"lchown(path, uid, gid)\n\n\
1841Change the owner and group id of path to the numeric uid and gid.\n\
1842This function will not follow symbolic links.");
1843
1844static PyObject *
1845posix_lchown(PyObject *self, PyObject *args)
1846{
1847 char *path = NULL;
1848 int uid, gid;
1849 int res;
1850 if (!PyArg_ParseTuple(args, "etii:lchown",
1851 Py_FileSystemDefaultEncoding, &path,
1852 &uid, &gid))
1853 return NULL;
1854 Py_BEGIN_ALLOW_THREADS
1855 res = lchown(path, (uid_t) uid, (gid_t) gid);
1856 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001857 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001858 return posix_error_with_allocated_filename(path);
1859 PyMem_Free(path);
1860 Py_INCREF(Py_None);
1861 return Py_None;
1862}
1863#endif /* HAVE_LCHOWN */
1864
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001865
Guido van Rossum36bc6801995-06-14 22:54:23 +00001866#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001868"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Barry Warsaw53699e91996-12-10 23:23:01 +00001871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001872posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001873{
1874 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001875 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001878#if defined(PYOS_OS2) && defined(PYCC_GCC)
1879 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001880#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001881 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001882#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001883 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001884 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001886 return PyUnicode_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001887}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001888
1889PyDoc_STRVAR(posix_getcwdu__doc__,
1890"getcwdu() -> path\n\n\
1891Return a unicode string representing the current working directory.");
1892
1893static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001894posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001895{
1896 char buf[1026];
1897 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001898
1899#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001900 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001901 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001902 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903 wchar_t *wbuf2 = wbuf;
1904 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001905 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1907 /* If the buffer is large enough, len does not include the
1908 terminating \0. If the buffer is too small, len includes
1909 the space needed for the terminator. */
1910 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1911 wbuf2 = malloc(len * sizeof(wchar_t));
1912 if (wbuf2)
1913 len = GetCurrentDirectoryW(len, wbuf2);
1914 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001915 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916 if (!wbuf2) {
1917 PyErr_NoMemory();
1918 return NULL;
1919 }
1920 if (!len) {
1921 if (wbuf2 != wbuf) free(wbuf2);
1922 return win32_error("getcwdu", NULL);
1923 }
1924 resobj = PyUnicode_FromWideChar(wbuf2, len);
1925 if (wbuf2 != wbuf) free(wbuf2);
1926 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001927 }
1928#endif
1929
1930 Py_BEGIN_ALLOW_THREADS
1931#if defined(PYOS_OS2) && defined(PYCC_GCC)
1932 res = _getcwd2(buf, sizeof buf);
1933#else
1934 res = getcwd(buf, sizeof buf);
1935#endif
1936 Py_END_ALLOW_THREADS
1937 if (res == NULL)
1938 return posix_error();
1939 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1940}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001941#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001943
Guido van Rossumb6775db1994-08-01 11:34:53 +00001944#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001946"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001948
Barry Warsaw53699e91996-12-10 23:23:01 +00001949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001950posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001951{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001952 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001953}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001954#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001958"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959Return a list containing the names of the entries in the directory.\n\
1960\n\
1961 path: path of directory to list\n\
1962\n\
1963The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001964entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001965
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001967posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001968{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001969 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001970 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001971#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001972
Barry Warsaw53699e91996-12-10 23:23:01 +00001973 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001974 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001975 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001978 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001979 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001980
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#ifdef Py_WIN_WIDE_FILENAMES
1982 /* If on wide-character-capable OS see if argument
1983 is Unicode and if so use wide API. */
1984 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001985 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001986 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1987 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001988 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990 /* Overallocate for \\*.*\0 */
1991 len = PyUnicode_GET_SIZE(po);
1992 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1993 if (!wnamebuf) {
1994 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001996 }
1997 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1998 wch = len > 0 ? wnamebuf[len-1] : '\0';
1999 if (wch != L'/' && wch != L'\\' && wch != L':')
2000 wnamebuf[len++] = L'\\';
2001 wcscpy(wnamebuf + len, L"*.*");
2002 if ((d = PyList_New(0)) == NULL) {
2003 free(wnamebuf);
2004 return NULL;
2005 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2007 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002008 int error = GetLastError();
2009 if (error == ERROR_FILE_NOT_FOUND) {
2010 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011 return d;
2012 }
2013 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002014 win32_error_unicode("FindFirstFileW", wnamebuf);
2015 free(wnamebuf);
2016 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017 }
2018 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002019 /* Skip over . and .. */
2020 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2021 wcscmp(wFileData.cFileName, L"..") != 0) {
2022 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2023 if (v == NULL) {
2024 Py_DECREF(d);
2025 d = NULL;
2026 break;
2027 }
2028 if (PyList_Append(d, v) != 0) {
2029 Py_DECREF(v);
2030 Py_DECREF(d);
2031 d = NULL;
2032 break;
2033 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002035 }
Georg Brandl622927b2006-03-07 12:48:03 +00002036 Py_BEGIN_ALLOW_THREADS
2037 result = FindNextFileW(hFindFile, &wFileData);
2038 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002039 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2040 it got to the end of the directory. */
2041 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2042 Py_DECREF(d);
2043 win32_error_unicode("FindNextFileW", wnamebuf);
2044 FindClose(hFindFile);
2045 free(wnamebuf);
2046 return NULL;
2047 }
Georg Brandl622927b2006-03-07 12:48:03 +00002048 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002049
2050 if (FindClose(hFindFile) == FALSE) {
2051 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002052 win32_error_unicode("FindClose", wnamebuf);
2053 free(wnamebuf);
2054 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002055 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002056 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002057 return d;
2058 }
2059 /* Drop the argument parsing error as narrow strings
2060 are also valid. */
2061 PyErr_Clear();
2062 }
2063#endif
2064
Tim Peters5aa91602002-01-30 05:46:57 +00002065 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002066 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002067 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002068 if (len > 0) {
2069 char ch = namebuf[len-1];
2070 if (ch != SEP && ch != ALTSEP && ch != ':')
2071 namebuf[len++] = '/';
2072 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002073 strcpy(namebuf + len, "*.*");
2074
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002076 return NULL;
2077
2078 hFindFile = FindFirstFile(namebuf, &FileData);
2079 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080 int error = GetLastError();
2081 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002082 return d;
2083 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002084 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085 }
2086 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002087 /* Skip over . and .. */
2088 if (strcmp(FileData.cFileName, ".") != 0 &&
2089 strcmp(FileData.cFileName, "..") != 0) {
2090 v = PyString_FromString(FileData.cFileName);
2091 if (v == NULL) {
2092 Py_DECREF(d);
2093 d = NULL;
2094 break;
2095 }
2096 if (PyList_Append(d, v) != 0) {
2097 Py_DECREF(v);
2098 Py_DECREF(d);
2099 d = NULL;
2100 break;
2101 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103 }
Georg Brandl622927b2006-03-07 12:48:03 +00002104 Py_BEGIN_ALLOW_THREADS
2105 result = FindNextFile(hFindFile, &FileData);
2106 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002107 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2108 it got to the end of the directory. */
2109 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2110 Py_DECREF(d);
2111 win32_error("FindNextFile", namebuf);
2112 FindClose(hFindFile);
2113 return NULL;
2114 }
Georg Brandl622927b2006-03-07 12:48:03 +00002115 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002116
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002117 if (FindClose(hFindFile) == FALSE) {
2118 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002119 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002120 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002121
2122 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002123
Tim Peters0bb44a42000-09-15 07:44:49 +00002124#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002125
2126#ifndef MAX_PATH
2127#define MAX_PATH CCHMAXPATH
2128#endif
2129 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002130 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002131 PyObject *d, *v;
2132 char namebuf[MAX_PATH+5];
2133 HDIR hdir = 1;
2134 ULONG srchcnt = 1;
2135 FILEFINDBUF3 ep;
2136 APIRET rc;
2137
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002138 if (!PyArg_ParseTuple(args, "et#:listdir",
2139 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002140 return NULL;
2141 if (len >= MAX_PATH) {
Neal Norwitz6c913782007-10-14 03:23:09 +00002142 PyMem_Free(name);
2143 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002144 return NULL;
2145 }
2146 strcpy(namebuf, name);
2147 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002148 if (*pt == ALTSEP)
2149 *pt = SEP;
2150 if (namebuf[len-1] != SEP)
2151 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002152 strcpy(namebuf + len, "*.*");
2153
Neal Norwitz6c913782007-10-14 03:23:09 +00002154 if ((d = PyList_New(0)) == NULL) {
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002155 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002156 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002157 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002158
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002159 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2160 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002161 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002162 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2163 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2164 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002165
2166 if (rc != NO_ERROR) {
2167 errno = ENOENT;
Neal Norwitz6c913782007-10-14 03:23:09 +00002168 return posix_error_with_allocated_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002169 }
2170
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002171 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002172 do {
2173 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002174 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002175 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002176
2177 strcpy(namebuf, ep.achName);
2178
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002179 /* Leave Case of Name Alone -- In Native Form */
2180 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002181
2182 v = PyString_FromString(namebuf);
2183 if (v == NULL) {
2184 Py_DECREF(d);
2185 d = NULL;
2186 break;
2187 }
2188 if (PyList_Append(d, v) != 0) {
2189 Py_DECREF(v);
2190 Py_DECREF(d);
2191 d = NULL;
2192 break;
2193 }
2194 Py_DECREF(v);
2195 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2196 }
2197
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002198 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002199 return d;
2200#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002201
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002202 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002204 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002205 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002206 int arg_is_unicode = 1;
2207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002209 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2210 arg_is_unicode = 0;
2211 PyErr_Clear();
2212 }
2213 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002214 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002215 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002216 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002217 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002218 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002219 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002220 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002221 return NULL;
2222 }
Georg Brandl622927b2006-03-07 12:48:03 +00002223 for (;;) {
2224 Py_BEGIN_ALLOW_THREADS
2225 ep = readdir(dirp);
2226 Py_END_ALLOW_THREADS
2227 if (ep == NULL)
2228 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002229 if (ep->d_name[0] == '.' &&
2230 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002231 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002232 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002233 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002234 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002235 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002236 d = NULL;
2237 break;
2238 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002239 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002240 PyObject *w;
2241
2242 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002243 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002244 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002245 if (w != NULL) {
2246 Py_DECREF(v);
2247 v = w;
2248 }
2249 else {
2250 /* fall back to the original byte string, as
2251 discussed in patch #683592 */
2252 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002253 }
Just van Rossum46c97842003-02-25 21:42:15 +00002254 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002255 if (PyList_Append(d, v) != 0) {
2256 Py_DECREF(v);
2257 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002258 d = NULL;
2259 break;
2260 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002261 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002262 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002263 if (errno != 0 && d != NULL) {
2264 /* readdir() returned NULL and set errno */
2265 closedir(dirp);
2266 Py_DECREF(d);
2267 return posix_error_with_allocated_filename(name);
2268 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002269 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002270 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002271
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002272 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002273
Tim Peters0bb44a42000-09-15 07:44:49 +00002274#endif /* which OS */
2275} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002276
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002277#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002278/* A helper function for abspath on win32 */
2279static PyObject *
2280posix__getfullpathname(PyObject *self, PyObject *args)
2281{
2282 /* assume encoded strings wont more than double no of chars */
2283 char inbuf[MAX_PATH*2];
2284 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002285 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002286 char outbuf[MAX_PATH*2];
2287 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002288#ifdef Py_WIN_WIDE_FILENAMES
2289 if (unicode_file_names()) {
2290 PyUnicodeObject *po;
2291 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2292 Py_UNICODE woutbuf[MAX_PATH*2];
2293 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002294 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002295 sizeof(woutbuf)/sizeof(woutbuf[0]),
2296 woutbuf, &wtemp))
2297 return win32_error("GetFullPathName", "");
2298 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2299 }
2300 /* Drop the argument parsing error as narrow strings
2301 are also valid. */
2302 PyErr_Clear();
2303 }
2304#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002305 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2306 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002307 &insize))
2308 return NULL;
2309 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2310 outbuf, &temp))
2311 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002312 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2313 return PyUnicode_Decode(outbuf, strlen(outbuf),
2314 Py_FileSystemDefaultEncoding, NULL);
2315 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002316 return PyString_FromString(outbuf);
2317} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002318#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002320PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002321"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002322Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002323
Barry Warsaw53699e91996-12-10 23:23:01 +00002324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002325posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002326{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002327 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002328 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002329 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330
2331#ifdef Py_WIN_WIDE_FILENAMES
2332 if (unicode_file_names()) {
2333 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002334 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002335 Py_BEGIN_ALLOW_THREADS
2336 /* PyUnicode_AS_UNICODE OK without thread lock as
2337 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002338 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002339 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002340 if (!res)
2341 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002342 Py_INCREF(Py_None);
2343 return Py_None;
2344 }
2345 /* Drop the argument parsing error as narrow strings
2346 are also valid. */
2347 PyErr_Clear();
2348 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002349 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2350 Py_FileSystemDefaultEncoding, &path, &mode))
2351 return NULL;
2352 Py_BEGIN_ALLOW_THREADS
2353 /* PyUnicode_AS_UNICODE OK without thread lock as
2354 it is a simple dereference. */
2355 res = CreateDirectoryA(path, NULL);
2356 Py_END_ALLOW_THREADS
2357 if (!res) {
2358 win32_error("mkdir", path);
2359 PyMem_Free(path);
2360 return NULL;
2361 }
2362 PyMem_Free(path);
2363 Py_INCREF(Py_None);
2364 return Py_None;
2365#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002366
Tim Peters5aa91602002-01-30 05:46:57 +00002367 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002368 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002369 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002370 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002371#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002372 res = mkdir(path);
2373#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002374 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002375#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002376 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002377 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002378 return posix_error_with_allocated_filename(path);
2379 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002380 Py_INCREF(Py_None);
2381 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002382#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002383}
2384
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002385
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2387#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002388#include <sys/resource.h>
2389#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002390
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391
2392#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002393PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002394"nice(inc) -> new_priority\n\n\
2395Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002396
Barry Warsaw53699e91996-12-10 23:23:01 +00002397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002398posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002399{
2400 int increment, value;
2401
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002402 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002403 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002404
2405 /* There are two flavours of 'nice': one that returns the new
2406 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002407 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2408 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002409
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002410 If we are of the nice family that returns the new priority, we
2411 need to clear errno before the call, and check if errno is filled
2412 before calling posix_error() on a returnvalue of -1, because the
2413 -1 may be the actual new priority! */
2414
2415 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002416 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002417#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002418 if (value == 0)
2419 value = getpriority(PRIO_PROCESS, 0);
2420#endif
2421 if (value == -1 && errno != 0)
2422 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002423 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002424 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002425}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002426#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002427
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002428PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002429"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002430Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002431
Barry Warsaw53699e91996-12-10 23:23:01 +00002432static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002433posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002434{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002435#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002436 PyObject *o1, *o2;
2437 char *p1, *p2;
2438 BOOL result;
2439 if (unicode_file_names()) {
2440 if (!PyArg_ParseTuple(args, "O&O&:rename",
2441 convert_to_unicode, &o1,
2442 convert_to_unicode, &o2))
2443 PyErr_Clear();
2444 else {
2445 Py_BEGIN_ALLOW_THREADS
2446 result = MoveFileW(PyUnicode_AsUnicode(o1),
2447 PyUnicode_AsUnicode(o2));
2448 Py_END_ALLOW_THREADS
2449 Py_DECREF(o1);
2450 Py_DECREF(o2);
2451 if (!result)
2452 return win32_error("rename", NULL);
2453 Py_INCREF(Py_None);
2454 return Py_None;
2455 }
2456 }
2457 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2458 return NULL;
2459 Py_BEGIN_ALLOW_THREADS
2460 result = MoveFileA(p1, p2);
2461 Py_END_ALLOW_THREADS
2462 if (!result)
2463 return win32_error("rename", NULL);
2464 Py_INCREF(Py_None);
2465 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002466#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002467 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002468#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002469}
2470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002473"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002474Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002475
Barry Warsaw53699e91996-12-10 23:23:01 +00002476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002477posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002478{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002479#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002480 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002481#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002482 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002483#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002484}
2485
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002487PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002488"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002489Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002490
Barry Warsaw53699e91996-12-10 23:23:01 +00002491static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002492posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002493{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002494#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002495 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002496#else
2497 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2498#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002499}
2500
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002502#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002503PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002504"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002505Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002506
Barry Warsaw53699e91996-12-10 23:23:01 +00002507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002508posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002509{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002510 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002511 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002512 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002513 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002514 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002515 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002516 Py_END_ALLOW_THREADS
2517 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002518}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002519#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002520
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002522PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002523"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002524Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002525
Barry Warsaw53699e91996-12-10 23:23:01 +00002526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002527posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002528{
2529 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002530 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002531 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002532 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002533 if (i < 0)
2534 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002535 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002536}
2537
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002540"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002541Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002543PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002544"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002545Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002546
Barry Warsaw53699e91996-12-10 23:23:01 +00002547static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002548posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002549{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002550#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002551 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002552#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002553 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002554#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002555}
2556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002557
Guido van Rossumb6775db1994-08-01 11:34:53 +00002558#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002559PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002560"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002561Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002562
Barry Warsaw53699e91996-12-10 23:23:01 +00002563static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002564posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002565{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002566 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002567 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002568
Barry Warsaw53699e91996-12-10 23:23:01 +00002569 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002570 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002571 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002572 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002573 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002574 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002575 u.sysname,
2576 u.nodename,
2577 u.release,
2578 u.version,
2579 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002580}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002581#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002582
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002583static int
2584extract_time(PyObject *t, long* sec, long* usec)
2585{
2586 long intval;
2587 if (PyFloat_Check(t)) {
2588 double tval = PyFloat_AsDouble(t);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002589 PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002590 if (!intobj)
2591 return -1;
2592 intval = PyInt_AsLong(intobj);
2593 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002594 if (intval == -1 && PyErr_Occurred())
2595 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002596 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002597 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002598 if (*usec < 0)
2599 /* If rounding gave us a negative number,
2600 truncate. */
2601 *usec = 0;
2602 return 0;
2603 }
2604 intval = PyInt_AsLong(t);
2605 if (intval == -1 && PyErr_Occurred())
2606 return -1;
2607 *sec = intval;
2608 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002609 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002610}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002612PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002613"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002614utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002615Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002616second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002617
Barry Warsaw53699e91996-12-10 23:23:01 +00002618static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002619posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002620{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002621#ifdef Py_WIN_WIDE_FILENAMES
2622 PyObject *arg;
2623 PyUnicodeObject *obwpath;
2624 wchar_t *wpath = NULL;
2625 char *apath = NULL;
2626 HANDLE hFile;
2627 long atimesec, mtimesec, ausec, musec;
2628 FILETIME atime, mtime;
2629 PyObject *result = NULL;
2630
2631 if (unicode_file_names()) {
2632 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2633 wpath = PyUnicode_AS_UNICODE(obwpath);
2634 Py_BEGIN_ALLOW_THREADS
2635 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002636 NULL, OPEN_EXISTING,
2637 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002638 Py_END_ALLOW_THREADS
2639 if (hFile == INVALID_HANDLE_VALUE)
2640 return win32_error_unicode("utime", wpath);
2641 } else
2642 /* Drop the argument parsing error as narrow strings
2643 are also valid. */
2644 PyErr_Clear();
2645 }
2646 if (!wpath) {
2647 if (!PyArg_ParseTuple(args, "etO:utime",
2648 Py_FileSystemDefaultEncoding, &apath, &arg))
2649 return NULL;
2650 Py_BEGIN_ALLOW_THREADS
2651 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002652 NULL, OPEN_EXISTING,
2653 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002654 Py_END_ALLOW_THREADS
2655 if (hFile == INVALID_HANDLE_VALUE) {
2656 win32_error("utime", apath);
2657 PyMem_Free(apath);
2658 return NULL;
2659 }
2660 PyMem_Free(apath);
2661 }
2662
2663 if (arg == Py_None) {
2664 SYSTEMTIME now;
2665 GetSystemTime(&now);
2666 if (!SystemTimeToFileTime(&now, &mtime) ||
2667 !SystemTimeToFileTime(&now, &atime)) {
2668 win32_error("utime", NULL);
2669 goto done;
2670 }
2671 }
2672 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2673 PyErr_SetString(PyExc_TypeError,
2674 "utime() arg 2 must be a tuple (atime, mtime)");
2675 goto done;
2676 }
2677 else {
2678 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2679 &atimesec, &ausec) == -1)
2680 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002681 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002682 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2683 &mtimesec, &musec) == -1)
2684 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002685 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002686 }
2687 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2688 /* Avoid putting the file name into the error here,
2689 as that may confuse the user into believing that
2690 something is wrong with the file, when it also
2691 could be the time stamp that gives a problem. */
2692 win32_error("utime", NULL);
2693 }
2694 Py_INCREF(Py_None);
2695 result = Py_None;
2696done:
2697 CloseHandle(hFile);
2698 return result;
2699#else /* Py_WIN_WIDE_FILENAMES */
2700
Neal Norwitz2adf2102004-06-09 01:46:02 +00002701 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002702 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002703 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002704 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002705
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002706#if defined(HAVE_UTIMES)
2707 struct timeval buf[2];
2708#define ATIME buf[0].tv_sec
2709#define MTIME buf[1].tv_sec
2710#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002711/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002712 struct utimbuf buf;
2713#define ATIME buf.actime
2714#define MTIME buf.modtime
2715#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002717 time_t buf[2];
2718#define ATIME buf[0]
2719#define MTIME buf[1]
2720#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002721#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002722
Mark Hammond817c9292003-12-03 01:22:38 +00002723
Thomas Wouters477c8d52006-05-27 19:21:47 +00002724 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002725 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002726 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002727 if (arg == Py_None) {
2728 /* optional time values not given */
2729 Py_BEGIN_ALLOW_THREADS
2730 res = utime(path, NULL);
2731 Py_END_ALLOW_THREADS
2732 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002733 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002734 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002735 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002736 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002737 return NULL;
2738 }
2739 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002740 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002741 &atime, &ausec) == -1) {
2742 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002743 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002744 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002745 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002746 &mtime, &musec) == -1) {
2747 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002748 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002749 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002750 ATIME = atime;
2751 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002752#ifdef HAVE_UTIMES
2753 buf[0].tv_usec = ausec;
2754 buf[1].tv_usec = musec;
2755 Py_BEGIN_ALLOW_THREADS
2756 res = utimes(path, buf);
2757 Py_END_ALLOW_THREADS
2758#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002759 Py_BEGIN_ALLOW_THREADS
2760 res = utime(path, UTIME_ARG);
2761 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002762#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002763 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002764 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002765 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002766 }
Neal Norwitz96652712004-06-06 20:40:27 +00002767 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002768 Py_INCREF(Py_None);
2769 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002770#undef UTIME_ARG
2771#undef ATIME
2772#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002773#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002774}
2775
Guido van Rossum85e3b011991-06-03 12:42:10 +00002776
Guido van Rossum3b066191991-06-04 19:40:25 +00002777/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002780"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002781Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002782
Barry Warsaw53699e91996-12-10 23:23:01 +00002783static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002784posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002785{
2786 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002787 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002788 return NULL;
2789 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002790 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002791}
2792
Martin v. Löwis114619e2002-10-07 06:44:21 +00002793#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2794static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002795free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002796{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002797 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002798 for (i = 0; i < count; i++)
2799 PyMem_Free(array[i]);
2800 PyMem_DEL(array);
2801}
2802#endif
2803
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002804
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002805#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002807"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808Execute an executable path with arguments, replacing current process.\n\
2809\n\
2810 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002811 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002812
Barry Warsaw53699e91996-12-10 23:23:01 +00002813static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002814posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002815{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002816 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002817 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002818 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002819 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002820 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002821
Guido van Rossum89b33251993-10-22 14:26:06 +00002822 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002823 argv is a list or tuple of strings. */
2824
Martin v. Löwis114619e2002-10-07 06:44:21 +00002825 if (!PyArg_ParseTuple(args, "etO:execv",
2826 Py_FileSystemDefaultEncoding,
2827 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002828 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002829 if (PyList_Check(argv)) {
2830 argc = PyList_Size(argv);
2831 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002832 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002833 else if (PyTuple_Check(argv)) {
2834 argc = PyTuple_Size(argv);
2835 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002836 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002837 else {
Fred Drake661ea262000-10-24 19:57:45 +00002838 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002839 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002840 return NULL;
2841 }
Thomas Heller6790d602007-08-30 17:15:14 +00002842 if (argc < 1) {
2843 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2844 PyMem_Free(path);
2845 return NULL;
2846 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002847
Barry Warsaw53699e91996-12-10 23:23:01 +00002848 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002849 if (argvlist == NULL) {
2850 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002851 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002852 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002853 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002854 if (!PyArg_Parse((*getitem)(argv, i), "et",
2855 Py_FileSystemDefaultEncoding,
2856 &argvlist[i])) {
2857 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002858 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002859 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002860 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002861 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002862
Guido van Rossum85e3b011991-06-03 12:42:10 +00002863 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002864 }
2865 argvlist[argc] = NULL;
2866
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002867 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002868
Guido van Rossum85e3b011991-06-03 12:42:10 +00002869 /* If we get here it's definitely an error */
2870
Martin v. Löwis114619e2002-10-07 06:44:21 +00002871 free_string_array(argvlist, argc);
2872 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002873 return posix_error();
2874}
2875
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002877PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002878"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002879Execute a path with arguments and environment, replacing current process.\n\
2880\n\
2881 path: path of executable file\n\
2882 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002883 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002884
Barry Warsaw53699e91996-12-10 23:23:01 +00002885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002886posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002887{
2888 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002889 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002890 char **argvlist;
2891 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002892 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002893 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002894 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002895 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002896
2897 /* execve has three arguments: (path, argv, env), where
2898 argv is a list or tuple of strings and env is a dictionary
2899 like posix.environ. */
2900
Martin v. Löwis114619e2002-10-07 06:44:21 +00002901 if (!PyArg_ParseTuple(args, "etOO:execve",
2902 Py_FileSystemDefaultEncoding,
2903 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002904 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002905 if (PyList_Check(argv)) {
2906 argc = PyList_Size(argv);
2907 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002908 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002909 else if (PyTuple_Check(argv)) {
2910 argc = PyTuple_Size(argv);
2911 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002912 }
2913 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002914 PyErr_SetString(PyExc_TypeError,
2915 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002916 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002917 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002918 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002919 PyErr_SetString(PyExc_TypeError,
2920 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002921 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002922 }
2923
Barry Warsaw53699e91996-12-10 23:23:01 +00002924 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002925 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002926 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002927 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002928 }
2929 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002930 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002931 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002932 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002933 &argvlist[i]))
2934 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002935 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002936 goto fail_1;
2937 }
2938 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002939 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002940 argvlist[argc] = NULL;
2941
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002942 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002943 if (i < 0)
2944 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002945 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002946 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002947 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002948 goto fail_1;
2949 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002950 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002951 keys = PyMapping_Keys(env);
2952 vals = PyMapping_Values(env);
2953 if (!keys || !vals)
2954 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002955 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2956 PyErr_SetString(PyExc_TypeError,
2957 "execve(): env.keys() or env.values() is not a list");
2958 goto fail_2;
2959 }
Tim Peters5aa91602002-01-30 05:46:57 +00002960
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002961 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002962 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002963 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002964
2965 key = PyList_GetItem(keys, pos);
2966 val = PyList_GetItem(vals, pos);
2967 if (!key || !val)
2968 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002969
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002970 if (!PyArg_Parse(
2971 key,
2972 "s;execve() arg 3 contains a non-string key",
2973 &k) ||
2974 !PyArg_Parse(
2975 val,
2976 "s;execve() arg 3 contains a non-string value",
2977 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002978 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002979 goto fail_2;
2980 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002981
2982#if defined(PYOS_OS2)
2983 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2984 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2985#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002986 len = PyString_Size(key) + PyString_Size(val) + 2;
2987 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002988 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002989 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002990 goto fail_2;
2991 }
Tim Petersc8996f52001-12-03 20:41:00 +00002992 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002993 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002994#if defined(PYOS_OS2)
2995 }
2996#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002997 }
2998 envlist[envc] = 0;
2999
3000 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003001
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003002 /* If we get here it's definitely an error */
3003
3004 (void) posix_error();
3005
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003006 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003007 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003008 PyMem_DEL(envlist[envc]);
3009 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003010 fail_1:
3011 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003012 Py_XDECREF(vals);
3013 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003014 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003015 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003016 return NULL;
3017}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003018#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003019
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003020
Guido van Rossuma1065681999-01-25 23:20:23 +00003021#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003022PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003023"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003024Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003025\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003026 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003027 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003028 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003029
3030static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003031posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003032{
3033 char *path;
3034 PyObject *argv;
3035 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003036 int mode, i;
3037 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003038 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003039 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003040
3041 /* spawnv has three arguments: (mode, path, argv), where
3042 argv is a list or tuple of strings. */
3043
Martin v. Löwis114619e2002-10-07 06:44:21 +00003044 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3045 Py_FileSystemDefaultEncoding,
3046 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003047 return NULL;
3048 if (PyList_Check(argv)) {
3049 argc = PyList_Size(argv);
3050 getitem = PyList_GetItem;
3051 }
3052 else if (PyTuple_Check(argv)) {
3053 argc = PyTuple_Size(argv);
3054 getitem = PyTuple_GetItem;
3055 }
3056 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003057 PyErr_SetString(PyExc_TypeError,
3058 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003059 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003060 return NULL;
3061 }
3062
3063 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003064 if (argvlist == NULL) {
3065 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003066 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003067 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003068 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003069 if (!PyArg_Parse((*getitem)(argv, i), "et",
3070 Py_FileSystemDefaultEncoding,
3071 &argvlist[i])) {
3072 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003073 PyErr_SetString(
3074 PyExc_TypeError,
3075 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003076 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003077 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003078 }
3079 }
3080 argvlist[argc] = NULL;
3081
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003082#if defined(PYOS_OS2) && defined(PYCC_GCC)
3083 Py_BEGIN_ALLOW_THREADS
3084 spawnval = spawnv(mode, path, argvlist);
3085 Py_END_ALLOW_THREADS
3086#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003087 if (mode == _OLD_P_OVERLAY)
3088 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003089
Tim Peters25059d32001-12-07 20:35:43 +00003090 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003091 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003092 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003093#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003094
Martin v. Löwis114619e2002-10-07 06:44:21 +00003095 free_string_array(argvlist, argc);
3096 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003097
Fred Drake699f3522000-06-29 21:12:41 +00003098 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003099 return posix_error();
3100 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003101#if SIZEOF_LONG == SIZEOF_VOID_P
3102 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003103#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003104 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003105#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003106}
3107
3108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003109PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003110"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003111Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003112\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003113 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003114 path: path of executable file\n\
3115 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003116 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003117
3118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003119posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003120{
3121 char *path;
3122 PyObject *argv, *env;
3123 char **argvlist;
3124 char **envlist;
3125 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003126 int mode, pos, envc;
3127 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003128 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003129 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003130 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003131
3132 /* spawnve has four arguments: (mode, path, argv, env), where
3133 argv is a list or tuple of strings and env is a dictionary
3134 like posix.environ. */
3135
Martin v. Löwis114619e2002-10-07 06:44:21 +00003136 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3137 Py_FileSystemDefaultEncoding,
3138 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003139 return NULL;
3140 if (PyList_Check(argv)) {
3141 argc = PyList_Size(argv);
3142 getitem = PyList_GetItem;
3143 }
3144 else if (PyTuple_Check(argv)) {
3145 argc = PyTuple_Size(argv);
3146 getitem = PyTuple_GetItem;
3147 }
3148 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003149 PyErr_SetString(PyExc_TypeError,
3150 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003151 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003152 }
3153 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003154 PyErr_SetString(PyExc_TypeError,
3155 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003156 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003157 }
3158
3159 argvlist = PyMem_NEW(char *, argc+1);
3160 if (argvlist == NULL) {
3161 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003162 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003163 }
3164 for (i = 0; i < argc; i++) {
3165 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003166 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003167 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003168 &argvlist[i]))
3169 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003170 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003171 goto fail_1;
3172 }
3173 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003174 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003175 argvlist[argc] = NULL;
3176
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003177 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003178 if (i < 0)
3179 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003180 envlist = PyMem_NEW(char *, i + 1);
3181 if (envlist == NULL) {
3182 PyErr_NoMemory();
3183 goto fail_1;
3184 }
3185 envc = 0;
3186 keys = PyMapping_Keys(env);
3187 vals = PyMapping_Values(env);
3188 if (!keys || !vals)
3189 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003190 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3191 PyErr_SetString(PyExc_TypeError,
3192 "spawnve(): env.keys() or env.values() is not a list");
3193 goto fail_2;
3194 }
Tim Peters5aa91602002-01-30 05:46:57 +00003195
Guido van Rossuma1065681999-01-25 23:20:23 +00003196 for (pos = 0; pos < i; pos++) {
3197 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003198 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003199
3200 key = PyList_GetItem(keys, pos);
3201 val = PyList_GetItem(vals, pos);
3202 if (!key || !val)
3203 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003204
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003205 if (!PyArg_Parse(
3206 key,
3207 "s;spawnve() arg 3 contains a non-string key",
3208 &k) ||
3209 !PyArg_Parse(
3210 val,
3211 "s;spawnve() arg 3 contains a non-string value",
3212 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003213 {
3214 goto fail_2;
3215 }
Tim Petersc8996f52001-12-03 20:41:00 +00003216 len = PyString_Size(key) + PyString_Size(val) + 2;
3217 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003218 if (p == NULL) {
3219 PyErr_NoMemory();
3220 goto fail_2;
3221 }
Tim Petersc8996f52001-12-03 20:41:00 +00003222 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003223 envlist[envc++] = p;
3224 }
3225 envlist[envc] = 0;
3226
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003227#if defined(PYOS_OS2) && defined(PYCC_GCC)
3228 Py_BEGIN_ALLOW_THREADS
3229 spawnval = spawnve(mode, path, argvlist, envlist);
3230 Py_END_ALLOW_THREADS
3231#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003232 if (mode == _OLD_P_OVERLAY)
3233 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003234
3235 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003236 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003237 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003238#endif
Tim Peters25059d32001-12-07 20:35:43 +00003239
Fred Drake699f3522000-06-29 21:12:41 +00003240 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003241 (void) posix_error();
3242 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003243#if SIZEOF_LONG == SIZEOF_VOID_P
3244 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003245#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003246 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003247#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003248
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003249 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003250 while (--envc >= 0)
3251 PyMem_DEL(envlist[envc]);
3252 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003253 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003254 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003255 Py_XDECREF(vals);
3256 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003257 fail_0:
3258 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003259 return res;
3260}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003261
3262/* OS/2 supports spawnvp & spawnvpe natively */
3263#if defined(PYOS_OS2)
3264PyDoc_STRVAR(posix_spawnvp__doc__,
3265"spawnvp(mode, file, args)\n\n\
3266Execute the program 'file' in a new process, using the environment\n\
3267search path to find the file.\n\
3268\n\
3269 mode: mode of process creation\n\
3270 file: executable file name\n\
3271 args: tuple or list of strings");
3272
3273static PyObject *
3274posix_spawnvp(PyObject *self, PyObject *args)
3275{
3276 char *path;
3277 PyObject *argv;
3278 char **argvlist;
3279 int mode, i, argc;
3280 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003281 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003282
3283 /* spawnvp has three arguments: (mode, path, argv), where
3284 argv is a list or tuple of strings. */
3285
3286 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3287 Py_FileSystemDefaultEncoding,
3288 &path, &argv))
3289 return NULL;
3290 if (PyList_Check(argv)) {
3291 argc = PyList_Size(argv);
3292 getitem = PyList_GetItem;
3293 }
3294 else if (PyTuple_Check(argv)) {
3295 argc = PyTuple_Size(argv);
3296 getitem = PyTuple_GetItem;
3297 }
3298 else {
3299 PyErr_SetString(PyExc_TypeError,
3300 "spawnvp() arg 2 must be a tuple or list");
3301 PyMem_Free(path);
3302 return NULL;
3303 }
3304
3305 argvlist = PyMem_NEW(char *, argc+1);
3306 if (argvlist == NULL) {
3307 PyMem_Free(path);
3308 return PyErr_NoMemory();
3309 }
3310 for (i = 0; i < argc; i++) {
3311 if (!PyArg_Parse((*getitem)(argv, i), "et",
3312 Py_FileSystemDefaultEncoding,
3313 &argvlist[i])) {
3314 free_string_array(argvlist, i);
3315 PyErr_SetString(
3316 PyExc_TypeError,
3317 "spawnvp() arg 2 must contain only strings");
3318 PyMem_Free(path);
3319 return NULL;
3320 }
3321 }
3322 argvlist[argc] = NULL;
3323
3324 Py_BEGIN_ALLOW_THREADS
3325#if defined(PYCC_GCC)
3326 spawnval = spawnvp(mode, path, argvlist);
3327#else
3328 spawnval = _spawnvp(mode, path, argvlist);
3329#endif
3330 Py_END_ALLOW_THREADS
3331
3332 free_string_array(argvlist, argc);
3333 PyMem_Free(path);
3334
3335 if (spawnval == -1)
3336 return posix_error();
3337 else
3338 return Py_BuildValue("l", (long) spawnval);
3339}
3340
3341
3342PyDoc_STRVAR(posix_spawnvpe__doc__,
3343"spawnvpe(mode, file, args, env)\n\n\
3344Execute the program 'file' in a new process, using the environment\n\
3345search path to find the file.\n\
3346\n\
3347 mode: mode of process creation\n\
3348 file: executable file name\n\
3349 args: tuple or list of arguments\n\
3350 env: dictionary of strings mapping to strings");
3351
3352static PyObject *
3353posix_spawnvpe(PyObject *self, PyObject *args)
3354{
3355 char *path;
3356 PyObject *argv, *env;
3357 char **argvlist;
3358 char **envlist;
3359 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3360 int mode, i, pos, argc, envc;
3361 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003362 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003363 int lastarg = 0;
3364
3365 /* spawnvpe has four arguments: (mode, path, argv, env), where
3366 argv is a list or tuple of strings and env is a dictionary
3367 like posix.environ. */
3368
3369 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3370 Py_FileSystemDefaultEncoding,
3371 &path, &argv, &env))
3372 return NULL;
3373 if (PyList_Check(argv)) {
3374 argc = PyList_Size(argv);
3375 getitem = PyList_GetItem;
3376 }
3377 else if (PyTuple_Check(argv)) {
3378 argc = PyTuple_Size(argv);
3379 getitem = PyTuple_GetItem;
3380 }
3381 else {
3382 PyErr_SetString(PyExc_TypeError,
3383 "spawnvpe() arg 2 must be a tuple or list");
3384 goto fail_0;
3385 }
3386 if (!PyMapping_Check(env)) {
3387 PyErr_SetString(PyExc_TypeError,
3388 "spawnvpe() arg 3 must be a mapping object");
3389 goto fail_0;
3390 }
3391
3392 argvlist = PyMem_NEW(char *, argc+1);
3393 if (argvlist == NULL) {
3394 PyErr_NoMemory();
3395 goto fail_0;
3396 }
3397 for (i = 0; i < argc; i++) {
3398 if (!PyArg_Parse((*getitem)(argv, i),
3399 "et;spawnvpe() arg 2 must contain only strings",
3400 Py_FileSystemDefaultEncoding,
3401 &argvlist[i]))
3402 {
3403 lastarg = i;
3404 goto fail_1;
3405 }
3406 }
3407 lastarg = argc;
3408 argvlist[argc] = NULL;
3409
3410 i = PyMapping_Size(env);
3411 if (i < 0)
3412 goto fail_1;
3413 envlist = PyMem_NEW(char *, i + 1);
3414 if (envlist == NULL) {
3415 PyErr_NoMemory();
3416 goto fail_1;
3417 }
3418 envc = 0;
3419 keys = PyMapping_Keys(env);
3420 vals = PyMapping_Values(env);
3421 if (!keys || !vals)
3422 goto fail_2;
3423 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3424 PyErr_SetString(PyExc_TypeError,
3425 "spawnvpe(): env.keys() or env.values() is not a list");
3426 goto fail_2;
3427 }
3428
3429 for (pos = 0; pos < i; pos++) {
3430 char *p, *k, *v;
3431 size_t len;
3432
3433 key = PyList_GetItem(keys, pos);
3434 val = PyList_GetItem(vals, pos);
3435 if (!key || !val)
3436 goto fail_2;
3437
3438 if (!PyArg_Parse(
3439 key,
3440 "s;spawnvpe() arg 3 contains a non-string key",
3441 &k) ||
3442 !PyArg_Parse(
3443 val,
3444 "s;spawnvpe() arg 3 contains a non-string value",
3445 &v))
3446 {
3447 goto fail_2;
3448 }
3449 len = PyString_Size(key) + PyString_Size(val) + 2;
3450 p = PyMem_NEW(char, len);
3451 if (p == NULL) {
3452 PyErr_NoMemory();
3453 goto fail_2;
3454 }
3455 PyOS_snprintf(p, len, "%s=%s", k, v);
3456 envlist[envc++] = p;
3457 }
3458 envlist[envc] = 0;
3459
3460 Py_BEGIN_ALLOW_THREADS
3461#if defined(PYCC_GCC)
3462 spawnval = spawnve(mode, path, argvlist, envlist);
3463#else
3464 spawnval = _spawnve(mode, path, argvlist, envlist);
3465#endif
3466 Py_END_ALLOW_THREADS
3467
3468 if (spawnval == -1)
3469 (void) posix_error();
3470 else
3471 res = Py_BuildValue("l", (long) spawnval);
3472
3473 fail_2:
3474 while (--envc >= 0)
3475 PyMem_DEL(envlist[envc]);
3476 PyMem_DEL(envlist);
3477 fail_1:
3478 free_string_array(argvlist, lastarg);
3479 Py_XDECREF(vals);
3480 Py_XDECREF(keys);
3481 fail_0:
3482 PyMem_Free(path);
3483 return res;
3484}
3485#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003486#endif /* HAVE_SPAWNV */
3487
3488
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003489#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003490PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003491"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003492Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3493\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003494Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003495
3496static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003497posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003498{
Neal Norwitze241ce82003-02-17 18:17:05 +00003499 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003500 if (pid == -1)
3501 return posix_error();
3502 PyOS_AfterFork();
3503 return PyInt_FromLong((long)pid);
3504}
3505#endif
3506
3507
Guido van Rossumad0ee831995-03-01 10:34:45 +00003508#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003509PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003510"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003511Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003512Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003513
Barry Warsaw53699e91996-12-10 23:23:01 +00003514static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003515posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003516{
Neal Norwitze241ce82003-02-17 18:17:05 +00003517 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003518 if (pid == -1)
3519 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003520 if (pid == 0)
3521 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003522 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003523}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003524#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003525
Neal Norwitzb59798b2003-03-21 01:43:31 +00003526/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003527/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3528#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003529#define DEV_PTY_FILE "/dev/ptc"
3530#define HAVE_DEV_PTMX
3531#else
3532#define DEV_PTY_FILE "/dev/ptmx"
3533#endif
3534
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003535#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003536#ifdef HAVE_PTY_H
3537#include <pty.h>
3538#else
3539#ifdef HAVE_LIBUTIL_H
3540#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003541#endif /* HAVE_LIBUTIL_H */
3542#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003543#ifdef HAVE_STROPTS_H
3544#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003545#endif
3546#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003547
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003548#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003549PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003550"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003551Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003552
3553static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003554posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003555{
3556 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003557#ifndef HAVE_OPENPTY
3558 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003559#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003560#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003561 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003562#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003563 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003564#endif
3565#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003566
Thomas Wouters70c21a12000-07-14 14:28:33 +00003567#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003568 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3569 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003570#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003571 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3572 if (slave_name == NULL)
3573 return posix_error();
3574
3575 slave_fd = open(slave_name, O_RDWR);
3576 if (slave_fd < 0)
3577 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003578#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003579 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003580 if (master_fd < 0)
3581 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003582 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003583 /* change permission of slave */
3584 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003585 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003586 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003587 }
3588 /* unlock slave */
3589 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003590 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003591 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003592 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003593 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003594 slave_name = ptsname(master_fd); /* get name of slave */
3595 if (slave_name == NULL)
3596 return posix_error();
3597 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3598 if (slave_fd < 0)
3599 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003600#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003601 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3602 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003603#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003604 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003605#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003606#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003607#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003608
Fred Drake8cef4cf2000-06-28 16:40:38 +00003609 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003610
Fred Drake8cef4cf2000-06-28 16:40:38 +00003611}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003612#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003613
3614#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003615PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003616"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003617Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3618Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003619To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003620
3621static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003622posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003623{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003624 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003625
Fred Drake8cef4cf2000-06-28 16:40:38 +00003626 pid = forkpty(&master_fd, NULL, NULL, NULL);
3627 if (pid == -1)
3628 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003629 if (pid == 0)
3630 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003631 return Py_BuildValue("(ii)", pid, master_fd);
3632}
3633#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003634
Guido van Rossumad0ee831995-03-01 10:34:45 +00003635#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003636PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003637"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003638Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003639
Barry Warsaw53699e91996-12-10 23:23:01 +00003640static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003641posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003642{
Barry Warsaw53699e91996-12-10 23:23:01 +00003643 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003644}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003645#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003646
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003647
Guido van Rossumad0ee831995-03-01 10:34:45 +00003648#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003649PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003650"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003651Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003652
Barry Warsaw53699e91996-12-10 23:23:01 +00003653static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003654posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003655{
Barry Warsaw53699e91996-12-10 23:23:01 +00003656 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003657}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003658#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003660
Guido van Rossumad0ee831995-03-01 10:34:45 +00003661#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003662PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003663"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003664Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003665
Barry Warsaw53699e91996-12-10 23:23:01 +00003666static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003667posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003668{
Barry Warsaw53699e91996-12-10 23:23:01 +00003669 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003670}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003671#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003672
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003674PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003675"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003676Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003677
Barry Warsaw53699e91996-12-10 23:23:01 +00003678static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003679posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003680{
Barry Warsaw53699e91996-12-10 23:23:01 +00003681 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003682}
3683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003684
Fred Drakec9680921999-12-13 16:37:25 +00003685#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003686PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003687"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003688Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003689
3690static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003691posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003692{
3693 PyObject *result = NULL;
3694
Fred Drakec9680921999-12-13 16:37:25 +00003695#ifdef NGROUPS_MAX
3696#define MAX_GROUPS NGROUPS_MAX
3697#else
3698 /* defined to be 16 on Solaris7, so this should be a small number */
3699#define MAX_GROUPS 64
3700#endif
3701 gid_t grouplist[MAX_GROUPS];
3702 int n;
3703
3704 n = getgroups(MAX_GROUPS, grouplist);
3705 if (n < 0)
3706 posix_error();
3707 else {
3708 result = PyList_New(n);
3709 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003710 int i;
3711 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003712 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003713 if (o == NULL) {
3714 Py_DECREF(result);
3715 result = NULL;
3716 break;
3717 }
3718 PyList_SET_ITEM(result, i, o);
3719 }
3720 }
3721 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003722
Fred Drakec9680921999-12-13 16:37:25 +00003723 return result;
3724}
3725#endif
3726
Martin v. Löwis606edc12002-06-13 21:09:11 +00003727#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003728PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003729"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003730Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003731
3732static PyObject *
3733posix_getpgid(PyObject *self, PyObject *args)
3734{
3735 int pid, pgid;
3736 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3737 return NULL;
3738 pgid = getpgid(pid);
3739 if (pgid < 0)
3740 return posix_error();
3741 return PyInt_FromLong((long)pgid);
3742}
3743#endif /* HAVE_GETPGID */
3744
3745
Guido van Rossumb6775db1994-08-01 11:34:53 +00003746#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003747PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003748"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003749Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003750
Barry Warsaw53699e91996-12-10 23:23:01 +00003751static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003752posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003753{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003754#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003755 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003756#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003757 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003758#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003759}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003760#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003762
Guido van Rossumb6775db1994-08-01 11:34:53 +00003763#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003764PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003765"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003766Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003767
Barry Warsaw53699e91996-12-10 23:23:01 +00003768static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003769posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003770{
Guido van Rossum64933891994-10-20 21:56:42 +00003771#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003772 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003773#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003774 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003775#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003776 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003777 Py_INCREF(Py_None);
3778 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003779}
3780
Guido van Rossumb6775db1994-08-01 11:34:53 +00003781#endif /* HAVE_SETPGRP */
3782
Guido van Rossumad0ee831995-03-01 10:34:45 +00003783#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003784PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003785"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003786Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003787
Barry Warsaw53699e91996-12-10 23:23:01 +00003788static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003789posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003790{
Barry Warsaw53699e91996-12-10 23:23:01 +00003791 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003792}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003793#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003794
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003795
Fred Drake12c6e2d1999-12-14 21:25:03 +00003796#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003797PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003798"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003799Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003800
3801static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003802posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003803{
Neal Norwitze241ce82003-02-17 18:17:05 +00003804 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003805 char *name;
3806 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003807
Fred Drakea30680b2000-12-06 21:24:28 +00003808 errno = 0;
3809 name = getlogin();
3810 if (name == NULL) {
3811 if (errno)
3812 posix_error();
3813 else
3814 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003815 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003816 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003817 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003818 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003819 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003820
Fred Drake12c6e2d1999-12-14 21:25:03 +00003821 return result;
3822}
3823#endif
3824
Guido van Rossumad0ee831995-03-01 10:34:45 +00003825#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003827"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003829
Barry Warsaw53699e91996-12-10 23:23:01 +00003830static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003831posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003832{
Barry Warsaw53699e91996-12-10 23:23:01 +00003833 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003834}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003835#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003836
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003837
Guido van Rossumad0ee831995-03-01 10:34:45 +00003838#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003839PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003840"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003841Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003842
Barry Warsaw53699e91996-12-10 23:23:01 +00003843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003844posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003845{
3846 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003847 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003848 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003849#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003850 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3851 APIRET rc;
3852 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003853 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003854
3855 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3856 APIRET rc;
3857 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003858 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003859
3860 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003861 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003862#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003863 if (kill(pid, sig) == -1)
3864 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003865#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003866 Py_INCREF(Py_None);
3867 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003868}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003869#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003870
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003871#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003872PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003873"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003874Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003875
3876static PyObject *
3877posix_killpg(PyObject *self, PyObject *args)
3878{
3879 int pgid, sig;
3880 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3881 return NULL;
3882 if (killpg(pgid, sig) == -1)
3883 return posix_error();
3884 Py_INCREF(Py_None);
3885 return Py_None;
3886}
3887#endif
3888
Guido van Rossumc0125471996-06-28 18:55:32 +00003889#ifdef HAVE_PLOCK
3890
3891#ifdef HAVE_SYS_LOCK_H
3892#include <sys/lock.h>
3893#endif
3894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003895PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003896"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003897Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003898
Barry Warsaw53699e91996-12-10 23:23:01 +00003899static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003900posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003901{
3902 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003903 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003904 return NULL;
3905 if (plock(op) == -1)
3906 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003907 Py_INCREF(Py_None);
3908 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003909}
3910#endif
3911
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003912
Guido van Rossum3b066191991-06-04 19:40:25 +00003913
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003914
Guido van Rossumb6775db1994-08-01 11:34:53 +00003915#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003916PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003917"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003918Set the current process's user id.");
3919
Barry Warsaw53699e91996-12-10 23:23:01 +00003920static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003921posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003922{
3923 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003924 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003925 return NULL;
3926 if (setuid(uid) < 0)
3927 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003928 Py_INCREF(Py_None);
3929 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003930}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003931#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003933
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003934#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003935PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003936"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003937Set the current process's effective user id.");
3938
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003939static PyObject *
3940posix_seteuid (PyObject *self, PyObject *args)
3941{
3942 int euid;
3943 if (!PyArg_ParseTuple(args, "i", &euid)) {
3944 return NULL;
3945 } else if (seteuid(euid) < 0) {
3946 return posix_error();
3947 } else {
3948 Py_INCREF(Py_None);
3949 return Py_None;
3950 }
3951}
3952#endif /* HAVE_SETEUID */
3953
3954#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003955PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003956"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003957Set the current process's effective group id.");
3958
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003959static PyObject *
3960posix_setegid (PyObject *self, PyObject *args)
3961{
3962 int egid;
3963 if (!PyArg_ParseTuple(args, "i", &egid)) {
3964 return NULL;
3965 } else if (setegid(egid) < 0) {
3966 return posix_error();
3967 } else {
3968 Py_INCREF(Py_None);
3969 return Py_None;
3970 }
3971}
3972#endif /* HAVE_SETEGID */
3973
3974#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003975PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003976"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003977Set the current process's real and effective user ids.");
3978
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003979static PyObject *
3980posix_setreuid (PyObject *self, PyObject *args)
3981{
3982 int ruid, euid;
3983 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
3984 return NULL;
3985 } else if (setreuid(ruid, euid) < 0) {
3986 return posix_error();
3987 } else {
3988 Py_INCREF(Py_None);
3989 return Py_None;
3990 }
3991}
3992#endif /* HAVE_SETREUID */
3993
3994#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003995PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003996"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003997Set the current process's real and effective group ids.");
3998
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003999static PyObject *
4000posix_setregid (PyObject *self, PyObject *args)
4001{
4002 int rgid, egid;
4003 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4004 return NULL;
4005 } else if (setregid(rgid, egid) < 0) {
4006 return posix_error();
4007 } else {
4008 Py_INCREF(Py_None);
4009 return Py_None;
4010 }
4011}
4012#endif /* HAVE_SETREGID */
4013
Guido van Rossumb6775db1994-08-01 11:34:53 +00004014#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004016"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004018
Barry Warsaw53699e91996-12-10 23:23:01 +00004019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004020posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004021{
4022 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004023 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004024 return NULL;
4025 if (setgid(gid) < 0)
4026 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004027 Py_INCREF(Py_None);
4028 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004029}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004030#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004031
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004032#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004033PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004034"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004035Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004036
4037static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004038posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004039{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004040 int i, len;
4041 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004042
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004043 if (!PySequence_Check(groups)) {
4044 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4045 return NULL;
4046 }
4047 len = PySequence_Size(groups);
4048 if (len > MAX_GROUPS) {
4049 PyErr_SetString(PyExc_ValueError, "too many groups");
4050 return NULL;
4051 }
4052 for(i = 0; i < len; i++) {
4053 PyObject *elem;
4054 elem = PySequence_GetItem(groups, i);
4055 if (!elem)
4056 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004057 if (!PyLong_Check(elem)) {
4058 PyErr_SetString(PyExc_TypeError,
4059 "groups must be integers");
4060 Py_DECREF(elem);
4061 return NULL;
4062 } else {
4063 unsigned long x = PyLong_AsUnsignedLong(elem);
4064 if (PyErr_Occurred()) {
4065 PyErr_SetString(PyExc_TypeError,
4066 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004067 Py_DECREF(elem);
4068 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004069 }
Georg Brandla13c2442005-11-22 19:30:31 +00004070 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004071 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004072 if (grouplist[i] != x) {
4073 PyErr_SetString(PyExc_TypeError,
4074 "group id too big");
4075 Py_DECREF(elem);
4076 return NULL;
4077 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004078 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004079 Py_DECREF(elem);
4080 }
4081
4082 if (setgroups(len, grouplist) < 0)
4083 return posix_error();
4084 Py_INCREF(Py_None);
4085 return Py_None;
4086}
4087#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004088
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004089#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4090static PyObject *
4091wait_helper(int pid, int status, struct rusage *ru)
4092{
4093 PyObject *result;
4094 static PyObject *struct_rusage;
4095
4096 if (pid == -1)
4097 return posix_error();
4098
4099 if (struct_rusage == NULL) {
4100 PyObject *m = PyImport_ImportModule("resource");
4101 if (m == NULL)
4102 return NULL;
4103 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4104 Py_DECREF(m);
4105 if (struct_rusage == NULL)
4106 return NULL;
4107 }
4108
4109 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4110 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4111 if (!result)
4112 return NULL;
4113
4114#ifndef doubletime
4115#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4116#endif
4117
4118 PyStructSequence_SET_ITEM(result, 0,
4119 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4120 PyStructSequence_SET_ITEM(result, 1,
4121 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4122#define SET_INT(result, index, value)\
4123 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4124 SET_INT(result, 2, ru->ru_maxrss);
4125 SET_INT(result, 3, ru->ru_ixrss);
4126 SET_INT(result, 4, ru->ru_idrss);
4127 SET_INT(result, 5, ru->ru_isrss);
4128 SET_INT(result, 6, ru->ru_minflt);
4129 SET_INT(result, 7, ru->ru_majflt);
4130 SET_INT(result, 8, ru->ru_nswap);
4131 SET_INT(result, 9, ru->ru_inblock);
4132 SET_INT(result, 10, ru->ru_oublock);
4133 SET_INT(result, 11, ru->ru_msgsnd);
4134 SET_INT(result, 12, ru->ru_msgrcv);
4135 SET_INT(result, 13, ru->ru_nsignals);
4136 SET_INT(result, 14, ru->ru_nvcsw);
4137 SET_INT(result, 15, ru->ru_nivcsw);
4138#undef SET_INT
4139
4140 if (PyErr_Occurred()) {
4141 Py_DECREF(result);
4142 return NULL;
4143 }
4144
4145 return Py_BuildValue("iiN", pid, status, result);
4146}
4147#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4148
4149#ifdef HAVE_WAIT3
4150PyDoc_STRVAR(posix_wait3__doc__,
4151"wait3(options) -> (pid, status, rusage)\n\n\
4152Wait for completion of a child process.");
4153
4154static PyObject *
4155posix_wait3(PyObject *self, PyObject *args)
4156{
4157 int pid, options;
4158 struct rusage ru;
4159 WAIT_TYPE status;
4160 WAIT_STATUS_INT(status) = 0;
4161
4162 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4163 return NULL;
4164
4165 Py_BEGIN_ALLOW_THREADS
4166 pid = wait3(&status, options, &ru);
4167 Py_END_ALLOW_THREADS
4168
4169 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4170}
4171#endif /* HAVE_WAIT3 */
4172
4173#ifdef HAVE_WAIT4
4174PyDoc_STRVAR(posix_wait4__doc__,
4175"wait4(pid, options) -> (pid, status, rusage)\n\n\
4176Wait for completion of a given child process.");
4177
4178static PyObject *
4179posix_wait4(PyObject *self, PyObject *args)
4180{
4181 int pid, options;
4182 struct rusage ru;
4183 WAIT_TYPE status;
4184 WAIT_STATUS_INT(status) = 0;
4185
4186 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4187 return NULL;
4188
4189 Py_BEGIN_ALLOW_THREADS
4190 pid = wait4(pid, &status, options, &ru);
4191 Py_END_ALLOW_THREADS
4192
4193 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4194}
4195#endif /* HAVE_WAIT4 */
4196
Guido van Rossumb6775db1994-08-01 11:34:53 +00004197#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004198PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004199"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004200Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004201
Barry Warsaw53699e91996-12-10 23:23:01 +00004202static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004203posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004204{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004205 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004206 WAIT_TYPE status;
4207 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004208
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004209 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004210 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004211 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004212 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004213 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004214 if (pid == -1)
4215 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004216
4217 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004218}
4219
Tim Petersab034fa2002-02-01 11:27:43 +00004220#elif defined(HAVE_CWAIT)
4221
4222/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004223PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004224"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004225"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004226
4227static PyObject *
4228posix_waitpid(PyObject *self, PyObject *args)
4229{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004230 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004231 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004232
4233 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4234 return NULL;
4235 Py_BEGIN_ALLOW_THREADS
4236 pid = _cwait(&status, pid, options);
4237 Py_END_ALLOW_THREADS
4238 if (pid == -1)
4239 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004240
4241 /* shift the status left a byte so this is more like the POSIX waitpid */
4242 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004243}
4244#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004245
Guido van Rossumad0ee831995-03-01 10:34:45 +00004246#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004247PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004248"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004249Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004250
Barry Warsaw53699e91996-12-10 23:23:01 +00004251static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004252posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004253{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004254 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004255 WAIT_TYPE status;
4256 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004257
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004258 Py_BEGIN_ALLOW_THREADS
4259 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004260 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004261 if (pid == -1)
4262 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004263
4264 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004265}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004266#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004269PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004270"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004271Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004272
Barry Warsaw53699e91996-12-10 23:23:01 +00004273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004274posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004275{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004276#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004277 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004278#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004279#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004280 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004281#else
4282 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4283#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004284#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004285}
4286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004287
Guido van Rossumb6775db1994-08-01 11:34:53 +00004288#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004289PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004290"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004291Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004292
Barry Warsaw53699e91996-12-10 23:23:01 +00004293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004294posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004295{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004296 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004297 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004298 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004299 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004300 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004301
4302 if (!PyArg_ParseTuple(args, "et:readlink",
4303 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004304 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004305 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004306 if (v == NULL) {
4307 PyMem_Free(path);
4308 return NULL;
4309 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004310
4311 if (PyUnicode_Check(v)) {
4312 arg_is_unicode = 1;
4313 }
4314 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004315
Barry Warsaw53699e91996-12-10 23:23:01 +00004316 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004317 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004318 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004319 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004320 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004321
Neal Norwitzfca70052007-08-12 16:56:02 +00004322 PyMem_Free(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004323 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004324 if (arg_is_unicode) {
4325 PyObject *w;
4326
4327 w = PyUnicode_FromEncodedObject(v,
4328 Py_FileSystemDefaultEncoding,
4329 "strict");
4330 if (w != NULL) {
4331 Py_DECREF(v);
4332 v = w;
4333 }
4334 else {
4335 /* fall back to the original byte string, as
4336 discussed in patch #683592 */
4337 PyErr_Clear();
4338 }
4339 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004340 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004341}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004342#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004343
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004344
Guido van Rossumb6775db1994-08-01 11:34:53 +00004345#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004346PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004347"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004348Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004349
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004351posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004352{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004353 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004354}
4355#endif /* HAVE_SYMLINK */
4356
4357
4358#ifdef HAVE_TIMES
4359#ifndef HZ
4360#define HZ 60 /* Universal constant :-) */
4361#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004362
Guido van Rossumd48f2521997-12-05 22:19:34 +00004363#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4364static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004365system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004366{
4367 ULONG value = 0;
4368
4369 Py_BEGIN_ALLOW_THREADS
4370 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4371 Py_END_ALLOW_THREADS
4372
4373 return value;
4374}
4375
4376static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004377posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004378{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004379 /* Currently Only Uptime is Provided -- Others Later */
4380 return Py_BuildValue("ddddd",
4381 (double)0 /* t.tms_utime / HZ */,
4382 (double)0 /* t.tms_stime / HZ */,
4383 (double)0 /* t.tms_cutime / HZ */,
4384 (double)0 /* t.tms_cstime / HZ */,
4385 (double)system_uptime() / 1000);
4386}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004387#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004388static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004389posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004390{
4391 struct tms t;
4392 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004393 errno = 0;
4394 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004395 if (c == (clock_t) -1)
4396 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004397 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004398 (double)t.tms_utime / HZ,
4399 (double)t.tms_stime / HZ,
4400 (double)t.tms_cutime / HZ,
4401 (double)t.tms_cstime / HZ,
4402 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004403}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004404#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004405#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004406
4407
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004408#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004409#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004410static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004411posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004412{
4413 FILETIME create, exit, kernel, user;
4414 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004415 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004416 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4417 /* The fields of a FILETIME structure are the hi and lo part
4418 of a 64-bit value expressed in 100 nanosecond units.
4419 1e7 is one second in such units; 1e-7 the inverse.
4420 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4421 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004422 return Py_BuildValue(
4423 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004424 (double)(kernel.dwHighDateTime*429.4967296 +
4425 kernel.dwLowDateTime*1e-7),
4426 (double)(user.dwHighDateTime*429.4967296 +
4427 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004428 (double)0,
4429 (double)0,
4430 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004431}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004432#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004433
4434#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004435PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004436"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004437Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004438#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004439
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004440
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004441#ifdef HAVE_GETSID
4442PyDoc_STRVAR(posix_getsid__doc__,
4443"getsid(pid) -> sid\n\n\
4444Call the system call getsid().");
4445
4446static PyObject *
4447posix_getsid(PyObject *self, PyObject *args)
4448{
4449 int pid, sid;
4450 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4451 return NULL;
4452 sid = getsid(pid);
4453 if (sid < 0)
4454 return posix_error();
4455 return PyInt_FromLong((long)sid);
4456}
4457#endif /* HAVE_GETSID */
4458
4459
Guido van Rossumb6775db1994-08-01 11:34:53 +00004460#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004461PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004462"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004463Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004464
Barry Warsaw53699e91996-12-10 23:23:01 +00004465static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004466posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004467{
Guido van Rossum687dd131993-05-17 08:34:16 +00004468 if (setsid() < 0)
4469 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004470 Py_INCREF(Py_None);
4471 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004472}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004473#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004474
Guido van Rossumb6775db1994-08-01 11:34:53 +00004475#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004476PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004477"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004478Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004479
Barry Warsaw53699e91996-12-10 23:23:01 +00004480static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004481posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004482{
4483 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004484 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004485 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004486 if (setpgid(pid, pgrp) < 0)
4487 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004488 Py_INCREF(Py_None);
4489 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004490}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004491#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004492
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004493
Guido van Rossumb6775db1994-08-01 11:34:53 +00004494#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004495PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004496"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004497Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004498
Barry Warsaw53699e91996-12-10 23:23:01 +00004499static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004500posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004501{
4502 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004503 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004504 return NULL;
4505 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004506 if (pgid < 0)
4507 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004508 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004509}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004510#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004511
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004512
Guido van Rossumb6775db1994-08-01 11:34:53 +00004513#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004514PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004515"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004516Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004517
Barry Warsaw53699e91996-12-10 23:23:01 +00004518static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004519posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004520{
4521 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004522 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004523 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004524 if (tcsetpgrp(fd, pgid) < 0)
4525 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004526 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004527 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004528}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004529#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004530
Guido van Rossum687dd131993-05-17 08:34:16 +00004531/* Functions acting on file descriptors */
4532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004533PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004534"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004535Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004536
Barry Warsaw53699e91996-12-10 23:23:01 +00004537static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004538posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004539{
Mark Hammondef8b6542001-05-13 08:04:26 +00004540 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004541 int flag;
4542 int mode = 0777;
4543 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004544
4545#ifdef MS_WINDOWS
4546 if (unicode_file_names()) {
4547 PyUnicodeObject *po;
4548 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4549 Py_BEGIN_ALLOW_THREADS
4550 /* PyUnicode_AS_UNICODE OK without thread
4551 lock as it is a simple dereference. */
4552 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4553 Py_END_ALLOW_THREADS
4554 if (fd < 0)
4555 return posix_error();
4556 return PyInt_FromLong((long)fd);
4557 }
4558 /* Drop the argument parsing error as narrow strings
4559 are also valid. */
4560 PyErr_Clear();
4561 }
4562#endif
4563
Tim Peters5aa91602002-01-30 05:46:57 +00004564 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004565 Py_FileSystemDefaultEncoding, &file,
4566 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004567 return NULL;
4568
Barry Warsaw53699e91996-12-10 23:23:01 +00004569 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004570 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004571 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004572 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004573 return posix_error_with_allocated_filename(file);
4574 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004575 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004576}
4577
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004578
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004579PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004580"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004581Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004582
Barry Warsaw53699e91996-12-10 23:23:01 +00004583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004584posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004585{
4586 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004587 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004588 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004589 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004590 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004591 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004592 if (res < 0)
4593 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004594 Py_INCREF(Py_None);
4595 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004596}
4597
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004599PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004600"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004601Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004602
Barry Warsaw53699e91996-12-10 23:23:01 +00004603static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004604posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004605{
4606 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004607 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004608 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004609 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004610 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004611 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004612 if (fd < 0)
4613 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004614 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004615}
4616
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004618PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004619"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004620Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004621
Barry Warsaw53699e91996-12-10 23:23:01 +00004622static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004623posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004624{
4625 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004626 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004627 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004628 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004629 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004630 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004631 if (res < 0)
4632 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004633 Py_INCREF(Py_None);
4634 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004635}
4636
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004638PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004639"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004640Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004641
Barry Warsaw53699e91996-12-10 23:23:01 +00004642static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004643posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004644{
4645 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004646#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004647 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004648#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004649 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004650#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004651 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004652 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004653 return NULL;
4654#ifdef SEEK_SET
4655 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4656 switch (how) {
4657 case 0: how = SEEK_SET; break;
4658 case 1: how = SEEK_CUR; break;
4659 case 2: how = SEEK_END; break;
4660 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004661#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004662
4663#if !defined(HAVE_LARGEFILE_SUPPORT)
4664 pos = PyInt_AsLong(posobj);
4665#else
4666 pos = PyLong_Check(posobj) ?
4667 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4668#endif
4669 if (PyErr_Occurred())
4670 return NULL;
4671
Barry Warsaw53699e91996-12-10 23:23:01 +00004672 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004673#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004674 res = _lseeki64(fd, pos, how);
4675#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004676 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004677#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004678 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004679 if (res < 0)
4680 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004681
4682#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004683 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004684#else
4685 return PyLong_FromLongLong(res);
4686#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004687}
4688
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004690PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004691"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004692Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004693
Barry Warsaw53699e91996-12-10 23:23:01 +00004694static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004695posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004696{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004697 int fd, size;
4698 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004700 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004701 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004702 if (size < 0) {
4703 errno = EINVAL;
4704 return posix_error();
4705 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00004706 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004707 if (buffer == NULL)
4708 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004709 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00004710 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004711 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004712 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004713 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004714 return posix_error();
4715 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004716 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00004717 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004718 return buffer;
4719}
4720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004722PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004723"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004724Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004725
Barry Warsaw53699e91996-12-10 23:23:01 +00004726static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004727posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004728{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004729 int fd;
4730 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004731 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004732
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004733 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004734 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004735 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004736 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004737 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004738 if (size < 0)
4739 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004740 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004741}
4742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004744PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004745"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004746Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004747
Barry Warsaw53699e91996-12-10 23:23:01 +00004748static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004749posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004750{
4751 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004752 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004753 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004754 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004755 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004756#ifdef __VMS
4757 /* on OpenVMS we must ensure that all bytes are written to the file */
4758 fsync(fd);
4759#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004760 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004761 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004762 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004763 if (res != 0) {
4764#ifdef MS_WINDOWS
4765 return win32_error("fstat", NULL);
4766#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004767 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004768#endif
4769 }
Tim Peters5aa91602002-01-30 05:46:57 +00004770
Martin v. Löwis14694662006-02-03 12:54:16 +00004771 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004772}
4773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004774PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004775"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004776Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004778
4779static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004780posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004781{
4782 int fd;
4783 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4784 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004785 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004786}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004787
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004788#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004789PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004790"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004791Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004792
Barry Warsaw53699e91996-12-10 23:23:01 +00004793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004794posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004795{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004796#if defined(PYOS_OS2)
4797 HFILE read, write;
4798 APIRET rc;
4799
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004800 Py_BEGIN_ALLOW_THREADS
4801 rc = DosCreatePipe( &read, &write, 4096);
4802 Py_END_ALLOW_THREADS
4803 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004804 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004805
4806 return Py_BuildValue("(ii)", read, write);
4807#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004808#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004809 int fds[2];
4810 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004811 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004812 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004813 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004814 if (res != 0)
4815 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004816 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004817#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004818 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004819 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004820 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004821 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004822 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004823 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004824 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004825 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004826 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4827 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004828 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004829#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004830#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004831}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004832#endif /* HAVE_PIPE */
4833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004834
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004835#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004836PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004837"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004838Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004839
Barry Warsaw53699e91996-12-10 23:23:01 +00004840static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004841posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004842{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004843 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004844 int mode = 0666;
4845 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004846 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004847 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004848 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004849 res = mkfifo(filename, mode);
4850 Py_END_ALLOW_THREADS
4851 if (res < 0)
4852 return posix_error();
4853 Py_INCREF(Py_None);
4854 return Py_None;
4855}
4856#endif
4857
4858
Neal Norwitz11690112002-07-30 01:08:28 +00004859#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004860PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004861"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004862Create a filesystem node (file, device special file or named pipe)\n\
4863named filename. mode specifies both the permissions to use and the\n\
4864type of node to be created, being combined (bitwise OR) with one of\n\
4865S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004866device defines the newly created device special file (probably using\n\
4867os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004868
4869
4870static PyObject *
4871posix_mknod(PyObject *self, PyObject *args)
4872{
4873 char *filename;
4874 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004875 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004876 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00004877 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004878 return NULL;
4879 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004880 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00004881 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004882 if (res < 0)
4883 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004884 Py_INCREF(Py_None);
4885 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004886}
4887#endif
4888
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004889#ifdef HAVE_DEVICE_MACROS
4890PyDoc_STRVAR(posix_major__doc__,
4891"major(device) -> major number\n\
4892Extracts a device major number from a raw device number.");
4893
4894static PyObject *
4895posix_major(PyObject *self, PyObject *args)
4896{
4897 int device;
4898 if (!PyArg_ParseTuple(args, "i:major", &device))
4899 return NULL;
4900 return PyInt_FromLong((long)major(device));
4901}
4902
4903PyDoc_STRVAR(posix_minor__doc__,
4904"minor(device) -> minor number\n\
4905Extracts a device minor number from a raw device number.");
4906
4907static PyObject *
4908posix_minor(PyObject *self, PyObject *args)
4909{
4910 int device;
4911 if (!PyArg_ParseTuple(args, "i:minor", &device))
4912 return NULL;
4913 return PyInt_FromLong((long)minor(device));
4914}
4915
4916PyDoc_STRVAR(posix_makedev__doc__,
4917"makedev(major, minor) -> device number\n\
4918Composes a raw device number from the major and minor device numbers.");
4919
4920static PyObject *
4921posix_makedev(PyObject *self, PyObject *args)
4922{
4923 int major, minor;
4924 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
4925 return NULL;
4926 return PyInt_FromLong((long)makedev(major, minor));
4927}
4928#endif /* device macros */
4929
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004930
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004931#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004932PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004933"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004934Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004935
Barry Warsaw53699e91996-12-10 23:23:01 +00004936static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004937posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004938{
4939 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004940 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004941 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004942 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004943
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004944 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004945 return NULL;
4946
4947#if !defined(HAVE_LARGEFILE_SUPPORT)
4948 length = PyInt_AsLong(lenobj);
4949#else
4950 length = PyLong_Check(lenobj) ?
4951 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
4952#endif
4953 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004954 return NULL;
4955
Barry Warsaw53699e91996-12-10 23:23:01 +00004956 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004957 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00004958 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004959 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004960 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004961 return NULL;
4962 }
Barry Warsaw53699e91996-12-10 23:23:01 +00004963 Py_INCREF(Py_None);
4964 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004965}
4966#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004967
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004968#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004969PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004970"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004971Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004972
Fred Drake762e2061999-08-26 17:23:54 +00004973/* Save putenv() parameters as values here, so we can collect them when they
4974 * get re-set with another call for the same key. */
4975static PyObject *posix_putenv_garbage;
4976
Tim Peters5aa91602002-01-30 05:46:57 +00004977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004978posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004979{
4980 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004981 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00004982 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00004983 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004984
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004985 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004986 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00004987
4988#if defined(PYOS_OS2)
4989 if (stricmp(s1, "BEGINLIBPATH") == 0) {
4990 APIRET rc;
4991
Guido van Rossumd48f2521997-12-05 22:19:34 +00004992 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
4993 if (rc != NO_ERROR)
4994 return os2_error(rc);
4995
4996 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
4997 APIRET rc;
4998
Guido van Rossumd48f2521997-12-05 22:19:34 +00004999 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5000 if (rc != NO_ERROR)
5001 return os2_error(rc);
5002 } else {
5003#endif
5004
Fred Drake762e2061999-08-26 17:23:54 +00005005 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005006 len = strlen(s1) + strlen(s2) + 2;
5007 /* len includes space for a trailing \0; the size arg to
5008 PyString_FromStringAndSize does not count that */
5009 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005010 if (newstr == NULL)
5011 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005012 newenv = PyString_AS_STRING(newstr);
5013 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5014 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005015 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005016 posix_error();
5017 return NULL;
5018 }
Fred Drake762e2061999-08-26 17:23:54 +00005019 /* Install the first arg and newstr in posix_putenv_garbage;
5020 * this will cause previous value to be collected. This has to
5021 * happen after the real putenv() call because the old value
5022 * was still accessible until then. */
5023 if (PyDict_SetItem(posix_putenv_garbage,
5024 PyTuple_GET_ITEM(args, 0), newstr)) {
5025 /* really not much we can do; just leak */
5026 PyErr_Clear();
5027 }
5028 else {
5029 Py_DECREF(newstr);
5030 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005031
5032#if defined(PYOS_OS2)
5033 }
5034#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005035 Py_INCREF(Py_None);
5036 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005037}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005038#endif /* putenv */
5039
Guido van Rossumc524d952001-10-19 01:31:59 +00005040#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005041PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005042"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005043Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005044
5045static PyObject *
5046posix_unsetenv(PyObject *self, PyObject *args)
5047{
5048 char *s1;
5049
5050 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5051 return NULL;
5052
5053 unsetenv(s1);
5054
5055 /* Remove the key from posix_putenv_garbage;
5056 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005057 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005058 * old value was still accessible until then.
5059 */
5060 if (PyDict_DelItem(posix_putenv_garbage,
5061 PyTuple_GET_ITEM(args, 0))) {
5062 /* really not much we can do; just leak */
5063 PyErr_Clear();
5064 }
5065
5066 Py_INCREF(Py_None);
5067 return Py_None;
5068}
5069#endif /* unsetenv */
5070
Guido van Rossumb6a47161997-09-15 22:54:34 +00005071#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005072PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005073"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005074Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005075
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005077posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005078{
5079 int code;
5080 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005081 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005082 return NULL;
5083 message = strerror(code);
5084 if (message == NULL) {
5085 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005086 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005087 return NULL;
5088 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005089 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005090}
5091#endif /* strerror */
5092
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005093
Guido van Rossumc9641791998-08-04 15:26:23 +00005094#ifdef HAVE_SYS_WAIT_H
5095
Fred Drake106c1a02002-04-23 15:58:02 +00005096#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005097PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005098"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005099Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005100
5101static PyObject *
5102posix_WCOREDUMP(PyObject *self, PyObject *args)
5103{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005104 WAIT_TYPE status;
5105 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005106
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005107 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005108 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005109
5110 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005111}
5112#endif /* WCOREDUMP */
5113
5114#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005115PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005116"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005117Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005118job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005119
5120static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005121posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005122{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005123 WAIT_TYPE status;
5124 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005125
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005126 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005127 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005128
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005129 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005130}
5131#endif /* WIFCONTINUED */
5132
Guido van Rossumc9641791998-08-04 15:26:23 +00005133#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005134PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005135"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005136Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005137
5138static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005139posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005140{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005141 WAIT_TYPE status;
5142 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005143
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005144 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005145 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005146
Fred Drake106c1a02002-04-23 15:58:02 +00005147 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005148}
5149#endif /* WIFSTOPPED */
5150
5151#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005152PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005153"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005154Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005155
5156static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005157posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005158{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005159 WAIT_TYPE status;
5160 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005161
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005162 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005163 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005164
Fred Drake106c1a02002-04-23 15:58:02 +00005165 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005166}
5167#endif /* WIFSIGNALED */
5168
5169#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005170PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005171"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005172Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005173system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005174
5175static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005176posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005177{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005178 WAIT_TYPE status;
5179 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005180
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005181 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005182 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005183
Fred Drake106c1a02002-04-23 15:58:02 +00005184 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005185}
5186#endif /* WIFEXITED */
5187
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005188#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005189PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005190"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005191Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005192
5193static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005194posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005195{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005196 WAIT_TYPE status;
5197 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005198
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005199 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005200 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005201
Guido van Rossumc9641791998-08-04 15:26:23 +00005202 return Py_BuildValue("i", WEXITSTATUS(status));
5203}
5204#endif /* WEXITSTATUS */
5205
5206#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005207PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005208"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005209Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005210value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005211
5212static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005213posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005214{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005215 WAIT_TYPE status;
5216 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005217
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005218 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005219 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005220
Guido van Rossumc9641791998-08-04 15:26:23 +00005221 return Py_BuildValue("i", WTERMSIG(status));
5222}
5223#endif /* WTERMSIG */
5224
5225#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005226PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005227"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005228Return the signal that stopped the process that provided\n\
5229the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005230
5231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005232posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005233{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005234 WAIT_TYPE status;
5235 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005236
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005237 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005238 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005239
Guido van Rossumc9641791998-08-04 15:26:23 +00005240 return Py_BuildValue("i", WSTOPSIG(status));
5241}
5242#endif /* WSTOPSIG */
5243
5244#endif /* HAVE_SYS_WAIT_H */
5245
5246
Thomas Wouters477c8d52006-05-27 19:21:47 +00005247#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005248#ifdef _SCO_DS
5249/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5250 needed definitions in sys/statvfs.h */
5251#define _SVID3
5252#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005253#include <sys/statvfs.h>
5254
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005255static PyObject*
5256_pystatvfs_fromstructstatvfs(struct statvfs st) {
5257 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5258 if (v == NULL)
5259 return NULL;
5260
5261#if !defined(HAVE_LARGEFILE_SUPPORT)
5262 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5263 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5264 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5265 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5266 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5267 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5268 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5269 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5270 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5271 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5272#else
5273 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5274 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005275 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005276 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005277 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005278 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005279 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005280 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005281 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005282 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005283 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005284 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005285 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005286 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005287 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5288 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5289#endif
5290
5291 return v;
5292}
5293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005294PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005295"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005296Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005297
5298static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005299posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005300{
5301 int fd, res;
5302 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005303
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005304 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005305 return NULL;
5306 Py_BEGIN_ALLOW_THREADS
5307 res = fstatvfs(fd, &st);
5308 Py_END_ALLOW_THREADS
5309 if (res != 0)
5310 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005311
5312 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005313}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005314#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005315
5316
Thomas Wouters477c8d52006-05-27 19:21:47 +00005317#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005318#include <sys/statvfs.h>
5319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005320PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005321"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005322Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005323
5324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005325posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005326{
5327 char *path;
5328 int res;
5329 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005330 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005331 return NULL;
5332 Py_BEGIN_ALLOW_THREADS
5333 res = statvfs(path, &st);
5334 Py_END_ALLOW_THREADS
5335 if (res != 0)
5336 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005337
5338 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005339}
5340#endif /* HAVE_STATVFS */
5341
5342
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005343#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005344PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005345"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005346Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005347The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005348or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005349
5350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005351posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005352{
5353 PyObject *result = NULL;
5354 char *dir = NULL;
5355 char *pfx = NULL;
5356 char *name;
5357
5358 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5359 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005360
Skip Montanaro46fc3372007-08-12 11:44:53 +00005361 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5362 "tempnam is a potential security risk to your program",
5363 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005364 return NULL;
5365
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005366#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005367 name = _tempnam(dir, pfx);
5368#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005369 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005370#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005371 if (name == NULL)
5372 return PyErr_NoMemory();
5373 result = PyString_FromString(name);
5374 free(name);
5375 return result;
5376}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005377#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005378
5379
5380#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005381PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005382"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005383Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005384
5385static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005386posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005387{
5388 FILE *fp;
5389
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005390 fp = tmpfile();
5391 if (fp == NULL)
5392 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005393 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005394}
5395#endif
5396
5397
5398#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005399PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005400"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005401Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005402
5403static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005404posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005405{
5406 char buffer[L_tmpnam];
5407 char *name;
5408
Skip Montanaro46fc3372007-08-12 11:44:53 +00005409 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5410 "tmpnam is a potential security risk to your program",
5411 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005412 return NULL;
5413
Greg Wardb48bc172000-03-01 21:51:56 +00005414#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005415 name = tmpnam_r(buffer);
5416#else
5417 name = tmpnam(buffer);
5418#endif
5419 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005420 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005421#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005422 "unexpected NULL from tmpnam_r"
5423#else
5424 "unexpected NULL from tmpnam"
5425#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005426 );
5427 PyErr_SetObject(PyExc_OSError, err);
5428 Py_XDECREF(err);
5429 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005430 }
5431 return PyString_FromString(buffer);
5432}
5433#endif
5434
5435
Fred Drakec9680921999-12-13 16:37:25 +00005436/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5437 * It maps strings representing configuration variable names to
5438 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005439 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005440 * rarely-used constants. There are three separate tables that use
5441 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005442 *
5443 * This code is always included, even if none of the interfaces that
5444 * need it are included. The #if hackery needed to avoid it would be
5445 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005446 */
5447struct constdef {
5448 char *name;
5449 long value;
5450};
5451
Fred Drake12c6e2d1999-12-14 21:25:03 +00005452static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005453conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005454 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005455{
5456 if (PyInt_Check(arg)) {
5457 *valuep = PyInt_AS_LONG(arg);
5458 return 1;
5459 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005460 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005461 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005462 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005463 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005464 size_t hi = tablesize;
5465 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005466 const char *confname;
5467 Py_ssize_t namelen;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005468 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005469 PyErr_SetString(PyExc_TypeError,
5470 "configuration names must be strings or integers");
5471 return 0;
5472 }
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005473 confname = PyUnicode_AsString(arg);
5474 if (confname == NULL)
5475 return 0;
5476 namelen = strlen(confname);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005477 while (lo < hi) {
5478 mid = (lo + hi) / 2;
5479 cmp = strcmp(confname, table[mid].name);
5480 if (cmp < 0)
5481 hi = mid;
5482 else if (cmp > 0)
5483 lo = mid + 1;
5484 else {
5485 *valuep = table[mid].value;
5486 return 1;
5487 }
5488 }
5489 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005490 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005491 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005492}
5493
5494
5495#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5496static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005497#ifdef _PC_ABI_AIO_XFER_MAX
5498 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5499#endif
5500#ifdef _PC_ABI_ASYNC_IO
5501 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5502#endif
Fred Drakec9680921999-12-13 16:37:25 +00005503#ifdef _PC_ASYNC_IO
5504 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5505#endif
5506#ifdef _PC_CHOWN_RESTRICTED
5507 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5508#endif
5509#ifdef _PC_FILESIZEBITS
5510 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5511#endif
5512#ifdef _PC_LAST
5513 {"PC_LAST", _PC_LAST},
5514#endif
5515#ifdef _PC_LINK_MAX
5516 {"PC_LINK_MAX", _PC_LINK_MAX},
5517#endif
5518#ifdef _PC_MAX_CANON
5519 {"PC_MAX_CANON", _PC_MAX_CANON},
5520#endif
5521#ifdef _PC_MAX_INPUT
5522 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5523#endif
5524#ifdef _PC_NAME_MAX
5525 {"PC_NAME_MAX", _PC_NAME_MAX},
5526#endif
5527#ifdef _PC_NO_TRUNC
5528 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5529#endif
5530#ifdef _PC_PATH_MAX
5531 {"PC_PATH_MAX", _PC_PATH_MAX},
5532#endif
5533#ifdef _PC_PIPE_BUF
5534 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5535#endif
5536#ifdef _PC_PRIO_IO
5537 {"PC_PRIO_IO", _PC_PRIO_IO},
5538#endif
5539#ifdef _PC_SOCK_MAXBUF
5540 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5541#endif
5542#ifdef _PC_SYNC_IO
5543 {"PC_SYNC_IO", _PC_SYNC_IO},
5544#endif
5545#ifdef _PC_VDISABLE
5546 {"PC_VDISABLE", _PC_VDISABLE},
5547#endif
5548};
5549
Fred Drakec9680921999-12-13 16:37:25 +00005550static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005551conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005552{
5553 return conv_confname(arg, valuep, posix_constants_pathconf,
5554 sizeof(posix_constants_pathconf)
5555 / sizeof(struct constdef));
5556}
5557#endif
5558
5559#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005561"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005562Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005563If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005564
5565static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005566posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005567{
5568 PyObject *result = NULL;
5569 int name, fd;
5570
Fred Drake12c6e2d1999-12-14 21:25:03 +00005571 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5572 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005573 long limit;
5574
5575 errno = 0;
5576 limit = fpathconf(fd, name);
5577 if (limit == -1 && errno != 0)
5578 posix_error();
5579 else
5580 result = PyInt_FromLong(limit);
5581 }
5582 return result;
5583}
5584#endif
5585
5586
5587#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005588PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005589"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005590Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005591If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005592
5593static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005594posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005595{
5596 PyObject *result = NULL;
5597 int name;
5598 char *path;
5599
5600 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5601 conv_path_confname, &name)) {
5602 long limit;
5603
5604 errno = 0;
5605 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005606 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005607 if (errno == EINVAL)
5608 /* could be a path or name problem */
5609 posix_error();
5610 else
5611 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005612 }
Fred Drakec9680921999-12-13 16:37:25 +00005613 else
5614 result = PyInt_FromLong(limit);
5615 }
5616 return result;
5617}
5618#endif
5619
5620#ifdef HAVE_CONFSTR
5621static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005622#ifdef _CS_ARCHITECTURE
5623 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5624#endif
5625#ifdef _CS_HOSTNAME
5626 {"CS_HOSTNAME", _CS_HOSTNAME},
5627#endif
5628#ifdef _CS_HW_PROVIDER
5629 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5630#endif
5631#ifdef _CS_HW_SERIAL
5632 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5633#endif
5634#ifdef _CS_INITTAB_NAME
5635 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5636#endif
Fred Drakec9680921999-12-13 16:37:25 +00005637#ifdef _CS_LFS64_CFLAGS
5638 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5639#endif
5640#ifdef _CS_LFS64_LDFLAGS
5641 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5642#endif
5643#ifdef _CS_LFS64_LIBS
5644 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5645#endif
5646#ifdef _CS_LFS64_LINTFLAGS
5647 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5648#endif
5649#ifdef _CS_LFS_CFLAGS
5650 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5651#endif
5652#ifdef _CS_LFS_LDFLAGS
5653 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5654#endif
5655#ifdef _CS_LFS_LIBS
5656 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5657#endif
5658#ifdef _CS_LFS_LINTFLAGS
5659 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5660#endif
Fred Draked86ed291999-12-15 15:34:33 +00005661#ifdef _CS_MACHINE
5662 {"CS_MACHINE", _CS_MACHINE},
5663#endif
Fred Drakec9680921999-12-13 16:37:25 +00005664#ifdef _CS_PATH
5665 {"CS_PATH", _CS_PATH},
5666#endif
Fred Draked86ed291999-12-15 15:34:33 +00005667#ifdef _CS_RELEASE
5668 {"CS_RELEASE", _CS_RELEASE},
5669#endif
5670#ifdef _CS_SRPC_DOMAIN
5671 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5672#endif
5673#ifdef _CS_SYSNAME
5674 {"CS_SYSNAME", _CS_SYSNAME},
5675#endif
5676#ifdef _CS_VERSION
5677 {"CS_VERSION", _CS_VERSION},
5678#endif
Fred Drakec9680921999-12-13 16:37:25 +00005679#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5680 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5681#endif
5682#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5683 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5684#endif
5685#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5686 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5687#endif
5688#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5689 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5690#endif
5691#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5692 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5693#endif
5694#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5695 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5696#endif
5697#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5698 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5699#endif
5700#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5701 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5702#endif
5703#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5704 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5705#endif
5706#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5707 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5708#endif
5709#ifdef _CS_XBS5_LP64_OFF64_LIBS
5710 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5711#endif
5712#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5713 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5714#endif
5715#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5716 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5717#endif
5718#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5719 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5720#endif
5721#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5722 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5723#endif
5724#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5725 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5726#endif
Fred Draked86ed291999-12-15 15:34:33 +00005727#ifdef _MIPS_CS_AVAIL_PROCESSORS
5728 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5729#endif
5730#ifdef _MIPS_CS_BASE
5731 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5732#endif
5733#ifdef _MIPS_CS_HOSTID
5734 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5735#endif
5736#ifdef _MIPS_CS_HW_NAME
5737 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5738#endif
5739#ifdef _MIPS_CS_NUM_PROCESSORS
5740 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5741#endif
5742#ifdef _MIPS_CS_OSREL_MAJ
5743 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5744#endif
5745#ifdef _MIPS_CS_OSREL_MIN
5746 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5747#endif
5748#ifdef _MIPS_CS_OSREL_PATCH
5749 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5750#endif
5751#ifdef _MIPS_CS_OS_NAME
5752 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5753#endif
5754#ifdef _MIPS_CS_OS_PROVIDER
5755 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5756#endif
5757#ifdef _MIPS_CS_PROCESSORS
5758 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5759#endif
5760#ifdef _MIPS_CS_SERIAL
5761 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5762#endif
5763#ifdef _MIPS_CS_VENDOR
5764 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5765#endif
Fred Drakec9680921999-12-13 16:37:25 +00005766};
5767
5768static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005769conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005770{
5771 return conv_confname(arg, valuep, posix_constants_confstr,
5772 sizeof(posix_constants_confstr)
5773 / sizeof(struct constdef));
5774}
5775
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005776PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005777"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005778Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005779
5780static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005781posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005782{
5783 PyObject *result = NULL;
5784 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005785 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005786
5787 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005788 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005789
Fred Drakec9680921999-12-13 16:37:25 +00005790 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005791 len = confstr(name, buffer, sizeof(buffer));
5792 if (len == 0) {
5793 if (errno) {
5794 posix_error();
5795 }
5796 else {
5797 result = Py_None;
5798 Py_INCREF(Py_None);
5799 }
Fred Drakec9680921999-12-13 16:37:25 +00005800 }
5801 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005802 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005803 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005804 if (result != NULL)
Neal Norwitz93c56822007-08-26 07:10:06 +00005805 confstr(name, PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005806 }
5807 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005808 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005809 }
5810 }
5811 return result;
5812}
5813#endif
5814
5815
5816#ifdef HAVE_SYSCONF
5817static struct constdef posix_constants_sysconf[] = {
5818#ifdef _SC_2_CHAR_TERM
5819 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5820#endif
5821#ifdef _SC_2_C_BIND
5822 {"SC_2_C_BIND", _SC_2_C_BIND},
5823#endif
5824#ifdef _SC_2_C_DEV
5825 {"SC_2_C_DEV", _SC_2_C_DEV},
5826#endif
5827#ifdef _SC_2_C_VERSION
5828 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5829#endif
5830#ifdef _SC_2_FORT_DEV
5831 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5832#endif
5833#ifdef _SC_2_FORT_RUN
5834 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5835#endif
5836#ifdef _SC_2_LOCALEDEF
5837 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5838#endif
5839#ifdef _SC_2_SW_DEV
5840 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5841#endif
5842#ifdef _SC_2_UPE
5843 {"SC_2_UPE", _SC_2_UPE},
5844#endif
5845#ifdef _SC_2_VERSION
5846 {"SC_2_VERSION", _SC_2_VERSION},
5847#endif
Fred Draked86ed291999-12-15 15:34:33 +00005848#ifdef _SC_ABI_ASYNCHRONOUS_IO
5849 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5850#endif
5851#ifdef _SC_ACL
5852 {"SC_ACL", _SC_ACL},
5853#endif
Fred Drakec9680921999-12-13 16:37:25 +00005854#ifdef _SC_AIO_LISTIO_MAX
5855 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5856#endif
Fred Drakec9680921999-12-13 16:37:25 +00005857#ifdef _SC_AIO_MAX
5858 {"SC_AIO_MAX", _SC_AIO_MAX},
5859#endif
5860#ifdef _SC_AIO_PRIO_DELTA_MAX
5861 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5862#endif
5863#ifdef _SC_ARG_MAX
5864 {"SC_ARG_MAX", _SC_ARG_MAX},
5865#endif
5866#ifdef _SC_ASYNCHRONOUS_IO
5867 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5868#endif
5869#ifdef _SC_ATEXIT_MAX
5870 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5871#endif
Fred Draked86ed291999-12-15 15:34:33 +00005872#ifdef _SC_AUDIT
5873 {"SC_AUDIT", _SC_AUDIT},
5874#endif
Fred Drakec9680921999-12-13 16:37:25 +00005875#ifdef _SC_AVPHYS_PAGES
5876 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5877#endif
5878#ifdef _SC_BC_BASE_MAX
5879 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5880#endif
5881#ifdef _SC_BC_DIM_MAX
5882 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5883#endif
5884#ifdef _SC_BC_SCALE_MAX
5885 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5886#endif
5887#ifdef _SC_BC_STRING_MAX
5888 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5889#endif
Fred Draked86ed291999-12-15 15:34:33 +00005890#ifdef _SC_CAP
5891 {"SC_CAP", _SC_CAP},
5892#endif
Fred Drakec9680921999-12-13 16:37:25 +00005893#ifdef _SC_CHARCLASS_NAME_MAX
5894 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5895#endif
5896#ifdef _SC_CHAR_BIT
5897 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5898#endif
5899#ifdef _SC_CHAR_MAX
5900 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5901#endif
5902#ifdef _SC_CHAR_MIN
5903 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5904#endif
5905#ifdef _SC_CHILD_MAX
5906 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5907#endif
5908#ifdef _SC_CLK_TCK
5909 {"SC_CLK_TCK", _SC_CLK_TCK},
5910#endif
5911#ifdef _SC_COHER_BLKSZ
5912 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5913#endif
5914#ifdef _SC_COLL_WEIGHTS_MAX
5915 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5916#endif
5917#ifdef _SC_DCACHE_ASSOC
5918 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5919#endif
5920#ifdef _SC_DCACHE_BLKSZ
5921 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5922#endif
5923#ifdef _SC_DCACHE_LINESZ
5924 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
5925#endif
5926#ifdef _SC_DCACHE_SZ
5927 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
5928#endif
5929#ifdef _SC_DCACHE_TBLKSZ
5930 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
5931#endif
5932#ifdef _SC_DELAYTIMER_MAX
5933 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
5934#endif
5935#ifdef _SC_EQUIV_CLASS_MAX
5936 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
5937#endif
5938#ifdef _SC_EXPR_NEST_MAX
5939 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
5940#endif
5941#ifdef _SC_FSYNC
5942 {"SC_FSYNC", _SC_FSYNC},
5943#endif
5944#ifdef _SC_GETGR_R_SIZE_MAX
5945 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
5946#endif
5947#ifdef _SC_GETPW_R_SIZE_MAX
5948 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
5949#endif
5950#ifdef _SC_ICACHE_ASSOC
5951 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
5952#endif
5953#ifdef _SC_ICACHE_BLKSZ
5954 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
5955#endif
5956#ifdef _SC_ICACHE_LINESZ
5957 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
5958#endif
5959#ifdef _SC_ICACHE_SZ
5960 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
5961#endif
Fred Draked86ed291999-12-15 15:34:33 +00005962#ifdef _SC_INF
5963 {"SC_INF", _SC_INF},
5964#endif
Fred Drakec9680921999-12-13 16:37:25 +00005965#ifdef _SC_INT_MAX
5966 {"SC_INT_MAX", _SC_INT_MAX},
5967#endif
5968#ifdef _SC_INT_MIN
5969 {"SC_INT_MIN", _SC_INT_MIN},
5970#endif
5971#ifdef _SC_IOV_MAX
5972 {"SC_IOV_MAX", _SC_IOV_MAX},
5973#endif
Fred Draked86ed291999-12-15 15:34:33 +00005974#ifdef _SC_IP_SECOPTS
5975 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
5976#endif
Fred Drakec9680921999-12-13 16:37:25 +00005977#ifdef _SC_JOB_CONTROL
5978 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
5979#endif
Fred Draked86ed291999-12-15 15:34:33 +00005980#ifdef _SC_KERN_POINTERS
5981 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
5982#endif
5983#ifdef _SC_KERN_SIM
5984 {"SC_KERN_SIM", _SC_KERN_SIM},
5985#endif
Fred Drakec9680921999-12-13 16:37:25 +00005986#ifdef _SC_LINE_MAX
5987 {"SC_LINE_MAX", _SC_LINE_MAX},
5988#endif
5989#ifdef _SC_LOGIN_NAME_MAX
5990 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
5991#endif
5992#ifdef _SC_LOGNAME_MAX
5993 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
5994#endif
5995#ifdef _SC_LONG_BIT
5996 {"SC_LONG_BIT", _SC_LONG_BIT},
5997#endif
Fred Draked86ed291999-12-15 15:34:33 +00005998#ifdef _SC_MAC
5999 {"SC_MAC", _SC_MAC},
6000#endif
Fred Drakec9680921999-12-13 16:37:25 +00006001#ifdef _SC_MAPPED_FILES
6002 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6003#endif
6004#ifdef _SC_MAXPID
6005 {"SC_MAXPID", _SC_MAXPID},
6006#endif
6007#ifdef _SC_MB_LEN_MAX
6008 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6009#endif
6010#ifdef _SC_MEMLOCK
6011 {"SC_MEMLOCK", _SC_MEMLOCK},
6012#endif
6013#ifdef _SC_MEMLOCK_RANGE
6014 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6015#endif
6016#ifdef _SC_MEMORY_PROTECTION
6017 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6018#endif
6019#ifdef _SC_MESSAGE_PASSING
6020 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6021#endif
Fred Draked86ed291999-12-15 15:34:33 +00006022#ifdef _SC_MMAP_FIXED_ALIGNMENT
6023 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6024#endif
Fred Drakec9680921999-12-13 16:37:25 +00006025#ifdef _SC_MQ_OPEN_MAX
6026 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6027#endif
6028#ifdef _SC_MQ_PRIO_MAX
6029 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6030#endif
Fred Draked86ed291999-12-15 15:34:33 +00006031#ifdef _SC_NACLS_MAX
6032 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6033#endif
Fred Drakec9680921999-12-13 16:37:25 +00006034#ifdef _SC_NGROUPS_MAX
6035 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6036#endif
6037#ifdef _SC_NL_ARGMAX
6038 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6039#endif
6040#ifdef _SC_NL_LANGMAX
6041 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6042#endif
6043#ifdef _SC_NL_MSGMAX
6044 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6045#endif
6046#ifdef _SC_NL_NMAX
6047 {"SC_NL_NMAX", _SC_NL_NMAX},
6048#endif
6049#ifdef _SC_NL_SETMAX
6050 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6051#endif
6052#ifdef _SC_NL_TEXTMAX
6053 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6054#endif
6055#ifdef _SC_NPROCESSORS_CONF
6056 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6057#endif
6058#ifdef _SC_NPROCESSORS_ONLN
6059 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6060#endif
Fred Draked86ed291999-12-15 15:34:33 +00006061#ifdef _SC_NPROC_CONF
6062 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6063#endif
6064#ifdef _SC_NPROC_ONLN
6065 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6066#endif
Fred Drakec9680921999-12-13 16:37:25 +00006067#ifdef _SC_NZERO
6068 {"SC_NZERO", _SC_NZERO},
6069#endif
6070#ifdef _SC_OPEN_MAX
6071 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6072#endif
6073#ifdef _SC_PAGESIZE
6074 {"SC_PAGESIZE", _SC_PAGESIZE},
6075#endif
6076#ifdef _SC_PAGE_SIZE
6077 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6078#endif
6079#ifdef _SC_PASS_MAX
6080 {"SC_PASS_MAX", _SC_PASS_MAX},
6081#endif
6082#ifdef _SC_PHYS_PAGES
6083 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6084#endif
6085#ifdef _SC_PII
6086 {"SC_PII", _SC_PII},
6087#endif
6088#ifdef _SC_PII_INTERNET
6089 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6090#endif
6091#ifdef _SC_PII_INTERNET_DGRAM
6092 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6093#endif
6094#ifdef _SC_PII_INTERNET_STREAM
6095 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6096#endif
6097#ifdef _SC_PII_OSI
6098 {"SC_PII_OSI", _SC_PII_OSI},
6099#endif
6100#ifdef _SC_PII_OSI_CLTS
6101 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6102#endif
6103#ifdef _SC_PII_OSI_COTS
6104 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6105#endif
6106#ifdef _SC_PII_OSI_M
6107 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6108#endif
6109#ifdef _SC_PII_SOCKET
6110 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6111#endif
6112#ifdef _SC_PII_XTI
6113 {"SC_PII_XTI", _SC_PII_XTI},
6114#endif
6115#ifdef _SC_POLL
6116 {"SC_POLL", _SC_POLL},
6117#endif
6118#ifdef _SC_PRIORITIZED_IO
6119 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6120#endif
6121#ifdef _SC_PRIORITY_SCHEDULING
6122 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6123#endif
6124#ifdef _SC_REALTIME_SIGNALS
6125 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6126#endif
6127#ifdef _SC_RE_DUP_MAX
6128 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6129#endif
6130#ifdef _SC_RTSIG_MAX
6131 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6132#endif
6133#ifdef _SC_SAVED_IDS
6134 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6135#endif
6136#ifdef _SC_SCHAR_MAX
6137 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6138#endif
6139#ifdef _SC_SCHAR_MIN
6140 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6141#endif
6142#ifdef _SC_SELECT
6143 {"SC_SELECT", _SC_SELECT},
6144#endif
6145#ifdef _SC_SEMAPHORES
6146 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6147#endif
6148#ifdef _SC_SEM_NSEMS_MAX
6149 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6150#endif
6151#ifdef _SC_SEM_VALUE_MAX
6152 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6153#endif
6154#ifdef _SC_SHARED_MEMORY_OBJECTS
6155 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6156#endif
6157#ifdef _SC_SHRT_MAX
6158 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6159#endif
6160#ifdef _SC_SHRT_MIN
6161 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6162#endif
6163#ifdef _SC_SIGQUEUE_MAX
6164 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6165#endif
6166#ifdef _SC_SIGRT_MAX
6167 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6168#endif
6169#ifdef _SC_SIGRT_MIN
6170 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6171#endif
Fred Draked86ed291999-12-15 15:34:33 +00006172#ifdef _SC_SOFTPOWER
6173 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6174#endif
Fred Drakec9680921999-12-13 16:37:25 +00006175#ifdef _SC_SPLIT_CACHE
6176 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6177#endif
6178#ifdef _SC_SSIZE_MAX
6179 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6180#endif
6181#ifdef _SC_STACK_PROT
6182 {"SC_STACK_PROT", _SC_STACK_PROT},
6183#endif
6184#ifdef _SC_STREAM_MAX
6185 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6186#endif
6187#ifdef _SC_SYNCHRONIZED_IO
6188 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6189#endif
6190#ifdef _SC_THREADS
6191 {"SC_THREADS", _SC_THREADS},
6192#endif
6193#ifdef _SC_THREAD_ATTR_STACKADDR
6194 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6195#endif
6196#ifdef _SC_THREAD_ATTR_STACKSIZE
6197 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6198#endif
6199#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6200 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6201#endif
6202#ifdef _SC_THREAD_KEYS_MAX
6203 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6204#endif
6205#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6206 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6207#endif
6208#ifdef _SC_THREAD_PRIO_INHERIT
6209 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6210#endif
6211#ifdef _SC_THREAD_PRIO_PROTECT
6212 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6213#endif
6214#ifdef _SC_THREAD_PROCESS_SHARED
6215 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6216#endif
6217#ifdef _SC_THREAD_SAFE_FUNCTIONS
6218 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6219#endif
6220#ifdef _SC_THREAD_STACK_MIN
6221 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6222#endif
6223#ifdef _SC_THREAD_THREADS_MAX
6224 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6225#endif
6226#ifdef _SC_TIMERS
6227 {"SC_TIMERS", _SC_TIMERS},
6228#endif
6229#ifdef _SC_TIMER_MAX
6230 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6231#endif
6232#ifdef _SC_TTY_NAME_MAX
6233 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6234#endif
6235#ifdef _SC_TZNAME_MAX
6236 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6237#endif
6238#ifdef _SC_T_IOV_MAX
6239 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6240#endif
6241#ifdef _SC_UCHAR_MAX
6242 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6243#endif
6244#ifdef _SC_UINT_MAX
6245 {"SC_UINT_MAX", _SC_UINT_MAX},
6246#endif
6247#ifdef _SC_UIO_MAXIOV
6248 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6249#endif
6250#ifdef _SC_ULONG_MAX
6251 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6252#endif
6253#ifdef _SC_USHRT_MAX
6254 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6255#endif
6256#ifdef _SC_VERSION
6257 {"SC_VERSION", _SC_VERSION},
6258#endif
6259#ifdef _SC_WORD_BIT
6260 {"SC_WORD_BIT", _SC_WORD_BIT},
6261#endif
6262#ifdef _SC_XBS5_ILP32_OFF32
6263 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6264#endif
6265#ifdef _SC_XBS5_ILP32_OFFBIG
6266 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6267#endif
6268#ifdef _SC_XBS5_LP64_OFF64
6269 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6270#endif
6271#ifdef _SC_XBS5_LPBIG_OFFBIG
6272 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6273#endif
6274#ifdef _SC_XOPEN_CRYPT
6275 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6276#endif
6277#ifdef _SC_XOPEN_ENH_I18N
6278 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6279#endif
6280#ifdef _SC_XOPEN_LEGACY
6281 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6282#endif
6283#ifdef _SC_XOPEN_REALTIME
6284 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6285#endif
6286#ifdef _SC_XOPEN_REALTIME_THREADS
6287 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6288#endif
6289#ifdef _SC_XOPEN_SHM
6290 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6291#endif
6292#ifdef _SC_XOPEN_UNIX
6293 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6294#endif
6295#ifdef _SC_XOPEN_VERSION
6296 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6297#endif
6298#ifdef _SC_XOPEN_XCU_VERSION
6299 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6300#endif
6301#ifdef _SC_XOPEN_XPG2
6302 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6303#endif
6304#ifdef _SC_XOPEN_XPG3
6305 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6306#endif
6307#ifdef _SC_XOPEN_XPG4
6308 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6309#endif
6310};
6311
6312static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006313conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006314{
6315 return conv_confname(arg, valuep, posix_constants_sysconf,
6316 sizeof(posix_constants_sysconf)
6317 / sizeof(struct constdef));
6318}
6319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006320PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006321"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006322Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006323
6324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006325posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006326{
6327 PyObject *result = NULL;
6328 int name;
6329
6330 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6331 int value;
6332
6333 errno = 0;
6334 value = sysconf(name);
6335 if (value == -1 && errno != 0)
6336 posix_error();
6337 else
6338 result = PyInt_FromLong(value);
6339 }
6340 return result;
6341}
6342#endif
6343
6344
Fred Drakebec628d1999-12-15 18:31:10 +00006345/* This code is used to ensure that the tables of configuration value names
6346 * are in sorted order as required by conv_confname(), and also to build the
6347 * the exported dictionaries that are used to publish information about the
6348 * names available on the host platform.
6349 *
6350 * Sorting the table at runtime ensures that the table is properly ordered
6351 * when used, even for platforms we're not able to test on. It also makes
6352 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006353 */
Fred Drakebec628d1999-12-15 18:31:10 +00006354
6355static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006356cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006357{
6358 const struct constdef *c1 =
6359 (const struct constdef *) v1;
6360 const struct constdef *c2 =
6361 (const struct constdef *) v2;
6362
6363 return strcmp(c1->name, c2->name);
6364}
6365
6366static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006367setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006368 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006369{
Fred Drakebec628d1999-12-15 18:31:10 +00006370 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006371 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006372
6373 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6374 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006375 if (d == NULL)
6376 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006377
Barry Warsaw3155db32000-04-13 15:20:40 +00006378 for (i=0; i < tablesize; ++i) {
6379 PyObject *o = PyInt_FromLong(table[i].value);
6380 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6381 Py_XDECREF(o);
6382 Py_DECREF(d);
6383 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006384 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006385 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006386 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006387 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006388}
6389
Fred Drakebec628d1999-12-15 18:31:10 +00006390/* Return -1 on failure, 0 on success. */
6391static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006392setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006393{
6394#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006395 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006396 sizeof(posix_constants_pathconf)
6397 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006398 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006399 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006400#endif
6401#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006402 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006403 sizeof(posix_constants_confstr)
6404 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006405 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006406 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006407#endif
6408#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006409 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006410 sizeof(posix_constants_sysconf)
6411 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006412 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006413 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006414#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006415 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006416}
Fred Draked86ed291999-12-15 15:34:33 +00006417
6418
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006419PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006420"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006421Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006422in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006423
6424static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006425posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006426{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006427 abort();
6428 /*NOTREACHED*/
6429 Py_FatalError("abort() called from Python code didn't abort!");
6430 return NULL;
6431}
Fred Drakebec628d1999-12-15 18:31:10 +00006432
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006433#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006435"startfile(filepath [, operation]) - Start a file with its associated\n\
6436application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006437\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006438When \"operation\" is not specified or \"open\", this acts like\n\
6439double-clicking the file in Explorer, or giving the file name as an\n\
6440argument to the DOS \"start\" command: the file is opened with whatever\n\
6441application (if any) its extension is associated.\n\
6442When another \"operation\" is given, it specifies what should be done with\n\
6443the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006444\n\
6445startfile returns as soon as the associated application is launched.\n\
6446There is no option to wait for the application to close, and no way\n\
6447to retrieve the application's exit status.\n\
6448\n\
6449The filepath is relative to the current directory. If you want to use\n\
6450an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006451the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006452
6453static PyObject *
6454win32_startfile(PyObject *self, PyObject *args)
6455{
6456 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006457 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006458 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006459#ifdef Py_WIN_WIDE_FILENAMES
6460 if (unicode_file_names()) {
6461 PyObject *unipath, *woperation = NULL;
6462 if (!PyArg_ParseTuple(args, "U|s:startfile",
6463 &unipath, &operation)) {
6464 PyErr_Clear();
6465 goto normal;
6466 }
6467
6468
6469 if (operation) {
6470 woperation = PyUnicode_DecodeASCII(operation,
6471 strlen(operation), NULL);
6472 if (!woperation) {
6473 PyErr_Clear();
6474 operation = NULL;
6475 goto normal;
6476 }
6477 }
6478
6479 Py_BEGIN_ALLOW_THREADS
6480 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6481 PyUnicode_AS_UNICODE(unipath),
6482 NULL, NULL, SW_SHOWNORMAL);
6483 Py_END_ALLOW_THREADS
6484
6485 Py_XDECREF(woperation);
6486 if (rc <= (HINSTANCE)32) {
6487 PyObject *errval = win32_error_unicode("startfile",
6488 PyUnicode_AS_UNICODE(unipath));
6489 return errval;
6490 }
6491 Py_INCREF(Py_None);
6492 return Py_None;
6493 }
6494#endif
6495
6496normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006497 if (!PyArg_ParseTuple(args, "et|s:startfile",
6498 Py_FileSystemDefaultEncoding, &filepath,
6499 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006500 return NULL;
6501 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006502 rc = ShellExecute((HWND)0, operation, filepath,
6503 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006504 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006505 if (rc <= (HINSTANCE)32) {
6506 PyObject *errval = win32_error("startfile", filepath);
6507 PyMem_Free(filepath);
6508 return errval;
6509 }
6510 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006511 Py_INCREF(Py_None);
6512 return Py_None;
6513}
6514#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006515
Martin v. Löwis438b5342002-12-27 10:16:42 +00006516#ifdef HAVE_GETLOADAVG
6517PyDoc_STRVAR(posix_getloadavg__doc__,
6518"getloadavg() -> (float, float, float)\n\n\
6519Return the number of processes in the system run queue averaged over\n\
6520the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6521was unobtainable");
6522
6523static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006524posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006525{
6526 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006527 if (getloadavg(loadavg, 3)!=3) {
6528 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6529 return NULL;
6530 } else
6531 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6532}
6533#endif
6534
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006535#ifdef MS_WINDOWS
6536
6537PyDoc_STRVAR(win32_urandom__doc__,
6538"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006539Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006540
6541typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6542 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6543 DWORD dwFlags );
6544typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6545 BYTE *pbBuffer );
6546
6547static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006548/* This handle is never explicitly released. Instead, the operating
6549 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006550static HCRYPTPROV hCryptProv = 0;
6551
Tim Peters4ad82172004-08-30 17:02:04 +00006552static PyObject*
6553win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006554{
Tim Petersd3115382004-08-30 17:36:46 +00006555 int howMany;
6556 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006557
Tim Peters4ad82172004-08-30 17:02:04 +00006558 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006559 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006560 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006561 if (howMany < 0)
6562 return PyErr_Format(PyExc_ValueError,
6563 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006564
Tim Peters4ad82172004-08-30 17:02:04 +00006565 if (hCryptProv == 0) {
6566 HINSTANCE hAdvAPI32 = NULL;
6567 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006568
Tim Peters4ad82172004-08-30 17:02:04 +00006569 /* Obtain handle to the DLL containing CryptoAPI
6570 This should not fail */
6571 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6572 if(hAdvAPI32 == NULL)
6573 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006574
Tim Peters4ad82172004-08-30 17:02:04 +00006575 /* Obtain pointers to the CryptoAPI functions
6576 This will fail on some early versions of Win95 */
6577 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6578 hAdvAPI32,
6579 "CryptAcquireContextA");
6580 if (pCryptAcquireContext == NULL)
6581 return PyErr_Format(PyExc_NotImplementedError,
6582 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006583
Tim Peters4ad82172004-08-30 17:02:04 +00006584 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6585 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006586 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006587 return PyErr_Format(PyExc_NotImplementedError,
6588 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006589
Tim Peters4ad82172004-08-30 17:02:04 +00006590 /* Acquire context */
6591 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6592 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6593 return win32_error("CryptAcquireContext", NULL);
6594 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006595
Tim Peters4ad82172004-08-30 17:02:04 +00006596 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006597 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006598 if (result != NULL) {
6599 /* Get random data */
6600 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006601 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006602 Py_DECREF(result);
6603 return win32_error("CryptGenRandom", NULL);
6604 }
Tim Peters4ad82172004-08-30 17:02:04 +00006605 }
Tim Petersd3115382004-08-30 17:36:46 +00006606 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006607}
6608#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006609
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006610PyDoc_STRVAR(device_encoding__doc__,
6611"device_encoding(fd) -> str\n\n\
6612Return a string describing the encoding of the device\n\
6613if the output is a terminal; else return None.");
6614
6615static PyObject *
6616device_encoding(PyObject *self, PyObject *args)
6617{
6618 int fd;
6619 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6620 return NULL;
6621 if (!isatty(fd)) {
6622 Py_INCREF(Py_None);
6623 return Py_None;
6624 }
6625#if defined(MS_WINDOWS) || defined(MS_WIN64)
6626 if (fd == 0) {
6627 char buf[100];
6628 sprintf(buf, "cp%d", GetConsoleCP());
6629 return PyUnicode_FromString(buf);
6630 }
6631 if (fd == 1 || fd == 2) {
6632 char buf[100];
6633 sprintf(buf, "cp%d", GetConsoleOutputCP());
6634 return PyUnicode_FromString(buf);
6635 }
6636#elif defined(CODESET)
6637 {
6638 char *codeset = nl_langinfo(CODESET);
6639 if (codeset)
6640 return PyUnicode_FromString(codeset);
6641 }
6642#endif
6643 Py_INCREF(Py_None);
6644 return Py_None;
6645}
6646
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006647#ifdef __VMS
6648/* Use openssl random routine */
6649#include <openssl/rand.h>
6650PyDoc_STRVAR(vms_urandom__doc__,
6651"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006652Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006653
6654static PyObject*
6655vms_urandom(PyObject *self, PyObject *args)
6656{
6657 int howMany;
6658 PyObject* result;
6659
6660 /* Read arguments */
6661 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6662 return NULL;
6663 if (howMany < 0)
6664 return PyErr_Format(PyExc_ValueError,
6665 "negative argument not allowed");
6666
6667 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006668 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006669 if (result != NULL) {
6670 /* Get random data */
6671 if (RAND_pseudo_bytes((unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006672 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006673 howMany) < 0) {
6674 Py_DECREF(result);
6675 return PyErr_Format(PyExc_ValueError,
6676 "RAND_pseudo_bytes");
6677 }
6678 }
6679 return result;
6680}
6681#endif
6682
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006683static PyMethodDef posix_methods[] = {
6684 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6685#ifdef HAVE_TTYNAME
6686 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6687#endif
6688 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006689#ifdef HAVE_CHFLAGS
6690 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6691#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006692 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006693#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006694 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006695#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006696#ifdef HAVE_LCHFLAGS
6697 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6698#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006699#ifdef HAVE_LCHOWN
6700 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6701#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006702#ifdef HAVE_CHROOT
6703 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6704#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006705#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006706 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006707#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006708#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006709 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006710 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006711#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006712#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006713 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006714#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006715 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6716 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6717 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006718#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006719 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006720#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006721#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006722 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006723#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006724 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6725 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6726 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006727 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006728#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006729 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006730#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006731#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006732 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006733#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006734 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006735#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006736 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006737#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006738 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6739 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6740 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006741#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006742 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006743#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006744 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006745#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006746 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6747 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006748#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006749#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006750 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6751 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006752#if defined(PYOS_OS2)
6753 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6754 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6755#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006756#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006757#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006758 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006759#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006760#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006761 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006762#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006763#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006764 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006765#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006766#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006767 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006768#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006769#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006770 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006771#endif /* HAVE_GETEGID */
6772#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006773 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006774#endif /* HAVE_GETEUID */
6775#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006776 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006777#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006778#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006779 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006780#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006781 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006782#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006783 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006784#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006785#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006786 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006787#endif /* HAVE_GETPPID */
6788#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006789 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006790#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006791#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006792 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006793#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006794#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006795 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006796#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006797#ifdef HAVE_KILLPG
6798 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6799#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006800#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006801 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006802#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006803#ifdef MS_WINDOWS
6804 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6805#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006806#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006807 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006808#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006809#ifdef HAVE_SETEUID
6810 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6811#endif /* HAVE_SETEUID */
6812#ifdef HAVE_SETEGID
6813 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6814#endif /* HAVE_SETEGID */
6815#ifdef HAVE_SETREUID
6816 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6817#endif /* HAVE_SETREUID */
6818#ifdef HAVE_SETREGID
6819 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6820#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006821#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006822 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006823#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006824#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006825 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006826#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006827#ifdef HAVE_GETPGID
6828 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6829#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006830#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006831 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006832#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006833#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006834 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006835#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006836#ifdef HAVE_WAIT3
6837 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6838#endif /* HAVE_WAIT3 */
6839#ifdef HAVE_WAIT4
6840 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6841#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006842#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006843 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006844#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006845#ifdef HAVE_GETSID
6846 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6847#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006848#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006849 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006850#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006851#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006852 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006853#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006854#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006855 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006856#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006857#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006858 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006859#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006860 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6861 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006862 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006863 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6864 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6865 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6866 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6867 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6868 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006869 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006870#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006871 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006872#endif
6873#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006874 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006875#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006876#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006877 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6878#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006879#ifdef HAVE_DEVICE_MACROS
6880 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6881 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6882 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6883#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006884#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006885 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006886#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006887#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006888 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006889#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006890#ifdef HAVE_UNSETENV
6891 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6892#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00006893#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006894 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00006895#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00006896#ifdef HAVE_FCHDIR
6897 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6898#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006899#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006900 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006901#endif
6902#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006903 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006904#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006905#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006906#ifdef WCOREDUMP
6907 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6908#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006909#ifdef WIFCONTINUED
6910 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6911#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006912#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006913 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006914#endif /* WIFSTOPPED */
6915#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006916 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006917#endif /* WIFSIGNALED */
6918#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006919 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006920#endif /* WIFEXITED */
6921#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006922 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006923#endif /* WEXITSTATUS */
6924#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006925 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006926#endif /* WTERMSIG */
6927#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006928 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006929#endif /* WSTOPSIG */
6930#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00006931#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006932 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006933#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00006934#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006935 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006936#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00006937#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00006938 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006939#endif
6940#ifdef HAVE_TEMPNAM
6941 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
6942#endif
6943#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00006944 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006945#endif
Fred Drakec9680921999-12-13 16:37:25 +00006946#ifdef HAVE_CONFSTR
6947 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
6948#endif
6949#ifdef HAVE_SYSCONF
6950 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
6951#endif
6952#ifdef HAVE_FPATHCONF
6953 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
6954#endif
6955#ifdef HAVE_PATHCONF
6956 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
6957#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006958 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006959#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00006960 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
6961#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006962#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00006963 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00006964#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006965 #ifdef MS_WINDOWS
6966 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
6967 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006968 #ifdef __VMS
6969 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
6970 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006971 {NULL, NULL} /* Sentinel */
6972};
6973
6974
Barry Warsaw4a342091996-12-19 23:50:02 +00006975static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006976ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00006977{
Fred Drake4d1e64b2002-04-15 19:40:07 +00006978 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00006979}
6980
Guido van Rossumd48f2521997-12-05 22:19:34 +00006981#if defined(PYOS_OS2)
6982/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006983static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006984{
6985 APIRET rc;
6986 ULONG values[QSV_MAX+1];
6987 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00006988 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00006989
6990 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00006991 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006992 Py_END_ALLOW_THREADS
6993
6994 if (rc != NO_ERROR) {
6995 os2_error(rc);
6996 return -1;
6997 }
6998
Fred Drake4d1e64b2002-04-15 19:40:07 +00006999 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7000 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7001 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7002 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7003 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7004 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7005 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007006
7007 switch (values[QSV_VERSION_MINOR]) {
7008 case 0: ver = "2.00"; break;
7009 case 10: ver = "2.10"; break;
7010 case 11: ver = "2.11"; break;
7011 case 30: ver = "3.00"; break;
7012 case 40: ver = "4.00"; break;
7013 case 50: ver = "5.00"; break;
7014 default:
Tim Peters885d4572001-11-28 20:27:42 +00007015 PyOS_snprintf(tmp, sizeof(tmp),
7016 "%d-%d", values[QSV_VERSION_MAJOR],
7017 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007018 ver = &tmp[0];
7019 }
7020
7021 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007022 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007023 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007024
7025 /* Add Indicator of Which Drive was Used to Boot the System */
7026 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7027 tmp[1] = ':';
7028 tmp[2] = '\0';
7029
Fred Drake4d1e64b2002-04-15 19:40:07 +00007030 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007031}
7032#endif
7033
Barry Warsaw4a342091996-12-19 23:50:02 +00007034static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007035all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007036{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007037#ifdef F_OK
7038 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007039#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007040#ifdef R_OK
7041 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007042#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007043#ifdef W_OK
7044 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007045#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007046#ifdef X_OK
7047 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007048#endif
Fred Drakec9680921999-12-13 16:37:25 +00007049#ifdef NGROUPS_MAX
7050 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7051#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007052#ifdef TMP_MAX
7053 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7054#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007055#ifdef WCONTINUED
7056 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7057#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007058#ifdef WNOHANG
7059 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007060#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007061#ifdef WUNTRACED
7062 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7063#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007064#ifdef O_RDONLY
7065 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7066#endif
7067#ifdef O_WRONLY
7068 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7069#endif
7070#ifdef O_RDWR
7071 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7072#endif
7073#ifdef O_NDELAY
7074 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7075#endif
7076#ifdef O_NONBLOCK
7077 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7078#endif
7079#ifdef O_APPEND
7080 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7081#endif
7082#ifdef O_DSYNC
7083 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7084#endif
7085#ifdef O_RSYNC
7086 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7087#endif
7088#ifdef O_SYNC
7089 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7090#endif
7091#ifdef O_NOCTTY
7092 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7093#endif
7094#ifdef O_CREAT
7095 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7096#endif
7097#ifdef O_EXCL
7098 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7099#endif
7100#ifdef O_TRUNC
7101 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7102#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007103#ifdef O_BINARY
7104 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7105#endif
7106#ifdef O_TEXT
7107 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7108#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007109#ifdef O_LARGEFILE
7110 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7111#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007112#ifdef O_SHLOCK
7113 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7114#endif
7115#ifdef O_EXLOCK
7116 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7117#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007118
Tim Peters5aa91602002-01-30 05:46:57 +00007119/* MS Windows */
7120#ifdef O_NOINHERIT
7121 /* Don't inherit in child processes. */
7122 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7123#endif
7124#ifdef _O_SHORT_LIVED
7125 /* Optimize for short life (keep in memory). */
7126 /* MS forgot to define this one with a non-underscore form too. */
7127 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7128#endif
7129#ifdef O_TEMPORARY
7130 /* Automatically delete when last handle is closed. */
7131 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7132#endif
7133#ifdef O_RANDOM
7134 /* Optimize for random access. */
7135 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7136#endif
7137#ifdef O_SEQUENTIAL
7138 /* Optimize for sequential access. */
7139 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7140#endif
7141
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007142/* GNU extensions. */
7143#ifdef O_DIRECT
7144 /* Direct disk access. */
7145 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7146#endif
7147#ifdef O_DIRECTORY
7148 /* Must be a directory. */
7149 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7150#endif
7151#ifdef O_NOFOLLOW
7152 /* Do not follow links. */
7153 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7154#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007155
Barry Warsaw5676bd12003-01-07 20:57:09 +00007156 /* These come from sysexits.h */
7157#ifdef EX_OK
7158 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007159#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007160#ifdef EX_USAGE
7161 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007162#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007163#ifdef EX_DATAERR
7164 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007165#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007166#ifdef EX_NOINPUT
7167 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007168#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007169#ifdef EX_NOUSER
7170 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007171#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007172#ifdef EX_NOHOST
7173 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007174#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007175#ifdef EX_UNAVAILABLE
7176 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007177#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007178#ifdef EX_SOFTWARE
7179 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007180#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007181#ifdef EX_OSERR
7182 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007183#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007184#ifdef EX_OSFILE
7185 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007186#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007187#ifdef EX_CANTCREAT
7188 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007189#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007190#ifdef EX_IOERR
7191 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007192#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007193#ifdef EX_TEMPFAIL
7194 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007195#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007196#ifdef EX_PROTOCOL
7197 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007198#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007199#ifdef EX_NOPERM
7200 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007201#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007202#ifdef EX_CONFIG
7203 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007204#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007205#ifdef EX_NOTFOUND
7206 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007207#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007208
Guido van Rossum246bc171999-02-01 23:54:31 +00007209#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007210#if defined(PYOS_OS2) && defined(PYCC_GCC)
7211 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7212 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7213 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7214 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7215 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7216 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7217 if (ins(d, "P_PM", (long)P_PM)) return -1;
7218 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7219 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7220 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7221 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7222 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7223 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7224 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7225 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7226 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7227 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7228 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7229 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7230 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7231#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007232 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7233 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7234 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7235 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7236 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007237#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007238#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007239
Guido van Rossumd48f2521997-12-05 22:19:34 +00007240#if defined(PYOS_OS2)
7241 if (insertvalues(d)) return -1;
7242#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007243 return 0;
7244}
7245
7246
Tim Peters5aa91602002-01-30 05:46:57 +00007247#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007248#define INITFUNC initnt
7249#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007250
7251#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007252#define INITFUNC initos2
7253#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007254
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007255#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007256#define INITFUNC initposix
7257#define MODNAME "posix"
7258#endif
7259
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007260PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007261INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007262{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007263 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007264
Fred Drake4d1e64b2002-04-15 19:40:07 +00007265 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007266 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007267 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007268 if (m == NULL)
7269 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007270
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007271 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007272 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007273 Py_XINCREF(v);
7274 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007275 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007276 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007277
Fred Drake4d1e64b2002-04-15 19:40:07 +00007278 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007279 return;
7280
Fred Drake4d1e64b2002-04-15 19:40:07 +00007281 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007282 return;
7283
Fred Drake4d1e64b2002-04-15 19:40:07 +00007284 Py_INCREF(PyExc_OSError);
7285 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007286
Guido van Rossumb3d39562000-01-31 18:41:26 +00007287#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007288 if (posix_putenv_garbage == NULL)
7289 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007290#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007291
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007292 if (!initialized) {
7293 stat_result_desc.name = MODNAME ".stat_result";
7294 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7295 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7296 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7297 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7298 structseq_new = StatResultType.tp_new;
7299 StatResultType.tp_new = statresult_new;
7300
7301 statvfs_result_desc.name = MODNAME ".statvfs_result";
7302 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7303 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007304 Py_INCREF((PyObject*) &StatResultType);
7305 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007306 Py_INCREF((PyObject*) &StatVFSResultType);
7307 PyModule_AddObject(m, "statvfs_result",
7308 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007309 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007310
7311#ifdef __APPLE__
7312 /*
7313 * Step 2 of weak-linking support on Mac OS X.
7314 *
7315 * The code below removes functions that are not available on the
7316 * currently active platform.
7317 *
7318 * This block allow one to use a python binary that was build on
7319 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7320 * OSX 10.4.
7321 */
7322#ifdef HAVE_FSTATVFS
7323 if (fstatvfs == NULL) {
7324 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7325 return;
7326 }
7327 }
7328#endif /* HAVE_FSTATVFS */
7329
7330#ifdef HAVE_STATVFS
7331 if (statvfs == NULL) {
7332 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7333 return;
7334 }
7335 }
7336#endif /* HAVE_STATVFS */
7337
7338# ifdef HAVE_LCHOWN
7339 if (lchown == NULL) {
7340 if (PyObject_DelAttrString(m, "lchown") == -1) {
7341 return;
7342 }
7343 }
7344#endif /* HAVE_LCHOWN */
7345
7346
7347#endif /* __APPLE__ */
7348
Guido van Rossumb6775db1994-08-01 11:34:53 +00007349}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007350
7351#ifdef __cplusplus
7352}
7353#endif