blob: 4e6e0c595db2a889f0eae5617f9816b70d59ebbe [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int chown(const char *, uid_t, gid_t);
190extern char *getcwd(char *, int);
191extern char *strerror(int);
192extern int link(const char *, const char *);
193extern int rename(const char *, const char *);
194extern int stat(const char *, struct stat *);
195extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000198#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000201#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000203
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_UTIME_H
207#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000210#ifdef HAVE_SYS_UTIME_H
211#include <sys/utime.h>
212#define HAVE_UTIME_H /* pretend we do for the rest of this file */
213#endif /* HAVE_SYS_UTIME_H */
214
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#ifdef HAVE_SYS_TIMES_H
216#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000217#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
219#ifdef HAVE_SYS_PARAM_H
220#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
223#ifdef HAVE_SYS_UTSNAME_H
224#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#define NAMLEN(dirent) strlen((dirent)->d_name)
230#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000231#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000232#include <direct.h>
233#define NAMLEN(dirent) strlen((dirent)->d_name)
234#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000237#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000238#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#endif
241#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#endif
244#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000252#endif
253#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255#endif
256#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000259#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000261#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
Guido van Rossumd48f2521997-12-05 22:19:34 +0000264#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000266#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267
Tim Petersbc2e10e2002-03-03 23:17:02 +0000268#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269#if defined(PATH_MAX) && PATH_MAX > 1024
270#define MAXPATHLEN PATH_MAX
271#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000272#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000273#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#endif /* MAXPATHLEN */
275
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000276#ifdef UNION_WAIT
277/* Emulate some macros on systems that have a union instead of macros */
278
279#ifndef WIFEXITED
280#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
281#endif
282
283#ifndef WEXITSTATUS
284#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
285#endif
286
287#ifndef WTERMSIG
288#define WTERMSIG(u_wait) ((u_wait).w_termsig)
289#endif
290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291#define WAIT_TYPE union wait
292#define WAIT_STATUS_INT(s) (s.w_status)
293
294#else /* !UNION_WAIT */
295#define WAIT_TYPE int
296#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000297#endif /* UNION_WAIT */
298
Greg Wardb48bc172000-03-01 21:51:56 +0000299/* Don't use the "_r" form if we don't need it (also, won't have a
300 prototype for it, at least on Solaris -- maybe others as well?). */
301#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
302#define USE_CTERMID_R
303#endif
304
305#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
306#define USE_TMPNAM_R
307#endif
308
Fred Drake699f3522000-06-29 21:12:41 +0000309/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000310#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000311#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000312# define STAT win32_stat
313# define FSTAT win32_fstat
314# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000315#else
316# define STAT stat
317# define FSTAT fstat
318# define STRUCT_STAT struct stat
319#endif
320
Tim Peters11b23062003-04-23 02:39:17 +0000321#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000322#include <sys/mkdev.h>
323#else
324#if defined(MAJOR_IN_SYSMACROS)
325#include <sys/sysmacros.h>
326#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000327#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
328#include <sys/mkdev.h>
329#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000330#endif
Fred Drake699f3522000-06-29 21:12:41 +0000331
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000333#ifdef WITH_NEXT_FRAMEWORK
334/* On Darwin/MacOSX a shared library or framework has no access to
335** environ directly, we must obtain it with _NSGetEnviron().
336*/
337#include <crt_externs.h>
338static char **environ;
339#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000341#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342
Barry Warsaw53699e91996-12-10 23:23:01 +0000343static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000344convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345{
Barry Warsaw53699e91996-12-10 23:23:01 +0000346 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 if (d == NULL)
350 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000351#ifdef WITH_NEXT_FRAMEWORK
352 if (environ == NULL)
353 environ = *_NSGetEnviron();
354#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 if (environ == NULL)
356 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000357 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000359 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 char *p = strchr(*e, '=');
362 if (p == NULL)
363 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000364 k = PyString_FromStringAndSize(*e, (int)(p-*e));
365 if (k == NULL) {
366 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000368 }
369 v = PyString_FromString(p+1);
370 if (v == NULL) {
371 PyErr_Clear();
372 Py_DECREF(k);
373 continue;
374 }
375 if (PyDict_GetItem(d, k) == NULL) {
376 if (PyDict_SetItem(d, k, v) != 0)
377 PyErr_Clear();
378 }
379 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000380 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000382#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000383 {
384 APIRET rc;
385 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
386
387 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000388 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000389 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000390 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000391 Py_DECREF(v);
392 }
393 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
394 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
395 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000396 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000397 Py_DECREF(v);
398 }
399 }
400#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 return d;
402}
403
404
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405/* Set a POSIX-specific error from errno, and return NULL */
406
Barry Warsawd58d7641998-07-23 16:14:40 +0000407static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000408posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000409{
Barry Warsawca74da41999-02-09 19:31:45 +0000410 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411}
Barry Warsawd58d7641998-07-23 16:14:40 +0000412static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000413posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000414{
Barry Warsawca74da41999-02-09 19:31:45 +0000415 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000416}
417
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000418#ifdef Py_WIN_WIDE_FILENAMES
419static PyObject *
420posix_error_with_unicode_filename(Py_UNICODE* name)
421{
422 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
423}
424#endif /* Py_WIN_WIDE_FILENAMES */
425
426
Mark Hammondef8b6542001-05-13 08:04:26 +0000427static PyObject *
428posix_error_with_allocated_filename(char* name)
429{
430 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
431 PyMem_Free(name);
432 return rc;
433}
434
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000435#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000436static PyObject *
437win32_error(char* function, char* filename)
438{
Mark Hammond33a6da92000-08-15 00:46:38 +0000439 /* XXX We should pass the function name along in the future.
440 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000441 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000442 Windows error object, which is non-trivial.
443 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000444 errno = GetLastError();
445 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000446 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000447 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000448 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000449}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450
451#ifdef Py_WIN_WIDE_FILENAMES
452static PyObject *
453win32_error_unicode(char* function, Py_UNICODE* filename)
454{
455 /* XXX - see win32_error for comments on 'function' */
456 errno = GetLastError();
457 if (filename)
458 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
459 else
460 return PyErr_SetFromWindowsErr(errno);
461}
462
463static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
464{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465}
466
467/* Function suitable for O& conversion */
468static int
469convert_to_unicode(PyObject *arg, void* _param)
470{
471 PyObject **param = (PyObject**)_param;
472 if (PyUnicode_CheckExact(arg)) {
473 Py_INCREF(arg);
474 *param = arg;
475 }
476 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000477 /* For a Unicode subtype that's not a Unicode object,
478 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
480 PyUnicode_GET_SIZE(arg));
481 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000482 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 else
484 *param = PyUnicode_FromEncodedObject(arg,
485 Py_FileSystemDefaultEncoding,
486 "strict");
487 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000488}
489
490#endif /* Py_WIN_WIDE_FILENAMES */
491
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000492#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493
Guido van Rossumd48f2521997-12-05 22:19:34 +0000494#if defined(PYOS_OS2)
495/**********************************************************************
496 * Helper Function to Trim and Format OS/2 Messages
497 **********************************************************************/
498 static void
499os2_formatmsg(char *msgbuf, int msglen, char *reason)
500{
501 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
502
503 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
504 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
505
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000506 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000507 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
508 }
509
510 /* Add Optional Reason Text */
511 if (reason) {
512 strcat(msgbuf, " : ");
513 strcat(msgbuf, reason);
514 }
515}
516
517/**********************************************************************
518 * Decode an OS/2 Operating System Error Code
519 *
520 * A convenience function to lookup an OS/2 error code and return a
521 * text message we can use to raise a Python exception.
522 *
523 * Notes:
524 * The messages for errors returned from the OS/2 kernel reside in
525 * the file OSO001.MSG in the \OS2 directory hierarchy.
526 *
527 **********************************************************************/
528 static char *
529os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
530{
531 APIRET rc;
532 ULONG msglen;
533
534 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
535 Py_BEGIN_ALLOW_THREADS
536 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
537 errorcode, "oso001.msg", &msglen);
538 Py_END_ALLOW_THREADS
539
540 if (rc == NO_ERROR)
541 os2_formatmsg(msgbuf, msglen, reason);
542 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000543 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000544 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000545
546 return msgbuf;
547}
548
549/* Set an OS/2-specific error and return NULL. OS/2 kernel
550 errors are not in a global variable e.g. 'errno' nor are
551 they congruent with posix error numbers. */
552
553static PyObject * os2_error(int code)
554{
555 char text[1024];
556 PyObject *v;
557
558 os2_strerror(text, sizeof(text), code, "");
559
560 v = Py_BuildValue("(is)", code, text);
561 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000562 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000563 Py_DECREF(v);
564 }
565 return NULL; /* Signal to Python that an Exception is Pending */
566}
567
568#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569
570/* POSIX generic methods */
571
Barry Warsaw53699e91996-12-10 23:23:01 +0000572static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000573posix_fildes(PyObject *fdobj, int (*func)(int))
574{
575 int fd;
576 int res;
577 fd = PyObject_AsFileDescriptor(fdobj);
578 if (fd < 0)
579 return NULL;
580 Py_BEGIN_ALLOW_THREADS
581 res = (*func)(fd);
582 Py_END_ALLOW_THREADS
583 if (res < 0)
584 return posix_error();
585 Py_INCREF(Py_None);
586 return Py_None;
587}
Guido van Rossum21142a01999-01-08 21:05:37 +0000588
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000589#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000590static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000591unicode_file_names(void)
592{
593 static int canusewide = -1;
594 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000595 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000596 the Windows NT family. */
597 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
598 }
599 return canusewide;
600}
601#endif
Tim Peters11b23062003-04-23 02:39:17 +0000602
Guido van Rossum21142a01999-01-08 21:05:37 +0000603static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605{
Mark Hammondef8b6542001-05-13 08:04:26 +0000606 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000608 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000611 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000612 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000614 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000615 return posix_error_with_allocated_filename(path1);
616 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000617 Py_INCREF(Py_None);
618 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Barry Warsaw53699e91996-12-10 23:23:01 +0000621static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000622posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625{
Mark Hammondef8b6542001-05-13 08:04:26 +0000626 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000627 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000628 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000629 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000630 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000632 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000633 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000634 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 PyMem_Free(path1);
636 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000637 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000638 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_INCREF(Py_None);
641 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644#ifdef Py_WIN_WIDE_FILENAMES
645static PyObject*
646win32_1str(PyObject* args, char* func,
647 char* format, BOOL (__stdcall *funcA)(LPCSTR),
648 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
649{
650 PyObject *uni;
651 char *ansi;
652 BOOL result;
653 if (unicode_file_names()) {
654 if (!PyArg_ParseTuple(args, wformat, &uni))
655 PyErr_Clear();
656 else {
657 Py_BEGIN_ALLOW_THREADS
658 result = funcW(PyUnicode_AsUnicode(uni));
659 Py_END_ALLOW_THREADS
660 if (!result)
661 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
662 Py_INCREF(Py_None);
663 return Py_None;
664 }
665 }
666 if (!PyArg_ParseTuple(args, format, &ansi))
667 return NULL;
668 Py_BEGIN_ALLOW_THREADS
669 result = funcA(ansi);
670 Py_END_ALLOW_THREADS
671 if (!result)
672 return win32_error(func, ansi);
673 Py_INCREF(Py_None);
674 return Py_None;
675
676}
677
678/* This is a reimplementation of the C library's chdir function,
679 but one that produces Win32 errors instead of DOS error codes.
680 chdir is essentially a wrapper around SetCurrentDirectory; however,
681 it also needs to set "magic" environment variables indicating
682 the per-drive current directory, which are of the form =<drive>: */
683BOOL __stdcall
684win32_chdir(LPCSTR path)
685{
686 char new_path[MAX_PATH+1];
687 int result;
688 char env[4] = "=x:";
689
690 if(!SetCurrentDirectoryA(path))
691 return FALSE;
692 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
693 if (!result)
694 return FALSE;
695 /* In the ANSI API, there should not be any paths longer
696 than MAX_PATH. */
697 assert(result <= MAX_PATH+1);
698 if (strncmp(new_path, "\\\\", 2) == 0 ||
699 strncmp(new_path, "//", 2) == 0)
700 /* UNC path, nothing to do. */
701 return TRUE;
702 env[1] = new_path[0];
703 return SetEnvironmentVariableA(env, new_path);
704}
705
706/* The Unicode version differs from the ANSI version
707 since the current directory might exceed MAX_PATH characters */
708BOOL __stdcall
709win32_wchdir(LPCWSTR path)
710{
711 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
712 int result;
713 wchar_t env[4] = L"=x:";
714
715 if(!SetCurrentDirectoryW(path))
716 return FALSE;
717 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
718 if (!result)
719 return FALSE;
720 if (result > MAX_PATH+1) {
721 new_path = malloc(result);
722 if (!new_path) {
723 SetLastError(ERROR_OUTOFMEMORY);
724 return FALSE;
725 }
726 }
727 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
728 wcsncmp(new_path, L"//", 2) == 0)
729 /* UNC path, nothing to do. */
730 return TRUE;
731 env[1] = new_path[0];
732 result = SetEnvironmentVariableW(env, new_path);
733 if (new_path != _new_path)
734 free(new_path);
735 return result;
736}
737#endif
738
Martin v. Löwis14694662006-02-03 12:54:16 +0000739#ifdef MS_WINDOWS
740/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
741 - time stamps are restricted to second resolution
742 - file modification times suffer from forth-and-back conversions between
743 UTC and local time
744 Therefore, we implement our own stat, based on the Win32 API directly.
745*/
746#define HAVE_STAT_NSEC 1
747
748struct win32_stat{
749 int st_dev;
750 __int64 st_ino;
751 unsigned short st_mode;
752 int st_nlink;
753 int st_uid;
754 int st_gid;
755 int st_rdev;
756 __int64 st_size;
757 int st_atime;
758 int st_atime_nsec;
759 int st_mtime;
760 int st_mtime_nsec;
761 int st_ctime;
762 int st_ctime_nsec;
763};
764
765static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
766
767static void
768FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
769{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000770 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
771 /* Cannot simply cast and dereference in_ptr,
772 since it might not be aligned properly */
773 __int64 in;
774 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000775 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
776 /* XXX Win32 supports time stamps past 2038; we currently don't */
777 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
778}
779
Thomas Wouters477c8d52006-05-27 19:21:47 +0000780static void
781time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
782{
783 /* XXX endianness */
784 __int64 out;
785 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000786 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 memcpy(out_ptr, &out, sizeof(out));
788}
789
Martin v. Löwis14694662006-02-03 12:54:16 +0000790/* Below, we *know* that ugo+r is 0444 */
791#if _S_IREAD != 0400
792#error Unsupported C library
793#endif
794static int
795attributes_to_mode(DWORD attr)
796{
797 int m = 0;
798 if (attr & FILE_ATTRIBUTE_DIRECTORY)
799 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
800 else
801 m |= _S_IFREG;
802 if (attr & FILE_ATTRIBUTE_READONLY)
803 m |= 0444;
804 else
805 m |= 0666;
806 return m;
807}
808
809static int
810attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
811{
812 memset(result, 0, sizeof(*result));
813 result->st_mode = attributes_to_mode(info->dwFileAttributes);
814 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
815 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
816 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
817 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
818
819 return 0;
820}
821
Thomas Wouters89f507f2006-12-13 04:49:30 +0000822/* Emulate GetFileAttributesEx[AW] on Windows 95 */
823static int checked = 0;
824static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
825static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
826static void
827check_gfax()
828{
829 HINSTANCE hKernel32;
830 if (checked)
831 return;
832 checked = 1;
833 hKernel32 = GetModuleHandle("KERNEL32");
834 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
835 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
836}
837
Guido van Rossumd8faa362007-04-27 19:54:29 +0000838static BOOL
839attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
840{
841 HANDLE hFindFile;
842 WIN32_FIND_DATAA FileData;
843 hFindFile = FindFirstFileA(pszFile, &FileData);
844 if (hFindFile == INVALID_HANDLE_VALUE)
845 return FALSE;
846 FindClose(hFindFile);
847 pfad->dwFileAttributes = FileData.dwFileAttributes;
848 pfad->ftCreationTime = FileData.ftCreationTime;
849 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
850 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
851 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
852 pfad->nFileSizeLow = FileData.nFileSizeLow;
853 return TRUE;
854}
855
856static BOOL
857attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
858{
859 HANDLE hFindFile;
860 WIN32_FIND_DATAW FileData;
861 hFindFile = FindFirstFileW(pszFile, &FileData);
862 if (hFindFile == INVALID_HANDLE_VALUE)
863 return FALSE;
864 FindClose(hFindFile);
865 pfad->dwFileAttributes = FileData.dwFileAttributes;
866 pfad->ftCreationTime = FileData.ftCreationTime;
867 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
868 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
869 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
870 pfad->nFileSizeLow = FileData.nFileSizeLow;
871 return TRUE;
872}
873
Thomas Wouters89f507f2006-12-13 04:49:30 +0000874static BOOL WINAPI
875Py_GetFileAttributesExA(LPCSTR pszFile,
876 GET_FILEEX_INFO_LEVELS level,
877 LPVOID pv)
878{
879 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000880 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
881 /* First try to use the system's implementation, if that is
882 available and either succeeds to gives an error other than
883 that it isn't implemented. */
884 check_gfax();
885 if (gfaxa) {
886 result = gfaxa(pszFile, level, pv);
887 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
888 return result;
889 }
890 /* It's either not present, or not implemented.
891 Emulate using FindFirstFile. */
892 if (level != GetFileExInfoStandard) {
893 SetLastError(ERROR_INVALID_PARAMETER);
894 return FALSE;
895 }
896 /* Use GetFileAttributes to validate that the file name
897 does not contain wildcards (which FindFirstFile would
898 accept). */
899 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
900 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000901 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000902}
903
904static BOOL WINAPI
905Py_GetFileAttributesExW(LPCWSTR pszFile,
906 GET_FILEEX_INFO_LEVELS level,
907 LPVOID pv)
908{
909 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
911 /* First try to use the system's implementation, if that is
912 available and either succeeds to gives an error other than
913 that it isn't implemented. */
914 check_gfax();
915 if (gfaxa) {
916 result = gfaxw(pszFile, level, pv);
917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
918 return result;
919 }
920 /* It's either not present, or not implemented.
921 Emulate using FindFirstFile. */
922 if (level != GetFileExInfoStandard) {
923 SetLastError(ERROR_INVALID_PARAMETER);
924 return FALSE;
925 }
926 /* Use GetFileAttributes to validate that the file name
927 does not contain wildcards (which FindFirstFile would
928 accept). */
929 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
930 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000931 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000932}
933
Martin v. Löwis14694662006-02-03 12:54:16 +0000934static int
935win32_stat(const char* path, struct win32_stat *result)
936{
937 WIN32_FILE_ATTRIBUTE_DATA info;
938 int code;
939 char *dot;
940 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000941 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942 if (GetLastError() != ERROR_SHARING_VIOLATION) {
943 /* Protocol violation: we explicitly clear errno, instead of
944 setting it to a POSIX error. Callers should use GetLastError. */
945 errno = 0;
946 return -1;
947 } else {
948 /* Could not get attributes on open file. Fall back to
949 reading the directory. */
950 if (!attributes_from_dir(path, &info)) {
951 /* Very strange. This should not fail now */
952 errno = 0;
953 return -1;
954 }
955 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000956 }
957 code = attribute_data_to_stat(&info, result);
958 if (code != 0)
959 return code;
960 /* Set S_IFEXEC if it is an .exe, .bat, ... */
961 dot = strrchr(path, '.');
962 if (dot) {
963 if (stricmp(dot, ".bat") == 0 ||
964 stricmp(dot, ".cmd") == 0 ||
965 stricmp(dot, ".exe") == 0 ||
966 stricmp(dot, ".com") == 0)
967 result->st_mode |= 0111;
968 }
969 return code;
970}
971
972static int
973win32_wstat(const wchar_t* path, struct win32_stat *result)
974{
975 int code;
976 const wchar_t *dot;
977 WIN32_FILE_ATTRIBUTE_DATA info;
978 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000979 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000980 if (GetLastError() != ERROR_SHARING_VIOLATION) {
981 /* Protocol violation: we explicitly clear errno, instead of
982 setting it to a POSIX error. Callers should use GetLastError. */
983 errno = 0;
984 return -1;
985 } else {
986 /* Could not get attributes on open file. Fall back to
987 reading the directory. */
988 if (!attributes_from_dir_w(path, &info)) {
989 /* Very strange. This should not fail now */
990 errno = 0;
991 return -1;
992 }
993 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 }
995 code = attribute_data_to_stat(&info, result);
996 if (code < 0)
997 return code;
998 /* Set IFEXEC if it is an .exe, .bat, ... */
999 dot = wcsrchr(path, '.');
1000 if (dot) {
1001 if (_wcsicmp(dot, L".bat") == 0 ||
1002 _wcsicmp(dot, L".cmd") == 0 ||
1003 _wcsicmp(dot, L".exe") == 0 ||
1004 _wcsicmp(dot, L".com") == 0)
1005 result->st_mode |= 0111;
1006 }
1007 return code;
1008}
1009
1010static int
1011win32_fstat(int file_number, struct win32_stat *result)
1012{
1013 BY_HANDLE_FILE_INFORMATION info;
1014 HANDLE h;
1015 int type;
1016
1017 h = (HANDLE)_get_osfhandle(file_number);
1018
1019 /* Protocol violation: we explicitly clear errno, instead of
1020 setting it to a POSIX error. Callers should use GetLastError. */
1021 errno = 0;
1022
1023 if (h == INVALID_HANDLE_VALUE) {
1024 /* This is really a C library error (invalid file handle).
1025 We set the Win32 error to the closes one matching. */
1026 SetLastError(ERROR_INVALID_HANDLE);
1027 return -1;
1028 }
1029 memset(result, 0, sizeof(*result));
1030
1031 type = GetFileType(h);
1032 if (type == FILE_TYPE_UNKNOWN) {
1033 DWORD error = GetLastError();
1034 if (error != 0) {
1035 return -1;
1036 }
1037 /* else: valid but unknown file */
1038 }
1039
1040 if (type != FILE_TYPE_DISK) {
1041 if (type == FILE_TYPE_CHAR)
1042 result->st_mode = _S_IFCHR;
1043 else if (type == FILE_TYPE_PIPE)
1044 result->st_mode = _S_IFIFO;
1045 return 0;
1046 }
1047
1048 if (!GetFileInformationByHandle(h, &info)) {
1049 return -1;
1050 }
1051
1052 /* similar to stat() */
1053 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1054 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1055 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1056 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1057 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1058 /* specific to fstat() */
1059 result->st_nlink = info.nNumberOfLinks;
1060 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1061 return 0;
1062}
1063
1064#endif /* MS_WINDOWS */
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001067"stat_result: Result from stat or lstat.\n\n\
1068This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001069 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1071\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001072Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1073or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001074\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076
1077static PyStructSequence_Field stat_result_fields[] = {
1078 {"st_mode", "protection bits"},
1079 {"st_ino", "inode"},
1080 {"st_dev", "device"},
1081 {"st_nlink", "number of hard links"},
1082 {"st_uid", "user ID of owner"},
1083 {"st_gid", "group ID of owner"},
1084 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001085 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1086 {NULL, "integer time of last access"},
1087 {NULL, "integer time of last modification"},
1088 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001089 {"st_atime", "time of last access"},
1090 {"st_mtime", "time of last modification"},
1091 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001092#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001093 {"st_blksize", "blocksize for filesystem I/O"},
1094#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001095#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001096 {"st_blocks", "number of blocks allocated"},
1097#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001098#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001099 {"st_rdev", "device type (if inode device)"},
1100#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001101#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1102 {"st_flags", "user defined flags for file"},
1103#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001104#ifdef HAVE_STRUCT_STAT_ST_GEN
1105 {"st_gen", "generation number"},
1106#endif
1107#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1108 {"st_birthtime", "time of creation"},
1109#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110 {0}
1111};
1112
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001113#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001114#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001116#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001117#endif
1118
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001119#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001120#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1121#else
1122#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1123#endif
1124
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001125#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1127#else
1128#define ST_RDEV_IDX ST_BLOCKS_IDX
1129#endif
1130
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001131#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1132#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1133#else
1134#define ST_FLAGS_IDX ST_RDEV_IDX
1135#endif
1136
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001137#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001138#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001139#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001140#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001141#endif
1142
1143#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1144#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1145#else
1146#define ST_BIRTHTIME_IDX ST_GEN_IDX
1147#endif
1148
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149static PyStructSequence_Desc stat_result_desc = {
1150 "stat_result", /* name */
1151 stat_result__doc__, /* doc */
1152 stat_result_fields,
1153 10
1154};
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1158This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001159 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001160or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001161\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001162See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163
1164static PyStructSequence_Field statvfs_result_fields[] = {
1165 {"f_bsize", },
1166 {"f_frsize", },
1167 {"f_blocks", },
1168 {"f_bfree", },
1169 {"f_bavail", },
1170 {"f_files", },
1171 {"f_ffree", },
1172 {"f_favail", },
1173 {"f_flag", },
1174 {"f_namemax",},
1175 {0}
1176};
1177
1178static PyStructSequence_Desc statvfs_result_desc = {
1179 "statvfs_result", /* name */
1180 statvfs_result__doc__, /* doc */
1181 statvfs_result_fields,
1182 10
1183};
1184
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001186static PyTypeObject StatResultType;
1187static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001188static newfunc structseq_new;
1189
1190static PyObject *
1191statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1192{
1193 PyStructSequence *result;
1194 int i;
1195
1196 result = (PyStructSequence*)structseq_new(type, args, kwds);
1197 if (!result)
1198 return NULL;
1199 /* If we have been initialized from a tuple,
1200 st_?time might be set to None. Initialize it
1201 from the int slots. */
1202 for (i = 7; i <= 9; i++) {
1203 if (result->ob_item[i+3] == Py_None) {
1204 Py_DECREF(Py_None);
1205 Py_INCREF(result->ob_item[i]);
1206 result->ob_item[i+3] = result->ob_item[i];
1207 }
1208 }
1209 return (PyObject*)result;
1210}
1211
1212
1213
1214/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001215static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001216
1217PyDoc_STRVAR(stat_float_times__doc__,
1218"stat_float_times([newval]) -> oldval\n\n\
1219Determine whether os.[lf]stat represents time stamps as float objects.\n\
1220If newval is True, future calls to stat() return floats, if it is False,\n\
1221future calls return ints. \n\
1222If newval is omitted, return the current setting.\n");
1223
1224static PyObject*
1225stat_float_times(PyObject* self, PyObject *args)
1226{
1227 int newval = -1;
1228 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1229 return NULL;
1230 if (newval == -1)
1231 /* Return old value */
1232 return PyBool_FromLong(_stat_float_times);
1233 _stat_float_times = newval;
1234 Py_INCREF(Py_None);
1235 return Py_None;
1236}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001237
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001238static void
1239fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1240{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001241 PyObject *fval,*ival;
1242#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001243 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001244#else
1245 ival = PyInt_FromLong((long)sec);
1246#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001247 if (!ival)
1248 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001249 if (_stat_float_times) {
1250 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1251 } else {
1252 fval = ival;
1253 Py_INCREF(fval);
1254 }
1255 PyStructSequence_SET_ITEM(v, index, ival);
1256 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001257}
1258
Tim Peters5aa91602002-01-30 05:46:57 +00001259/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001260 (used by posix_stat() and posix_fstat()) */
1261static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001262_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001263{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001264 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001265 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001266 if (v == NULL)
1267 return NULL;
1268
Martin v. Löwis14694662006-02-03 12:54:16 +00001269 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001270#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001271 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001272 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001273#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001274 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001275#endif
1276#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001277 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001278 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001279#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001280 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001281#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001282 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1283 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1284 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001285#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001286 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001287 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001288#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001289 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001290#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001291
Martin v. Löwis14694662006-02-03 12:54:16 +00001292#if defined(HAVE_STAT_TV_NSEC)
1293 ansec = st->st_atim.tv_nsec;
1294 mnsec = st->st_mtim.tv_nsec;
1295 cnsec = st->st_ctim.tv_nsec;
1296#elif defined(HAVE_STAT_TV_NSEC2)
1297 ansec = st->st_atimespec.tv_nsec;
1298 mnsec = st->st_mtimespec.tv_nsec;
1299 cnsec = st->st_ctimespec.tv_nsec;
1300#elif defined(HAVE_STAT_NSEC)
1301 ansec = st->st_atime_nsec;
1302 mnsec = st->st_mtime_nsec;
1303 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001304#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001305 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001306#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001307 fill_time(v, 7, st->st_atime, ansec);
1308 fill_time(v, 8, st->st_mtime, mnsec);
1309 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001311#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001312 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001313 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001315#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001316 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001318#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001319#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001320 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001322#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001323#ifdef HAVE_STRUCT_STAT_ST_GEN
1324 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001325 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001326#endif
1327#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1328 {
1329 PyObject *val;
1330 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001331 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001332#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001333 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#else
1335 bnsec = 0;
1336#endif
1337 if (_stat_float_times) {
1338 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1339 } else {
1340 val = PyInt_FromLong((long)bsec);
1341 }
1342 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1343 val);
1344 }
1345#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001346#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1347 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001348 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001349#endif
Fred Drake699f3522000-06-29 21:12:41 +00001350
1351 if (PyErr_Occurred()) {
1352 Py_DECREF(v);
1353 return NULL;
1354 }
1355
1356 return v;
1357}
1358
Martin v. Löwisd8948722004-06-02 09:57:56 +00001359#ifdef MS_WINDOWS
1360
1361/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1362 where / can be used in place of \ and the trailing slash is optional.
1363 Both SERVER and SHARE must have at least one character.
1364*/
1365
1366#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1367#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001368#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001369#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001370#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001371
Tim Peters4ad82172004-08-30 17:02:04 +00001372static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001373IsUNCRootA(char *path, int pathlen)
1374{
1375 #define ISSLASH ISSLASHA
1376
1377 int i, share;
1378
1379 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1380 /* minimum UNCRoot is \\x\y */
1381 return FALSE;
1382 for (i = 2; i < pathlen ; i++)
1383 if (ISSLASH(path[i])) break;
1384 if (i == 2 || i == pathlen)
1385 /* do not allow \\\SHARE or \\SERVER */
1386 return FALSE;
1387 share = i+1;
1388 for (i = share; i < pathlen; i++)
1389 if (ISSLASH(path[i])) break;
1390 return (i != share && (i == pathlen || i == pathlen-1));
1391
1392 #undef ISSLASH
1393}
1394
1395#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001396static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001397IsUNCRootW(Py_UNICODE *path, int pathlen)
1398{
1399 #define ISSLASH ISSLASHW
1400
1401 int i, share;
1402
1403 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1404 /* minimum UNCRoot is \\x\y */
1405 return FALSE;
1406 for (i = 2; i < pathlen ; i++)
1407 if (ISSLASH(path[i])) break;
1408 if (i == 2 || i == pathlen)
1409 /* do not allow \\\SHARE or \\SERVER */
1410 return FALSE;
1411 share = i+1;
1412 for (i = share; i < pathlen; i++)
1413 if (ISSLASH(path[i])) break;
1414 return (i != share && (i == pathlen || i == pathlen-1));
1415
1416 #undef ISSLASH
1417}
1418#endif /* Py_WIN_WIDE_FILENAMES */
1419#endif /* MS_WINDOWS */
1420
Barry Warsaw53699e91996-12-10 23:23:01 +00001421static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001422posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001423 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001424#ifdef __VMS
1425 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1426#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001427 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001428#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001429 char *wformat,
1430 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431{
Fred Drake699f3522000-06-29 21:12:41 +00001432 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001433 char *path = NULL; /* pass this to stat; do not free() it */
1434 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001435 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001436 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001437
1438#ifdef Py_WIN_WIDE_FILENAMES
1439 /* If on wide-character-capable OS see if argument
1440 is Unicode and if so use wide API. */
1441 if (unicode_file_names()) {
1442 PyUnicodeObject *po;
1443 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001444 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1445
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001446 Py_BEGIN_ALLOW_THREADS
1447 /* PyUnicode_AS_UNICODE result OK without
1448 thread lock as it is a simple dereference. */
1449 res = wstatfunc(wpath, &st);
1450 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001451
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001452 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001453 return win32_error_unicode("stat", wpath);
1454 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001455 }
1456 /* Drop the argument parsing error as narrow strings
1457 are also valid. */
1458 PyErr_Clear();
1459 }
1460#endif
1461
Tim Peters5aa91602002-01-30 05:46:57 +00001462 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001463 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001464 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001465 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001466
Barry Warsaw53699e91996-12-10 23:23:01 +00001467 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001468 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001469 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001470
1471 if (res != 0) {
1472#ifdef MS_WINDOWS
1473 result = win32_error("stat", pathfree);
1474#else
1475 result = posix_error_with_filename(pathfree);
1476#endif
1477 }
1478 else
1479 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001480
Tim Peters500bd032001-12-19 19:05:01 +00001481 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001482 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001483}
1484
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001485/* POSIX methods */
1486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001488"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001489Use the real uid/gid to test for access to a path. Note that most\n\
1490operations will use the effective uid/gid, therefore this routine can\n\
1491be used in a suid/sgid environment to test if the invoking user has the\n\
1492specified access to the path. The mode argument can be F_OK to test\n\
1493existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001494
1495static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001496posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001497{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001498 char *path;
1499 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001500
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001501#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001503 if (unicode_file_names()) {
1504 PyUnicodeObject *po;
1505 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1506 Py_BEGIN_ALLOW_THREADS
1507 /* PyUnicode_AS_UNICODE OK without thread lock as
1508 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001509 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001510 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001511 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001512 }
1513 /* Drop the argument parsing error as narrow strings
1514 are also valid. */
1515 PyErr_Clear();
1516 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001517 if (!PyArg_ParseTuple(args, "eti:access",
1518 Py_FileSystemDefaultEncoding, &path, &mode))
1519 return 0;
1520 Py_BEGIN_ALLOW_THREADS
1521 attr = GetFileAttributesA(path);
1522 Py_END_ALLOW_THREADS
1523 PyMem_Free(path);
1524finish:
1525 if (attr == 0xFFFFFFFF)
1526 /* File does not exist, or cannot read attributes */
1527 return PyBool_FromLong(0);
1528 /* Access is possible if either write access wasn't requested, or
1529 the file isn't read-only. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001530 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001531#else
1532 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001533 if (!PyArg_ParseTuple(args, "eti:access",
1534 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001535 return NULL;
1536 Py_BEGIN_ALLOW_THREADS
1537 res = access(path, mode);
1538 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001539 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001540 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001542}
1543
Guido van Rossumd371ff11999-01-25 16:12:23 +00001544#ifndef F_OK
1545#define F_OK 0
1546#endif
1547#ifndef R_OK
1548#define R_OK 4
1549#endif
1550#ifndef W_OK
1551#define W_OK 2
1552#endif
1553#ifndef X_OK
1554#define X_OK 1
1555#endif
1556
1557#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001558PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001559"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001561
1562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001563posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001564{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001565 int id;
1566 char *ret;
1567
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001568 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001569 return NULL;
1570
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001571#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001572 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001573 if (id == 0) {
1574 ret = ttyname();
1575 }
1576 else {
1577 ret = NULL;
1578 }
1579#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001580 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001581#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001582 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001583 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001584 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001585}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001587
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001588#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001590"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001592
1593static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001594posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001595{
1596 char *ret;
1597 char buffer[L_ctermid];
1598
Greg Wardb48bc172000-03-01 21:51:56 +00001599#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600 ret = ctermid_r(buffer);
1601#else
1602 ret = ctermid(buffer);
1603#endif
1604 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001605 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001606 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607}
1608#endif
1609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001610PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001611"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001613
Barry Warsaw53699e91996-12-10 23:23:01 +00001614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001615posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001616{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001617#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001619#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001621#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001625#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626}
1627
Fred Drake4d1e64b2002-04-15 19:40:07 +00001628#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001630"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001631Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001632opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001633
1634static PyObject *
1635posix_fchdir(PyObject *self, PyObject *fdobj)
1636{
1637 return posix_fildes(fdobj, fchdir);
1638}
1639#endif /* HAVE_FCHDIR */
1640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001642PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001643"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001644Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001645
Barry Warsaw53699e91996-12-10 23:23:01 +00001646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001647posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648{
Mark Hammondef8b6542001-05-13 08:04:26 +00001649 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001650 int i;
1651 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001652#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001654 if (unicode_file_names()) {
1655 PyUnicodeObject *po;
1656 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1657 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001658 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1659 if (attr != 0xFFFFFFFF) {
1660 if (i & _S_IWRITE)
1661 attr &= ~FILE_ATTRIBUTE_READONLY;
1662 else
1663 attr |= FILE_ATTRIBUTE_READONLY;
1664 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1665 }
1666 else
1667 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001668 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001669 if (!res)
1670 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001671 PyUnicode_AS_UNICODE(po));
1672 Py_INCREF(Py_None);
1673 return Py_None;
1674 }
1675 /* Drop the argument parsing error as narrow strings
1676 are also valid. */
1677 PyErr_Clear();
1678 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001679 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1680 &path, &i))
1681 return NULL;
1682 Py_BEGIN_ALLOW_THREADS
1683 attr = GetFileAttributesA(path);
1684 if (attr != 0xFFFFFFFF) {
1685 if (i & _S_IWRITE)
1686 attr &= ~FILE_ATTRIBUTE_READONLY;
1687 else
1688 attr |= FILE_ATTRIBUTE_READONLY;
1689 res = SetFileAttributesA(path, attr);
1690 }
1691 else
1692 res = 0;
1693 Py_END_ALLOW_THREADS
1694 if (!res) {
1695 win32_error("chmod", path);
1696 PyMem_Free(path);
1697 return NULL;
1698 }
1699 PyMem_Free(path);
1700 Py_INCREF(Py_None);
1701 return Py_None;
1702#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001703 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001704 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001705 return NULL;
1706 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001707 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001708 Py_END_ALLOW_THREADS
1709 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001710 return posix_error_with_allocated_filename(path);
1711 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001712 Py_INCREF(Py_None);
1713 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001714#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001715}
1716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001717
Thomas Wouterscf297e42007-02-23 15:07:44 +00001718#ifdef HAVE_CHFLAGS
1719PyDoc_STRVAR(posix_chflags__doc__,
1720"chflags(path, flags)\n\n\
1721Set file flags.");
1722
1723static PyObject *
1724posix_chflags(PyObject *self, PyObject *args)
1725{
1726 char *path;
1727 unsigned long flags;
1728 int res;
1729 if (!PyArg_ParseTuple(args, "etk:chflags",
1730 Py_FileSystemDefaultEncoding, &path, &flags))
1731 return NULL;
1732 Py_BEGIN_ALLOW_THREADS
1733 res = chflags(path, flags);
1734 Py_END_ALLOW_THREADS
1735 if (res < 0)
1736 return posix_error_with_allocated_filename(path);
1737 PyMem_Free(path);
1738 Py_INCREF(Py_None);
1739 return Py_None;
1740}
1741#endif /* HAVE_CHFLAGS */
1742
1743#ifdef HAVE_LCHFLAGS
1744PyDoc_STRVAR(posix_lchflags__doc__,
1745"lchflags(path, flags)\n\n\
1746Set file flags.\n\
1747This function will not follow symbolic links.");
1748
1749static PyObject *
1750posix_lchflags(PyObject *self, PyObject *args)
1751{
1752 char *path;
1753 unsigned long flags;
1754 int res;
1755 if (!PyArg_ParseTuple(args, "etk:lchflags",
1756 Py_FileSystemDefaultEncoding, &path, &flags))
1757 return NULL;
1758 Py_BEGIN_ALLOW_THREADS
1759 res = lchflags(path, flags);
1760 Py_END_ALLOW_THREADS
1761 if (res < 0)
1762 return posix_error_with_allocated_filename(path);
1763 PyMem_Free(path);
1764 Py_INCREF(Py_None);
1765 return Py_None;
1766}
1767#endif /* HAVE_LCHFLAGS */
1768
Martin v. Löwis244edc82001-10-04 22:44:26 +00001769#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001771"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001773
1774static PyObject *
1775posix_chroot(PyObject *self, PyObject *args)
1776{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001777 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001778}
1779#endif
1780
Guido van Rossum21142a01999-01-08 21:05:37 +00001781#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001783"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001784force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001785
1786static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001787posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001788{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001789 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001790}
1791#endif /* HAVE_FSYNC */
1792
1793#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001794
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001795#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001796extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1797#endif
1798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001801force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001803
1804static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001805posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001806{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001807 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001808}
1809#endif /* HAVE_FDATASYNC */
1810
1811
Fredrik Lundh10723342000-07-10 16:38:09 +00001812#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001813PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001814"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001816
Barry Warsaw53699e91996-12-10 23:23:01 +00001817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001818posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001819{
Mark Hammondef8b6542001-05-13 08:04:26 +00001820 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001821 int uid, gid;
1822 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001823 if (!PyArg_ParseTuple(args, "etii:chown",
1824 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001825 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001826 return NULL;
1827 Py_BEGIN_ALLOW_THREADS
1828 res = chown(path, (uid_t) uid, (gid_t) gid);
1829 Py_END_ALLOW_THREADS
1830 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001831 return posix_error_with_allocated_filename(path);
1832 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001833 Py_INCREF(Py_None);
1834 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001835}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001836#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001837
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001838#ifdef HAVE_LCHOWN
1839PyDoc_STRVAR(posix_lchown__doc__,
1840"lchown(path, uid, gid)\n\n\
1841Change the owner and group id of path to the numeric uid and gid.\n\
1842This function will not follow symbolic links.");
1843
1844static PyObject *
1845posix_lchown(PyObject *self, PyObject *args)
1846{
1847 char *path = NULL;
1848 int uid, gid;
1849 int res;
1850 if (!PyArg_ParseTuple(args, "etii:lchown",
1851 Py_FileSystemDefaultEncoding, &path,
1852 &uid, &gid))
1853 return NULL;
1854 Py_BEGIN_ALLOW_THREADS
1855 res = lchown(path, (uid_t) uid, (gid_t) gid);
1856 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001857 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001858 return posix_error_with_allocated_filename(path);
1859 PyMem_Free(path);
1860 Py_INCREF(Py_None);
1861 return Py_None;
1862}
1863#endif /* HAVE_LCHOWN */
1864
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001865
Guido van Rossum36bc6801995-06-14 22:54:23 +00001866#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001868"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Barry Warsaw53699e91996-12-10 23:23:01 +00001871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001872posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001873{
1874 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001875 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001878#if defined(PYOS_OS2) && defined(PYCC_GCC)
1879 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001880#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001881 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001882#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001883 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001884 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001886 return PyUnicode_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001887}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001888
1889PyDoc_STRVAR(posix_getcwdu__doc__,
1890"getcwdu() -> path\n\n\
1891Return a unicode string representing the current working directory.");
1892
1893static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001894posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001895{
1896 char buf[1026];
1897 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001898
1899#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001900 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001901 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001902 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903 wchar_t *wbuf2 = wbuf;
1904 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001905 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1907 /* If the buffer is large enough, len does not include the
1908 terminating \0. If the buffer is too small, len includes
1909 the space needed for the terminator. */
1910 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1911 wbuf2 = malloc(len * sizeof(wchar_t));
1912 if (wbuf2)
1913 len = GetCurrentDirectoryW(len, wbuf2);
1914 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001915 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916 if (!wbuf2) {
1917 PyErr_NoMemory();
1918 return NULL;
1919 }
1920 if (!len) {
1921 if (wbuf2 != wbuf) free(wbuf2);
1922 return win32_error("getcwdu", NULL);
1923 }
1924 resobj = PyUnicode_FromWideChar(wbuf2, len);
1925 if (wbuf2 != wbuf) free(wbuf2);
1926 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001927 }
1928#endif
1929
1930 Py_BEGIN_ALLOW_THREADS
1931#if defined(PYOS_OS2) && defined(PYCC_GCC)
1932 res = _getcwd2(buf, sizeof buf);
1933#else
1934 res = getcwd(buf, sizeof buf);
1935#endif
1936 Py_END_ALLOW_THREADS
1937 if (res == NULL)
1938 return posix_error();
1939 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1940}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001941#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001943
Guido van Rossumb6775db1994-08-01 11:34:53 +00001944#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001946"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001948
Barry Warsaw53699e91996-12-10 23:23:01 +00001949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001950posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001951{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001952 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001953}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001954#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001958"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959Return a list containing the names of the entries in the directory.\n\
1960\n\
1961 path: path of directory to list\n\
1962\n\
1963The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001964entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001965
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001967posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001968{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001969 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001970 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001971#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001972
Barry Warsaw53699e91996-12-10 23:23:01 +00001973 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001974 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001975 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001978 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001979 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001980
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#ifdef Py_WIN_WIDE_FILENAMES
1982 /* If on wide-character-capable OS see if argument
1983 is Unicode and if so use wide API. */
1984 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001985 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001986 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1987 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001988 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990 /* Overallocate for \\*.*\0 */
1991 len = PyUnicode_GET_SIZE(po);
1992 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1993 if (!wnamebuf) {
1994 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001996 }
1997 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1998 wch = len > 0 ? wnamebuf[len-1] : '\0';
1999 if (wch != L'/' && wch != L'\\' && wch != L':')
2000 wnamebuf[len++] = L'\\';
2001 wcscpy(wnamebuf + len, L"*.*");
2002 if ((d = PyList_New(0)) == NULL) {
2003 free(wnamebuf);
2004 return NULL;
2005 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2007 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002008 int error = GetLastError();
2009 if (error == ERROR_FILE_NOT_FOUND) {
2010 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011 return d;
2012 }
2013 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002014 win32_error_unicode("FindFirstFileW", wnamebuf);
2015 free(wnamebuf);
2016 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017 }
2018 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002019 /* Skip over . and .. */
2020 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2021 wcscmp(wFileData.cFileName, L"..") != 0) {
2022 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2023 if (v == NULL) {
2024 Py_DECREF(d);
2025 d = NULL;
2026 break;
2027 }
2028 if (PyList_Append(d, v) != 0) {
2029 Py_DECREF(v);
2030 Py_DECREF(d);
2031 d = NULL;
2032 break;
2033 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002035 }
Georg Brandl622927b2006-03-07 12:48:03 +00002036 Py_BEGIN_ALLOW_THREADS
2037 result = FindNextFileW(hFindFile, &wFileData);
2038 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002039 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2040 it got to the end of the directory. */
2041 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2042 Py_DECREF(d);
2043 win32_error_unicode("FindNextFileW", wnamebuf);
2044 FindClose(hFindFile);
2045 free(wnamebuf);
2046 return NULL;
2047 }
Georg Brandl622927b2006-03-07 12:48:03 +00002048 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002049
2050 if (FindClose(hFindFile) == FALSE) {
2051 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002052 win32_error_unicode("FindClose", wnamebuf);
2053 free(wnamebuf);
2054 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002055 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002056 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002057 return d;
2058 }
2059 /* Drop the argument parsing error as narrow strings
2060 are also valid. */
2061 PyErr_Clear();
2062 }
2063#endif
2064
Tim Peters5aa91602002-01-30 05:46:57 +00002065 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002066 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002067 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002068 if (len > 0) {
2069 char ch = namebuf[len-1];
2070 if (ch != SEP && ch != ALTSEP && ch != ':')
2071 namebuf[len++] = '/';
2072 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002073 strcpy(namebuf + len, "*.*");
2074
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002076 return NULL;
2077
2078 hFindFile = FindFirstFile(namebuf, &FileData);
2079 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080 int error = GetLastError();
2081 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002082 return d;
2083 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002084 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085 }
2086 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002087 /* Skip over . and .. */
2088 if (strcmp(FileData.cFileName, ".") != 0 &&
2089 strcmp(FileData.cFileName, "..") != 0) {
2090 v = PyString_FromString(FileData.cFileName);
2091 if (v == NULL) {
2092 Py_DECREF(d);
2093 d = NULL;
2094 break;
2095 }
2096 if (PyList_Append(d, v) != 0) {
2097 Py_DECREF(v);
2098 Py_DECREF(d);
2099 d = NULL;
2100 break;
2101 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103 }
Georg Brandl622927b2006-03-07 12:48:03 +00002104 Py_BEGIN_ALLOW_THREADS
2105 result = FindNextFile(hFindFile, &FileData);
2106 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002107 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2108 it got to the end of the directory. */
2109 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2110 Py_DECREF(d);
2111 win32_error("FindNextFile", namebuf);
2112 FindClose(hFindFile);
2113 return NULL;
2114 }
Georg Brandl622927b2006-03-07 12:48:03 +00002115 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002116
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002117 if (FindClose(hFindFile) == FALSE) {
2118 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002119 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002120 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002121
2122 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002123
Tim Peters0bb44a42000-09-15 07:44:49 +00002124#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002125
2126#ifndef MAX_PATH
2127#define MAX_PATH CCHMAXPATH
2128#endif
2129 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002130 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002131 PyObject *d, *v;
2132 char namebuf[MAX_PATH+5];
2133 HDIR hdir = 1;
2134 ULONG srchcnt = 1;
2135 FILEFINDBUF3 ep;
2136 APIRET rc;
2137
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002138 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002139 return NULL;
2140 if (len >= MAX_PATH) {
2141 PyErr_SetString(PyExc_ValueError, "path too long");
2142 return NULL;
2143 }
2144 strcpy(namebuf, name);
2145 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002146 if (*pt == ALTSEP)
2147 *pt = SEP;
2148 if (namebuf[len-1] != SEP)
2149 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002150 strcpy(namebuf + len, "*.*");
2151
2152 if ((d = PyList_New(0)) == NULL)
2153 return NULL;
2154
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002155 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2156 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002157 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002158 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2159 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2160 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002161
2162 if (rc != NO_ERROR) {
2163 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002164 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002165 }
2166
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002167 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002168 do {
2169 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002170 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002171 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002172
2173 strcpy(namebuf, ep.achName);
2174
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002175 /* Leave Case of Name Alone -- In Native Form */
2176 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002177
2178 v = PyString_FromString(namebuf);
2179 if (v == NULL) {
2180 Py_DECREF(d);
2181 d = NULL;
2182 break;
2183 }
2184 if (PyList_Append(d, v) != 0) {
2185 Py_DECREF(v);
2186 Py_DECREF(d);
2187 d = NULL;
2188 break;
2189 }
2190 Py_DECREF(v);
2191 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2192 }
2193
2194 return d;
2195#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002196
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002197 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002198 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002199 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002200 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002201 int arg_is_unicode = 1;
2202
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002203 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002204 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2205 arg_is_unicode = 0;
2206 PyErr_Clear();
2207 }
2208 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002209 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002210 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002211 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002212 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002213 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002214 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002215 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002216 return NULL;
2217 }
Georg Brandl622927b2006-03-07 12:48:03 +00002218 for (;;) {
2219 Py_BEGIN_ALLOW_THREADS
2220 ep = readdir(dirp);
2221 Py_END_ALLOW_THREADS
2222 if (ep == NULL)
2223 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002224 if (ep->d_name[0] == '.' &&
2225 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002226 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002227 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002229 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002230 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002231 d = NULL;
2232 break;
2233 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002234 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002235 PyObject *w;
2236
2237 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002238 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002239 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002240 if (w != NULL) {
2241 Py_DECREF(v);
2242 v = w;
2243 }
2244 else {
2245 /* fall back to the original byte string, as
2246 discussed in patch #683592 */
2247 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002248 }
Just van Rossum46c97842003-02-25 21:42:15 +00002249 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002250 if (PyList_Append(d, v) != 0) {
2251 Py_DECREF(v);
2252 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002253 d = NULL;
2254 break;
2255 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002256 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002257 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002258 if (errno != 0 && d != NULL) {
2259 /* readdir() returned NULL and set errno */
2260 closedir(dirp);
2261 Py_DECREF(d);
2262 return posix_error_with_allocated_filename(name);
2263 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002264 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002265 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002266
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002267 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002268
Tim Peters0bb44a42000-09-15 07:44:49 +00002269#endif /* which OS */
2270} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002271
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002272#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002273/* A helper function for abspath on win32 */
2274static PyObject *
2275posix__getfullpathname(PyObject *self, PyObject *args)
2276{
2277 /* assume encoded strings wont more than double no of chars */
2278 char inbuf[MAX_PATH*2];
2279 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002280 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002281 char outbuf[MAX_PATH*2];
2282 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002283#ifdef Py_WIN_WIDE_FILENAMES
2284 if (unicode_file_names()) {
2285 PyUnicodeObject *po;
2286 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2287 Py_UNICODE woutbuf[MAX_PATH*2];
2288 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002289 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002290 sizeof(woutbuf)/sizeof(woutbuf[0]),
2291 woutbuf, &wtemp))
2292 return win32_error("GetFullPathName", "");
2293 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2294 }
2295 /* Drop the argument parsing error as narrow strings
2296 are also valid. */
2297 PyErr_Clear();
2298 }
2299#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002300 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2301 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002302 &insize))
2303 return NULL;
2304 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2305 outbuf, &temp))
2306 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002307 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2308 return PyUnicode_Decode(outbuf, strlen(outbuf),
2309 Py_FileSystemDefaultEncoding, NULL);
2310 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002311 return PyString_FromString(outbuf);
2312} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002313#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002314
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002315PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002316"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002317Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002318
Barry Warsaw53699e91996-12-10 23:23:01 +00002319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002320posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002321{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002322 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002323 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002324 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002325
2326#ifdef Py_WIN_WIDE_FILENAMES
2327 if (unicode_file_names()) {
2328 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002329 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330 Py_BEGIN_ALLOW_THREADS
2331 /* PyUnicode_AS_UNICODE OK without thread lock as
2332 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002333 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002334 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002335 if (!res)
2336 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002337 Py_INCREF(Py_None);
2338 return Py_None;
2339 }
2340 /* Drop the argument parsing error as narrow strings
2341 are also valid. */
2342 PyErr_Clear();
2343 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002344 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2345 Py_FileSystemDefaultEncoding, &path, &mode))
2346 return NULL;
2347 Py_BEGIN_ALLOW_THREADS
2348 /* PyUnicode_AS_UNICODE OK without thread lock as
2349 it is a simple dereference. */
2350 res = CreateDirectoryA(path, NULL);
2351 Py_END_ALLOW_THREADS
2352 if (!res) {
2353 win32_error("mkdir", path);
2354 PyMem_Free(path);
2355 return NULL;
2356 }
2357 PyMem_Free(path);
2358 Py_INCREF(Py_None);
2359 return Py_None;
2360#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002361
Tim Peters5aa91602002-01-30 05:46:57 +00002362 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002363 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002364 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002365 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002366#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002367 res = mkdir(path);
2368#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002369 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002370#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002371 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002372 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002373 return posix_error_with_allocated_filename(path);
2374 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002375 Py_INCREF(Py_None);
2376 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002377#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378}
2379
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002380
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002381/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2382#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002383#include <sys/resource.h>
2384#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002385
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386
2387#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002388PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002389"nice(inc) -> new_priority\n\n\
2390Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002391
Barry Warsaw53699e91996-12-10 23:23:01 +00002392static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002393posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002394{
2395 int increment, value;
2396
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002397 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002398 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002399
2400 /* There are two flavours of 'nice': one that returns the new
2401 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002402 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2403 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002404
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002405 If we are of the nice family that returns the new priority, we
2406 need to clear errno before the call, and check if errno is filled
2407 before calling posix_error() on a returnvalue of -1, because the
2408 -1 may be the actual new priority! */
2409
2410 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002411 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002412#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002413 if (value == 0)
2414 value = getpriority(PRIO_PROCESS, 0);
2415#endif
2416 if (value == -1 && errno != 0)
2417 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002418 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002419 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002420}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002421#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002423PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002424"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002425Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002426
Barry Warsaw53699e91996-12-10 23:23:01 +00002427static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002428posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002429{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002430#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002431 PyObject *o1, *o2;
2432 char *p1, *p2;
2433 BOOL result;
2434 if (unicode_file_names()) {
2435 if (!PyArg_ParseTuple(args, "O&O&:rename",
2436 convert_to_unicode, &o1,
2437 convert_to_unicode, &o2))
2438 PyErr_Clear();
2439 else {
2440 Py_BEGIN_ALLOW_THREADS
2441 result = MoveFileW(PyUnicode_AsUnicode(o1),
2442 PyUnicode_AsUnicode(o2));
2443 Py_END_ALLOW_THREADS
2444 Py_DECREF(o1);
2445 Py_DECREF(o2);
2446 if (!result)
2447 return win32_error("rename", NULL);
2448 Py_INCREF(Py_None);
2449 return Py_None;
2450 }
2451 }
2452 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2453 return NULL;
2454 Py_BEGIN_ALLOW_THREADS
2455 result = MoveFileA(p1, p2);
2456 Py_END_ALLOW_THREADS
2457 if (!result)
2458 return win32_error("rename", NULL);
2459 Py_INCREF(Py_None);
2460 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002461#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002462 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002463#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002464}
2465
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002467PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002468"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002469Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002470
Barry Warsaw53699e91996-12-10 23:23:01 +00002471static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002472posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002473{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002474#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002475 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002476#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002477 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002478#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002479}
2480
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002482PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002483"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002484Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002485
Barry Warsaw53699e91996-12-10 23:23:01 +00002486static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002487posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002488{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002489#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002490 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002491#else
2492 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2493#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002494}
2495
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002496
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002497#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002498PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002499"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002500Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501
Barry Warsaw53699e91996-12-10 23:23:01 +00002502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002503posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002504{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002505 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002506 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002507 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002508 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002509 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002510 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002511 Py_END_ALLOW_THREADS
2512 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002513}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002514#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002515
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002517PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002518"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002519Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002520
Barry Warsaw53699e91996-12-10 23:23:01 +00002521static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002522posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002523{
2524 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002525 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002526 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002527 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002528 if (i < 0)
2529 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002530 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002531}
2532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002535"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002539"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002540Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541
Barry Warsaw53699e91996-12-10 23:23:01 +00002542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002543posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002544{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002545#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002546 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002547#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002548 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002549#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002550}
2551
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002552
Guido van Rossumb6775db1994-08-01 11:34:53 +00002553#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002554PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002555"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002556Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002557
Barry Warsaw53699e91996-12-10 23:23:01 +00002558static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002559posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002560{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002561 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002562 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002563
Barry Warsaw53699e91996-12-10 23:23:01 +00002564 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002565 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002566 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002567 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002568 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002569 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002570 u.sysname,
2571 u.nodename,
2572 u.release,
2573 u.version,
2574 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002575}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002576#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002577
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002578static int
2579extract_time(PyObject *t, long* sec, long* usec)
2580{
2581 long intval;
2582 if (PyFloat_Check(t)) {
2583 double tval = PyFloat_AsDouble(t);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002584 PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002585 if (!intobj)
2586 return -1;
2587 intval = PyInt_AsLong(intobj);
2588 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002589 if (intval == -1 && PyErr_Occurred())
2590 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002591 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002592 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002593 if (*usec < 0)
2594 /* If rounding gave us a negative number,
2595 truncate. */
2596 *usec = 0;
2597 return 0;
2598 }
2599 intval = PyInt_AsLong(t);
2600 if (intval == -1 && PyErr_Occurred())
2601 return -1;
2602 *sec = intval;
2603 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002604 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002605}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002607PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002608"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002609utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002610Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002612
Barry Warsaw53699e91996-12-10 23:23:01 +00002613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002614posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002615{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002616#ifdef Py_WIN_WIDE_FILENAMES
2617 PyObject *arg;
2618 PyUnicodeObject *obwpath;
2619 wchar_t *wpath = NULL;
2620 char *apath = NULL;
2621 HANDLE hFile;
2622 long atimesec, mtimesec, ausec, musec;
2623 FILETIME atime, mtime;
2624 PyObject *result = NULL;
2625
2626 if (unicode_file_names()) {
2627 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2628 wpath = PyUnicode_AS_UNICODE(obwpath);
2629 Py_BEGIN_ALLOW_THREADS
2630 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002631 NULL, OPEN_EXISTING,
2632 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002633 Py_END_ALLOW_THREADS
2634 if (hFile == INVALID_HANDLE_VALUE)
2635 return win32_error_unicode("utime", wpath);
2636 } else
2637 /* Drop the argument parsing error as narrow strings
2638 are also valid. */
2639 PyErr_Clear();
2640 }
2641 if (!wpath) {
2642 if (!PyArg_ParseTuple(args, "etO:utime",
2643 Py_FileSystemDefaultEncoding, &apath, &arg))
2644 return NULL;
2645 Py_BEGIN_ALLOW_THREADS
2646 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002647 NULL, OPEN_EXISTING,
2648 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002649 Py_END_ALLOW_THREADS
2650 if (hFile == INVALID_HANDLE_VALUE) {
2651 win32_error("utime", apath);
2652 PyMem_Free(apath);
2653 return NULL;
2654 }
2655 PyMem_Free(apath);
2656 }
2657
2658 if (arg == Py_None) {
2659 SYSTEMTIME now;
2660 GetSystemTime(&now);
2661 if (!SystemTimeToFileTime(&now, &mtime) ||
2662 !SystemTimeToFileTime(&now, &atime)) {
2663 win32_error("utime", NULL);
2664 goto done;
2665 }
2666 }
2667 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2668 PyErr_SetString(PyExc_TypeError,
2669 "utime() arg 2 must be a tuple (atime, mtime)");
2670 goto done;
2671 }
2672 else {
2673 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2674 &atimesec, &ausec) == -1)
2675 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002676 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002677 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2678 &mtimesec, &musec) == -1)
2679 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002680 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002681 }
2682 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2683 /* Avoid putting the file name into the error here,
2684 as that may confuse the user into believing that
2685 something is wrong with the file, when it also
2686 could be the time stamp that gives a problem. */
2687 win32_error("utime", NULL);
2688 }
2689 Py_INCREF(Py_None);
2690 result = Py_None;
2691done:
2692 CloseHandle(hFile);
2693 return result;
2694#else /* Py_WIN_WIDE_FILENAMES */
2695
Neal Norwitz2adf2102004-06-09 01:46:02 +00002696 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002697 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002698 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002699 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002700
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002701#if defined(HAVE_UTIMES)
2702 struct timeval buf[2];
2703#define ATIME buf[0].tv_sec
2704#define MTIME buf[1].tv_sec
2705#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002706/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002707 struct utimbuf buf;
2708#define ATIME buf.actime
2709#define MTIME buf.modtime
2710#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002711#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002712 time_t buf[2];
2713#define ATIME buf[0]
2714#define MTIME buf[1]
2715#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002716#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002717
Mark Hammond817c9292003-12-03 01:22:38 +00002718
Thomas Wouters477c8d52006-05-27 19:21:47 +00002719 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002720 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002721 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002722 if (arg == Py_None) {
2723 /* optional time values not given */
2724 Py_BEGIN_ALLOW_THREADS
2725 res = utime(path, NULL);
2726 Py_END_ALLOW_THREADS
2727 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002728 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002729 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002730 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002731 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002732 return NULL;
2733 }
2734 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002735 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002736 &atime, &ausec) == -1) {
2737 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002738 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002739 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002740 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002741 &mtime, &musec) == -1) {
2742 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002743 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002744 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002745 ATIME = atime;
2746 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002747#ifdef HAVE_UTIMES
2748 buf[0].tv_usec = ausec;
2749 buf[1].tv_usec = musec;
2750 Py_BEGIN_ALLOW_THREADS
2751 res = utimes(path, buf);
2752 Py_END_ALLOW_THREADS
2753#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002754 Py_BEGIN_ALLOW_THREADS
2755 res = utime(path, UTIME_ARG);
2756 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002757#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002758 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002759 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002760 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002761 }
Neal Norwitz96652712004-06-06 20:40:27 +00002762 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002763 Py_INCREF(Py_None);
2764 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002765#undef UTIME_ARG
2766#undef ATIME
2767#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002768#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002769}
2770
Guido van Rossum85e3b011991-06-03 12:42:10 +00002771
Guido van Rossum3b066191991-06-04 19:40:25 +00002772/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002775"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002779posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002780{
2781 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002782 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002783 return NULL;
2784 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002785 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002786}
2787
Martin v. Löwis114619e2002-10-07 06:44:21 +00002788#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2789static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002790free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002791{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002792 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002793 for (i = 0; i < count; i++)
2794 PyMem_Free(array[i]);
2795 PyMem_DEL(array);
2796}
2797#endif
2798
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002799
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002800#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002801PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002802"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002803Execute an executable path with arguments, replacing current process.\n\
2804\n\
2805 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002807
Barry Warsaw53699e91996-12-10 23:23:01 +00002808static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002809posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002810{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002811 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002812 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002813 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002814 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002815 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002816
Guido van Rossum89b33251993-10-22 14:26:06 +00002817 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002818 argv is a list or tuple of strings. */
2819
Martin v. Löwis114619e2002-10-07 06:44:21 +00002820 if (!PyArg_ParseTuple(args, "etO:execv",
2821 Py_FileSystemDefaultEncoding,
2822 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002823 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002824 if (PyList_Check(argv)) {
2825 argc = PyList_Size(argv);
2826 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002827 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002828 else if (PyTuple_Check(argv)) {
2829 argc = PyTuple_Size(argv);
2830 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002831 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002832 else {
Fred Drake661ea262000-10-24 19:57:45 +00002833 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002834 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002835 return NULL;
2836 }
2837
Barry Warsaw53699e91996-12-10 23:23:01 +00002838 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002839 if (argvlist == NULL) {
2840 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002841 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002842 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002843 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002844 if (!PyArg_Parse((*getitem)(argv, i), "et",
2845 Py_FileSystemDefaultEncoding,
2846 &argvlist[i])) {
2847 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002848 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002849 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002850 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002851 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002852
Guido van Rossum85e3b011991-06-03 12:42:10 +00002853 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002854 }
2855 argvlist[argc] = NULL;
2856
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002857 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002858
Guido van Rossum85e3b011991-06-03 12:42:10 +00002859 /* If we get here it's definitely an error */
2860
Martin v. Löwis114619e2002-10-07 06:44:21 +00002861 free_string_array(argvlist, argc);
2862 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002863 return posix_error();
2864}
2865
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002867PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002868"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002869Execute a path with arguments and environment, replacing current process.\n\
2870\n\
2871 path: path of executable file\n\
2872 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002873 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874
Barry Warsaw53699e91996-12-10 23:23:01 +00002875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002876posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002877{
2878 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002879 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002880 char **argvlist;
2881 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002882 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002883 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002884 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002885 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002886
2887 /* execve has three arguments: (path, argv, env), where
2888 argv is a list or tuple of strings and env is a dictionary
2889 like posix.environ. */
2890
Martin v. Löwis114619e2002-10-07 06:44:21 +00002891 if (!PyArg_ParseTuple(args, "etOO:execve",
2892 Py_FileSystemDefaultEncoding,
2893 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002894 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002895 if (PyList_Check(argv)) {
2896 argc = PyList_Size(argv);
2897 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002898 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002899 else if (PyTuple_Check(argv)) {
2900 argc = PyTuple_Size(argv);
2901 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002902 }
2903 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002904 PyErr_SetString(PyExc_TypeError,
2905 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002906 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002907 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002908 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002909 PyErr_SetString(PyExc_TypeError,
2910 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002911 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002912 }
2913
Barry Warsaw53699e91996-12-10 23:23:01 +00002914 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002915 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002916 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002917 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002918 }
2919 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002920 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002921 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002922 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002923 &argvlist[i]))
2924 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002925 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002926 goto fail_1;
2927 }
2928 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002929 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002930 argvlist[argc] = NULL;
2931
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002932 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002933 if (i < 0)
2934 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002935 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002936 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002937 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002938 goto fail_1;
2939 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002940 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002941 keys = PyMapping_Keys(env);
2942 vals = PyMapping_Values(env);
2943 if (!keys || !vals)
2944 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002945 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2946 PyErr_SetString(PyExc_TypeError,
2947 "execve(): env.keys() or env.values() is not a list");
2948 goto fail_2;
2949 }
Tim Peters5aa91602002-01-30 05:46:57 +00002950
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002951 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002952 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002953 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002954
2955 key = PyList_GetItem(keys, pos);
2956 val = PyList_GetItem(vals, pos);
2957 if (!key || !val)
2958 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002959
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002960 if (!PyArg_Parse(
2961 key,
2962 "s;execve() arg 3 contains a non-string key",
2963 &k) ||
2964 !PyArg_Parse(
2965 val,
2966 "s;execve() arg 3 contains a non-string value",
2967 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002968 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002969 goto fail_2;
2970 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002971
2972#if defined(PYOS_OS2)
2973 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2974 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2975#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002976 len = PyString_Size(key) + PyString_Size(val) + 2;
2977 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002978 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002979 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002980 goto fail_2;
2981 }
Tim Petersc8996f52001-12-03 20:41:00 +00002982 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002983 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002984#if defined(PYOS_OS2)
2985 }
2986#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002987 }
2988 envlist[envc] = 0;
2989
2990 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00002991
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002992 /* If we get here it's definitely an error */
2993
2994 (void) posix_error();
2995
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002996 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002997 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002998 PyMem_DEL(envlist[envc]);
2999 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003000 fail_1:
3001 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003002 Py_XDECREF(vals);
3003 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003004 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003005 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003006 return NULL;
3007}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003008#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003009
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003010
Guido van Rossuma1065681999-01-25 23:20:23 +00003011#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003013"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003014Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003015\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003016 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003017 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003018 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003019
3020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003021posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003022{
3023 char *path;
3024 PyObject *argv;
3025 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003026 int mode, i;
3027 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003028 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003029 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003030
3031 /* spawnv has three arguments: (mode, path, argv), where
3032 argv is a list or tuple of strings. */
3033
Martin v. Löwis114619e2002-10-07 06:44:21 +00003034 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3035 Py_FileSystemDefaultEncoding,
3036 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003037 return NULL;
3038 if (PyList_Check(argv)) {
3039 argc = PyList_Size(argv);
3040 getitem = PyList_GetItem;
3041 }
3042 else if (PyTuple_Check(argv)) {
3043 argc = PyTuple_Size(argv);
3044 getitem = PyTuple_GetItem;
3045 }
3046 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003047 PyErr_SetString(PyExc_TypeError,
3048 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003049 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003050 return NULL;
3051 }
3052
3053 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003054 if (argvlist == NULL) {
3055 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003056 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003057 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003058 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003059 if (!PyArg_Parse((*getitem)(argv, i), "et",
3060 Py_FileSystemDefaultEncoding,
3061 &argvlist[i])) {
3062 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003063 PyErr_SetString(
3064 PyExc_TypeError,
3065 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003066 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003067 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003068 }
3069 }
3070 argvlist[argc] = NULL;
3071
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003072#if defined(PYOS_OS2) && defined(PYCC_GCC)
3073 Py_BEGIN_ALLOW_THREADS
3074 spawnval = spawnv(mode, path, argvlist);
3075 Py_END_ALLOW_THREADS
3076#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003077 if (mode == _OLD_P_OVERLAY)
3078 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003079
Tim Peters25059d32001-12-07 20:35:43 +00003080 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003081 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003082 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003083#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003084
Martin v. Löwis114619e2002-10-07 06:44:21 +00003085 free_string_array(argvlist, argc);
3086 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003087
Fred Drake699f3522000-06-29 21:12:41 +00003088 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003089 return posix_error();
3090 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003091#if SIZEOF_LONG == SIZEOF_VOID_P
3092 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003093#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003094 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003095#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003096}
3097
3098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003099PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003100"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003101Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003102\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003103 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003104 path: path of executable file\n\
3105 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003107
3108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003109posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003110{
3111 char *path;
3112 PyObject *argv, *env;
3113 char **argvlist;
3114 char **envlist;
3115 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003116 int mode, pos, envc;
3117 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003118 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003119 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003121
3122 /* spawnve has four arguments: (mode, path, argv, env), where
3123 argv is a list or tuple of strings and env is a dictionary
3124 like posix.environ. */
3125
Martin v. Löwis114619e2002-10-07 06:44:21 +00003126 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3127 Py_FileSystemDefaultEncoding,
3128 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003129 return NULL;
3130 if (PyList_Check(argv)) {
3131 argc = PyList_Size(argv);
3132 getitem = PyList_GetItem;
3133 }
3134 else if (PyTuple_Check(argv)) {
3135 argc = PyTuple_Size(argv);
3136 getitem = PyTuple_GetItem;
3137 }
3138 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003139 PyErr_SetString(PyExc_TypeError,
3140 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003141 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003142 }
3143 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003144 PyErr_SetString(PyExc_TypeError,
3145 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003146 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003147 }
3148
3149 argvlist = PyMem_NEW(char *, argc+1);
3150 if (argvlist == NULL) {
3151 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003152 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003153 }
3154 for (i = 0; i < argc; i++) {
3155 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003156 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003157 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003158 &argvlist[i]))
3159 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003160 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003161 goto fail_1;
3162 }
3163 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003164 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003165 argvlist[argc] = NULL;
3166
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003167 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003168 if (i < 0)
3169 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003170 envlist = PyMem_NEW(char *, i + 1);
3171 if (envlist == NULL) {
3172 PyErr_NoMemory();
3173 goto fail_1;
3174 }
3175 envc = 0;
3176 keys = PyMapping_Keys(env);
3177 vals = PyMapping_Values(env);
3178 if (!keys || !vals)
3179 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003180 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3181 PyErr_SetString(PyExc_TypeError,
3182 "spawnve(): env.keys() or env.values() is not a list");
3183 goto fail_2;
3184 }
Tim Peters5aa91602002-01-30 05:46:57 +00003185
Guido van Rossuma1065681999-01-25 23:20:23 +00003186 for (pos = 0; pos < i; pos++) {
3187 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003188 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003189
3190 key = PyList_GetItem(keys, pos);
3191 val = PyList_GetItem(vals, pos);
3192 if (!key || !val)
3193 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003194
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003195 if (!PyArg_Parse(
3196 key,
3197 "s;spawnve() arg 3 contains a non-string key",
3198 &k) ||
3199 !PyArg_Parse(
3200 val,
3201 "s;spawnve() arg 3 contains a non-string value",
3202 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003203 {
3204 goto fail_2;
3205 }
Tim Petersc8996f52001-12-03 20:41:00 +00003206 len = PyString_Size(key) + PyString_Size(val) + 2;
3207 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003208 if (p == NULL) {
3209 PyErr_NoMemory();
3210 goto fail_2;
3211 }
Tim Petersc8996f52001-12-03 20:41:00 +00003212 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003213 envlist[envc++] = p;
3214 }
3215 envlist[envc] = 0;
3216
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003217#if defined(PYOS_OS2) && defined(PYCC_GCC)
3218 Py_BEGIN_ALLOW_THREADS
3219 spawnval = spawnve(mode, path, argvlist, envlist);
3220 Py_END_ALLOW_THREADS
3221#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003222 if (mode == _OLD_P_OVERLAY)
3223 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003224
3225 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003226 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003227 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003228#endif
Tim Peters25059d32001-12-07 20:35:43 +00003229
Fred Drake699f3522000-06-29 21:12:41 +00003230 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003231 (void) posix_error();
3232 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003233#if SIZEOF_LONG == SIZEOF_VOID_P
3234 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003235#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003236 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003237#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003238
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003239 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003240 while (--envc >= 0)
3241 PyMem_DEL(envlist[envc]);
3242 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003243 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003244 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003245 Py_XDECREF(vals);
3246 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003247 fail_0:
3248 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003249 return res;
3250}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003251
3252/* OS/2 supports spawnvp & spawnvpe natively */
3253#if defined(PYOS_OS2)
3254PyDoc_STRVAR(posix_spawnvp__doc__,
3255"spawnvp(mode, file, args)\n\n\
3256Execute the program 'file' in a new process, using the environment\n\
3257search path to find the file.\n\
3258\n\
3259 mode: mode of process creation\n\
3260 file: executable file name\n\
3261 args: tuple or list of strings");
3262
3263static PyObject *
3264posix_spawnvp(PyObject *self, PyObject *args)
3265{
3266 char *path;
3267 PyObject *argv;
3268 char **argvlist;
3269 int mode, i, argc;
3270 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003271 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003272
3273 /* spawnvp has three arguments: (mode, path, argv), where
3274 argv is a list or tuple of strings. */
3275
3276 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3277 Py_FileSystemDefaultEncoding,
3278 &path, &argv))
3279 return NULL;
3280 if (PyList_Check(argv)) {
3281 argc = PyList_Size(argv);
3282 getitem = PyList_GetItem;
3283 }
3284 else if (PyTuple_Check(argv)) {
3285 argc = PyTuple_Size(argv);
3286 getitem = PyTuple_GetItem;
3287 }
3288 else {
3289 PyErr_SetString(PyExc_TypeError,
3290 "spawnvp() arg 2 must be a tuple or list");
3291 PyMem_Free(path);
3292 return NULL;
3293 }
3294
3295 argvlist = PyMem_NEW(char *, argc+1);
3296 if (argvlist == NULL) {
3297 PyMem_Free(path);
3298 return PyErr_NoMemory();
3299 }
3300 for (i = 0; i < argc; i++) {
3301 if (!PyArg_Parse((*getitem)(argv, i), "et",
3302 Py_FileSystemDefaultEncoding,
3303 &argvlist[i])) {
3304 free_string_array(argvlist, i);
3305 PyErr_SetString(
3306 PyExc_TypeError,
3307 "spawnvp() arg 2 must contain only strings");
3308 PyMem_Free(path);
3309 return NULL;
3310 }
3311 }
3312 argvlist[argc] = NULL;
3313
3314 Py_BEGIN_ALLOW_THREADS
3315#if defined(PYCC_GCC)
3316 spawnval = spawnvp(mode, path, argvlist);
3317#else
3318 spawnval = _spawnvp(mode, path, argvlist);
3319#endif
3320 Py_END_ALLOW_THREADS
3321
3322 free_string_array(argvlist, argc);
3323 PyMem_Free(path);
3324
3325 if (spawnval == -1)
3326 return posix_error();
3327 else
3328 return Py_BuildValue("l", (long) spawnval);
3329}
3330
3331
3332PyDoc_STRVAR(posix_spawnvpe__doc__,
3333"spawnvpe(mode, file, args, env)\n\n\
3334Execute the program 'file' in a new process, using the environment\n\
3335search path to find the file.\n\
3336\n\
3337 mode: mode of process creation\n\
3338 file: executable file name\n\
3339 args: tuple or list of arguments\n\
3340 env: dictionary of strings mapping to strings");
3341
3342static PyObject *
3343posix_spawnvpe(PyObject *self, PyObject *args)
3344{
3345 char *path;
3346 PyObject *argv, *env;
3347 char **argvlist;
3348 char **envlist;
3349 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3350 int mode, i, pos, argc, envc;
3351 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003352 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003353 int lastarg = 0;
3354
3355 /* spawnvpe has four arguments: (mode, path, argv, env), where
3356 argv is a list or tuple of strings and env is a dictionary
3357 like posix.environ. */
3358
3359 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3360 Py_FileSystemDefaultEncoding,
3361 &path, &argv, &env))
3362 return NULL;
3363 if (PyList_Check(argv)) {
3364 argc = PyList_Size(argv);
3365 getitem = PyList_GetItem;
3366 }
3367 else if (PyTuple_Check(argv)) {
3368 argc = PyTuple_Size(argv);
3369 getitem = PyTuple_GetItem;
3370 }
3371 else {
3372 PyErr_SetString(PyExc_TypeError,
3373 "spawnvpe() arg 2 must be a tuple or list");
3374 goto fail_0;
3375 }
3376 if (!PyMapping_Check(env)) {
3377 PyErr_SetString(PyExc_TypeError,
3378 "spawnvpe() arg 3 must be a mapping object");
3379 goto fail_0;
3380 }
3381
3382 argvlist = PyMem_NEW(char *, argc+1);
3383 if (argvlist == NULL) {
3384 PyErr_NoMemory();
3385 goto fail_0;
3386 }
3387 for (i = 0; i < argc; i++) {
3388 if (!PyArg_Parse((*getitem)(argv, i),
3389 "et;spawnvpe() arg 2 must contain only strings",
3390 Py_FileSystemDefaultEncoding,
3391 &argvlist[i]))
3392 {
3393 lastarg = i;
3394 goto fail_1;
3395 }
3396 }
3397 lastarg = argc;
3398 argvlist[argc] = NULL;
3399
3400 i = PyMapping_Size(env);
3401 if (i < 0)
3402 goto fail_1;
3403 envlist = PyMem_NEW(char *, i + 1);
3404 if (envlist == NULL) {
3405 PyErr_NoMemory();
3406 goto fail_1;
3407 }
3408 envc = 0;
3409 keys = PyMapping_Keys(env);
3410 vals = PyMapping_Values(env);
3411 if (!keys || !vals)
3412 goto fail_2;
3413 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3414 PyErr_SetString(PyExc_TypeError,
3415 "spawnvpe(): env.keys() or env.values() is not a list");
3416 goto fail_2;
3417 }
3418
3419 for (pos = 0; pos < i; pos++) {
3420 char *p, *k, *v;
3421 size_t len;
3422
3423 key = PyList_GetItem(keys, pos);
3424 val = PyList_GetItem(vals, pos);
3425 if (!key || !val)
3426 goto fail_2;
3427
3428 if (!PyArg_Parse(
3429 key,
3430 "s;spawnvpe() arg 3 contains a non-string key",
3431 &k) ||
3432 !PyArg_Parse(
3433 val,
3434 "s;spawnvpe() arg 3 contains a non-string value",
3435 &v))
3436 {
3437 goto fail_2;
3438 }
3439 len = PyString_Size(key) + PyString_Size(val) + 2;
3440 p = PyMem_NEW(char, len);
3441 if (p == NULL) {
3442 PyErr_NoMemory();
3443 goto fail_2;
3444 }
3445 PyOS_snprintf(p, len, "%s=%s", k, v);
3446 envlist[envc++] = p;
3447 }
3448 envlist[envc] = 0;
3449
3450 Py_BEGIN_ALLOW_THREADS
3451#if defined(PYCC_GCC)
3452 spawnval = spawnve(mode, path, argvlist, envlist);
3453#else
3454 spawnval = _spawnve(mode, path, argvlist, envlist);
3455#endif
3456 Py_END_ALLOW_THREADS
3457
3458 if (spawnval == -1)
3459 (void) posix_error();
3460 else
3461 res = Py_BuildValue("l", (long) spawnval);
3462
3463 fail_2:
3464 while (--envc >= 0)
3465 PyMem_DEL(envlist[envc]);
3466 PyMem_DEL(envlist);
3467 fail_1:
3468 free_string_array(argvlist, lastarg);
3469 Py_XDECREF(vals);
3470 Py_XDECREF(keys);
3471 fail_0:
3472 PyMem_Free(path);
3473 return res;
3474}
3475#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003476#endif /* HAVE_SPAWNV */
3477
3478
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003479#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003480PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003481"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003482Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3483\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003484Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003485
3486static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003487posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003488{
Neal Norwitze241ce82003-02-17 18:17:05 +00003489 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003490 if (pid == -1)
3491 return posix_error();
3492 PyOS_AfterFork();
3493 return PyInt_FromLong((long)pid);
3494}
3495#endif
3496
3497
Guido van Rossumad0ee831995-03-01 10:34:45 +00003498#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003499PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003500"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003501Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003502Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003503
Barry Warsaw53699e91996-12-10 23:23:01 +00003504static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003505posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003506{
Neal Norwitze241ce82003-02-17 18:17:05 +00003507 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003508 if (pid == -1)
3509 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003510 if (pid == 0)
3511 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003512 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003513}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003514#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003515
Neal Norwitzb59798b2003-03-21 01:43:31 +00003516/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003517/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3518#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003519#define DEV_PTY_FILE "/dev/ptc"
3520#define HAVE_DEV_PTMX
3521#else
3522#define DEV_PTY_FILE "/dev/ptmx"
3523#endif
3524
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003525#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003526#ifdef HAVE_PTY_H
3527#include <pty.h>
3528#else
3529#ifdef HAVE_LIBUTIL_H
3530#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003531#endif /* HAVE_LIBUTIL_H */
3532#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003533#ifdef HAVE_STROPTS_H
3534#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003535#endif
3536#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003537
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003538#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003539PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003540"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003541Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003542
3543static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003544posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003545{
3546 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003547#ifndef HAVE_OPENPTY
3548 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003549#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003550#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003551 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003552#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003553 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003554#endif
3555#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003556
Thomas Wouters70c21a12000-07-14 14:28:33 +00003557#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003558 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3559 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003560#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003561 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3562 if (slave_name == NULL)
3563 return posix_error();
3564
3565 slave_fd = open(slave_name, O_RDWR);
3566 if (slave_fd < 0)
3567 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003568#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003569 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003570 if (master_fd < 0)
3571 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003572 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003573 /* change permission of slave */
3574 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003575 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003576 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003577 }
3578 /* unlock slave */
3579 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003580 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003581 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003582 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003583 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003584 slave_name = ptsname(master_fd); /* get name of slave */
3585 if (slave_name == NULL)
3586 return posix_error();
3587 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3588 if (slave_fd < 0)
3589 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003590#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003591 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3592 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003593#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003594 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003595#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003596#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003597#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003598
Fred Drake8cef4cf2000-06-28 16:40:38 +00003599 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003600
Fred Drake8cef4cf2000-06-28 16:40:38 +00003601}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003602#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003603
3604#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003605PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003606"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003607Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3608Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003609To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003610
3611static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003612posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003613{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003614 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003615
Fred Drake8cef4cf2000-06-28 16:40:38 +00003616 pid = forkpty(&master_fd, NULL, NULL, NULL);
3617 if (pid == -1)
3618 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003619 if (pid == 0)
3620 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003621 return Py_BuildValue("(ii)", pid, master_fd);
3622}
3623#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003624
Guido van Rossumad0ee831995-03-01 10:34:45 +00003625#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003626PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003627"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003628Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003629
Barry Warsaw53699e91996-12-10 23:23:01 +00003630static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003631posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003632{
Barry Warsaw53699e91996-12-10 23:23:01 +00003633 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003634}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003635#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003636
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003637
Guido van Rossumad0ee831995-03-01 10:34:45 +00003638#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003639PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003640"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003641Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003642
Barry Warsaw53699e91996-12-10 23:23:01 +00003643static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003644posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003645{
Barry Warsaw53699e91996-12-10 23:23:01 +00003646 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003647}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003648#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003649
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003650
Guido van Rossumad0ee831995-03-01 10:34:45 +00003651#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003652PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003653"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003654Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003655
Barry Warsaw53699e91996-12-10 23:23:01 +00003656static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003657posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003658{
Barry Warsaw53699e91996-12-10 23:23:01 +00003659 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003660}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003661#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003662
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003664PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003665"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003666Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003667
Barry Warsaw53699e91996-12-10 23:23:01 +00003668static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003669posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003670{
Barry Warsaw53699e91996-12-10 23:23:01 +00003671 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003672}
3673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003674
Fred Drakec9680921999-12-13 16:37:25 +00003675#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003676PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003677"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003678Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003679
3680static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003681posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003682{
3683 PyObject *result = NULL;
3684
Fred Drakec9680921999-12-13 16:37:25 +00003685#ifdef NGROUPS_MAX
3686#define MAX_GROUPS NGROUPS_MAX
3687#else
3688 /* defined to be 16 on Solaris7, so this should be a small number */
3689#define MAX_GROUPS 64
3690#endif
3691 gid_t grouplist[MAX_GROUPS];
3692 int n;
3693
3694 n = getgroups(MAX_GROUPS, grouplist);
3695 if (n < 0)
3696 posix_error();
3697 else {
3698 result = PyList_New(n);
3699 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003700 int i;
3701 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003702 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003703 if (o == NULL) {
3704 Py_DECREF(result);
3705 result = NULL;
3706 break;
3707 }
3708 PyList_SET_ITEM(result, i, o);
3709 }
3710 }
3711 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003712
Fred Drakec9680921999-12-13 16:37:25 +00003713 return result;
3714}
3715#endif
3716
Martin v. Löwis606edc12002-06-13 21:09:11 +00003717#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003718PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003719"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003720Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003721
3722static PyObject *
3723posix_getpgid(PyObject *self, PyObject *args)
3724{
3725 int pid, pgid;
3726 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3727 return NULL;
3728 pgid = getpgid(pid);
3729 if (pgid < 0)
3730 return posix_error();
3731 return PyInt_FromLong((long)pgid);
3732}
3733#endif /* HAVE_GETPGID */
3734
3735
Guido van Rossumb6775db1994-08-01 11:34:53 +00003736#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003737PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003738"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003739Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003740
Barry Warsaw53699e91996-12-10 23:23:01 +00003741static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003742posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003743{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003744#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003745 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003746#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003747 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003748#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003749}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003750#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003752
Guido van Rossumb6775db1994-08-01 11:34:53 +00003753#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003754PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003755"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003756Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003757
Barry Warsaw53699e91996-12-10 23:23:01 +00003758static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003759posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003760{
Guido van Rossum64933891994-10-20 21:56:42 +00003761#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003762 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003763#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003764 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003765#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003766 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003767 Py_INCREF(Py_None);
3768 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003769}
3770
Guido van Rossumb6775db1994-08-01 11:34:53 +00003771#endif /* HAVE_SETPGRP */
3772
Guido van Rossumad0ee831995-03-01 10:34:45 +00003773#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003774PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003775"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003776Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003777
Barry Warsaw53699e91996-12-10 23:23:01 +00003778static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003779posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003780{
Barry Warsaw53699e91996-12-10 23:23:01 +00003781 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003782}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003783#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003785
Fred Drake12c6e2d1999-12-14 21:25:03 +00003786#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003790
3791static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003792posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003793{
Neal Norwitze241ce82003-02-17 18:17:05 +00003794 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003795 char *name;
3796 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003797
Fred Drakea30680b2000-12-06 21:24:28 +00003798 errno = 0;
3799 name = getlogin();
3800 if (name == NULL) {
3801 if (errno)
3802 posix_error();
3803 else
3804 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003805 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003806 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003807 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003808 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003809 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003810
Fred Drake12c6e2d1999-12-14 21:25:03 +00003811 return result;
3812}
3813#endif
3814
Guido van Rossumad0ee831995-03-01 10:34:45 +00003815#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003816PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003817"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003818Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003819
Barry Warsaw53699e91996-12-10 23:23:01 +00003820static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003821posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003822{
Barry Warsaw53699e91996-12-10 23:23:01 +00003823 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003824}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003825#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003827
Guido van Rossumad0ee831995-03-01 10:34:45 +00003828#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003829PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003830"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003831Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003832
Barry Warsaw53699e91996-12-10 23:23:01 +00003833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003834posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003835{
3836 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003837 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003838 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003839#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003840 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3841 APIRET rc;
3842 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003843 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003844
3845 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3846 APIRET rc;
3847 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003848 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003849
3850 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003851 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003852#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003853 if (kill(pid, sig) == -1)
3854 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003855#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003856 Py_INCREF(Py_None);
3857 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003858}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003859#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003860
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003861#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003862PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003863"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003864Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003865
3866static PyObject *
3867posix_killpg(PyObject *self, PyObject *args)
3868{
3869 int pgid, sig;
3870 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3871 return NULL;
3872 if (killpg(pgid, sig) == -1)
3873 return posix_error();
3874 Py_INCREF(Py_None);
3875 return Py_None;
3876}
3877#endif
3878
Guido van Rossumc0125471996-06-28 18:55:32 +00003879#ifdef HAVE_PLOCK
3880
3881#ifdef HAVE_SYS_LOCK_H
3882#include <sys/lock.h>
3883#endif
3884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003885PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003886"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003887Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003888
Barry Warsaw53699e91996-12-10 23:23:01 +00003889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003890posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003891{
3892 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003893 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003894 return NULL;
3895 if (plock(op) == -1)
3896 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003897 Py_INCREF(Py_None);
3898 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003899}
3900#endif
3901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003902
Guido van Rossum3b066191991-06-04 19:40:25 +00003903
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003904
Guido van Rossumb6775db1994-08-01 11:34:53 +00003905#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003906PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003907"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003908Set the current process's user id.");
3909
Barry Warsaw53699e91996-12-10 23:23:01 +00003910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003911posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003912{
3913 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003914 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003915 return NULL;
3916 if (setuid(uid) < 0)
3917 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003918 Py_INCREF(Py_None);
3919 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003920}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003921#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003923
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003924#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003925PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003926"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003927Set the current process's effective user id.");
3928
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003929static PyObject *
3930posix_seteuid (PyObject *self, PyObject *args)
3931{
3932 int euid;
3933 if (!PyArg_ParseTuple(args, "i", &euid)) {
3934 return NULL;
3935 } else if (seteuid(euid) < 0) {
3936 return posix_error();
3937 } else {
3938 Py_INCREF(Py_None);
3939 return Py_None;
3940 }
3941}
3942#endif /* HAVE_SETEUID */
3943
3944#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003945PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003946"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003947Set the current process's effective group id.");
3948
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003949static PyObject *
3950posix_setegid (PyObject *self, PyObject *args)
3951{
3952 int egid;
3953 if (!PyArg_ParseTuple(args, "i", &egid)) {
3954 return NULL;
3955 } else if (setegid(egid) < 0) {
3956 return posix_error();
3957 } else {
3958 Py_INCREF(Py_None);
3959 return Py_None;
3960 }
3961}
3962#endif /* HAVE_SETEGID */
3963
3964#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003965PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003966"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003967Set the current process's real and effective user ids.");
3968
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003969static PyObject *
3970posix_setreuid (PyObject *self, PyObject *args)
3971{
3972 int ruid, euid;
3973 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
3974 return NULL;
3975 } else if (setreuid(ruid, euid) < 0) {
3976 return posix_error();
3977 } else {
3978 Py_INCREF(Py_None);
3979 return Py_None;
3980 }
3981}
3982#endif /* HAVE_SETREUID */
3983
3984#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003985PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003986"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003987Set the current process's real and effective group ids.");
3988
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003989static PyObject *
3990posix_setregid (PyObject *self, PyObject *args)
3991{
3992 int rgid, egid;
3993 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
3994 return NULL;
3995 } else if (setregid(rgid, egid) < 0) {
3996 return posix_error();
3997 } else {
3998 Py_INCREF(Py_None);
3999 return Py_None;
4000 }
4001}
4002#endif /* HAVE_SETREGID */
4003
Guido van Rossumb6775db1994-08-01 11:34:53 +00004004#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004006"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004007Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004008
Barry Warsaw53699e91996-12-10 23:23:01 +00004009static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004010posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004011{
4012 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004013 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004014 return NULL;
4015 if (setgid(gid) < 0)
4016 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004017 Py_INCREF(Py_None);
4018 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004019}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004020#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004021
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004022#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004023PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004024"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004025Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004026
4027static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004028posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004029{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004030 int i, len;
4031 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004032
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004033 if (!PySequence_Check(groups)) {
4034 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4035 return NULL;
4036 }
4037 len = PySequence_Size(groups);
4038 if (len > MAX_GROUPS) {
4039 PyErr_SetString(PyExc_ValueError, "too many groups");
4040 return NULL;
4041 }
4042 for(i = 0; i < len; i++) {
4043 PyObject *elem;
4044 elem = PySequence_GetItem(groups, i);
4045 if (!elem)
4046 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004047 if (!PyLong_Check(elem)) {
4048 PyErr_SetString(PyExc_TypeError,
4049 "groups must be integers");
4050 Py_DECREF(elem);
4051 return NULL;
4052 } else {
4053 unsigned long x = PyLong_AsUnsignedLong(elem);
4054 if (PyErr_Occurred()) {
4055 PyErr_SetString(PyExc_TypeError,
4056 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004057 Py_DECREF(elem);
4058 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004059 }
Georg Brandla13c2442005-11-22 19:30:31 +00004060 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004061 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004062 if (grouplist[i] != x) {
4063 PyErr_SetString(PyExc_TypeError,
4064 "group id too big");
4065 Py_DECREF(elem);
4066 return NULL;
4067 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004068 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004069 Py_DECREF(elem);
4070 }
4071
4072 if (setgroups(len, grouplist) < 0)
4073 return posix_error();
4074 Py_INCREF(Py_None);
4075 return Py_None;
4076}
4077#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004078
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004079#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4080static PyObject *
4081wait_helper(int pid, int status, struct rusage *ru)
4082{
4083 PyObject *result;
4084 static PyObject *struct_rusage;
4085
4086 if (pid == -1)
4087 return posix_error();
4088
4089 if (struct_rusage == NULL) {
4090 PyObject *m = PyImport_ImportModule("resource");
4091 if (m == NULL)
4092 return NULL;
4093 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4094 Py_DECREF(m);
4095 if (struct_rusage == NULL)
4096 return NULL;
4097 }
4098
4099 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4100 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4101 if (!result)
4102 return NULL;
4103
4104#ifndef doubletime
4105#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4106#endif
4107
4108 PyStructSequence_SET_ITEM(result, 0,
4109 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4110 PyStructSequence_SET_ITEM(result, 1,
4111 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4112#define SET_INT(result, index, value)\
4113 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4114 SET_INT(result, 2, ru->ru_maxrss);
4115 SET_INT(result, 3, ru->ru_ixrss);
4116 SET_INT(result, 4, ru->ru_idrss);
4117 SET_INT(result, 5, ru->ru_isrss);
4118 SET_INT(result, 6, ru->ru_minflt);
4119 SET_INT(result, 7, ru->ru_majflt);
4120 SET_INT(result, 8, ru->ru_nswap);
4121 SET_INT(result, 9, ru->ru_inblock);
4122 SET_INT(result, 10, ru->ru_oublock);
4123 SET_INT(result, 11, ru->ru_msgsnd);
4124 SET_INT(result, 12, ru->ru_msgrcv);
4125 SET_INT(result, 13, ru->ru_nsignals);
4126 SET_INT(result, 14, ru->ru_nvcsw);
4127 SET_INT(result, 15, ru->ru_nivcsw);
4128#undef SET_INT
4129
4130 if (PyErr_Occurred()) {
4131 Py_DECREF(result);
4132 return NULL;
4133 }
4134
4135 return Py_BuildValue("iiN", pid, status, result);
4136}
4137#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4138
4139#ifdef HAVE_WAIT3
4140PyDoc_STRVAR(posix_wait3__doc__,
4141"wait3(options) -> (pid, status, rusage)\n\n\
4142Wait for completion of a child process.");
4143
4144static PyObject *
4145posix_wait3(PyObject *self, PyObject *args)
4146{
4147 int pid, options;
4148 struct rusage ru;
4149 WAIT_TYPE status;
4150 WAIT_STATUS_INT(status) = 0;
4151
4152 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4153 return NULL;
4154
4155 Py_BEGIN_ALLOW_THREADS
4156 pid = wait3(&status, options, &ru);
4157 Py_END_ALLOW_THREADS
4158
4159 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4160}
4161#endif /* HAVE_WAIT3 */
4162
4163#ifdef HAVE_WAIT4
4164PyDoc_STRVAR(posix_wait4__doc__,
4165"wait4(pid, options) -> (pid, status, rusage)\n\n\
4166Wait for completion of a given child process.");
4167
4168static PyObject *
4169posix_wait4(PyObject *self, PyObject *args)
4170{
4171 int pid, options;
4172 struct rusage ru;
4173 WAIT_TYPE status;
4174 WAIT_STATUS_INT(status) = 0;
4175
4176 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4177 return NULL;
4178
4179 Py_BEGIN_ALLOW_THREADS
4180 pid = wait4(pid, &status, options, &ru);
4181 Py_END_ALLOW_THREADS
4182
4183 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4184}
4185#endif /* HAVE_WAIT4 */
4186
Guido van Rossumb6775db1994-08-01 11:34:53 +00004187#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004188PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004189"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004190Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004191
Barry Warsaw53699e91996-12-10 23:23:01 +00004192static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004193posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004194{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004195 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004196 WAIT_TYPE status;
4197 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004198
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004199 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004200 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004201 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004202 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004203 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004204 if (pid == -1)
4205 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004206
4207 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004208}
4209
Tim Petersab034fa2002-02-01 11:27:43 +00004210#elif defined(HAVE_CWAIT)
4211
4212/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004214"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004215"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004216
4217static PyObject *
4218posix_waitpid(PyObject *self, PyObject *args)
4219{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004220 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004221 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004222
4223 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4224 return NULL;
4225 Py_BEGIN_ALLOW_THREADS
4226 pid = _cwait(&status, pid, options);
4227 Py_END_ALLOW_THREADS
4228 if (pid == -1)
4229 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004230
4231 /* shift the status left a byte so this is more like the POSIX waitpid */
4232 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004233}
4234#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004235
Guido van Rossumad0ee831995-03-01 10:34:45 +00004236#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004237PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004238"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004240
Barry Warsaw53699e91996-12-10 23:23:01 +00004241static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004242posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004243{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004244 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004245 WAIT_TYPE status;
4246 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004247
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004248 Py_BEGIN_ALLOW_THREADS
4249 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004250 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004251 if (pid == -1)
4252 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004253
4254 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004255}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004256#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004257
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004259PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004260"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004261Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004262
Barry Warsaw53699e91996-12-10 23:23:01 +00004263static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004264posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004265{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004266#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004267 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004268#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004269#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004270 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004271#else
4272 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4273#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004274#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004275}
4276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004277
Guido van Rossumb6775db1994-08-01 11:34:53 +00004278#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004279PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004280"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004281Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004282
Barry Warsaw53699e91996-12-10 23:23:01 +00004283static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004284posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004285{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004286 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004287 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004288 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004289 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004290 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004291
4292 if (!PyArg_ParseTuple(args, "et:readlink",
4293 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004294 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004295 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004296 if (v == NULL) {
4297 PyMem_Free(path);
4298 return NULL;
4299 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004300
4301 if (PyUnicode_Check(v)) {
4302 arg_is_unicode = 1;
4303 }
4304 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004305
Barry Warsaw53699e91996-12-10 23:23:01 +00004306 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004307 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004308 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004309 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004310 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004311
Neal Norwitzfca70052007-08-12 16:56:02 +00004312 PyMem_Free(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004313 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004314 if (arg_is_unicode) {
4315 PyObject *w;
4316
4317 w = PyUnicode_FromEncodedObject(v,
4318 Py_FileSystemDefaultEncoding,
4319 "strict");
4320 if (w != NULL) {
4321 Py_DECREF(v);
4322 v = w;
4323 }
4324 else {
4325 /* fall back to the original byte string, as
4326 discussed in patch #683592 */
4327 PyErr_Clear();
4328 }
4329 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004330 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004331}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004332#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004333
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004334
Guido van Rossumb6775db1994-08-01 11:34:53 +00004335#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004336PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004337"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004338Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004339
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004340static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004341posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004342{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004343 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004344}
4345#endif /* HAVE_SYMLINK */
4346
4347
4348#ifdef HAVE_TIMES
4349#ifndef HZ
4350#define HZ 60 /* Universal constant :-) */
4351#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004352
Guido van Rossumd48f2521997-12-05 22:19:34 +00004353#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4354static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004355system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004356{
4357 ULONG value = 0;
4358
4359 Py_BEGIN_ALLOW_THREADS
4360 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4361 Py_END_ALLOW_THREADS
4362
4363 return value;
4364}
4365
4366static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004367posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004368{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004369 /* Currently Only Uptime is Provided -- Others Later */
4370 return Py_BuildValue("ddddd",
4371 (double)0 /* t.tms_utime / HZ */,
4372 (double)0 /* t.tms_stime / HZ */,
4373 (double)0 /* t.tms_cutime / HZ */,
4374 (double)0 /* t.tms_cstime / HZ */,
4375 (double)system_uptime() / 1000);
4376}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004377#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004378static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004379posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004380{
4381 struct tms t;
4382 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004383 errno = 0;
4384 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004385 if (c == (clock_t) -1)
4386 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004387 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004388 (double)t.tms_utime / HZ,
4389 (double)t.tms_stime / HZ,
4390 (double)t.tms_cutime / HZ,
4391 (double)t.tms_cstime / HZ,
4392 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004393}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004394#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004395#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004396
4397
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004398#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004399#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004401posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004402{
4403 FILETIME create, exit, kernel, user;
4404 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004405 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004406 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4407 /* The fields of a FILETIME structure are the hi and lo part
4408 of a 64-bit value expressed in 100 nanosecond units.
4409 1e7 is one second in such units; 1e-7 the inverse.
4410 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4411 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004412 return Py_BuildValue(
4413 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004414 (double)(kernel.dwHighDateTime*429.4967296 +
4415 kernel.dwLowDateTime*1e-7),
4416 (double)(user.dwHighDateTime*429.4967296 +
4417 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004418 (double)0,
4419 (double)0,
4420 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004421}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004422#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004423
4424#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004425PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004426"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004427Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004428#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004429
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004430
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004431#ifdef HAVE_GETSID
4432PyDoc_STRVAR(posix_getsid__doc__,
4433"getsid(pid) -> sid\n\n\
4434Call the system call getsid().");
4435
4436static PyObject *
4437posix_getsid(PyObject *self, PyObject *args)
4438{
4439 int pid, sid;
4440 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4441 return NULL;
4442 sid = getsid(pid);
4443 if (sid < 0)
4444 return posix_error();
4445 return PyInt_FromLong((long)sid);
4446}
4447#endif /* HAVE_GETSID */
4448
4449
Guido van Rossumb6775db1994-08-01 11:34:53 +00004450#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004451PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004452"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004453Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004454
Barry Warsaw53699e91996-12-10 23:23:01 +00004455static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004456posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004457{
Guido van Rossum687dd131993-05-17 08:34:16 +00004458 if (setsid() < 0)
4459 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004460 Py_INCREF(Py_None);
4461 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004462}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004463#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004464
Guido van Rossumb6775db1994-08-01 11:34:53 +00004465#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004466PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004467"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004468Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004469
Barry Warsaw53699e91996-12-10 23:23:01 +00004470static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004471posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004472{
4473 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004474 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004475 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004476 if (setpgid(pid, pgrp) < 0)
4477 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004478 Py_INCREF(Py_None);
4479 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004480}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004481#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004482
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004483
Guido van Rossumb6775db1994-08-01 11:34:53 +00004484#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004485PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004486"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004487Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004488
Barry Warsaw53699e91996-12-10 23:23:01 +00004489static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004490posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004491{
4492 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004493 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004494 return NULL;
4495 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004496 if (pgid < 0)
4497 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004498 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004499}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004500#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004501
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004502
Guido van Rossumb6775db1994-08-01 11:34:53 +00004503#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004504PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004505"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004506Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004507
Barry Warsaw53699e91996-12-10 23:23:01 +00004508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004509posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004510{
4511 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004512 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004513 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004514 if (tcsetpgrp(fd, pgid) < 0)
4515 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004516 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004517 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004518}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004519#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004520
Guido van Rossum687dd131993-05-17 08:34:16 +00004521/* Functions acting on file descriptors */
4522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004523PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004524"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004525Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004526
Barry Warsaw53699e91996-12-10 23:23:01 +00004527static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004528posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004529{
Mark Hammondef8b6542001-05-13 08:04:26 +00004530 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004531 int flag;
4532 int mode = 0777;
4533 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004534
4535#ifdef MS_WINDOWS
4536 if (unicode_file_names()) {
4537 PyUnicodeObject *po;
4538 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4539 Py_BEGIN_ALLOW_THREADS
4540 /* PyUnicode_AS_UNICODE OK without thread
4541 lock as it is a simple dereference. */
4542 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4543 Py_END_ALLOW_THREADS
4544 if (fd < 0)
4545 return posix_error();
4546 return PyInt_FromLong((long)fd);
4547 }
4548 /* Drop the argument parsing error as narrow strings
4549 are also valid. */
4550 PyErr_Clear();
4551 }
4552#endif
4553
Tim Peters5aa91602002-01-30 05:46:57 +00004554 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004555 Py_FileSystemDefaultEncoding, &file,
4556 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004557 return NULL;
4558
Barry Warsaw53699e91996-12-10 23:23:01 +00004559 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004560 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004561 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004562 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004563 return posix_error_with_allocated_filename(file);
4564 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004565 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004566}
4567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004569PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004570"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004571Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004572
Barry Warsaw53699e91996-12-10 23:23:01 +00004573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004574posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004575{
4576 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004577 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004578 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004579 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004580 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004581 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004582 if (res < 0)
4583 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004584 Py_INCREF(Py_None);
4585 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004586}
4587
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004589PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004590"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004591Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004592
Barry Warsaw53699e91996-12-10 23:23:01 +00004593static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004594posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004595{
4596 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004597 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004598 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004599 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004600 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004601 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004602 if (fd < 0)
4603 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004604 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004605}
4606
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004608PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004609"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004610Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004611
Barry Warsaw53699e91996-12-10 23:23:01 +00004612static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004613posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004614{
4615 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004616 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004617 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004618 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004619 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004620 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004621 if (res < 0)
4622 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004623 Py_INCREF(Py_None);
4624 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004625}
4626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004628PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004629"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004630Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004631
Barry Warsaw53699e91996-12-10 23:23:01 +00004632static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004633posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004634{
4635 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004636#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004637 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004638#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004639 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004640#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004641 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004642 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004643 return NULL;
4644#ifdef SEEK_SET
4645 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4646 switch (how) {
4647 case 0: how = SEEK_SET; break;
4648 case 1: how = SEEK_CUR; break;
4649 case 2: how = SEEK_END; break;
4650 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004651#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004652
4653#if !defined(HAVE_LARGEFILE_SUPPORT)
4654 pos = PyInt_AsLong(posobj);
4655#else
4656 pos = PyLong_Check(posobj) ?
4657 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4658#endif
4659 if (PyErr_Occurred())
4660 return NULL;
4661
Barry Warsaw53699e91996-12-10 23:23:01 +00004662 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004663#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004664 res = _lseeki64(fd, pos, how);
4665#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004666 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004667#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004668 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004669 if (res < 0)
4670 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004671
4672#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004673 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004674#else
4675 return PyLong_FromLongLong(res);
4676#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004677}
4678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004680PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004681"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004682Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004683
Barry Warsaw53699e91996-12-10 23:23:01 +00004684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004685posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004686{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004687 int fd, size;
4688 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004689 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004690 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004691 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004692 if (size < 0) {
4693 errno = EINVAL;
4694 return posix_error();
4695 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00004696 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004697 if (buffer == NULL)
4698 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00004700 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004701 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004702 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004703 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004704 return posix_error();
4705 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004706 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00004707 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004708 return buffer;
4709}
4710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004712PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004713"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004714Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004715
Barry Warsaw53699e91996-12-10 23:23:01 +00004716static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004717posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004718{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004719 int fd;
4720 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004721 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004722
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004723 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004724 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004725 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004726 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004727 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004728 if (size < 0)
4729 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004730 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004731}
4732
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004734PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004735"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004736Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004737
Barry Warsaw53699e91996-12-10 23:23:01 +00004738static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004739posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004740{
4741 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004742 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004743 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004744 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004745 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004746#ifdef __VMS
4747 /* on OpenVMS we must ensure that all bytes are written to the file */
4748 fsync(fd);
4749#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004750 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004751 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004752 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004753 if (res != 0) {
4754#ifdef MS_WINDOWS
4755 return win32_error("fstat", NULL);
4756#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004757 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004758#endif
4759 }
Tim Peters5aa91602002-01-30 05:46:57 +00004760
Martin v. Löwis14694662006-02-03 12:54:16 +00004761 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004762}
4763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004764PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004765"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004766Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004767connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004768
4769static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004770posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004771{
4772 int fd;
4773 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4774 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004775 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004776}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004777
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004778#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004779PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004780"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004781Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004782
Barry Warsaw53699e91996-12-10 23:23:01 +00004783static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004784posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004785{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004786#if defined(PYOS_OS2)
4787 HFILE read, write;
4788 APIRET rc;
4789
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004790 Py_BEGIN_ALLOW_THREADS
4791 rc = DosCreatePipe( &read, &write, 4096);
4792 Py_END_ALLOW_THREADS
4793 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004794 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004795
4796 return Py_BuildValue("(ii)", read, write);
4797#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004798#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004799 int fds[2];
4800 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004801 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004802 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004803 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004804 if (res != 0)
4805 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004806 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004807#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004808 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004809 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004810 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004811 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004812 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004813 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004814 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004815 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004816 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4817 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004818 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004819#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004820#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004821}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004822#endif /* HAVE_PIPE */
4823
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004824
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004825#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004826PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004827"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004828Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004829
Barry Warsaw53699e91996-12-10 23:23:01 +00004830static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004831posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004832{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004833 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004834 int mode = 0666;
4835 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004836 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004837 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004838 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004839 res = mkfifo(filename, mode);
4840 Py_END_ALLOW_THREADS
4841 if (res < 0)
4842 return posix_error();
4843 Py_INCREF(Py_None);
4844 return Py_None;
4845}
4846#endif
4847
4848
Neal Norwitz11690112002-07-30 01:08:28 +00004849#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004850PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004851"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004852Create a filesystem node (file, device special file or named pipe)\n\
4853named filename. mode specifies both the permissions to use and the\n\
4854type of node to be created, being combined (bitwise OR) with one of\n\
4855S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004856device defines the newly created device special file (probably using\n\
4857os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004858
4859
4860static PyObject *
4861posix_mknod(PyObject *self, PyObject *args)
4862{
4863 char *filename;
4864 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004865 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004866 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00004867 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004868 return NULL;
4869 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004870 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00004871 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004872 if (res < 0)
4873 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004874 Py_INCREF(Py_None);
4875 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004876}
4877#endif
4878
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004879#ifdef HAVE_DEVICE_MACROS
4880PyDoc_STRVAR(posix_major__doc__,
4881"major(device) -> major number\n\
4882Extracts a device major number from a raw device number.");
4883
4884static PyObject *
4885posix_major(PyObject *self, PyObject *args)
4886{
4887 int device;
4888 if (!PyArg_ParseTuple(args, "i:major", &device))
4889 return NULL;
4890 return PyInt_FromLong((long)major(device));
4891}
4892
4893PyDoc_STRVAR(posix_minor__doc__,
4894"minor(device) -> minor number\n\
4895Extracts a device minor number from a raw device number.");
4896
4897static PyObject *
4898posix_minor(PyObject *self, PyObject *args)
4899{
4900 int device;
4901 if (!PyArg_ParseTuple(args, "i:minor", &device))
4902 return NULL;
4903 return PyInt_FromLong((long)minor(device));
4904}
4905
4906PyDoc_STRVAR(posix_makedev__doc__,
4907"makedev(major, minor) -> device number\n\
4908Composes a raw device number from the major and minor device numbers.");
4909
4910static PyObject *
4911posix_makedev(PyObject *self, PyObject *args)
4912{
4913 int major, minor;
4914 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
4915 return NULL;
4916 return PyInt_FromLong((long)makedev(major, minor));
4917}
4918#endif /* device macros */
4919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004920
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004921#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004922PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004923"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004924Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004925
Barry Warsaw53699e91996-12-10 23:23:01 +00004926static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004927posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004928{
4929 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004930 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004931 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004932 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004933
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004934 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004935 return NULL;
4936
4937#if !defined(HAVE_LARGEFILE_SUPPORT)
4938 length = PyInt_AsLong(lenobj);
4939#else
4940 length = PyLong_Check(lenobj) ?
4941 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
4942#endif
4943 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004944 return NULL;
4945
Barry Warsaw53699e91996-12-10 23:23:01 +00004946 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004947 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00004948 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004949 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004950 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004951 return NULL;
4952 }
Barry Warsaw53699e91996-12-10 23:23:01 +00004953 Py_INCREF(Py_None);
4954 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004955}
4956#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004957
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004958#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004959PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004960"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004961Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004962
Fred Drake762e2061999-08-26 17:23:54 +00004963/* Save putenv() parameters as values here, so we can collect them when they
4964 * get re-set with another call for the same key. */
4965static PyObject *posix_putenv_garbage;
4966
Tim Peters5aa91602002-01-30 05:46:57 +00004967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004968posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004969{
4970 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004971 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00004972 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00004973 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004974
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004975 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004976 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00004977
4978#if defined(PYOS_OS2)
4979 if (stricmp(s1, "BEGINLIBPATH") == 0) {
4980 APIRET rc;
4981
Guido van Rossumd48f2521997-12-05 22:19:34 +00004982 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
4983 if (rc != NO_ERROR)
4984 return os2_error(rc);
4985
4986 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
4987 APIRET rc;
4988
Guido van Rossumd48f2521997-12-05 22:19:34 +00004989 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
4990 if (rc != NO_ERROR)
4991 return os2_error(rc);
4992 } else {
4993#endif
4994
Fred Drake762e2061999-08-26 17:23:54 +00004995 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00004996 len = strlen(s1) + strlen(s2) + 2;
4997 /* len includes space for a trailing \0; the size arg to
4998 PyString_FromStringAndSize does not count that */
4999 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005000 if (newstr == NULL)
5001 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005002 newenv = PyString_AS_STRING(newstr);
5003 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5004 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005005 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005006 posix_error();
5007 return NULL;
5008 }
Fred Drake762e2061999-08-26 17:23:54 +00005009 /* Install the first arg and newstr in posix_putenv_garbage;
5010 * this will cause previous value to be collected. This has to
5011 * happen after the real putenv() call because the old value
5012 * was still accessible until then. */
5013 if (PyDict_SetItem(posix_putenv_garbage,
5014 PyTuple_GET_ITEM(args, 0), newstr)) {
5015 /* really not much we can do; just leak */
5016 PyErr_Clear();
5017 }
5018 else {
5019 Py_DECREF(newstr);
5020 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005021
5022#if defined(PYOS_OS2)
5023 }
5024#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005025 Py_INCREF(Py_None);
5026 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005027}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005028#endif /* putenv */
5029
Guido van Rossumc524d952001-10-19 01:31:59 +00005030#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005031PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005032"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005033Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005034
5035static PyObject *
5036posix_unsetenv(PyObject *self, PyObject *args)
5037{
5038 char *s1;
5039
5040 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5041 return NULL;
5042
5043 unsetenv(s1);
5044
5045 /* Remove the key from posix_putenv_garbage;
5046 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005047 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005048 * old value was still accessible until then.
5049 */
5050 if (PyDict_DelItem(posix_putenv_garbage,
5051 PyTuple_GET_ITEM(args, 0))) {
5052 /* really not much we can do; just leak */
5053 PyErr_Clear();
5054 }
5055
5056 Py_INCREF(Py_None);
5057 return Py_None;
5058}
5059#endif /* unsetenv */
5060
Guido van Rossumb6a47161997-09-15 22:54:34 +00005061#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005062PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005063"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005064Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005065
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005066static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005067posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005068{
5069 int code;
5070 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005071 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005072 return NULL;
5073 message = strerror(code);
5074 if (message == NULL) {
5075 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005076 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005077 return NULL;
5078 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005079 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005080}
5081#endif /* strerror */
5082
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005083
Guido van Rossumc9641791998-08-04 15:26:23 +00005084#ifdef HAVE_SYS_WAIT_H
5085
Fred Drake106c1a02002-04-23 15:58:02 +00005086#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005087PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005088"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005089Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005090
5091static PyObject *
5092posix_WCOREDUMP(PyObject *self, PyObject *args)
5093{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005094 WAIT_TYPE status;
5095 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005096
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005097 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005098 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005099
5100 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005101}
5102#endif /* WCOREDUMP */
5103
5104#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005105PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005106"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005107Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005108job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005109
5110static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005111posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005112{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005113 WAIT_TYPE status;
5114 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005115
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005116 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005117 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005118
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005119 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005120}
5121#endif /* WIFCONTINUED */
5122
Guido van Rossumc9641791998-08-04 15:26:23 +00005123#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005124PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005125"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005126Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005127
5128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005129posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005130{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005131 WAIT_TYPE status;
5132 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005133
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005134 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005135 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005136
Fred Drake106c1a02002-04-23 15:58:02 +00005137 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005138}
5139#endif /* WIFSTOPPED */
5140
5141#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005142PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005143"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005144Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005145
5146static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005147posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005148{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005149 WAIT_TYPE status;
5150 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005152 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005153 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005154
Fred Drake106c1a02002-04-23 15:58:02 +00005155 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005156}
5157#endif /* WIFSIGNALED */
5158
5159#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005160PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005161"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005162Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005163system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005164
5165static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005166posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005167{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005168 WAIT_TYPE status;
5169 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005170
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005171 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005172 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005173
Fred Drake106c1a02002-04-23 15:58:02 +00005174 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005175}
5176#endif /* WIFEXITED */
5177
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005178#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005179PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005180"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005181Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005182
5183static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005184posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005185{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005186 WAIT_TYPE status;
5187 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005188
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005189 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005190 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005191
Guido van Rossumc9641791998-08-04 15:26:23 +00005192 return Py_BuildValue("i", WEXITSTATUS(status));
5193}
5194#endif /* WEXITSTATUS */
5195
5196#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005197PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005198"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005199Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005200value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005201
5202static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005203posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005204{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005205 WAIT_TYPE status;
5206 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005208 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005209 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005210
Guido van Rossumc9641791998-08-04 15:26:23 +00005211 return Py_BuildValue("i", WTERMSIG(status));
5212}
5213#endif /* WTERMSIG */
5214
5215#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005216PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005217"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005218Return the signal that stopped the process that provided\n\
5219the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005220
5221static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005222posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005223{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005224 WAIT_TYPE status;
5225 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005226
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005227 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005228 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005229
Guido van Rossumc9641791998-08-04 15:26:23 +00005230 return Py_BuildValue("i", WSTOPSIG(status));
5231}
5232#endif /* WSTOPSIG */
5233
5234#endif /* HAVE_SYS_WAIT_H */
5235
5236
Thomas Wouters477c8d52006-05-27 19:21:47 +00005237#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005238#ifdef _SCO_DS
5239/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5240 needed definitions in sys/statvfs.h */
5241#define _SVID3
5242#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005243#include <sys/statvfs.h>
5244
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005245static PyObject*
5246_pystatvfs_fromstructstatvfs(struct statvfs st) {
5247 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5248 if (v == NULL)
5249 return NULL;
5250
5251#if !defined(HAVE_LARGEFILE_SUPPORT)
5252 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5253 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5254 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5255 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5256 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5257 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5258 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5259 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5260 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5261 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5262#else
5263 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5264 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005265 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005266 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005267 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005268 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005269 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005270 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005271 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005272 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005273 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005274 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005275 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005276 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005277 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5278 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5279#endif
5280
5281 return v;
5282}
5283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005284PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005285"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005286Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005287
5288static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005289posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005290{
5291 int fd, res;
5292 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005293
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005294 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005295 return NULL;
5296 Py_BEGIN_ALLOW_THREADS
5297 res = fstatvfs(fd, &st);
5298 Py_END_ALLOW_THREADS
5299 if (res != 0)
5300 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005301
5302 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005303}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005304#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005305
5306
Thomas Wouters477c8d52006-05-27 19:21:47 +00005307#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005308#include <sys/statvfs.h>
5309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005310PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005311"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005312Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005313
5314static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005315posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005316{
5317 char *path;
5318 int res;
5319 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005320 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005321 return NULL;
5322 Py_BEGIN_ALLOW_THREADS
5323 res = statvfs(path, &st);
5324 Py_END_ALLOW_THREADS
5325 if (res != 0)
5326 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005327
5328 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005329}
5330#endif /* HAVE_STATVFS */
5331
5332
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005333#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005334PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005335"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005336Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005337The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005338or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005339
5340static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005341posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005342{
5343 PyObject *result = NULL;
5344 char *dir = NULL;
5345 char *pfx = NULL;
5346 char *name;
5347
5348 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5349 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005350
Skip Montanaro46fc3372007-08-12 11:44:53 +00005351 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5352 "tempnam is a potential security risk to your program",
5353 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005354 return NULL;
5355
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005356#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005357 name = _tempnam(dir, pfx);
5358#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005359 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005360#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005361 if (name == NULL)
5362 return PyErr_NoMemory();
5363 result = PyString_FromString(name);
5364 free(name);
5365 return result;
5366}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005367#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005368
5369
5370#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005371PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005372"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005373Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005374
5375static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005376posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005377{
5378 FILE *fp;
5379
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005380 fp = tmpfile();
5381 if (fp == NULL)
5382 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005383 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005384}
5385#endif
5386
5387
5388#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005389PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005390"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005391Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005392
5393static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005394posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005395{
5396 char buffer[L_tmpnam];
5397 char *name;
5398
Skip Montanaro46fc3372007-08-12 11:44:53 +00005399 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5400 "tmpnam is a potential security risk to your program",
5401 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005402 return NULL;
5403
Greg Wardb48bc172000-03-01 21:51:56 +00005404#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005405 name = tmpnam_r(buffer);
5406#else
5407 name = tmpnam(buffer);
5408#endif
5409 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005410 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005411#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005412 "unexpected NULL from tmpnam_r"
5413#else
5414 "unexpected NULL from tmpnam"
5415#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005416 );
5417 PyErr_SetObject(PyExc_OSError, err);
5418 Py_XDECREF(err);
5419 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005420 }
5421 return PyString_FromString(buffer);
5422}
5423#endif
5424
5425
Fred Drakec9680921999-12-13 16:37:25 +00005426/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5427 * It maps strings representing configuration variable names to
5428 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005429 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005430 * rarely-used constants. There are three separate tables that use
5431 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005432 *
5433 * This code is always included, even if none of the interfaces that
5434 * need it are included. The #if hackery needed to avoid it would be
5435 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005436 */
5437struct constdef {
5438 char *name;
5439 long value;
5440};
5441
Fred Drake12c6e2d1999-12-14 21:25:03 +00005442static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005443conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005444 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005445{
5446 if (PyInt_Check(arg)) {
5447 *valuep = PyInt_AS_LONG(arg);
5448 return 1;
5449 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005450 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005451 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005452 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005453 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005454 size_t hi = tablesize;
5455 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005456 const char *confname;
5457 Py_ssize_t namelen;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005458 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005459 PyErr_SetString(PyExc_TypeError,
5460 "configuration names must be strings or integers");
5461 return 0;
5462 }
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005463 confname = PyUnicode_AsString(arg);
5464 if (confname == NULL)
5465 return 0;
5466 namelen = strlen(confname);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005467 while (lo < hi) {
5468 mid = (lo + hi) / 2;
5469 cmp = strcmp(confname, table[mid].name);
5470 if (cmp < 0)
5471 hi = mid;
5472 else if (cmp > 0)
5473 lo = mid + 1;
5474 else {
5475 *valuep = table[mid].value;
5476 return 1;
5477 }
5478 }
5479 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005480 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005481 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005482}
5483
5484
5485#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5486static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005487#ifdef _PC_ABI_AIO_XFER_MAX
5488 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5489#endif
5490#ifdef _PC_ABI_ASYNC_IO
5491 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5492#endif
Fred Drakec9680921999-12-13 16:37:25 +00005493#ifdef _PC_ASYNC_IO
5494 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5495#endif
5496#ifdef _PC_CHOWN_RESTRICTED
5497 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5498#endif
5499#ifdef _PC_FILESIZEBITS
5500 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5501#endif
5502#ifdef _PC_LAST
5503 {"PC_LAST", _PC_LAST},
5504#endif
5505#ifdef _PC_LINK_MAX
5506 {"PC_LINK_MAX", _PC_LINK_MAX},
5507#endif
5508#ifdef _PC_MAX_CANON
5509 {"PC_MAX_CANON", _PC_MAX_CANON},
5510#endif
5511#ifdef _PC_MAX_INPUT
5512 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5513#endif
5514#ifdef _PC_NAME_MAX
5515 {"PC_NAME_MAX", _PC_NAME_MAX},
5516#endif
5517#ifdef _PC_NO_TRUNC
5518 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5519#endif
5520#ifdef _PC_PATH_MAX
5521 {"PC_PATH_MAX", _PC_PATH_MAX},
5522#endif
5523#ifdef _PC_PIPE_BUF
5524 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5525#endif
5526#ifdef _PC_PRIO_IO
5527 {"PC_PRIO_IO", _PC_PRIO_IO},
5528#endif
5529#ifdef _PC_SOCK_MAXBUF
5530 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5531#endif
5532#ifdef _PC_SYNC_IO
5533 {"PC_SYNC_IO", _PC_SYNC_IO},
5534#endif
5535#ifdef _PC_VDISABLE
5536 {"PC_VDISABLE", _PC_VDISABLE},
5537#endif
5538};
5539
Fred Drakec9680921999-12-13 16:37:25 +00005540static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005541conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005542{
5543 return conv_confname(arg, valuep, posix_constants_pathconf,
5544 sizeof(posix_constants_pathconf)
5545 / sizeof(struct constdef));
5546}
5547#endif
5548
5549#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005550PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005551"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005552Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005553If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005554
5555static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005556posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005557{
5558 PyObject *result = NULL;
5559 int name, fd;
5560
Fred Drake12c6e2d1999-12-14 21:25:03 +00005561 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5562 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005563 long limit;
5564
5565 errno = 0;
5566 limit = fpathconf(fd, name);
5567 if (limit == -1 && errno != 0)
5568 posix_error();
5569 else
5570 result = PyInt_FromLong(limit);
5571 }
5572 return result;
5573}
5574#endif
5575
5576
5577#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005579"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005580Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005581If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005582
5583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005584posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005585{
5586 PyObject *result = NULL;
5587 int name;
5588 char *path;
5589
5590 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5591 conv_path_confname, &name)) {
5592 long limit;
5593
5594 errno = 0;
5595 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005596 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005597 if (errno == EINVAL)
5598 /* could be a path or name problem */
5599 posix_error();
5600 else
5601 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005602 }
Fred Drakec9680921999-12-13 16:37:25 +00005603 else
5604 result = PyInt_FromLong(limit);
5605 }
5606 return result;
5607}
5608#endif
5609
5610#ifdef HAVE_CONFSTR
5611static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005612#ifdef _CS_ARCHITECTURE
5613 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5614#endif
5615#ifdef _CS_HOSTNAME
5616 {"CS_HOSTNAME", _CS_HOSTNAME},
5617#endif
5618#ifdef _CS_HW_PROVIDER
5619 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5620#endif
5621#ifdef _CS_HW_SERIAL
5622 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5623#endif
5624#ifdef _CS_INITTAB_NAME
5625 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5626#endif
Fred Drakec9680921999-12-13 16:37:25 +00005627#ifdef _CS_LFS64_CFLAGS
5628 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5629#endif
5630#ifdef _CS_LFS64_LDFLAGS
5631 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5632#endif
5633#ifdef _CS_LFS64_LIBS
5634 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5635#endif
5636#ifdef _CS_LFS64_LINTFLAGS
5637 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5638#endif
5639#ifdef _CS_LFS_CFLAGS
5640 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5641#endif
5642#ifdef _CS_LFS_LDFLAGS
5643 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5644#endif
5645#ifdef _CS_LFS_LIBS
5646 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5647#endif
5648#ifdef _CS_LFS_LINTFLAGS
5649 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5650#endif
Fred Draked86ed291999-12-15 15:34:33 +00005651#ifdef _CS_MACHINE
5652 {"CS_MACHINE", _CS_MACHINE},
5653#endif
Fred Drakec9680921999-12-13 16:37:25 +00005654#ifdef _CS_PATH
5655 {"CS_PATH", _CS_PATH},
5656#endif
Fred Draked86ed291999-12-15 15:34:33 +00005657#ifdef _CS_RELEASE
5658 {"CS_RELEASE", _CS_RELEASE},
5659#endif
5660#ifdef _CS_SRPC_DOMAIN
5661 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5662#endif
5663#ifdef _CS_SYSNAME
5664 {"CS_SYSNAME", _CS_SYSNAME},
5665#endif
5666#ifdef _CS_VERSION
5667 {"CS_VERSION", _CS_VERSION},
5668#endif
Fred Drakec9680921999-12-13 16:37:25 +00005669#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5670 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5671#endif
5672#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5673 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5674#endif
5675#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5676 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5677#endif
5678#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5679 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5680#endif
5681#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5682 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5683#endif
5684#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5685 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5686#endif
5687#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5688 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5689#endif
5690#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5691 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5692#endif
5693#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5694 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5695#endif
5696#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5697 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5698#endif
5699#ifdef _CS_XBS5_LP64_OFF64_LIBS
5700 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5701#endif
5702#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5703 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5704#endif
5705#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5706 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5707#endif
5708#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5709 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5710#endif
5711#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5712 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5713#endif
5714#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5715 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5716#endif
Fred Draked86ed291999-12-15 15:34:33 +00005717#ifdef _MIPS_CS_AVAIL_PROCESSORS
5718 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5719#endif
5720#ifdef _MIPS_CS_BASE
5721 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5722#endif
5723#ifdef _MIPS_CS_HOSTID
5724 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5725#endif
5726#ifdef _MIPS_CS_HW_NAME
5727 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5728#endif
5729#ifdef _MIPS_CS_NUM_PROCESSORS
5730 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5731#endif
5732#ifdef _MIPS_CS_OSREL_MAJ
5733 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5734#endif
5735#ifdef _MIPS_CS_OSREL_MIN
5736 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5737#endif
5738#ifdef _MIPS_CS_OSREL_PATCH
5739 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5740#endif
5741#ifdef _MIPS_CS_OS_NAME
5742 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5743#endif
5744#ifdef _MIPS_CS_OS_PROVIDER
5745 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5746#endif
5747#ifdef _MIPS_CS_PROCESSORS
5748 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5749#endif
5750#ifdef _MIPS_CS_SERIAL
5751 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5752#endif
5753#ifdef _MIPS_CS_VENDOR
5754 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5755#endif
Fred Drakec9680921999-12-13 16:37:25 +00005756};
5757
5758static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005759conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005760{
5761 return conv_confname(arg, valuep, posix_constants_confstr,
5762 sizeof(posix_constants_confstr)
5763 / sizeof(struct constdef));
5764}
5765
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005766PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005767"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005768Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005769
5770static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005771posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005772{
5773 PyObject *result = NULL;
5774 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005775 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005776
5777 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005778 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005779
Fred Drakec9680921999-12-13 16:37:25 +00005780 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005781 len = confstr(name, buffer, sizeof(buffer));
5782 if (len == 0) {
5783 if (errno) {
5784 posix_error();
5785 }
5786 else {
5787 result = Py_None;
5788 Py_INCREF(Py_None);
5789 }
Fred Drakec9680921999-12-13 16:37:25 +00005790 }
5791 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005792 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005793 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005794 if (result != NULL)
Neal Norwitz93c56822007-08-26 07:10:06 +00005795 confstr(name, PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005796 }
5797 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005798 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005799 }
5800 }
5801 return result;
5802}
5803#endif
5804
5805
5806#ifdef HAVE_SYSCONF
5807static struct constdef posix_constants_sysconf[] = {
5808#ifdef _SC_2_CHAR_TERM
5809 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5810#endif
5811#ifdef _SC_2_C_BIND
5812 {"SC_2_C_BIND", _SC_2_C_BIND},
5813#endif
5814#ifdef _SC_2_C_DEV
5815 {"SC_2_C_DEV", _SC_2_C_DEV},
5816#endif
5817#ifdef _SC_2_C_VERSION
5818 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5819#endif
5820#ifdef _SC_2_FORT_DEV
5821 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5822#endif
5823#ifdef _SC_2_FORT_RUN
5824 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5825#endif
5826#ifdef _SC_2_LOCALEDEF
5827 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5828#endif
5829#ifdef _SC_2_SW_DEV
5830 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5831#endif
5832#ifdef _SC_2_UPE
5833 {"SC_2_UPE", _SC_2_UPE},
5834#endif
5835#ifdef _SC_2_VERSION
5836 {"SC_2_VERSION", _SC_2_VERSION},
5837#endif
Fred Draked86ed291999-12-15 15:34:33 +00005838#ifdef _SC_ABI_ASYNCHRONOUS_IO
5839 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5840#endif
5841#ifdef _SC_ACL
5842 {"SC_ACL", _SC_ACL},
5843#endif
Fred Drakec9680921999-12-13 16:37:25 +00005844#ifdef _SC_AIO_LISTIO_MAX
5845 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5846#endif
Fred Drakec9680921999-12-13 16:37:25 +00005847#ifdef _SC_AIO_MAX
5848 {"SC_AIO_MAX", _SC_AIO_MAX},
5849#endif
5850#ifdef _SC_AIO_PRIO_DELTA_MAX
5851 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5852#endif
5853#ifdef _SC_ARG_MAX
5854 {"SC_ARG_MAX", _SC_ARG_MAX},
5855#endif
5856#ifdef _SC_ASYNCHRONOUS_IO
5857 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5858#endif
5859#ifdef _SC_ATEXIT_MAX
5860 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5861#endif
Fred Draked86ed291999-12-15 15:34:33 +00005862#ifdef _SC_AUDIT
5863 {"SC_AUDIT", _SC_AUDIT},
5864#endif
Fred Drakec9680921999-12-13 16:37:25 +00005865#ifdef _SC_AVPHYS_PAGES
5866 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5867#endif
5868#ifdef _SC_BC_BASE_MAX
5869 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5870#endif
5871#ifdef _SC_BC_DIM_MAX
5872 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5873#endif
5874#ifdef _SC_BC_SCALE_MAX
5875 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5876#endif
5877#ifdef _SC_BC_STRING_MAX
5878 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5879#endif
Fred Draked86ed291999-12-15 15:34:33 +00005880#ifdef _SC_CAP
5881 {"SC_CAP", _SC_CAP},
5882#endif
Fred Drakec9680921999-12-13 16:37:25 +00005883#ifdef _SC_CHARCLASS_NAME_MAX
5884 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5885#endif
5886#ifdef _SC_CHAR_BIT
5887 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5888#endif
5889#ifdef _SC_CHAR_MAX
5890 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5891#endif
5892#ifdef _SC_CHAR_MIN
5893 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5894#endif
5895#ifdef _SC_CHILD_MAX
5896 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5897#endif
5898#ifdef _SC_CLK_TCK
5899 {"SC_CLK_TCK", _SC_CLK_TCK},
5900#endif
5901#ifdef _SC_COHER_BLKSZ
5902 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5903#endif
5904#ifdef _SC_COLL_WEIGHTS_MAX
5905 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5906#endif
5907#ifdef _SC_DCACHE_ASSOC
5908 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5909#endif
5910#ifdef _SC_DCACHE_BLKSZ
5911 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5912#endif
5913#ifdef _SC_DCACHE_LINESZ
5914 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
5915#endif
5916#ifdef _SC_DCACHE_SZ
5917 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
5918#endif
5919#ifdef _SC_DCACHE_TBLKSZ
5920 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
5921#endif
5922#ifdef _SC_DELAYTIMER_MAX
5923 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
5924#endif
5925#ifdef _SC_EQUIV_CLASS_MAX
5926 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
5927#endif
5928#ifdef _SC_EXPR_NEST_MAX
5929 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
5930#endif
5931#ifdef _SC_FSYNC
5932 {"SC_FSYNC", _SC_FSYNC},
5933#endif
5934#ifdef _SC_GETGR_R_SIZE_MAX
5935 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
5936#endif
5937#ifdef _SC_GETPW_R_SIZE_MAX
5938 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
5939#endif
5940#ifdef _SC_ICACHE_ASSOC
5941 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
5942#endif
5943#ifdef _SC_ICACHE_BLKSZ
5944 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
5945#endif
5946#ifdef _SC_ICACHE_LINESZ
5947 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
5948#endif
5949#ifdef _SC_ICACHE_SZ
5950 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
5951#endif
Fred Draked86ed291999-12-15 15:34:33 +00005952#ifdef _SC_INF
5953 {"SC_INF", _SC_INF},
5954#endif
Fred Drakec9680921999-12-13 16:37:25 +00005955#ifdef _SC_INT_MAX
5956 {"SC_INT_MAX", _SC_INT_MAX},
5957#endif
5958#ifdef _SC_INT_MIN
5959 {"SC_INT_MIN", _SC_INT_MIN},
5960#endif
5961#ifdef _SC_IOV_MAX
5962 {"SC_IOV_MAX", _SC_IOV_MAX},
5963#endif
Fred Draked86ed291999-12-15 15:34:33 +00005964#ifdef _SC_IP_SECOPTS
5965 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
5966#endif
Fred Drakec9680921999-12-13 16:37:25 +00005967#ifdef _SC_JOB_CONTROL
5968 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
5969#endif
Fred Draked86ed291999-12-15 15:34:33 +00005970#ifdef _SC_KERN_POINTERS
5971 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
5972#endif
5973#ifdef _SC_KERN_SIM
5974 {"SC_KERN_SIM", _SC_KERN_SIM},
5975#endif
Fred Drakec9680921999-12-13 16:37:25 +00005976#ifdef _SC_LINE_MAX
5977 {"SC_LINE_MAX", _SC_LINE_MAX},
5978#endif
5979#ifdef _SC_LOGIN_NAME_MAX
5980 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
5981#endif
5982#ifdef _SC_LOGNAME_MAX
5983 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
5984#endif
5985#ifdef _SC_LONG_BIT
5986 {"SC_LONG_BIT", _SC_LONG_BIT},
5987#endif
Fred Draked86ed291999-12-15 15:34:33 +00005988#ifdef _SC_MAC
5989 {"SC_MAC", _SC_MAC},
5990#endif
Fred Drakec9680921999-12-13 16:37:25 +00005991#ifdef _SC_MAPPED_FILES
5992 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
5993#endif
5994#ifdef _SC_MAXPID
5995 {"SC_MAXPID", _SC_MAXPID},
5996#endif
5997#ifdef _SC_MB_LEN_MAX
5998 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
5999#endif
6000#ifdef _SC_MEMLOCK
6001 {"SC_MEMLOCK", _SC_MEMLOCK},
6002#endif
6003#ifdef _SC_MEMLOCK_RANGE
6004 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6005#endif
6006#ifdef _SC_MEMORY_PROTECTION
6007 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6008#endif
6009#ifdef _SC_MESSAGE_PASSING
6010 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6011#endif
Fred Draked86ed291999-12-15 15:34:33 +00006012#ifdef _SC_MMAP_FIXED_ALIGNMENT
6013 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6014#endif
Fred Drakec9680921999-12-13 16:37:25 +00006015#ifdef _SC_MQ_OPEN_MAX
6016 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6017#endif
6018#ifdef _SC_MQ_PRIO_MAX
6019 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6020#endif
Fred Draked86ed291999-12-15 15:34:33 +00006021#ifdef _SC_NACLS_MAX
6022 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6023#endif
Fred Drakec9680921999-12-13 16:37:25 +00006024#ifdef _SC_NGROUPS_MAX
6025 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6026#endif
6027#ifdef _SC_NL_ARGMAX
6028 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6029#endif
6030#ifdef _SC_NL_LANGMAX
6031 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6032#endif
6033#ifdef _SC_NL_MSGMAX
6034 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6035#endif
6036#ifdef _SC_NL_NMAX
6037 {"SC_NL_NMAX", _SC_NL_NMAX},
6038#endif
6039#ifdef _SC_NL_SETMAX
6040 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6041#endif
6042#ifdef _SC_NL_TEXTMAX
6043 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6044#endif
6045#ifdef _SC_NPROCESSORS_CONF
6046 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6047#endif
6048#ifdef _SC_NPROCESSORS_ONLN
6049 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6050#endif
Fred Draked86ed291999-12-15 15:34:33 +00006051#ifdef _SC_NPROC_CONF
6052 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6053#endif
6054#ifdef _SC_NPROC_ONLN
6055 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6056#endif
Fred Drakec9680921999-12-13 16:37:25 +00006057#ifdef _SC_NZERO
6058 {"SC_NZERO", _SC_NZERO},
6059#endif
6060#ifdef _SC_OPEN_MAX
6061 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6062#endif
6063#ifdef _SC_PAGESIZE
6064 {"SC_PAGESIZE", _SC_PAGESIZE},
6065#endif
6066#ifdef _SC_PAGE_SIZE
6067 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6068#endif
6069#ifdef _SC_PASS_MAX
6070 {"SC_PASS_MAX", _SC_PASS_MAX},
6071#endif
6072#ifdef _SC_PHYS_PAGES
6073 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6074#endif
6075#ifdef _SC_PII
6076 {"SC_PII", _SC_PII},
6077#endif
6078#ifdef _SC_PII_INTERNET
6079 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6080#endif
6081#ifdef _SC_PII_INTERNET_DGRAM
6082 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6083#endif
6084#ifdef _SC_PII_INTERNET_STREAM
6085 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6086#endif
6087#ifdef _SC_PII_OSI
6088 {"SC_PII_OSI", _SC_PII_OSI},
6089#endif
6090#ifdef _SC_PII_OSI_CLTS
6091 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6092#endif
6093#ifdef _SC_PII_OSI_COTS
6094 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6095#endif
6096#ifdef _SC_PII_OSI_M
6097 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6098#endif
6099#ifdef _SC_PII_SOCKET
6100 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6101#endif
6102#ifdef _SC_PII_XTI
6103 {"SC_PII_XTI", _SC_PII_XTI},
6104#endif
6105#ifdef _SC_POLL
6106 {"SC_POLL", _SC_POLL},
6107#endif
6108#ifdef _SC_PRIORITIZED_IO
6109 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6110#endif
6111#ifdef _SC_PRIORITY_SCHEDULING
6112 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6113#endif
6114#ifdef _SC_REALTIME_SIGNALS
6115 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6116#endif
6117#ifdef _SC_RE_DUP_MAX
6118 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6119#endif
6120#ifdef _SC_RTSIG_MAX
6121 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6122#endif
6123#ifdef _SC_SAVED_IDS
6124 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6125#endif
6126#ifdef _SC_SCHAR_MAX
6127 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6128#endif
6129#ifdef _SC_SCHAR_MIN
6130 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6131#endif
6132#ifdef _SC_SELECT
6133 {"SC_SELECT", _SC_SELECT},
6134#endif
6135#ifdef _SC_SEMAPHORES
6136 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6137#endif
6138#ifdef _SC_SEM_NSEMS_MAX
6139 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6140#endif
6141#ifdef _SC_SEM_VALUE_MAX
6142 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6143#endif
6144#ifdef _SC_SHARED_MEMORY_OBJECTS
6145 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6146#endif
6147#ifdef _SC_SHRT_MAX
6148 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6149#endif
6150#ifdef _SC_SHRT_MIN
6151 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6152#endif
6153#ifdef _SC_SIGQUEUE_MAX
6154 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6155#endif
6156#ifdef _SC_SIGRT_MAX
6157 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6158#endif
6159#ifdef _SC_SIGRT_MIN
6160 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6161#endif
Fred Draked86ed291999-12-15 15:34:33 +00006162#ifdef _SC_SOFTPOWER
6163 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6164#endif
Fred Drakec9680921999-12-13 16:37:25 +00006165#ifdef _SC_SPLIT_CACHE
6166 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6167#endif
6168#ifdef _SC_SSIZE_MAX
6169 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6170#endif
6171#ifdef _SC_STACK_PROT
6172 {"SC_STACK_PROT", _SC_STACK_PROT},
6173#endif
6174#ifdef _SC_STREAM_MAX
6175 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6176#endif
6177#ifdef _SC_SYNCHRONIZED_IO
6178 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6179#endif
6180#ifdef _SC_THREADS
6181 {"SC_THREADS", _SC_THREADS},
6182#endif
6183#ifdef _SC_THREAD_ATTR_STACKADDR
6184 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6185#endif
6186#ifdef _SC_THREAD_ATTR_STACKSIZE
6187 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6188#endif
6189#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6190 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6191#endif
6192#ifdef _SC_THREAD_KEYS_MAX
6193 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6194#endif
6195#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6196 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6197#endif
6198#ifdef _SC_THREAD_PRIO_INHERIT
6199 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6200#endif
6201#ifdef _SC_THREAD_PRIO_PROTECT
6202 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6203#endif
6204#ifdef _SC_THREAD_PROCESS_SHARED
6205 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6206#endif
6207#ifdef _SC_THREAD_SAFE_FUNCTIONS
6208 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6209#endif
6210#ifdef _SC_THREAD_STACK_MIN
6211 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6212#endif
6213#ifdef _SC_THREAD_THREADS_MAX
6214 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6215#endif
6216#ifdef _SC_TIMERS
6217 {"SC_TIMERS", _SC_TIMERS},
6218#endif
6219#ifdef _SC_TIMER_MAX
6220 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6221#endif
6222#ifdef _SC_TTY_NAME_MAX
6223 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6224#endif
6225#ifdef _SC_TZNAME_MAX
6226 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6227#endif
6228#ifdef _SC_T_IOV_MAX
6229 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6230#endif
6231#ifdef _SC_UCHAR_MAX
6232 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6233#endif
6234#ifdef _SC_UINT_MAX
6235 {"SC_UINT_MAX", _SC_UINT_MAX},
6236#endif
6237#ifdef _SC_UIO_MAXIOV
6238 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6239#endif
6240#ifdef _SC_ULONG_MAX
6241 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6242#endif
6243#ifdef _SC_USHRT_MAX
6244 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6245#endif
6246#ifdef _SC_VERSION
6247 {"SC_VERSION", _SC_VERSION},
6248#endif
6249#ifdef _SC_WORD_BIT
6250 {"SC_WORD_BIT", _SC_WORD_BIT},
6251#endif
6252#ifdef _SC_XBS5_ILP32_OFF32
6253 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6254#endif
6255#ifdef _SC_XBS5_ILP32_OFFBIG
6256 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6257#endif
6258#ifdef _SC_XBS5_LP64_OFF64
6259 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6260#endif
6261#ifdef _SC_XBS5_LPBIG_OFFBIG
6262 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6263#endif
6264#ifdef _SC_XOPEN_CRYPT
6265 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6266#endif
6267#ifdef _SC_XOPEN_ENH_I18N
6268 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6269#endif
6270#ifdef _SC_XOPEN_LEGACY
6271 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6272#endif
6273#ifdef _SC_XOPEN_REALTIME
6274 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6275#endif
6276#ifdef _SC_XOPEN_REALTIME_THREADS
6277 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6278#endif
6279#ifdef _SC_XOPEN_SHM
6280 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6281#endif
6282#ifdef _SC_XOPEN_UNIX
6283 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6284#endif
6285#ifdef _SC_XOPEN_VERSION
6286 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6287#endif
6288#ifdef _SC_XOPEN_XCU_VERSION
6289 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6290#endif
6291#ifdef _SC_XOPEN_XPG2
6292 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6293#endif
6294#ifdef _SC_XOPEN_XPG3
6295 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6296#endif
6297#ifdef _SC_XOPEN_XPG4
6298 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6299#endif
6300};
6301
6302static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006303conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006304{
6305 return conv_confname(arg, valuep, posix_constants_sysconf,
6306 sizeof(posix_constants_sysconf)
6307 / sizeof(struct constdef));
6308}
6309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006310PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006311"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006312Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006313
6314static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006315posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006316{
6317 PyObject *result = NULL;
6318 int name;
6319
6320 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6321 int value;
6322
6323 errno = 0;
6324 value = sysconf(name);
6325 if (value == -1 && errno != 0)
6326 posix_error();
6327 else
6328 result = PyInt_FromLong(value);
6329 }
6330 return result;
6331}
6332#endif
6333
6334
Fred Drakebec628d1999-12-15 18:31:10 +00006335/* This code is used to ensure that the tables of configuration value names
6336 * are in sorted order as required by conv_confname(), and also to build the
6337 * the exported dictionaries that are used to publish information about the
6338 * names available on the host platform.
6339 *
6340 * Sorting the table at runtime ensures that the table is properly ordered
6341 * when used, even for platforms we're not able to test on. It also makes
6342 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006343 */
Fred Drakebec628d1999-12-15 18:31:10 +00006344
6345static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006346cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006347{
6348 const struct constdef *c1 =
6349 (const struct constdef *) v1;
6350 const struct constdef *c2 =
6351 (const struct constdef *) v2;
6352
6353 return strcmp(c1->name, c2->name);
6354}
6355
6356static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006357setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006358 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006359{
Fred Drakebec628d1999-12-15 18:31:10 +00006360 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006361 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006362
6363 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6364 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006365 if (d == NULL)
6366 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006367
Barry Warsaw3155db32000-04-13 15:20:40 +00006368 for (i=0; i < tablesize; ++i) {
6369 PyObject *o = PyInt_FromLong(table[i].value);
6370 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6371 Py_XDECREF(o);
6372 Py_DECREF(d);
6373 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006374 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006375 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006376 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006377 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006378}
6379
Fred Drakebec628d1999-12-15 18:31:10 +00006380/* Return -1 on failure, 0 on success. */
6381static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006382setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006383{
6384#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006385 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006386 sizeof(posix_constants_pathconf)
6387 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006388 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006389 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006390#endif
6391#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006392 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006393 sizeof(posix_constants_confstr)
6394 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006395 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006396 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006397#endif
6398#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006399 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006400 sizeof(posix_constants_sysconf)
6401 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006402 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006403 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006404#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006405 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006406}
Fred Draked86ed291999-12-15 15:34:33 +00006407
6408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006409PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006410"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006411Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006412in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006413
6414static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006415posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006416{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006417 abort();
6418 /*NOTREACHED*/
6419 Py_FatalError("abort() called from Python code didn't abort!");
6420 return NULL;
6421}
Fred Drakebec628d1999-12-15 18:31:10 +00006422
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006423#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006424PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006425"startfile(filepath [, operation]) - Start a file with its associated\n\
6426application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006427\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006428When \"operation\" is not specified or \"open\", this acts like\n\
6429double-clicking the file in Explorer, or giving the file name as an\n\
6430argument to the DOS \"start\" command: the file is opened with whatever\n\
6431application (if any) its extension is associated.\n\
6432When another \"operation\" is given, it specifies what should be done with\n\
6433the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006434\n\
6435startfile returns as soon as the associated application is launched.\n\
6436There is no option to wait for the application to close, and no way\n\
6437to retrieve the application's exit status.\n\
6438\n\
6439The filepath is relative to the current directory. If you want to use\n\
6440an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006441the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006442
6443static PyObject *
6444win32_startfile(PyObject *self, PyObject *args)
6445{
6446 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006447 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006448 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006449#ifdef Py_WIN_WIDE_FILENAMES
6450 if (unicode_file_names()) {
6451 PyObject *unipath, *woperation = NULL;
6452 if (!PyArg_ParseTuple(args, "U|s:startfile",
6453 &unipath, &operation)) {
6454 PyErr_Clear();
6455 goto normal;
6456 }
6457
6458
6459 if (operation) {
6460 woperation = PyUnicode_DecodeASCII(operation,
6461 strlen(operation), NULL);
6462 if (!woperation) {
6463 PyErr_Clear();
6464 operation = NULL;
6465 goto normal;
6466 }
6467 }
6468
6469 Py_BEGIN_ALLOW_THREADS
6470 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6471 PyUnicode_AS_UNICODE(unipath),
6472 NULL, NULL, SW_SHOWNORMAL);
6473 Py_END_ALLOW_THREADS
6474
6475 Py_XDECREF(woperation);
6476 if (rc <= (HINSTANCE)32) {
6477 PyObject *errval = win32_error_unicode("startfile",
6478 PyUnicode_AS_UNICODE(unipath));
6479 return errval;
6480 }
6481 Py_INCREF(Py_None);
6482 return Py_None;
6483 }
6484#endif
6485
6486normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006487 if (!PyArg_ParseTuple(args, "et|s:startfile",
6488 Py_FileSystemDefaultEncoding, &filepath,
6489 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006490 return NULL;
6491 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006492 rc = ShellExecute((HWND)0, operation, filepath,
6493 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006494 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006495 if (rc <= (HINSTANCE)32) {
6496 PyObject *errval = win32_error("startfile", filepath);
6497 PyMem_Free(filepath);
6498 return errval;
6499 }
6500 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006501 Py_INCREF(Py_None);
6502 return Py_None;
6503}
6504#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006505
Martin v. Löwis438b5342002-12-27 10:16:42 +00006506#ifdef HAVE_GETLOADAVG
6507PyDoc_STRVAR(posix_getloadavg__doc__,
6508"getloadavg() -> (float, float, float)\n\n\
6509Return the number of processes in the system run queue averaged over\n\
6510the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6511was unobtainable");
6512
6513static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006514posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006515{
6516 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006517 if (getloadavg(loadavg, 3)!=3) {
6518 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6519 return NULL;
6520 } else
6521 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6522}
6523#endif
6524
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006525#ifdef MS_WINDOWS
6526
6527PyDoc_STRVAR(win32_urandom__doc__,
6528"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006529Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006530
6531typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6532 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6533 DWORD dwFlags );
6534typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6535 BYTE *pbBuffer );
6536
6537static CRYPTGENRANDOM pCryptGenRandom = NULL;
6538static HCRYPTPROV hCryptProv = 0;
6539
Tim Peters4ad82172004-08-30 17:02:04 +00006540static PyObject*
6541win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006542{
Tim Petersd3115382004-08-30 17:36:46 +00006543 int howMany;
6544 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006545
Tim Peters4ad82172004-08-30 17:02:04 +00006546 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006547 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006548 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006549 if (howMany < 0)
6550 return PyErr_Format(PyExc_ValueError,
6551 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006552
Tim Peters4ad82172004-08-30 17:02:04 +00006553 if (hCryptProv == 0) {
6554 HINSTANCE hAdvAPI32 = NULL;
6555 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006556
Tim Peters4ad82172004-08-30 17:02:04 +00006557 /* Obtain handle to the DLL containing CryptoAPI
6558 This should not fail */
6559 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6560 if(hAdvAPI32 == NULL)
6561 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006562
Tim Peters4ad82172004-08-30 17:02:04 +00006563 /* Obtain pointers to the CryptoAPI functions
6564 This will fail on some early versions of Win95 */
6565 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6566 hAdvAPI32,
6567 "CryptAcquireContextA");
6568 if (pCryptAcquireContext == NULL)
6569 return PyErr_Format(PyExc_NotImplementedError,
6570 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006571
Tim Peters4ad82172004-08-30 17:02:04 +00006572 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6573 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006574 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006575 return PyErr_Format(PyExc_NotImplementedError,
6576 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006577
Tim Peters4ad82172004-08-30 17:02:04 +00006578 /* Acquire context */
6579 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6580 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6581 return win32_error("CryptAcquireContext", NULL);
6582 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006583
Tim Peters4ad82172004-08-30 17:02:04 +00006584 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006585 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006586 if (result != NULL) {
6587 /* Get random data */
6588 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006589 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006590 Py_DECREF(result);
6591 return win32_error("CryptGenRandom", NULL);
6592 }
Tim Peters4ad82172004-08-30 17:02:04 +00006593 }
Tim Petersd3115382004-08-30 17:36:46 +00006594 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006595}
6596#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006597
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006598PyDoc_STRVAR(device_encoding__doc__,
6599"device_encoding(fd) -> str\n\n\
6600Return a string describing the encoding of the device\n\
6601if the output is a terminal; else return None.");
6602
6603static PyObject *
6604device_encoding(PyObject *self, PyObject *args)
6605{
6606 int fd;
6607 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6608 return NULL;
6609 if (!isatty(fd)) {
6610 Py_INCREF(Py_None);
6611 return Py_None;
6612 }
6613#if defined(MS_WINDOWS) || defined(MS_WIN64)
6614 if (fd == 0) {
6615 char buf[100];
6616 sprintf(buf, "cp%d", GetConsoleCP());
6617 return PyUnicode_FromString(buf);
6618 }
6619 if (fd == 1 || fd == 2) {
6620 char buf[100];
6621 sprintf(buf, "cp%d", GetConsoleOutputCP());
6622 return PyUnicode_FromString(buf);
6623 }
6624#elif defined(CODESET)
6625 {
6626 char *codeset = nl_langinfo(CODESET);
6627 if (codeset)
6628 return PyUnicode_FromString(codeset);
6629 }
6630#endif
6631 Py_INCREF(Py_None);
6632 return Py_None;
6633}
6634
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006635#ifdef __VMS
6636/* Use openssl random routine */
6637#include <openssl/rand.h>
6638PyDoc_STRVAR(vms_urandom__doc__,
6639"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006640Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006641
6642static PyObject*
6643vms_urandom(PyObject *self, PyObject *args)
6644{
6645 int howMany;
6646 PyObject* result;
6647
6648 /* Read arguments */
6649 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6650 return NULL;
6651 if (howMany < 0)
6652 return PyErr_Format(PyExc_ValueError,
6653 "negative argument not allowed");
6654
6655 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006656 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006657 if (result != NULL) {
6658 /* Get random data */
6659 if (RAND_pseudo_bytes((unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006660 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006661 howMany) < 0) {
6662 Py_DECREF(result);
6663 return PyErr_Format(PyExc_ValueError,
6664 "RAND_pseudo_bytes");
6665 }
6666 }
6667 return result;
6668}
6669#endif
6670
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006671static PyMethodDef posix_methods[] = {
6672 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6673#ifdef HAVE_TTYNAME
6674 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6675#endif
6676 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006677#ifdef HAVE_CHFLAGS
6678 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6679#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006680 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006681#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006682 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006683#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006684#ifdef HAVE_LCHFLAGS
6685 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6686#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006687#ifdef HAVE_LCHOWN
6688 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6689#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006690#ifdef HAVE_CHROOT
6691 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6692#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006693#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006694 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006695#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006696#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006697 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006698 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006699#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006700#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006701 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006702#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006703 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6704 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6705 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006706#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006707 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006708#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006709#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006710 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006711#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006712 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6713 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6714 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006715 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006716#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006717 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006718#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006719#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006720 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006721#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006722 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006723#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006724 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006725#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006726 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6727 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6728 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006729#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006730 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006731#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006732 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006733#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006734 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6735 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006736#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006737#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006738 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6739 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006740#if defined(PYOS_OS2)
6741 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6742 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6743#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006744#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006745#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006746 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006747#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006748#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006749 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006750#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006751#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006752 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006753#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006754#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006755 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006756#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006757#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006758 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006759#endif /* HAVE_GETEGID */
6760#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006761 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006762#endif /* HAVE_GETEUID */
6763#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006764 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006765#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006766#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006767 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006768#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006769 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006770#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006771 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006772#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006773#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006774 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006775#endif /* HAVE_GETPPID */
6776#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006777 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006778#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006779#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006780 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006781#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006782#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006783 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006784#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006785#ifdef HAVE_KILLPG
6786 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6787#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006788#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006789 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006790#endif /* HAVE_PLOCK */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006791#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006792 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006793#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006794#ifdef HAVE_SETEUID
6795 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6796#endif /* HAVE_SETEUID */
6797#ifdef HAVE_SETEGID
6798 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6799#endif /* HAVE_SETEGID */
6800#ifdef HAVE_SETREUID
6801 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6802#endif /* HAVE_SETREUID */
6803#ifdef HAVE_SETREGID
6804 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6805#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006806#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006807 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006808#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006809#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006810 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006811#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006812#ifdef HAVE_GETPGID
6813 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6814#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006815#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006816 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006817#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006818#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006819 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006820#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006821#ifdef HAVE_WAIT3
6822 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6823#endif /* HAVE_WAIT3 */
6824#ifdef HAVE_WAIT4
6825 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6826#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006827#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006828 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006829#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006830#ifdef HAVE_GETSID
6831 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6832#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006833#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006834 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006835#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006836#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006837 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006838#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006839#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006840 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006841#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006842#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006843 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006844#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006845 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6846 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006847 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006848 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6849 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6850 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6851 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6852 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6853 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006854 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006855#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006856 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006857#endif
6858#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006859 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006860#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006861#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006862 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6863#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006864#ifdef HAVE_DEVICE_MACROS
6865 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6866 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6867 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6868#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006869#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006870 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006871#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006872#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006873 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006874#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006875#ifdef HAVE_UNSETENV
6876 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6877#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00006878#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006879 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00006880#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00006881#ifdef HAVE_FCHDIR
6882 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6883#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006884#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006885 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006886#endif
6887#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006888 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006889#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006890#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006891#ifdef WCOREDUMP
6892 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6893#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006894#ifdef WIFCONTINUED
6895 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6896#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006897#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006898 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006899#endif /* WIFSTOPPED */
6900#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006901 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006902#endif /* WIFSIGNALED */
6903#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006904 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006905#endif /* WIFEXITED */
6906#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006907 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006908#endif /* WEXITSTATUS */
6909#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006910 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006911#endif /* WTERMSIG */
6912#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006913 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006914#endif /* WSTOPSIG */
6915#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00006916#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006917 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006918#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00006919#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006920 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006921#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00006922#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00006923 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006924#endif
6925#ifdef HAVE_TEMPNAM
6926 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
6927#endif
6928#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00006929 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006930#endif
Fred Drakec9680921999-12-13 16:37:25 +00006931#ifdef HAVE_CONFSTR
6932 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
6933#endif
6934#ifdef HAVE_SYSCONF
6935 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
6936#endif
6937#ifdef HAVE_FPATHCONF
6938 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
6939#endif
6940#ifdef HAVE_PATHCONF
6941 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
6942#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006943 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006944#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00006945 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
6946#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006947#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00006948 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00006949#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006950 #ifdef MS_WINDOWS
6951 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
6952 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006953 #ifdef __VMS
6954 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
6955 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006956 {NULL, NULL} /* Sentinel */
6957};
6958
6959
Barry Warsaw4a342091996-12-19 23:50:02 +00006960static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006961ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00006962{
Fred Drake4d1e64b2002-04-15 19:40:07 +00006963 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00006964}
6965
Guido van Rossumd48f2521997-12-05 22:19:34 +00006966#if defined(PYOS_OS2)
6967/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006968static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006969{
6970 APIRET rc;
6971 ULONG values[QSV_MAX+1];
6972 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00006973 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00006974
6975 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00006976 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006977 Py_END_ALLOW_THREADS
6978
6979 if (rc != NO_ERROR) {
6980 os2_error(rc);
6981 return -1;
6982 }
6983
Fred Drake4d1e64b2002-04-15 19:40:07 +00006984 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6985 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
6986 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
6987 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
6988 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
6989 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
6990 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00006991
6992 switch (values[QSV_VERSION_MINOR]) {
6993 case 0: ver = "2.00"; break;
6994 case 10: ver = "2.10"; break;
6995 case 11: ver = "2.11"; break;
6996 case 30: ver = "3.00"; break;
6997 case 40: ver = "4.00"; break;
6998 case 50: ver = "5.00"; break;
6999 default:
Tim Peters885d4572001-11-28 20:27:42 +00007000 PyOS_snprintf(tmp, sizeof(tmp),
7001 "%d-%d", values[QSV_VERSION_MAJOR],
7002 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007003 ver = &tmp[0];
7004 }
7005
7006 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007007 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007008 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007009
7010 /* Add Indicator of Which Drive was Used to Boot the System */
7011 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7012 tmp[1] = ':';
7013 tmp[2] = '\0';
7014
Fred Drake4d1e64b2002-04-15 19:40:07 +00007015 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007016}
7017#endif
7018
Barry Warsaw4a342091996-12-19 23:50:02 +00007019static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007020all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007021{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007022#ifdef F_OK
7023 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007024#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007025#ifdef R_OK
7026 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007027#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007028#ifdef W_OK
7029 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007030#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007031#ifdef X_OK
7032 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007033#endif
Fred Drakec9680921999-12-13 16:37:25 +00007034#ifdef NGROUPS_MAX
7035 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7036#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007037#ifdef TMP_MAX
7038 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7039#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007040#ifdef WCONTINUED
7041 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7042#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007043#ifdef WNOHANG
7044 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007045#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007046#ifdef WUNTRACED
7047 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7048#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007049#ifdef O_RDONLY
7050 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7051#endif
7052#ifdef O_WRONLY
7053 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7054#endif
7055#ifdef O_RDWR
7056 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7057#endif
7058#ifdef O_NDELAY
7059 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7060#endif
7061#ifdef O_NONBLOCK
7062 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7063#endif
7064#ifdef O_APPEND
7065 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7066#endif
7067#ifdef O_DSYNC
7068 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7069#endif
7070#ifdef O_RSYNC
7071 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7072#endif
7073#ifdef O_SYNC
7074 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7075#endif
7076#ifdef O_NOCTTY
7077 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7078#endif
7079#ifdef O_CREAT
7080 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7081#endif
7082#ifdef O_EXCL
7083 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7084#endif
7085#ifdef O_TRUNC
7086 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7087#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007088#ifdef O_BINARY
7089 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7090#endif
7091#ifdef O_TEXT
7092 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7093#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007094#ifdef O_LARGEFILE
7095 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7096#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007097#ifdef O_SHLOCK
7098 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7099#endif
7100#ifdef O_EXLOCK
7101 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7102#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007103
Tim Peters5aa91602002-01-30 05:46:57 +00007104/* MS Windows */
7105#ifdef O_NOINHERIT
7106 /* Don't inherit in child processes. */
7107 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7108#endif
7109#ifdef _O_SHORT_LIVED
7110 /* Optimize for short life (keep in memory). */
7111 /* MS forgot to define this one with a non-underscore form too. */
7112 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7113#endif
7114#ifdef O_TEMPORARY
7115 /* Automatically delete when last handle is closed. */
7116 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7117#endif
7118#ifdef O_RANDOM
7119 /* Optimize for random access. */
7120 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7121#endif
7122#ifdef O_SEQUENTIAL
7123 /* Optimize for sequential access. */
7124 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7125#endif
7126
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007127/* GNU extensions. */
7128#ifdef O_DIRECT
7129 /* Direct disk access. */
7130 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7131#endif
7132#ifdef O_DIRECTORY
7133 /* Must be a directory. */
7134 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7135#endif
7136#ifdef O_NOFOLLOW
7137 /* Do not follow links. */
7138 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7139#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007140
Barry Warsaw5676bd12003-01-07 20:57:09 +00007141 /* These come from sysexits.h */
7142#ifdef EX_OK
7143 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007144#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007145#ifdef EX_USAGE
7146 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007147#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007148#ifdef EX_DATAERR
7149 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007150#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007151#ifdef EX_NOINPUT
7152 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007153#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007154#ifdef EX_NOUSER
7155 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007156#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007157#ifdef EX_NOHOST
7158 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007159#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007160#ifdef EX_UNAVAILABLE
7161 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007162#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007163#ifdef EX_SOFTWARE
7164 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007165#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007166#ifdef EX_OSERR
7167 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007168#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007169#ifdef EX_OSFILE
7170 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007171#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007172#ifdef EX_CANTCREAT
7173 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007174#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007175#ifdef EX_IOERR
7176 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007177#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007178#ifdef EX_TEMPFAIL
7179 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007180#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007181#ifdef EX_PROTOCOL
7182 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007183#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007184#ifdef EX_NOPERM
7185 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007186#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007187#ifdef EX_CONFIG
7188 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007189#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007190#ifdef EX_NOTFOUND
7191 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007192#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007193
Guido van Rossum246bc171999-02-01 23:54:31 +00007194#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007195#if defined(PYOS_OS2) && defined(PYCC_GCC)
7196 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7197 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7198 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7199 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7200 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7201 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7202 if (ins(d, "P_PM", (long)P_PM)) return -1;
7203 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7204 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7205 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7206 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7207 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7208 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7209 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7210 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7211 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7212 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7213 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7214 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7215 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7216#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007217 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7218 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7219 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7220 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7221 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007222#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007223#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007224
Guido van Rossumd48f2521997-12-05 22:19:34 +00007225#if defined(PYOS_OS2)
7226 if (insertvalues(d)) return -1;
7227#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007228 return 0;
7229}
7230
7231
Tim Peters5aa91602002-01-30 05:46:57 +00007232#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007233#define INITFUNC initnt
7234#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007235
7236#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007237#define INITFUNC initos2
7238#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007239
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007240#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007241#define INITFUNC initposix
7242#define MODNAME "posix"
7243#endif
7244
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007245PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007246INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007247{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007248 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007249
Fred Drake4d1e64b2002-04-15 19:40:07 +00007250 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007251 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007252 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007253 if (m == NULL)
7254 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007255
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007256 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007257 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007258 Py_XINCREF(v);
7259 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007260 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007261 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007262
Fred Drake4d1e64b2002-04-15 19:40:07 +00007263 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007264 return;
7265
Fred Drake4d1e64b2002-04-15 19:40:07 +00007266 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007267 return;
7268
Fred Drake4d1e64b2002-04-15 19:40:07 +00007269 Py_INCREF(PyExc_OSError);
7270 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007271
Guido van Rossumb3d39562000-01-31 18:41:26 +00007272#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007273 if (posix_putenv_garbage == NULL)
7274 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007275#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007276
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007277 if (!initialized) {
7278 stat_result_desc.name = MODNAME ".stat_result";
7279 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7280 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7281 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7282 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7283 structseq_new = StatResultType.tp_new;
7284 StatResultType.tp_new = statresult_new;
7285
7286 statvfs_result_desc.name = MODNAME ".statvfs_result";
7287 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7288 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007289 Py_INCREF((PyObject*) &StatResultType);
7290 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007291 Py_INCREF((PyObject*) &StatVFSResultType);
7292 PyModule_AddObject(m, "statvfs_result",
7293 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007294 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007295
7296#ifdef __APPLE__
7297 /*
7298 * Step 2 of weak-linking support on Mac OS X.
7299 *
7300 * The code below removes functions that are not available on the
7301 * currently active platform.
7302 *
7303 * This block allow one to use a python binary that was build on
7304 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7305 * OSX 10.4.
7306 */
7307#ifdef HAVE_FSTATVFS
7308 if (fstatvfs == NULL) {
7309 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7310 return;
7311 }
7312 }
7313#endif /* HAVE_FSTATVFS */
7314
7315#ifdef HAVE_STATVFS
7316 if (statvfs == NULL) {
7317 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7318 return;
7319 }
7320 }
7321#endif /* HAVE_STATVFS */
7322
7323# ifdef HAVE_LCHOWN
7324 if (lchown == NULL) {
7325 if (PyObject_DelAttrString(m, "lchown") == -1) {
7326 return;
7327 }
7328 }
7329#endif /* HAVE_LCHOWN */
7330
7331
7332#endif /* __APPLE__ */
7333
Guido van Rossumb6775db1994-08-01 11:34:53 +00007334}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007335
7336#ifdef __cplusplus
7337}
7338#endif