blob: 53856b4cd9589de5c64c4eb9310e2e7dc97faffc [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
72#include <sys/wait.h> /* For WNOHANG */
73#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
107#define HAVE_SYSTEM 1
108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
114#ifdef __BORLANDC__ /* Borland compiler */
115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_SYSTEM 1
120#define HAVE_WAIT 1
121#else
122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000128#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000129#define HAVE_FSYNC 1
130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#else /* all other compilers */
135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_SYSTEM 1
151#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int chown(const char *, uid_t, gid_t);
190extern char *getcwd(char *, int);
191extern char *strerror(int);
192extern int link(const char *, const char *);
193extern int rename(const char *, const char *);
194extern int stat(const char *, struct stat *);
195extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000198#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000201#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000203
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_UTIME_H
207#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000210#ifdef HAVE_SYS_UTIME_H
211#include <sys/utime.h>
212#define HAVE_UTIME_H /* pretend we do for the rest of this file */
213#endif /* HAVE_SYS_UTIME_H */
214
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#ifdef HAVE_SYS_TIMES_H
216#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000217#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
219#ifdef HAVE_SYS_PARAM_H
220#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
223#ifdef HAVE_SYS_UTSNAME_H
224#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#define NAMLEN(dirent) strlen((dirent)->d_name)
230#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000231#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000232#include <direct.h>
233#define NAMLEN(dirent) strlen((dirent)->d_name)
234#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000237#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000238#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#endif
241#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#endif
244#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000252#endif
253#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255#endif
256#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000259#include "osdefs.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000261#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
Guido van Rossumd48f2521997-12-05 22:19:34 +0000264#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000266#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267
Tim Petersbc2e10e2002-03-03 23:17:02 +0000268#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269#if defined(PATH_MAX) && PATH_MAX > 1024
270#define MAXPATHLEN PATH_MAX
271#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000272#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000273#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#endif /* MAXPATHLEN */
275
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000276#ifdef UNION_WAIT
277/* Emulate some macros on systems that have a union instead of macros */
278
279#ifndef WIFEXITED
280#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
281#endif
282
283#ifndef WEXITSTATUS
284#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
285#endif
286
287#ifndef WTERMSIG
288#define WTERMSIG(u_wait) ((u_wait).w_termsig)
289#endif
290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291#define WAIT_TYPE union wait
292#define WAIT_STATUS_INT(s) (s.w_status)
293
294#else /* !UNION_WAIT */
295#define WAIT_TYPE int
296#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000297#endif /* UNION_WAIT */
298
Greg Wardb48bc172000-03-01 21:51:56 +0000299/* Don't use the "_r" form if we don't need it (also, won't have a
300 prototype for it, at least on Solaris -- maybe others as well?). */
301#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
302#define USE_CTERMID_R
303#endif
304
305#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
306#define USE_TMPNAM_R
307#endif
308
Fred Drake699f3522000-06-29 21:12:41 +0000309/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000310#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000311#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwis14694662006-02-03 12:54:16 +0000312# define STAT win32_stat
313# define FSTAT win32_fstat
314# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000315#else
316# define STAT stat
317# define FSTAT fstat
318# define STRUCT_STAT struct stat
319#endif
320
Tim Peters11b23062003-04-23 02:39:17 +0000321#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000322#include <sys/mkdev.h>
323#else
324#if defined(MAJOR_IN_SYSMACROS)
325#include <sys/sysmacros.h>
326#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000327#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
328#include <sys/mkdev.h>
329#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000330#endif
Fred Drake699f3522000-06-29 21:12:41 +0000331
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000333#ifdef WITH_NEXT_FRAMEWORK
334/* On Darwin/MacOSX a shared library or framework has no access to
335** environ directly, we must obtain it with _NSGetEnviron().
336*/
337#include <crt_externs.h>
338static char **environ;
339#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000341#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342
Barry Warsaw53699e91996-12-10 23:23:01 +0000343static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000344convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345{
Barry Warsaw53699e91996-12-10 23:23:01 +0000346 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000348 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 if (d == NULL)
350 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000351#ifdef WITH_NEXT_FRAMEWORK
352 if (environ == NULL)
353 environ = *_NSGetEnviron();
354#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 if (environ == NULL)
356 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000357 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000359 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000360 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 char *p = strchr(*e, '=');
362 if (p == NULL)
363 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000364 k = PyString_FromStringAndSize(*e, (int)(p-*e));
365 if (k == NULL) {
366 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000368 }
369 v = PyString_FromString(p+1);
370 if (v == NULL) {
371 PyErr_Clear();
372 Py_DECREF(k);
373 continue;
374 }
375 if (PyDict_GetItem(d, k) == NULL) {
376 if (PyDict_SetItem(d, k, v) != 0)
377 PyErr_Clear();
378 }
379 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000380 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000382#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000383 {
384 APIRET rc;
385 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
386
387 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000388 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000389 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000390 PyDict_SetItemString(d, "BEGINLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000391 Py_DECREF(v);
392 }
393 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
394 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
395 PyObject *v = PyString_FromString(buffer);
Neal Norwitz93c56822007-08-26 07:10:06 +0000396 PyDict_SetItemString(d, "ENDLIBPATH", v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000397 Py_DECREF(v);
398 }
399 }
400#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 return d;
402}
403
404
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405/* Set a POSIX-specific error from errno, and return NULL */
406
Barry Warsawd58d7641998-07-23 16:14:40 +0000407static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000408posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000409{
Barry Warsawca74da41999-02-09 19:31:45 +0000410 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411}
Barry Warsawd58d7641998-07-23 16:14:40 +0000412static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000413posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000414{
Barry Warsawca74da41999-02-09 19:31:45 +0000415 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000416}
417
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000418#ifdef Py_WIN_WIDE_FILENAMES
419static PyObject *
420posix_error_with_unicode_filename(Py_UNICODE* name)
421{
422 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
423}
424#endif /* Py_WIN_WIDE_FILENAMES */
425
426
Mark Hammondef8b6542001-05-13 08:04:26 +0000427static PyObject *
428posix_error_with_allocated_filename(char* name)
429{
430 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
431 PyMem_Free(name);
432 return rc;
433}
434
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000435#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000436static PyObject *
437win32_error(char* function, char* filename)
438{
Mark Hammond33a6da92000-08-15 00:46:38 +0000439 /* XXX We should pass the function name along in the future.
440 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000441 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000442 Windows error object, which is non-trivial.
443 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000444 errno = GetLastError();
445 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000446 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000447 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000448 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000449}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450
451#ifdef Py_WIN_WIDE_FILENAMES
452static PyObject *
453win32_error_unicode(char* function, Py_UNICODE* filename)
454{
455 /* XXX - see win32_error for comments on 'function' */
456 errno = GetLastError();
457 if (filename)
458 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
459 else
460 return PyErr_SetFromWindowsErr(errno);
461}
462
463static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
464{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465}
466
467/* Function suitable for O& conversion */
468static int
469convert_to_unicode(PyObject *arg, void* _param)
470{
471 PyObject **param = (PyObject**)_param;
472 if (PyUnicode_CheckExact(arg)) {
473 Py_INCREF(arg);
474 *param = arg;
475 }
476 else if (PyUnicode_Check(arg)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000477 /* For a Unicode subtype that's not a Unicode object,
478 return a true Unicode object with the same data. */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
480 PyUnicode_GET_SIZE(arg));
481 return *param != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000482 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 else
484 *param = PyUnicode_FromEncodedObject(arg,
485 Py_FileSystemDefaultEncoding,
486 "strict");
487 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000488}
489
490#endif /* Py_WIN_WIDE_FILENAMES */
491
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000492#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493
Guido van Rossumd48f2521997-12-05 22:19:34 +0000494#if defined(PYOS_OS2)
495/**********************************************************************
496 * Helper Function to Trim and Format OS/2 Messages
497 **********************************************************************/
498 static void
499os2_formatmsg(char *msgbuf, int msglen, char *reason)
500{
501 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
502
503 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
504 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
505
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000506 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000507 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
508 }
509
510 /* Add Optional Reason Text */
511 if (reason) {
512 strcat(msgbuf, " : ");
513 strcat(msgbuf, reason);
514 }
515}
516
517/**********************************************************************
518 * Decode an OS/2 Operating System Error Code
519 *
520 * A convenience function to lookup an OS/2 error code and return a
521 * text message we can use to raise a Python exception.
522 *
523 * Notes:
524 * The messages for errors returned from the OS/2 kernel reside in
525 * the file OSO001.MSG in the \OS2 directory hierarchy.
526 *
527 **********************************************************************/
528 static char *
529os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
530{
531 APIRET rc;
532 ULONG msglen;
533
534 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
535 Py_BEGIN_ALLOW_THREADS
536 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
537 errorcode, "oso001.msg", &msglen);
538 Py_END_ALLOW_THREADS
539
540 if (rc == NO_ERROR)
541 os2_formatmsg(msgbuf, msglen, reason);
542 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000543 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000544 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000545
546 return msgbuf;
547}
548
549/* Set an OS/2-specific error and return NULL. OS/2 kernel
550 errors are not in a global variable e.g. 'errno' nor are
551 they congruent with posix error numbers. */
552
553static PyObject * os2_error(int code)
554{
555 char text[1024];
556 PyObject *v;
557
558 os2_strerror(text, sizeof(text), code, "");
559
560 v = Py_BuildValue("(is)", code, text);
561 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000562 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000563 Py_DECREF(v);
564 }
565 return NULL; /* Signal to Python that an Exception is Pending */
566}
567
568#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569
570/* POSIX generic methods */
571
Barry Warsaw53699e91996-12-10 23:23:01 +0000572static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000573posix_fildes(PyObject *fdobj, int (*func)(int))
574{
575 int fd;
576 int res;
577 fd = PyObject_AsFileDescriptor(fdobj);
578 if (fd < 0)
579 return NULL;
580 Py_BEGIN_ALLOW_THREADS
581 res = (*func)(fd);
582 Py_END_ALLOW_THREADS
583 if (res < 0)
584 return posix_error();
585 Py_INCREF(Py_None);
586 return Py_None;
587}
Guido van Rossum21142a01999-01-08 21:05:37 +0000588
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000589#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000590static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000591unicode_file_names(void)
592{
593 static int canusewide = -1;
594 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000595 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000596 the Windows NT family. */
597 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
598 }
599 return canusewide;
600}
601#endif
Tim Peters11b23062003-04-23 02:39:17 +0000602
Guido van Rossum21142a01999-01-08 21:05:37 +0000603static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605{
Mark Hammondef8b6542001-05-13 08:04:26 +0000606 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 int res;
Tim Peters5aa91602002-01-30 05:46:57 +0000608 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000609 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000611 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000612 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000613 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000614 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000615 return posix_error_with_allocated_filename(path1);
616 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000617 Py_INCREF(Py_None);
618 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Barry Warsaw53699e91996-12-10 23:23:01 +0000621static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000622posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623 char *format,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625{
Mark Hammondef8b6542001-05-13 08:04:26 +0000626 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000627 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +0000628 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000629 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000630 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000632 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000633 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000634 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000635 PyMem_Free(path1);
636 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000637 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000638 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 Py_INCREF(Py_None);
641 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644#ifdef Py_WIN_WIDE_FILENAMES
645static PyObject*
646win32_1str(PyObject* args, char* func,
647 char* format, BOOL (__stdcall *funcA)(LPCSTR),
648 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
649{
650 PyObject *uni;
651 char *ansi;
652 BOOL result;
653 if (unicode_file_names()) {
654 if (!PyArg_ParseTuple(args, wformat, &uni))
655 PyErr_Clear();
656 else {
657 Py_BEGIN_ALLOW_THREADS
658 result = funcW(PyUnicode_AsUnicode(uni));
659 Py_END_ALLOW_THREADS
660 if (!result)
661 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
662 Py_INCREF(Py_None);
663 return Py_None;
664 }
665 }
666 if (!PyArg_ParseTuple(args, format, &ansi))
667 return NULL;
668 Py_BEGIN_ALLOW_THREADS
669 result = funcA(ansi);
670 Py_END_ALLOW_THREADS
671 if (!result)
672 return win32_error(func, ansi);
673 Py_INCREF(Py_None);
674 return Py_None;
675
676}
677
678/* This is a reimplementation of the C library's chdir function,
679 but one that produces Win32 errors instead of DOS error codes.
680 chdir is essentially a wrapper around SetCurrentDirectory; however,
681 it also needs to set "magic" environment variables indicating
682 the per-drive current directory, which are of the form =<drive>: */
683BOOL __stdcall
684win32_chdir(LPCSTR path)
685{
686 char new_path[MAX_PATH+1];
687 int result;
688 char env[4] = "=x:";
689
690 if(!SetCurrentDirectoryA(path))
691 return FALSE;
692 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
693 if (!result)
694 return FALSE;
695 /* In the ANSI API, there should not be any paths longer
696 than MAX_PATH. */
697 assert(result <= MAX_PATH+1);
698 if (strncmp(new_path, "\\\\", 2) == 0 ||
699 strncmp(new_path, "//", 2) == 0)
700 /* UNC path, nothing to do. */
701 return TRUE;
702 env[1] = new_path[0];
703 return SetEnvironmentVariableA(env, new_path);
704}
705
706/* The Unicode version differs from the ANSI version
707 since the current directory might exceed MAX_PATH characters */
708BOOL __stdcall
709win32_wchdir(LPCWSTR path)
710{
711 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
712 int result;
713 wchar_t env[4] = L"=x:";
714
715 if(!SetCurrentDirectoryW(path))
716 return FALSE;
717 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
718 if (!result)
719 return FALSE;
720 if (result > MAX_PATH+1) {
721 new_path = malloc(result);
722 if (!new_path) {
723 SetLastError(ERROR_OUTOFMEMORY);
724 return FALSE;
725 }
726 }
727 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
728 wcsncmp(new_path, L"//", 2) == 0)
729 /* UNC path, nothing to do. */
730 return TRUE;
731 env[1] = new_path[0];
732 result = SetEnvironmentVariableW(env, new_path);
733 if (new_path != _new_path)
734 free(new_path);
735 return result;
736}
737#endif
738
Martin v. Löwis14694662006-02-03 12:54:16 +0000739#ifdef MS_WINDOWS
740/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
741 - time stamps are restricted to second resolution
742 - file modification times suffer from forth-and-back conversions between
743 UTC and local time
744 Therefore, we implement our own stat, based on the Win32 API directly.
745*/
746#define HAVE_STAT_NSEC 1
747
748struct win32_stat{
749 int st_dev;
750 __int64 st_ino;
751 unsigned short st_mode;
752 int st_nlink;
753 int st_uid;
754 int st_gid;
755 int st_rdev;
756 __int64 st_size;
757 int st_atime;
758 int st_atime_nsec;
759 int st_mtime;
760 int st_mtime_nsec;
761 int st_ctime;
762 int st_ctime_nsec;
763};
764
765static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
766
767static void
768FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
769{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000770 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
771 /* Cannot simply cast and dereference in_ptr,
772 since it might not be aligned properly */
773 __int64 in;
774 memcpy(&in, in_ptr, sizeof(in));
Martin v. Löwis14694662006-02-03 12:54:16 +0000775 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
776 /* XXX Win32 supports time stamps past 2038; we currently don't */
777 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
778}
779
Thomas Wouters477c8d52006-05-27 19:21:47 +0000780static void
781time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
782{
783 /* XXX endianness */
784 __int64 out;
785 out = time_in + secs_between_epochs;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000786 out = out * 10000000 + nsec_in / 100;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 memcpy(out_ptr, &out, sizeof(out));
788}
789
Martin v. Löwis14694662006-02-03 12:54:16 +0000790/* Below, we *know* that ugo+r is 0444 */
791#if _S_IREAD != 0400
792#error Unsupported C library
793#endif
794static int
795attributes_to_mode(DWORD attr)
796{
797 int m = 0;
798 if (attr & FILE_ATTRIBUTE_DIRECTORY)
799 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
800 else
801 m |= _S_IFREG;
802 if (attr & FILE_ATTRIBUTE_READONLY)
803 m |= 0444;
804 else
805 m |= 0666;
806 return m;
807}
808
809static int
810attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
811{
812 memset(result, 0, sizeof(*result));
813 result->st_mode = attributes_to_mode(info->dwFileAttributes);
814 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
815 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
816 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
817 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
818
819 return 0;
820}
821
Thomas Wouters89f507f2006-12-13 04:49:30 +0000822/* Emulate GetFileAttributesEx[AW] on Windows 95 */
823static int checked = 0;
824static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
825static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
826static void
827check_gfax()
828{
829 HINSTANCE hKernel32;
830 if (checked)
831 return;
832 checked = 1;
833 hKernel32 = GetModuleHandle("KERNEL32");
834 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
835 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
836}
837
Guido van Rossumd8faa362007-04-27 19:54:29 +0000838static BOOL
839attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
840{
841 HANDLE hFindFile;
842 WIN32_FIND_DATAA FileData;
843 hFindFile = FindFirstFileA(pszFile, &FileData);
844 if (hFindFile == INVALID_HANDLE_VALUE)
845 return FALSE;
846 FindClose(hFindFile);
847 pfad->dwFileAttributes = FileData.dwFileAttributes;
848 pfad->ftCreationTime = FileData.ftCreationTime;
849 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
850 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
851 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
852 pfad->nFileSizeLow = FileData.nFileSizeLow;
853 return TRUE;
854}
855
856static BOOL
857attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
858{
859 HANDLE hFindFile;
860 WIN32_FIND_DATAW FileData;
861 hFindFile = FindFirstFileW(pszFile, &FileData);
862 if (hFindFile == INVALID_HANDLE_VALUE)
863 return FALSE;
864 FindClose(hFindFile);
865 pfad->dwFileAttributes = FileData.dwFileAttributes;
866 pfad->ftCreationTime = FileData.ftCreationTime;
867 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
868 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
869 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
870 pfad->nFileSizeLow = FileData.nFileSizeLow;
871 return TRUE;
872}
873
Thomas Wouters89f507f2006-12-13 04:49:30 +0000874static BOOL WINAPI
875Py_GetFileAttributesExA(LPCSTR pszFile,
876 GET_FILEEX_INFO_LEVELS level,
877 LPVOID pv)
878{
879 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000880 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
881 /* First try to use the system's implementation, if that is
882 available and either succeeds to gives an error other than
883 that it isn't implemented. */
884 check_gfax();
885 if (gfaxa) {
886 result = gfaxa(pszFile, level, pv);
887 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
888 return result;
889 }
890 /* It's either not present, or not implemented.
891 Emulate using FindFirstFile. */
892 if (level != GetFileExInfoStandard) {
893 SetLastError(ERROR_INVALID_PARAMETER);
894 return FALSE;
895 }
896 /* Use GetFileAttributes to validate that the file name
897 does not contain wildcards (which FindFirstFile would
898 accept). */
899 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
900 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000901 return attributes_from_dir(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000902}
903
904static BOOL WINAPI
905Py_GetFileAttributesExW(LPCWSTR pszFile,
906 GET_FILEEX_INFO_LEVELS level,
907 LPVOID pv)
908{
909 BOOL result;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
911 /* First try to use the system's implementation, if that is
912 available and either succeeds to gives an error other than
913 that it isn't implemented. */
914 check_gfax();
915 if (gfaxa) {
916 result = gfaxw(pszFile, level, pv);
917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
918 return result;
919 }
920 /* It's either not present, or not implemented.
921 Emulate using FindFirstFile. */
922 if (level != GetFileExInfoStandard) {
923 SetLastError(ERROR_INVALID_PARAMETER);
924 return FALSE;
925 }
926 /* Use GetFileAttributes to validate that the file name
927 does not contain wildcards (which FindFirstFile would
928 accept). */
929 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
930 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000931 return attributes_from_dir_w(pszFile, pfad);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000932}
933
Martin v. Löwis14694662006-02-03 12:54:16 +0000934static int
935win32_stat(const char* path, struct win32_stat *result)
936{
937 WIN32_FILE_ATTRIBUTE_DATA info;
938 int code;
939 char *dot;
940 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000941 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942 if (GetLastError() != ERROR_SHARING_VIOLATION) {
943 /* Protocol violation: we explicitly clear errno, instead of
944 setting it to a POSIX error. Callers should use GetLastError. */
945 errno = 0;
946 return -1;
947 } else {
948 /* Could not get attributes on open file. Fall back to
949 reading the directory. */
950 if (!attributes_from_dir(path, &info)) {
951 /* Very strange. This should not fail now */
952 errno = 0;
953 return -1;
954 }
955 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000956 }
957 code = attribute_data_to_stat(&info, result);
958 if (code != 0)
959 return code;
960 /* Set S_IFEXEC if it is an .exe, .bat, ... */
961 dot = strrchr(path, '.');
962 if (dot) {
963 if (stricmp(dot, ".bat") == 0 ||
964 stricmp(dot, ".cmd") == 0 ||
965 stricmp(dot, ".exe") == 0 ||
966 stricmp(dot, ".com") == 0)
967 result->st_mode |= 0111;
968 }
969 return code;
970}
971
972static int
973win32_wstat(const wchar_t* path, struct win32_stat *result)
974{
975 int code;
976 const wchar_t *dot;
977 WIN32_FILE_ATTRIBUTE_DATA info;
978 /* XXX not supported on Win95 and NT 3.x */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000979 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000980 if (GetLastError() != ERROR_SHARING_VIOLATION) {
981 /* Protocol violation: we explicitly clear errno, instead of
982 setting it to a POSIX error. Callers should use GetLastError. */
983 errno = 0;
984 return -1;
985 } else {
986 /* Could not get attributes on open file. Fall back to
987 reading the directory. */
988 if (!attributes_from_dir_w(path, &info)) {
989 /* Very strange. This should not fail now */
990 errno = 0;
991 return -1;
992 }
993 }
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 }
995 code = attribute_data_to_stat(&info, result);
996 if (code < 0)
997 return code;
998 /* Set IFEXEC if it is an .exe, .bat, ... */
999 dot = wcsrchr(path, '.');
1000 if (dot) {
1001 if (_wcsicmp(dot, L".bat") == 0 ||
1002 _wcsicmp(dot, L".cmd") == 0 ||
1003 _wcsicmp(dot, L".exe") == 0 ||
1004 _wcsicmp(dot, L".com") == 0)
1005 result->st_mode |= 0111;
1006 }
1007 return code;
1008}
1009
1010static int
1011win32_fstat(int file_number, struct win32_stat *result)
1012{
1013 BY_HANDLE_FILE_INFORMATION info;
1014 HANDLE h;
1015 int type;
1016
1017 h = (HANDLE)_get_osfhandle(file_number);
1018
1019 /* Protocol violation: we explicitly clear errno, instead of
1020 setting it to a POSIX error. Callers should use GetLastError. */
1021 errno = 0;
1022
1023 if (h == INVALID_HANDLE_VALUE) {
1024 /* This is really a C library error (invalid file handle).
1025 We set the Win32 error to the closes one matching. */
1026 SetLastError(ERROR_INVALID_HANDLE);
1027 return -1;
1028 }
1029 memset(result, 0, sizeof(*result));
1030
1031 type = GetFileType(h);
1032 if (type == FILE_TYPE_UNKNOWN) {
1033 DWORD error = GetLastError();
1034 if (error != 0) {
1035 return -1;
1036 }
1037 /* else: valid but unknown file */
1038 }
1039
1040 if (type != FILE_TYPE_DISK) {
1041 if (type == FILE_TYPE_CHAR)
1042 result->st_mode = _S_IFCHR;
1043 else if (type == FILE_TYPE_PIPE)
1044 result->st_mode = _S_IFIFO;
1045 return 0;
1046 }
1047
1048 if (!GetFileInformationByHandle(h, &info)) {
1049 return -1;
1050 }
1051
1052 /* similar to stat() */
1053 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1054 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1055 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1056 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1057 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1058 /* specific to fstat() */
1059 result->st_nlink = info.nNumberOfLinks;
1060 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1061 return 0;
1062}
1063
1064#endif /* MS_WINDOWS */
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001067"stat_result: Result from stat or lstat.\n\n\
1068This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001069 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001070or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1071\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001072Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1073or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001074\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001076
1077static PyStructSequence_Field stat_result_fields[] = {
1078 {"st_mode", "protection bits"},
1079 {"st_ino", "inode"},
1080 {"st_dev", "device"},
1081 {"st_nlink", "number of hard links"},
1082 {"st_uid", "user ID of owner"},
1083 {"st_gid", "group ID of owner"},
1084 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001085 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1086 {NULL, "integer time of last access"},
1087 {NULL, "integer time of last modification"},
1088 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001089 {"st_atime", "time of last access"},
1090 {"st_mtime", "time of last modification"},
1091 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001092#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001093 {"st_blksize", "blocksize for filesystem I/O"},
1094#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001095#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001096 {"st_blocks", "number of blocks allocated"},
1097#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001098#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001099 {"st_rdev", "device type (if inode device)"},
1100#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001101#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1102 {"st_flags", "user defined flags for file"},
1103#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001104#ifdef HAVE_STRUCT_STAT_ST_GEN
1105 {"st_gen", "generation number"},
1106#endif
1107#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1108 {"st_birthtime", "time of creation"},
1109#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110 {0}
1111};
1112
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001113#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001114#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001115#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001116#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001117#endif
1118
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001119#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001120#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1121#else
1122#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1123#endif
1124
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001125#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001126#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1127#else
1128#define ST_RDEV_IDX ST_BLOCKS_IDX
1129#endif
1130
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001131#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1132#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1133#else
1134#define ST_FLAGS_IDX ST_RDEV_IDX
1135#endif
1136
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001137#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001138#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001139#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001140#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001141#endif
1142
1143#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1144#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1145#else
1146#define ST_BIRTHTIME_IDX ST_GEN_IDX
1147#endif
1148
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001149static PyStructSequence_Desc stat_result_desc = {
1150 "stat_result", /* name */
1151 stat_result__doc__, /* doc */
1152 stat_result_fields,
1153 10
1154};
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001157"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1158This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001159 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001160or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001161\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001162See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163
1164static PyStructSequence_Field statvfs_result_fields[] = {
1165 {"f_bsize", },
1166 {"f_frsize", },
1167 {"f_blocks", },
1168 {"f_bfree", },
1169 {"f_bavail", },
1170 {"f_files", },
1171 {"f_ffree", },
1172 {"f_favail", },
1173 {"f_flag", },
1174 {"f_namemax",},
1175 {0}
1176};
1177
1178static PyStructSequence_Desc statvfs_result_desc = {
1179 "statvfs_result", /* name */
1180 statvfs_result__doc__, /* doc */
1181 statvfs_result_fields,
1182 10
1183};
1184
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001186static PyTypeObject StatResultType;
1187static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001188static newfunc structseq_new;
1189
1190static PyObject *
1191statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1192{
1193 PyStructSequence *result;
1194 int i;
1195
1196 result = (PyStructSequence*)structseq_new(type, args, kwds);
1197 if (!result)
1198 return NULL;
1199 /* If we have been initialized from a tuple,
1200 st_?time might be set to None. Initialize it
1201 from the int slots. */
1202 for (i = 7; i <= 9; i++) {
1203 if (result->ob_item[i+3] == Py_None) {
1204 Py_DECREF(Py_None);
1205 Py_INCREF(result->ob_item[i]);
1206 result->ob_item[i+3] = result->ob_item[i];
1207 }
1208 }
1209 return (PyObject*)result;
1210}
1211
1212
1213
1214/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001215static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001216
1217PyDoc_STRVAR(stat_float_times__doc__,
1218"stat_float_times([newval]) -> oldval\n\n\
1219Determine whether os.[lf]stat represents time stamps as float objects.\n\
1220If newval is True, future calls to stat() return floats, if it is False,\n\
1221future calls return ints. \n\
1222If newval is omitted, return the current setting.\n");
1223
1224static PyObject*
1225stat_float_times(PyObject* self, PyObject *args)
1226{
1227 int newval = -1;
1228 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1229 return NULL;
1230 if (newval == -1)
1231 /* Return old value */
1232 return PyBool_FromLong(_stat_float_times);
1233 _stat_float_times = newval;
1234 Py_INCREF(Py_None);
1235 return Py_None;
1236}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001237
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001238static void
1239fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1240{
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001241 PyObject *fval,*ival;
1242#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001243 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001244#else
1245 ival = PyInt_FromLong((long)sec);
1246#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001247 if (!ival)
1248 return;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001249 if (_stat_float_times) {
1250 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1251 } else {
1252 fval = ival;
1253 Py_INCREF(fval);
1254 }
1255 PyStructSequence_SET_ITEM(v, index, ival);
1256 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001257}
1258
Tim Peters5aa91602002-01-30 05:46:57 +00001259/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001260 (used by posix_stat() and posix_fstat()) */
1261static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001262_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001263{
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001264 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001265 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +00001266 if (v == NULL)
1267 return NULL;
1268
Martin v. Löwis14694662006-02-03 12:54:16 +00001269 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001270#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001271 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwis14694662006-02-03 12:54:16 +00001272 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001273#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001274 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001275#endif
1276#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +00001277 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwis14694662006-02-03 12:54:16 +00001278 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001279#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001280 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001281#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001282 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
1283 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
1284 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001285#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +00001286 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwis14694662006-02-03 12:54:16 +00001287 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001288#else
Martin v. Löwis14694662006-02-03 12:54:16 +00001289 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001290#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001291
Martin v. Löwis14694662006-02-03 12:54:16 +00001292#if defined(HAVE_STAT_TV_NSEC)
1293 ansec = st->st_atim.tv_nsec;
1294 mnsec = st->st_mtim.tv_nsec;
1295 cnsec = st->st_ctim.tv_nsec;
1296#elif defined(HAVE_STAT_TV_NSEC2)
1297 ansec = st->st_atimespec.tv_nsec;
1298 mnsec = st->st_mtimespec.tv_nsec;
1299 cnsec = st->st_ctimespec.tv_nsec;
1300#elif defined(HAVE_STAT_NSEC)
1301 ansec = st->st_atime_nsec;
1302 mnsec = st->st_mtime_nsec;
1303 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001304#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001305 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001306#endif
Martin v. Löwis14694662006-02-03 12:54:16 +00001307 fill_time(v, 7, st->st_atime, ansec);
1308 fill_time(v, 8, st->st_mtime, mnsec);
1309 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001310
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001311#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +00001312 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001313 PyInt_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001314#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001315#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +00001316 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001317 PyInt_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001318#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001319#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001320 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001321 PyInt_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001322#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001323#ifdef HAVE_STRUCT_STAT_ST_GEN
1324 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001325 PyInt_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001326#endif
1327#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1328 {
1329 PyObject *val;
1330 unsigned long bsec,bnsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001331 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001332#ifdef HAVE_STAT_TV_NSEC2
Hye-Shik Changd69e0342006-02-19 16:22:22 +00001333 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001334#else
1335 bnsec = 0;
1336#endif
1337 if (_stat_float_times) {
1338 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1339 } else {
1340 val = PyInt_FromLong((long)bsec);
1341 }
1342 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1343 val);
1344 }
1345#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001346#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1347 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
Martin v. Löwis14694662006-02-03 12:54:16 +00001348 PyInt_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001349#endif
Fred Drake699f3522000-06-29 21:12:41 +00001350
1351 if (PyErr_Occurred()) {
1352 Py_DECREF(v);
1353 return NULL;
1354 }
1355
1356 return v;
1357}
1358
Martin v. Löwisd8948722004-06-02 09:57:56 +00001359#ifdef MS_WINDOWS
1360
1361/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1362 where / can be used in place of \ and the trailing slash is optional.
1363 Both SERVER and SHARE must have at least one character.
1364*/
1365
1366#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1367#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001368#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001369#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001370#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001371
Tim Peters4ad82172004-08-30 17:02:04 +00001372static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001373IsUNCRootA(char *path, int pathlen)
1374{
1375 #define ISSLASH ISSLASHA
1376
1377 int i, share;
1378
1379 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1380 /* minimum UNCRoot is \\x\y */
1381 return FALSE;
1382 for (i = 2; i < pathlen ; i++)
1383 if (ISSLASH(path[i])) break;
1384 if (i == 2 || i == pathlen)
1385 /* do not allow \\\SHARE or \\SERVER */
1386 return FALSE;
1387 share = i+1;
1388 for (i = share; i < pathlen; i++)
1389 if (ISSLASH(path[i])) break;
1390 return (i != share && (i == pathlen || i == pathlen-1));
1391
1392 #undef ISSLASH
1393}
1394
1395#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters4ad82172004-08-30 17:02:04 +00001396static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001397IsUNCRootW(Py_UNICODE *path, int pathlen)
1398{
1399 #define ISSLASH ISSLASHW
1400
1401 int i, share;
1402
1403 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1404 /* minimum UNCRoot is \\x\y */
1405 return FALSE;
1406 for (i = 2; i < pathlen ; i++)
1407 if (ISSLASH(path[i])) break;
1408 if (i == 2 || i == pathlen)
1409 /* do not allow \\\SHARE or \\SERVER */
1410 return FALSE;
1411 share = i+1;
1412 for (i = share; i < pathlen; i++)
1413 if (ISSLASH(path[i])) break;
1414 return (i != share && (i == pathlen || i == pathlen-1));
1415
1416 #undef ISSLASH
1417}
1418#endif /* Py_WIN_WIDE_FILENAMES */
1419#endif /* MS_WINDOWS */
1420
Barry Warsaw53699e91996-12-10 23:23:01 +00001421static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001422posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001423 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001424#ifdef __VMS
1425 int (*statfunc)(const char *, STRUCT_STAT *, ...),
1426#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001427 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001428#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001429 char *wformat,
1430 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431{
Fred Drake699f3522000-06-29 21:12:41 +00001432 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +00001433 char *path = NULL; /* pass this to stat; do not free() it */
1434 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +00001435 int res;
Martin v. Löwis14694662006-02-03 12:54:16 +00001436 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001437
1438#ifdef Py_WIN_WIDE_FILENAMES
1439 /* If on wide-character-capable OS see if argument
1440 is Unicode and if so use wide API. */
1441 if (unicode_file_names()) {
1442 PyUnicodeObject *po;
1443 if (PyArg_ParseTuple(args, wformat, &po)) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001444 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
1445
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001446 Py_BEGIN_ALLOW_THREADS
1447 /* PyUnicode_AS_UNICODE result OK without
1448 thread lock as it is a simple dereference. */
1449 res = wstatfunc(wpath, &st);
1450 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001451
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001452 if (res != 0)
Martin v. Löwis14694662006-02-03 12:54:16 +00001453 return win32_error_unicode("stat", wpath);
1454 return _pystat_fromstructstat(&st);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001455 }
1456 /* Drop the argument parsing error as narrow strings
1457 are also valid. */
1458 PyErr_Clear();
1459 }
1460#endif
1461
Tim Peters5aa91602002-01-30 05:46:57 +00001462 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +00001463 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001464 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +00001465 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +00001466
Barry Warsaw53699e91996-12-10 23:23:01 +00001467 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001468 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001469 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001470
1471 if (res != 0) {
1472#ifdef MS_WINDOWS
1473 result = win32_error("stat", pathfree);
1474#else
1475 result = posix_error_with_filename(pathfree);
1476#endif
1477 }
1478 else
1479 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001480
Tim Peters500bd032001-12-19 19:05:01 +00001481 PyMem_Free(pathfree);
Martin v. Löwis14694662006-02-03 12:54:16 +00001482 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001483}
1484
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001485/* POSIX methods */
1486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001488"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001489Use the real uid/gid to test for access to a path. Note that most\n\
1490operations will use the effective uid/gid, therefore this routine can\n\
1491be used in a suid/sgid environment to test if the invoking user has the\n\
1492specified access to the path. The mode argument can be F_OK to test\n\
1493existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001494
1495static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001496posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001497{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001498 char *path;
1499 int mode;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001500
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001501#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502 DWORD attr;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001503 if (unicode_file_names()) {
1504 PyUnicodeObject *po;
1505 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1506 Py_BEGIN_ALLOW_THREADS
1507 /* PyUnicode_AS_UNICODE OK without thread lock as
1508 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001509 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001510 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001511 goto finish;
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001512 }
1513 /* Drop the argument parsing error as narrow strings
1514 are also valid. */
1515 PyErr_Clear();
1516 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001517 if (!PyArg_ParseTuple(args, "eti:access",
1518 Py_FileSystemDefaultEncoding, &path, &mode))
1519 return 0;
1520 Py_BEGIN_ALLOW_THREADS
1521 attr = GetFileAttributesA(path);
1522 Py_END_ALLOW_THREADS
1523 PyMem_Free(path);
1524finish:
1525 if (attr == 0xFFFFFFFF)
1526 /* File does not exist, or cannot read attributes */
1527 return PyBool_FromLong(0);
1528 /* Access is possible if either write access wasn't requested, or
1529 the file isn't read-only. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001530 return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001531#else
1532 int res;
Martin v. Löwisb60ae992005-03-08 09:10:29 +00001533 if (!PyArg_ParseTuple(args, "eti:access",
1534 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001535 return NULL;
1536 Py_BEGIN_ALLOW_THREADS
1537 res = access(path, mode);
1538 Py_END_ALLOW_THREADS
Neal Norwitz24b3c222005-09-19 06:43:44 +00001539 PyMem_Free(path);
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001540 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001542}
1543
Guido van Rossumd371ff11999-01-25 16:12:23 +00001544#ifndef F_OK
1545#define F_OK 0
1546#endif
1547#ifndef R_OK
1548#define R_OK 4
1549#endif
1550#ifndef W_OK
1551#define W_OK 2
1552#endif
1553#ifndef X_OK
1554#define X_OK 1
1555#endif
1556
1557#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001558PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001559"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001561
1562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001563posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001564{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001565 int id;
1566 char *ret;
1567
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001568 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001569 return NULL;
1570
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001571#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001572 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001573 if (id == 0) {
1574 ret = ttyname();
1575 }
1576 else {
1577 ret = NULL;
1578 }
1579#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001580 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001581#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001582 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001583 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001584 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001585}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001586#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001587
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001588#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001590"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001592
1593static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001594posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001595{
1596 char *ret;
1597 char buffer[L_ctermid];
1598
Greg Wardb48bc172000-03-01 21:51:56 +00001599#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001600 ret = ctermid_r(buffer);
1601#else
1602 ret = ctermid(buffer);
1603#endif
1604 if (ret == NULL)
Neal Norwitz3efd0a12005-09-19 06:45:53 +00001605 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001606 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001607}
1608#endif
1609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001610PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001611"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001613
Barry Warsaw53699e91996-12-10 23:23:01 +00001614static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001615posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001616{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001617#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001619#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620 return posix_1str(args, "et:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001621#elif defined(__VMS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001623#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624 return posix_1str(args, "et:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001625#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626}
1627
Fred Drake4d1e64b2002-04-15 19:40:07 +00001628#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001630"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001631Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001632opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001633
1634static PyObject *
1635posix_fchdir(PyObject *self, PyObject *fdobj)
1636{
1637 return posix_fildes(fdobj, fchdir);
1638}
1639#endif /* HAVE_FCHDIR */
1640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001642PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001643"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001644Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001645
Barry Warsaw53699e91996-12-10 23:23:01 +00001646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001647posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648{
Mark Hammondef8b6542001-05-13 08:04:26 +00001649 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001650 int i;
1651 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001652#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653 DWORD attr;
Mark Hammond817c9292003-12-03 01:22:38 +00001654 if (unicode_file_names()) {
1655 PyUnicodeObject *po;
1656 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1657 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001658 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1659 if (attr != 0xFFFFFFFF) {
1660 if (i & _S_IWRITE)
1661 attr &= ~FILE_ATTRIBUTE_READONLY;
1662 else
1663 attr |= FILE_ATTRIBUTE_READONLY;
1664 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1665 }
1666 else
1667 res = 0;
Mark Hammond817c9292003-12-03 01:22:38 +00001668 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001669 if (!res)
1670 return win32_error_unicode("chmod",
Mark Hammond817c9292003-12-03 01:22:38 +00001671 PyUnicode_AS_UNICODE(po));
1672 Py_INCREF(Py_None);
1673 return Py_None;
1674 }
1675 /* Drop the argument parsing error as narrow strings
1676 are also valid. */
1677 PyErr_Clear();
1678 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001679 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
1680 &path, &i))
1681 return NULL;
1682 Py_BEGIN_ALLOW_THREADS
1683 attr = GetFileAttributesA(path);
1684 if (attr != 0xFFFFFFFF) {
1685 if (i & _S_IWRITE)
1686 attr &= ~FILE_ATTRIBUTE_READONLY;
1687 else
1688 attr |= FILE_ATTRIBUTE_READONLY;
1689 res = SetFileAttributesA(path, attr);
1690 }
1691 else
1692 res = 0;
1693 Py_END_ALLOW_THREADS
1694 if (!res) {
1695 win32_error("chmod", path);
1696 PyMem_Free(path);
1697 return NULL;
1698 }
1699 PyMem_Free(path);
1700 Py_INCREF(Py_None);
1701 return Py_None;
1702#else /* Py_WIN_WIDE_FILENAMES */
Mark Hammond817c9292003-12-03 01:22:38 +00001703 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001704 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001705 return NULL;
1706 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001707 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001708 Py_END_ALLOW_THREADS
1709 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001710 return posix_error_with_allocated_filename(path);
1711 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001712 Py_INCREF(Py_None);
1713 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001714#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001715}
1716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001717
Thomas Wouterscf297e42007-02-23 15:07:44 +00001718#ifdef HAVE_CHFLAGS
1719PyDoc_STRVAR(posix_chflags__doc__,
1720"chflags(path, flags)\n\n\
1721Set file flags.");
1722
1723static PyObject *
1724posix_chflags(PyObject *self, PyObject *args)
1725{
1726 char *path;
1727 unsigned long flags;
1728 int res;
1729 if (!PyArg_ParseTuple(args, "etk:chflags",
1730 Py_FileSystemDefaultEncoding, &path, &flags))
1731 return NULL;
1732 Py_BEGIN_ALLOW_THREADS
1733 res = chflags(path, flags);
1734 Py_END_ALLOW_THREADS
1735 if (res < 0)
1736 return posix_error_with_allocated_filename(path);
1737 PyMem_Free(path);
1738 Py_INCREF(Py_None);
1739 return Py_None;
1740}
1741#endif /* HAVE_CHFLAGS */
1742
1743#ifdef HAVE_LCHFLAGS
1744PyDoc_STRVAR(posix_lchflags__doc__,
1745"lchflags(path, flags)\n\n\
1746Set file flags.\n\
1747This function will not follow symbolic links.");
1748
1749static PyObject *
1750posix_lchflags(PyObject *self, PyObject *args)
1751{
1752 char *path;
1753 unsigned long flags;
1754 int res;
1755 if (!PyArg_ParseTuple(args, "etk:lchflags",
1756 Py_FileSystemDefaultEncoding, &path, &flags))
1757 return NULL;
1758 Py_BEGIN_ALLOW_THREADS
1759 res = lchflags(path, flags);
1760 Py_END_ALLOW_THREADS
1761 if (res < 0)
1762 return posix_error_with_allocated_filename(path);
1763 PyMem_Free(path);
1764 Py_INCREF(Py_None);
1765 return Py_None;
1766}
1767#endif /* HAVE_LCHFLAGS */
1768
Martin v. Löwis244edc82001-10-04 22:44:26 +00001769#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001771"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001773
1774static PyObject *
1775posix_chroot(PyObject *self, PyObject *args)
1776{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001777 return posix_1str(args, "et:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001778}
1779#endif
1780
Guido van Rossum21142a01999-01-08 21:05:37 +00001781#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001783"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001784force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001785
1786static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001787posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001788{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001789 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001790}
1791#endif /* HAVE_FSYNC */
1792
1793#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001794
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001795#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001796extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1797#endif
1798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001801force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001803
1804static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001805posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001806{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001807 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001808}
1809#endif /* HAVE_FDATASYNC */
1810
1811
Fredrik Lundh10723342000-07-10 16:38:09 +00001812#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001813PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001814"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001816
Barry Warsaw53699e91996-12-10 23:23:01 +00001817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001818posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001819{
Mark Hammondef8b6542001-05-13 08:04:26 +00001820 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001821 int uid, gid;
1822 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001823 if (!PyArg_ParseTuple(args, "etii:chown",
1824 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001825 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001826 return NULL;
1827 Py_BEGIN_ALLOW_THREADS
1828 res = chown(path, (uid_t) uid, (gid_t) gid);
1829 Py_END_ALLOW_THREADS
1830 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001831 return posix_error_with_allocated_filename(path);
1832 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001833 Py_INCREF(Py_None);
1834 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001835}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001836#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001837
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001838#ifdef HAVE_LCHOWN
1839PyDoc_STRVAR(posix_lchown__doc__,
1840"lchown(path, uid, gid)\n\n\
1841Change the owner and group id of path to the numeric uid and gid.\n\
1842This function will not follow symbolic links.");
1843
1844static PyObject *
1845posix_lchown(PyObject *self, PyObject *args)
1846{
1847 char *path = NULL;
1848 int uid, gid;
1849 int res;
1850 if (!PyArg_ParseTuple(args, "etii:lchown",
1851 Py_FileSystemDefaultEncoding, &path,
1852 &uid, &gid))
1853 return NULL;
1854 Py_BEGIN_ALLOW_THREADS
1855 res = lchown(path, (uid_t) uid, (gid_t) gid);
1856 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001857 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001858 return posix_error_with_allocated_filename(path);
1859 PyMem_Free(path);
1860 Py_INCREF(Py_None);
1861 return Py_None;
1862}
1863#endif /* HAVE_LCHOWN */
1864
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001865
Guido van Rossum36bc6801995-06-14 22:54:23 +00001866#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001868"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Barry Warsaw53699e91996-12-10 23:23:01 +00001871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001872posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001873{
1874 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001875 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001878#if defined(PYOS_OS2) && defined(PYCC_GCC)
1879 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001880#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001881 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001882#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001883 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001884 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885 return posix_error();
Neal Norwitz93c56822007-08-26 07:10:06 +00001886 return PyUnicode_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001887}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001888
1889PyDoc_STRVAR(posix_getcwdu__doc__,
1890"getcwdu() -> path\n\n\
1891Return a unicode string representing the current working directory.");
1892
1893static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001894posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001895{
1896 char buf[1026];
1897 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001898
1899#ifdef Py_WIN_WIDE_FILENAMES
Thomas Wouters477c8d52006-05-27 19:21:47 +00001900 DWORD len;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001901 if (unicode_file_names()) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001902 wchar_t wbuf[1026];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903 wchar_t *wbuf2 = wbuf;
1904 PyObject *resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001905 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
1907 /* If the buffer is large enough, len does not include the
1908 terminating \0. If the buffer is too small, len includes
1909 the space needed for the terminator. */
1910 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
1911 wbuf2 = malloc(len * sizeof(wchar_t));
1912 if (wbuf2)
1913 len = GetCurrentDirectoryW(len, wbuf2);
1914 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001915 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916 if (!wbuf2) {
1917 PyErr_NoMemory();
1918 return NULL;
1919 }
1920 if (!len) {
1921 if (wbuf2 != wbuf) free(wbuf2);
1922 return win32_error("getcwdu", NULL);
1923 }
1924 resobj = PyUnicode_FromWideChar(wbuf2, len);
1925 if (wbuf2 != wbuf) free(wbuf2);
1926 return resobj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001927 }
1928#endif
1929
1930 Py_BEGIN_ALLOW_THREADS
1931#if defined(PYOS_OS2) && defined(PYCC_GCC)
1932 res = _getcwd2(buf, sizeof buf);
1933#else
1934 res = getcwd(buf, sizeof buf);
1935#endif
1936 Py_END_ALLOW_THREADS
1937 if (res == NULL)
1938 return posix_error();
1939 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1940}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001941#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001943
Guido van Rossumb6775db1994-08-01 11:34:53 +00001944#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001946"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001948
Barry Warsaw53699e91996-12-10 23:23:01 +00001949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001950posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001951{
Thomas Wouters477c8d52006-05-27 19:21:47 +00001952 return posix_2str(args, "etet:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001953}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001954#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001958"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959Return a list containing the names of the entries in the directory.\n\
1960\n\
1961 path: path of directory to list\n\
1962\n\
1963The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001964entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001965
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001967posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001968{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001969 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001970 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001971#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001972
Barry Warsaw53699e91996-12-10 23:23:01 +00001973 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001974 HANDLE hFindFile;
Martin v. Löwise920f0d2006-03-07 23:59:33 +00001975 BOOL result;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976 WIN32_FIND_DATA FileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
Mark Hammondef8b6542001-05-13 08:04:26 +00001978 char *bufptr = namebuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001979 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001980
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#ifdef Py_WIN_WIDE_FILENAMES
1982 /* If on wide-character-capable OS see if argument
1983 is Unicode and if so use wide API. */
1984 if (unicode_file_names()) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001985 PyObject *po;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001986 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1987 WIN32_FIND_DATAW wFileData;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001988 Py_UNICODE *wnamebuf;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001989 Py_UNICODE wch;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990 /* Overallocate for \\*.*\0 */
1991 len = PyUnicode_GET_SIZE(po);
1992 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
1993 if (!wnamebuf) {
1994 PyErr_NoMemory();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001996 }
1997 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
1998 wch = len > 0 ? wnamebuf[len-1] : '\0';
1999 if (wch != L'/' && wch != L'\\' && wch != L':')
2000 wnamebuf[len++] = L'\\';
2001 wcscpy(wnamebuf + len, L"*.*");
2002 if ((d = PyList_New(0)) == NULL) {
2003 free(wnamebuf);
2004 return NULL;
2005 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2007 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002008 int error = GetLastError();
2009 if (error == ERROR_FILE_NOT_FOUND) {
2010 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002011 return d;
2012 }
2013 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002014 win32_error_unicode("FindFirstFileW", wnamebuf);
2015 free(wnamebuf);
2016 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002017 }
2018 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002019 /* Skip over . and .. */
2020 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2021 wcscmp(wFileData.cFileName, L"..") != 0) {
2022 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2023 if (v == NULL) {
2024 Py_DECREF(d);
2025 d = NULL;
2026 break;
2027 }
2028 if (PyList_Append(d, v) != 0) {
2029 Py_DECREF(v);
2030 Py_DECREF(d);
2031 d = NULL;
2032 break;
2033 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034 Py_DECREF(v);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002035 }
Georg Brandl622927b2006-03-07 12:48:03 +00002036 Py_BEGIN_ALLOW_THREADS
2037 result = FindNextFileW(hFindFile, &wFileData);
2038 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002039 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2040 it got to the end of the directory. */
2041 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2042 Py_DECREF(d);
2043 win32_error_unicode("FindNextFileW", wnamebuf);
2044 FindClose(hFindFile);
2045 free(wnamebuf);
2046 return NULL;
2047 }
Georg Brandl622927b2006-03-07 12:48:03 +00002048 } while (result == TRUE);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002049
2050 if (FindClose(hFindFile) == FALSE) {
2051 Py_DECREF(d);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002052 win32_error_unicode("FindClose", wnamebuf);
2053 free(wnamebuf);
2054 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002055 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002056 free(wnamebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002057 return d;
2058 }
2059 /* Drop the argument parsing error as narrow strings
2060 are also valid. */
2061 PyErr_Clear();
2062 }
2063#endif
2064
Tim Peters5aa91602002-01-30 05:46:57 +00002065 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002066 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00002067 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00002068 if (len > 0) {
2069 char ch = namebuf[len-1];
2070 if (ch != SEP && ch != ALTSEP && ch != ':')
2071 namebuf[len++] = '/';
2072 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002073 strcpy(namebuf + len, "*.*");
2074
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002076 return NULL;
2077
2078 hFindFile = FindFirstFile(namebuf, &FileData);
2079 if (hFindFile == INVALID_HANDLE_VALUE) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080 int error = GetLastError();
2081 if (error == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002082 return d;
2083 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002084 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085 }
2086 do {
Martin v. Löwise920f0d2006-03-07 23:59:33 +00002087 /* Skip over . and .. */
2088 if (strcmp(FileData.cFileName, ".") != 0 &&
2089 strcmp(FileData.cFileName, "..") != 0) {
2090 v = PyString_FromString(FileData.cFileName);
2091 if (v == NULL) {
2092 Py_DECREF(d);
2093 d = NULL;
2094 break;
2095 }
2096 if (PyList_Append(d, v) != 0) {
2097 Py_DECREF(v);
2098 Py_DECREF(d);
2099 d = NULL;
2100 break;
2101 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103 }
Georg Brandl622927b2006-03-07 12:48:03 +00002104 Py_BEGIN_ALLOW_THREADS
2105 result = FindNextFile(hFindFile, &FileData);
2106 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002107 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2108 it got to the end of the directory. */
2109 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2110 Py_DECREF(d);
2111 win32_error("FindNextFile", namebuf);
2112 FindClose(hFindFile);
2113 return NULL;
2114 }
Georg Brandl622927b2006-03-07 12:48:03 +00002115 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002116
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002117 if (FindClose(hFindFile) == FALSE) {
2118 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00002119 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002120 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002121
2122 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002123
Tim Peters0bb44a42000-09-15 07:44:49 +00002124#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002125
2126#ifndef MAX_PATH
2127#define MAX_PATH CCHMAXPATH
2128#endif
2129 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002130 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002131 PyObject *d, *v;
2132 char namebuf[MAX_PATH+5];
2133 HDIR hdir = 1;
2134 ULONG srchcnt = 1;
2135 FILEFINDBUF3 ep;
2136 APIRET rc;
2137
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00002138 if (!PyArg_ParseTuple(args, "et#:listdir",
2139 Py_FileSystemDefaultEncoding, &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002140 return NULL;
2141 if (len >= MAX_PATH) {
2142 PyErr_SetString(PyExc_ValueError, "path too long");
2143 return NULL;
2144 }
2145 strcpy(namebuf, name);
2146 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002147 if (*pt == ALTSEP)
2148 *pt = SEP;
2149 if (namebuf[len-1] != SEP)
2150 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002151 strcpy(namebuf + len, "*.*");
2152
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002153 if ((d = PyList_New(0)) == NULL) {
2154 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002155 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002156 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002157
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002158 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2159 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002160 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002161 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2162 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2163 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002164
2165 if (rc != NO_ERROR) {
2166 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00002167 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002168 }
2169
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002170 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002171 do {
2172 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002173 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002174 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002175
2176 strcpy(namebuf, ep.achName);
2177
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002178 /* Leave Case of Name Alone -- In Native Form */
2179 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002180
2181 v = PyString_FromString(namebuf);
2182 if (v == NULL) {
2183 Py_DECREF(d);
2184 d = NULL;
2185 break;
2186 }
2187 if (PyList_Append(d, v) != 0) {
2188 Py_DECREF(v);
2189 Py_DECREF(d);
2190 d = NULL;
2191 break;
2192 }
2193 Py_DECREF(v);
2194 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2195 }
2196
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002197 PyMem_Free(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002198 return d;
2199#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002200
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002201 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002202 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002203 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002204 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00002205 int arg_is_unicode = 1;
2206
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002207 errno = 0;
Just van Rossum96b1c902003-03-03 17:32:15 +00002208 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2209 arg_is_unicode = 0;
2210 PyErr_Clear();
2211 }
2212 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002213 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002214 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002215 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00002216 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002217 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002218 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002219 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002220 return NULL;
2221 }
Georg Brandl622927b2006-03-07 12:48:03 +00002222 for (;;) {
2223 Py_BEGIN_ALLOW_THREADS
2224 ep = readdir(dirp);
2225 Py_END_ALLOW_THREADS
2226 if (ep == NULL)
2227 break;
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002228 if (ep->d_name[0] == '.' &&
2229 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00002230 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00002231 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00002232 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002233 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002234 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002235 d = NULL;
2236 break;
2237 }
Just van Rossum96b1c902003-03-03 17:32:15 +00002238 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00002239 PyObject *w;
2240
2241 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00002242 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00002243 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00002244 if (w != NULL) {
2245 Py_DECREF(v);
2246 v = w;
2247 }
2248 else {
2249 /* fall back to the original byte string, as
2250 discussed in patch #683592 */
2251 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00002252 }
Just van Rossum46c97842003-02-25 21:42:15 +00002253 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002254 if (PyList_Append(d, v) != 0) {
2255 Py_DECREF(v);
2256 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002257 d = NULL;
2258 break;
2259 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002260 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002262 if (errno != 0 && d != NULL) {
2263 /* readdir() returned NULL and set errno */
2264 closedir(dirp);
2265 Py_DECREF(d);
2266 return posix_error_with_allocated_filename(name);
2267 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002268 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00002269 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002270
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002271 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002272
Tim Peters0bb44a42000-09-15 07:44:49 +00002273#endif /* which OS */
2274} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002275
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002276#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002277/* A helper function for abspath on win32 */
2278static PyObject *
2279posix__getfullpathname(PyObject *self, PyObject *args)
2280{
2281 /* assume encoded strings wont more than double no of chars */
2282 char inbuf[MAX_PATH*2];
2283 char *inbufp = inbuf;
Tim Peters67d70eb2006-03-01 04:35:45 +00002284 Py_ssize_t insize = sizeof(inbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002285 char outbuf[MAX_PATH*2];
2286 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002287#ifdef Py_WIN_WIDE_FILENAMES
2288 if (unicode_file_names()) {
2289 PyUnicodeObject *po;
2290 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2291 Py_UNICODE woutbuf[MAX_PATH*2];
2292 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00002293 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002294 sizeof(woutbuf)/sizeof(woutbuf[0]),
2295 woutbuf, &wtemp))
2296 return win32_error("GetFullPathName", "");
2297 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
2298 }
2299 /* Drop the argument parsing error as narrow strings
2300 are also valid. */
2301 PyErr_Clear();
2302 }
2303#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002304 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
2305 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00002306 &insize))
2307 return NULL;
2308 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
2309 outbuf, &temp))
2310 return win32_error("GetFullPathName", inbuf);
Martin v. Löwis969297f2004-06-15 18:49:58 +00002311 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2312 return PyUnicode_Decode(outbuf, strlen(outbuf),
2313 Py_FileSystemDefaultEncoding, NULL);
2314 }
Mark Hammondef8b6542001-05-13 08:04:26 +00002315 return PyString_FromString(outbuf);
2316} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002317#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002319PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002320"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002321Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002322
Barry Warsaw53699e91996-12-10 23:23:01 +00002323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002324posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002325{
Guido van Rossumb0824db1996-02-25 04:50:32 +00002326 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00002327 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002328 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002329
2330#ifdef Py_WIN_WIDE_FILENAMES
2331 if (unicode_file_names()) {
2332 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00002333 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002334 Py_BEGIN_ALLOW_THREADS
2335 /* PyUnicode_AS_UNICODE OK without thread lock as
2336 it is a simple dereference. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002337 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002338 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002339 if (!res)
2340 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002341 Py_INCREF(Py_None);
2342 return Py_None;
2343 }
2344 /* Drop the argument parsing error as narrow strings
2345 are also valid. */
2346 PyErr_Clear();
2347 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002348 if (!PyArg_ParseTuple(args, "et|i:mkdir",
2349 Py_FileSystemDefaultEncoding, &path, &mode))
2350 return NULL;
2351 Py_BEGIN_ALLOW_THREADS
2352 /* PyUnicode_AS_UNICODE OK without thread lock as
2353 it is a simple dereference. */
2354 res = CreateDirectoryA(path, NULL);
2355 Py_END_ALLOW_THREADS
2356 if (!res) {
2357 win32_error("mkdir", path);
2358 PyMem_Free(path);
2359 return NULL;
2360 }
2361 PyMem_Free(path);
2362 Py_INCREF(Py_None);
2363 return Py_None;
2364#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002365
Tim Peters5aa91602002-01-30 05:46:57 +00002366 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00002367 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00002368 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002369 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002370#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002371 res = mkdir(path);
2372#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00002373 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002374#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002375 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00002376 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00002377 return posix_error_with_allocated_filename(path);
2378 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002379 Py_INCREF(Py_None);
2380 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002381#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002382}
2383
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002384
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002385/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2386#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002387#include <sys/resource.h>
2388#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002389
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002390
2391#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002392PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002393"nice(inc) -> new_priority\n\n\
2394Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002395
Barry Warsaw53699e91996-12-10 23:23:01 +00002396static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002397posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002398{
2399 int increment, value;
2400
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002401 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00002402 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002403
2404 /* There are two flavours of 'nice': one that returns the new
2405 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002406 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2407 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002408
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002409 If we are of the nice family that returns the new priority, we
2410 need to clear errno before the call, and check if errno is filled
2411 before calling posix_error() on a returnvalue of -1, because the
2412 -1 may be the actual new priority! */
2413
2414 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002415 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002416#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002417 if (value == 0)
2418 value = getpriority(PRIO_PROCESS, 0);
2419#endif
2420 if (value == -1 && errno != 0)
2421 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00002422 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002423 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002424}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002425#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002427PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002428"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002429Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002430
Barry Warsaw53699e91996-12-10 23:23:01 +00002431static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002432posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002433{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002434#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002435 PyObject *o1, *o2;
2436 char *p1, *p2;
2437 BOOL result;
2438 if (unicode_file_names()) {
2439 if (!PyArg_ParseTuple(args, "O&O&:rename",
2440 convert_to_unicode, &o1,
2441 convert_to_unicode, &o2))
2442 PyErr_Clear();
2443 else {
2444 Py_BEGIN_ALLOW_THREADS
2445 result = MoveFileW(PyUnicode_AsUnicode(o1),
2446 PyUnicode_AsUnicode(o2));
2447 Py_END_ALLOW_THREADS
2448 Py_DECREF(o1);
2449 Py_DECREF(o2);
2450 if (!result)
2451 return win32_error("rename", NULL);
2452 Py_INCREF(Py_None);
2453 return Py_None;
2454 }
2455 }
2456 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2457 return NULL;
2458 Py_BEGIN_ALLOW_THREADS
2459 result = MoveFileA(p1, p2);
2460 Py_END_ALLOW_THREADS
2461 if (!result)
2462 return win32_error("rename", NULL);
2463 Py_INCREF(Py_None);
2464 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002465#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002466 return posix_2str(args, "etet:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002467#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002468}
2469
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002470
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002471PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002472"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002473Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002474
Barry Warsaw53699e91996-12-10 23:23:01 +00002475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002476posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002477{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002478#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002479 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002480#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002481 return posix_1str(args, "et:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002482#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002483}
2484
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002486PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002487"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002488Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002489
Barry Warsaw53699e91996-12-10 23:23:01 +00002490static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002491posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002492{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002493#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00002494 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002495#else
2496 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
2497#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002498}
2499
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002500
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002501#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002502PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002503"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002504Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002505
Barry Warsaw53699e91996-12-10 23:23:01 +00002506static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002507posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002508{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002509 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002510 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002511 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002512 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002513 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002514 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00002515 Py_END_ALLOW_THREADS
2516 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002517}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002518#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002519
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002521PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002522"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002523Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002524
Barry Warsaw53699e91996-12-10 23:23:01 +00002525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002526posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002527{
2528 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002529 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002530 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00002531 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002532 if (i < 0)
2533 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002534 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002535}
2536
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002539"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002540Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002543"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002544Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002545
Barry Warsaw53699e91996-12-10 23:23:01 +00002546static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002547posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002548{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002549#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002550 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002551#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002552 return posix_1str(args, "et:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002553#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002554}
2555
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002556
Guido van Rossumb6775db1994-08-01 11:34:53 +00002557#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002558PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002559"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002560Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002561
Barry Warsaw53699e91996-12-10 23:23:01 +00002562static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002563posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002564{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002565 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002566 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002567
Barry Warsaw53699e91996-12-10 23:23:01 +00002568 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002569 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00002570 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002571 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002572 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002573 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002574 u.sysname,
2575 u.nodename,
2576 u.release,
2577 u.version,
2578 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002579}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002580#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002581
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002582static int
2583extract_time(PyObject *t, long* sec, long* usec)
2584{
2585 long intval;
2586 if (PyFloat_Check(t)) {
2587 double tval = PyFloat_AsDouble(t);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002588 PyObject *intobj = Py_Type(t)->tp_as_number->nb_int(t);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002589 if (!intobj)
2590 return -1;
2591 intval = PyInt_AsLong(intobj);
2592 Py_DECREF(intobj);
Michael W. Hudsonb8963812005-07-05 15:21:58 +00002593 if (intval == -1 && PyErr_Occurred())
2594 return -1;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002595 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00002596 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002597 if (*usec < 0)
2598 /* If rounding gave us a negative number,
2599 truncate. */
2600 *usec = 0;
2601 return 0;
2602 }
2603 intval = PyInt_AsLong(t);
2604 if (intval == -1 && PyErr_Occurred())
2605 return -1;
2606 *sec = intval;
2607 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002608 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002609}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002612"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002613utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002614Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002615second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002616
Barry Warsaw53699e91996-12-10 23:23:01 +00002617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002618posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002619{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002620#ifdef Py_WIN_WIDE_FILENAMES
2621 PyObject *arg;
2622 PyUnicodeObject *obwpath;
2623 wchar_t *wpath = NULL;
2624 char *apath = NULL;
2625 HANDLE hFile;
2626 long atimesec, mtimesec, ausec, musec;
2627 FILETIME atime, mtime;
2628 PyObject *result = NULL;
2629
2630 if (unicode_file_names()) {
2631 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2632 wpath = PyUnicode_AS_UNICODE(obwpath);
2633 Py_BEGIN_ALLOW_THREADS
2634 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002635 NULL, OPEN_EXISTING,
2636 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002637 Py_END_ALLOW_THREADS
2638 if (hFile == INVALID_HANDLE_VALUE)
2639 return win32_error_unicode("utime", wpath);
2640 } else
2641 /* Drop the argument parsing error as narrow strings
2642 are also valid. */
2643 PyErr_Clear();
2644 }
2645 if (!wpath) {
2646 if (!PyArg_ParseTuple(args, "etO:utime",
2647 Py_FileSystemDefaultEncoding, &apath, &arg))
2648 return NULL;
2649 Py_BEGIN_ALLOW_THREADS
2650 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002651 NULL, OPEN_EXISTING,
2652 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002653 Py_END_ALLOW_THREADS
2654 if (hFile == INVALID_HANDLE_VALUE) {
2655 win32_error("utime", apath);
2656 PyMem_Free(apath);
2657 return NULL;
2658 }
2659 PyMem_Free(apath);
2660 }
2661
2662 if (arg == Py_None) {
2663 SYSTEMTIME now;
2664 GetSystemTime(&now);
2665 if (!SystemTimeToFileTime(&now, &mtime) ||
2666 !SystemTimeToFileTime(&now, &atime)) {
2667 win32_error("utime", NULL);
2668 goto done;
2669 }
2670 }
2671 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2672 PyErr_SetString(PyExc_TypeError,
2673 "utime() arg 2 must be a tuple (atime, mtime)");
2674 goto done;
2675 }
2676 else {
2677 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2678 &atimesec, &ausec) == -1)
2679 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002680 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002681 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2682 &mtimesec, &musec) == -1)
2683 goto done;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002684 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002685 }
2686 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2687 /* Avoid putting the file name into the error here,
2688 as that may confuse the user into believing that
2689 something is wrong with the file, when it also
2690 could be the time stamp that gives a problem. */
2691 win32_error("utime", NULL);
2692 }
2693 Py_INCREF(Py_None);
2694 result = Py_None;
2695done:
2696 CloseHandle(hFile);
2697 return result;
2698#else /* Py_WIN_WIDE_FILENAMES */
2699
Neal Norwitz2adf2102004-06-09 01:46:02 +00002700 char *path = NULL;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002701 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002702 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002703 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002704
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002705#if defined(HAVE_UTIMES)
2706 struct timeval buf[2];
2707#define ATIME buf[0].tv_sec
2708#define MTIME buf[1].tv_sec
2709#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002710/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002711 struct utimbuf buf;
2712#define ATIME buf.actime
2713#define MTIME buf.modtime
2714#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002715#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002716 time_t buf[2];
2717#define ATIME buf[0]
2718#define MTIME buf[1]
2719#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002720#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002721
Mark Hammond817c9292003-12-03 01:22:38 +00002722
Thomas Wouters477c8d52006-05-27 19:21:47 +00002723 if (!PyArg_ParseTuple(args, "etO:utime",
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00002724 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002725 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00002726 if (arg == Py_None) {
2727 /* optional time values not given */
2728 Py_BEGIN_ALLOW_THREADS
2729 res = utime(path, NULL);
2730 Py_END_ALLOW_THREADS
2731 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002732 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00002733 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002734 "utime() arg 2 must be a tuple (atime, mtime)");
Neal Norwitz96652712004-06-06 20:40:27 +00002735 PyMem_Free(path);
Barry Warsaw3cef8562000-05-01 16:17:24 +00002736 return NULL;
2737 }
2738 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002739 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Neal Norwitz96652712004-06-06 20:40:27 +00002740 &atime, &ausec) == -1) {
2741 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002742 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002743 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002744 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Neal Norwitz96652712004-06-06 20:40:27 +00002745 &mtime, &musec) == -1) {
2746 PyMem_Free(path);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002747 return NULL;
Neal Norwitz96652712004-06-06 20:40:27 +00002748 }
Barry Warsaw3cef8562000-05-01 16:17:24 +00002749 ATIME = atime;
2750 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002751#ifdef HAVE_UTIMES
2752 buf[0].tv_usec = ausec;
2753 buf[1].tv_usec = musec;
2754 Py_BEGIN_ALLOW_THREADS
2755 res = utimes(path, buf);
2756 Py_END_ALLOW_THREADS
2757#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002758 Py_BEGIN_ALLOW_THREADS
2759 res = utime(path, UTIME_ARG);
2760 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002761#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002762 }
Mark Hammond2d5914b2004-05-04 08:10:37 +00002763 if (res < 0) {
Neal Norwitz96652712004-06-06 20:40:27 +00002764 return posix_error_with_allocated_filename(path);
Mark Hammond2d5914b2004-05-04 08:10:37 +00002765 }
Neal Norwitz96652712004-06-06 20:40:27 +00002766 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002767 Py_INCREF(Py_None);
2768 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002769#undef UTIME_ARG
2770#undef ATIME
2771#undef MTIME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002772#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002773}
2774
Guido van Rossum85e3b011991-06-03 12:42:10 +00002775
Guido van Rossum3b066191991-06-04 19:40:25 +00002776/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002778PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002779"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002780Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002781
Barry Warsaw53699e91996-12-10 23:23:01 +00002782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002783posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002784{
2785 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002786 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002787 return NULL;
2788 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002789 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002790}
2791
Martin v. Löwis114619e2002-10-07 06:44:21 +00002792#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2793static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002794free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002795{
Martin v. Löwis725507b2006-03-07 12:08:51 +00002796 Py_ssize_t i;
Martin v. Löwis114619e2002-10-07 06:44:21 +00002797 for (i = 0; i < count; i++)
2798 PyMem_Free(array[i]);
2799 PyMem_DEL(array);
2800}
2801#endif
2802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002803
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002804#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002805PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002806"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002807Execute an executable path with arguments, replacing current process.\n\
2808\n\
2809 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002810 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002811
Barry Warsaw53699e91996-12-10 23:23:01 +00002812static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002813posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002814{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002815 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002816 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002817 char **argvlist;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002818 Py_ssize_t i, argc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002819 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002820
Guido van Rossum89b33251993-10-22 14:26:06 +00002821 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002822 argv is a list or tuple of strings. */
2823
Martin v. Löwis114619e2002-10-07 06:44:21 +00002824 if (!PyArg_ParseTuple(args, "etO:execv",
2825 Py_FileSystemDefaultEncoding,
2826 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002827 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002828 if (PyList_Check(argv)) {
2829 argc = PyList_Size(argv);
2830 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002831 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002832 else if (PyTuple_Check(argv)) {
2833 argc = PyTuple_Size(argv);
2834 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002835 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002836 else {
Fred Drake661ea262000-10-24 19:57:45 +00002837 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002838 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002839 return NULL;
2840 }
Thomas Heller6790d602007-08-30 17:15:14 +00002841 if (argc < 1) {
2842 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
2843 PyMem_Free(path);
2844 return NULL;
2845 }
Guido van Rossum50422b42000-04-26 20:34:28 +00002846
Barry Warsaw53699e91996-12-10 23:23:01 +00002847 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002848 if (argvlist == NULL) {
2849 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002850 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002851 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002852 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002853 if (!PyArg_Parse((*getitem)(argv, i), "et",
2854 Py_FileSystemDefaultEncoding,
2855 &argvlist[i])) {
2856 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002857 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002858 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002859 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002860 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002861
Guido van Rossum85e3b011991-06-03 12:42:10 +00002862 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002863 }
2864 argvlist[argc] = NULL;
2865
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002866 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002867
Guido van Rossum85e3b011991-06-03 12:42:10 +00002868 /* If we get here it's definitely an error */
2869
Martin v. Löwis114619e2002-10-07 06:44:21 +00002870 free_string_array(argvlist, argc);
2871 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002872 return posix_error();
2873}
2874
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002876PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002877"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002878Execute a path with arguments and environment, replacing current process.\n\
2879\n\
2880 path: path of executable file\n\
2881 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002882 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002883
Barry Warsaw53699e91996-12-10 23:23:01 +00002884static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002885posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002886{
2887 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002888 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002889 char **argvlist;
2890 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002891 PyObject *key, *val, *keys=NULL, *vals=NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002892 Py_ssize_t i, pos, argc, envc;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002893 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002894 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002895
2896 /* execve has three arguments: (path, argv, env), where
2897 argv is a list or tuple of strings and env is a dictionary
2898 like posix.environ. */
2899
Martin v. Löwis114619e2002-10-07 06:44:21 +00002900 if (!PyArg_ParseTuple(args, "etOO:execve",
2901 Py_FileSystemDefaultEncoding,
2902 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002903 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002904 if (PyList_Check(argv)) {
2905 argc = PyList_Size(argv);
2906 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002907 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002908 else if (PyTuple_Check(argv)) {
2909 argc = PyTuple_Size(argv);
2910 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002911 }
2912 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002913 PyErr_SetString(PyExc_TypeError,
2914 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002915 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002916 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002917 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002918 PyErr_SetString(PyExc_TypeError,
2919 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002920 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002921 }
2922
Barry Warsaw53699e91996-12-10 23:23:01 +00002923 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002924 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002925 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002926 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002927 }
2928 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002929 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002930 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002931 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002932 &argvlist[i]))
2933 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002934 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002935 goto fail_1;
2936 }
2937 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002938 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002939 argvlist[argc] = NULL;
2940
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002941 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002942 if (i < 0)
2943 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002944 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002945 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002946 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002947 goto fail_1;
2948 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002949 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002950 keys = PyMapping_Keys(env);
2951 vals = PyMapping_Values(env);
2952 if (!keys || !vals)
2953 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002954 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2955 PyErr_SetString(PyExc_TypeError,
2956 "execve(): env.keys() or env.values() is not a list");
2957 goto fail_2;
2958 }
Tim Peters5aa91602002-01-30 05:46:57 +00002959
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002960 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002961 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002962 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002963
2964 key = PyList_GetItem(keys, pos);
2965 val = PyList_GetItem(vals, pos);
2966 if (!key || !val)
2967 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002968
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002969 if (!PyArg_Parse(
2970 key,
2971 "s;execve() arg 3 contains a non-string key",
2972 &k) ||
2973 !PyArg_Parse(
2974 val,
2975 "s;execve() arg 3 contains a non-string value",
2976 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002977 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002978 goto fail_2;
2979 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002980
2981#if defined(PYOS_OS2)
2982 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2983 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2984#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002985 len = PyString_Size(key) + PyString_Size(val) + 2;
2986 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002987 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002988 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002989 goto fail_2;
2990 }
Tim Petersc8996f52001-12-03 20:41:00 +00002991 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002992 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002993#if defined(PYOS_OS2)
2994 }
2995#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002996 }
2997 envlist[envc] = 0;
2998
2999 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003000
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003001 /* If we get here it's definitely an error */
3002
3003 (void) posix_error();
3004
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003005 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003006 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00003007 PyMem_DEL(envlist[envc]);
3008 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003009 fail_1:
3010 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00003011 Py_XDECREF(vals);
3012 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003013 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003014 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003015 return NULL;
3016}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003017#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003018
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003019
Guido van Rossuma1065681999-01-25 23:20:23 +00003020#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003021PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003022"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003023Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003024\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003025 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003026 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003027 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003028
3029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003030posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003031{
3032 char *path;
3033 PyObject *argv;
3034 char **argvlist;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003035 int mode, i;
3036 Py_ssize_t argc;
Tim Peters79248aa2001-08-29 21:37:10 +00003037 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003038 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003039
3040 /* spawnv has three arguments: (mode, path, argv), where
3041 argv is a list or tuple of strings. */
3042
Martin v. Löwis114619e2002-10-07 06:44:21 +00003043 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
3044 Py_FileSystemDefaultEncoding,
3045 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00003046 return NULL;
3047 if (PyList_Check(argv)) {
3048 argc = PyList_Size(argv);
3049 getitem = PyList_GetItem;
3050 }
3051 else if (PyTuple_Check(argv)) {
3052 argc = PyTuple_Size(argv);
3053 getitem = PyTuple_GetItem;
3054 }
3055 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003056 PyErr_SetString(PyExc_TypeError,
3057 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003058 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003059 return NULL;
3060 }
3061
3062 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003063 if (argvlist == NULL) {
3064 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00003065 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003066 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003067 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003068 if (!PyArg_Parse((*getitem)(argv, i), "et",
3069 Py_FileSystemDefaultEncoding,
3070 &argvlist[i])) {
3071 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003072 PyErr_SetString(
3073 PyExc_TypeError,
3074 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003075 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00003076 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003077 }
3078 }
3079 argvlist[argc] = NULL;
3080
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003081#if defined(PYOS_OS2) && defined(PYCC_GCC)
3082 Py_BEGIN_ALLOW_THREADS
3083 spawnval = spawnv(mode, path, argvlist);
3084 Py_END_ALLOW_THREADS
3085#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003086 if (mode == _OLD_P_OVERLAY)
3087 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003088
Tim Peters25059d32001-12-07 20:35:43 +00003089 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003090 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00003091 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003092#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003093
Martin v. Löwis114619e2002-10-07 06:44:21 +00003094 free_string_array(argvlist, argc);
3095 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003096
Fred Drake699f3522000-06-29 21:12:41 +00003097 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003098 return posix_error();
3099 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003100#if SIZEOF_LONG == SIZEOF_VOID_P
3101 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003102#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003103 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003104#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003105}
3106
3107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003109"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003110Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003111\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00003112 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003113 path: path of executable file\n\
3114 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003115 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003116
3117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003118posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003119{
3120 char *path;
3121 PyObject *argv, *env;
3122 char **argvlist;
3123 char **envlist;
3124 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003125 int mode, pos, envc;
3126 Py_ssize_t argc, i;
Tim Peters79248aa2001-08-29 21:37:10 +00003127 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003128 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003129 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003130
3131 /* spawnve has four arguments: (mode, path, argv, env), where
3132 argv is a list or tuple of strings and env is a dictionary
3133 like posix.environ. */
3134
Martin v. Löwis114619e2002-10-07 06:44:21 +00003135 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
3136 Py_FileSystemDefaultEncoding,
3137 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00003138 return NULL;
3139 if (PyList_Check(argv)) {
3140 argc = PyList_Size(argv);
3141 getitem = PyList_GetItem;
3142 }
3143 else if (PyTuple_Check(argv)) {
3144 argc = PyTuple_Size(argv);
3145 getitem = PyTuple_GetItem;
3146 }
3147 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003148 PyErr_SetString(PyExc_TypeError,
3149 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003150 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003151 }
3152 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003153 PyErr_SetString(PyExc_TypeError,
3154 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00003155 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003156 }
3157
3158 argvlist = PyMem_NEW(char *, argc+1);
3159 if (argvlist == NULL) {
3160 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00003161 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003162 }
3163 for (i = 0; i < argc; i++) {
3164 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003165 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00003166 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00003167 &argvlist[i]))
3168 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00003169 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00003170 goto fail_1;
3171 }
3172 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00003173 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00003174 argvlist[argc] = NULL;
3175
Jeremy Hylton03657cf2000-07-12 13:05:33 +00003176 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003177 if (i < 0)
3178 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003179 envlist = PyMem_NEW(char *, i + 1);
3180 if (envlist == NULL) {
3181 PyErr_NoMemory();
3182 goto fail_1;
3183 }
3184 envc = 0;
3185 keys = PyMapping_Keys(env);
3186 vals = PyMapping_Values(env);
3187 if (!keys || !vals)
3188 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003189 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3190 PyErr_SetString(PyExc_TypeError,
3191 "spawnve(): env.keys() or env.values() is not a list");
3192 goto fail_2;
3193 }
Tim Peters5aa91602002-01-30 05:46:57 +00003194
Guido van Rossuma1065681999-01-25 23:20:23 +00003195 for (pos = 0; pos < i; pos++) {
3196 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00003197 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00003198
3199 key = PyList_GetItem(keys, pos);
3200 val = PyList_GetItem(vals, pos);
3201 if (!key || !val)
3202 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00003203
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003204 if (!PyArg_Parse(
3205 key,
3206 "s;spawnve() arg 3 contains a non-string key",
3207 &k) ||
3208 !PyArg_Parse(
3209 val,
3210 "s;spawnve() arg 3 contains a non-string value",
3211 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00003212 {
3213 goto fail_2;
3214 }
Tim Petersc8996f52001-12-03 20:41:00 +00003215 len = PyString_Size(key) + PyString_Size(val) + 2;
3216 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00003217 if (p == NULL) {
3218 PyErr_NoMemory();
3219 goto fail_2;
3220 }
Tim Petersc8996f52001-12-03 20:41:00 +00003221 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00003222 envlist[envc++] = p;
3223 }
3224 envlist[envc] = 0;
3225
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003226#if defined(PYOS_OS2) && defined(PYCC_GCC)
3227 Py_BEGIN_ALLOW_THREADS
3228 spawnval = spawnve(mode, path, argvlist, envlist);
3229 Py_END_ALLOW_THREADS
3230#else
Guido van Rossum246bc171999-02-01 23:54:31 +00003231 if (mode == _OLD_P_OVERLAY)
3232 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003233
3234 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003235 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00003236 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003237#endif
Tim Peters25059d32001-12-07 20:35:43 +00003238
Fred Drake699f3522000-06-29 21:12:41 +00003239 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00003240 (void) posix_error();
3241 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003242#if SIZEOF_LONG == SIZEOF_VOID_P
3243 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003244#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00003245 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003246#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003247
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003248 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00003249 while (--envc >= 0)
3250 PyMem_DEL(envlist[envc]);
3251 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003252 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00003253 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00003254 Py_XDECREF(vals);
3255 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003256 fail_0:
3257 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00003258 return res;
3259}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003260
3261/* OS/2 supports spawnvp & spawnvpe natively */
3262#if defined(PYOS_OS2)
3263PyDoc_STRVAR(posix_spawnvp__doc__,
3264"spawnvp(mode, file, args)\n\n\
3265Execute the program 'file' in a new process, using the environment\n\
3266search path to find the file.\n\
3267\n\
3268 mode: mode of process creation\n\
3269 file: executable file name\n\
3270 args: tuple or list of strings");
3271
3272static PyObject *
3273posix_spawnvp(PyObject *self, PyObject *args)
3274{
3275 char *path;
3276 PyObject *argv;
3277 char **argvlist;
3278 int mode, i, argc;
3279 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003280 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003281
3282 /* spawnvp has three arguments: (mode, path, argv), where
3283 argv is a list or tuple of strings. */
3284
3285 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
3286 Py_FileSystemDefaultEncoding,
3287 &path, &argv))
3288 return NULL;
3289 if (PyList_Check(argv)) {
3290 argc = PyList_Size(argv);
3291 getitem = PyList_GetItem;
3292 }
3293 else if (PyTuple_Check(argv)) {
3294 argc = PyTuple_Size(argv);
3295 getitem = PyTuple_GetItem;
3296 }
3297 else {
3298 PyErr_SetString(PyExc_TypeError,
3299 "spawnvp() arg 2 must be a tuple or list");
3300 PyMem_Free(path);
3301 return NULL;
3302 }
3303
3304 argvlist = PyMem_NEW(char *, argc+1);
3305 if (argvlist == NULL) {
3306 PyMem_Free(path);
3307 return PyErr_NoMemory();
3308 }
3309 for (i = 0; i < argc; i++) {
3310 if (!PyArg_Parse((*getitem)(argv, i), "et",
3311 Py_FileSystemDefaultEncoding,
3312 &argvlist[i])) {
3313 free_string_array(argvlist, i);
3314 PyErr_SetString(
3315 PyExc_TypeError,
3316 "spawnvp() arg 2 must contain only strings");
3317 PyMem_Free(path);
3318 return NULL;
3319 }
3320 }
3321 argvlist[argc] = NULL;
3322
3323 Py_BEGIN_ALLOW_THREADS
3324#if defined(PYCC_GCC)
3325 spawnval = spawnvp(mode, path, argvlist);
3326#else
3327 spawnval = _spawnvp(mode, path, argvlist);
3328#endif
3329 Py_END_ALLOW_THREADS
3330
3331 free_string_array(argvlist, argc);
3332 PyMem_Free(path);
3333
3334 if (spawnval == -1)
3335 return posix_error();
3336 else
3337 return Py_BuildValue("l", (long) spawnval);
3338}
3339
3340
3341PyDoc_STRVAR(posix_spawnvpe__doc__,
3342"spawnvpe(mode, file, args, env)\n\n\
3343Execute the program 'file' in a new process, using the environment\n\
3344search path to find the file.\n\
3345\n\
3346 mode: mode of process creation\n\
3347 file: executable file name\n\
3348 args: tuple or list of arguments\n\
3349 env: dictionary of strings mapping to strings");
3350
3351static PyObject *
3352posix_spawnvpe(PyObject *self, PyObject *args)
3353{
3354 char *path;
3355 PyObject *argv, *env;
3356 char **argvlist;
3357 char **envlist;
3358 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
3359 int mode, i, pos, argc, envc;
3360 Py_intptr_t spawnval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003361 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003362 int lastarg = 0;
3363
3364 /* spawnvpe has four arguments: (mode, path, argv, env), where
3365 argv is a list or tuple of strings and env is a dictionary
3366 like posix.environ. */
3367
3368 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3369 Py_FileSystemDefaultEncoding,
3370 &path, &argv, &env))
3371 return NULL;
3372 if (PyList_Check(argv)) {
3373 argc = PyList_Size(argv);
3374 getitem = PyList_GetItem;
3375 }
3376 else if (PyTuple_Check(argv)) {
3377 argc = PyTuple_Size(argv);
3378 getitem = PyTuple_GetItem;
3379 }
3380 else {
3381 PyErr_SetString(PyExc_TypeError,
3382 "spawnvpe() arg 2 must be a tuple or list");
3383 goto fail_0;
3384 }
3385 if (!PyMapping_Check(env)) {
3386 PyErr_SetString(PyExc_TypeError,
3387 "spawnvpe() arg 3 must be a mapping object");
3388 goto fail_0;
3389 }
3390
3391 argvlist = PyMem_NEW(char *, argc+1);
3392 if (argvlist == NULL) {
3393 PyErr_NoMemory();
3394 goto fail_0;
3395 }
3396 for (i = 0; i < argc; i++) {
3397 if (!PyArg_Parse((*getitem)(argv, i),
3398 "et;spawnvpe() arg 2 must contain only strings",
3399 Py_FileSystemDefaultEncoding,
3400 &argvlist[i]))
3401 {
3402 lastarg = i;
3403 goto fail_1;
3404 }
3405 }
3406 lastarg = argc;
3407 argvlist[argc] = NULL;
3408
3409 i = PyMapping_Size(env);
3410 if (i < 0)
3411 goto fail_1;
3412 envlist = PyMem_NEW(char *, i + 1);
3413 if (envlist == NULL) {
3414 PyErr_NoMemory();
3415 goto fail_1;
3416 }
3417 envc = 0;
3418 keys = PyMapping_Keys(env);
3419 vals = PyMapping_Values(env);
3420 if (!keys || !vals)
3421 goto fail_2;
3422 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3423 PyErr_SetString(PyExc_TypeError,
3424 "spawnvpe(): env.keys() or env.values() is not a list");
3425 goto fail_2;
3426 }
3427
3428 for (pos = 0; pos < i; pos++) {
3429 char *p, *k, *v;
3430 size_t len;
3431
3432 key = PyList_GetItem(keys, pos);
3433 val = PyList_GetItem(vals, pos);
3434 if (!key || !val)
3435 goto fail_2;
3436
3437 if (!PyArg_Parse(
3438 key,
3439 "s;spawnvpe() arg 3 contains a non-string key",
3440 &k) ||
3441 !PyArg_Parse(
3442 val,
3443 "s;spawnvpe() arg 3 contains a non-string value",
3444 &v))
3445 {
3446 goto fail_2;
3447 }
3448 len = PyString_Size(key) + PyString_Size(val) + 2;
3449 p = PyMem_NEW(char, len);
3450 if (p == NULL) {
3451 PyErr_NoMemory();
3452 goto fail_2;
3453 }
3454 PyOS_snprintf(p, len, "%s=%s", k, v);
3455 envlist[envc++] = p;
3456 }
3457 envlist[envc] = 0;
3458
3459 Py_BEGIN_ALLOW_THREADS
3460#if defined(PYCC_GCC)
3461 spawnval = spawnve(mode, path, argvlist, envlist);
3462#else
3463 spawnval = _spawnve(mode, path, argvlist, envlist);
3464#endif
3465 Py_END_ALLOW_THREADS
3466
3467 if (spawnval == -1)
3468 (void) posix_error();
3469 else
3470 res = Py_BuildValue("l", (long) spawnval);
3471
3472 fail_2:
3473 while (--envc >= 0)
3474 PyMem_DEL(envlist[envc]);
3475 PyMem_DEL(envlist);
3476 fail_1:
3477 free_string_array(argvlist, lastarg);
3478 Py_XDECREF(vals);
3479 Py_XDECREF(keys);
3480 fail_0:
3481 PyMem_Free(path);
3482 return res;
3483}
3484#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003485#endif /* HAVE_SPAWNV */
3486
3487
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003488#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003489PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003490"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003491Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3492\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003493Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003494
3495static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003496posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003497{
Neal Norwitze241ce82003-02-17 18:17:05 +00003498 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003499 if (pid == -1)
3500 return posix_error();
3501 PyOS_AfterFork();
3502 return PyInt_FromLong((long)pid);
3503}
3504#endif
3505
3506
Guido van Rossumad0ee831995-03-01 10:34:45 +00003507#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003508PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003509"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003510Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003511Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003512
Barry Warsaw53699e91996-12-10 23:23:01 +00003513static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003514posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003515{
Neal Norwitze241ce82003-02-17 18:17:05 +00003516 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003517 if (pid == -1)
3518 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003519 if (pid == 0)
3520 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00003521 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003522}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003523#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003524
Neal Norwitzb59798b2003-03-21 01:43:31 +00003525/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003526/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3527#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003528#define DEV_PTY_FILE "/dev/ptc"
3529#define HAVE_DEV_PTMX
3530#else
3531#define DEV_PTY_FILE "/dev/ptmx"
3532#endif
3533
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003534#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003535#ifdef HAVE_PTY_H
3536#include <pty.h>
3537#else
3538#ifdef HAVE_LIBUTIL_H
3539#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00003540#endif /* HAVE_LIBUTIL_H */
3541#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003542#ifdef HAVE_STROPTS_H
3543#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003544#endif
3545#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003546
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003547#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003548PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003549"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003550Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003551
3552static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003553posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003554{
3555 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003556#ifndef HAVE_OPENPTY
3557 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003558#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003559#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003560 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003561#ifdef sun
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003562 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003563#endif
3564#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003565
Thomas Wouters70c21a12000-07-14 14:28:33 +00003566#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00003567 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3568 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003569#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00003570 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3571 if (slave_name == NULL)
3572 return posix_error();
3573
3574 slave_fd = open(slave_name, O_RDWR);
3575 if (slave_fd < 0)
3576 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003577#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00003578 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003579 if (master_fd < 0)
3580 return posix_error();
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003581 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003582 /* change permission of slave */
3583 if (grantpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003584 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003585 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003586 }
3587 /* unlock slave */
3588 if (unlockpt(master_fd) < 0) {
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003589 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003590 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00003591 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +00003592 PyOS_setsig(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003593 slave_name = ptsname(master_fd); /* get name of slave */
3594 if (slave_name == NULL)
3595 return posix_error();
3596 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3597 if (slave_fd < 0)
3598 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003599#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003600 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3601 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003602#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003603 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003604#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003605#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003606#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003607
Fred Drake8cef4cf2000-06-28 16:40:38 +00003608 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003609
Fred Drake8cef4cf2000-06-28 16:40:38 +00003610}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003611#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003612
3613#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003614PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003615"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003616Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3617Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003618To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003619
3620static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003621posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003622{
Neal Norwitz24b3c222005-09-19 06:43:44 +00003623 int master_fd = -1, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003624
Fred Drake8cef4cf2000-06-28 16:40:38 +00003625 pid = forkpty(&master_fd, NULL, NULL, NULL);
3626 if (pid == -1)
3627 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00003628 if (pid == 0)
3629 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00003630 return Py_BuildValue("(ii)", pid, master_fd);
3631}
3632#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003633
Guido van Rossumad0ee831995-03-01 10:34:45 +00003634#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003635PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003636"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003637Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003638
Barry Warsaw53699e91996-12-10 23:23:01 +00003639static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003640posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003641{
Barry Warsaw53699e91996-12-10 23:23:01 +00003642 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003643}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003644#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003646
Guido van Rossumad0ee831995-03-01 10:34:45 +00003647#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003648PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003649"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003650Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003651
Barry Warsaw53699e91996-12-10 23:23:01 +00003652static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003653posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003654{
Barry Warsaw53699e91996-12-10 23:23:01 +00003655 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003656}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003657#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003658
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003659
Guido van Rossumad0ee831995-03-01 10:34:45 +00003660#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003661PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003662"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003663Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003664
Barry Warsaw53699e91996-12-10 23:23:01 +00003665static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003666posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003667{
Barry Warsaw53699e91996-12-10 23:23:01 +00003668 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003669}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003670#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003671
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003673PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003674"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003675Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003676
Barry Warsaw53699e91996-12-10 23:23:01 +00003677static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003678posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003679{
Barry Warsaw53699e91996-12-10 23:23:01 +00003680 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003681}
3682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003683
Fred Drakec9680921999-12-13 16:37:25 +00003684#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003685PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003686"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003687Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003688
3689static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003690posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003691{
3692 PyObject *result = NULL;
3693
Fred Drakec9680921999-12-13 16:37:25 +00003694#ifdef NGROUPS_MAX
3695#define MAX_GROUPS NGROUPS_MAX
3696#else
3697 /* defined to be 16 on Solaris7, so this should be a small number */
3698#define MAX_GROUPS 64
3699#endif
3700 gid_t grouplist[MAX_GROUPS];
3701 int n;
3702
3703 n = getgroups(MAX_GROUPS, grouplist);
3704 if (n < 0)
3705 posix_error();
3706 else {
3707 result = PyList_New(n);
3708 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00003709 int i;
3710 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00003711 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00003712 if (o == NULL) {
3713 Py_DECREF(result);
3714 result = NULL;
3715 break;
3716 }
3717 PyList_SET_ITEM(result, i, o);
3718 }
3719 }
3720 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003721
Fred Drakec9680921999-12-13 16:37:25 +00003722 return result;
3723}
3724#endif
3725
Martin v. Löwis606edc12002-06-13 21:09:11 +00003726#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003727PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003728"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003729Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003730
3731static PyObject *
3732posix_getpgid(PyObject *self, PyObject *args)
3733{
3734 int pid, pgid;
3735 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
3736 return NULL;
3737 pgid = getpgid(pid);
3738 if (pgid < 0)
3739 return posix_error();
3740 return PyInt_FromLong((long)pgid);
3741}
3742#endif /* HAVE_GETPGID */
3743
3744
Guido van Rossumb6775db1994-08-01 11:34:53 +00003745#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003746PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003747"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003748Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003749
Barry Warsaw53699e91996-12-10 23:23:01 +00003750static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003751posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003752{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003753#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00003754 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003755#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00003756 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003757#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003758}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003759#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003760
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003761
Guido van Rossumb6775db1994-08-01 11:34:53 +00003762#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003763PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003764"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003765Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003766
Barry Warsaw53699e91996-12-10 23:23:01 +00003767static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003768posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003769{
Guido van Rossum64933891994-10-20 21:56:42 +00003770#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00003771 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003772#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003773 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003774#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00003775 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003776 Py_INCREF(Py_None);
3777 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003778}
3779
Guido van Rossumb6775db1994-08-01 11:34:53 +00003780#endif /* HAVE_SETPGRP */
3781
Guido van Rossumad0ee831995-03-01 10:34:45 +00003782#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003783PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003784"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003785Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003786
Barry Warsaw53699e91996-12-10 23:23:01 +00003787static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003788posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003789{
Barry Warsaw53699e91996-12-10 23:23:01 +00003790 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003791}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003792#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003794
Fred Drake12c6e2d1999-12-14 21:25:03 +00003795#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003796PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003797"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003798Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003799
3800static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003801posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003802{
Neal Norwitze241ce82003-02-17 18:17:05 +00003803 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00003804 char *name;
3805 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003806
Fred Drakea30680b2000-12-06 21:24:28 +00003807 errno = 0;
3808 name = getlogin();
3809 if (name == NULL) {
3810 if (errno)
3811 posix_error();
3812 else
3813 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00003814 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00003815 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00003816 else
Neal Norwitz93c56822007-08-26 07:10:06 +00003817 result = PyUnicode_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00003818 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003819
Fred Drake12c6e2d1999-12-14 21:25:03 +00003820 return result;
3821}
3822#endif
3823
Guido van Rossumad0ee831995-03-01 10:34:45 +00003824#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003825PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003826"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003827Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003828
Barry Warsaw53699e91996-12-10 23:23:01 +00003829static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003830posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003831{
Barry Warsaw53699e91996-12-10 23:23:01 +00003832 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003833}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003834#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003836
Guido van Rossumad0ee831995-03-01 10:34:45 +00003837#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003838PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003839"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003840Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003841
Barry Warsaw53699e91996-12-10 23:23:01 +00003842static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003843posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003844{
3845 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003846 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00003847 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003848#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003849 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
3850 APIRET rc;
3851 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003852 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003853
3854 } else if (sig == XCPT_SIGNAL_KILLPROC) {
3855 APIRET rc;
3856 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003857 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003858
3859 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003860 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003861#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00003862 if (kill(pid, sig) == -1)
3863 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003864#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003865 Py_INCREF(Py_None);
3866 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003867}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003868#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003869
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003870#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003871PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003872"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003873Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00003874
3875static PyObject *
3876posix_killpg(PyObject *self, PyObject *args)
3877{
3878 int pgid, sig;
3879 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
3880 return NULL;
3881 if (killpg(pgid, sig) == -1)
3882 return posix_error();
3883 Py_INCREF(Py_None);
3884 return Py_None;
3885}
3886#endif
3887
Guido van Rossumc0125471996-06-28 18:55:32 +00003888#ifdef HAVE_PLOCK
3889
3890#ifdef HAVE_SYS_LOCK_H
3891#include <sys/lock.h>
3892#endif
3893
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003894PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003895"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003896Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003897
Barry Warsaw53699e91996-12-10 23:23:01 +00003898static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003899posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00003900{
3901 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003902 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00003903 return NULL;
3904 if (plock(op) == -1)
3905 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003906 Py_INCREF(Py_None);
3907 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00003908}
3909#endif
3910
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003911
Guido van Rossum3b066191991-06-04 19:40:25 +00003912
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003913
Guido van Rossumb6775db1994-08-01 11:34:53 +00003914#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003915PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003916"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003917Set the current process's user id.");
3918
Barry Warsaw53699e91996-12-10 23:23:01 +00003919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003920posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003921{
3922 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003923 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003924 return NULL;
3925 if (setuid(uid) < 0)
3926 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003927 Py_INCREF(Py_None);
3928 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003929}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003930#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00003931
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003932
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003933#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003934PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003935"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003936Set the current process's effective user id.");
3937
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003938static PyObject *
3939posix_seteuid (PyObject *self, PyObject *args)
3940{
3941 int euid;
3942 if (!PyArg_ParseTuple(args, "i", &euid)) {
3943 return NULL;
3944 } else if (seteuid(euid) < 0) {
3945 return posix_error();
3946 } else {
3947 Py_INCREF(Py_None);
3948 return Py_None;
3949 }
3950}
3951#endif /* HAVE_SETEUID */
3952
3953#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003954PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003955"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003956Set the current process's effective group id.");
3957
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003958static PyObject *
3959posix_setegid (PyObject *self, PyObject *args)
3960{
3961 int egid;
3962 if (!PyArg_ParseTuple(args, "i", &egid)) {
3963 return NULL;
3964 } else if (setegid(egid) < 0) {
3965 return posix_error();
3966 } else {
3967 Py_INCREF(Py_None);
3968 return Py_None;
3969 }
3970}
3971#endif /* HAVE_SETEGID */
3972
3973#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003974PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003975"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003976Set the current process's real and effective user ids.");
3977
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003978static PyObject *
3979posix_setreuid (PyObject *self, PyObject *args)
3980{
3981 int ruid, euid;
3982 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
3983 return NULL;
3984 } else if (setreuid(ruid, euid) < 0) {
3985 return posix_error();
3986 } else {
3987 Py_INCREF(Py_None);
3988 return Py_None;
3989 }
3990}
3991#endif /* HAVE_SETREUID */
3992
3993#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003994PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00003995"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003996Set the current process's real and effective group ids.");
3997
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00003998static PyObject *
3999posix_setregid (PyObject *self, PyObject *args)
4000{
4001 int rgid, egid;
4002 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4003 return NULL;
4004 } else if (setregid(rgid, egid) < 0) {
4005 return posix_error();
4006 } else {
4007 Py_INCREF(Py_None);
4008 return Py_None;
4009 }
4010}
4011#endif /* HAVE_SETREGID */
4012
Guido van Rossumb6775db1994-08-01 11:34:53 +00004013#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004014PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004015"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004016Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004017
Barry Warsaw53699e91996-12-10 23:23:01 +00004018static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004019posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004020{
4021 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004022 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004023 return NULL;
4024 if (setgid(gid) < 0)
4025 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004026 Py_INCREF(Py_None);
4027 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004028}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004029#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004030
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004031#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004032PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004033"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004034Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004035
4036static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004037posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004038{
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004039 int i, len;
4040 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004041
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004042 if (!PySequence_Check(groups)) {
4043 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4044 return NULL;
4045 }
4046 len = PySequence_Size(groups);
4047 if (len > MAX_GROUPS) {
4048 PyErr_SetString(PyExc_ValueError, "too many groups");
4049 return NULL;
4050 }
4051 for(i = 0; i < len; i++) {
4052 PyObject *elem;
4053 elem = PySequence_GetItem(groups, i);
4054 if (!elem)
4055 return NULL;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004056 if (!PyLong_Check(elem)) {
4057 PyErr_SetString(PyExc_TypeError,
4058 "groups must be integers");
4059 Py_DECREF(elem);
4060 return NULL;
4061 } else {
4062 unsigned long x = PyLong_AsUnsignedLong(elem);
4063 if (PyErr_Occurred()) {
4064 PyErr_SetString(PyExc_TypeError,
4065 "group id too big");
Georg Brandla13c2442005-11-22 19:30:31 +00004066 Py_DECREF(elem);
4067 return NULL;
Georg Brandla13c2442005-11-22 19:30:31 +00004068 }
Georg Brandla13c2442005-11-22 19:30:31 +00004069 grouplist[i] = x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00004070 /* read back the value to see if it fitted in gid_t */
Georg Brandla13c2442005-11-22 19:30:31 +00004071 if (grouplist[i] != x) {
4072 PyErr_SetString(PyExc_TypeError,
4073 "group id too big");
4074 Py_DECREF(elem);
4075 return NULL;
4076 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004077 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004078 Py_DECREF(elem);
4079 }
4080
4081 if (setgroups(len, grouplist) < 0)
4082 return posix_error();
4083 Py_INCREF(Py_None);
4084 return Py_None;
4085}
4086#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004087
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004088#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4089static PyObject *
4090wait_helper(int pid, int status, struct rusage *ru)
4091{
4092 PyObject *result;
4093 static PyObject *struct_rusage;
4094
4095 if (pid == -1)
4096 return posix_error();
4097
4098 if (struct_rusage == NULL) {
4099 PyObject *m = PyImport_ImportModule("resource");
4100 if (m == NULL)
4101 return NULL;
4102 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4103 Py_DECREF(m);
4104 if (struct_rusage == NULL)
4105 return NULL;
4106 }
4107
4108 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4109 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4110 if (!result)
4111 return NULL;
4112
4113#ifndef doubletime
4114#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4115#endif
4116
4117 PyStructSequence_SET_ITEM(result, 0,
4118 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4119 PyStructSequence_SET_ITEM(result, 1,
4120 PyFloat_FromDouble(doubletime(ru->ru_stime)));
4121#define SET_INT(result, index, value)\
4122 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4123 SET_INT(result, 2, ru->ru_maxrss);
4124 SET_INT(result, 3, ru->ru_ixrss);
4125 SET_INT(result, 4, ru->ru_idrss);
4126 SET_INT(result, 5, ru->ru_isrss);
4127 SET_INT(result, 6, ru->ru_minflt);
4128 SET_INT(result, 7, ru->ru_majflt);
4129 SET_INT(result, 8, ru->ru_nswap);
4130 SET_INT(result, 9, ru->ru_inblock);
4131 SET_INT(result, 10, ru->ru_oublock);
4132 SET_INT(result, 11, ru->ru_msgsnd);
4133 SET_INT(result, 12, ru->ru_msgrcv);
4134 SET_INT(result, 13, ru->ru_nsignals);
4135 SET_INT(result, 14, ru->ru_nvcsw);
4136 SET_INT(result, 15, ru->ru_nivcsw);
4137#undef SET_INT
4138
4139 if (PyErr_Occurred()) {
4140 Py_DECREF(result);
4141 return NULL;
4142 }
4143
4144 return Py_BuildValue("iiN", pid, status, result);
4145}
4146#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4147
4148#ifdef HAVE_WAIT3
4149PyDoc_STRVAR(posix_wait3__doc__,
4150"wait3(options) -> (pid, status, rusage)\n\n\
4151Wait for completion of a child process.");
4152
4153static PyObject *
4154posix_wait3(PyObject *self, PyObject *args)
4155{
4156 int pid, options;
4157 struct rusage ru;
4158 WAIT_TYPE status;
4159 WAIT_STATUS_INT(status) = 0;
4160
4161 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4162 return NULL;
4163
4164 Py_BEGIN_ALLOW_THREADS
4165 pid = wait3(&status, options, &ru);
4166 Py_END_ALLOW_THREADS
4167
4168 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4169}
4170#endif /* HAVE_WAIT3 */
4171
4172#ifdef HAVE_WAIT4
4173PyDoc_STRVAR(posix_wait4__doc__,
4174"wait4(pid, options) -> (pid, status, rusage)\n\n\
4175Wait for completion of a given child process.");
4176
4177static PyObject *
4178posix_wait4(PyObject *self, PyObject *args)
4179{
4180 int pid, options;
4181 struct rusage ru;
4182 WAIT_TYPE status;
4183 WAIT_STATUS_INT(status) = 0;
4184
4185 if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options))
4186 return NULL;
4187
4188 Py_BEGIN_ALLOW_THREADS
4189 pid = wait4(pid, &status, options, &ru);
4190 Py_END_ALLOW_THREADS
4191
4192 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
4193}
4194#endif /* HAVE_WAIT4 */
4195
Guido van Rossumb6775db1994-08-01 11:34:53 +00004196#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004197PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004198"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004199Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004200
Barry Warsaw53699e91996-12-10 23:23:01 +00004201static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004202posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004203{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004204 int pid, options;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004205 WAIT_TYPE status;
4206 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004207
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004208 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004209 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004210 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004211 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004212 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004213 if (pid == -1)
4214 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004215
4216 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004217}
4218
Tim Petersab034fa2002-02-01 11:27:43 +00004219#elif defined(HAVE_CWAIT)
4220
4221/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004222PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004223"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004224"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004225
4226static PyObject *
4227posix_waitpid(PyObject *self, PyObject *args)
4228{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004229 Py_intptr_t pid;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004230 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004231
4232 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4233 return NULL;
4234 Py_BEGIN_ALLOW_THREADS
4235 pid = _cwait(&status, pid, options);
4236 Py_END_ALLOW_THREADS
4237 if (pid == -1)
4238 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004239
4240 /* shift the status left a byte so this is more like the POSIX waitpid */
4241 return Py_BuildValue("ii", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004242}
4243#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004244
Guido van Rossumad0ee831995-03-01 10:34:45 +00004245#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004246PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004247"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004248Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004249
Barry Warsaw53699e91996-12-10 23:23:01 +00004250static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004251posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004252{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004253 int pid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004254 WAIT_TYPE status;
4255 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004256
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004257 Py_BEGIN_ALLOW_THREADS
4258 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004259 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004260 if (pid == -1)
4261 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004262
4263 return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004264}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004265#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004266
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004268PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004269"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004270Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004271
Barry Warsaw53699e91996-12-10 23:23:01 +00004272static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004273posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004274{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004275#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004276 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004277#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004278#ifdef MS_WINDOWS
Martin v. Löwis14694662006-02-03 12:54:16 +00004279 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004280#else
4281 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4282#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004283#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004284}
4285
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004286
Guido van Rossumb6775db1994-08-01 11:34:53 +00004287#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004288PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004289"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004290Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004291
Barry Warsaw53699e91996-12-10 23:23:01 +00004292static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004293posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004294{
Thomas Wouters89f507f2006-12-13 04:49:30 +00004295 PyObject* v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004296 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004297 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004298 int n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004299 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004300
4301 if (!PyArg_ParseTuple(args, "et:readlink",
4302 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004303 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004304 v = PySequence_GetItem(args, 0);
Neal Norwitzcda5c062007-08-12 17:09:36 +00004305 if (v == NULL) {
4306 PyMem_Free(path);
4307 return NULL;
4308 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004309
4310 if (PyUnicode_Check(v)) {
4311 arg_is_unicode = 1;
4312 }
4313 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004314
Barry Warsaw53699e91996-12-10 23:23:01 +00004315 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004316 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004317 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004318 if (n < 0)
Neal Norwitzfca70052007-08-12 16:56:02 +00004319 return posix_error_with_allocated_filename(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004320
Neal Norwitzfca70052007-08-12 16:56:02 +00004321 PyMem_Free(path);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004322 v = PyString_FromStringAndSize(buf, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004323 if (arg_is_unicode) {
4324 PyObject *w;
4325
4326 w = PyUnicode_FromEncodedObject(v,
4327 Py_FileSystemDefaultEncoding,
4328 "strict");
4329 if (w != NULL) {
4330 Py_DECREF(v);
4331 v = w;
4332 }
4333 else {
4334 /* fall back to the original byte string, as
4335 discussed in patch #683592 */
4336 PyErr_Clear();
4337 }
4338 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004339 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004340}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004341#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004342
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004343
Guido van Rossumb6775db1994-08-01 11:34:53 +00004344#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004345PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004346"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004347Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004348
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004349static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004350posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004351{
Thomas Wouters477c8d52006-05-27 19:21:47 +00004352 return posix_2str(args, "etet:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004353}
4354#endif /* HAVE_SYMLINK */
4355
4356
4357#ifdef HAVE_TIMES
4358#ifndef HZ
4359#define HZ 60 /* Universal constant :-) */
4360#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004361
Guido van Rossumd48f2521997-12-05 22:19:34 +00004362#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4363static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004364system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004365{
4366 ULONG value = 0;
4367
4368 Py_BEGIN_ALLOW_THREADS
4369 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4370 Py_END_ALLOW_THREADS
4371
4372 return value;
4373}
4374
4375static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004376posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004377{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004378 /* Currently Only Uptime is Provided -- Others Later */
4379 return Py_BuildValue("ddddd",
4380 (double)0 /* t.tms_utime / HZ */,
4381 (double)0 /* t.tms_stime / HZ */,
4382 (double)0 /* t.tms_cutime / HZ */,
4383 (double)0 /* t.tms_cstime / HZ */,
4384 (double)system_uptime() / 1000);
4385}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004386#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004387static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004388posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004389{
4390 struct tms t;
4391 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004392 errno = 0;
4393 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004394 if (c == (clock_t) -1)
4395 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004396 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004397 (double)t.tms_utime / HZ,
4398 (double)t.tms_stime / HZ,
4399 (double)t.tms_cutime / HZ,
4400 (double)t.tms_cstime / HZ,
4401 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004402}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004403#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004404#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004405
4406
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004407#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004408#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004409static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004410posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004411{
4412 FILETIME create, exit, kernel, user;
4413 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004414 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004415 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4416 /* The fields of a FILETIME structure are the hi and lo part
4417 of a 64-bit value expressed in 100 nanosecond units.
4418 1e7 is one second in such units; 1e-7 the inverse.
4419 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4420 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004421 return Py_BuildValue(
4422 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004423 (double)(kernel.dwHighDateTime*429.4967296 +
4424 kernel.dwLowDateTime*1e-7),
4425 (double)(user.dwHighDateTime*429.4967296 +
4426 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004427 (double)0,
4428 (double)0,
4429 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004430}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004431#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004432
4433#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004434PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004435"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004436Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004437#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004438
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004439
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004440#ifdef HAVE_GETSID
4441PyDoc_STRVAR(posix_getsid__doc__,
4442"getsid(pid) -> sid\n\n\
4443Call the system call getsid().");
4444
4445static PyObject *
4446posix_getsid(PyObject *self, PyObject *args)
4447{
4448 int pid, sid;
4449 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4450 return NULL;
4451 sid = getsid(pid);
4452 if (sid < 0)
4453 return posix_error();
4454 return PyInt_FromLong((long)sid);
4455}
4456#endif /* HAVE_GETSID */
4457
4458
Guido van Rossumb6775db1994-08-01 11:34:53 +00004459#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004460PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004461"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004462Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004463
Barry Warsaw53699e91996-12-10 23:23:01 +00004464static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004465posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004466{
Guido van Rossum687dd131993-05-17 08:34:16 +00004467 if (setsid() < 0)
4468 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004469 Py_INCREF(Py_None);
4470 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004471}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004472#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004473
Guido van Rossumb6775db1994-08-01 11:34:53 +00004474#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004475PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004476"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004477Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004478
Barry Warsaw53699e91996-12-10 23:23:01 +00004479static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004480posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004481{
4482 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004483 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004484 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004485 if (setpgid(pid, pgrp) < 0)
4486 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004487 Py_INCREF(Py_None);
4488 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004489}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004490#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004491
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004492
Guido van Rossumb6775db1994-08-01 11:34:53 +00004493#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004494PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004495"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004496Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004497
Barry Warsaw53699e91996-12-10 23:23:01 +00004498static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004499posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004500{
4501 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004502 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004503 return NULL;
4504 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004505 if (pgid < 0)
4506 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004507 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004508}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004509#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004510
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004511
Guido van Rossumb6775db1994-08-01 11:34:53 +00004512#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004513PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004514"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004515Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004516
Barry Warsaw53699e91996-12-10 23:23:01 +00004517static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004518posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004519{
4520 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004521 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004522 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004523 if (tcsetpgrp(fd, pgid) < 0)
4524 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004525 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004526 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004527}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004528#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004529
Guido van Rossum687dd131993-05-17 08:34:16 +00004530/* Functions acting on file descriptors */
4531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004532PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004533"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004534Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004535
Barry Warsaw53699e91996-12-10 23:23:01 +00004536static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004537posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004538{
Mark Hammondef8b6542001-05-13 08:04:26 +00004539 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004540 int flag;
4541 int mode = 0777;
4542 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004543
4544#ifdef MS_WINDOWS
4545 if (unicode_file_names()) {
4546 PyUnicodeObject *po;
4547 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4548 Py_BEGIN_ALLOW_THREADS
4549 /* PyUnicode_AS_UNICODE OK without thread
4550 lock as it is a simple dereference. */
4551 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4552 Py_END_ALLOW_THREADS
4553 if (fd < 0)
4554 return posix_error();
4555 return PyInt_FromLong((long)fd);
4556 }
4557 /* Drop the argument parsing error as narrow strings
4558 are also valid. */
4559 PyErr_Clear();
4560 }
4561#endif
4562
Tim Peters5aa91602002-01-30 05:46:57 +00004563 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004564 Py_FileSystemDefaultEncoding, &file,
4565 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004566 return NULL;
4567
Barry Warsaw53699e91996-12-10 23:23:01 +00004568 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004569 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004570 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004571 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004572 return posix_error_with_allocated_filename(file);
4573 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004574 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004575}
4576
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004578PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004579"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004580Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004581
Barry Warsaw53699e91996-12-10 23:23:01 +00004582static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004583posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004584{
4585 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004586 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004587 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004588 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004589 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004590 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004591 if (res < 0)
4592 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004593 Py_INCREF(Py_None);
4594 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004595}
4596
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004598PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004599"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004600Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004601
Barry Warsaw53699e91996-12-10 23:23:01 +00004602static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004603posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004604{
4605 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004606 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004607 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004608 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004609 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004610 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004611 if (fd < 0)
4612 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004613 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004614}
4615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004617PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004618"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004619Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004620
Barry Warsaw53699e91996-12-10 23:23:01 +00004621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004622posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004623{
4624 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004625 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004626 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004627 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004628 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004629 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004630 if (res < 0)
4631 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004632 Py_INCREF(Py_None);
4633 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004634}
4635
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004636
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004637PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004638"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004640
Barry Warsaw53699e91996-12-10 23:23:01 +00004641static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004642posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004643{
4644 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004645#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004646 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004647#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004648 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004649#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004650 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004651 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004652 return NULL;
4653#ifdef SEEK_SET
4654 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4655 switch (how) {
4656 case 0: how = SEEK_SET; break;
4657 case 1: how = SEEK_CUR; break;
4658 case 2: how = SEEK_END; break;
4659 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004660#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004661
4662#if !defined(HAVE_LARGEFILE_SUPPORT)
4663 pos = PyInt_AsLong(posobj);
4664#else
4665 pos = PyLong_Check(posobj) ?
4666 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4667#endif
4668 if (PyErr_Occurred())
4669 return NULL;
4670
Barry Warsaw53699e91996-12-10 23:23:01 +00004671 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004672#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004673 res = _lseeki64(fd, pos, how);
4674#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004675 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004676#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004677 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004678 if (res < 0)
4679 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004680
4681#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004682 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004683#else
4684 return PyLong_FromLongLong(res);
4685#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004686}
4687
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004689PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004690"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004691Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004692
Barry Warsaw53699e91996-12-10 23:23:01 +00004693static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004694posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004695{
Guido van Rossum572dbf82007-04-27 23:53:51 +00004696 int fd, size;
4697 Py_ssize_t n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004698 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004699 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004700 return NULL;
Michael W. Hudson9867ced2005-01-31 17:01:59 +00004701 if (size < 0) {
4702 errno = EINVAL;
4703 return posix_error();
4704 }
Guido van Rossum572dbf82007-04-27 23:53:51 +00004705 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004706 if (buffer == NULL)
4707 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004708 Py_BEGIN_ALLOW_THREADS
Guido van Rossum572dbf82007-04-27 23:53:51 +00004709 n = read(fd, PyBytes_AsString(buffer), size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004710 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00004711 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004712 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00004713 return posix_error();
4714 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00004715 if (n != size)
Guido van Rossum572dbf82007-04-27 23:53:51 +00004716 PyBytes_Resize(buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00004717 return buffer;
4718}
4719
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004721PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004722"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004723Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004724
Barry Warsaw53699e91996-12-10 23:23:01 +00004725static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004726posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004727{
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004728 int fd;
4729 Py_ssize_t size;
Guido van Rossum687dd131993-05-17 08:34:16 +00004730 char *buffer;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004731
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004732 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004733 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004734 Py_BEGIN_ALLOW_THREADS
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004735 size = write(fd, buffer, (size_t)size);
Barry Warsaw53699e91996-12-10 23:23:01 +00004736 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004737 if (size < 0)
4738 return posix_error();
Thomas Wouters68bc4f92006-03-01 01:05:10 +00004739 return PyInt_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004740}
4741
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004743PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004744"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004745Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004746
Barry Warsaw53699e91996-12-10 23:23:01 +00004747static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004748posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004749{
4750 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00004751 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00004752 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004753 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004754 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00004755#ifdef __VMS
4756 /* on OpenVMS we must ensure that all bytes are written to the file */
4757 fsync(fd);
4758#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004759 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00004760 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00004761 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00004762 if (res != 0) {
4763#ifdef MS_WINDOWS
4764 return win32_error("fstat", NULL);
4765#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004766 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00004767#endif
4768 }
Tim Peters5aa91602002-01-30 05:46:57 +00004769
Martin v. Löwis14694662006-02-03 12:54:16 +00004770 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00004771}
4772
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004773PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004774"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00004775Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004776connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00004777
4778static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00004779posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00004780{
4781 int fd;
4782 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
4783 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00004784 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00004785}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004786
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004787#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004788PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004789"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004790Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004791
Barry Warsaw53699e91996-12-10 23:23:01 +00004792static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004793posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00004794{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004795#if defined(PYOS_OS2)
4796 HFILE read, write;
4797 APIRET rc;
4798
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004799 Py_BEGIN_ALLOW_THREADS
4800 rc = DosCreatePipe( &read, &write, 4096);
4801 Py_END_ALLOW_THREADS
4802 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004803 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004804
4805 return Py_BuildValue("(ii)", read, write);
4806#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004807#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00004808 int fds[2];
4809 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00004810 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004811 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00004812 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004813 if (res != 0)
4814 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004815 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004816#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00004817 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004818 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00004819 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00004820 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004821 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00004822 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00004823 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004824 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00004825 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
4826 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00004827 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004828#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004829#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004830}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004831#endif /* HAVE_PIPE */
4832
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004833
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004834#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004835PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004836"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004837Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004838
Barry Warsaw53699e91996-12-10 23:23:01 +00004839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004840posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004841{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004842 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004843 int mode = 0666;
4844 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004845 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004846 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004847 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004848 res = mkfifo(filename, mode);
4849 Py_END_ALLOW_THREADS
4850 if (res < 0)
4851 return posix_error();
4852 Py_INCREF(Py_None);
4853 return Py_None;
4854}
4855#endif
4856
4857
Neal Norwitz11690112002-07-30 01:08:28 +00004858#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004859PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00004860"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004861Create a filesystem node (file, device special file or named pipe)\n\
4862named filename. mode specifies both the permissions to use and the\n\
4863type of node to be created, being combined (bitwise OR) with one of\n\
4864S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004865device defines the newly created device special file (probably using\n\
4866os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004867
4868
4869static PyObject *
4870posix_mknod(PyObject *self, PyObject *args)
4871{
4872 char *filename;
4873 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004874 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004875 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00004876 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00004877 return NULL;
4878 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004879 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00004880 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004881 if (res < 0)
4882 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004883 Py_INCREF(Py_None);
4884 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004885}
4886#endif
4887
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00004888#ifdef HAVE_DEVICE_MACROS
4889PyDoc_STRVAR(posix_major__doc__,
4890"major(device) -> major number\n\
4891Extracts a device major number from a raw device number.");
4892
4893static PyObject *
4894posix_major(PyObject *self, PyObject *args)
4895{
4896 int device;
4897 if (!PyArg_ParseTuple(args, "i:major", &device))
4898 return NULL;
4899 return PyInt_FromLong((long)major(device));
4900}
4901
4902PyDoc_STRVAR(posix_minor__doc__,
4903"minor(device) -> minor number\n\
4904Extracts a device minor number from a raw device number.");
4905
4906static PyObject *
4907posix_minor(PyObject *self, PyObject *args)
4908{
4909 int device;
4910 if (!PyArg_ParseTuple(args, "i:minor", &device))
4911 return NULL;
4912 return PyInt_FromLong((long)minor(device));
4913}
4914
4915PyDoc_STRVAR(posix_makedev__doc__,
4916"makedev(major, minor) -> device number\n\
4917Composes a raw device number from the major and minor device numbers.");
4918
4919static PyObject *
4920posix_makedev(PyObject *self, PyObject *args)
4921{
4922 int major, minor;
4923 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
4924 return NULL;
4925 return PyInt_FromLong((long)makedev(major, minor));
4926}
4927#endif /* device macros */
4928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004929
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004930#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004931PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004932"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004933Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004934
Barry Warsaw53699e91996-12-10 23:23:01 +00004935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004936posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004937{
4938 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004939 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004940 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004941 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004942
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004943 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004944 return NULL;
4945
4946#if !defined(HAVE_LARGEFILE_SUPPORT)
4947 length = PyInt_AsLong(lenobj);
4948#else
4949 length = PyLong_Check(lenobj) ?
4950 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
4951#endif
4952 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004953 return NULL;
4954
Barry Warsaw53699e91996-12-10 23:23:01 +00004955 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004956 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00004957 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004958 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00004959 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004960 return NULL;
4961 }
Barry Warsaw53699e91996-12-10 23:23:01 +00004962 Py_INCREF(Py_None);
4963 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004964}
4965#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004966
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004967#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004968PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004969"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004970Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004971
Fred Drake762e2061999-08-26 17:23:54 +00004972/* Save putenv() parameters as values here, so we can collect them when they
4973 * get re-set with another call for the same key. */
4974static PyObject *posix_putenv_garbage;
4975
Tim Peters5aa91602002-01-30 05:46:57 +00004976static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004977posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004978{
4979 char *s1, *s2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004980 char *newenv;
Fred Drake762e2061999-08-26 17:23:54 +00004981 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00004982 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004983
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004984 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004985 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00004986
4987#if defined(PYOS_OS2)
4988 if (stricmp(s1, "BEGINLIBPATH") == 0) {
4989 APIRET rc;
4990
Guido van Rossumd48f2521997-12-05 22:19:34 +00004991 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
4992 if (rc != NO_ERROR)
4993 return os2_error(rc);
4994
4995 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
4996 APIRET rc;
4997
Guido van Rossumd48f2521997-12-05 22:19:34 +00004998 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
4999 if (rc != NO_ERROR)
5000 return os2_error(rc);
5001 } else {
5002#endif
5003
Fred Drake762e2061999-08-26 17:23:54 +00005004 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005005 len = strlen(s1) + strlen(s2) + 2;
5006 /* len includes space for a trailing \0; the size arg to
5007 PyString_FromStringAndSize does not count that */
5008 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005009 if (newstr == NULL)
5010 return PyErr_NoMemory();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005011 newenv = PyString_AS_STRING(newstr);
5012 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5013 if (putenv(newenv)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005014 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005015 posix_error();
5016 return NULL;
5017 }
Fred Drake762e2061999-08-26 17:23:54 +00005018 /* Install the first arg and newstr in posix_putenv_garbage;
5019 * this will cause previous value to be collected. This has to
5020 * happen after the real putenv() call because the old value
5021 * was still accessible until then. */
5022 if (PyDict_SetItem(posix_putenv_garbage,
5023 PyTuple_GET_ITEM(args, 0), newstr)) {
5024 /* really not much we can do; just leak */
5025 PyErr_Clear();
5026 }
5027 else {
5028 Py_DECREF(newstr);
5029 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005030
5031#if defined(PYOS_OS2)
5032 }
5033#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005034 Py_INCREF(Py_None);
5035 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005036}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005037#endif /* putenv */
5038
Guido van Rossumc524d952001-10-19 01:31:59 +00005039#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005040PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005041"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005042Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005043
5044static PyObject *
5045posix_unsetenv(PyObject *self, PyObject *args)
5046{
5047 char *s1;
5048
5049 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5050 return NULL;
5051
5052 unsetenv(s1);
5053
5054 /* Remove the key from posix_putenv_garbage;
5055 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005056 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005057 * old value was still accessible until then.
5058 */
5059 if (PyDict_DelItem(posix_putenv_garbage,
5060 PyTuple_GET_ITEM(args, 0))) {
5061 /* really not much we can do; just leak */
5062 PyErr_Clear();
5063 }
5064
5065 Py_INCREF(Py_None);
5066 return Py_None;
5067}
5068#endif /* unsetenv */
5069
Guido van Rossumb6a47161997-09-15 22:54:34 +00005070#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005071PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005072"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005073Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005074
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005075static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005076posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005077{
5078 int code;
5079 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005080 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005081 return NULL;
5082 message = strerror(code);
5083 if (message == NULL) {
5084 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005085 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005086 return NULL;
5087 }
Neal Norwitz93c56822007-08-26 07:10:06 +00005088 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005089}
5090#endif /* strerror */
5091
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005092
Guido van Rossumc9641791998-08-04 15:26:23 +00005093#ifdef HAVE_SYS_WAIT_H
5094
Fred Drake106c1a02002-04-23 15:58:02 +00005095#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005096PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005097"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005098Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005099
5100static PyObject *
5101posix_WCOREDUMP(PyObject *self, PyObject *args)
5102{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005103 WAIT_TYPE status;
5104 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005105
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005106 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005107 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005108
5109 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005110}
5111#endif /* WCOREDUMP */
5112
5113#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005114PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005115"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005116Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005117job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005118
5119static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005120posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005121{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005122 WAIT_TYPE status;
5123 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005124
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005125 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
Fred Drake106c1a02002-04-23 15:58:02 +00005126 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005127
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005128 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005129}
5130#endif /* WIFCONTINUED */
5131
Guido van Rossumc9641791998-08-04 15:26:23 +00005132#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005133PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005134"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005135Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005136
5137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005138posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005139{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005140 WAIT_TYPE status;
5141 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005142
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005143 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005144 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005145
Fred Drake106c1a02002-04-23 15:58:02 +00005146 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005147}
5148#endif /* WIFSTOPPED */
5149
5150#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005151PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005152"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005153Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005154
5155static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005156posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005157{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005158 WAIT_TYPE status;
5159 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005160
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005161 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005162 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005163
Fred Drake106c1a02002-04-23 15:58:02 +00005164 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005165}
5166#endif /* WIFSIGNALED */
5167
5168#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005169PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005170"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005171Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005172system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005173
5174static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005175posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005176{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005177 WAIT_TYPE status;
5178 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005179
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005180 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005181 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005182
Fred Drake106c1a02002-04-23 15:58:02 +00005183 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005184}
5185#endif /* WIFEXITED */
5186
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005187#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005188PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005189"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005190Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005191
5192static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005193posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005194{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005195 WAIT_TYPE status;
5196 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005197
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005198 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005199 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005200
Guido van Rossumc9641791998-08-04 15:26:23 +00005201 return Py_BuildValue("i", WEXITSTATUS(status));
5202}
5203#endif /* WEXITSTATUS */
5204
5205#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005206PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005207"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005208Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005209value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005210
5211static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005212posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005213{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005214 WAIT_TYPE status;
5215 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005216
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005217 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005218 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005219
Guido van Rossumc9641791998-08-04 15:26:23 +00005220 return Py_BuildValue("i", WTERMSIG(status));
5221}
5222#endif /* WTERMSIG */
5223
5224#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005225PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005226"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005227Return the signal that stopped the process that provided\n\
5228the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005229
5230static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005231posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005232{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005233 WAIT_TYPE status;
5234 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005235
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005236 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
Guido van Rossumc9641791998-08-04 15:26:23 +00005237 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005238
Guido van Rossumc9641791998-08-04 15:26:23 +00005239 return Py_BuildValue("i", WSTOPSIG(status));
5240}
5241#endif /* WSTOPSIG */
5242
5243#endif /* HAVE_SYS_WAIT_H */
5244
5245
Thomas Wouters477c8d52006-05-27 19:21:47 +00005246#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005247#ifdef _SCO_DS
5248/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5249 needed definitions in sys/statvfs.h */
5250#define _SVID3
5251#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005252#include <sys/statvfs.h>
5253
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005254static PyObject*
5255_pystatvfs_fromstructstatvfs(struct statvfs st) {
5256 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5257 if (v == NULL)
5258 return NULL;
5259
5260#if !defined(HAVE_LARGEFILE_SUPPORT)
5261 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5262 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5263 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5264 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5265 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5266 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5267 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5268 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5269 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5270 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5271#else
5272 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5273 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005274 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005275 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005276 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005277 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005278 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005279 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005280 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005281 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005282 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005283 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005284 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005285 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005286 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5287 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5288#endif
5289
5290 return v;
5291}
5292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005293PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005294"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005295Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005296
5297static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005298posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005299{
5300 int fd, res;
5301 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005302
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005303 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005304 return NULL;
5305 Py_BEGIN_ALLOW_THREADS
5306 res = fstatvfs(fd, &st);
5307 Py_END_ALLOW_THREADS
5308 if (res != 0)
5309 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005310
5311 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005312}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005313#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005314
5315
Thomas Wouters477c8d52006-05-27 19:21:47 +00005316#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005317#include <sys/statvfs.h>
5318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005319PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005320"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005321Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005322
5323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005324posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005325{
5326 char *path;
5327 int res;
5328 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005329 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005330 return NULL;
5331 Py_BEGIN_ALLOW_THREADS
5332 res = statvfs(path, &st);
5333 Py_END_ALLOW_THREADS
5334 if (res != 0)
5335 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005336
5337 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005338}
5339#endif /* HAVE_STATVFS */
5340
5341
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005342#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005343PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005344"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005345Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005346The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005347or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005348
5349static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005350posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005351{
5352 PyObject *result = NULL;
5353 char *dir = NULL;
5354 char *pfx = NULL;
5355 char *name;
5356
5357 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5358 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005359
Skip Montanaro46fc3372007-08-12 11:44:53 +00005360 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5361 "tempnam is a potential security risk to your program",
5362 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005363 return NULL;
5364
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005365#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005366 name = _tempnam(dir, pfx);
5367#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005368 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005369#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005370 if (name == NULL)
5371 return PyErr_NoMemory();
5372 result = PyString_FromString(name);
5373 free(name);
5374 return result;
5375}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005376#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005377
5378
5379#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005380PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005381"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005382Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005383
5384static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005385posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005386{
5387 FILE *fp;
5388
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005389 fp = tmpfile();
5390 if (fp == NULL)
5391 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005392 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005393}
5394#endif
5395
5396
5397#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005398PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005399"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005400Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005401
5402static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005403posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005404{
5405 char buffer[L_tmpnam];
5406 char *name;
5407
Skip Montanaro46fc3372007-08-12 11:44:53 +00005408 if (PyErr_WarnEx(PyExc_RuntimeWarning,
5409 "tmpnam is a potential security risk to your program",
5410 1) < 0)
Skip Montanaro95618b52001-08-18 18:52:10 +00005411 return NULL;
5412
Greg Wardb48bc172000-03-01 21:51:56 +00005413#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005414 name = tmpnam_r(buffer);
5415#else
5416 name = tmpnam(buffer);
5417#endif
5418 if (name == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005419 PyObject *err = Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005420#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005421 "unexpected NULL from tmpnam_r"
5422#else
5423 "unexpected NULL from tmpnam"
5424#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005425 );
5426 PyErr_SetObject(PyExc_OSError, err);
5427 Py_XDECREF(err);
5428 return NULL;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005429 }
5430 return PyString_FromString(buffer);
5431}
5432#endif
5433
5434
Fred Drakec9680921999-12-13 16:37:25 +00005435/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5436 * It maps strings representing configuration variable names to
5437 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005438 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005439 * rarely-used constants. There are three separate tables that use
5440 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005441 *
5442 * This code is always included, even if none of the interfaces that
5443 * need it are included. The #if hackery needed to avoid it would be
5444 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005445 */
5446struct constdef {
5447 char *name;
5448 long value;
5449};
5450
Fred Drake12c6e2d1999-12-14 21:25:03 +00005451static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005452conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005453 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005454{
5455 if (PyInt_Check(arg)) {
5456 *valuep = PyInt_AS_LONG(arg);
5457 return 1;
5458 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005459 else {
Fred Drake12c6e2d1999-12-14 21:25:03 +00005460 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005461 size_t lo = 0;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005462 size_t mid;
Fred Drake699f3522000-06-29 21:12:41 +00005463 size_t hi = tablesize;
5464 int cmp;
Guido van Rossumbce56a62007-05-10 18:04:33 +00005465 const char *confname;
5466 Py_ssize_t namelen;
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005467 if (!PyUnicode_Check(arg)) {
Guido van Rossumbce56a62007-05-10 18:04:33 +00005468 PyErr_SetString(PyExc_TypeError,
5469 "configuration names must be strings or integers");
5470 return 0;
5471 }
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005472 confname = PyUnicode_AsString(arg);
5473 if (confname == NULL)
5474 return 0;
5475 namelen = strlen(confname);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005476 while (lo < hi) {
5477 mid = (lo + hi) / 2;
5478 cmp = strcmp(confname, table[mid].name);
5479 if (cmp < 0)
5480 hi = mid;
5481 else if (cmp > 0)
5482 lo = mid + 1;
5483 else {
5484 *valuep = table[mid].value;
5485 return 1;
5486 }
5487 }
5488 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005489 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005490 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005491}
5492
5493
5494#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5495static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005496#ifdef _PC_ABI_AIO_XFER_MAX
5497 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5498#endif
5499#ifdef _PC_ABI_ASYNC_IO
5500 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5501#endif
Fred Drakec9680921999-12-13 16:37:25 +00005502#ifdef _PC_ASYNC_IO
5503 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5504#endif
5505#ifdef _PC_CHOWN_RESTRICTED
5506 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5507#endif
5508#ifdef _PC_FILESIZEBITS
5509 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5510#endif
5511#ifdef _PC_LAST
5512 {"PC_LAST", _PC_LAST},
5513#endif
5514#ifdef _PC_LINK_MAX
5515 {"PC_LINK_MAX", _PC_LINK_MAX},
5516#endif
5517#ifdef _PC_MAX_CANON
5518 {"PC_MAX_CANON", _PC_MAX_CANON},
5519#endif
5520#ifdef _PC_MAX_INPUT
5521 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5522#endif
5523#ifdef _PC_NAME_MAX
5524 {"PC_NAME_MAX", _PC_NAME_MAX},
5525#endif
5526#ifdef _PC_NO_TRUNC
5527 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5528#endif
5529#ifdef _PC_PATH_MAX
5530 {"PC_PATH_MAX", _PC_PATH_MAX},
5531#endif
5532#ifdef _PC_PIPE_BUF
5533 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5534#endif
5535#ifdef _PC_PRIO_IO
5536 {"PC_PRIO_IO", _PC_PRIO_IO},
5537#endif
5538#ifdef _PC_SOCK_MAXBUF
5539 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5540#endif
5541#ifdef _PC_SYNC_IO
5542 {"PC_SYNC_IO", _PC_SYNC_IO},
5543#endif
5544#ifdef _PC_VDISABLE
5545 {"PC_VDISABLE", _PC_VDISABLE},
5546#endif
5547};
5548
Fred Drakec9680921999-12-13 16:37:25 +00005549static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005550conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005551{
5552 return conv_confname(arg, valuep, posix_constants_pathconf,
5553 sizeof(posix_constants_pathconf)
5554 / sizeof(struct constdef));
5555}
5556#endif
5557
5558#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005560"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005561Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005562If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005563
5564static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005565posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005566{
5567 PyObject *result = NULL;
5568 int name, fd;
5569
Fred Drake12c6e2d1999-12-14 21:25:03 +00005570 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5571 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005572 long limit;
5573
5574 errno = 0;
5575 limit = fpathconf(fd, name);
5576 if (limit == -1 && errno != 0)
5577 posix_error();
5578 else
5579 result = PyInt_FromLong(limit);
5580 }
5581 return result;
5582}
5583#endif
5584
5585
5586#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005587PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005588"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005589Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005590If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005591
5592static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005593posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005594{
5595 PyObject *result = NULL;
5596 int name;
5597 char *path;
5598
5599 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5600 conv_path_confname, &name)) {
5601 long limit;
5602
5603 errno = 0;
5604 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005605 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005606 if (errno == EINVAL)
5607 /* could be a path or name problem */
5608 posix_error();
5609 else
5610 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005611 }
Fred Drakec9680921999-12-13 16:37:25 +00005612 else
5613 result = PyInt_FromLong(limit);
5614 }
5615 return result;
5616}
5617#endif
5618
5619#ifdef HAVE_CONFSTR
5620static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005621#ifdef _CS_ARCHITECTURE
5622 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
5623#endif
5624#ifdef _CS_HOSTNAME
5625 {"CS_HOSTNAME", _CS_HOSTNAME},
5626#endif
5627#ifdef _CS_HW_PROVIDER
5628 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
5629#endif
5630#ifdef _CS_HW_SERIAL
5631 {"CS_HW_SERIAL", _CS_HW_SERIAL},
5632#endif
5633#ifdef _CS_INITTAB_NAME
5634 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
5635#endif
Fred Drakec9680921999-12-13 16:37:25 +00005636#ifdef _CS_LFS64_CFLAGS
5637 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
5638#endif
5639#ifdef _CS_LFS64_LDFLAGS
5640 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
5641#endif
5642#ifdef _CS_LFS64_LIBS
5643 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
5644#endif
5645#ifdef _CS_LFS64_LINTFLAGS
5646 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
5647#endif
5648#ifdef _CS_LFS_CFLAGS
5649 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
5650#endif
5651#ifdef _CS_LFS_LDFLAGS
5652 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
5653#endif
5654#ifdef _CS_LFS_LIBS
5655 {"CS_LFS_LIBS", _CS_LFS_LIBS},
5656#endif
5657#ifdef _CS_LFS_LINTFLAGS
5658 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
5659#endif
Fred Draked86ed291999-12-15 15:34:33 +00005660#ifdef _CS_MACHINE
5661 {"CS_MACHINE", _CS_MACHINE},
5662#endif
Fred Drakec9680921999-12-13 16:37:25 +00005663#ifdef _CS_PATH
5664 {"CS_PATH", _CS_PATH},
5665#endif
Fred Draked86ed291999-12-15 15:34:33 +00005666#ifdef _CS_RELEASE
5667 {"CS_RELEASE", _CS_RELEASE},
5668#endif
5669#ifdef _CS_SRPC_DOMAIN
5670 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
5671#endif
5672#ifdef _CS_SYSNAME
5673 {"CS_SYSNAME", _CS_SYSNAME},
5674#endif
5675#ifdef _CS_VERSION
5676 {"CS_VERSION", _CS_VERSION},
5677#endif
Fred Drakec9680921999-12-13 16:37:25 +00005678#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5679 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
5680#endif
5681#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5682 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
5683#endif
5684#ifdef _CS_XBS5_ILP32_OFF32_LIBS
5685 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
5686#endif
5687#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5688 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
5689#endif
5690#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5691 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
5692#endif
5693#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5694 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
5695#endif
5696#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5697 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
5698#endif
5699#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5700 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
5701#endif
5702#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5703 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
5704#endif
5705#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5706 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
5707#endif
5708#ifdef _CS_XBS5_LP64_OFF64_LIBS
5709 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
5710#endif
5711#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5712 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
5713#endif
5714#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
5715 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
5716#endif
5717#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
5718 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
5719#endif
5720#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
5721 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
5722#endif
5723#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
5724 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
5725#endif
Fred Draked86ed291999-12-15 15:34:33 +00005726#ifdef _MIPS_CS_AVAIL_PROCESSORS
5727 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
5728#endif
5729#ifdef _MIPS_CS_BASE
5730 {"MIPS_CS_BASE", _MIPS_CS_BASE},
5731#endif
5732#ifdef _MIPS_CS_HOSTID
5733 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
5734#endif
5735#ifdef _MIPS_CS_HW_NAME
5736 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
5737#endif
5738#ifdef _MIPS_CS_NUM_PROCESSORS
5739 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
5740#endif
5741#ifdef _MIPS_CS_OSREL_MAJ
5742 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
5743#endif
5744#ifdef _MIPS_CS_OSREL_MIN
5745 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
5746#endif
5747#ifdef _MIPS_CS_OSREL_PATCH
5748 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
5749#endif
5750#ifdef _MIPS_CS_OS_NAME
5751 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
5752#endif
5753#ifdef _MIPS_CS_OS_PROVIDER
5754 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
5755#endif
5756#ifdef _MIPS_CS_PROCESSORS
5757 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
5758#endif
5759#ifdef _MIPS_CS_SERIAL
5760 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
5761#endif
5762#ifdef _MIPS_CS_VENDOR
5763 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
5764#endif
Fred Drakec9680921999-12-13 16:37:25 +00005765};
5766
5767static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005768conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005769{
5770 return conv_confname(arg, valuep, posix_constants_confstr,
5771 sizeof(posix_constants_confstr)
5772 / sizeof(struct constdef));
5773}
5774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005775PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005776"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005777Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00005778
5779static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005780posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005781{
5782 PyObject *result = NULL;
5783 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005784 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00005785
5786 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005787 int len;
Fred Drakec9680921999-12-13 16:37:25 +00005788
Fred Drakec9680921999-12-13 16:37:25 +00005789 errno = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005790 len = confstr(name, buffer, sizeof(buffer));
5791 if (len == 0) {
5792 if (errno) {
5793 posix_error();
5794 }
5795 else {
5796 result = Py_None;
5797 Py_INCREF(Py_None);
5798 }
Fred Drakec9680921999-12-13 16:37:25 +00005799 }
5800 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005801 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00005802 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005803 if (result != NULL)
Neal Norwitz93c56822007-08-26 07:10:06 +00005804 confstr(name, PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00005805 }
5806 else
Neal Norwitz93c56822007-08-26 07:10:06 +00005807 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00005808 }
5809 }
5810 return result;
5811}
5812#endif
5813
5814
5815#ifdef HAVE_SYSCONF
5816static struct constdef posix_constants_sysconf[] = {
5817#ifdef _SC_2_CHAR_TERM
5818 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
5819#endif
5820#ifdef _SC_2_C_BIND
5821 {"SC_2_C_BIND", _SC_2_C_BIND},
5822#endif
5823#ifdef _SC_2_C_DEV
5824 {"SC_2_C_DEV", _SC_2_C_DEV},
5825#endif
5826#ifdef _SC_2_C_VERSION
5827 {"SC_2_C_VERSION", _SC_2_C_VERSION},
5828#endif
5829#ifdef _SC_2_FORT_DEV
5830 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
5831#endif
5832#ifdef _SC_2_FORT_RUN
5833 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
5834#endif
5835#ifdef _SC_2_LOCALEDEF
5836 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
5837#endif
5838#ifdef _SC_2_SW_DEV
5839 {"SC_2_SW_DEV", _SC_2_SW_DEV},
5840#endif
5841#ifdef _SC_2_UPE
5842 {"SC_2_UPE", _SC_2_UPE},
5843#endif
5844#ifdef _SC_2_VERSION
5845 {"SC_2_VERSION", _SC_2_VERSION},
5846#endif
Fred Draked86ed291999-12-15 15:34:33 +00005847#ifdef _SC_ABI_ASYNCHRONOUS_IO
5848 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
5849#endif
5850#ifdef _SC_ACL
5851 {"SC_ACL", _SC_ACL},
5852#endif
Fred Drakec9680921999-12-13 16:37:25 +00005853#ifdef _SC_AIO_LISTIO_MAX
5854 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
5855#endif
Fred Drakec9680921999-12-13 16:37:25 +00005856#ifdef _SC_AIO_MAX
5857 {"SC_AIO_MAX", _SC_AIO_MAX},
5858#endif
5859#ifdef _SC_AIO_PRIO_DELTA_MAX
5860 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
5861#endif
5862#ifdef _SC_ARG_MAX
5863 {"SC_ARG_MAX", _SC_ARG_MAX},
5864#endif
5865#ifdef _SC_ASYNCHRONOUS_IO
5866 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
5867#endif
5868#ifdef _SC_ATEXIT_MAX
5869 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
5870#endif
Fred Draked86ed291999-12-15 15:34:33 +00005871#ifdef _SC_AUDIT
5872 {"SC_AUDIT", _SC_AUDIT},
5873#endif
Fred Drakec9680921999-12-13 16:37:25 +00005874#ifdef _SC_AVPHYS_PAGES
5875 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
5876#endif
5877#ifdef _SC_BC_BASE_MAX
5878 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
5879#endif
5880#ifdef _SC_BC_DIM_MAX
5881 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
5882#endif
5883#ifdef _SC_BC_SCALE_MAX
5884 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
5885#endif
5886#ifdef _SC_BC_STRING_MAX
5887 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
5888#endif
Fred Draked86ed291999-12-15 15:34:33 +00005889#ifdef _SC_CAP
5890 {"SC_CAP", _SC_CAP},
5891#endif
Fred Drakec9680921999-12-13 16:37:25 +00005892#ifdef _SC_CHARCLASS_NAME_MAX
5893 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
5894#endif
5895#ifdef _SC_CHAR_BIT
5896 {"SC_CHAR_BIT", _SC_CHAR_BIT},
5897#endif
5898#ifdef _SC_CHAR_MAX
5899 {"SC_CHAR_MAX", _SC_CHAR_MAX},
5900#endif
5901#ifdef _SC_CHAR_MIN
5902 {"SC_CHAR_MIN", _SC_CHAR_MIN},
5903#endif
5904#ifdef _SC_CHILD_MAX
5905 {"SC_CHILD_MAX", _SC_CHILD_MAX},
5906#endif
5907#ifdef _SC_CLK_TCK
5908 {"SC_CLK_TCK", _SC_CLK_TCK},
5909#endif
5910#ifdef _SC_COHER_BLKSZ
5911 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
5912#endif
5913#ifdef _SC_COLL_WEIGHTS_MAX
5914 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
5915#endif
5916#ifdef _SC_DCACHE_ASSOC
5917 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
5918#endif
5919#ifdef _SC_DCACHE_BLKSZ
5920 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
5921#endif
5922#ifdef _SC_DCACHE_LINESZ
5923 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
5924#endif
5925#ifdef _SC_DCACHE_SZ
5926 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
5927#endif
5928#ifdef _SC_DCACHE_TBLKSZ
5929 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
5930#endif
5931#ifdef _SC_DELAYTIMER_MAX
5932 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
5933#endif
5934#ifdef _SC_EQUIV_CLASS_MAX
5935 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
5936#endif
5937#ifdef _SC_EXPR_NEST_MAX
5938 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
5939#endif
5940#ifdef _SC_FSYNC
5941 {"SC_FSYNC", _SC_FSYNC},
5942#endif
5943#ifdef _SC_GETGR_R_SIZE_MAX
5944 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
5945#endif
5946#ifdef _SC_GETPW_R_SIZE_MAX
5947 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
5948#endif
5949#ifdef _SC_ICACHE_ASSOC
5950 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
5951#endif
5952#ifdef _SC_ICACHE_BLKSZ
5953 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
5954#endif
5955#ifdef _SC_ICACHE_LINESZ
5956 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
5957#endif
5958#ifdef _SC_ICACHE_SZ
5959 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
5960#endif
Fred Draked86ed291999-12-15 15:34:33 +00005961#ifdef _SC_INF
5962 {"SC_INF", _SC_INF},
5963#endif
Fred Drakec9680921999-12-13 16:37:25 +00005964#ifdef _SC_INT_MAX
5965 {"SC_INT_MAX", _SC_INT_MAX},
5966#endif
5967#ifdef _SC_INT_MIN
5968 {"SC_INT_MIN", _SC_INT_MIN},
5969#endif
5970#ifdef _SC_IOV_MAX
5971 {"SC_IOV_MAX", _SC_IOV_MAX},
5972#endif
Fred Draked86ed291999-12-15 15:34:33 +00005973#ifdef _SC_IP_SECOPTS
5974 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
5975#endif
Fred Drakec9680921999-12-13 16:37:25 +00005976#ifdef _SC_JOB_CONTROL
5977 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
5978#endif
Fred Draked86ed291999-12-15 15:34:33 +00005979#ifdef _SC_KERN_POINTERS
5980 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
5981#endif
5982#ifdef _SC_KERN_SIM
5983 {"SC_KERN_SIM", _SC_KERN_SIM},
5984#endif
Fred Drakec9680921999-12-13 16:37:25 +00005985#ifdef _SC_LINE_MAX
5986 {"SC_LINE_MAX", _SC_LINE_MAX},
5987#endif
5988#ifdef _SC_LOGIN_NAME_MAX
5989 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
5990#endif
5991#ifdef _SC_LOGNAME_MAX
5992 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
5993#endif
5994#ifdef _SC_LONG_BIT
5995 {"SC_LONG_BIT", _SC_LONG_BIT},
5996#endif
Fred Draked86ed291999-12-15 15:34:33 +00005997#ifdef _SC_MAC
5998 {"SC_MAC", _SC_MAC},
5999#endif
Fred Drakec9680921999-12-13 16:37:25 +00006000#ifdef _SC_MAPPED_FILES
6001 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6002#endif
6003#ifdef _SC_MAXPID
6004 {"SC_MAXPID", _SC_MAXPID},
6005#endif
6006#ifdef _SC_MB_LEN_MAX
6007 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6008#endif
6009#ifdef _SC_MEMLOCK
6010 {"SC_MEMLOCK", _SC_MEMLOCK},
6011#endif
6012#ifdef _SC_MEMLOCK_RANGE
6013 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6014#endif
6015#ifdef _SC_MEMORY_PROTECTION
6016 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6017#endif
6018#ifdef _SC_MESSAGE_PASSING
6019 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6020#endif
Fred Draked86ed291999-12-15 15:34:33 +00006021#ifdef _SC_MMAP_FIXED_ALIGNMENT
6022 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6023#endif
Fred Drakec9680921999-12-13 16:37:25 +00006024#ifdef _SC_MQ_OPEN_MAX
6025 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6026#endif
6027#ifdef _SC_MQ_PRIO_MAX
6028 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6029#endif
Fred Draked86ed291999-12-15 15:34:33 +00006030#ifdef _SC_NACLS_MAX
6031 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6032#endif
Fred Drakec9680921999-12-13 16:37:25 +00006033#ifdef _SC_NGROUPS_MAX
6034 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6035#endif
6036#ifdef _SC_NL_ARGMAX
6037 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6038#endif
6039#ifdef _SC_NL_LANGMAX
6040 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6041#endif
6042#ifdef _SC_NL_MSGMAX
6043 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6044#endif
6045#ifdef _SC_NL_NMAX
6046 {"SC_NL_NMAX", _SC_NL_NMAX},
6047#endif
6048#ifdef _SC_NL_SETMAX
6049 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6050#endif
6051#ifdef _SC_NL_TEXTMAX
6052 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6053#endif
6054#ifdef _SC_NPROCESSORS_CONF
6055 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6056#endif
6057#ifdef _SC_NPROCESSORS_ONLN
6058 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6059#endif
Fred Draked86ed291999-12-15 15:34:33 +00006060#ifdef _SC_NPROC_CONF
6061 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6062#endif
6063#ifdef _SC_NPROC_ONLN
6064 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6065#endif
Fred Drakec9680921999-12-13 16:37:25 +00006066#ifdef _SC_NZERO
6067 {"SC_NZERO", _SC_NZERO},
6068#endif
6069#ifdef _SC_OPEN_MAX
6070 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6071#endif
6072#ifdef _SC_PAGESIZE
6073 {"SC_PAGESIZE", _SC_PAGESIZE},
6074#endif
6075#ifdef _SC_PAGE_SIZE
6076 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6077#endif
6078#ifdef _SC_PASS_MAX
6079 {"SC_PASS_MAX", _SC_PASS_MAX},
6080#endif
6081#ifdef _SC_PHYS_PAGES
6082 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6083#endif
6084#ifdef _SC_PII
6085 {"SC_PII", _SC_PII},
6086#endif
6087#ifdef _SC_PII_INTERNET
6088 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6089#endif
6090#ifdef _SC_PII_INTERNET_DGRAM
6091 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6092#endif
6093#ifdef _SC_PII_INTERNET_STREAM
6094 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6095#endif
6096#ifdef _SC_PII_OSI
6097 {"SC_PII_OSI", _SC_PII_OSI},
6098#endif
6099#ifdef _SC_PII_OSI_CLTS
6100 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6101#endif
6102#ifdef _SC_PII_OSI_COTS
6103 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6104#endif
6105#ifdef _SC_PII_OSI_M
6106 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6107#endif
6108#ifdef _SC_PII_SOCKET
6109 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6110#endif
6111#ifdef _SC_PII_XTI
6112 {"SC_PII_XTI", _SC_PII_XTI},
6113#endif
6114#ifdef _SC_POLL
6115 {"SC_POLL", _SC_POLL},
6116#endif
6117#ifdef _SC_PRIORITIZED_IO
6118 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6119#endif
6120#ifdef _SC_PRIORITY_SCHEDULING
6121 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6122#endif
6123#ifdef _SC_REALTIME_SIGNALS
6124 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6125#endif
6126#ifdef _SC_RE_DUP_MAX
6127 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6128#endif
6129#ifdef _SC_RTSIG_MAX
6130 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6131#endif
6132#ifdef _SC_SAVED_IDS
6133 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6134#endif
6135#ifdef _SC_SCHAR_MAX
6136 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6137#endif
6138#ifdef _SC_SCHAR_MIN
6139 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6140#endif
6141#ifdef _SC_SELECT
6142 {"SC_SELECT", _SC_SELECT},
6143#endif
6144#ifdef _SC_SEMAPHORES
6145 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6146#endif
6147#ifdef _SC_SEM_NSEMS_MAX
6148 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6149#endif
6150#ifdef _SC_SEM_VALUE_MAX
6151 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6152#endif
6153#ifdef _SC_SHARED_MEMORY_OBJECTS
6154 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6155#endif
6156#ifdef _SC_SHRT_MAX
6157 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6158#endif
6159#ifdef _SC_SHRT_MIN
6160 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6161#endif
6162#ifdef _SC_SIGQUEUE_MAX
6163 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6164#endif
6165#ifdef _SC_SIGRT_MAX
6166 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6167#endif
6168#ifdef _SC_SIGRT_MIN
6169 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6170#endif
Fred Draked86ed291999-12-15 15:34:33 +00006171#ifdef _SC_SOFTPOWER
6172 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6173#endif
Fred Drakec9680921999-12-13 16:37:25 +00006174#ifdef _SC_SPLIT_CACHE
6175 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6176#endif
6177#ifdef _SC_SSIZE_MAX
6178 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6179#endif
6180#ifdef _SC_STACK_PROT
6181 {"SC_STACK_PROT", _SC_STACK_PROT},
6182#endif
6183#ifdef _SC_STREAM_MAX
6184 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6185#endif
6186#ifdef _SC_SYNCHRONIZED_IO
6187 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6188#endif
6189#ifdef _SC_THREADS
6190 {"SC_THREADS", _SC_THREADS},
6191#endif
6192#ifdef _SC_THREAD_ATTR_STACKADDR
6193 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6194#endif
6195#ifdef _SC_THREAD_ATTR_STACKSIZE
6196 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6197#endif
6198#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6199 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6200#endif
6201#ifdef _SC_THREAD_KEYS_MAX
6202 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6203#endif
6204#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6205 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6206#endif
6207#ifdef _SC_THREAD_PRIO_INHERIT
6208 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6209#endif
6210#ifdef _SC_THREAD_PRIO_PROTECT
6211 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6212#endif
6213#ifdef _SC_THREAD_PROCESS_SHARED
6214 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6215#endif
6216#ifdef _SC_THREAD_SAFE_FUNCTIONS
6217 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6218#endif
6219#ifdef _SC_THREAD_STACK_MIN
6220 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6221#endif
6222#ifdef _SC_THREAD_THREADS_MAX
6223 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6224#endif
6225#ifdef _SC_TIMERS
6226 {"SC_TIMERS", _SC_TIMERS},
6227#endif
6228#ifdef _SC_TIMER_MAX
6229 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6230#endif
6231#ifdef _SC_TTY_NAME_MAX
6232 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6233#endif
6234#ifdef _SC_TZNAME_MAX
6235 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6236#endif
6237#ifdef _SC_T_IOV_MAX
6238 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6239#endif
6240#ifdef _SC_UCHAR_MAX
6241 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6242#endif
6243#ifdef _SC_UINT_MAX
6244 {"SC_UINT_MAX", _SC_UINT_MAX},
6245#endif
6246#ifdef _SC_UIO_MAXIOV
6247 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6248#endif
6249#ifdef _SC_ULONG_MAX
6250 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6251#endif
6252#ifdef _SC_USHRT_MAX
6253 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6254#endif
6255#ifdef _SC_VERSION
6256 {"SC_VERSION", _SC_VERSION},
6257#endif
6258#ifdef _SC_WORD_BIT
6259 {"SC_WORD_BIT", _SC_WORD_BIT},
6260#endif
6261#ifdef _SC_XBS5_ILP32_OFF32
6262 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6263#endif
6264#ifdef _SC_XBS5_ILP32_OFFBIG
6265 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6266#endif
6267#ifdef _SC_XBS5_LP64_OFF64
6268 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6269#endif
6270#ifdef _SC_XBS5_LPBIG_OFFBIG
6271 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6272#endif
6273#ifdef _SC_XOPEN_CRYPT
6274 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6275#endif
6276#ifdef _SC_XOPEN_ENH_I18N
6277 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6278#endif
6279#ifdef _SC_XOPEN_LEGACY
6280 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6281#endif
6282#ifdef _SC_XOPEN_REALTIME
6283 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6284#endif
6285#ifdef _SC_XOPEN_REALTIME_THREADS
6286 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6287#endif
6288#ifdef _SC_XOPEN_SHM
6289 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6290#endif
6291#ifdef _SC_XOPEN_UNIX
6292 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6293#endif
6294#ifdef _SC_XOPEN_VERSION
6295 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6296#endif
6297#ifdef _SC_XOPEN_XCU_VERSION
6298 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6299#endif
6300#ifdef _SC_XOPEN_XPG2
6301 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6302#endif
6303#ifdef _SC_XOPEN_XPG3
6304 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6305#endif
6306#ifdef _SC_XOPEN_XPG4
6307 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6308#endif
6309};
6310
6311static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006312conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006313{
6314 return conv_confname(arg, valuep, posix_constants_sysconf,
6315 sizeof(posix_constants_sysconf)
6316 / sizeof(struct constdef));
6317}
6318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006319PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006320"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006321Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006322
6323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006324posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006325{
6326 PyObject *result = NULL;
6327 int name;
6328
6329 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6330 int value;
6331
6332 errno = 0;
6333 value = sysconf(name);
6334 if (value == -1 && errno != 0)
6335 posix_error();
6336 else
6337 result = PyInt_FromLong(value);
6338 }
6339 return result;
6340}
6341#endif
6342
6343
Fred Drakebec628d1999-12-15 18:31:10 +00006344/* This code is used to ensure that the tables of configuration value names
6345 * are in sorted order as required by conv_confname(), and also to build the
6346 * the exported dictionaries that are used to publish information about the
6347 * names available on the host platform.
6348 *
6349 * Sorting the table at runtime ensures that the table is properly ordered
6350 * when used, even for platforms we're not able to test on. It also makes
6351 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006352 */
Fred Drakebec628d1999-12-15 18:31:10 +00006353
6354static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006355cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006356{
6357 const struct constdef *c1 =
6358 (const struct constdef *) v1;
6359 const struct constdef *c2 =
6360 (const struct constdef *) v2;
6361
6362 return strcmp(c1->name, c2->name);
6363}
6364
6365static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006366setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006367 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006368{
Fred Drakebec628d1999-12-15 18:31:10 +00006369 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006370 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006371
6372 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6373 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006374 if (d == NULL)
6375 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006376
Barry Warsaw3155db32000-04-13 15:20:40 +00006377 for (i=0; i < tablesize; ++i) {
6378 PyObject *o = PyInt_FromLong(table[i].value);
6379 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6380 Py_XDECREF(o);
6381 Py_DECREF(d);
6382 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006383 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006384 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006385 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006386 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006387}
6388
Fred Drakebec628d1999-12-15 18:31:10 +00006389/* Return -1 on failure, 0 on success. */
6390static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006391setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006392{
6393#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006394 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006395 sizeof(posix_constants_pathconf)
6396 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006397 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006398 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006399#endif
6400#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006401 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006402 sizeof(posix_constants_confstr)
6403 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006404 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006405 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006406#endif
6407#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006408 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006409 sizeof(posix_constants_sysconf)
6410 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006411 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006412 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006413#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006414 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006415}
Fred Draked86ed291999-12-15 15:34:33 +00006416
6417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006418PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006419"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006420Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006421in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006422
6423static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006424posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006425{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006426 abort();
6427 /*NOTREACHED*/
6428 Py_FatalError("abort() called from Python code didn't abort!");
6429 return NULL;
6430}
Fred Drakebec628d1999-12-15 18:31:10 +00006431
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006432#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006433PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006434"startfile(filepath [, operation]) - Start a file with its associated\n\
6435application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006436\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006437When \"operation\" is not specified or \"open\", this acts like\n\
6438double-clicking the file in Explorer, or giving the file name as an\n\
6439argument to the DOS \"start\" command: the file is opened with whatever\n\
6440application (if any) its extension is associated.\n\
6441When another \"operation\" is given, it specifies what should be done with\n\
6442the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006443\n\
6444startfile returns as soon as the associated application is launched.\n\
6445There is no option to wait for the application to close, and no way\n\
6446to retrieve the application's exit status.\n\
6447\n\
6448The filepath is relative to the current directory. If you want to use\n\
6449an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006450the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006451
6452static PyObject *
6453win32_startfile(PyObject *self, PyObject *args)
6454{
6455 char *filepath;
Georg Brandlf4f44152006-02-18 22:29:33 +00006456 char *operation = NULL;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006457 HINSTANCE rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006458#ifdef Py_WIN_WIDE_FILENAMES
6459 if (unicode_file_names()) {
6460 PyObject *unipath, *woperation = NULL;
6461 if (!PyArg_ParseTuple(args, "U|s:startfile",
6462 &unipath, &operation)) {
6463 PyErr_Clear();
6464 goto normal;
6465 }
6466
6467
6468 if (operation) {
6469 woperation = PyUnicode_DecodeASCII(operation,
6470 strlen(operation), NULL);
6471 if (!woperation) {
6472 PyErr_Clear();
6473 operation = NULL;
6474 goto normal;
6475 }
6476 }
6477
6478 Py_BEGIN_ALLOW_THREADS
6479 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6480 PyUnicode_AS_UNICODE(unipath),
6481 NULL, NULL, SW_SHOWNORMAL);
6482 Py_END_ALLOW_THREADS
6483
6484 Py_XDECREF(woperation);
6485 if (rc <= (HINSTANCE)32) {
6486 PyObject *errval = win32_error_unicode("startfile",
6487 PyUnicode_AS_UNICODE(unipath));
6488 return errval;
6489 }
6490 Py_INCREF(Py_None);
6491 return Py_None;
6492 }
6493#endif
6494
6495normal:
Georg Brandlf4f44152006-02-18 22:29:33 +00006496 if (!PyArg_ParseTuple(args, "et|s:startfile",
6497 Py_FileSystemDefaultEncoding, &filepath,
6498 &operation))
Tim Petersf58a7aa2000-09-22 10:05:54 +00006499 return NULL;
6500 Py_BEGIN_ALLOW_THREADS
Georg Brandlf4f44152006-02-18 22:29:33 +00006501 rc = ShellExecute((HWND)0, operation, filepath,
6502 NULL, NULL, SW_SHOWNORMAL);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006503 Py_END_ALLOW_THREADS
Georg Brandle9f8ec92005-09-25 06:16:40 +00006504 if (rc <= (HINSTANCE)32) {
6505 PyObject *errval = win32_error("startfile", filepath);
6506 PyMem_Free(filepath);
6507 return errval;
6508 }
6509 PyMem_Free(filepath);
Tim Petersf58a7aa2000-09-22 10:05:54 +00006510 Py_INCREF(Py_None);
6511 return Py_None;
6512}
6513#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006514
Martin v. Löwis438b5342002-12-27 10:16:42 +00006515#ifdef HAVE_GETLOADAVG
6516PyDoc_STRVAR(posix_getloadavg__doc__,
6517"getloadavg() -> (float, float, float)\n\n\
6518Return the number of processes in the system run queue averaged over\n\
6519the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6520was unobtainable");
6521
6522static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006523posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006524{
6525 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006526 if (getloadavg(loadavg, 3)!=3) {
6527 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6528 return NULL;
6529 } else
6530 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6531}
6532#endif
6533
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006534#ifdef MS_WINDOWS
6535
6536PyDoc_STRVAR(win32_urandom__doc__,
6537"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006538Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006539
6540typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6541 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6542 DWORD dwFlags );
6543typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6544 BYTE *pbBuffer );
6545
6546static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006547/* This handle is never explicitly released. Instead, the operating
6548 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006549static HCRYPTPROV hCryptProv = 0;
6550
Tim Peters4ad82172004-08-30 17:02:04 +00006551static PyObject*
6552win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006553{
Tim Petersd3115382004-08-30 17:36:46 +00006554 int howMany;
6555 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006556
Tim Peters4ad82172004-08-30 17:02:04 +00006557 /* Read arguments */
Tim Peters9b279a82004-08-30 17:10:53 +00006558 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
Tim Peters4ad82172004-08-30 17:02:04 +00006559 return NULL;
Tim Peters51eba612004-08-30 17:08:02 +00006560 if (howMany < 0)
6561 return PyErr_Format(PyExc_ValueError,
6562 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006563
Tim Peters4ad82172004-08-30 17:02:04 +00006564 if (hCryptProv == 0) {
6565 HINSTANCE hAdvAPI32 = NULL;
6566 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006567
Tim Peters4ad82172004-08-30 17:02:04 +00006568 /* Obtain handle to the DLL containing CryptoAPI
6569 This should not fail */
6570 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6571 if(hAdvAPI32 == NULL)
6572 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006573
Tim Peters4ad82172004-08-30 17:02:04 +00006574 /* Obtain pointers to the CryptoAPI functions
6575 This will fail on some early versions of Win95 */
6576 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6577 hAdvAPI32,
6578 "CryptAcquireContextA");
6579 if (pCryptAcquireContext == NULL)
6580 return PyErr_Format(PyExc_NotImplementedError,
6581 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006582
Tim Peters4ad82172004-08-30 17:02:04 +00006583 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6584 hAdvAPI32, "CryptGenRandom");
Thomas Wouters89f507f2006-12-13 04:49:30 +00006585 if (pCryptGenRandom == NULL)
Tim Peters4ad82172004-08-30 17:02:04 +00006586 return PyErr_Format(PyExc_NotImplementedError,
6587 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006588
Tim Peters4ad82172004-08-30 17:02:04 +00006589 /* Acquire context */
6590 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6591 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6592 return win32_error("CryptAcquireContext", NULL);
6593 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006594
Tim Peters4ad82172004-08-30 17:02:04 +00006595 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006596 result = PyBytes_FromStringAndSize(NULL, howMany);
Tim Petersd3115382004-08-30 17:36:46 +00006597 if (result != NULL) {
6598 /* Get random data */
6599 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006600 PyBytes_AS_STRING(result))) {
Tim Petersd3115382004-08-30 17:36:46 +00006601 Py_DECREF(result);
6602 return win32_error("CryptGenRandom", NULL);
6603 }
Tim Peters4ad82172004-08-30 17:02:04 +00006604 }
Tim Petersd3115382004-08-30 17:36:46 +00006605 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006606}
6607#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006608
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006609PyDoc_STRVAR(device_encoding__doc__,
6610"device_encoding(fd) -> str\n\n\
6611Return a string describing the encoding of the device\n\
6612if the output is a terminal; else return None.");
6613
6614static PyObject *
6615device_encoding(PyObject *self, PyObject *args)
6616{
6617 int fd;
6618 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6619 return NULL;
6620 if (!isatty(fd)) {
6621 Py_INCREF(Py_None);
6622 return Py_None;
6623 }
6624#if defined(MS_WINDOWS) || defined(MS_WIN64)
6625 if (fd == 0) {
6626 char buf[100];
6627 sprintf(buf, "cp%d", GetConsoleCP());
6628 return PyUnicode_FromString(buf);
6629 }
6630 if (fd == 1 || fd == 2) {
6631 char buf[100];
6632 sprintf(buf, "cp%d", GetConsoleOutputCP());
6633 return PyUnicode_FromString(buf);
6634 }
6635#elif defined(CODESET)
6636 {
6637 char *codeset = nl_langinfo(CODESET);
6638 if (codeset)
6639 return PyUnicode_FromString(codeset);
6640 }
6641#endif
6642 Py_INCREF(Py_None);
6643 return Py_None;
6644}
6645
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006646#ifdef __VMS
6647/* Use openssl random routine */
6648#include <openssl/rand.h>
6649PyDoc_STRVAR(vms_urandom__doc__,
6650"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006651Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006652
6653static PyObject*
6654vms_urandom(PyObject *self, PyObject *args)
6655{
6656 int howMany;
6657 PyObject* result;
6658
6659 /* Read arguments */
6660 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6661 return NULL;
6662 if (howMany < 0)
6663 return PyErr_Format(PyExc_ValueError,
6664 "negative argument not allowed");
6665
6666 /* Allocate bytes */
Neal Norwitz93c56822007-08-26 07:10:06 +00006667 result = PyBytes_FromStringAndSize(NULL, howMany);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006668 if (result != NULL) {
6669 /* Get random data */
6670 if (RAND_pseudo_bytes((unsigned char*)
Neal Norwitz93c56822007-08-26 07:10:06 +00006671 PyBytes_AS_STRING(result),
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006672 howMany) < 0) {
6673 Py_DECREF(result);
6674 return PyErr_Format(PyExc_ValueError,
6675 "RAND_pseudo_bytes");
6676 }
6677 }
6678 return result;
6679}
6680#endif
6681
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006682static PyMethodDef posix_methods[] = {
6683 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6684#ifdef HAVE_TTYNAME
6685 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6686#endif
6687 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00006688#ifdef HAVE_CHFLAGS
6689 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
6690#endif /* HAVE_CHFLAGS */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006691 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006692#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006693 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006694#endif /* HAVE_CHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00006695#ifdef HAVE_LCHFLAGS
6696 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
6697#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006698#ifdef HAVE_LCHOWN
6699 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6700#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006701#ifdef HAVE_CHROOT
6702 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6703#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006704#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006705 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006706#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006707#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006708 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Neal Norwitze241ce82003-02-17 18:17:05 +00006709 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006710#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006711#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006712 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006713#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006714 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6715 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6716 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006717#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006718 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006719#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006720#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006721 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006722#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006723 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6724 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6725 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006726 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006727#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006728 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006729#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006730#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006731 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006732#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006733 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006734#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006735 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006736#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006737 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6738 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6739 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006740#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006741 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006742#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006743 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006744#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006745 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6746 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006747#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006748#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006749 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6750 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006751#if defined(PYOS_OS2)
6752 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
6753 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
6754#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00006755#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006756#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006757 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006758#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006759#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006760 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006761#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006762#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006763 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006764#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006765#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006766 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006767#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006768#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006769 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006770#endif /* HAVE_GETEGID */
6771#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006772 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006773#endif /* HAVE_GETEUID */
6774#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006775 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006776#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006777#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006778 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006779#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006780 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006781#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006782 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006783#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006784#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006785 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006786#endif /* HAVE_GETPPID */
6787#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006788 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006789#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006790#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006791 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006792#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006793#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006794 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006795#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006796#ifdef HAVE_KILLPG
6797 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6798#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006799#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006800 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006801#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00006802#ifdef MS_WINDOWS
6803 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
6804#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006805#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006806 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006807#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006808#ifdef HAVE_SETEUID
6809 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6810#endif /* HAVE_SETEUID */
6811#ifdef HAVE_SETEGID
6812 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6813#endif /* HAVE_SETEGID */
6814#ifdef HAVE_SETREUID
6815 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6816#endif /* HAVE_SETREUID */
6817#ifdef HAVE_SETREGID
6818 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6819#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006820#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006821 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006822#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006823#ifdef HAVE_SETGROUPS
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006824 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006825#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00006826#ifdef HAVE_GETPGID
6827 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
6828#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006829#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006830 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006831#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006832#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00006833 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006834#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006835#ifdef HAVE_WAIT3
6836 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
6837#endif /* HAVE_WAIT3 */
6838#ifdef HAVE_WAIT4
6839 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
6840#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00006841#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006842 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006843#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006844#ifdef HAVE_GETSID
6845 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
6846#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006847#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00006848 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006849#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006850#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006851 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006852#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006853#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006854 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006855#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006856#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006857 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006858#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006859 {"open", posix_open, METH_VARARGS, posix_open__doc__},
6860 {"close", posix_close, METH_VARARGS, posix_close__doc__},
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006861 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006862 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
6863 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
6864 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
6865 {"read", posix_read, METH_VARARGS, posix_read__doc__},
6866 {"write", posix_write, METH_VARARGS, posix_write__doc__},
6867 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00006868 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006869#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00006870 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006871#endif
6872#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006873 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006874#endif
Neal Norwitz11690112002-07-30 01:08:28 +00006875#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006876 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
6877#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006878#ifdef HAVE_DEVICE_MACROS
6879 {"major", posix_major, METH_VARARGS, posix_major__doc__},
6880 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6881 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6882#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006883#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006884 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006885#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006886#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006887 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006888#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006889#ifdef HAVE_UNSETENV
6890 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
6891#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00006892#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006893 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00006894#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00006895#ifdef HAVE_FCHDIR
6896 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6897#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00006898#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006899 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006900#endif
6901#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00006902 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00006903#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00006904#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00006905#ifdef WCOREDUMP
6906 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
6907#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006908#ifdef WIFCONTINUED
6909 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
6910#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00006911#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006912 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006913#endif /* WIFSTOPPED */
6914#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006915 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006916#endif /* WIFSIGNALED */
6917#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006918 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006919#endif /* WIFEXITED */
6920#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006921 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006922#endif /* WEXITSTATUS */
6923#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006924 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006925#endif /* WTERMSIG */
6926#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006927 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00006928#endif /* WSTOPSIG */
6929#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00006930#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006931 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006932#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00006933#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006934 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00006935#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00006936#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00006937 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006938#endif
6939#ifdef HAVE_TEMPNAM
6940 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
6941#endif
6942#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00006943 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006944#endif
Fred Drakec9680921999-12-13 16:37:25 +00006945#ifdef HAVE_CONFSTR
6946 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
6947#endif
6948#ifdef HAVE_SYSCONF
6949 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
6950#endif
6951#ifdef HAVE_FPATHCONF
6952 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
6953#endif
6954#ifdef HAVE_PATHCONF
6955 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
6956#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006957 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006958#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00006959 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
6960#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006961#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00006962 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00006963#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006964 #ifdef MS_WINDOWS
6965 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
6966 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006967 #ifdef __VMS
6968 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
6969 #endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006970 {NULL, NULL} /* Sentinel */
6971};
6972
6973
Barry Warsaw4a342091996-12-19 23:50:02 +00006974static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006975ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00006976{
Fred Drake4d1e64b2002-04-15 19:40:07 +00006977 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00006978}
6979
Guido van Rossumd48f2521997-12-05 22:19:34 +00006980#if defined(PYOS_OS2)
6981/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00006982static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006983{
6984 APIRET rc;
6985 ULONG values[QSV_MAX+1];
6986 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00006987 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00006988
6989 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00006990 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006991 Py_END_ALLOW_THREADS
6992
6993 if (rc != NO_ERROR) {
6994 os2_error(rc);
6995 return -1;
6996 }
6997
Fred Drake4d1e64b2002-04-15 19:40:07 +00006998 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6999 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7000 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7001 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7002 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7003 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7004 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007005
7006 switch (values[QSV_VERSION_MINOR]) {
7007 case 0: ver = "2.00"; break;
7008 case 10: ver = "2.10"; break;
7009 case 11: ver = "2.11"; break;
7010 case 30: ver = "3.00"; break;
7011 case 40: ver = "4.00"; break;
7012 case 50: ver = "5.00"; break;
7013 default:
Tim Peters885d4572001-11-28 20:27:42 +00007014 PyOS_snprintf(tmp, sizeof(tmp),
7015 "%d-%d", values[QSV_VERSION_MAJOR],
7016 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007017 ver = &tmp[0];
7018 }
7019
7020 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007021 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007022 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007023
7024 /* Add Indicator of Which Drive was Used to Boot the System */
7025 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7026 tmp[1] = ':';
7027 tmp[2] = '\0';
7028
Fred Drake4d1e64b2002-04-15 19:40:07 +00007029 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007030}
7031#endif
7032
Barry Warsaw4a342091996-12-19 23:50:02 +00007033static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007034all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007035{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007036#ifdef F_OK
7037 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007038#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007039#ifdef R_OK
7040 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007041#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007042#ifdef W_OK
7043 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007044#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007045#ifdef X_OK
7046 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007047#endif
Fred Drakec9680921999-12-13 16:37:25 +00007048#ifdef NGROUPS_MAX
7049 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7050#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007051#ifdef TMP_MAX
7052 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7053#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007054#ifdef WCONTINUED
7055 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7056#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007057#ifdef WNOHANG
7058 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007059#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007060#ifdef WUNTRACED
7061 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7062#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007063#ifdef O_RDONLY
7064 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7065#endif
7066#ifdef O_WRONLY
7067 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7068#endif
7069#ifdef O_RDWR
7070 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7071#endif
7072#ifdef O_NDELAY
7073 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7074#endif
7075#ifdef O_NONBLOCK
7076 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7077#endif
7078#ifdef O_APPEND
7079 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7080#endif
7081#ifdef O_DSYNC
7082 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7083#endif
7084#ifdef O_RSYNC
7085 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7086#endif
7087#ifdef O_SYNC
7088 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7089#endif
7090#ifdef O_NOCTTY
7091 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7092#endif
7093#ifdef O_CREAT
7094 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7095#endif
7096#ifdef O_EXCL
7097 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7098#endif
7099#ifdef O_TRUNC
7100 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7101#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007102#ifdef O_BINARY
7103 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7104#endif
7105#ifdef O_TEXT
7106 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7107#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007108#ifdef O_LARGEFILE
7109 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7110#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007111#ifdef O_SHLOCK
7112 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
7113#endif
7114#ifdef O_EXLOCK
7115 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
7116#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007117
Tim Peters5aa91602002-01-30 05:46:57 +00007118/* MS Windows */
7119#ifdef O_NOINHERIT
7120 /* Don't inherit in child processes. */
7121 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7122#endif
7123#ifdef _O_SHORT_LIVED
7124 /* Optimize for short life (keep in memory). */
7125 /* MS forgot to define this one with a non-underscore form too. */
7126 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7127#endif
7128#ifdef O_TEMPORARY
7129 /* Automatically delete when last handle is closed. */
7130 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7131#endif
7132#ifdef O_RANDOM
7133 /* Optimize for random access. */
7134 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7135#endif
7136#ifdef O_SEQUENTIAL
7137 /* Optimize for sequential access. */
7138 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7139#endif
7140
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007141/* GNU extensions. */
7142#ifdef O_DIRECT
7143 /* Direct disk access. */
7144 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7145#endif
7146#ifdef O_DIRECTORY
7147 /* Must be a directory. */
7148 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7149#endif
7150#ifdef O_NOFOLLOW
7151 /* Do not follow links. */
7152 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7153#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007154
Barry Warsaw5676bd12003-01-07 20:57:09 +00007155 /* These come from sysexits.h */
7156#ifdef EX_OK
7157 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007158#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007159#ifdef EX_USAGE
7160 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007161#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007162#ifdef EX_DATAERR
7163 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007164#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007165#ifdef EX_NOINPUT
7166 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007167#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007168#ifdef EX_NOUSER
7169 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007170#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007171#ifdef EX_NOHOST
7172 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007173#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007174#ifdef EX_UNAVAILABLE
7175 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007176#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007177#ifdef EX_SOFTWARE
7178 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007179#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007180#ifdef EX_OSERR
7181 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007182#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007183#ifdef EX_OSFILE
7184 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007185#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007186#ifdef EX_CANTCREAT
7187 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007188#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007189#ifdef EX_IOERR
7190 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007191#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007192#ifdef EX_TEMPFAIL
7193 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007194#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007195#ifdef EX_PROTOCOL
7196 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007197#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007198#ifdef EX_NOPERM
7199 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007200#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007201#ifdef EX_CONFIG
7202 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007203#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007204#ifdef EX_NOTFOUND
7205 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007206#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007207
Guido van Rossum246bc171999-02-01 23:54:31 +00007208#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007209#if defined(PYOS_OS2) && defined(PYCC_GCC)
7210 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7211 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7212 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7213 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7214 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7215 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7216 if (ins(d, "P_PM", (long)P_PM)) return -1;
7217 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7218 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7219 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7220 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7221 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7222 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7223 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7224 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7225 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7226 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7227 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7228 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7229 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7230#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007231 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7232 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7233 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7234 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7235 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007236#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007237#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007238
Guido van Rossumd48f2521997-12-05 22:19:34 +00007239#if defined(PYOS_OS2)
7240 if (insertvalues(d)) return -1;
7241#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007242 return 0;
7243}
7244
7245
Tim Peters5aa91602002-01-30 05:46:57 +00007246#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007247#define INITFUNC initnt
7248#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007249
7250#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007251#define INITFUNC initos2
7252#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007253
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007254#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007255#define INITFUNC initposix
7256#define MODNAME "posix"
7257#endif
7258
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007259PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007260INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007261{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007262 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007263
Fred Drake4d1e64b2002-04-15 19:40:07 +00007264 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007265 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007266 posix__doc__);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00007267 if (m == NULL)
7268 return;
Tim Peters5aa91602002-01-30 05:46:57 +00007269
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007270 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007271 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007272 Py_XINCREF(v);
7273 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007274 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007275 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007276
Fred Drake4d1e64b2002-04-15 19:40:07 +00007277 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007278 return;
7279
Fred Drake4d1e64b2002-04-15 19:40:07 +00007280 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007281 return;
7282
Fred Drake4d1e64b2002-04-15 19:40:07 +00007283 Py_INCREF(PyExc_OSError);
7284 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007285
Guido van Rossumb3d39562000-01-31 18:41:26 +00007286#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007287 if (posix_putenv_garbage == NULL)
7288 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007289#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007291 if (!initialized) {
7292 stat_result_desc.name = MODNAME ".stat_result";
7293 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7294 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7295 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7296 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7297 structseq_new = StatResultType.tp_new;
7298 StatResultType.tp_new = statresult_new;
7299
7300 statvfs_result_desc.name = MODNAME ".statvfs_result";
7301 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7302 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007303 Py_INCREF((PyObject*) &StatResultType);
7304 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007305 Py_INCREF((PyObject*) &StatVFSResultType);
7306 PyModule_AddObject(m, "statvfs_result",
7307 (PyObject*) &StatVFSResultType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007308 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007309
7310#ifdef __APPLE__
7311 /*
7312 * Step 2 of weak-linking support on Mac OS X.
7313 *
7314 * The code below removes functions that are not available on the
7315 * currently active platform.
7316 *
7317 * This block allow one to use a python binary that was build on
7318 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7319 * OSX 10.4.
7320 */
7321#ifdef HAVE_FSTATVFS
7322 if (fstatvfs == NULL) {
7323 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7324 return;
7325 }
7326 }
7327#endif /* HAVE_FSTATVFS */
7328
7329#ifdef HAVE_STATVFS
7330 if (statvfs == NULL) {
7331 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7332 return;
7333 }
7334 }
7335#endif /* HAVE_STATVFS */
7336
7337# ifdef HAVE_LCHOWN
7338 if (lchown == NULL) {
7339 if (PyObject_DelAttrString(m, "lchown") == -1) {
7340 return;
7341 }
7342 }
7343#endif /* HAVE_LCHOWN */
7344
7345
7346#endif /* __APPLE__ */
7347
Guido van Rossumb6775db1994-08-01 11:34:53 +00007348}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007349
7350#ifdef __cplusplus
7351}
7352#endif