blob: aa9f36ca7da6ad68eb186bef02d876a58e0d628c [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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000016#include "Python.h"
17#include "structseq.h"
18
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000019#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000020# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000021#endif /* defined(__VMS) */
22
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000023PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000024"This module provides access to operating system functionality that is\n\
25standardized by the C Standard and the POSIX standard (a thinly\n\
26disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000029#ifndef Py_USING_UNICODE
30/* This is used in signatures of functions. */
31#define Py_UNICODE void
32#endif
33
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000034#if defined(PYOS_OS2)
35#define INCL_DOS
36#define INCL_DOSERRORS
37#define INCL_DOSPROCESS
38#define INCL_NOPMAPI
39#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000040#if defined(PYCC_GCC)
41#include <ctype.h>
42#include <io.h>
43#include <stdio.h>
44#include <process.h>
45#include "osdefs.h"
46#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#endif
48
Guido van Rossumb6775db1994-08-01 11:34:53 +000049#include <sys/types.h>
50#include <sys/stat.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000051
Guido van Rossum36bc6801995-06-14 22:54:23 +000052#ifdef HAVE_SYS_WAIT_H
53#include <sys/wait.h> /* For WNOHANG */
54#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000055
Guido van Rossuma376cc51996-12-05 23:43:35 +000056#ifdef HAVE_SIGNAL_H
57#include <signal.h>
58#endif
59
Guido van Rossumb6775db1994-08-01 11:34:53 +000060#ifdef HAVE_FCNTL_H
61#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000062#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000063
Guido van Rossuma6535fd2001-10-18 19:44:10 +000064#ifdef HAVE_GRP_H
65#include <grp.h>
66#endif
67
Barry Warsaw5676bd12003-01-07 20:57:09 +000068#ifdef HAVE_SYSEXITS_H
69#include <sysexits.h>
70#endif /* HAVE_SYSEXITS_H */
71
Guido van Rossuma4916fa1996-05-23 22:58:55 +000072/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000073/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000074#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000075#include <process.h>
76#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000077#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000078#define HAVE_GETCWD 1
79#define HAVE_OPENDIR 1
80#define HAVE_SYSTEM 1
81#if defined(__OS2__)
82#define HAVE_EXECV 1
83#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000084#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000085#include <process.h>
86#else
87#ifdef __BORLANDC__ /* Borland compiler */
88#define HAVE_EXECV 1
89#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000090#define HAVE_OPENDIR 1
91#define HAVE_PIPE 1
92#define HAVE_POPEN 1
93#define HAVE_SYSTEM 1
94#define HAVE_WAIT 1
95#else
96#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000097#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +000098#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099#define HAVE_EXECV 1
100#define HAVE_PIPE 1
101#define HAVE_POPEN 1
102#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000103#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000104#define HAVE_FSYNC 1
105#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000106#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000107#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
108/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#else /* all other compilers */
110/* Unix functions that the configure script doesn't check for */
111#define HAVE_EXECV 1
112#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000113#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
114#define HAVE_FORK1 1
115#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_GETCWD 1
117#define HAVE_GETEGID 1
118#define HAVE_GETEUID 1
119#define HAVE_GETGID 1
120#define HAVE_GETPPID 1
121#define HAVE_GETUID 1
122#define HAVE_KILL 1
123#define HAVE_OPENDIR 1
124#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000125#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000127#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128#define HAVE_SYSTEM 1
129#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000130#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000131#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#endif /* _MSC_VER */
133#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000134#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000135#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000138
Guido van Rossumb00adfb2000-09-25 13:22:00 +0000139#if defined(sun) && !defined(__SVR4)
140/* SunOS 4.1.4 doesn't have prototypes for these: */
141extern int rename(const char *, const char *);
142extern int pclose(FILE *);
143extern int fclose(FILE *);
Thomas Wouters0f954a42001-02-15 08:46:56 +0000144extern int fsync(int);
145extern int lstat(const char *, struct stat *);
146extern int symlink(const char *, const char *);
Guido van Rossumb00adfb2000-09-25 13:22:00 +0000147#endif
148
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000149#if defined(__sgi)&&_COMPILER_VERSION>=700
150/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
151 (default) */
152extern char *ctermid_r(char *);
153#endif
154
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000155#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000157extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000159#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000160extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000162extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000163#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000164#endif
165#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000166extern int chdir(char *);
167extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int chdir(const char *);
170extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000172#ifdef __BORLANDC__
173extern int chmod(const char *, int);
174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000176#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int chown(const char *, uid_t, gid_t);
178extern char *getcwd(char *, int);
179extern char *strerror(int);
180extern int link(const char *, const char *);
181extern int rename(const char *, const char *);
182extern int stat(const char *, struct stat *);
183extern int unlink(const char *);
184extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000187#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000190#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000192
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#ifdef HAVE_UTIME_H
196#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000197#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000199#ifdef HAVE_SYS_UTIME_H
200#include <sys/utime.h>
201#define HAVE_UTIME_H /* pretend we do for the rest of this file */
202#endif /* HAVE_SYS_UTIME_H */
203
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_SYS_TIMES_H
205#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
208#ifdef HAVE_SYS_PARAM_H
209#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
212#ifdef HAVE_SYS_UTSNAME_H
213#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000216#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000218#define NAMLEN(dirent) strlen((dirent)->d_name)
219#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000220#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#include <direct.h>
222#define NAMLEN(dirent) strlen((dirent)->d_name)
223#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000225#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#endif
230#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232#endif
233#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#endif
236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <direct.h>
240#include <io.h>
241#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000242#include "osdefs.h"
Tim Petersee66d0c2002-07-15 16:10:55 +0000243#define WIN32_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000245#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000246#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000247#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249
Guido van Rossumd48f2521997-12-05 22:19:34 +0000250#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000252#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253
Tim Petersbc2e10e2002-03-03 23:17:02 +0000254#ifndef MAXPATHLEN
255#define MAXPATHLEN 1024
256#endif /* MAXPATHLEN */
257
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000258#ifdef UNION_WAIT
259/* Emulate some macros on systems that have a union instead of macros */
260
261#ifndef WIFEXITED
262#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
263#endif
264
265#ifndef WEXITSTATUS
266#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
267#endif
268
269#ifndef WTERMSIG
270#define WTERMSIG(u_wait) ((u_wait).w_termsig)
271#endif
272
273#endif /* UNION_WAIT */
274
Greg Wardb48bc172000-03-01 21:51:56 +0000275/* Don't use the "_r" form if we don't need it (also, won't have a
276 prototype for it, at least on Solaris -- maybe others as well?). */
277#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
278#define USE_CTERMID_R
279#endif
280
281#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
282#define USE_TMPNAM_R
283#endif
284
Fred Drake699f3522000-06-29 21:12:41 +0000285/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000286#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000287#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +0000288# define STAT _stati64
289# define FSTAT _fstati64
290# define STRUCT_STAT struct _stati64
291#else
292# define STAT stat
293# define FSTAT fstat
294# define STRUCT_STAT struct stat
295#endif
296
Tim Peters11b23062003-04-23 02:39:17 +0000297#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000298#include <sys/mkdev.h>
299#else
300#if defined(MAJOR_IN_SYSMACROS)
301#include <sys/sysmacros.h>
302#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000303#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
304#include <sys/mkdev.h>
305#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000306#endif
Fred Drake699f3522000-06-29 21:12:41 +0000307
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000309#ifdef WITH_NEXT_FRAMEWORK
310/* On Darwin/MacOSX a shared library or framework has no access to
311** environ directly, we must obtain it with _NSGetEnviron().
312*/
313#include <crt_externs.h>
314static char **environ;
315#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000317#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318
Barry Warsaw53699e91996-12-10 23:23:01 +0000319static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000320convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321{
Barry Warsaw53699e91996-12-10 23:23:01 +0000322 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000324 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 if (d == NULL)
326 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000327#ifdef WITH_NEXT_FRAMEWORK
328 if (environ == NULL)
329 environ = *_NSGetEnviron();
330#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 if (environ == NULL)
332 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000333 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000335 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000336 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337 char *p = strchr(*e, '=');
338 if (p == NULL)
339 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000340 k = PyString_FromStringAndSize(*e, (int)(p-*e));
341 if (k == NULL) {
342 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000344 }
345 v = PyString_FromString(p+1);
346 if (v == NULL) {
347 PyErr_Clear();
348 Py_DECREF(k);
349 continue;
350 }
351 if (PyDict_GetItem(d, k) == NULL) {
352 if (PyDict_SetItem(d, k, v) != 0)
353 PyErr_Clear();
354 }
355 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000356 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000358#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000359 {
360 APIRET rc;
361 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
362
363 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000364 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000365 PyObject *v = PyString_FromString(buffer);
366 PyDict_SetItemString(d, "BEGINLIBPATH", v);
367 Py_DECREF(v);
368 }
369 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
370 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
371 PyObject *v = PyString_FromString(buffer);
372 PyDict_SetItemString(d, "ENDLIBPATH", v);
373 Py_DECREF(v);
374 }
375 }
376#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 return d;
378}
379
380
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381/* Set a POSIX-specific error from errno, and return NULL */
382
Barry Warsawd58d7641998-07-23 16:14:40 +0000383static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000384posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000385{
Barry Warsawca74da41999-02-09 19:31:45 +0000386 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387}
Barry Warsawd58d7641998-07-23 16:14:40 +0000388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000389posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000390{
Barry Warsawca74da41999-02-09 19:31:45 +0000391 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000392}
393
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000394#ifdef Py_WIN_WIDE_FILENAMES
395static PyObject *
396posix_error_with_unicode_filename(Py_UNICODE* name)
397{
398 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
399}
400#endif /* Py_WIN_WIDE_FILENAMES */
401
402
Mark Hammondef8b6542001-05-13 08:04:26 +0000403static PyObject *
404posix_error_with_allocated_filename(char* name)
405{
406 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
407 PyMem_Free(name);
408 return rc;
409}
410
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000411#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000412static PyObject *
413win32_error(char* function, char* filename)
414{
Mark Hammond33a6da92000-08-15 00:46:38 +0000415 /* XXX We should pass the function name along in the future.
416 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000417 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000418 Windows error object, which is non-trivial.
419 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000420 errno = GetLastError();
421 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000422 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000423 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000424 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000425}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000426
427#ifdef Py_WIN_WIDE_FILENAMES
428static PyObject *
429win32_error_unicode(char* function, Py_UNICODE* filename)
430{
431 /* XXX - see win32_error for comments on 'function' */
432 errno = GetLastError();
433 if (filename)
434 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
435 else
436 return PyErr_SetFromWindowsErr(errno);
437}
438
439static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
440{
441 /* XXX Perhaps we should make this API an alias of
442 PyObject_Unicode() instead ?! */
443 if (PyUnicode_CheckExact(obj)) {
444 Py_INCREF(obj);
445 return obj;
446 }
447 if (PyUnicode_Check(obj)) {
448 /* For a Unicode subtype that's not a Unicode object,
449 return a true Unicode object with the same data. */
450 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
451 PyUnicode_GET_SIZE(obj));
452 }
Tim Peters11b23062003-04-23 02:39:17 +0000453 return PyUnicode_FromEncodedObject(obj,
454 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000455 "strict");
456}
457
458#endif /* Py_WIN_WIDE_FILENAMES */
459
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000460#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461
Guido van Rossumd48f2521997-12-05 22:19:34 +0000462#if defined(PYOS_OS2)
463/**********************************************************************
464 * Helper Function to Trim and Format OS/2 Messages
465 **********************************************************************/
466 static void
467os2_formatmsg(char *msgbuf, int msglen, char *reason)
468{
469 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
470
471 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
472 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
473
474 while (lastc > msgbuf && isspace(*lastc))
475 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
476 }
477
478 /* Add Optional Reason Text */
479 if (reason) {
480 strcat(msgbuf, " : ");
481 strcat(msgbuf, reason);
482 }
483}
484
485/**********************************************************************
486 * Decode an OS/2 Operating System Error Code
487 *
488 * A convenience function to lookup an OS/2 error code and return a
489 * text message we can use to raise a Python exception.
490 *
491 * Notes:
492 * The messages for errors returned from the OS/2 kernel reside in
493 * the file OSO001.MSG in the \OS2 directory hierarchy.
494 *
495 **********************************************************************/
496 static char *
497os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
498{
499 APIRET rc;
500 ULONG msglen;
501
502 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
503 Py_BEGIN_ALLOW_THREADS
504 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
505 errorcode, "oso001.msg", &msglen);
506 Py_END_ALLOW_THREADS
507
508 if (rc == NO_ERROR)
509 os2_formatmsg(msgbuf, msglen, reason);
510 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000511 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000512 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000513
514 return msgbuf;
515}
516
517/* Set an OS/2-specific error and return NULL. OS/2 kernel
518 errors are not in a global variable e.g. 'errno' nor are
519 they congruent with posix error numbers. */
520
521static PyObject * os2_error(int code)
522{
523 char text[1024];
524 PyObject *v;
525
526 os2_strerror(text, sizeof(text), code, "");
527
528 v = Py_BuildValue("(is)", code, text);
529 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000530 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000531 Py_DECREF(v);
532 }
533 return NULL; /* Signal to Python that an Exception is Pending */
534}
535
536#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537
538/* POSIX generic methods */
539
Barry Warsaw53699e91996-12-10 23:23:01 +0000540static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000541posix_fildes(PyObject *fdobj, int (*func)(int))
542{
543 int fd;
544 int res;
545 fd = PyObject_AsFileDescriptor(fdobj);
546 if (fd < 0)
547 return NULL;
548 Py_BEGIN_ALLOW_THREADS
549 res = (*func)(fd);
550 Py_END_ALLOW_THREADS
551 if (res < 0)
552 return posix_error();
553 Py_INCREF(Py_None);
554 return Py_None;
555}
Guido van Rossum21142a01999-01-08 21:05:37 +0000556
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000558static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000559unicode_file_names(void)
560{
561 static int canusewide = -1;
562 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000563 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000564 the Windows NT family. */
565 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
566 }
567 return canusewide;
568}
569#endif
Tim Peters11b23062003-04-23 02:39:17 +0000570
Guido van Rossum21142a01999-01-08 21:05:37 +0000571static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000572posix_1str(PyObject *args, char *format, int (*func)(const char*),
573 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574{
Mark Hammondef8b6542001-05-13 08:04:26 +0000575 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000576 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000577#ifdef Py_WIN_WIDE_FILENAMES
578 if (unicode_file_names()) {
579 PyUnicodeObject *po;
580 if (PyArg_ParseTuple(args, wformat, &po)) {
581 Py_BEGIN_ALLOW_THREADS
582 /* PyUnicode_AS_UNICODE OK without thread
583 lock as it is a simple dereference. */
584 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
585 Py_END_ALLOW_THREADS
586 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000587 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000588 Py_INCREF(Py_None);
589 return Py_None;
590 }
591 /* Drop the argument parsing error as narrow
592 strings are also valid. */
593 PyErr_Clear();
594 }
595#else
596 /* Platforms that don't support Unicode filenames
597 shouldn't be passing these extra params */
598 assert(wformat==NULL && wfunc == NULL);
599#endif
600
Tim Peters5aa91602002-01-30 05:46:57 +0000601 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000602 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000603 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000604 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000605 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000606 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000608 return posix_error_with_allocated_filename(path1);
609 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000610 Py_INCREF(Py_None);
611 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612}
613
Barry Warsaw53699e91996-12-10 23:23:01 +0000614static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000615posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000616 char *format,
617 int (*func)(const char *, const char *),
618 char *wformat,
619 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620{
Mark Hammondef8b6542001-05-13 08:04:26 +0000621 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000622 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623#ifdef Py_WIN_WIDE_FILENAMES
624 if (unicode_file_names()) {
625 PyObject *po1;
626 PyObject *po2;
627 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
628 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
629 PyObject *wpath1;
630 PyObject *wpath2;
631 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
632 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
633 if (!wpath1 || !wpath2) {
634 Py_XDECREF(wpath1);
635 Py_XDECREF(wpath2);
636 return NULL;
637 }
638 Py_BEGIN_ALLOW_THREADS
639 /* PyUnicode_AS_UNICODE OK without thread
640 lock as it is a simple dereference. */
641 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
642 PyUnicode_AS_UNICODE(wpath2));
643 Py_END_ALLOW_THREADS
644 Py_XDECREF(wpath1);
645 Py_XDECREF(wpath2);
646 if (res != 0)
647 return posix_error();
648 Py_INCREF(Py_None);
649 return Py_None;
650 }
651 /* Else flow through as neither is Unicode. */
652 }
653 /* Drop the argument parsing error as narrow
654 strings are also valid. */
655 PyErr_Clear();
656 }
657#else
658 /* Platforms that don't support Unicode filenames
659 shouldn't be passing these extra params */
660 assert(wformat==NULL && wfunc == NULL);
661#endif
662
Mark Hammondef8b6542001-05-13 08:04:26 +0000663 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000664 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000665 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000667 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000668 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000670 PyMem_Free(path1);
671 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000672 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000673 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000675 Py_INCREF(Py_None);
676 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677}
678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000679PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000680"stat_result: Result from stat or lstat.\n\n\
681This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000682 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000683or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
684\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000685Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000686they are available as attributes only.\n\
687\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000688See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000689
690static PyStructSequence_Field stat_result_fields[] = {
691 {"st_mode", "protection bits"},
692 {"st_ino", "inode"},
693 {"st_dev", "device"},
694 {"st_nlink", "number of hard links"},
695 {"st_uid", "user ID of owner"},
696 {"st_gid", "group ID of owner"},
697 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000698 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
699 {NULL, "integer time of last access"},
700 {NULL, "integer time of last modification"},
701 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000702 {"st_atime", "time of last access"},
703 {"st_mtime", "time of last modification"},
704 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000705#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000706 {"st_blksize", "blocksize for filesystem I/O"},
707#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000708#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000709 {"st_blocks", "number of blocks allocated"},
710#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000711#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712 {"st_rdev", "device type (if inode device)"},
713#endif
714 {0}
715};
716
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000717#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000718#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000719#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000720#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000721#endif
722
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000723#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000724#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
725#else
726#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
727#endif
728
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000729#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000730#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
731#else
732#define ST_RDEV_IDX ST_BLOCKS_IDX
733#endif
734
735static PyStructSequence_Desc stat_result_desc = {
736 "stat_result", /* name */
737 stat_result__doc__, /* doc */
738 stat_result_fields,
739 10
740};
741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000742PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000743"statvfs_result: Result from statvfs or fstatvfs.\n\n\
744This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000745 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000746or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000747\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000748See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000749
750static PyStructSequence_Field statvfs_result_fields[] = {
751 {"f_bsize", },
752 {"f_frsize", },
753 {"f_blocks", },
754 {"f_bfree", },
755 {"f_bavail", },
756 {"f_files", },
757 {"f_ffree", },
758 {"f_favail", },
759 {"f_flag", },
760 {"f_namemax",},
761 {0}
762};
763
764static PyStructSequence_Desc statvfs_result_desc = {
765 "statvfs_result", /* name */
766 statvfs_result__doc__, /* doc */
767 statvfs_result_fields,
768 10
769};
770
771static PyTypeObject StatResultType;
772static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000773static newfunc structseq_new;
774
775static PyObject *
776statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
777{
778 PyStructSequence *result;
779 int i;
780
781 result = (PyStructSequence*)structseq_new(type, args, kwds);
782 if (!result)
783 return NULL;
784 /* If we have been initialized from a tuple,
785 st_?time might be set to None. Initialize it
786 from the int slots. */
787 for (i = 7; i <= 9; i++) {
788 if (result->ob_item[i+3] == Py_None) {
789 Py_DECREF(Py_None);
790 Py_INCREF(result->ob_item[i]);
791 result->ob_item[i+3] = result->ob_item[i];
792 }
793 }
794 return (PyObject*)result;
795}
796
797
798
799/* If true, st_?time is float. */
800static int _stat_float_times = 0;
801
802PyDoc_STRVAR(stat_float_times__doc__,
803"stat_float_times([newval]) -> oldval\n\n\
804Determine whether os.[lf]stat represents time stamps as float objects.\n\
805If newval is True, future calls to stat() return floats, if it is False,\n\
806future calls return ints. \n\
807If newval is omitted, return the current setting.\n");
808
809static PyObject*
810stat_float_times(PyObject* self, PyObject *args)
811{
812 int newval = -1;
813 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
814 return NULL;
815 if (newval == -1)
816 /* Return old value */
817 return PyBool_FromLong(_stat_float_times);
818 _stat_float_times = newval;
819 Py_INCREF(Py_None);
820 return Py_None;
821}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000822
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000823static void
824fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
825{
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000826 PyObject *fval,*ival;
827#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000828 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000829#else
830 ival = PyInt_FromLong((long)sec);
831#endif
832 if (_stat_float_times) {
833 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
834 } else {
835 fval = ival;
836 Py_INCREF(fval);
837 }
838 PyStructSequence_SET_ITEM(v, index, ival);
839 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000840}
841
Tim Peters5aa91602002-01-30 05:46:57 +0000842/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +0000843 (used by posix_stat() and posix_fstat()) */
844static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000845_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000846{
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000847 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000848 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +0000849 if (v == NULL)
850 return NULL;
851
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000852 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
Fred Drake699f3522000-06-29 21:12:41 +0000853#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000854 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000855 PyLong_FromLongLong((PY_LONG_LONG)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000856#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000857 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000858#endif
859#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +0000860 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000861 PyLong_FromLongLong((PY_LONG_LONG)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000862#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000863 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000864#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000865 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
866 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
867 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
Fred Drake699f3522000-06-29 21:12:41 +0000868#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000869 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000870 PyLong_FromLongLong((PY_LONG_LONG)st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000871#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000872 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000873#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000874
875#ifdef HAVE_STAT_TV_NSEC
876 ansec = st.st_atim.tv_nsec;
877 mnsec = st.st_mtim.tv_nsec;
878 cnsec = st.st_ctim.tv_nsec;
Fred Drake699f3522000-06-29 21:12:41 +0000879#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000880 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000881#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000882 fill_time(v, 7, st.st_atime, ansec);
883 fill_time(v, 8, st.st_mtime, mnsec);
884 fill_time(v, 9, st.st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000885
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000886#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +0000887 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000888 PyInt_FromLong((long)st.st_blksize));
889#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000890#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +0000891 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000892 PyInt_FromLong((long)st.st_blocks));
893#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000894#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000895 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
896 PyInt_FromLong((long)st.st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +0000897#endif
898
899 if (PyErr_Occurred()) {
900 Py_DECREF(v);
901 return NULL;
902 }
903
904 return v;
905}
906
Barry Warsaw53699e91996-12-10 23:23:01 +0000907static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000908posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000909 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000910#ifdef __VMS
911 int (*statfunc)(const char *, STRUCT_STAT *, ...),
912#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000913 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000914#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000915 char *wformat,
916 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917{
Fred Drake699f3522000-06-29 21:12:41 +0000918 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +0000919 char *path = NULL; /* pass this to stat; do not free() it */
920 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000921 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000922
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000923#ifdef MS_WINDOWS
Tim Peters500bd032001-12-19 19:05:01 +0000924 int pathlen;
925 char pathcopy[MAX_PATH];
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000926#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000927
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000928
929#ifdef Py_WIN_WIDE_FILENAMES
930 /* If on wide-character-capable OS see if argument
931 is Unicode and if so use wide API. */
932 if (unicode_file_names()) {
933 PyUnicodeObject *po;
934 if (PyArg_ParseTuple(args, wformat, &po)) {
935 Py_UNICODE wpath[MAX_PATH+1];
936 pathlen = wcslen(PyUnicode_AS_UNICODE(po));
937 /* the library call can blow up if the file name is too long! */
938 if (pathlen > MAX_PATH) {
939 errno = ENAMETOOLONG;
940 return posix_error();
941 }
942 wcscpy(wpath, PyUnicode_AS_UNICODE(po));
943 /* Remove trailing slash or backslash, unless it's the current
944 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
945 */
946 if (pathlen > 0 &&
947 (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) {
948 /* It does end with a slash -- exempt the root drive cases. */
949 /* XXX UNC root drives should also be exempted? */
950 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':'))
951 /* leave it alone */;
952 else {
953 /* nuke the trailing backslash */
954 wpath[pathlen-1] = L'\0';
955 }
956 }
957 Py_BEGIN_ALLOW_THREADS
958 /* PyUnicode_AS_UNICODE result OK without
959 thread lock as it is a simple dereference. */
960 res = wstatfunc(wpath, &st);
961 Py_END_ALLOW_THREADS
962 if (res != 0)
963 return posix_error_with_unicode_filename(wpath);
964 return _pystat_fromstructstat(st);
965 }
966 /* Drop the argument parsing error as narrow strings
967 are also valid. */
968 PyErr_Clear();
969 }
970#endif
971
Tim Peters5aa91602002-01-30 05:46:57 +0000972 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000973 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000974 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +0000975 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000976
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000977#ifdef MS_WINDOWS
Guido van Rossumace88ae2000-04-21 18:54:45 +0000978 pathlen = strlen(path);
979 /* the library call can blow up if the file name is too long! */
980 if (pathlen > MAX_PATH) {
Tim Peters500bd032001-12-19 19:05:01 +0000981 PyMem_Free(pathfree);
Guido van Rossumace88ae2000-04-21 18:54:45 +0000982 errno = ENAMETOOLONG;
983 return posix_error();
984 }
985
Tim Peters500bd032001-12-19 19:05:01 +0000986 /* Remove trailing slash or backslash, unless it's the current
987 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
988 */
989 if (pathlen > 0 &&
990 (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) {
991 /* It does end with a slash -- exempt the root drive cases. */
992 /* XXX UNC root drives should also be exempted? */
993 if (pathlen == 1 || (pathlen == 3 && path[1] == ':'))
994 /* leave it alone */;
995 else {
996 /* nuke the trailing backslash */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000997 strncpy(pathcopy, path, pathlen);
Tim Peters500bd032001-12-19 19:05:01 +0000998 pathcopy[pathlen-1] = '\0';
Guido van Rossumace88ae2000-04-21 18:54:45 +0000999 path = pathcopy;
1000 }
1001 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001002#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001003
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001005 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001006 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001007 if (res != 0)
Tim Peters500bd032001-12-19 19:05:01 +00001008 return posix_error_with_allocated_filename(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001009
Tim Peters500bd032001-12-19 19:05:01 +00001010 PyMem_Free(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001011 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001012}
1013
1014
1015/* POSIX methods */
1016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001018"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001019Use the real uid/gid to test for access to a path. Note that most\n\
1020operations will use the effective uid/gid, therefore this routine can\n\
1021be used in a suid/sgid environment to test if the invoking user has the\n\
1022specified access to the path. The mode argument can be F_OK to test\n\
1023existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001024
1025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001026posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001027{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001028 char *path;
1029 int mode;
1030 int res;
1031
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001032#ifdef Py_WIN_WIDE_FILENAMES
1033 if (unicode_file_names()) {
1034 PyUnicodeObject *po;
1035 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1036 Py_BEGIN_ALLOW_THREADS
1037 /* PyUnicode_AS_UNICODE OK without thread lock as
1038 it is a simple dereference. */
1039 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1040 Py_END_ALLOW_THREADS
1041 return(PyBool_FromLong(res == 0));
1042 }
1043 /* Drop the argument parsing error as narrow strings
1044 are also valid. */
1045 PyErr_Clear();
1046 }
1047#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001048 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001049 return NULL;
1050 Py_BEGIN_ALLOW_THREADS
1051 res = access(path, mode);
1052 Py_END_ALLOW_THREADS
Guido van Rossum674deb22002-09-01 15:06:28 +00001053 return(PyBool_FromLong(res == 0));
Guido van Rossum94f6f721999-01-06 18:42:14 +00001054}
1055
Guido van Rossumd371ff11999-01-25 16:12:23 +00001056#ifndef F_OK
1057#define F_OK 0
1058#endif
1059#ifndef R_OK
1060#define R_OK 4
1061#endif
1062#ifndef W_OK
1063#define W_OK 2
1064#endif
1065#ifndef X_OK
1066#define X_OK 1
1067#endif
1068
1069#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001071"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001073
1074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001075posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001076{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001077 int id;
1078 char *ret;
1079
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001080 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001081 return NULL;
1082
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001083#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001084 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001085 if (id == 0) {
1086 ret = ttyname();
1087 }
1088 else {
1089 ret = NULL;
1090 }
1091#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001092 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001093#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001094 if (ret == NULL)
1095 return(posix_error());
1096 return(PyString_FromString(ret));
1097}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001098#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001099
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001100#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001102"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001103Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001104
1105static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001106posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001107{
1108 char *ret;
1109 char buffer[L_ctermid];
1110
Greg Wardb48bc172000-03-01 21:51:56 +00001111#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001112 ret = ctermid_r(buffer);
1113#else
1114 ret = ctermid(buffer);
1115#endif
1116 if (ret == NULL)
1117 return(posix_error());
1118 return(PyString_FromString(buffer));
1119}
1120#endif
1121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001123"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001124Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001125
Barry Warsaw53699e91996-12-10 23:23:01 +00001126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001127posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001129#ifdef MS_WINDOWS
1130 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1131#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1132 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001133#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001134 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001135 NULL, NULL);
1136#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001137 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001138#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001139}
1140
Fred Drake4d1e64b2002-04-15 19:40:07 +00001141#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001143"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001144Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001146
1147static PyObject *
1148posix_fchdir(PyObject *self, PyObject *fdobj)
1149{
1150 return posix_fildes(fdobj, fchdir);
1151}
1152#endif /* HAVE_FCHDIR */
1153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001155PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001156"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001157Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001158
Barry Warsaw53699e91996-12-10 23:23:01 +00001159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001160posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161{
Mark Hammondef8b6542001-05-13 08:04:26 +00001162 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001163 int i;
1164 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001165#ifdef Py_WIN_WIDE_FILENAMES
1166 if (unicode_file_names()) {
1167 PyUnicodeObject *po;
1168 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1169 Py_BEGIN_ALLOW_THREADS
1170 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1171 Py_END_ALLOW_THREADS
1172 if (res < 0)
1173 return posix_error_with_unicode_filename(
1174 PyUnicode_AS_UNICODE(po));
1175 Py_INCREF(Py_None);
1176 return Py_None;
1177 }
1178 /* Drop the argument parsing error as narrow strings
1179 are also valid. */
1180 PyErr_Clear();
1181 }
1182#endif /* Py_WIN_WIDE_FILENAMES */
1183 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001184 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001185 return NULL;
1186 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001187 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001188 Py_END_ALLOW_THREADS
1189 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001190 return posix_error_with_allocated_filename(path);
1191 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001192 Py_INCREF(Py_None);
1193 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001194}
1195
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001196
Martin v. Löwis244edc82001-10-04 22:44:26 +00001197#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001199"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001200Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001201
1202static PyObject *
1203posix_chroot(PyObject *self, PyObject *args)
1204{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001205 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001206}
1207#endif
1208
Guido van Rossum21142a01999-01-08 21:05:37 +00001209#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001210PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001211"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001212force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001213
1214static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001215posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001216{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001217 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001218}
1219#endif /* HAVE_FSYNC */
1220
1221#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001222
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001223#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001224extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1225#endif
1226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001227PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001228"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001229force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001230 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001231
1232static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001233posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001234{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001235 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001236}
1237#endif /* HAVE_FDATASYNC */
1238
1239
Fredrik Lundh10723342000-07-10 16:38:09 +00001240#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001241PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001242"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001243Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001244
Barry Warsaw53699e91996-12-10 23:23:01 +00001245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001246posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001247{
Mark Hammondef8b6542001-05-13 08:04:26 +00001248 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001249 int uid, gid;
1250 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001251 if (!PyArg_ParseTuple(args, "etii:chown",
1252 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001253 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001254 return NULL;
1255 Py_BEGIN_ALLOW_THREADS
1256 res = chown(path, (uid_t) uid, (gid_t) gid);
1257 Py_END_ALLOW_THREADS
1258 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001259 return posix_error_with_allocated_filename(path);
1260 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001261 Py_INCREF(Py_None);
1262 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001263}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001264#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001265
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001266#ifdef HAVE_LCHOWN
1267PyDoc_STRVAR(posix_lchown__doc__,
1268"lchown(path, uid, gid)\n\n\
1269Change the owner and group id of path to the numeric uid and gid.\n\
1270This function will not follow symbolic links.");
1271
1272static PyObject *
1273posix_lchown(PyObject *self, PyObject *args)
1274{
1275 char *path = NULL;
1276 int uid, gid;
1277 int res;
1278 if (!PyArg_ParseTuple(args, "etii:lchown",
1279 Py_FileSystemDefaultEncoding, &path,
1280 &uid, &gid))
1281 return NULL;
1282 Py_BEGIN_ALLOW_THREADS
1283 res = lchown(path, (uid_t) uid, (gid_t) gid);
1284 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001285 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001286 return posix_error_with_allocated_filename(path);
1287 PyMem_Free(path);
1288 Py_INCREF(Py_None);
1289 return Py_None;
1290}
1291#endif /* HAVE_LCHOWN */
1292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001293
Guido van Rossum36bc6801995-06-14 22:54:23 +00001294#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001295PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001296"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001297Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001298
Barry Warsaw53699e91996-12-10 23:23:01 +00001299static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001300posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001301{
1302 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001303 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001304
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001306#if defined(PYOS_OS2) && defined(PYCC_GCC)
1307 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001308#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001309 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001310#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001311 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001312 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001314 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001315}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001316
Walter Dörwald3b918c32002-11-21 20:18:46 +00001317#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001318PyDoc_STRVAR(posix_getcwdu__doc__,
1319"getcwdu() -> path\n\n\
1320Return a unicode string representing the current working directory.");
1321
1322static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001323posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001324{
1325 char buf[1026];
1326 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001327
1328#ifdef Py_WIN_WIDE_FILENAMES
1329 if (unicode_file_names()) {
1330 wchar_t *wres;
1331 wchar_t wbuf[1026];
1332 Py_BEGIN_ALLOW_THREADS
1333 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1334 Py_END_ALLOW_THREADS
1335 if (wres == NULL)
1336 return posix_error();
1337 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1338 }
1339#endif
1340
1341 Py_BEGIN_ALLOW_THREADS
1342#if defined(PYOS_OS2) && defined(PYCC_GCC)
1343 res = _getcwd2(buf, sizeof buf);
1344#else
1345 res = getcwd(buf, sizeof buf);
1346#endif
1347 Py_END_ALLOW_THREADS
1348 if (res == NULL)
1349 return posix_error();
1350 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1351}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001352#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001353#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001354
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001355
Guido van Rossumb6775db1994-08-01 11:34:53 +00001356#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001358"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001360
Barry Warsaw53699e91996-12-10 23:23:01 +00001361static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001362posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001363{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001364 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001365}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001366#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001367
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001370"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001371Return a list containing the names of the entries in the directory.\n\
1372\n\
1373 path: path of directory to list\n\
1374\n\
1375The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001376entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001377
Barry Warsaw53699e91996-12-10 23:23:01 +00001378static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001379posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001380{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001381 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001382 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001383#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001384
Barry Warsaw53699e91996-12-10 23:23:01 +00001385 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001386 HANDLE hFindFile;
1387 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001388 /* MAX_PATH characters could mean a bigger encoded string */
1389 char namebuf[MAX_PATH*2+5];
1390 char *bufptr = namebuf;
1391 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001392
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001393#ifdef Py_WIN_WIDE_FILENAMES
1394 /* If on wide-character-capable OS see if argument
1395 is Unicode and if so use wide API. */
1396 if (unicode_file_names()) {
1397 PyUnicodeObject *po;
1398 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1399 WIN32_FIND_DATAW wFileData;
1400 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1401 Py_UNICODE wch;
1402 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1403 wnamebuf[MAX_PATH] = L'\0';
1404 len = wcslen(wnamebuf);
1405 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1406 if (wch != L'/' && wch != L'\\' && wch != L':')
1407 wnamebuf[len++] = L'/';
1408 wcscpy(wnamebuf + len, L"*.*");
1409 if ((d = PyList_New(0)) == NULL)
1410 return NULL;
1411 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1412 if (hFindFile == INVALID_HANDLE_VALUE) {
1413 errno = GetLastError();
1414 if (errno == ERROR_FILE_NOT_FOUND) {
1415 return d;
1416 }
1417 Py_DECREF(d);
1418 return win32_error_unicode("FindFirstFileW", wnamebuf);
1419 }
1420 do {
1421 if (wFileData.cFileName[0] == L'.' &&
1422 (wFileData.cFileName[1] == L'\0' ||
1423 wFileData.cFileName[1] == L'.' &&
1424 wFileData.cFileName[2] == L'\0'))
1425 continue;
1426 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1427 if (v == NULL) {
1428 Py_DECREF(d);
1429 d = NULL;
1430 break;
1431 }
1432 if (PyList_Append(d, v) != 0) {
1433 Py_DECREF(v);
1434 Py_DECREF(d);
1435 d = NULL;
1436 break;
1437 }
1438 Py_DECREF(v);
1439 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1440
1441 if (FindClose(hFindFile) == FALSE) {
1442 Py_DECREF(d);
1443 return win32_error_unicode("FindClose", wnamebuf);
1444 }
1445 return d;
1446 }
1447 /* Drop the argument parsing error as narrow strings
1448 are also valid. */
1449 PyErr_Clear();
1450 }
1451#endif
1452
Tim Peters5aa91602002-01-30 05:46:57 +00001453 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001454 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001455 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001456 if (len > 0) {
1457 char ch = namebuf[len-1];
1458 if (ch != SEP && ch != ALTSEP && ch != ':')
1459 namebuf[len++] = '/';
1460 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001461 strcpy(namebuf + len, "*.*");
1462
Barry Warsaw53699e91996-12-10 23:23:01 +00001463 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001464 return NULL;
1465
1466 hFindFile = FindFirstFile(namebuf, &FileData);
1467 if (hFindFile == INVALID_HANDLE_VALUE) {
1468 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001469 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001470 return d;
1471 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001472 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001473 }
1474 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001475 if (FileData.cFileName[0] == '.' &&
1476 (FileData.cFileName[1] == '\0' ||
1477 FileData.cFileName[1] == '.' &&
1478 FileData.cFileName[2] == '\0'))
1479 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001480 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001481 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001482 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001483 d = NULL;
1484 break;
1485 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001486 if (PyList_Append(d, v) != 0) {
1487 Py_DECREF(v);
1488 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001489 d = NULL;
1490 break;
1491 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001492 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001493 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1494
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001495 if (FindClose(hFindFile) == FALSE) {
1496 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001497 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001498 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001499
1500 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001501
Tim Peters0bb44a42000-09-15 07:44:49 +00001502#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001503
1504#ifndef MAX_PATH
1505#define MAX_PATH CCHMAXPATH
1506#endif
1507 char *name, *pt;
1508 int len;
1509 PyObject *d, *v;
1510 char namebuf[MAX_PATH+5];
1511 HDIR hdir = 1;
1512 ULONG srchcnt = 1;
1513 FILEFINDBUF3 ep;
1514 APIRET rc;
1515
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001516 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001517 return NULL;
1518 if (len >= MAX_PATH) {
1519 PyErr_SetString(PyExc_ValueError, "path too long");
1520 return NULL;
1521 }
1522 strcpy(namebuf, name);
1523 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001524 if (*pt == ALTSEP)
1525 *pt = SEP;
1526 if (namebuf[len-1] != SEP)
1527 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001528 strcpy(namebuf + len, "*.*");
1529
1530 if ((d = PyList_New(0)) == NULL)
1531 return NULL;
1532
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001533 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1534 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001535 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001536 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1537 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1538 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001539
1540 if (rc != NO_ERROR) {
1541 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001542 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001543 }
1544
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001545 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001546 do {
1547 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001548 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001549 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001550
1551 strcpy(namebuf, ep.achName);
1552
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001553 /* Leave Case of Name Alone -- In Native Form */
1554 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001555
1556 v = PyString_FromString(namebuf);
1557 if (v == NULL) {
1558 Py_DECREF(d);
1559 d = NULL;
1560 break;
1561 }
1562 if (PyList_Append(d, v) != 0) {
1563 Py_DECREF(v);
1564 Py_DECREF(d);
1565 d = NULL;
1566 break;
1567 }
1568 Py_DECREF(v);
1569 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1570 }
1571
1572 return d;
1573#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001574
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001575 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001576 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001577 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001578 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001579 int arg_is_unicode = 1;
1580
1581 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1582 arg_is_unicode = 0;
1583 PyErr_Clear();
1584 }
1585 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001586 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001587 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001588 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001589 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001590 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001591 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001592 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593 return NULL;
1594 }
1595 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001596 if (ep->d_name[0] == '.' &&
1597 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001598 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001599 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001600 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001601 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001602 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001603 d = NULL;
1604 break;
1605 }
Just van Rossum46c97842003-02-25 21:42:15 +00001606#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001607 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001608 PyObject *w;
1609
1610 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001611 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001612 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001613 if (w != NULL) {
1614 Py_DECREF(v);
1615 v = w;
1616 }
1617 else {
1618 /* fall back to the original byte string, as
1619 discussed in patch #683592 */
1620 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001621 }
Just van Rossum46c97842003-02-25 21:42:15 +00001622 }
1623#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001624 if (PyList_Append(d, v) != 0) {
1625 Py_DECREF(v);
1626 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627 d = NULL;
1628 break;
1629 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001630 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001631 }
1632 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001633 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001634
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001635 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001636
Tim Peters0bb44a42000-09-15 07:44:49 +00001637#endif /* which OS */
1638} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001639
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001640#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001641/* A helper function for abspath on win32 */
1642static PyObject *
1643posix__getfullpathname(PyObject *self, PyObject *args)
1644{
1645 /* assume encoded strings wont more than double no of chars */
1646 char inbuf[MAX_PATH*2];
1647 char *inbufp = inbuf;
1648 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1649 char outbuf[MAX_PATH*2];
1650 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001651#ifdef Py_WIN_WIDE_FILENAMES
1652 if (unicode_file_names()) {
1653 PyUnicodeObject *po;
1654 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1655 Py_UNICODE woutbuf[MAX_PATH*2];
1656 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001657 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001658 sizeof(woutbuf)/sizeof(woutbuf[0]),
1659 woutbuf, &wtemp))
1660 return win32_error("GetFullPathName", "");
1661 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1662 }
1663 /* Drop the argument parsing error as narrow strings
1664 are also valid. */
1665 PyErr_Clear();
1666 }
1667#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001668 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1669 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001670 &insize))
1671 return NULL;
1672 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1673 outbuf, &temp))
1674 return win32_error("GetFullPathName", inbuf);
1675 return PyString_FromString(outbuf);
1676} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001677#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001679PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001680"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682
Barry Warsaw53699e91996-12-10 23:23:01 +00001683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001684posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001685{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001686 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001687 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001688 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001689
1690#ifdef Py_WIN_WIDE_FILENAMES
1691 if (unicode_file_names()) {
1692 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001693 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001694 Py_BEGIN_ALLOW_THREADS
1695 /* PyUnicode_AS_UNICODE OK without thread lock as
1696 it is a simple dereference. */
1697 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1698 Py_END_ALLOW_THREADS
1699 if (res < 0)
1700 return posix_error();
1701 Py_INCREF(Py_None);
1702 return Py_None;
1703 }
1704 /* Drop the argument parsing error as narrow strings
1705 are also valid. */
1706 PyErr_Clear();
1707 }
1708#endif
1709
Tim Peters5aa91602002-01-30 05:46:57 +00001710 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001711 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001712 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001713 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001714#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001715 res = mkdir(path);
1716#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001717 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001718#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001719 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001720 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001721 return posix_error_with_allocated_filename(path);
1722 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001723 Py_INCREF(Py_None);
1724 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001725}
1726
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001727
Guido van Rossumb6775db1994-08-01 11:34:53 +00001728#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001729#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1730#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1731#include <sys/resource.h>
1732#endif
1733#endif
1734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001735PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001736"nice(inc) -> new_priority\n\n\
1737Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001738
Barry Warsaw53699e91996-12-10 23:23:01 +00001739static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001740posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001741{
1742 int increment, value;
1743
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001744 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001745 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001746
1747 /* There are two flavours of 'nice': one that returns the new
1748 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001749 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1750 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001751
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001752 If we are of the nice family that returns the new priority, we
1753 need to clear errno before the call, and check if errno is filled
1754 before calling posix_error() on a returnvalue of -1, because the
1755 -1 may be the actual new priority! */
1756
1757 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001758 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001759#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001760 if (value == 0)
1761 value = getpriority(PRIO_PROCESS, 0);
1762#endif
1763 if (value == -1 && errno != 0)
1764 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001765 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001766 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001767}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001768#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001769
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001771PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001772"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001773Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001774
Barry Warsaw53699e91996-12-10 23:23:01 +00001775static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001776posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001777{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001778#ifdef MS_WINDOWS
1779 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1780#else
1781 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1782#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001783}
1784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001785
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001786PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001787"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001789
Barry Warsaw53699e91996-12-10 23:23:01 +00001790static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001791posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001792{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001793#ifdef MS_WINDOWS
1794 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1795#else
1796 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1797#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001798}
1799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001802"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001804
Barry Warsaw53699e91996-12-10 23:23:01 +00001805static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001806posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001807{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001808#ifdef MS_WINDOWS
1809 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1810#else
1811 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1812#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001813}
1814
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001815
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001816#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001818"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001820
Barry Warsaw53699e91996-12-10 23:23:01 +00001821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001822posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001823{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001824 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001825 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001826 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001827 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001828 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001829 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001830 Py_END_ALLOW_THREADS
1831 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001832}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001833#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001835
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001836PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001837"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001838Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001839
Barry Warsaw53699e91996-12-10 23:23:01 +00001840static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001841posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001842{
1843 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001844 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001846 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001847 if (i < 0)
1848 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001849 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001850}
1851
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001853PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001854"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001858"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001859Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001860
Barry Warsaw53699e91996-12-10 23:23:01 +00001861static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001862posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001863{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001864#ifdef MS_WINDOWS
1865 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1866#else
1867 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1868#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001869}
1870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001871
Guido van Rossumb6775db1994-08-01 11:34:53 +00001872#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001874"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001878posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001879{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001880 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001881 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001882
Barry Warsaw53699e91996-12-10 23:23:01 +00001883 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001884 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001885 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001886 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001887 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001888 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001889 u.sysname,
1890 u.nodename,
1891 u.release,
1892 u.version,
1893 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001894}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001895#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001896
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001897static int
1898extract_time(PyObject *t, long* sec, long* usec)
1899{
1900 long intval;
1901 if (PyFloat_Check(t)) {
1902 double tval = PyFloat_AsDouble(t);
1903 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1904 if (!intobj)
1905 return -1;
1906 intval = PyInt_AsLong(intobj);
1907 Py_DECREF(intobj);
1908 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001909 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001910 if (*usec < 0)
1911 /* If rounding gave us a negative number,
1912 truncate. */
1913 *usec = 0;
1914 return 0;
1915 }
1916 intval = PyInt_AsLong(t);
1917 if (intval == -1 && PyErr_Occurred())
1918 return -1;
1919 *sec = intval;
1920 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001921 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001922}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925"utime(path, (atime, utime))\n\
1926utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001927Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001928second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001929
Barry Warsaw53699e91996-12-10 23:23:01 +00001930static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001931posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001932{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001933 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001934 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001935 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001936 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001937
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001938#if defined(HAVE_UTIMES)
1939 struct timeval buf[2];
1940#define ATIME buf[0].tv_sec
1941#define MTIME buf[1].tv_sec
1942#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001943/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001944 struct utimbuf buf;
1945#define ATIME buf.actime
1946#define MTIME buf.modtime
1947#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001948#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001949 time_t buf[2];
1950#define ATIME buf[0]
1951#define MTIME buf[1]
1952#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001953#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001954
Mark Hammond817c9292003-12-03 01:22:38 +00001955 int have_unicode_filename = 0;
1956#ifdef Py_WIN_WIDE_FILENAMES
1957 PyUnicodeObject *obwpath;
1958 wchar_t *wpath;
1959 if (unicode_file_names()) {
1960 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
1961 wpath = PyUnicode_AS_UNICODE(obwpath);
1962 have_unicode_filename = 1;
1963 } else
1964 /* Drop the argument parsing error as narrow strings
1965 are also valid. */
1966 PyErr_Clear();
1967 }
1968#endif /* Py_WIN_WIDE_FILENAMES */
1969
1970 if (!have_unicode_filename && \
Hye-Shik Chang2b2c9732004-01-04 13:54:25 +00001971 !PyArg_ParseTuple(args, "etO:utime",
1972 Py_FileSystemDefaultEncoding, &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001973 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001974 if (arg == Py_None) {
1975 /* optional time values not given */
1976 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00001977#ifdef Py_WIN_WIDE_FILENAMES
1978 if (have_unicode_filename)
1979 res = _wutime(wpath, NULL);
1980 else
1981#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00001982 res = utime(path, NULL);
1983 Py_END_ALLOW_THREADS
1984 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001985 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00001986 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001987 "utime() arg 2 must be a tuple (atime, mtime)");
Barry Warsaw3cef8562000-05-01 16:17:24 +00001988 return NULL;
1989 }
1990 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001991 if (extract_time(PyTuple_GET_ITEM(arg, 0),
1992 &atime, &ausec) == -1)
1993 return NULL;
1994 if (extract_time(PyTuple_GET_ITEM(arg, 1),
1995 &mtime, &musec) == -1)
1996 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001997 ATIME = atime;
1998 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001999#ifdef HAVE_UTIMES
2000 buf[0].tv_usec = ausec;
2001 buf[1].tv_usec = musec;
2002 Py_BEGIN_ALLOW_THREADS
2003 res = utimes(path, buf);
2004 Py_END_ALLOW_THREADS
2005#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002006 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002007#ifdef Py_WIN_WIDE_FILENAMES
2008 if (have_unicode_filename)
2009 /* utime is OK with utimbuf, but _wutime insists
2010 on _utimbuf (the msvc headers assert the
2011 underscore version is ansi) */
2012 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2013 else
2014#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002015 res = utime(path, UTIME_ARG);
2016 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002017#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002018 }
Guido van Rossumff4949e1992-08-05 19:58:53 +00002019 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002020 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 Py_INCREF(Py_None);
2022 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002023#undef UTIME_ARG
2024#undef ATIME
2025#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002026}
2027
Guido van Rossum85e3b011991-06-03 12:42:10 +00002028
Guido van Rossum3b066191991-06-04 19:40:25 +00002029/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002032"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002033Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002034
Barry Warsaw53699e91996-12-10 23:23:01 +00002035static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002036posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002037{
2038 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002039 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002040 return NULL;
2041 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002042 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002043}
2044
Martin v. Löwis114619e2002-10-07 06:44:21 +00002045#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2046static void
2047free_string_array(char **array, int count)
2048{
2049 int i;
2050 for (i = 0; i < count; i++)
2051 PyMem_Free(array[i]);
2052 PyMem_DEL(array);
2053}
2054#endif
2055
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002056
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002057#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002059"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060Execute an executable path with arguments, replacing current process.\n\
2061\n\
2062 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002063 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002064
Barry Warsaw53699e91996-12-10 23:23:01 +00002065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002066posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002067{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002068 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002069 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002070 char **argvlist;
2071 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002072 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002073
Guido van Rossum89b33251993-10-22 14:26:06 +00002074 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002075 argv is a list or tuple of strings. */
2076
Martin v. Löwis114619e2002-10-07 06:44:21 +00002077 if (!PyArg_ParseTuple(args, "etO:execv",
2078 Py_FileSystemDefaultEncoding,
2079 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002080 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002081 if (PyList_Check(argv)) {
2082 argc = PyList_Size(argv);
2083 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002084 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002085 else if (PyTuple_Check(argv)) {
2086 argc = PyTuple_Size(argv);
2087 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002088 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002089 else {
Fred Drake661ea262000-10-24 19:57:45 +00002090 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002091 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002092 return NULL;
2093 }
2094
2095 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00002096 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002097 PyMem_Free(path);
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002098 return NULL;
2099 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002100
Barry Warsaw53699e91996-12-10 23:23:01 +00002101 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002102 if (argvlist == NULL) {
2103 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002104 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002105 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002106 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002107 if (!PyArg_Parse((*getitem)(argv, i), "et",
2108 Py_FileSystemDefaultEncoding,
2109 &argvlist[i])) {
2110 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002111 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002112 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002113 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002114 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002115
Guido van Rossum85e3b011991-06-03 12:42:10 +00002116 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002117 }
2118 argvlist[argc] = NULL;
2119
Guido van Rossumb6775db1994-08-01 11:34:53 +00002120#ifdef BAD_EXEC_PROTOTYPES
2121 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002122#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002123 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002124#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002125
Guido van Rossum85e3b011991-06-03 12:42:10 +00002126 /* If we get here it's definitely an error */
2127
Martin v. Löwis114619e2002-10-07 06:44:21 +00002128 free_string_array(argvlist, argc);
2129 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002130 return posix_error();
2131}
2132
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002133
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002134PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002135"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002136Execute a path with arguments and environment, replacing current process.\n\
2137\n\
2138 path: path of executable file\n\
2139 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002140 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002141
Barry Warsaw53699e91996-12-10 23:23:01 +00002142static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002143posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002144{
2145 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002146 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002147 char **argvlist;
2148 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002149 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002150 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002151 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002152 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002153
2154 /* execve has three arguments: (path, argv, env), where
2155 argv is a list or tuple of strings and env is a dictionary
2156 like posix.environ. */
2157
Martin v. Löwis114619e2002-10-07 06:44:21 +00002158 if (!PyArg_ParseTuple(args, "etOO:execve",
2159 Py_FileSystemDefaultEncoding,
2160 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002161 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002162 if (PyList_Check(argv)) {
2163 argc = PyList_Size(argv);
2164 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002165 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002166 else if (PyTuple_Check(argv)) {
2167 argc = PyTuple_Size(argv);
2168 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002169 }
2170 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002171 PyErr_SetString(PyExc_TypeError,
2172 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002173 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002174 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002175 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002176 PyErr_SetString(PyExc_TypeError,
2177 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002178 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002179 }
2180
Guido van Rossum50422b42000-04-26 20:34:28 +00002181 if (argc == 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00002182 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00002183 "execve() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002184 goto fail_0;
Guido van Rossum50422b42000-04-26 20:34:28 +00002185 }
2186
Barry Warsaw53699e91996-12-10 23:23:01 +00002187 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002188 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002189 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002190 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002191 }
2192 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002193 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002194 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002195 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002196 &argvlist[i]))
2197 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002198 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002199 goto fail_1;
2200 }
2201 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002202 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002203 argvlist[argc] = NULL;
2204
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002205 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002206 if (i < 0)
2207 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002208 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002209 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002210 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002211 goto fail_1;
2212 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002213 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002214 keys = PyMapping_Keys(env);
2215 vals = PyMapping_Values(env);
2216 if (!keys || !vals)
2217 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002218 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2219 PyErr_SetString(PyExc_TypeError,
2220 "execve(): env.keys() or env.values() is not a list");
2221 goto fail_2;
2222 }
Tim Peters5aa91602002-01-30 05:46:57 +00002223
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002224 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002225 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002226 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002227
2228 key = PyList_GetItem(keys, pos);
2229 val = PyList_GetItem(vals, pos);
2230 if (!key || !val)
2231 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002232
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002233 if (!PyArg_Parse(
2234 key,
2235 "s;execve() arg 3 contains a non-string key",
2236 &k) ||
2237 !PyArg_Parse(
2238 val,
2239 "s;execve() arg 3 contains a non-string value",
2240 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002241 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002242 goto fail_2;
2243 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002244
2245#if defined(PYOS_OS2)
2246 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2247 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2248#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002249 len = PyString_Size(key) + PyString_Size(val) + 2;
2250 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002251 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002252 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002253 goto fail_2;
2254 }
Tim Petersc8996f52001-12-03 20:41:00 +00002255 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002256 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002257#if defined(PYOS_OS2)
2258 }
2259#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002260 }
2261 envlist[envc] = 0;
2262
Guido van Rossumb6775db1994-08-01 11:34:53 +00002263
2264#ifdef BAD_EXEC_PROTOTYPES
2265 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002266#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002267 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002268#endif /* BAD_EXEC_PROTOTYPES */
Tim Peters5aa91602002-01-30 05:46:57 +00002269
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002270 /* If we get here it's definitely an error */
2271
2272 (void) posix_error();
2273
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002274 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002275 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002276 PyMem_DEL(envlist[envc]);
2277 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002278 fail_1:
2279 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002280 Py_XDECREF(vals);
2281 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002282 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002283 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002284 return NULL;
2285}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002286#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002287
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002288
Guido van Rossuma1065681999-01-25 23:20:23 +00002289#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002290PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002291"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002292Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002293\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002294 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002295 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002296 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002297
2298static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002299posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002300{
2301 char *path;
2302 PyObject *argv;
2303 char **argvlist;
2304 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002305 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002306 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002307
2308 /* spawnv has three arguments: (mode, path, argv), where
2309 argv is a list or tuple of strings. */
2310
Martin v. Löwis114619e2002-10-07 06:44:21 +00002311 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2312 Py_FileSystemDefaultEncoding,
2313 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002314 return NULL;
2315 if (PyList_Check(argv)) {
2316 argc = PyList_Size(argv);
2317 getitem = PyList_GetItem;
2318 }
2319 else if (PyTuple_Check(argv)) {
2320 argc = PyTuple_Size(argv);
2321 getitem = PyTuple_GetItem;
2322 }
2323 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002324 PyErr_SetString(PyExc_TypeError,
2325 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002326 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002327 return NULL;
2328 }
2329
2330 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002331 if (argvlist == NULL) {
2332 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002333 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002334 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002335 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002336 if (!PyArg_Parse((*getitem)(argv, i), "et",
2337 Py_FileSystemDefaultEncoding,
2338 &argvlist[i])) {
2339 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002340 PyErr_SetString(
2341 PyExc_TypeError,
2342 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002343 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002344 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002345 }
2346 }
2347 argvlist[argc] = NULL;
2348
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002349#if defined(PYOS_OS2) && defined(PYCC_GCC)
2350 Py_BEGIN_ALLOW_THREADS
2351 spawnval = spawnv(mode, path, argvlist);
2352 Py_END_ALLOW_THREADS
2353#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002354 if (mode == _OLD_P_OVERLAY)
2355 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002356
Tim Peters25059d32001-12-07 20:35:43 +00002357 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002358 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002359 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002360#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002361
Martin v. Löwis114619e2002-10-07 06:44:21 +00002362 free_string_array(argvlist, argc);
2363 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002364
Fred Drake699f3522000-06-29 21:12:41 +00002365 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002366 return posix_error();
2367 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002368#if SIZEOF_LONG == SIZEOF_VOID_P
2369 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002370#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002371 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002372#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002373}
2374
2375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002376PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002377"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002378Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002379\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002380 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002381 path: path of executable file\n\
2382 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002383 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002384
2385static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002386posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002387{
2388 char *path;
2389 PyObject *argv, *env;
2390 char **argvlist;
2391 char **envlist;
2392 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2393 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002394 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002395 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002396 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002397
2398 /* spawnve has four arguments: (mode, path, argv, env), where
2399 argv is a list or tuple of strings and env is a dictionary
2400 like posix.environ. */
2401
Martin v. Löwis114619e2002-10-07 06:44:21 +00002402 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2403 Py_FileSystemDefaultEncoding,
2404 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002405 return NULL;
2406 if (PyList_Check(argv)) {
2407 argc = PyList_Size(argv);
2408 getitem = PyList_GetItem;
2409 }
2410 else if (PyTuple_Check(argv)) {
2411 argc = PyTuple_Size(argv);
2412 getitem = PyTuple_GetItem;
2413 }
2414 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002415 PyErr_SetString(PyExc_TypeError,
2416 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002417 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002418 }
2419 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002420 PyErr_SetString(PyExc_TypeError,
2421 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002422 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002423 }
2424
2425 argvlist = PyMem_NEW(char *, argc+1);
2426 if (argvlist == NULL) {
2427 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002428 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002429 }
2430 for (i = 0; i < argc; i++) {
2431 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002432 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002433 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002434 &argvlist[i]))
2435 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002436 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002437 goto fail_1;
2438 }
2439 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002440 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002441 argvlist[argc] = NULL;
2442
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002443 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002444 if (i < 0)
2445 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002446 envlist = PyMem_NEW(char *, i + 1);
2447 if (envlist == NULL) {
2448 PyErr_NoMemory();
2449 goto fail_1;
2450 }
2451 envc = 0;
2452 keys = PyMapping_Keys(env);
2453 vals = PyMapping_Values(env);
2454 if (!keys || !vals)
2455 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002456 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2457 PyErr_SetString(PyExc_TypeError,
2458 "spawnve(): env.keys() or env.values() is not a list");
2459 goto fail_2;
2460 }
Tim Peters5aa91602002-01-30 05:46:57 +00002461
Guido van Rossuma1065681999-01-25 23:20:23 +00002462 for (pos = 0; pos < i; pos++) {
2463 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002464 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002465
2466 key = PyList_GetItem(keys, pos);
2467 val = PyList_GetItem(vals, pos);
2468 if (!key || !val)
2469 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002470
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002471 if (!PyArg_Parse(
2472 key,
2473 "s;spawnve() arg 3 contains a non-string key",
2474 &k) ||
2475 !PyArg_Parse(
2476 val,
2477 "s;spawnve() arg 3 contains a non-string value",
2478 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002479 {
2480 goto fail_2;
2481 }
Tim Petersc8996f52001-12-03 20:41:00 +00002482 len = PyString_Size(key) + PyString_Size(val) + 2;
2483 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002484 if (p == NULL) {
2485 PyErr_NoMemory();
2486 goto fail_2;
2487 }
Tim Petersc8996f52001-12-03 20:41:00 +00002488 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002489 envlist[envc++] = p;
2490 }
2491 envlist[envc] = 0;
2492
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002493#if defined(PYOS_OS2) && defined(PYCC_GCC)
2494 Py_BEGIN_ALLOW_THREADS
2495 spawnval = spawnve(mode, path, argvlist, envlist);
2496 Py_END_ALLOW_THREADS
2497#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002498 if (mode == _OLD_P_OVERLAY)
2499 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002500
2501 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002502 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002503 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002504#endif
Tim Peters25059d32001-12-07 20:35:43 +00002505
Fred Drake699f3522000-06-29 21:12:41 +00002506 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002507 (void) posix_error();
2508 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002509#if SIZEOF_LONG == SIZEOF_VOID_P
2510 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002511#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002512 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002513#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002514
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002515 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002516 while (--envc >= 0)
2517 PyMem_DEL(envlist[envc]);
2518 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002519 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002520 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002521 Py_XDECREF(vals);
2522 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002523 fail_0:
2524 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002525 return res;
2526}
2527#endif /* HAVE_SPAWNV */
2528
2529
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002530#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002531PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002532"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002533Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2534\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002536
2537static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002538posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002539{
Neal Norwitze241ce82003-02-17 18:17:05 +00002540 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002541 if (pid == -1)
2542 return posix_error();
2543 PyOS_AfterFork();
2544 return PyInt_FromLong((long)pid);
2545}
2546#endif
2547
2548
Guido van Rossumad0ee831995-03-01 10:34:45 +00002549#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002551"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002552Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002553Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002554
Barry Warsaw53699e91996-12-10 23:23:01 +00002555static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002556posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002557{
Neal Norwitze241ce82003-02-17 18:17:05 +00002558 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002559 if (pid == -1)
2560 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002561 if (pid == 0)
2562 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002563 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002564}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002565#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002566
Neal Norwitzb59798b2003-03-21 01:43:31 +00002567/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002568/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2569#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002570#define DEV_PTY_FILE "/dev/ptc"
2571#define HAVE_DEV_PTMX
2572#else
2573#define DEV_PTY_FILE "/dev/ptmx"
2574#endif
2575
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002576#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002577#ifdef HAVE_PTY_H
2578#include <pty.h>
2579#else
2580#ifdef HAVE_LIBUTIL_H
2581#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002582#endif /* HAVE_LIBUTIL_H */
2583#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002584#ifdef HAVE_STROPTS_H
2585#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002586#endif
2587#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002588
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002589#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002590PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002591"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002592Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002593
2594static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002595posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002596{
2597 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002598#ifndef HAVE_OPENPTY
2599 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002600#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002601#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002602 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002603#ifdef sun
2604 extern char *ptsname();
2605#endif
2606#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002607
Thomas Wouters70c21a12000-07-14 14:28:33 +00002608#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002609 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2610 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002611#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002612 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2613 if (slave_name == NULL)
2614 return posix_error();
2615
2616 slave_fd = open(slave_name, O_RDWR);
2617 if (slave_fd < 0)
2618 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002619#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002620 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002621 if (master_fd < 0)
2622 return posix_error();
2623 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002624 /* change permission of slave */
2625 if (grantpt(master_fd) < 0) {
2626 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002627 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002628 }
2629 /* unlock slave */
2630 if (unlockpt(master_fd) < 0) {
2631 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002632 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002633 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002634 signal(SIGCHLD, sig_saved);
2635 slave_name = ptsname(master_fd); /* get name of slave */
2636 if (slave_name == NULL)
2637 return posix_error();
2638 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2639 if (slave_fd < 0)
2640 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002641#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002642 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2643 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002644#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002645 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002646#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002647#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002648#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002649
Fred Drake8cef4cf2000-06-28 16:40:38 +00002650 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002651
Fred Drake8cef4cf2000-06-28 16:40:38 +00002652}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002653#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002654
2655#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002657"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002658Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2659Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002660To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002661
2662static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002663posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002664{
2665 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002666
Fred Drake8cef4cf2000-06-28 16:40:38 +00002667 pid = forkpty(&master_fd, NULL, NULL, NULL);
2668 if (pid == -1)
2669 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002670 if (pid == 0)
2671 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002672 return Py_BuildValue("(ii)", pid, master_fd);
2673}
2674#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002675
Guido van Rossumad0ee831995-03-01 10:34:45 +00002676#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002677PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002678"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002680
Barry Warsaw53699e91996-12-10 23:23:01 +00002681static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002682posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002683{
Barry Warsaw53699e91996-12-10 23:23:01 +00002684 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002685}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002686#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002687
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002688
Guido van Rossumad0ee831995-03-01 10:34:45 +00002689#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002690PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002691"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002692Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002693
Barry Warsaw53699e91996-12-10 23:23:01 +00002694static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002695posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002696{
Barry Warsaw53699e91996-12-10 23:23:01 +00002697 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002698}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002699#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002700
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Guido van Rossumad0ee831995-03-01 10:34:45 +00002702#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002703PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002704"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002705Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002706
Barry Warsaw53699e91996-12-10 23:23:01 +00002707static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002708posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002709{
Barry Warsaw53699e91996-12-10 23:23:01 +00002710 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002711}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002712#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002713
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002714
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002715PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002716"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002717Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002718
Barry Warsaw53699e91996-12-10 23:23:01 +00002719static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002720posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002721{
Barry Warsaw53699e91996-12-10 23:23:01 +00002722 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002723}
2724
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002725
Fred Drakec9680921999-12-13 16:37:25 +00002726#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002728"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002729Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00002730
2731static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002732posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00002733{
2734 PyObject *result = NULL;
2735
Fred Drakec9680921999-12-13 16:37:25 +00002736#ifdef NGROUPS_MAX
2737#define MAX_GROUPS NGROUPS_MAX
2738#else
2739 /* defined to be 16 on Solaris7, so this should be a small number */
2740#define MAX_GROUPS 64
2741#endif
2742 gid_t grouplist[MAX_GROUPS];
2743 int n;
2744
2745 n = getgroups(MAX_GROUPS, grouplist);
2746 if (n < 0)
2747 posix_error();
2748 else {
2749 result = PyList_New(n);
2750 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00002751 int i;
2752 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00002753 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00002754 if (o == NULL) {
2755 Py_DECREF(result);
2756 result = NULL;
2757 break;
2758 }
2759 PyList_SET_ITEM(result, i, o);
2760 }
2761 }
2762 }
Neal Norwitze241ce82003-02-17 18:17:05 +00002763
Fred Drakec9680921999-12-13 16:37:25 +00002764 return result;
2765}
2766#endif
2767
Martin v. Löwis606edc12002-06-13 21:09:11 +00002768#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002769PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002770"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002771Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00002772
2773static PyObject *
2774posix_getpgid(PyObject *self, PyObject *args)
2775{
2776 int pid, pgid;
2777 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
2778 return NULL;
2779 pgid = getpgid(pid);
2780 if (pgid < 0)
2781 return posix_error();
2782 return PyInt_FromLong((long)pgid);
2783}
2784#endif /* HAVE_GETPGID */
2785
2786
Guido van Rossumb6775db1994-08-01 11:34:53 +00002787#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002788PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002789"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002791
Barry Warsaw53699e91996-12-10 23:23:01 +00002792static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002793posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00002794{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002795#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00002796 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002797#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00002798 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002799#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00002800}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002801#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00002802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002803
Guido van Rossumb6775db1994-08-01 11:34:53 +00002804#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002805PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002806"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002807Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002808
Barry Warsaw53699e91996-12-10 23:23:01 +00002809static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002810posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00002811{
Guido van Rossum64933891994-10-20 21:56:42 +00002812#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00002813 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002814#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002815 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002816#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00002817 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002818 Py_INCREF(Py_None);
2819 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002820}
2821
Guido van Rossumb6775db1994-08-01 11:34:53 +00002822#endif /* HAVE_SETPGRP */
2823
Guido van Rossumad0ee831995-03-01 10:34:45 +00002824#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002825PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002826"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002827Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002828
Barry Warsaw53699e91996-12-10 23:23:01 +00002829static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002830posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002831{
Barry Warsaw53699e91996-12-10 23:23:01 +00002832 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002833}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002834#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002836
Fred Drake12c6e2d1999-12-14 21:25:03 +00002837#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002838PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002839"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002840Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00002841
2842static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002843posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00002844{
Neal Norwitze241ce82003-02-17 18:17:05 +00002845 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00002846 char *name;
2847 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00002848
Fred Drakea30680b2000-12-06 21:24:28 +00002849 errno = 0;
2850 name = getlogin();
2851 if (name == NULL) {
2852 if (errno)
2853 posix_error();
2854 else
2855 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00002856 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00002857 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00002858 else
2859 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00002860 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00002861
Fred Drake12c6e2d1999-12-14 21:25:03 +00002862 return result;
2863}
2864#endif
2865
Guido van Rossumad0ee831995-03-01 10:34:45 +00002866#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002867PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002868"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002869Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002870
Barry Warsaw53699e91996-12-10 23:23:01 +00002871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002872posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002873{
Barry Warsaw53699e91996-12-10 23:23:01 +00002874 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002875}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002876#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002877
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002878
Guido van Rossumad0ee831995-03-01 10:34:45 +00002879#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002880PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002881"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002882Kill a process with a signal.");
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_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002886{
2887 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002888 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002889 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002890#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002891 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
2892 APIRET rc;
2893 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002894 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002895
2896 } else if (sig == XCPT_SIGNAL_KILLPROC) {
2897 APIRET rc;
2898 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002899 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002900
2901 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002902 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002903#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00002904 if (kill(pid, sig) == -1)
2905 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002906#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002907 Py_INCREF(Py_None);
2908 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002909}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002910#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002911
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002912#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002913PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002914"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002916
2917static PyObject *
2918posix_killpg(PyObject *self, PyObject *args)
2919{
2920 int pgid, sig;
2921 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
2922 return NULL;
2923 if (killpg(pgid, sig) == -1)
2924 return posix_error();
2925 Py_INCREF(Py_None);
2926 return Py_None;
2927}
2928#endif
2929
Guido van Rossumc0125471996-06-28 18:55:32 +00002930#ifdef HAVE_PLOCK
2931
2932#ifdef HAVE_SYS_LOCK_H
2933#include <sys/lock.h>
2934#endif
2935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002936PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002937"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002938Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002939
Barry Warsaw53699e91996-12-10 23:23:01 +00002940static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002941posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00002942{
2943 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002944 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00002945 return NULL;
2946 if (plock(op) == -1)
2947 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002948 Py_INCREF(Py_None);
2949 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00002950}
2951#endif
2952
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002953
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002954#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002955PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002956"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002957Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002958
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002959#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002960#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002961static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002962async_system(const char *command)
2963{
2964 char *p, errormsg[256], args[1024];
2965 RESULTCODES rcodes;
2966 APIRET rc;
2967 char *shell = getenv("COMSPEC");
2968 if (!shell)
2969 shell = "cmd";
2970
2971 strcpy(args, shell);
2972 p = &args[ strlen(args)+1 ];
2973 strcpy(p, "/c ");
2974 strcat(p, command);
2975 p += strlen(p) + 1;
2976 *p = '\0';
2977
2978 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002979 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002980 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002981 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002982 &rcodes, shell);
2983 return rc;
2984}
2985
Guido van Rossumd48f2521997-12-05 22:19:34 +00002986static FILE *
2987popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002988{
2989 HFILE rhan, whan;
2990 FILE *retfd = NULL;
2991 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2992
Guido van Rossumd48f2521997-12-05 22:19:34 +00002993 if (rc != NO_ERROR) {
2994 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002995 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002996 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002997
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002998 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2999 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003000
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003001 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3002 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003003
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003004 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
3005 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003006
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003007 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003008 }
3009
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003010 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
3011 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003012
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003013 if (rc == NO_ERROR)
3014 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
3015
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003016 close(oldfd); /* And Close Saved STDOUT Handle */
3017 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003018
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003019 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
3020 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003021
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003022 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3023 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003024
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003025 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
3026 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003027
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003028 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003029 }
3030
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003031 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
3032 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003033
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003034 if (rc == NO_ERROR)
3035 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
3036
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003037 close(oldfd); /* And Close Saved STDIN Handle */
3038 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003039
Guido van Rossumd48f2521997-12-05 22:19:34 +00003040 } else {
3041 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003042 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003043 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003044}
3045
3046static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003047posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003048{
3049 char *name;
3050 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003051 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003052 FILE *fp;
3053 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003054 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003055 return NULL;
3056 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003057 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003058 Py_END_ALLOW_THREADS
3059 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003060 return os2_error(err);
3061
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003062 f = PyFile_FromFile(fp, name, mode, fclose);
3063 if (f != NULL)
3064 PyFile_SetBufSize(f, bufsize);
3065 return f;
3066}
3067
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003068#elif defined(PYCC_GCC)
3069
3070/* standard posix version of popen() support */
3071static PyObject *
3072posix_popen(PyObject *self, PyObject *args)
3073{
3074 char *name;
3075 char *mode = "r";
3076 int bufsize = -1;
3077 FILE *fp;
3078 PyObject *f;
3079 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3080 return NULL;
3081 Py_BEGIN_ALLOW_THREADS
3082 fp = popen(name, mode);
3083 Py_END_ALLOW_THREADS
3084 if (fp == NULL)
3085 return posix_error();
3086 f = PyFile_FromFile(fp, name, mode, pclose);
3087 if (f != NULL)
3088 PyFile_SetBufSize(f, bufsize);
3089 return f;
3090}
3091
3092/* fork() under OS/2 has lots'o'warts
3093 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3094 * most of this code is a ripoff of the win32 code, but using the
3095 * capabilities of EMX's C library routines
3096 */
3097
3098/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3099#define POPEN_1 1
3100#define POPEN_2 2
3101#define POPEN_3 3
3102#define POPEN_4 4
3103
3104static PyObject *_PyPopen(char *, int, int, int);
3105static int _PyPclose(FILE *file);
3106
3107/*
3108 * Internal dictionary mapping popen* file pointers to process handles,
3109 * for use when retrieving the process exit code. See _PyPclose() below
3110 * for more information on this dictionary's use.
3111 */
3112static PyObject *_PyPopenProcs = NULL;
3113
3114/* os2emx version of popen2()
3115 *
3116 * The result of this function is a pipe (file) connected to the
3117 * process's stdin, and a pipe connected to the process's stdout.
3118 */
3119
3120static PyObject *
3121os2emx_popen2(PyObject *self, PyObject *args)
3122{
3123 PyObject *f;
3124 int tm=0;
3125
3126 char *cmdstring;
3127 char *mode = "t";
3128 int bufsize = -1;
3129 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3130 return NULL;
3131
3132 if (*mode == 't')
3133 tm = O_TEXT;
3134 else if (*mode != 'b') {
3135 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3136 return NULL;
3137 } else
3138 tm = O_BINARY;
3139
3140 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3141
3142 return f;
3143}
3144
3145/*
3146 * Variation on os2emx.popen2
3147 *
3148 * The result of this function is 3 pipes - the process's stdin,
3149 * stdout and stderr
3150 */
3151
3152static PyObject *
3153os2emx_popen3(PyObject *self, PyObject *args)
3154{
3155 PyObject *f;
3156 int tm = 0;
3157
3158 char *cmdstring;
3159 char *mode = "t";
3160 int bufsize = -1;
3161 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3162 return NULL;
3163
3164 if (*mode == 't')
3165 tm = O_TEXT;
3166 else if (*mode != 'b') {
3167 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3168 return NULL;
3169 } else
3170 tm = O_BINARY;
3171
3172 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3173
3174 return f;
3175}
3176
3177/*
3178 * Variation on os2emx.popen2
3179 *
Tim Peters11b23062003-04-23 02:39:17 +00003180 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003181 * and stdout+stderr combined as a single pipe.
3182 */
3183
3184static PyObject *
3185os2emx_popen4(PyObject *self, PyObject *args)
3186{
3187 PyObject *f;
3188 int tm = 0;
3189
3190 char *cmdstring;
3191 char *mode = "t";
3192 int bufsize = -1;
3193 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3194 return NULL;
3195
3196 if (*mode == 't')
3197 tm = O_TEXT;
3198 else if (*mode != 'b') {
3199 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3200 return NULL;
3201 } else
3202 tm = O_BINARY;
3203
3204 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3205
3206 return f;
3207}
3208
3209/* a couple of structures for convenient handling of multiple
3210 * file handles and pipes
3211 */
3212struct file_ref
3213{
3214 int handle;
3215 int flags;
3216};
3217
3218struct pipe_ref
3219{
3220 int rd;
3221 int wr;
3222};
3223
3224/* The following code is derived from the win32 code */
3225
3226static PyObject *
3227_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3228{
3229 struct file_ref stdio[3];
3230 struct pipe_ref p_fd[3];
3231 FILE *p_s[3];
3232 int file_count, i, pipe_err, pipe_pid;
3233 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3234 PyObject *f, *p_f[3];
3235
3236 /* file modes for subsequent fdopen's on pipe handles */
3237 if (mode == O_TEXT)
3238 {
3239 rd_mode = "rt";
3240 wr_mode = "wt";
3241 }
3242 else
3243 {
3244 rd_mode = "rb";
3245 wr_mode = "wb";
3246 }
3247
3248 /* prepare shell references */
3249 if ((shell = getenv("EMXSHELL")) == NULL)
3250 if ((shell = getenv("COMSPEC")) == NULL)
3251 {
3252 errno = ENOENT;
3253 return posix_error();
3254 }
3255
3256 sh_name = _getname(shell);
3257 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3258 opt = "/c";
3259 else
3260 opt = "-c";
3261
3262 /* save current stdio fds + their flags, and set not inheritable */
3263 i = pipe_err = 0;
3264 while (pipe_err >= 0 && i < 3)
3265 {
3266 pipe_err = stdio[i].handle = dup(i);
3267 stdio[i].flags = fcntl(i, F_GETFD, 0);
3268 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3269 i++;
3270 }
3271 if (pipe_err < 0)
3272 {
3273 /* didn't get them all saved - clean up and bail out */
3274 int saved_err = errno;
3275 while (i-- > 0)
3276 {
3277 close(stdio[i].handle);
3278 }
3279 errno = saved_err;
3280 return posix_error();
3281 }
3282
3283 /* create pipe ends */
3284 file_count = 2;
3285 if (n == POPEN_3)
3286 file_count = 3;
3287 i = pipe_err = 0;
3288 while ((pipe_err == 0) && (i < file_count))
3289 pipe_err = pipe((int *)&p_fd[i++]);
3290 if (pipe_err < 0)
3291 {
3292 /* didn't get them all made - clean up and bail out */
3293 while (i-- > 0)
3294 {
3295 close(p_fd[i].wr);
3296 close(p_fd[i].rd);
3297 }
3298 errno = EPIPE;
3299 return posix_error();
3300 }
3301
3302 /* change the actual standard IO streams over temporarily,
3303 * making the retained pipe ends non-inheritable
3304 */
3305 pipe_err = 0;
3306
3307 /* - stdin */
3308 if (dup2(p_fd[0].rd, 0) == 0)
3309 {
3310 close(p_fd[0].rd);
3311 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3312 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3313 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3314 {
3315 close(p_fd[0].wr);
3316 pipe_err = -1;
3317 }
3318 }
3319 else
3320 {
3321 pipe_err = -1;
3322 }
3323
3324 /* - stdout */
3325 if (pipe_err == 0)
3326 {
3327 if (dup2(p_fd[1].wr, 1) == 1)
3328 {
3329 close(p_fd[1].wr);
3330 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3331 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3332 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3333 {
3334 close(p_fd[1].rd);
3335 pipe_err = -1;
3336 }
3337 }
3338 else
3339 {
3340 pipe_err = -1;
3341 }
3342 }
3343
3344 /* - stderr, as required */
3345 if (pipe_err == 0)
3346 switch (n)
3347 {
3348 case POPEN_3:
3349 {
3350 if (dup2(p_fd[2].wr, 2) == 2)
3351 {
3352 close(p_fd[2].wr);
3353 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3354 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3355 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3356 {
3357 close(p_fd[2].rd);
3358 pipe_err = -1;
3359 }
3360 }
3361 else
3362 {
3363 pipe_err = -1;
3364 }
3365 break;
3366 }
3367
3368 case POPEN_4:
3369 {
3370 if (dup2(1, 2) != 2)
3371 {
3372 pipe_err = -1;
3373 }
3374 break;
3375 }
3376 }
3377
3378 /* spawn the child process */
3379 if (pipe_err == 0)
3380 {
3381 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3382 if (pipe_pid == -1)
3383 {
3384 pipe_err = -1;
3385 }
3386 else
3387 {
3388 /* save the PID into the FILE structure
3389 * NOTE: this implementation doesn't actually
3390 * take advantage of this, but do it for
3391 * completeness - AIM Apr01
3392 */
3393 for (i = 0; i < file_count; i++)
3394 p_s[i]->_pid = pipe_pid;
3395 }
3396 }
3397
3398 /* reset standard IO to normal */
3399 for (i = 0; i < 3; i++)
3400 {
3401 dup2(stdio[i].handle, i);
3402 fcntl(i, F_SETFD, stdio[i].flags);
3403 close(stdio[i].handle);
3404 }
3405
3406 /* if any remnant problems, clean up and bail out */
3407 if (pipe_err < 0)
3408 {
3409 for (i = 0; i < 3; i++)
3410 {
3411 close(p_fd[i].rd);
3412 close(p_fd[i].wr);
3413 }
3414 errno = EPIPE;
3415 return posix_error_with_filename(cmdstring);
3416 }
3417
3418 /* build tuple of file objects to return */
3419 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3420 PyFile_SetBufSize(p_f[0], bufsize);
3421 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3422 PyFile_SetBufSize(p_f[1], bufsize);
3423 if (n == POPEN_3)
3424 {
3425 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3426 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003427 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003428 }
3429 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003430 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003431
3432 /*
3433 * Insert the files we've created into the process dictionary
3434 * all referencing the list with the process handle and the
3435 * initial number of files (see description below in _PyPclose).
3436 * Since if _PyPclose later tried to wait on a process when all
3437 * handles weren't closed, it could create a deadlock with the
3438 * child, we spend some energy here to try to ensure that we
3439 * either insert all file handles into the dictionary or none
3440 * at all. It's a little clumsy with the various popen modes
3441 * and variable number of files involved.
3442 */
3443 if (!_PyPopenProcs)
3444 {
3445 _PyPopenProcs = PyDict_New();
3446 }
3447
3448 if (_PyPopenProcs)
3449 {
3450 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3451 int ins_rc[3];
3452
3453 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3454 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3455
3456 procObj = PyList_New(2);
3457 pidObj = PyInt_FromLong((long) pipe_pid);
3458 intObj = PyInt_FromLong((long) file_count);
3459
3460 if (procObj && pidObj && intObj)
3461 {
3462 PyList_SetItem(procObj, 0, pidObj);
3463 PyList_SetItem(procObj, 1, intObj);
3464
3465 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3466 if (fileObj[0])
3467 {
3468 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3469 fileObj[0],
3470 procObj);
3471 }
3472 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3473 if (fileObj[1])
3474 {
3475 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3476 fileObj[1],
3477 procObj);
3478 }
3479 if (file_count >= 3)
3480 {
3481 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3482 if (fileObj[2])
3483 {
3484 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3485 fileObj[2],
3486 procObj);
3487 }
3488 }
3489
3490 if (ins_rc[0] < 0 || !fileObj[0] ||
3491 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3492 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3493 {
3494 /* Something failed - remove any dictionary
3495 * entries that did make it.
3496 */
3497 if (!ins_rc[0] && fileObj[0])
3498 {
3499 PyDict_DelItem(_PyPopenProcs,
3500 fileObj[0]);
3501 }
3502 if (!ins_rc[1] && fileObj[1])
3503 {
3504 PyDict_DelItem(_PyPopenProcs,
3505 fileObj[1]);
3506 }
3507 if (!ins_rc[2] && fileObj[2])
3508 {
3509 PyDict_DelItem(_PyPopenProcs,
3510 fileObj[2]);
3511 }
3512 }
3513 }
Tim Peters11b23062003-04-23 02:39:17 +00003514
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003515 /*
3516 * Clean up our localized references for the dictionary keys
3517 * and value since PyDict_SetItem will Py_INCREF any copies
3518 * that got placed in the dictionary.
3519 */
3520 Py_XDECREF(procObj);
3521 Py_XDECREF(fileObj[0]);
3522 Py_XDECREF(fileObj[1]);
3523 Py_XDECREF(fileObj[2]);
3524 }
3525
3526 /* Child is launched. */
3527 return f;
3528}
3529
3530/*
3531 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3532 * exit code for the child process and return as a result of the close.
3533 *
3534 * This function uses the _PyPopenProcs dictionary in order to map the
3535 * input file pointer to information about the process that was
3536 * originally created by the popen* call that created the file pointer.
3537 * The dictionary uses the file pointer as a key (with one entry
3538 * inserted for each file returned by the original popen* call) and a
3539 * single list object as the value for all files from a single call.
3540 * The list object contains the Win32 process handle at [0], and a file
3541 * count at [1], which is initialized to the total number of file
3542 * handles using that list.
3543 *
3544 * This function closes whichever handle it is passed, and decrements
3545 * the file count in the dictionary for the process handle pointed to
3546 * by this file. On the last close (when the file count reaches zero),
3547 * this function will wait for the child process and then return its
3548 * exit code as the result of the close() operation. This permits the
3549 * files to be closed in any order - it is always the close() of the
3550 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003551 *
3552 * NOTE: This function is currently called with the GIL released.
3553 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003554 */
3555
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003556static int _PyPclose(FILE *file)
3557{
3558 int result;
3559 int exit_code;
3560 int pipe_pid;
3561 PyObject *procObj, *pidObj, *intObj, *fileObj;
3562 int file_count;
3563#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003564 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003565#endif
3566
3567 /* Close the file handle first, to ensure it can't block the
3568 * child from exiting if it's the last handle.
3569 */
3570 result = fclose(file);
3571
3572#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003573 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003574#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003575 if (_PyPopenProcs)
3576 {
3577 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3578 (procObj = PyDict_GetItem(_PyPopenProcs,
3579 fileObj)) != NULL &&
3580 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3581 (intObj = PyList_GetItem(procObj,1)) != NULL)
3582 {
3583 pipe_pid = (int) PyInt_AsLong(pidObj);
3584 file_count = (int) PyInt_AsLong(intObj);
3585
3586 if (file_count > 1)
3587 {
3588 /* Still other files referencing process */
3589 file_count--;
3590 PyList_SetItem(procObj,1,
3591 PyInt_FromLong((long) file_count));
3592 }
3593 else
3594 {
3595 /* Last file for this process */
3596 if (result != EOF &&
3597 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3598 {
3599 /* extract exit status */
3600 if (WIFEXITED(exit_code))
3601 {
3602 result = WEXITSTATUS(exit_code);
3603 }
3604 else
3605 {
3606 errno = EPIPE;
3607 result = -1;
3608 }
3609 }
3610 else
3611 {
3612 /* Indicate failure - this will cause the file object
3613 * to raise an I/O error and translate the last
3614 * error code from errno. We do have a problem with
3615 * last errors that overlap the normal errno table,
3616 * but that's a consistent problem with the file object.
3617 */
3618 result = -1;
3619 }
3620 }
3621
3622 /* Remove this file pointer from dictionary */
3623 PyDict_DelItem(_PyPopenProcs, fileObj);
3624
3625 if (PyDict_Size(_PyPopenProcs) == 0)
3626 {
3627 Py_DECREF(_PyPopenProcs);
3628 _PyPopenProcs = NULL;
3629 }
3630
3631 } /* if object retrieval ok */
3632
3633 Py_XDECREF(fileObj);
3634 } /* if _PyPopenProcs */
3635
3636#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003637 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003638#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003639 return result;
3640}
3641
3642#endif /* PYCC_??? */
3643
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003644#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003645
3646/*
3647 * Portable 'popen' replacement for Win32.
3648 *
3649 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3650 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003651 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003652 */
3653
3654#include <malloc.h>
3655#include <io.h>
3656#include <fcntl.h>
3657
3658/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3659#define POPEN_1 1
3660#define POPEN_2 2
3661#define POPEN_3 3
3662#define POPEN_4 4
3663
3664static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003665static int _PyPclose(FILE *file);
3666
3667/*
3668 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003669 * for use when retrieving the process exit code. See _PyPclose() below
3670 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003671 */
3672static PyObject *_PyPopenProcs = NULL;
3673
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003674
3675/* popen that works from a GUI.
3676 *
3677 * The result of this function is a pipe (file) connected to the
3678 * processes stdin or stdout, depending on the requested mode.
3679 */
3680
3681static PyObject *
3682posix_popen(PyObject *self, PyObject *args)
3683{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003684 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003685 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003686
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003687 char *cmdstring;
3688 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003689 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003690 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003691 return NULL;
3692
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003693 if (*mode == 'r')
3694 tm = _O_RDONLY;
3695 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003696 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003697 return NULL;
3698 } else
3699 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003700
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003701 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003702 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003703 return NULL;
3704 }
3705
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003706 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003707 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003708 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003709 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003710 else
3711 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
3712
3713 return f;
3714}
3715
3716/* Variation on win32pipe.popen
3717 *
3718 * The result of this function is a pipe (file) connected to the
3719 * process's stdin, and a pipe connected to the process's stdout.
3720 */
3721
3722static PyObject *
3723win32_popen2(PyObject *self, PyObject *args)
3724{
3725 PyObject *f;
3726 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00003727
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003728 char *cmdstring;
3729 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003730 int bufsize = -1;
3731 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003732 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003733
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003734 if (*mode == 't')
3735 tm = _O_TEXT;
3736 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003737 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003738 return NULL;
3739 } else
3740 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003741
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003742 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003743 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003744 return NULL;
3745 }
3746
3747 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00003748
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003749 return f;
3750}
3751
3752/*
3753 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003754 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003755 * The result of this function is 3 pipes - the process's stdin,
3756 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003757 */
3758
3759static PyObject *
3760win32_popen3(PyObject *self, PyObject *args)
3761{
3762 PyObject *f;
3763 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003764
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003765 char *cmdstring;
3766 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003767 int bufsize = -1;
3768 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003769 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003770
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003771 if (*mode == 't')
3772 tm = _O_TEXT;
3773 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003774 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003775 return NULL;
3776 } else
3777 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003778
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003779 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003780 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003781 return NULL;
3782 }
3783
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003784 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00003785
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003786 return f;
3787}
3788
3789/*
3790 * Variation on win32pipe.popen
3791 *
Tim Peters5aa91602002-01-30 05:46:57 +00003792 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003793 * and stdout+stderr combined as a single pipe.
3794 */
3795
3796static PyObject *
3797win32_popen4(PyObject *self, PyObject *args)
3798{
3799 PyObject *f;
3800 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003801
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003802 char *cmdstring;
3803 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003804 int bufsize = -1;
3805 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003806 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003807
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003808 if (*mode == 't')
3809 tm = _O_TEXT;
3810 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003811 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003812 return NULL;
3813 } else
3814 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003815
3816 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003817 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003818 return NULL;
3819 }
3820
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003821 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003822
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003823 return f;
3824}
3825
Mark Hammond08501372001-01-31 07:30:29 +00003826static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00003827_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003828 HANDLE hStdin,
3829 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00003830 HANDLE hStderr,
3831 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003832{
3833 PROCESS_INFORMATION piProcInfo;
3834 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003835 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003836 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00003837 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003838 int i;
3839 int x;
3840
3841 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00003842 char *comshell;
3843
Tim Peters92e4dd82002-10-05 01:47:34 +00003844 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003845 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
3846 return x;
Tim Peters402d5982001-08-27 06:37:48 +00003847
3848 /* Explicitly check if we are using COMMAND.COM. If we are
3849 * then use the w9xpopen hack.
3850 */
3851 comshell = s1 + x;
3852 while (comshell >= s1 && *comshell != '\\')
3853 --comshell;
3854 ++comshell;
3855
3856 if (GetVersion() < 0x80000000 &&
3857 _stricmp(comshell, "command.com") != 0) {
3858 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003859 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00003860 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003861 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00003862 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003863 }
3864 else {
3865 /*
Tim Peters402d5982001-08-27 06:37:48 +00003866 * Oh gag, we're on Win9x or using COMMAND.COM. Use
3867 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003868 */
Mark Hammond08501372001-01-31 07:30:29 +00003869 char modulepath[_MAX_PATH];
3870 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003871 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
3872 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003873 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003874 x = i+1;
3875 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00003876 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00003877 strncat(modulepath,
3878 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003879 (sizeof(modulepath)/sizeof(modulepath[0]))
3880 -strlen(modulepath));
3881 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003882 /* Eeek - file-not-found - possibly an embedding
3883 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00003884 */
Tim Peters5aa91602002-01-30 05:46:57 +00003885 strncpy(modulepath,
3886 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00003887 sizeof(modulepath)/sizeof(modulepath[0]));
3888 if (modulepath[strlen(modulepath)-1] != '\\')
3889 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00003890 strncat(modulepath,
3891 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003892 (sizeof(modulepath)/sizeof(modulepath[0]))
3893 -strlen(modulepath));
3894 /* No where else to look - raise an easily identifiable
3895 error, rather than leaving Windows to report
3896 "file not found" - as the user is probably blissfully
3897 unaware this shim EXE is used, and it will confuse them.
3898 (well, it confused me for a while ;-)
3899 */
3900 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003901 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00003902 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00003903 "for popen to work with your shell "
3904 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00003905 szConsoleSpawn);
3906 return FALSE;
3907 }
3908 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003909 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00003910 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003911 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00003912
Tim Peters92e4dd82002-10-05 01:47:34 +00003913 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003914 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003915 /* To maintain correct argument passing semantics,
3916 we pass the command-line as it stands, and allow
3917 quoting to be applied. w9xpopen.exe will then
3918 use its argv vector, and re-quote the necessary
3919 args for the ultimate child process.
3920 */
Tim Peters75cdad52001-11-28 22:07:30 +00003921 PyOS_snprintf(
3922 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003923 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003924 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003925 s1,
3926 s3,
3927 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003928 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00003929 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003930 dialog:
3931 "Your program accessed mem currently in use at xxx"
3932 and a hopeful warning about the stability of your
3933 system.
3934 Cost is Ctrl+C wont kill children, but anyone
3935 who cares can have a go!
3936 */
3937 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003938 }
3939 }
3940
3941 /* Could be an else here to try cmd.exe / command.com in the path
3942 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00003943 else {
Tim Peters402d5982001-08-27 06:37:48 +00003944 PyErr_SetString(PyExc_RuntimeError,
3945 "Cannot locate a COMSPEC environment variable to "
3946 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00003947 return FALSE;
3948 }
Tim Peters5aa91602002-01-30 05:46:57 +00003949
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003950 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
3951 siStartInfo.cb = sizeof(STARTUPINFO);
3952 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
3953 siStartInfo.hStdInput = hStdin;
3954 siStartInfo.hStdOutput = hStdout;
3955 siStartInfo.hStdError = hStderr;
3956 siStartInfo.wShowWindow = SW_HIDE;
3957
3958 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003959 s2,
3960 NULL,
3961 NULL,
3962 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003963 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003964 NULL,
3965 NULL,
3966 &siStartInfo,
3967 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003968 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003969 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003970
Mark Hammondb37a3732000-08-14 04:47:33 +00003971 /* Return process handle */
3972 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003973 return TRUE;
3974 }
Tim Peters402d5982001-08-27 06:37:48 +00003975 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003976 return FALSE;
3977}
3978
3979/* The following code is based off of KB: Q190351 */
3980
3981static PyObject *
3982_PyPopen(char *cmdstring, int mode, int n)
3983{
3984 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
3985 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00003986 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00003987
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003988 SECURITY_ATTRIBUTES saAttr;
3989 BOOL fSuccess;
3990 int fd1, fd2, fd3;
3991 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00003992 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003993 PyObject *f;
3994
3995 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
3996 saAttr.bInheritHandle = TRUE;
3997 saAttr.lpSecurityDescriptor = NULL;
3998
3999 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
4000 return win32_error("CreatePipe", NULL);
4001
4002 /* Create new output read handle and the input write handle. Set
4003 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004004 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004005 * being created. */
4006 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004007 GetCurrentProcess(), &hChildStdinWrDup, 0,
4008 FALSE,
4009 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004010 if (!fSuccess)
4011 return win32_error("DuplicateHandle", NULL);
4012
4013 /* Close the inheritable version of ChildStdin
4014 that we're using. */
4015 CloseHandle(hChildStdinWr);
4016
4017 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4018 return win32_error("CreatePipe", NULL);
4019
4020 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004021 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4022 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004023 if (!fSuccess)
4024 return win32_error("DuplicateHandle", NULL);
4025
4026 /* Close the inheritable version of ChildStdout
4027 that we're using. */
4028 CloseHandle(hChildStdoutRd);
4029
4030 if (n != POPEN_4) {
4031 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4032 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004033 fSuccess = DuplicateHandle(GetCurrentProcess(),
4034 hChildStderrRd,
4035 GetCurrentProcess(),
4036 &hChildStderrRdDup, 0,
4037 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004038 if (!fSuccess)
4039 return win32_error("DuplicateHandle", NULL);
4040 /* Close the inheritable version of ChildStdErr that we're using. */
4041 CloseHandle(hChildStderrRd);
4042 }
Tim Peters5aa91602002-01-30 05:46:57 +00004043
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004044 switch (n) {
4045 case POPEN_1:
4046 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4047 case _O_WRONLY | _O_TEXT:
4048 /* Case for writing to child Stdin in text mode. */
4049 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4050 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004051 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004052 PyFile_SetBufSize(f, 0);
4053 /* We don't care about these pipes anymore, so close them. */
4054 CloseHandle(hChildStdoutRdDup);
4055 CloseHandle(hChildStderrRdDup);
4056 break;
4057
4058 case _O_RDONLY | _O_TEXT:
4059 /* Case for reading from child Stdout in text mode. */
4060 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4061 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004062 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004063 PyFile_SetBufSize(f, 0);
4064 /* We don't care about these pipes anymore, so close them. */
4065 CloseHandle(hChildStdinWrDup);
4066 CloseHandle(hChildStderrRdDup);
4067 break;
4068
4069 case _O_RDONLY | _O_BINARY:
4070 /* Case for readinig from child Stdout in binary mode. */
4071 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4072 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004073 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004074 PyFile_SetBufSize(f, 0);
4075 /* We don't care about these pipes anymore, so close them. */
4076 CloseHandle(hChildStdinWrDup);
4077 CloseHandle(hChildStderrRdDup);
4078 break;
4079
4080 case _O_WRONLY | _O_BINARY:
4081 /* Case for writing to child Stdin in binary mode. */
4082 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4083 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004084 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004085 PyFile_SetBufSize(f, 0);
4086 /* We don't care about these pipes anymore, so close them. */
4087 CloseHandle(hChildStdoutRdDup);
4088 CloseHandle(hChildStderrRdDup);
4089 break;
4090 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004091 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004092 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004093
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004094 case POPEN_2:
4095 case POPEN_4:
4096 {
4097 char *m1, *m2;
4098 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004099
Tim Peters7dca21e2002-08-19 00:42:29 +00004100 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004101 m1 = "r";
4102 m2 = "w";
4103 } else {
4104 m1 = "rb";
4105 m2 = "wb";
4106 }
4107
4108 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4109 f1 = _fdopen(fd1, m2);
4110 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4111 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004112 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004113 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004114 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004115 PyFile_SetBufSize(p2, 0);
4116
4117 if (n != 4)
4118 CloseHandle(hChildStderrRdDup);
4119
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004120 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004121 Py_XDECREF(p1);
4122 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004123 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004124 break;
4125 }
Tim Peters5aa91602002-01-30 05:46:57 +00004126
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004127 case POPEN_3:
4128 {
4129 char *m1, *m2;
4130 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004131
Tim Peters7dca21e2002-08-19 00:42:29 +00004132 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004133 m1 = "r";
4134 m2 = "w";
4135 } else {
4136 m1 = "rb";
4137 m2 = "wb";
4138 }
4139
4140 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4141 f1 = _fdopen(fd1, m2);
4142 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4143 f2 = _fdopen(fd2, m1);
4144 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4145 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004146 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004147 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4148 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004149 PyFile_SetBufSize(p1, 0);
4150 PyFile_SetBufSize(p2, 0);
4151 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004152 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004153 Py_XDECREF(p1);
4154 Py_XDECREF(p2);
4155 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004156 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004157 break;
4158 }
4159 }
4160
4161 if (n == POPEN_4) {
4162 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004163 hChildStdinRd,
4164 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004165 hChildStdoutWr,
4166 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004167 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004168 }
4169 else {
4170 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004171 hChildStdinRd,
4172 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004173 hChildStderrWr,
4174 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004175 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004176 }
4177
Mark Hammondb37a3732000-08-14 04:47:33 +00004178 /*
4179 * Insert the files we've created into the process dictionary
4180 * all referencing the list with the process handle and the
4181 * initial number of files (see description below in _PyPclose).
4182 * Since if _PyPclose later tried to wait on a process when all
4183 * handles weren't closed, it could create a deadlock with the
4184 * child, we spend some energy here to try to ensure that we
4185 * either insert all file handles into the dictionary or none
4186 * at all. It's a little clumsy with the various popen modes
4187 * and variable number of files involved.
4188 */
4189 if (!_PyPopenProcs) {
4190 _PyPopenProcs = PyDict_New();
4191 }
4192
4193 if (_PyPopenProcs) {
4194 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4195 int ins_rc[3];
4196
4197 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4198 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4199
4200 procObj = PyList_New(2);
4201 hProcessObj = PyLong_FromVoidPtr(hProcess);
4202 intObj = PyInt_FromLong(file_count);
4203
4204 if (procObj && hProcessObj && intObj) {
4205 PyList_SetItem(procObj,0,hProcessObj);
4206 PyList_SetItem(procObj,1,intObj);
4207
4208 fileObj[0] = PyLong_FromVoidPtr(f1);
4209 if (fileObj[0]) {
4210 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4211 fileObj[0],
4212 procObj);
4213 }
4214 if (file_count >= 2) {
4215 fileObj[1] = PyLong_FromVoidPtr(f2);
4216 if (fileObj[1]) {
4217 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4218 fileObj[1],
4219 procObj);
4220 }
4221 }
4222 if (file_count >= 3) {
4223 fileObj[2] = PyLong_FromVoidPtr(f3);
4224 if (fileObj[2]) {
4225 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4226 fileObj[2],
4227 procObj);
4228 }
4229 }
4230
4231 if (ins_rc[0] < 0 || !fileObj[0] ||
4232 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4233 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4234 /* Something failed - remove any dictionary
4235 * entries that did make it.
4236 */
4237 if (!ins_rc[0] && fileObj[0]) {
4238 PyDict_DelItem(_PyPopenProcs,
4239 fileObj[0]);
4240 }
4241 if (!ins_rc[1] && fileObj[1]) {
4242 PyDict_DelItem(_PyPopenProcs,
4243 fileObj[1]);
4244 }
4245 if (!ins_rc[2] && fileObj[2]) {
4246 PyDict_DelItem(_PyPopenProcs,
4247 fileObj[2]);
4248 }
4249 }
4250 }
Tim Peters5aa91602002-01-30 05:46:57 +00004251
Mark Hammondb37a3732000-08-14 04:47:33 +00004252 /*
4253 * Clean up our localized references for the dictionary keys
4254 * and value since PyDict_SetItem will Py_INCREF any copies
4255 * that got placed in the dictionary.
4256 */
4257 Py_XDECREF(procObj);
4258 Py_XDECREF(fileObj[0]);
4259 Py_XDECREF(fileObj[1]);
4260 Py_XDECREF(fileObj[2]);
4261 }
4262
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004263 /* Child is launched. Close the parents copy of those pipe
4264 * handles that only the child should have open. You need to
4265 * make sure that no handles to the write end of the output pipe
4266 * are maintained in this process or else the pipe will not close
4267 * when the child process exits and the ReadFile will hang. */
4268
4269 if (!CloseHandle(hChildStdinRd))
4270 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004271
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004272 if (!CloseHandle(hChildStdoutWr))
4273 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004274
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004275 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4276 return win32_error("CloseHandle", NULL);
4277
4278 return f;
4279}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004280
4281/*
4282 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4283 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004284 *
4285 * This function uses the _PyPopenProcs dictionary in order to map the
4286 * input file pointer to information about the process that was
4287 * originally created by the popen* call that created the file pointer.
4288 * The dictionary uses the file pointer as a key (with one entry
4289 * inserted for each file returned by the original popen* call) and a
4290 * single list object as the value for all files from a single call.
4291 * The list object contains the Win32 process handle at [0], and a file
4292 * count at [1], which is initialized to the total number of file
4293 * handles using that list.
4294 *
4295 * This function closes whichever handle it is passed, and decrements
4296 * the file count in the dictionary for the process handle pointed to
4297 * by this file. On the last close (when the file count reaches zero),
4298 * this function will wait for the child process and then return its
4299 * exit code as the result of the close() operation. This permits the
4300 * files to be closed in any order - it is always the close() of the
4301 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004302 *
4303 * NOTE: This function is currently called with the GIL released.
4304 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004305 */
Tim Peters736aa322000-09-01 06:51:24 +00004306
Fredrik Lundh56055a42000-07-23 19:47:12 +00004307static int _PyPclose(FILE *file)
4308{
Fredrik Lundh20318932000-07-26 17:29:12 +00004309 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004310 DWORD exit_code;
4311 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004312 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4313 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004314#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004315 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004316#endif
4317
Fredrik Lundh20318932000-07-26 17:29:12 +00004318 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004319 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004320 */
4321 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004322#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004323 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004324#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004325 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004326 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4327 (procObj = PyDict_GetItem(_PyPopenProcs,
4328 fileObj)) != NULL &&
4329 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4330 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4331
4332 hProcess = PyLong_AsVoidPtr(hProcessObj);
4333 file_count = PyInt_AsLong(intObj);
4334
4335 if (file_count > 1) {
4336 /* Still other files referencing process */
4337 file_count--;
4338 PyList_SetItem(procObj,1,
4339 PyInt_FromLong(file_count));
4340 } else {
4341 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004342 if (result != EOF &&
4343 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4344 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004345 /* Possible truncation here in 16-bit environments, but
4346 * real exit codes are just the lower byte in any event.
4347 */
4348 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004349 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004350 /* Indicate failure - this will cause the file object
4351 * to raise an I/O error and translate the last Win32
4352 * error code from errno. We do have a problem with
4353 * last errors that overlap the normal errno table,
4354 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004355 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004356 if (result != EOF) {
4357 /* If the error wasn't from the fclose(), then
4358 * set errno for the file object error handling.
4359 */
4360 errno = GetLastError();
4361 }
4362 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004363 }
4364
4365 /* Free up the native handle at this point */
4366 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004367 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004368
Mark Hammondb37a3732000-08-14 04:47:33 +00004369 /* Remove this file pointer from dictionary */
4370 PyDict_DelItem(_PyPopenProcs, fileObj);
4371
4372 if (PyDict_Size(_PyPopenProcs) == 0) {
4373 Py_DECREF(_PyPopenProcs);
4374 _PyPopenProcs = NULL;
4375 }
4376
4377 } /* if object retrieval ok */
4378
4379 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004380 } /* if _PyPopenProcs */
4381
Tim Peters736aa322000-09-01 06:51:24 +00004382#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004383 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004384#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004385 return result;
4386}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004387
4388#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004389static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004390posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004391{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004392 char *name;
4393 char *mode = "r";
4394 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004395 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004396 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004397 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004398 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004399 /* Strip mode of binary or text modifiers */
4400 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4401 mode = "r";
4402 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4403 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004404 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004405 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004406 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004407 if (fp == NULL)
4408 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004409 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004410 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004411 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004412 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004413}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004414
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004415#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004416#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004417
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004418
Guido van Rossumb6775db1994-08-01 11:34:53 +00004419#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004420PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004421"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004422Set the current process's user id.");
4423
Barry Warsaw53699e91996-12-10 23:23:01 +00004424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004425posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004426{
4427 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004428 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004429 return NULL;
4430 if (setuid(uid) < 0)
4431 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004432 Py_INCREF(Py_None);
4433 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004434}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004435#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004436
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004437
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004438#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004439PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004440"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004441Set the current process's effective user id.");
4442
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004443static PyObject *
4444posix_seteuid (PyObject *self, PyObject *args)
4445{
4446 int euid;
4447 if (!PyArg_ParseTuple(args, "i", &euid)) {
4448 return NULL;
4449 } else if (seteuid(euid) < 0) {
4450 return posix_error();
4451 } else {
4452 Py_INCREF(Py_None);
4453 return Py_None;
4454 }
4455}
4456#endif /* HAVE_SETEUID */
4457
4458#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004459PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004460"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004461Set the current process's effective group id.");
4462
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004463static PyObject *
4464posix_setegid (PyObject *self, PyObject *args)
4465{
4466 int egid;
4467 if (!PyArg_ParseTuple(args, "i", &egid)) {
4468 return NULL;
4469 } else if (setegid(egid) < 0) {
4470 return posix_error();
4471 } else {
4472 Py_INCREF(Py_None);
4473 return Py_None;
4474 }
4475}
4476#endif /* HAVE_SETEGID */
4477
4478#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004479PyDoc_STRVAR(posix_setreuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004480"seteuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004481Set the current process's real and effective user ids.");
4482
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004483static PyObject *
4484posix_setreuid (PyObject *self, PyObject *args)
4485{
4486 int ruid, euid;
4487 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4488 return NULL;
4489 } else if (setreuid(ruid, euid) < 0) {
4490 return posix_error();
4491 } else {
4492 Py_INCREF(Py_None);
4493 return Py_None;
4494 }
4495}
4496#endif /* HAVE_SETREUID */
4497
4498#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004499PyDoc_STRVAR(posix_setregid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004500"setegid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004501Set the current process's real and effective group ids.");
4502
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004503static PyObject *
4504posix_setregid (PyObject *self, PyObject *args)
4505{
4506 int rgid, egid;
4507 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4508 return NULL;
4509 } else if (setregid(rgid, egid) < 0) {
4510 return posix_error();
4511 } else {
4512 Py_INCREF(Py_None);
4513 return Py_None;
4514 }
4515}
4516#endif /* HAVE_SETREGID */
4517
Guido van Rossumb6775db1994-08-01 11:34:53 +00004518#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004519PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004520"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004521Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004522
Barry Warsaw53699e91996-12-10 23:23:01 +00004523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004524posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004525{
4526 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004527 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004528 return NULL;
4529 if (setgid(gid) < 0)
4530 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004531 Py_INCREF(Py_None);
4532 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004533}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004534#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004535
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004536#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004537PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004538"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004539Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004540
4541static PyObject *
4542posix_setgroups(PyObject *self, PyObject *args)
4543{
4544 PyObject *groups;
4545 int i, len;
4546 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004547
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004548 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4549 return NULL;
4550 if (!PySequence_Check(groups)) {
4551 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4552 return NULL;
4553 }
4554 len = PySequence_Size(groups);
4555 if (len > MAX_GROUPS) {
4556 PyErr_SetString(PyExc_ValueError, "too many groups");
4557 return NULL;
4558 }
4559 for(i = 0; i < len; i++) {
4560 PyObject *elem;
4561 elem = PySequence_GetItem(groups, i);
4562 if (!elem)
4563 return NULL;
4564 if (!PyInt_Check(elem)) {
4565 PyErr_SetString(PyExc_TypeError,
4566 "groups must be integers");
4567 Py_DECREF(elem);
4568 return NULL;
4569 }
4570 /* XXX: check that value fits into gid_t. */
4571 grouplist[i] = PyInt_AsLong(elem);
4572 Py_DECREF(elem);
4573 }
4574
4575 if (setgroups(len, grouplist) < 0)
4576 return posix_error();
4577 Py_INCREF(Py_None);
4578 return Py_None;
4579}
4580#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004581
Guido van Rossumb6775db1994-08-01 11:34:53 +00004582#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004583PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004584"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004585Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004586
Barry Warsaw53699e91996-12-10 23:23:01 +00004587static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004588posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004589{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004590 int pid, options;
4591#ifdef UNION_WAIT
4592 union wait status;
4593#define status_i (status.w_status)
4594#else
4595 int status;
4596#define status_i status
4597#endif
4598 status_i = 0;
4599
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004600 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004601 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004602 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004603 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004604 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004605 if (pid == -1)
4606 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004607 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004608 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004609}
4610
Tim Petersab034fa2002-02-01 11:27:43 +00004611#elif defined(HAVE_CWAIT)
4612
4613/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004614PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004615"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004616"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004617
4618static PyObject *
4619posix_waitpid(PyObject *self, PyObject *args)
4620{
4621 int pid, options;
4622 int status;
4623
4624 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4625 return NULL;
4626 Py_BEGIN_ALLOW_THREADS
4627 pid = _cwait(&status, pid, options);
4628 Py_END_ALLOW_THREADS
4629 if (pid == -1)
4630 return posix_error();
4631 else
4632 /* shift the status left a byte so this is more like the
4633 POSIX waitpid */
4634 return Py_BuildValue("ii", pid, status << 8);
4635}
4636#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004637
Guido van Rossumad0ee831995-03-01 10:34:45 +00004638#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004640"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004641Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004642
Barry Warsaw53699e91996-12-10 23:23:01 +00004643static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004644posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004645{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004646 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004647#ifdef UNION_WAIT
4648 union wait status;
4649#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004650#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004651 int status;
4652#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004653#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004654
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004655 status_i = 0;
4656 Py_BEGIN_ALLOW_THREADS
4657 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004658 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004659 if (pid == -1)
4660 return posix_error();
4661 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004662 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004663#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004664}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004665#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004668PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004669"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004670Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004671
Barry Warsaw53699e91996-12-10 23:23:01 +00004672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004673posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004674{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004675#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004676 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004677#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004678#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004679 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004680#else
4681 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4682#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004683#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004684}
4685
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004686
Guido van Rossumb6775db1994-08-01 11:34:53 +00004687#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004688PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004689"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004690Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004691
Barry Warsaw53699e91996-12-10 23:23:01 +00004692static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004693posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004694{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004695 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004696 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004697 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004698 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004699 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004700 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004701 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004702 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004703 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004704 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004705 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004706}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004707#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004709
Guido van Rossumb6775db1994-08-01 11:34:53 +00004710#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004711PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004712"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004713Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004714
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004715static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004716posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004717{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004718 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004719}
4720#endif /* HAVE_SYMLINK */
4721
4722
4723#ifdef HAVE_TIMES
4724#ifndef HZ
4725#define HZ 60 /* Universal constant :-) */
4726#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004727
Guido van Rossumd48f2521997-12-05 22:19:34 +00004728#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4729static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004730system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004731{
4732 ULONG value = 0;
4733
4734 Py_BEGIN_ALLOW_THREADS
4735 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4736 Py_END_ALLOW_THREADS
4737
4738 return value;
4739}
4740
4741static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004742posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004743{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004744 /* Currently Only Uptime is Provided -- Others Later */
4745 return Py_BuildValue("ddddd",
4746 (double)0 /* t.tms_utime / HZ */,
4747 (double)0 /* t.tms_stime / HZ */,
4748 (double)0 /* t.tms_cutime / HZ */,
4749 (double)0 /* t.tms_cstime / HZ */,
4750 (double)system_uptime() / 1000);
4751}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004752#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004753static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004754posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004755{
4756 struct tms t;
4757 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004758 errno = 0;
4759 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004760 if (c == (clock_t) -1)
4761 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004762 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004763 (double)t.tms_utime / HZ,
4764 (double)t.tms_stime / HZ,
4765 (double)t.tms_cutime / HZ,
4766 (double)t.tms_cstime / HZ,
4767 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004768}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004769#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004770#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004771
4772
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004773#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004774#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004775static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004776posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004777{
4778 FILETIME create, exit, kernel, user;
4779 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004780 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004781 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4782 /* The fields of a FILETIME structure are the hi and lo part
4783 of a 64-bit value expressed in 100 nanosecond units.
4784 1e7 is one second in such units; 1e-7 the inverse.
4785 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4786 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004787 return Py_BuildValue(
4788 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004789 (double)(kernel.dwHighDateTime*429.4967296 +
4790 kernel.dwLowDateTime*1e-7),
4791 (double)(user.dwHighDateTime*429.4967296 +
4792 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004793 (double)0,
4794 (double)0,
4795 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004796}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004797#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004798
4799#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004800PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004801"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004802Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004803#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004805
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004806#ifdef HAVE_GETSID
4807PyDoc_STRVAR(posix_getsid__doc__,
4808"getsid(pid) -> sid\n\n\
4809Call the system call getsid().");
4810
4811static PyObject *
4812posix_getsid(PyObject *self, PyObject *args)
4813{
4814 int pid, sid;
4815 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4816 return NULL;
4817 sid = getsid(pid);
4818 if (sid < 0)
4819 return posix_error();
4820 return PyInt_FromLong((long)sid);
4821}
4822#endif /* HAVE_GETSID */
4823
4824
Guido van Rossumb6775db1994-08-01 11:34:53 +00004825#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004826PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004827"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004828Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004829
Barry Warsaw53699e91996-12-10 23:23:01 +00004830static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004831posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004832{
Guido van Rossum687dd131993-05-17 08:34:16 +00004833 if (setsid() < 0)
4834 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004835 Py_INCREF(Py_None);
4836 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004837}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004838#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004839
Guido van Rossumb6775db1994-08-01 11:34:53 +00004840#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004841PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004842"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004843Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004844
Barry Warsaw53699e91996-12-10 23:23:01 +00004845static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004846posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004847{
4848 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004849 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004850 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004851 if (setpgid(pid, pgrp) < 0)
4852 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004853 Py_INCREF(Py_None);
4854 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004855}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004856#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004857
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004858
Guido van Rossumb6775db1994-08-01 11:34:53 +00004859#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004860PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004861"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004862Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004863
Barry Warsaw53699e91996-12-10 23:23:01 +00004864static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004865posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004866{
4867 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004868 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004869 return NULL;
4870 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004871 if (pgid < 0)
4872 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004873 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004874}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004875#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004876
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004877
Guido van Rossumb6775db1994-08-01 11:34:53 +00004878#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004879PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004880"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004881Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004882
Barry Warsaw53699e91996-12-10 23:23:01 +00004883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004884posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004885{
4886 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004887 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004888 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004889 if (tcsetpgrp(fd, pgid) < 0)
4890 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004891 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004892 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004893}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004894#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004895
Guido van Rossum687dd131993-05-17 08:34:16 +00004896/* Functions acting on file descriptors */
4897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004898PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004899"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004900Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004901
Barry Warsaw53699e91996-12-10 23:23:01 +00004902static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004903posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004904{
Mark Hammondef8b6542001-05-13 08:04:26 +00004905 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004906 int flag;
4907 int mode = 0777;
4908 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004909
4910#ifdef MS_WINDOWS
4911 if (unicode_file_names()) {
4912 PyUnicodeObject *po;
4913 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4914 Py_BEGIN_ALLOW_THREADS
4915 /* PyUnicode_AS_UNICODE OK without thread
4916 lock as it is a simple dereference. */
4917 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4918 Py_END_ALLOW_THREADS
4919 if (fd < 0)
4920 return posix_error();
4921 return PyInt_FromLong((long)fd);
4922 }
4923 /* Drop the argument parsing error as narrow strings
4924 are also valid. */
4925 PyErr_Clear();
4926 }
4927#endif
4928
Tim Peters5aa91602002-01-30 05:46:57 +00004929 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004930 Py_FileSystemDefaultEncoding, &file,
4931 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004932 return NULL;
4933
Barry Warsaw53699e91996-12-10 23:23:01 +00004934 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004935 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004936 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004937 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004938 return posix_error_with_allocated_filename(file);
4939 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004940 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004941}
4942
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004943
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004944PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004945"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004946Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004947
Barry Warsaw53699e91996-12-10 23:23:01 +00004948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004949posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004950{
4951 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004952 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004953 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004954 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004955 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004956 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004957 if (res < 0)
4958 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004959 Py_INCREF(Py_None);
4960 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004961}
4962
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004964PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004965"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004966Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004967
Barry Warsaw53699e91996-12-10 23:23:01 +00004968static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004969posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004970{
4971 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004972 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004973 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004974 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004975 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004976 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004977 if (fd < 0)
4978 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004979 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004980}
4981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004983PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004984"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004985Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004986
Barry Warsaw53699e91996-12-10 23:23:01 +00004987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004988posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004989{
4990 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004991 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004992 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004993 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004994 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004995 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004996 if (res < 0)
4997 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004998 Py_INCREF(Py_None);
4999 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005000}
5001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005002
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005003PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005004"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005005Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005006
Barry Warsaw53699e91996-12-10 23:23:01 +00005007static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005008posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005009{
5010 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005011#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005012 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005013#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005014 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005015#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005016 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005017 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005018 return NULL;
5019#ifdef SEEK_SET
5020 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5021 switch (how) {
5022 case 0: how = SEEK_SET; break;
5023 case 1: how = SEEK_CUR; break;
5024 case 2: how = SEEK_END; break;
5025 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005026#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005027
5028#if !defined(HAVE_LARGEFILE_SUPPORT)
5029 pos = PyInt_AsLong(posobj);
5030#else
5031 pos = PyLong_Check(posobj) ?
5032 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5033#endif
5034 if (PyErr_Occurred())
5035 return NULL;
5036
Barry Warsaw53699e91996-12-10 23:23:01 +00005037 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005038#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005039 res = _lseeki64(fd, pos, how);
5040#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005041 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005042#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005043 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005044 if (res < 0)
5045 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005046
5047#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005048 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005049#else
5050 return PyLong_FromLongLong(res);
5051#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005052}
5053
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005055PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005056"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005057Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005058
Barry Warsaw53699e91996-12-10 23:23:01 +00005059static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005060posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005061{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005062 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005063 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005064 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005065 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005066 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005067 if (buffer == NULL)
5068 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005069 Py_BEGIN_ALLOW_THREADS
5070 n = read(fd, PyString_AsString(buffer), size);
5071 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005072 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005073 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005074 return posix_error();
5075 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005076 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005077 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005078 return buffer;
5079}
5080
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005082PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005083"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005084Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005085
Barry Warsaw53699e91996-12-10 23:23:01 +00005086static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005087posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005088{
5089 int fd, size;
5090 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005091 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005092 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005093 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005094 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005095 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005096 if (size < 0)
5097 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005098 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005099}
5100
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005102PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005103"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005104Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005105
Barry Warsaw53699e91996-12-10 23:23:01 +00005106static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005107posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005108{
5109 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005110 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005111 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005112 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005113 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005114#ifdef __VMS
5115 /* on OpenVMS we must ensure that all bytes are written to the file */
5116 fsync(fd);
5117#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005118 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005119 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005120 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005121 if (res != 0)
5122 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005123
Fred Drake699f3522000-06-29 21:12:41 +00005124 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005125}
5126
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005128PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005129"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005130Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005131
Barry Warsaw53699e91996-12-10 23:23:01 +00005132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005133posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005134{
Guido van Rossum687dd131993-05-17 08:34:16 +00005135 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005136 char *mode = "r";
5137 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005138 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005139 PyObject *f;
5140 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005141 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005142
Thomas Heller1f043e22002-11-07 16:00:59 +00005143 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5144 PyErr_Format(PyExc_ValueError,
5145 "invalid file mode '%s'", mode);
5146 return NULL;
5147 }
5148
Barry Warsaw53699e91996-12-10 23:23:01 +00005149 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005150 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005151 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005152 if (fp == NULL)
5153 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005154 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005155 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005156 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005157 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005158}
5159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005160PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005161"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005162Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005163connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005164
5165static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005166posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005167{
5168 int fd;
5169 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5170 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005171 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005172}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005173
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005174#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005175PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005176"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005177Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005178
Barry Warsaw53699e91996-12-10 23:23:01 +00005179static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005180posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005181{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005182#if defined(PYOS_OS2)
5183 HFILE read, write;
5184 APIRET rc;
5185
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005186 Py_BEGIN_ALLOW_THREADS
5187 rc = DosCreatePipe( &read, &write, 4096);
5188 Py_END_ALLOW_THREADS
5189 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005190 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005191
5192 return Py_BuildValue("(ii)", read, write);
5193#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005194#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005195 int fds[2];
5196 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005197 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005198 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005199 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005200 if (res != 0)
5201 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005202 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005203#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005204 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005205 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005206 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005207 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005208 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005209 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005210 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005211 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005212 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5213 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005214 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005215#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005216#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005217}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005218#endif /* HAVE_PIPE */
5219
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005220
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005221#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005222PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005223"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005225
Barry Warsaw53699e91996-12-10 23:23:01 +00005226static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005227posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005228{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005229 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005230 int mode = 0666;
5231 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005232 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005233 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005234 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005235 res = mkfifo(filename, mode);
5236 Py_END_ALLOW_THREADS
5237 if (res < 0)
5238 return posix_error();
5239 Py_INCREF(Py_None);
5240 return Py_None;
5241}
5242#endif
5243
5244
Neal Norwitz11690112002-07-30 01:08:28 +00005245#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005246PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005247"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005248Create a filesystem node (file, device special file or named pipe)\n\
5249named filename. mode specifies both the permissions to use and the\n\
5250type of node to be created, being combined (bitwise OR) with one of\n\
5251S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005252device defines the newly created device special file (probably using\n\
5253os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005254
5255
5256static PyObject *
5257posix_mknod(PyObject *self, PyObject *args)
5258{
5259 char *filename;
5260 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005261 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005262 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005263 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005264 return NULL;
5265 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005266 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005267 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005268 if (res < 0)
5269 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005270 Py_INCREF(Py_None);
5271 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005272}
5273#endif
5274
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005275#ifdef HAVE_DEVICE_MACROS
5276PyDoc_STRVAR(posix_major__doc__,
5277"major(device) -> major number\n\
5278Extracts a device major number from a raw device number.");
5279
5280static PyObject *
5281posix_major(PyObject *self, PyObject *args)
5282{
5283 int device;
5284 if (!PyArg_ParseTuple(args, "i:major", &device))
5285 return NULL;
5286 return PyInt_FromLong((long)major(device));
5287}
5288
5289PyDoc_STRVAR(posix_minor__doc__,
5290"minor(device) -> minor number\n\
5291Extracts a device minor number from a raw device number.");
5292
5293static PyObject *
5294posix_minor(PyObject *self, PyObject *args)
5295{
5296 int device;
5297 if (!PyArg_ParseTuple(args, "i:minor", &device))
5298 return NULL;
5299 return PyInt_FromLong((long)minor(device));
5300}
5301
5302PyDoc_STRVAR(posix_makedev__doc__,
5303"makedev(major, minor) -> device number\n\
5304Composes a raw device number from the major and minor device numbers.");
5305
5306static PyObject *
5307posix_makedev(PyObject *self, PyObject *args)
5308{
5309 int major, minor;
5310 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5311 return NULL;
5312 return PyInt_FromLong((long)makedev(major, minor));
5313}
5314#endif /* device macros */
5315
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005316
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005317#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005318PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005319"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005320Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005321
Barry Warsaw53699e91996-12-10 23:23:01 +00005322static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005323posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005324{
5325 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005326 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005327 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005328 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005329
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005330 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005331 return NULL;
5332
5333#if !defined(HAVE_LARGEFILE_SUPPORT)
5334 length = PyInt_AsLong(lenobj);
5335#else
5336 length = PyLong_Check(lenobj) ?
5337 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5338#endif
5339 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005340 return NULL;
5341
Barry Warsaw53699e91996-12-10 23:23:01 +00005342 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005343 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005344 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005345 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005346 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005347 return NULL;
5348 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005349 Py_INCREF(Py_None);
5350 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005351}
5352#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005353
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005354#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005355PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005356"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005358
Fred Drake762e2061999-08-26 17:23:54 +00005359/* Save putenv() parameters as values here, so we can collect them when they
5360 * get re-set with another call for the same key. */
5361static PyObject *posix_putenv_garbage;
5362
Tim Peters5aa91602002-01-30 05:46:57 +00005363static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005364posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005365{
5366 char *s1, *s2;
5367 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005368 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005369 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005370
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005371 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005372 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005373
5374#if defined(PYOS_OS2)
5375 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5376 APIRET rc;
5377
Guido van Rossumd48f2521997-12-05 22:19:34 +00005378 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5379 if (rc != NO_ERROR)
5380 return os2_error(rc);
5381
5382 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5383 APIRET rc;
5384
Guido van Rossumd48f2521997-12-05 22:19:34 +00005385 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5386 if (rc != NO_ERROR)
5387 return os2_error(rc);
5388 } else {
5389#endif
5390
Fred Drake762e2061999-08-26 17:23:54 +00005391 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005392 len = strlen(s1) + strlen(s2) + 2;
5393 /* len includes space for a trailing \0; the size arg to
5394 PyString_FromStringAndSize does not count that */
5395 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005396 if (newstr == NULL)
5397 return PyErr_NoMemory();
5398 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005399 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005400 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005401 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005402 posix_error();
5403 return NULL;
5404 }
Fred Drake762e2061999-08-26 17:23:54 +00005405 /* Install the first arg and newstr in posix_putenv_garbage;
5406 * this will cause previous value to be collected. This has to
5407 * happen after the real putenv() call because the old value
5408 * was still accessible until then. */
5409 if (PyDict_SetItem(posix_putenv_garbage,
5410 PyTuple_GET_ITEM(args, 0), newstr)) {
5411 /* really not much we can do; just leak */
5412 PyErr_Clear();
5413 }
5414 else {
5415 Py_DECREF(newstr);
5416 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005417
5418#if defined(PYOS_OS2)
5419 }
5420#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005421 Py_INCREF(Py_None);
5422 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005423}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005424#endif /* putenv */
5425
Guido van Rossumc524d952001-10-19 01:31:59 +00005426#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005427PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005428"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005429Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005430
5431static PyObject *
5432posix_unsetenv(PyObject *self, PyObject *args)
5433{
5434 char *s1;
5435
5436 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5437 return NULL;
5438
5439 unsetenv(s1);
5440
5441 /* Remove the key from posix_putenv_garbage;
5442 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005443 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005444 * old value was still accessible until then.
5445 */
5446 if (PyDict_DelItem(posix_putenv_garbage,
5447 PyTuple_GET_ITEM(args, 0))) {
5448 /* really not much we can do; just leak */
5449 PyErr_Clear();
5450 }
5451
5452 Py_INCREF(Py_None);
5453 return Py_None;
5454}
5455#endif /* unsetenv */
5456
Guido van Rossumb6a47161997-09-15 22:54:34 +00005457#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005459"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005460Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005461
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005462static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005463posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005464{
5465 int code;
5466 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005467 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005468 return NULL;
5469 message = strerror(code);
5470 if (message == NULL) {
5471 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005472 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005473 return NULL;
5474 }
5475 return PyString_FromString(message);
5476}
5477#endif /* strerror */
5478
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005479
Guido van Rossumc9641791998-08-04 15:26:23 +00005480#ifdef HAVE_SYS_WAIT_H
5481
Fred Drake106c1a02002-04-23 15:58:02 +00005482#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005483PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005484"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005486
5487static PyObject *
5488posix_WCOREDUMP(PyObject *self, PyObject *args)
5489{
5490#ifdef UNION_WAIT
5491 union wait status;
5492#define status_i (status.w_status)
5493#else
5494 int status;
5495#define status_i status
5496#endif
5497 status_i = 0;
5498
5499 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5500 {
5501 return NULL;
5502 }
5503
5504 return PyBool_FromLong(WCOREDUMP(status));
5505#undef status_i
5506}
5507#endif /* WCOREDUMP */
5508
5509#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005510PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005511"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005512Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005513job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005514
5515static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005516posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005517{
5518#ifdef UNION_WAIT
5519 union wait status;
5520#define status_i (status.w_status)
5521#else
5522 int status;
5523#define status_i status
5524#endif
5525 status_i = 0;
5526
5527 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5528 {
5529 return NULL;
5530 }
5531
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005532 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005533#undef status_i
5534}
5535#endif /* WIFCONTINUED */
5536
Guido van Rossumc9641791998-08-04 15:26:23 +00005537#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005538PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005539"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005540Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005541
5542static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005543posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005544{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005545#ifdef UNION_WAIT
5546 union wait status;
5547#define status_i (status.w_status)
5548#else
5549 int status;
5550#define status_i status
5551#endif
5552 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005553
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005554 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005555 {
5556 return NULL;
5557 }
Tim Peters5aa91602002-01-30 05:46:57 +00005558
Fred Drake106c1a02002-04-23 15:58:02 +00005559 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005560#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005561}
5562#endif /* WIFSTOPPED */
5563
5564#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005565PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005566"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005567Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005568
5569static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005570posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005571{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005572#ifdef UNION_WAIT
5573 union wait status;
5574#define status_i (status.w_status)
5575#else
5576 int status;
5577#define status_i status
5578#endif
5579 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005580
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005581 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005582 {
5583 return NULL;
5584 }
Tim Peters5aa91602002-01-30 05:46:57 +00005585
Fred Drake106c1a02002-04-23 15:58:02 +00005586 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005587#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005588}
5589#endif /* WIFSIGNALED */
5590
5591#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005592PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005593"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005594Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005595system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005596
5597static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005598posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005599{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005600#ifdef UNION_WAIT
5601 union wait status;
5602#define status_i (status.w_status)
5603#else
5604 int status;
5605#define status_i status
5606#endif
5607 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005608
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005609 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005610 {
5611 return NULL;
5612 }
Tim Peters5aa91602002-01-30 05:46:57 +00005613
Fred Drake106c1a02002-04-23 15:58:02 +00005614 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005615#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005616}
5617#endif /* WIFEXITED */
5618
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005619#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005621"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005622Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005623
5624static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005625posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005626{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005627#ifdef UNION_WAIT
5628 union wait status;
5629#define status_i (status.w_status)
5630#else
5631 int status;
5632#define status_i status
5633#endif
5634 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005635
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005636 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005637 {
5638 return NULL;
5639 }
Tim Peters5aa91602002-01-30 05:46:57 +00005640
Guido van Rossumc9641791998-08-04 15:26:23 +00005641 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005642#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005643}
5644#endif /* WEXITSTATUS */
5645
5646#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005647PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005648"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005649Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005650value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005651
5652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005653posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005654{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005655#ifdef UNION_WAIT
5656 union wait status;
5657#define status_i (status.w_status)
5658#else
5659 int status;
5660#define status_i status
5661#endif
5662 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005663
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005664 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005665 {
5666 return NULL;
5667 }
Tim Peters5aa91602002-01-30 05:46:57 +00005668
Guido van Rossumc9641791998-08-04 15:26:23 +00005669 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005670#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005671}
5672#endif /* WTERMSIG */
5673
5674#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005676"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005677Return the signal that stopped the process that provided\n\
5678the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005679
5680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005681posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005682{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005683#ifdef UNION_WAIT
5684 union wait status;
5685#define status_i (status.w_status)
5686#else
5687 int status;
5688#define status_i status
5689#endif
5690 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005691
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005692 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005693 {
5694 return NULL;
5695 }
Tim Peters5aa91602002-01-30 05:46:57 +00005696
Guido van Rossumc9641791998-08-04 15:26:23 +00005697 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005698#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005699}
5700#endif /* WSTOPSIG */
5701
5702#endif /* HAVE_SYS_WAIT_H */
5703
5704
Guido van Rossum94f6f721999-01-06 18:42:14 +00005705#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005706#ifdef _SCO_DS
5707/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5708 needed definitions in sys/statvfs.h */
5709#define _SVID3
5710#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005711#include <sys/statvfs.h>
5712
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005713static PyObject*
5714_pystatvfs_fromstructstatvfs(struct statvfs st) {
5715 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5716 if (v == NULL)
5717 return NULL;
5718
5719#if !defined(HAVE_LARGEFILE_SUPPORT)
5720 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5721 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5722 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5723 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5724 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5725 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5726 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5727 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5728 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5729 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5730#else
5731 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5732 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005733 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005734 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005735 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005736 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005737 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005738 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005739 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005740 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005741 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005742 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005743 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005744 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005745 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5746 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5747#endif
5748
5749 return v;
5750}
5751
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005752PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005753"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005755
5756static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005757posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005758{
5759 int fd, res;
5760 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005761
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005762 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005763 return NULL;
5764 Py_BEGIN_ALLOW_THREADS
5765 res = fstatvfs(fd, &st);
5766 Py_END_ALLOW_THREADS
5767 if (res != 0)
5768 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005769
5770 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005771}
5772#endif /* HAVE_FSTATVFS */
5773
5774
5775#if defined(HAVE_STATVFS)
5776#include <sys/statvfs.h>
5777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005778PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005779"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005780Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005781
5782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005783posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005784{
5785 char *path;
5786 int res;
5787 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005788 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005789 return NULL;
5790 Py_BEGIN_ALLOW_THREADS
5791 res = statvfs(path, &st);
5792 Py_END_ALLOW_THREADS
5793 if (res != 0)
5794 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005795
5796 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005797}
5798#endif /* HAVE_STATVFS */
5799
5800
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005801#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005802PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005803"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005804Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005805The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005806or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005807
5808static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005809posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005810{
5811 PyObject *result = NULL;
5812 char *dir = NULL;
5813 char *pfx = NULL;
5814 char *name;
5815
5816 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5817 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005818
5819 if (PyErr_Warn(PyExc_RuntimeWarning,
5820 "tempnam is a potential security risk to your program") < 0)
5821 return NULL;
5822
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005823#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005824 name = _tempnam(dir, pfx);
5825#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005826 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005827#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005828 if (name == NULL)
5829 return PyErr_NoMemory();
5830 result = PyString_FromString(name);
5831 free(name);
5832 return result;
5833}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005834#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005835
5836
5837#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005838PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005839"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005840Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005841
5842static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005843posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005844{
5845 FILE *fp;
5846
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005847 fp = tmpfile();
5848 if (fp == NULL)
5849 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005850 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005851}
5852#endif
5853
5854
5855#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005856PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005857"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005858Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005859
5860static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005861posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005862{
5863 char buffer[L_tmpnam];
5864 char *name;
5865
Skip Montanaro95618b52001-08-18 18:52:10 +00005866 if (PyErr_Warn(PyExc_RuntimeWarning,
5867 "tmpnam is a potential security risk to your program") < 0)
5868 return NULL;
5869
Greg Wardb48bc172000-03-01 21:51:56 +00005870#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005871 name = tmpnam_r(buffer);
5872#else
5873 name = tmpnam(buffer);
5874#endif
5875 if (name == NULL) {
5876 PyErr_SetObject(PyExc_OSError,
5877 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005878#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005879 "unexpected NULL from tmpnam_r"
5880#else
5881 "unexpected NULL from tmpnam"
5882#endif
5883 ));
5884 return NULL;
5885 }
5886 return PyString_FromString(buffer);
5887}
5888#endif
5889
5890
Fred Drakec9680921999-12-13 16:37:25 +00005891/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5892 * It maps strings representing configuration variable names to
5893 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005894 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005895 * rarely-used constants. There are three separate tables that use
5896 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005897 *
5898 * This code is always included, even if none of the interfaces that
5899 * need it are included. The #if hackery needed to avoid it would be
5900 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005901 */
5902struct constdef {
5903 char *name;
5904 long value;
5905};
5906
Fred Drake12c6e2d1999-12-14 21:25:03 +00005907static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005908conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5909 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005910{
5911 if (PyInt_Check(arg)) {
5912 *valuep = PyInt_AS_LONG(arg);
5913 return 1;
5914 }
5915 if (PyString_Check(arg)) {
5916 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005917 size_t lo = 0;
5918 size_t mid;
5919 size_t hi = tablesize;
5920 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005921 char *confname = PyString_AS_STRING(arg);
5922 while (lo < hi) {
5923 mid = (lo + hi) / 2;
5924 cmp = strcmp(confname, table[mid].name);
5925 if (cmp < 0)
5926 hi = mid;
5927 else if (cmp > 0)
5928 lo = mid + 1;
5929 else {
5930 *valuep = table[mid].value;
5931 return 1;
5932 }
5933 }
5934 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5935 }
5936 else
5937 PyErr_SetString(PyExc_TypeError,
5938 "configuration names must be strings or integers");
5939 return 0;
5940}
5941
5942
5943#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5944static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005945#ifdef _PC_ABI_AIO_XFER_MAX
5946 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5947#endif
5948#ifdef _PC_ABI_ASYNC_IO
5949 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5950#endif
Fred Drakec9680921999-12-13 16:37:25 +00005951#ifdef _PC_ASYNC_IO
5952 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5953#endif
5954#ifdef _PC_CHOWN_RESTRICTED
5955 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5956#endif
5957#ifdef _PC_FILESIZEBITS
5958 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5959#endif
5960#ifdef _PC_LAST
5961 {"PC_LAST", _PC_LAST},
5962#endif
5963#ifdef _PC_LINK_MAX
5964 {"PC_LINK_MAX", _PC_LINK_MAX},
5965#endif
5966#ifdef _PC_MAX_CANON
5967 {"PC_MAX_CANON", _PC_MAX_CANON},
5968#endif
5969#ifdef _PC_MAX_INPUT
5970 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5971#endif
5972#ifdef _PC_NAME_MAX
5973 {"PC_NAME_MAX", _PC_NAME_MAX},
5974#endif
5975#ifdef _PC_NO_TRUNC
5976 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5977#endif
5978#ifdef _PC_PATH_MAX
5979 {"PC_PATH_MAX", _PC_PATH_MAX},
5980#endif
5981#ifdef _PC_PIPE_BUF
5982 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5983#endif
5984#ifdef _PC_PRIO_IO
5985 {"PC_PRIO_IO", _PC_PRIO_IO},
5986#endif
5987#ifdef _PC_SOCK_MAXBUF
5988 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5989#endif
5990#ifdef _PC_SYNC_IO
5991 {"PC_SYNC_IO", _PC_SYNC_IO},
5992#endif
5993#ifdef _PC_VDISABLE
5994 {"PC_VDISABLE", _PC_VDISABLE},
5995#endif
5996};
5997
Fred Drakec9680921999-12-13 16:37:25 +00005998static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005999conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006000{
6001 return conv_confname(arg, valuep, posix_constants_pathconf,
6002 sizeof(posix_constants_pathconf)
6003 / sizeof(struct constdef));
6004}
6005#endif
6006
6007#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006008PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006009"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006010Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006011If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006012
6013static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006014posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006015{
6016 PyObject *result = NULL;
6017 int name, fd;
6018
Fred Drake12c6e2d1999-12-14 21:25:03 +00006019 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6020 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006021 long limit;
6022
6023 errno = 0;
6024 limit = fpathconf(fd, name);
6025 if (limit == -1 && errno != 0)
6026 posix_error();
6027 else
6028 result = PyInt_FromLong(limit);
6029 }
6030 return result;
6031}
6032#endif
6033
6034
6035#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006036PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006037"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006038Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006039If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006040
6041static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006042posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006043{
6044 PyObject *result = NULL;
6045 int name;
6046 char *path;
6047
6048 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6049 conv_path_confname, &name)) {
6050 long limit;
6051
6052 errno = 0;
6053 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006054 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006055 if (errno == EINVAL)
6056 /* could be a path or name problem */
6057 posix_error();
6058 else
6059 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006060 }
Fred Drakec9680921999-12-13 16:37:25 +00006061 else
6062 result = PyInt_FromLong(limit);
6063 }
6064 return result;
6065}
6066#endif
6067
6068#ifdef HAVE_CONFSTR
6069static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006070#ifdef _CS_ARCHITECTURE
6071 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6072#endif
6073#ifdef _CS_HOSTNAME
6074 {"CS_HOSTNAME", _CS_HOSTNAME},
6075#endif
6076#ifdef _CS_HW_PROVIDER
6077 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6078#endif
6079#ifdef _CS_HW_SERIAL
6080 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6081#endif
6082#ifdef _CS_INITTAB_NAME
6083 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6084#endif
Fred Drakec9680921999-12-13 16:37:25 +00006085#ifdef _CS_LFS64_CFLAGS
6086 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6087#endif
6088#ifdef _CS_LFS64_LDFLAGS
6089 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6090#endif
6091#ifdef _CS_LFS64_LIBS
6092 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6093#endif
6094#ifdef _CS_LFS64_LINTFLAGS
6095 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6096#endif
6097#ifdef _CS_LFS_CFLAGS
6098 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6099#endif
6100#ifdef _CS_LFS_LDFLAGS
6101 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6102#endif
6103#ifdef _CS_LFS_LIBS
6104 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6105#endif
6106#ifdef _CS_LFS_LINTFLAGS
6107 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6108#endif
Fred Draked86ed291999-12-15 15:34:33 +00006109#ifdef _CS_MACHINE
6110 {"CS_MACHINE", _CS_MACHINE},
6111#endif
Fred Drakec9680921999-12-13 16:37:25 +00006112#ifdef _CS_PATH
6113 {"CS_PATH", _CS_PATH},
6114#endif
Fred Draked86ed291999-12-15 15:34:33 +00006115#ifdef _CS_RELEASE
6116 {"CS_RELEASE", _CS_RELEASE},
6117#endif
6118#ifdef _CS_SRPC_DOMAIN
6119 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6120#endif
6121#ifdef _CS_SYSNAME
6122 {"CS_SYSNAME", _CS_SYSNAME},
6123#endif
6124#ifdef _CS_VERSION
6125 {"CS_VERSION", _CS_VERSION},
6126#endif
Fred Drakec9680921999-12-13 16:37:25 +00006127#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6128 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6129#endif
6130#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6131 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6132#endif
6133#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6134 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6135#endif
6136#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6137 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6138#endif
6139#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6140 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6141#endif
6142#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6143 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6144#endif
6145#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6146 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6147#endif
6148#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6149 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6150#endif
6151#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6152 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6153#endif
6154#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6155 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6156#endif
6157#ifdef _CS_XBS5_LP64_OFF64_LIBS
6158 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6159#endif
6160#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6161 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6162#endif
6163#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6164 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6165#endif
6166#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6167 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6168#endif
6169#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6170 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6171#endif
6172#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6173 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6174#endif
Fred Draked86ed291999-12-15 15:34:33 +00006175#ifdef _MIPS_CS_AVAIL_PROCESSORS
6176 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6177#endif
6178#ifdef _MIPS_CS_BASE
6179 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6180#endif
6181#ifdef _MIPS_CS_HOSTID
6182 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6183#endif
6184#ifdef _MIPS_CS_HW_NAME
6185 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6186#endif
6187#ifdef _MIPS_CS_NUM_PROCESSORS
6188 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6189#endif
6190#ifdef _MIPS_CS_OSREL_MAJ
6191 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6192#endif
6193#ifdef _MIPS_CS_OSREL_MIN
6194 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6195#endif
6196#ifdef _MIPS_CS_OSREL_PATCH
6197 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6198#endif
6199#ifdef _MIPS_CS_OS_NAME
6200 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6201#endif
6202#ifdef _MIPS_CS_OS_PROVIDER
6203 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6204#endif
6205#ifdef _MIPS_CS_PROCESSORS
6206 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6207#endif
6208#ifdef _MIPS_CS_SERIAL
6209 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6210#endif
6211#ifdef _MIPS_CS_VENDOR
6212 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6213#endif
Fred Drakec9680921999-12-13 16:37:25 +00006214};
6215
6216static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006217conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006218{
6219 return conv_confname(arg, valuep, posix_constants_confstr,
6220 sizeof(posix_constants_confstr)
6221 / sizeof(struct constdef));
6222}
6223
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006224PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006225"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006226Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006227
6228static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006229posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006230{
6231 PyObject *result = NULL;
6232 int name;
6233 char buffer[64];
6234
6235 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6236 int len = confstr(name, buffer, sizeof(buffer));
6237
Fred Drakec9680921999-12-13 16:37:25 +00006238 errno = 0;
6239 if (len == 0) {
6240 if (errno != 0)
6241 posix_error();
6242 else
6243 result = PyString_FromString("");
6244 }
6245 else {
6246 if (len >= sizeof(buffer)) {
6247 result = PyString_FromStringAndSize(NULL, len);
6248 if (result != NULL)
6249 confstr(name, PyString_AS_STRING(result), len+1);
6250 }
6251 else
6252 result = PyString_FromString(buffer);
6253 }
6254 }
6255 return result;
6256}
6257#endif
6258
6259
6260#ifdef HAVE_SYSCONF
6261static struct constdef posix_constants_sysconf[] = {
6262#ifdef _SC_2_CHAR_TERM
6263 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6264#endif
6265#ifdef _SC_2_C_BIND
6266 {"SC_2_C_BIND", _SC_2_C_BIND},
6267#endif
6268#ifdef _SC_2_C_DEV
6269 {"SC_2_C_DEV", _SC_2_C_DEV},
6270#endif
6271#ifdef _SC_2_C_VERSION
6272 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6273#endif
6274#ifdef _SC_2_FORT_DEV
6275 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6276#endif
6277#ifdef _SC_2_FORT_RUN
6278 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6279#endif
6280#ifdef _SC_2_LOCALEDEF
6281 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6282#endif
6283#ifdef _SC_2_SW_DEV
6284 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6285#endif
6286#ifdef _SC_2_UPE
6287 {"SC_2_UPE", _SC_2_UPE},
6288#endif
6289#ifdef _SC_2_VERSION
6290 {"SC_2_VERSION", _SC_2_VERSION},
6291#endif
Fred Draked86ed291999-12-15 15:34:33 +00006292#ifdef _SC_ABI_ASYNCHRONOUS_IO
6293 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6294#endif
6295#ifdef _SC_ACL
6296 {"SC_ACL", _SC_ACL},
6297#endif
Fred Drakec9680921999-12-13 16:37:25 +00006298#ifdef _SC_AIO_LISTIO_MAX
6299 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6300#endif
Fred Drakec9680921999-12-13 16:37:25 +00006301#ifdef _SC_AIO_MAX
6302 {"SC_AIO_MAX", _SC_AIO_MAX},
6303#endif
6304#ifdef _SC_AIO_PRIO_DELTA_MAX
6305 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6306#endif
6307#ifdef _SC_ARG_MAX
6308 {"SC_ARG_MAX", _SC_ARG_MAX},
6309#endif
6310#ifdef _SC_ASYNCHRONOUS_IO
6311 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6312#endif
6313#ifdef _SC_ATEXIT_MAX
6314 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6315#endif
Fred Draked86ed291999-12-15 15:34:33 +00006316#ifdef _SC_AUDIT
6317 {"SC_AUDIT", _SC_AUDIT},
6318#endif
Fred Drakec9680921999-12-13 16:37:25 +00006319#ifdef _SC_AVPHYS_PAGES
6320 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6321#endif
6322#ifdef _SC_BC_BASE_MAX
6323 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6324#endif
6325#ifdef _SC_BC_DIM_MAX
6326 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6327#endif
6328#ifdef _SC_BC_SCALE_MAX
6329 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6330#endif
6331#ifdef _SC_BC_STRING_MAX
6332 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6333#endif
Fred Draked86ed291999-12-15 15:34:33 +00006334#ifdef _SC_CAP
6335 {"SC_CAP", _SC_CAP},
6336#endif
Fred Drakec9680921999-12-13 16:37:25 +00006337#ifdef _SC_CHARCLASS_NAME_MAX
6338 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6339#endif
6340#ifdef _SC_CHAR_BIT
6341 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6342#endif
6343#ifdef _SC_CHAR_MAX
6344 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6345#endif
6346#ifdef _SC_CHAR_MIN
6347 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6348#endif
6349#ifdef _SC_CHILD_MAX
6350 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6351#endif
6352#ifdef _SC_CLK_TCK
6353 {"SC_CLK_TCK", _SC_CLK_TCK},
6354#endif
6355#ifdef _SC_COHER_BLKSZ
6356 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6357#endif
6358#ifdef _SC_COLL_WEIGHTS_MAX
6359 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6360#endif
6361#ifdef _SC_DCACHE_ASSOC
6362 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6363#endif
6364#ifdef _SC_DCACHE_BLKSZ
6365 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6366#endif
6367#ifdef _SC_DCACHE_LINESZ
6368 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6369#endif
6370#ifdef _SC_DCACHE_SZ
6371 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6372#endif
6373#ifdef _SC_DCACHE_TBLKSZ
6374 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6375#endif
6376#ifdef _SC_DELAYTIMER_MAX
6377 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6378#endif
6379#ifdef _SC_EQUIV_CLASS_MAX
6380 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6381#endif
6382#ifdef _SC_EXPR_NEST_MAX
6383 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6384#endif
6385#ifdef _SC_FSYNC
6386 {"SC_FSYNC", _SC_FSYNC},
6387#endif
6388#ifdef _SC_GETGR_R_SIZE_MAX
6389 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6390#endif
6391#ifdef _SC_GETPW_R_SIZE_MAX
6392 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6393#endif
6394#ifdef _SC_ICACHE_ASSOC
6395 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6396#endif
6397#ifdef _SC_ICACHE_BLKSZ
6398 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6399#endif
6400#ifdef _SC_ICACHE_LINESZ
6401 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6402#endif
6403#ifdef _SC_ICACHE_SZ
6404 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6405#endif
Fred Draked86ed291999-12-15 15:34:33 +00006406#ifdef _SC_INF
6407 {"SC_INF", _SC_INF},
6408#endif
Fred Drakec9680921999-12-13 16:37:25 +00006409#ifdef _SC_INT_MAX
6410 {"SC_INT_MAX", _SC_INT_MAX},
6411#endif
6412#ifdef _SC_INT_MIN
6413 {"SC_INT_MIN", _SC_INT_MIN},
6414#endif
6415#ifdef _SC_IOV_MAX
6416 {"SC_IOV_MAX", _SC_IOV_MAX},
6417#endif
Fred Draked86ed291999-12-15 15:34:33 +00006418#ifdef _SC_IP_SECOPTS
6419 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6420#endif
Fred Drakec9680921999-12-13 16:37:25 +00006421#ifdef _SC_JOB_CONTROL
6422 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6423#endif
Fred Draked86ed291999-12-15 15:34:33 +00006424#ifdef _SC_KERN_POINTERS
6425 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6426#endif
6427#ifdef _SC_KERN_SIM
6428 {"SC_KERN_SIM", _SC_KERN_SIM},
6429#endif
Fred Drakec9680921999-12-13 16:37:25 +00006430#ifdef _SC_LINE_MAX
6431 {"SC_LINE_MAX", _SC_LINE_MAX},
6432#endif
6433#ifdef _SC_LOGIN_NAME_MAX
6434 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6435#endif
6436#ifdef _SC_LOGNAME_MAX
6437 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6438#endif
6439#ifdef _SC_LONG_BIT
6440 {"SC_LONG_BIT", _SC_LONG_BIT},
6441#endif
Fred Draked86ed291999-12-15 15:34:33 +00006442#ifdef _SC_MAC
6443 {"SC_MAC", _SC_MAC},
6444#endif
Fred Drakec9680921999-12-13 16:37:25 +00006445#ifdef _SC_MAPPED_FILES
6446 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6447#endif
6448#ifdef _SC_MAXPID
6449 {"SC_MAXPID", _SC_MAXPID},
6450#endif
6451#ifdef _SC_MB_LEN_MAX
6452 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6453#endif
6454#ifdef _SC_MEMLOCK
6455 {"SC_MEMLOCK", _SC_MEMLOCK},
6456#endif
6457#ifdef _SC_MEMLOCK_RANGE
6458 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6459#endif
6460#ifdef _SC_MEMORY_PROTECTION
6461 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6462#endif
6463#ifdef _SC_MESSAGE_PASSING
6464 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6465#endif
Fred Draked86ed291999-12-15 15:34:33 +00006466#ifdef _SC_MMAP_FIXED_ALIGNMENT
6467 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6468#endif
Fred Drakec9680921999-12-13 16:37:25 +00006469#ifdef _SC_MQ_OPEN_MAX
6470 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6471#endif
6472#ifdef _SC_MQ_PRIO_MAX
6473 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6474#endif
Fred Draked86ed291999-12-15 15:34:33 +00006475#ifdef _SC_NACLS_MAX
6476 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6477#endif
Fred Drakec9680921999-12-13 16:37:25 +00006478#ifdef _SC_NGROUPS_MAX
6479 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6480#endif
6481#ifdef _SC_NL_ARGMAX
6482 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6483#endif
6484#ifdef _SC_NL_LANGMAX
6485 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6486#endif
6487#ifdef _SC_NL_MSGMAX
6488 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6489#endif
6490#ifdef _SC_NL_NMAX
6491 {"SC_NL_NMAX", _SC_NL_NMAX},
6492#endif
6493#ifdef _SC_NL_SETMAX
6494 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6495#endif
6496#ifdef _SC_NL_TEXTMAX
6497 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6498#endif
6499#ifdef _SC_NPROCESSORS_CONF
6500 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6501#endif
6502#ifdef _SC_NPROCESSORS_ONLN
6503 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6504#endif
Fred Draked86ed291999-12-15 15:34:33 +00006505#ifdef _SC_NPROC_CONF
6506 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6507#endif
6508#ifdef _SC_NPROC_ONLN
6509 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6510#endif
Fred Drakec9680921999-12-13 16:37:25 +00006511#ifdef _SC_NZERO
6512 {"SC_NZERO", _SC_NZERO},
6513#endif
6514#ifdef _SC_OPEN_MAX
6515 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6516#endif
6517#ifdef _SC_PAGESIZE
6518 {"SC_PAGESIZE", _SC_PAGESIZE},
6519#endif
6520#ifdef _SC_PAGE_SIZE
6521 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6522#endif
6523#ifdef _SC_PASS_MAX
6524 {"SC_PASS_MAX", _SC_PASS_MAX},
6525#endif
6526#ifdef _SC_PHYS_PAGES
6527 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6528#endif
6529#ifdef _SC_PII
6530 {"SC_PII", _SC_PII},
6531#endif
6532#ifdef _SC_PII_INTERNET
6533 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6534#endif
6535#ifdef _SC_PII_INTERNET_DGRAM
6536 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6537#endif
6538#ifdef _SC_PII_INTERNET_STREAM
6539 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6540#endif
6541#ifdef _SC_PII_OSI
6542 {"SC_PII_OSI", _SC_PII_OSI},
6543#endif
6544#ifdef _SC_PII_OSI_CLTS
6545 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6546#endif
6547#ifdef _SC_PII_OSI_COTS
6548 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6549#endif
6550#ifdef _SC_PII_OSI_M
6551 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6552#endif
6553#ifdef _SC_PII_SOCKET
6554 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6555#endif
6556#ifdef _SC_PII_XTI
6557 {"SC_PII_XTI", _SC_PII_XTI},
6558#endif
6559#ifdef _SC_POLL
6560 {"SC_POLL", _SC_POLL},
6561#endif
6562#ifdef _SC_PRIORITIZED_IO
6563 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6564#endif
6565#ifdef _SC_PRIORITY_SCHEDULING
6566 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6567#endif
6568#ifdef _SC_REALTIME_SIGNALS
6569 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6570#endif
6571#ifdef _SC_RE_DUP_MAX
6572 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6573#endif
6574#ifdef _SC_RTSIG_MAX
6575 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6576#endif
6577#ifdef _SC_SAVED_IDS
6578 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6579#endif
6580#ifdef _SC_SCHAR_MAX
6581 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6582#endif
6583#ifdef _SC_SCHAR_MIN
6584 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6585#endif
6586#ifdef _SC_SELECT
6587 {"SC_SELECT", _SC_SELECT},
6588#endif
6589#ifdef _SC_SEMAPHORES
6590 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6591#endif
6592#ifdef _SC_SEM_NSEMS_MAX
6593 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6594#endif
6595#ifdef _SC_SEM_VALUE_MAX
6596 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6597#endif
6598#ifdef _SC_SHARED_MEMORY_OBJECTS
6599 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6600#endif
6601#ifdef _SC_SHRT_MAX
6602 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6603#endif
6604#ifdef _SC_SHRT_MIN
6605 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6606#endif
6607#ifdef _SC_SIGQUEUE_MAX
6608 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6609#endif
6610#ifdef _SC_SIGRT_MAX
6611 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6612#endif
6613#ifdef _SC_SIGRT_MIN
6614 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6615#endif
Fred Draked86ed291999-12-15 15:34:33 +00006616#ifdef _SC_SOFTPOWER
6617 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6618#endif
Fred Drakec9680921999-12-13 16:37:25 +00006619#ifdef _SC_SPLIT_CACHE
6620 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6621#endif
6622#ifdef _SC_SSIZE_MAX
6623 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6624#endif
6625#ifdef _SC_STACK_PROT
6626 {"SC_STACK_PROT", _SC_STACK_PROT},
6627#endif
6628#ifdef _SC_STREAM_MAX
6629 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6630#endif
6631#ifdef _SC_SYNCHRONIZED_IO
6632 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6633#endif
6634#ifdef _SC_THREADS
6635 {"SC_THREADS", _SC_THREADS},
6636#endif
6637#ifdef _SC_THREAD_ATTR_STACKADDR
6638 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6639#endif
6640#ifdef _SC_THREAD_ATTR_STACKSIZE
6641 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6642#endif
6643#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6644 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6645#endif
6646#ifdef _SC_THREAD_KEYS_MAX
6647 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6648#endif
6649#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6650 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6651#endif
6652#ifdef _SC_THREAD_PRIO_INHERIT
6653 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6654#endif
6655#ifdef _SC_THREAD_PRIO_PROTECT
6656 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6657#endif
6658#ifdef _SC_THREAD_PROCESS_SHARED
6659 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6660#endif
6661#ifdef _SC_THREAD_SAFE_FUNCTIONS
6662 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6663#endif
6664#ifdef _SC_THREAD_STACK_MIN
6665 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6666#endif
6667#ifdef _SC_THREAD_THREADS_MAX
6668 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6669#endif
6670#ifdef _SC_TIMERS
6671 {"SC_TIMERS", _SC_TIMERS},
6672#endif
6673#ifdef _SC_TIMER_MAX
6674 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6675#endif
6676#ifdef _SC_TTY_NAME_MAX
6677 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6678#endif
6679#ifdef _SC_TZNAME_MAX
6680 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6681#endif
6682#ifdef _SC_T_IOV_MAX
6683 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6684#endif
6685#ifdef _SC_UCHAR_MAX
6686 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6687#endif
6688#ifdef _SC_UINT_MAX
6689 {"SC_UINT_MAX", _SC_UINT_MAX},
6690#endif
6691#ifdef _SC_UIO_MAXIOV
6692 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6693#endif
6694#ifdef _SC_ULONG_MAX
6695 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6696#endif
6697#ifdef _SC_USHRT_MAX
6698 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6699#endif
6700#ifdef _SC_VERSION
6701 {"SC_VERSION", _SC_VERSION},
6702#endif
6703#ifdef _SC_WORD_BIT
6704 {"SC_WORD_BIT", _SC_WORD_BIT},
6705#endif
6706#ifdef _SC_XBS5_ILP32_OFF32
6707 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6708#endif
6709#ifdef _SC_XBS5_ILP32_OFFBIG
6710 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6711#endif
6712#ifdef _SC_XBS5_LP64_OFF64
6713 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6714#endif
6715#ifdef _SC_XBS5_LPBIG_OFFBIG
6716 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6717#endif
6718#ifdef _SC_XOPEN_CRYPT
6719 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6720#endif
6721#ifdef _SC_XOPEN_ENH_I18N
6722 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6723#endif
6724#ifdef _SC_XOPEN_LEGACY
6725 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6726#endif
6727#ifdef _SC_XOPEN_REALTIME
6728 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6729#endif
6730#ifdef _SC_XOPEN_REALTIME_THREADS
6731 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6732#endif
6733#ifdef _SC_XOPEN_SHM
6734 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6735#endif
6736#ifdef _SC_XOPEN_UNIX
6737 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6738#endif
6739#ifdef _SC_XOPEN_VERSION
6740 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6741#endif
6742#ifdef _SC_XOPEN_XCU_VERSION
6743 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6744#endif
6745#ifdef _SC_XOPEN_XPG2
6746 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6747#endif
6748#ifdef _SC_XOPEN_XPG3
6749 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6750#endif
6751#ifdef _SC_XOPEN_XPG4
6752 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6753#endif
6754};
6755
6756static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006757conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006758{
6759 return conv_confname(arg, valuep, posix_constants_sysconf,
6760 sizeof(posix_constants_sysconf)
6761 / sizeof(struct constdef));
6762}
6763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006764PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006765"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006766Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006767
6768static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006769posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006770{
6771 PyObject *result = NULL;
6772 int name;
6773
6774 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6775 int value;
6776
6777 errno = 0;
6778 value = sysconf(name);
6779 if (value == -1 && errno != 0)
6780 posix_error();
6781 else
6782 result = PyInt_FromLong(value);
6783 }
6784 return result;
6785}
6786#endif
6787
6788
Fred Drakebec628d1999-12-15 18:31:10 +00006789/* This code is used to ensure that the tables of configuration value names
6790 * are in sorted order as required by conv_confname(), and also to build the
6791 * the exported dictionaries that are used to publish information about the
6792 * names available on the host platform.
6793 *
6794 * Sorting the table at runtime ensures that the table is properly ordered
6795 * when used, even for platforms we're not able to test on. It also makes
6796 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006797 */
Fred Drakebec628d1999-12-15 18:31:10 +00006798
6799static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006800cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006801{
6802 const struct constdef *c1 =
6803 (const struct constdef *) v1;
6804 const struct constdef *c2 =
6805 (const struct constdef *) v2;
6806
6807 return strcmp(c1->name, c2->name);
6808}
6809
6810static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006811setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006812 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006813{
Fred Drakebec628d1999-12-15 18:31:10 +00006814 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006815 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006816
6817 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6818 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006819 if (d == NULL)
6820 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006821
Barry Warsaw3155db32000-04-13 15:20:40 +00006822 for (i=0; i < tablesize; ++i) {
6823 PyObject *o = PyInt_FromLong(table[i].value);
6824 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6825 Py_XDECREF(o);
6826 Py_DECREF(d);
6827 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006828 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006829 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006830 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006831 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006832}
6833
Fred Drakebec628d1999-12-15 18:31:10 +00006834/* Return -1 on failure, 0 on success. */
6835static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006836setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006837{
6838#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006839 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006840 sizeof(posix_constants_pathconf)
6841 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006842 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006843 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006844#endif
6845#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006846 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006847 sizeof(posix_constants_confstr)
6848 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006849 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006850 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006851#endif
6852#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006853 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006854 sizeof(posix_constants_sysconf)
6855 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006856 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006857 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006858#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006859 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006860}
Fred Draked86ed291999-12-15 15:34:33 +00006861
6862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006863PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006864"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006865Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006866in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006867
6868static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006869posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006870{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006871 abort();
6872 /*NOTREACHED*/
6873 Py_FatalError("abort() called from Python code didn't abort!");
6874 return NULL;
6875}
Fred Drakebec628d1999-12-15 18:31:10 +00006876
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006877#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006878PyDoc_STRVAR(win32_startfile__doc__,
6879"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006880\n\
6881This acts like double-clicking the file in Explorer, or giving the file\n\
6882name as an argument to the DOS \"start\" command: the file is opened\n\
6883with whatever application (if any) its extension is associated.\n\
6884\n\
6885startfile returns as soon as the associated application is launched.\n\
6886There is no option to wait for the application to close, and no way\n\
6887to retrieve the application's exit status.\n\
6888\n\
6889The filepath is relative to the current directory. If you want to use\n\
6890an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006891the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006892
6893static PyObject *
6894win32_startfile(PyObject *self, PyObject *args)
6895{
6896 char *filepath;
6897 HINSTANCE rc;
6898 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
6899 return NULL;
6900 Py_BEGIN_ALLOW_THREADS
6901 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
6902 Py_END_ALLOW_THREADS
6903 if (rc <= (HINSTANCE)32)
6904 return win32_error("startfile", filepath);
6905 Py_INCREF(Py_None);
6906 return Py_None;
6907}
6908#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006909
Martin v. Löwis438b5342002-12-27 10:16:42 +00006910#ifdef HAVE_GETLOADAVG
6911PyDoc_STRVAR(posix_getloadavg__doc__,
6912"getloadavg() -> (float, float, float)\n\n\
6913Return the number of processes in the system run queue averaged over\n\
6914the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6915was unobtainable");
6916
6917static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006918posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006919{
6920 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006921 if (getloadavg(loadavg, 3)!=3) {
6922 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6923 return NULL;
6924 } else
6925 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6926}
6927#endif
6928
6929
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006930static PyMethodDef posix_methods[] = {
6931 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6932#ifdef HAVE_TTYNAME
6933 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6934#endif
6935 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
6936 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006937#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006938 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006939#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006940#ifdef HAVE_LCHOWN
6941 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6942#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006943#ifdef HAVE_CHROOT
6944 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6945#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006946#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006947 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006948#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006949#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006950 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00006951#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00006952 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006953#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00006954#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006955#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006956 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006957#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006958 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6959 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6960 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006961#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006962 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006963#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006964#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006965 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006966#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006967 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6968 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6969 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006970 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006971#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006972 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006973#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006974#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006975 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006976#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006977 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006978#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006979 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006980#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006981 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6982 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6983 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006984#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006985 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006986#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006987 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006988#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006989 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6990 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006991#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006992#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006993 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6994 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00006995#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006996#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006997 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006998#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006999#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00007000 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007001#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007002#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007003 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007004#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007005#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007006 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007007#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007008#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007009 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007010#endif /* HAVE_GETEGID */
7011#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007012 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007013#endif /* HAVE_GETEUID */
7014#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007015 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007016#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007017#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007018 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007020 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007021#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007022 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007023#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007024#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007025 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007026#endif /* HAVE_GETPPID */
7027#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007028 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007029#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007030#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007031 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007032#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007033#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007034 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007035#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007036#ifdef HAVE_KILLPG
7037 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7038#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007039#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007040 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007041#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007042#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007043 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007044#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007045 {"popen2", win32_popen2, METH_VARARGS},
7046 {"popen3", win32_popen3, METH_VARARGS},
7047 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007048 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007049#else
7050#if defined(PYOS_OS2) && defined(PYCC_GCC)
7051 {"popen2", os2emx_popen2, METH_VARARGS},
7052 {"popen3", os2emx_popen3, METH_VARARGS},
7053 {"popen4", os2emx_popen4, METH_VARARGS},
7054#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007055#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007056#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007057#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007058 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007059#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007060#ifdef HAVE_SETEUID
7061 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7062#endif /* HAVE_SETEUID */
7063#ifdef HAVE_SETEGID
7064 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7065#endif /* HAVE_SETEGID */
7066#ifdef HAVE_SETREUID
7067 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7068#endif /* HAVE_SETREUID */
7069#ifdef HAVE_SETREGID
7070 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7071#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007072#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007073 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007074#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007075#ifdef HAVE_SETGROUPS
7076 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7077#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007078#ifdef HAVE_GETPGID
7079 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7080#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007081#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007082 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007083#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007084#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007085 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007086#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007087#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007088 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007089#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007090#ifdef HAVE_GETSID
7091 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7092#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007093#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007094 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007095#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007096#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007097 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007098#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007099#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007100 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007101#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007102#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007103 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007104#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007105 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7106 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7107 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7108 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7109 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7110 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7111 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7112 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7113 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007114 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007115#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007116 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007117#endif
7118#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007119 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007120#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007121#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007122 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7123#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007124#ifdef HAVE_DEVICE_MACROS
7125 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7126 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7127 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7128#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007129#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007130 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007131#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007132#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007133 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007134#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007135#ifdef HAVE_UNSETENV
7136 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7137#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007138#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007139 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007140#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007141#ifdef HAVE_FCHDIR
7142 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7143#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007144#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007145 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007146#endif
7147#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007148 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007149#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007150#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007151#ifdef WCOREDUMP
7152 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7153#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007154#ifdef WIFCONTINUED
7155 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7156#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007157#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007158 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007159#endif /* WIFSTOPPED */
7160#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007161 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007162#endif /* WIFSIGNALED */
7163#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007164 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007165#endif /* WIFEXITED */
7166#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007167 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007168#endif /* WEXITSTATUS */
7169#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007170 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007171#endif /* WTERMSIG */
7172#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007173 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007174#endif /* WSTOPSIG */
7175#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007176#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007177 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007178#endif
7179#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007180 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007181#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007182#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007183 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007184#endif
7185#ifdef HAVE_TEMPNAM
7186 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7187#endif
7188#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007189 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007190#endif
Fred Drakec9680921999-12-13 16:37:25 +00007191#ifdef HAVE_CONFSTR
7192 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7193#endif
7194#ifdef HAVE_SYSCONF
7195 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7196#endif
7197#ifdef HAVE_FPATHCONF
7198 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7199#endif
7200#ifdef HAVE_PATHCONF
7201 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7202#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007203 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007204#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007205 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7206#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007207#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007208 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007209#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007210 {NULL, NULL} /* Sentinel */
7211};
7212
7213
Barry Warsaw4a342091996-12-19 23:50:02 +00007214static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007215ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007216{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007217 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007218}
7219
Guido van Rossumd48f2521997-12-05 22:19:34 +00007220#if defined(PYOS_OS2)
7221/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007222static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007223{
7224 APIRET rc;
7225 ULONG values[QSV_MAX+1];
7226 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007227 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007228
7229 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007230 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007231 Py_END_ALLOW_THREADS
7232
7233 if (rc != NO_ERROR) {
7234 os2_error(rc);
7235 return -1;
7236 }
7237
Fred Drake4d1e64b2002-04-15 19:40:07 +00007238 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7239 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7240 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7241 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7242 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7243 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7244 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007245
7246 switch (values[QSV_VERSION_MINOR]) {
7247 case 0: ver = "2.00"; break;
7248 case 10: ver = "2.10"; break;
7249 case 11: ver = "2.11"; break;
7250 case 30: ver = "3.00"; break;
7251 case 40: ver = "4.00"; break;
7252 case 50: ver = "5.00"; break;
7253 default:
Tim Peters885d4572001-11-28 20:27:42 +00007254 PyOS_snprintf(tmp, sizeof(tmp),
7255 "%d-%d", values[QSV_VERSION_MAJOR],
7256 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007257 ver = &tmp[0];
7258 }
7259
7260 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007261 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007262 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007263
7264 /* Add Indicator of Which Drive was Used to Boot the System */
7265 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7266 tmp[1] = ':';
7267 tmp[2] = '\0';
7268
Fred Drake4d1e64b2002-04-15 19:40:07 +00007269 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007270}
7271#endif
7272
Barry Warsaw4a342091996-12-19 23:50:02 +00007273static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007274all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007275{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007276#ifdef F_OK
7277 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007278#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007279#ifdef R_OK
7280 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007281#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007282#ifdef W_OK
7283 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007284#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007285#ifdef X_OK
7286 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007287#endif
Fred Drakec9680921999-12-13 16:37:25 +00007288#ifdef NGROUPS_MAX
7289 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7290#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007291#ifdef TMP_MAX
7292 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7293#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007294#ifdef WCONTINUED
7295 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7296#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007297#ifdef WNOHANG
7298 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007299#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007300#ifdef WUNTRACED
7301 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7302#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007303#ifdef O_RDONLY
7304 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7305#endif
7306#ifdef O_WRONLY
7307 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7308#endif
7309#ifdef O_RDWR
7310 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7311#endif
7312#ifdef O_NDELAY
7313 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7314#endif
7315#ifdef O_NONBLOCK
7316 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7317#endif
7318#ifdef O_APPEND
7319 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7320#endif
7321#ifdef O_DSYNC
7322 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7323#endif
7324#ifdef O_RSYNC
7325 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7326#endif
7327#ifdef O_SYNC
7328 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7329#endif
7330#ifdef O_NOCTTY
7331 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7332#endif
7333#ifdef O_CREAT
7334 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7335#endif
7336#ifdef O_EXCL
7337 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7338#endif
7339#ifdef O_TRUNC
7340 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7341#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007342#ifdef O_BINARY
7343 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7344#endif
7345#ifdef O_TEXT
7346 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7347#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007348#ifdef O_LARGEFILE
7349 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7350#endif
7351
Tim Peters5aa91602002-01-30 05:46:57 +00007352/* MS Windows */
7353#ifdef O_NOINHERIT
7354 /* Don't inherit in child processes. */
7355 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7356#endif
7357#ifdef _O_SHORT_LIVED
7358 /* Optimize for short life (keep in memory). */
7359 /* MS forgot to define this one with a non-underscore form too. */
7360 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7361#endif
7362#ifdef O_TEMPORARY
7363 /* Automatically delete when last handle is closed. */
7364 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7365#endif
7366#ifdef O_RANDOM
7367 /* Optimize for random access. */
7368 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7369#endif
7370#ifdef O_SEQUENTIAL
7371 /* Optimize for sequential access. */
7372 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7373#endif
7374
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007375/* GNU extensions. */
7376#ifdef O_DIRECT
7377 /* Direct disk access. */
7378 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7379#endif
7380#ifdef O_DIRECTORY
7381 /* Must be a directory. */
7382 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7383#endif
7384#ifdef O_NOFOLLOW
7385 /* Do not follow links. */
7386 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7387#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007388
Barry Warsaw5676bd12003-01-07 20:57:09 +00007389 /* These come from sysexits.h */
7390#ifdef EX_OK
7391 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007392#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007393#ifdef EX_USAGE
7394 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007395#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007396#ifdef EX_DATAERR
7397 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007398#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007399#ifdef EX_NOINPUT
7400 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007401#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007402#ifdef EX_NOUSER
7403 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007404#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007405#ifdef EX_NOHOST
7406 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007407#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007408#ifdef EX_UNAVAILABLE
7409 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007410#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007411#ifdef EX_SOFTWARE
7412 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007413#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007414#ifdef EX_OSERR
7415 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007416#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007417#ifdef EX_OSFILE
7418 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007419#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007420#ifdef EX_CANTCREAT
7421 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007422#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007423#ifdef EX_IOERR
7424 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007425#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007426#ifdef EX_TEMPFAIL
7427 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007428#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007429#ifdef EX_PROTOCOL
7430 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007431#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007432#ifdef EX_NOPERM
7433 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007434#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007435#ifdef EX_CONFIG
7436 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007437#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007438#ifdef EX_NOTFOUND
7439 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007440#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007441
Guido van Rossum246bc171999-02-01 23:54:31 +00007442#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007443#if defined(PYOS_OS2) && defined(PYCC_GCC)
7444 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7445 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7446 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7447 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7448 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7449 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7450 if (ins(d, "P_PM", (long)P_PM)) return -1;
7451 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7452 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7453 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7454 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7455 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7456 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7457 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7458 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7459 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7460 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7461 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7462 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7463 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7464#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007465 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7466 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7467 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7468 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7469 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007470#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007471#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007472
Guido van Rossumd48f2521997-12-05 22:19:34 +00007473#if defined(PYOS_OS2)
7474 if (insertvalues(d)) return -1;
7475#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007476 return 0;
7477}
7478
7479
Tim Peters5aa91602002-01-30 05:46:57 +00007480#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007481#define INITFUNC initnt
7482#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007483
7484#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007485#define INITFUNC initos2
7486#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007487
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007488#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007489#define INITFUNC initposix
7490#define MODNAME "posix"
7491#endif
7492
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007493PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007494INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007495{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007496 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007497
Fred Drake4d1e64b2002-04-15 19:40:07 +00007498 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007499 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007500 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007501
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007502 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007503 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007504 Py_XINCREF(v);
7505 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007506 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007507 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007508
Fred Drake4d1e64b2002-04-15 19:40:07 +00007509 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007510 return;
7511
Fred Drake4d1e64b2002-04-15 19:40:07 +00007512 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007513 return;
7514
Fred Drake4d1e64b2002-04-15 19:40:07 +00007515 Py_INCREF(PyExc_OSError);
7516 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007517
Guido van Rossumb3d39562000-01-31 18:41:26 +00007518#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007519 if (posix_putenv_garbage == NULL)
7520 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007521#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007522
Guido van Rossum14648392001-12-08 18:02:58 +00007523 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007524 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7525 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7526 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007527 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007528 structseq_new = StatResultType.tp_new;
7529 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007530 Py_INCREF((PyObject*) &StatResultType);
7531 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007532
Guido van Rossum14648392001-12-08 18:02:58 +00007533 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007534 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007535 Py_INCREF((PyObject*) &StatVFSResultType);
7536 PyModule_AddObject(m, "statvfs_result",
7537 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007538}