blob: fa8215a468e018b1df518b32728c50bfb7d263f3 [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;
Tim Peters5aa91602002-01-30 05:46:57 +00001165 if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001166 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001167 return NULL;
1168 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001169 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001170 Py_END_ALLOW_THREADS
1171 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001172 return posix_error_with_allocated_filename(path);
1173 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001174 Py_INCREF(Py_None);
1175 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001176}
1177
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001178
Martin v. Löwis244edc82001-10-04 22:44:26 +00001179#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001180PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001181"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001182Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001183
1184static PyObject *
1185posix_chroot(PyObject *self, PyObject *args)
1186{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001187 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001188}
1189#endif
1190
Guido van Rossum21142a01999-01-08 21:05:37 +00001191#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001193"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001194force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001195
1196static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001197posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001198{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001199 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001200}
1201#endif /* HAVE_FSYNC */
1202
1203#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001204
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001205#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001206extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1207#endif
1208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001209PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001210"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001211force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001212 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001213
1214static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001215posix_fdatasync(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, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001218}
1219#endif /* HAVE_FDATASYNC */
1220
1221
Fredrik Lundh10723342000-07-10 16:38:09 +00001222#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001223PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001224"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001225Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001226
Barry Warsaw53699e91996-12-10 23:23:01 +00001227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001228posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001229{
Mark Hammondef8b6542001-05-13 08:04:26 +00001230 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001231 int uid, gid;
1232 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001233 if (!PyArg_ParseTuple(args, "etii:chown",
1234 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001235 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001236 return NULL;
1237 Py_BEGIN_ALLOW_THREADS
1238 res = chown(path, (uid_t) uid, (gid_t) gid);
1239 Py_END_ALLOW_THREADS
1240 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001241 return posix_error_with_allocated_filename(path);
1242 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001243 Py_INCREF(Py_None);
1244 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001245}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001246#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001247
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001248#ifdef HAVE_LCHOWN
1249PyDoc_STRVAR(posix_lchown__doc__,
1250"lchown(path, uid, gid)\n\n\
1251Change the owner and group id of path to the numeric uid and gid.\n\
1252This function will not follow symbolic links.");
1253
1254static PyObject *
1255posix_lchown(PyObject *self, PyObject *args)
1256{
1257 char *path = NULL;
1258 int uid, gid;
1259 int res;
1260 if (!PyArg_ParseTuple(args, "etii:lchown",
1261 Py_FileSystemDefaultEncoding, &path,
1262 &uid, &gid))
1263 return NULL;
1264 Py_BEGIN_ALLOW_THREADS
1265 res = lchown(path, (uid_t) uid, (gid_t) gid);
1266 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001267 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001268 return posix_error_with_allocated_filename(path);
1269 PyMem_Free(path);
1270 Py_INCREF(Py_None);
1271 return Py_None;
1272}
1273#endif /* HAVE_LCHOWN */
1274
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001275
Guido van Rossum36bc6801995-06-14 22:54:23 +00001276#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001277PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001278"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001279Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001280
Barry Warsaw53699e91996-12-10 23:23:01 +00001281static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001282posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001283{
1284 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001285 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001286
Barry Warsaw53699e91996-12-10 23:23:01 +00001287 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001288#if defined(PYOS_OS2) && defined(PYCC_GCC)
1289 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001290#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001291 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001292#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001293 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001294 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001295 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001296 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001298
Walter Dörwald3b918c32002-11-21 20:18:46 +00001299#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001300PyDoc_STRVAR(posix_getcwdu__doc__,
1301"getcwdu() -> path\n\n\
1302Return a unicode string representing the current working directory.");
1303
1304static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001305posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001306{
1307 char buf[1026];
1308 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001309
1310#ifdef Py_WIN_WIDE_FILENAMES
1311 if (unicode_file_names()) {
1312 wchar_t *wres;
1313 wchar_t wbuf[1026];
1314 Py_BEGIN_ALLOW_THREADS
1315 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1316 Py_END_ALLOW_THREADS
1317 if (wres == NULL)
1318 return posix_error();
1319 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1320 }
1321#endif
1322
1323 Py_BEGIN_ALLOW_THREADS
1324#if defined(PYOS_OS2) && defined(PYCC_GCC)
1325 res = _getcwd2(buf, sizeof buf);
1326#else
1327 res = getcwd(buf, sizeof buf);
1328#endif
1329 Py_END_ALLOW_THREADS
1330 if (res == NULL)
1331 return posix_error();
1332 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1333}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001334#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001335#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001336
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001337
Guido van Rossumb6775db1994-08-01 11:34:53 +00001338#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001340"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001341Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001342
Barry Warsaw53699e91996-12-10 23:23:01 +00001343static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001344posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001346 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001348#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001351PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001352"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001353Return a list containing the names of the entries in the directory.\n\
1354\n\
1355 path: path of directory to list\n\
1356\n\
1357The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001358entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001359
Barry Warsaw53699e91996-12-10 23:23:01 +00001360static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001361posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001362{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001363 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001364 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001365#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001366
Barry Warsaw53699e91996-12-10 23:23:01 +00001367 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001368 HANDLE hFindFile;
1369 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001370 /* MAX_PATH characters could mean a bigger encoded string */
1371 char namebuf[MAX_PATH*2+5];
1372 char *bufptr = namebuf;
1373 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001374
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001375#ifdef Py_WIN_WIDE_FILENAMES
1376 /* If on wide-character-capable OS see if argument
1377 is Unicode and if so use wide API. */
1378 if (unicode_file_names()) {
1379 PyUnicodeObject *po;
1380 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1381 WIN32_FIND_DATAW wFileData;
1382 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1383 Py_UNICODE wch;
1384 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1385 wnamebuf[MAX_PATH] = L'\0';
1386 len = wcslen(wnamebuf);
1387 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1388 if (wch != L'/' && wch != L'\\' && wch != L':')
1389 wnamebuf[len++] = L'/';
1390 wcscpy(wnamebuf + len, L"*.*");
1391 if ((d = PyList_New(0)) == NULL)
1392 return NULL;
1393 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1394 if (hFindFile == INVALID_HANDLE_VALUE) {
1395 errno = GetLastError();
1396 if (errno == ERROR_FILE_NOT_FOUND) {
1397 return d;
1398 }
1399 Py_DECREF(d);
1400 return win32_error_unicode("FindFirstFileW", wnamebuf);
1401 }
1402 do {
1403 if (wFileData.cFileName[0] == L'.' &&
1404 (wFileData.cFileName[1] == L'\0' ||
1405 wFileData.cFileName[1] == L'.' &&
1406 wFileData.cFileName[2] == L'\0'))
1407 continue;
1408 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1409 if (v == NULL) {
1410 Py_DECREF(d);
1411 d = NULL;
1412 break;
1413 }
1414 if (PyList_Append(d, v) != 0) {
1415 Py_DECREF(v);
1416 Py_DECREF(d);
1417 d = NULL;
1418 break;
1419 }
1420 Py_DECREF(v);
1421 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1422
1423 if (FindClose(hFindFile) == FALSE) {
1424 Py_DECREF(d);
1425 return win32_error_unicode("FindClose", wnamebuf);
1426 }
1427 return d;
1428 }
1429 /* Drop the argument parsing error as narrow strings
1430 are also valid. */
1431 PyErr_Clear();
1432 }
1433#endif
1434
Tim Peters5aa91602002-01-30 05:46:57 +00001435 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001436 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001437 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001438 if (len > 0) {
1439 char ch = namebuf[len-1];
1440 if (ch != SEP && ch != ALTSEP && ch != ':')
1441 namebuf[len++] = '/';
1442 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001443 strcpy(namebuf + len, "*.*");
1444
Barry Warsaw53699e91996-12-10 23:23:01 +00001445 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001446 return NULL;
1447
1448 hFindFile = FindFirstFile(namebuf, &FileData);
1449 if (hFindFile == INVALID_HANDLE_VALUE) {
1450 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001451 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001452 return d;
1453 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001454 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001455 }
1456 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001457 if (FileData.cFileName[0] == '.' &&
1458 (FileData.cFileName[1] == '\0' ||
1459 FileData.cFileName[1] == '.' &&
1460 FileData.cFileName[2] == '\0'))
1461 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001462 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001463 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001464 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001465 d = NULL;
1466 break;
1467 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001468 if (PyList_Append(d, v) != 0) {
1469 Py_DECREF(v);
1470 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001471 d = NULL;
1472 break;
1473 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001474 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001475 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1476
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001477 if (FindClose(hFindFile) == FALSE) {
1478 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001479 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001480 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001481
1482 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001483
Tim Peters0bb44a42000-09-15 07:44:49 +00001484#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001485
1486#ifndef MAX_PATH
1487#define MAX_PATH CCHMAXPATH
1488#endif
1489 char *name, *pt;
1490 int len;
1491 PyObject *d, *v;
1492 char namebuf[MAX_PATH+5];
1493 HDIR hdir = 1;
1494 ULONG srchcnt = 1;
1495 FILEFINDBUF3 ep;
1496 APIRET rc;
1497
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001498 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001499 return NULL;
1500 if (len >= MAX_PATH) {
1501 PyErr_SetString(PyExc_ValueError, "path too long");
1502 return NULL;
1503 }
1504 strcpy(namebuf, name);
1505 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001506 if (*pt == ALTSEP)
1507 *pt = SEP;
1508 if (namebuf[len-1] != SEP)
1509 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001510 strcpy(namebuf + len, "*.*");
1511
1512 if ((d = PyList_New(0)) == NULL)
1513 return NULL;
1514
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001515 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1516 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001517 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001518 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1519 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1520 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001521
1522 if (rc != NO_ERROR) {
1523 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001524 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001525 }
1526
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001527 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001528 do {
1529 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001530 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001531 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001532
1533 strcpy(namebuf, ep.achName);
1534
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001535 /* Leave Case of Name Alone -- In Native Form */
1536 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001537
1538 v = PyString_FromString(namebuf);
1539 if (v == NULL) {
1540 Py_DECREF(d);
1541 d = NULL;
1542 break;
1543 }
1544 if (PyList_Append(d, v) != 0) {
1545 Py_DECREF(v);
1546 Py_DECREF(d);
1547 d = NULL;
1548 break;
1549 }
1550 Py_DECREF(v);
1551 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1552 }
1553
1554 return d;
1555#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001556
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001557 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001558 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001559 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001560 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001561 int arg_is_unicode = 1;
1562
1563 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1564 arg_is_unicode = 0;
1565 PyErr_Clear();
1566 }
1567 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001568 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001569 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001570 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001571 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001572 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001573 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001574 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001575 return NULL;
1576 }
1577 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001578 if (ep->d_name[0] == '.' &&
1579 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001580 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001581 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001582 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001583 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001584 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001585 d = NULL;
1586 break;
1587 }
Just van Rossum46c97842003-02-25 21:42:15 +00001588#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001589 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001590 PyObject *w;
1591
1592 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001593 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001594 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001595 if (w != NULL) {
1596 Py_DECREF(v);
1597 v = w;
1598 }
1599 else {
1600 /* fall back to the original byte string, as
1601 discussed in patch #683592 */
1602 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001603 }
Just van Rossum46c97842003-02-25 21:42:15 +00001604 }
1605#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001606 if (PyList_Append(d, v) != 0) {
1607 Py_DECREF(v);
1608 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609 d = NULL;
1610 break;
1611 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001612 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001613 }
1614 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001615 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001616
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001617 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001618
Tim Peters0bb44a42000-09-15 07:44:49 +00001619#endif /* which OS */
1620} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001621
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001622#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001623/* A helper function for abspath on win32 */
1624static PyObject *
1625posix__getfullpathname(PyObject *self, PyObject *args)
1626{
1627 /* assume encoded strings wont more than double no of chars */
1628 char inbuf[MAX_PATH*2];
1629 char *inbufp = inbuf;
1630 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1631 char outbuf[MAX_PATH*2];
1632 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001633#ifdef Py_WIN_WIDE_FILENAMES
1634 if (unicode_file_names()) {
1635 PyUnicodeObject *po;
1636 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1637 Py_UNICODE woutbuf[MAX_PATH*2];
1638 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001639 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001640 sizeof(woutbuf)/sizeof(woutbuf[0]),
1641 woutbuf, &wtemp))
1642 return win32_error("GetFullPathName", "");
1643 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1644 }
1645 /* Drop the argument parsing error as narrow strings
1646 are also valid. */
1647 PyErr_Clear();
1648 }
1649#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001650 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1651 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001652 &insize))
1653 return NULL;
1654 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1655 outbuf, &temp))
1656 return win32_error("GetFullPathName", inbuf);
1657 return PyString_FromString(outbuf);
1658} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001659#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001661PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001662"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001663Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001664
Barry Warsaw53699e91996-12-10 23:23:01 +00001665static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001666posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001667{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001668 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001669 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001670 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001671
1672#ifdef Py_WIN_WIDE_FILENAMES
1673 if (unicode_file_names()) {
1674 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001675 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001676 Py_BEGIN_ALLOW_THREADS
1677 /* PyUnicode_AS_UNICODE OK without thread lock as
1678 it is a simple dereference. */
1679 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1680 Py_END_ALLOW_THREADS
1681 if (res < 0)
1682 return posix_error();
1683 Py_INCREF(Py_None);
1684 return Py_None;
1685 }
1686 /* Drop the argument parsing error as narrow strings
1687 are also valid. */
1688 PyErr_Clear();
1689 }
1690#endif
1691
Tim Peters5aa91602002-01-30 05:46:57 +00001692 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001693 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001694 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001695 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001696#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001697 res = mkdir(path);
1698#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001699 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001700#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001701 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001702 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001703 return posix_error_with_allocated_filename(path);
1704 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001705 Py_INCREF(Py_None);
1706 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001707}
1708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001709
Guido van Rossumb6775db1994-08-01 11:34:53 +00001710#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001711#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1712#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1713#include <sys/resource.h>
1714#endif
1715#endif
1716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001718"nice(inc) -> new_priority\n\n\
1719Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001720
Barry Warsaw53699e91996-12-10 23:23:01 +00001721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001722posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001723{
1724 int increment, value;
1725
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001726 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001727 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001728
1729 /* There are two flavours of 'nice': one that returns the new
1730 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001731 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1732 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001733
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001734 If we are of the nice family that returns the new priority, we
1735 need to clear errno before the call, and check if errno is filled
1736 before calling posix_error() on a returnvalue of -1, because the
1737 -1 may be the actual new priority! */
1738
1739 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001740 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001741#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001742 if (value == 0)
1743 value = getpriority(PRIO_PROCESS, 0);
1744#endif
1745 if (value == -1 && errno != 0)
1746 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001747 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001748 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001749}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001750#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001754"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001755Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001756
Barry Warsaw53699e91996-12-10 23:23:01 +00001757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001758posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001759{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001760#ifdef MS_WINDOWS
1761 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1762#else
1763 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1764#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001765}
1766
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001769"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001771
Barry Warsaw53699e91996-12-10 23:23:01 +00001772static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001773posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001774{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001775#ifdef MS_WINDOWS
1776 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1777#else
1778 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1779#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001780}
1781
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001783PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001784"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001786
Barry Warsaw53699e91996-12-10 23:23:01 +00001787static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001788posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001789{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001790#ifdef MS_WINDOWS
1791 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1792#else
1793 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1794#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001795}
1796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001797
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001798#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001802
Barry Warsaw53699e91996-12-10 23:23:01 +00001803static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001804posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001805{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001806 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001807 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001808 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001809 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001810 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001811 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001812 Py_END_ALLOW_THREADS
1813 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001814}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001815#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001816
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001817
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001819"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001821
Barry Warsaw53699e91996-12-10 23:23:01 +00001822static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001823posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001824{
1825 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001826 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001827 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001828 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001829 if (i < 0)
1830 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001831 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001832}
1833
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001836"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001837Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001840"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001844posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef MS_WINDOWS
1847 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1848#else
1849 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1850#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001851}
1852
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001853
Guido van Rossumb6775db1994-08-01 11:34:53 +00001854#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001856"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001858
Barry Warsaw53699e91996-12-10 23:23:01 +00001859static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001860posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001861{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001862 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001863 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001864
Barry Warsaw53699e91996-12-10 23:23:01 +00001865 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001866 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001867 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001868 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001869 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001870 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001871 u.sysname,
1872 u.nodename,
1873 u.release,
1874 u.version,
1875 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001876}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001877#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001878
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001879static int
1880extract_time(PyObject *t, long* sec, long* usec)
1881{
1882 long intval;
1883 if (PyFloat_Check(t)) {
1884 double tval = PyFloat_AsDouble(t);
1885 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1886 if (!intobj)
1887 return -1;
1888 intval = PyInt_AsLong(intobj);
1889 Py_DECREF(intobj);
1890 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001891 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001892 if (*usec < 0)
1893 /* If rounding gave us a negative number,
1894 truncate. */
1895 *usec = 0;
1896 return 0;
1897 }
1898 intval = PyInt_AsLong(t);
1899 if (intval == -1 && PyErr_Occurred())
1900 return -1;
1901 *sec = intval;
1902 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001903 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001904}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001907"utime(path, (atime, utime))\n\
1908utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001909Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001911
Barry Warsaw53699e91996-12-10 23:23:01 +00001912static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001913posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001914{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001915 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001916 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001917 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001918 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001919
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001920#if defined(HAVE_UTIMES)
1921 struct timeval buf[2];
1922#define ATIME buf[0].tv_sec
1923#define MTIME buf[1].tv_sec
1924#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001925/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001926 struct utimbuf buf;
1927#define ATIME buf.actime
1928#define MTIME buf.modtime
1929#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001930#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001931 time_t buf[2];
1932#define ATIME buf[0]
1933#define MTIME buf[1]
1934#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001935#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001936
Barry Warsaw3cef8562000-05-01 16:17:24 +00001937 if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001938 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001939 if (arg == Py_None) {
1940 /* optional time values not given */
1941 Py_BEGIN_ALLOW_THREADS
1942 res = utime(path, NULL);
1943 Py_END_ALLOW_THREADS
1944 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001945 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00001946 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001947 "utime() arg 2 must be a tuple (atime, mtime)");
Barry Warsaw3cef8562000-05-01 16:17:24 +00001948 return NULL;
1949 }
1950 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001951 if (extract_time(PyTuple_GET_ITEM(arg, 0),
1952 &atime, &ausec) == -1)
1953 return NULL;
1954 if (extract_time(PyTuple_GET_ITEM(arg, 1),
1955 &mtime, &musec) == -1)
1956 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001957 ATIME = atime;
1958 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001959#ifdef HAVE_UTIMES
1960 buf[0].tv_usec = ausec;
1961 buf[1].tv_usec = musec;
1962 Py_BEGIN_ALLOW_THREADS
1963 res = utimes(path, buf);
1964 Py_END_ALLOW_THREADS
1965#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00001966 Py_BEGIN_ALLOW_THREADS
1967 res = utime(path, UTIME_ARG);
1968 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001969#endif
Barry Warsaw3cef8562000-05-01 16:17:24 +00001970 }
Guido van Rossumff4949e1992-08-05 19:58:53 +00001971 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001972 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001973 Py_INCREF(Py_None);
1974 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001975#undef UTIME_ARG
1976#undef ATIME
1977#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978}
1979
Guido van Rossum85e3b011991-06-03 12:42:10 +00001980
Guido van Rossum3b066191991-06-04 19:40:25 +00001981/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001983PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001984"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001985Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001986
Barry Warsaw53699e91996-12-10 23:23:01 +00001987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001988posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001989{
1990 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001991 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001992 return NULL;
1993 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001994 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001995}
1996
Martin v. Löwis114619e2002-10-07 06:44:21 +00001997#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
1998static void
1999free_string_array(char **array, int count)
2000{
2001 int i;
2002 for (i = 0; i < count; i++)
2003 PyMem_Free(array[i]);
2004 PyMem_DEL(array);
2005}
2006#endif
2007
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002008
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002009#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002010PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002011"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002012Execute an executable path with arguments, replacing current process.\n\
2013\n\
2014 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002016
Barry Warsaw53699e91996-12-10 23:23:01 +00002017static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002018posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002019{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002020 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002022 char **argvlist;
2023 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002024 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002025
Guido van Rossum89b33251993-10-22 14:26:06 +00002026 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002027 argv is a list or tuple of strings. */
2028
Martin v. Löwis114619e2002-10-07 06:44:21 +00002029 if (!PyArg_ParseTuple(args, "etO:execv",
2030 Py_FileSystemDefaultEncoding,
2031 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002032 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002033 if (PyList_Check(argv)) {
2034 argc = PyList_Size(argv);
2035 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002036 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002037 else if (PyTuple_Check(argv)) {
2038 argc = PyTuple_Size(argv);
2039 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002040 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002041 else {
Fred Drake661ea262000-10-24 19:57:45 +00002042 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002043 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002044 return NULL;
2045 }
2046
2047 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00002048 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002049 PyMem_Free(path);
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002050 return NULL;
2051 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002052
Barry Warsaw53699e91996-12-10 23:23:01 +00002053 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002054 if (argvlist == NULL) {
2055 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002056 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002057 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002058 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002059 if (!PyArg_Parse((*getitem)(argv, i), "et",
2060 Py_FileSystemDefaultEncoding,
2061 &argvlist[i])) {
2062 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002063 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002064 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002065 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002066 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002067
Guido van Rossum85e3b011991-06-03 12:42:10 +00002068 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002069 }
2070 argvlist[argc] = NULL;
2071
Guido van Rossumb6775db1994-08-01 11:34:53 +00002072#ifdef BAD_EXEC_PROTOTYPES
2073 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002074#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002075 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002076#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077
Guido van Rossum85e3b011991-06-03 12:42:10 +00002078 /* If we get here it's definitely an error */
2079
Martin v. Löwis114619e2002-10-07 06:44:21 +00002080 free_string_array(argvlist, argc);
2081 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002082 return posix_error();
2083}
2084
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002086PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002087"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088Execute a path with arguments and environment, replacing current process.\n\
2089\n\
2090 path: path of executable file\n\
2091 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002092 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002093
Barry Warsaw53699e91996-12-10 23:23:01 +00002094static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002095posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002096{
2097 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002098 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002099 char **argvlist;
2100 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002101 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002102 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002103 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002104 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002105
2106 /* execve has three arguments: (path, argv, env), where
2107 argv is a list or tuple of strings and env is a dictionary
2108 like posix.environ. */
2109
Martin v. Löwis114619e2002-10-07 06:44:21 +00002110 if (!PyArg_ParseTuple(args, "etOO:execve",
2111 Py_FileSystemDefaultEncoding,
2112 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002113 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002114 if (PyList_Check(argv)) {
2115 argc = PyList_Size(argv);
2116 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002117 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002118 else if (PyTuple_Check(argv)) {
2119 argc = PyTuple_Size(argv);
2120 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002121 }
2122 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002123 PyErr_SetString(PyExc_TypeError,
2124 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002125 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002126 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002127 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002128 PyErr_SetString(PyExc_TypeError,
2129 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002130 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002131 }
2132
Guido van Rossum50422b42000-04-26 20:34:28 +00002133 if (argc == 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00002134 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00002135 "execve() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002136 goto fail_0;
Guido van Rossum50422b42000-04-26 20:34:28 +00002137 }
2138
Barry Warsaw53699e91996-12-10 23:23:01 +00002139 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002140 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002141 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002142 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002143 }
2144 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002145 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002146 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002147 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002148 &argvlist[i]))
2149 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002150 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002151 goto fail_1;
2152 }
2153 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002154 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002155 argvlist[argc] = NULL;
2156
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002157 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002158 if (i < 0)
2159 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002160 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002161 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002162 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002163 goto fail_1;
2164 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002165 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002166 keys = PyMapping_Keys(env);
2167 vals = PyMapping_Values(env);
2168 if (!keys || !vals)
2169 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002170 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2171 PyErr_SetString(PyExc_TypeError,
2172 "execve(): env.keys() or env.values() is not a list");
2173 goto fail_2;
2174 }
Tim Peters5aa91602002-01-30 05:46:57 +00002175
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002176 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002177 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002178 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002179
2180 key = PyList_GetItem(keys, pos);
2181 val = PyList_GetItem(vals, pos);
2182 if (!key || !val)
2183 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002184
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002185 if (!PyArg_Parse(
2186 key,
2187 "s;execve() arg 3 contains a non-string key",
2188 &k) ||
2189 !PyArg_Parse(
2190 val,
2191 "s;execve() arg 3 contains a non-string value",
2192 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002193 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002194 goto fail_2;
2195 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002196
2197#if defined(PYOS_OS2)
2198 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2199 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2200#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002201 len = PyString_Size(key) + PyString_Size(val) + 2;
2202 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002203 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002204 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002205 goto fail_2;
2206 }
Tim Petersc8996f52001-12-03 20:41:00 +00002207 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002208 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002209#if defined(PYOS_OS2)
2210 }
2211#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002212 }
2213 envlist[envc] = 0;
2214
Guido van Rossumb6775db1994-08-01 11:34:53 +00002215
2216#ifdef BAD_EXEC_PROTOTYPES
2217 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002218#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002219 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002220#endif /* BAD_EXEC_PROTOTYPES */
Tim Peters5aa91602002-01-30 05:46:57 +00002221
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002222 /* If we get here it's definitely an error */
2223
2224 (void) posix_error();
2225
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002226 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002227 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 PyMem_DEL(envlist[envc]);
2229 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002230 fail_1:
2231 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002232 Py_XDECREF(vals);
2233 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002234 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002235 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002236 return NULL;
2237}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002238#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002240
Guido van Rossuma1065681999-01-25 23:20:23 +00002241#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002242PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002243"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002244Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002245\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002246 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002247 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002248 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002249
2250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002251posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002252{
2253 char *path;
2254 PyObject *argv;
2255 char **argvlist;
2256 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002257 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002258 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002259
2260 /* spawnv has three arguments: (mode, path, argv), where
2261 argv is a list or tuple of strings. */
2262
Martin v. Löwis114619e2002-10-07 06:44:21 +00002263 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2264 Py_FileSystemDefaultEncoding,
2265 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002266 return NULL;
2267 if (PyList_Check(argv)) {
2268 argc = PyList_Size(argv);
2269 getitem = PyList_GetItem;
2270 }
2271 else if (PyTuple_Check(argv)) {
2272 argc = PyTuple_Size(argv);
2273 getitem = PyTuple_GetItem;
2274 }
2275 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002276 PyErr_SetString(PyExc_TypeError,
2277 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002278 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002279 return NULL;
2280 }
2281
2282 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002283 if (argvlist == NULL) {
2284 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002285 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002286 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002287 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002288 if (!PyArg_Parse((*getitem)(argv, i), "et",
2289 Py_FileSystemDefaultEncoding,
2290 &argvlist[i])) {
2291 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002292 PyErr_SetString(
2293 PyExc_TypeError,
2294 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002295 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002296 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002297 }
2298 }
2299 argvlist[argc] = NULL;
2300
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002301#if defined(PYOS_OS2) && defined(PYCC_GCC)
2302 Py_BEGIN_ALLOW_THREADS
2303 spawnval = spawnv(mode, path, argvlist);
2304 Py_END_ALLOW_THREADS
2305#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002306 if (mode == _OLD_P_OVERLAY)
2307 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002308
Tim Peters25059d32001-12-07 20:35:43 +00002309 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002310 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002311 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002312#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002313
Martin v. Löwis114619e2002-10-07 06:44:21 +00002314 free_string_array(argvlist, argc);
2315 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002316
Fred Drake699f3522000-06-29 21:12:41 +00002317 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002318 return posix_error();
2319 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002320#if SIZEOF_LONG == SIZEOF_VOID_P
2321 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002322#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002323 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002324#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002325}
2326
2327
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002328PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002329"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002330Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002331\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002332 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002333 path: path of executable file\n\
2334 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002335 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002336
2337static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002338posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002339{
2340 char *path;
2341 PyObject *argv, *env;
2342 char **argvlist;
2343 char **envlist;
2344 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2345 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002346 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002347 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002348 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002349
2350 /* spawnve has four arguments: (mode, path, argv, env), where
2351 argv is a list or tuple of strings and env is a dictionary
2352 like posix.environ. */
2353
Martin v. Löwis114619e2002-10-07 06:44:21 +00002354 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2355 Py_FileSystemDefaultEncoding,
2356 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002357 return NULL;
2358 if (PyList_Check(argv)) {
2359 argc = PyList_Size(argv);
2360 getitem = PyList_GetItem;
2361 }
2362 else if (PyTuple_Check(argv)) {
2363 argc = PyTuple_Size(argv);
2364 getitem = PyTuple_GetItem;
2365 }
2366 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002367 PyErr_SetString(PyExc_TypeError,
2368 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002369 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002370 }
2371 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002372 PyErr_SetString(PyExc_TypeError,
2373 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002374 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002375 }
2376
2377 argvlist = PyMem_NEW(char *, argc+1);
2378 if (argvlist == NULL) {
2379 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002380 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002381 }
2382 for (i = 0; i < argc; i++) {
2383 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002384 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002385 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002386 &argvlist[i]))
2387 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002388 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002389 goto fail_1;
2390 }
2391 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002392 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002393 argvlist[argc] = NULL;
2394
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002395 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002396 if (i < 0)
2397 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002398 envlist = PyMem_NEW(char *, i + 1);
2399 if (envlist == NULL) {
2400 PyErr_NoMemory();
2401 goto fail_1;
2402 }
2403 envc = 0;
2404 keys = PyMapping_Keys(env);
2405 vals = PyMapping_Values(env);
2406 if (!keys || !vals)
2407 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002408 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2409 PyErr_SetString(PyExc_TypeError,
2410 "spawnve(): env.keys() or env.values() is not a list");
2411 goto fail_2;
2412 }
Tim Peters5aa91602002-01-30 05:46:57 +00002413
Guido van Rossuma1065681999-01-25 23:20:23 +00002414 for (pos = 0; pos < i; pos++) {
2415 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002416 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002417
2418 key = PyList_GetItem(keys, pos);
2419 val = PyList_GetItem(vals, pos);
2420 if (!key || !val)
2421 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002422
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002423 if (!PyArg_Parse(
2424 key,
2425 "s;spawnve() arg 3 contains a non-string key",
2426 &k) ||
2427 !PyArg_Parse(
2428 val,
2429 "s;spawnve() arg 3 contains a non-string value",
2430 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002431 {
2432 goto fail_2;
2433 }
Tim Petersc8996f52001-12-03 20:41:00 +00002434 len = PyString_Size(key) + PyString_Size(val) + 2;
2435 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002436 if (p == NULL) {
2437 PyErr_NoMemory();
2438 goto fail_2;
2439 }
Tim Petersc8996f52001-12-03 20:41:00 +00002440 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002441 envlist[envc++] = p;
2442 }
2443 envlist[envc] = 0;
2444
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002445#if defined(PYOS_OS2) && defined(PYCC_GCC)
2446 Py_BEGIN_ALLOW_THREADS
2447 spawnval = spawnve(mode, path, argvlist, envlist);
2448 Py_END_ALLOW_THREADS
2449#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002450 if (mode == _OLD_P_OVERLAY)
2451 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002452
2453 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002454 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002455 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002456#endif
Tim Peters25059d32001-12-07 20:35:43 +00002457
Fred Drake699f3522000-06-29 21:12:41 +00002458 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002459 (void) posix_error();
2460 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002461#if SIZEOF_LONG == SIZEOF_VOID_P
2462 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002463#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002464 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002465#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002466
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002467 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002468 while (--envc >= 0)
2469 PyMem_DEL(envlist[envc]);
2470 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002471 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002472 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002473 Py_XDECREF(vals);
2474 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002475 fail_0:
2476 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002477 return res;
2478}
2479#endif /* HAVE_SPAWNV */
2480
2481
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002482#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002483PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002484"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002485Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2486\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002487Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002488
2489static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002490posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002491{
Neal Norwitze241ce82003-02-17 18:17:05 +00002492 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002493 if (pid == -1)
2494 return posix_error();
2495 PyOS_AfterFork();
2496 return PyInt_FromLong((long)pid);
2497}
2498#endif
2499
2500
Guido van Rossumad0ee831995-03-01 10:34:45 +00002501#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002502PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002503"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002504Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002505Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002506
Barry Warsaw53699e91996-12-10 23:23:01 +00002507static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002508posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002509{
Neal Norwitze241ce82003-02-17 18:17:05 +00002510 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002511 if (pid == -1)
2512 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002513 if (pid == 0)
2514 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002515 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002516}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002517#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002518
Neal Norwitzb59798b2003-03-21 01:43:31 +00002519/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002520/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2521#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002522#define DEV_PTY_FILE "/dev/ptc"
2523#define HAVE_DEV_PTMX
2524#else
2525#define DEV_PTY_FILE "/dev/ptmx"
2526#endif
2527
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002528#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002529#ifdef HAVE_PTY_H
2530#include <pty.h>
2531#else
2532#ifdef HAVE_LIBUTIL_H
2533#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002534#endif /* HAVE_LIBUTIL_H */
2535#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002536#ifdef HAVE_STROPTS_H
2537#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002538#endif
2539#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002540
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002541#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002543"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002544Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002545
2546static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002547posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002548{
2549 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002550#ifndef HAVE_OPENPTY
2551 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002552#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002553#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002554 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002555#ifdef sun
2556 extern char *ptsname();
2557#endif
2558#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002559
Thomas Wouters70c21a12000-07-14 14:28:33 +00002560#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002561 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2562 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002563#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002564 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2565 if (slave_name == NULL)
2566 return posix_error();
2567
2568 slave_fd = open(slave_name, O_RDWR);
2569 if (slave_fd < 0)
2570 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002571#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002572 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002573 if (master_fd < 0)
2574 return posix_error();
2575 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002576 /* change permission of slave */
2577 if (grantpt(master_fd) < 0) {
2578 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002579 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002580 }
2581 /* unlock slave */
2582 if (unlockpt(master_fd) < 0) {
2583 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002584 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002585 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002586 signal(SIGCHLD, sig_saved);
2587 slave_name = ptsname(master_fd); /* get name of slave */
2588 if (slave_name == NULL)
2589 return posix_error();
2590 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2591 if (slave_fd < 0)
2592 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002593#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002594 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2595 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002596#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002597 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002598#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002599#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002600#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002601
Fred Drake8cef4cf2000-06-28 16:40:38 +00002602 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002603
Fred Drake8cef4cf2000-06-28 16:40:38 +00002604}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002605#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002606
2607#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002608PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002609"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002610Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2611Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002612To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002613
2614static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002615posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002616{
2617 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002618
Fred Drake8cef4cf2000-06-28 16:40:38 +00002619 pid = forkpty(&master_fd, NULL, NULL, NULL);
2620 if (pid == -1)
2621 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002622 if (pid == 0)
2623 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002624 return Py_BuildValue("(ii)", pid, master_fd);
2625}
2626#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002627
Guido van Rossumad0ee831995-03-01 10:34:45 +00002628#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002630"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002631Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002632
Barry Warsaw53699e91996-12-10 23:23:01 +00002633static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002634posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002635{
Barry Warsaw53699e91996-12-10 23:23:01 +00002636 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002637}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002638#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002639
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002640
Guido van Rossumad0ee831995-03-01 10:34:45 +00002641#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002642PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002643"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002644Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645
Barry Warsaw53699e91996-12-10 23:23:01 +00002646static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002647posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002648{
Barry Warsaw53699e91996-12-10 23:23:01 +00002649 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002650}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002651#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002652
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002653
Guido van Rossumad0ee831995-03-01 10:34:45 +00002654#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002655PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002656"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002657Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002658
Barry Warsaw53699e91996-12-10 23:23:01 +00002659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002660posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002661{
Barry Warsaw53699e91996-12-10 23:23:01 +00002662 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002663}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002664#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002667PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002668"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002669Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002670
Barry Warsaw53699e91996-12-10 23:23:01 +00002671static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002672posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002673{
Barry Warsaw53699e91996-12-10 23:23:01 +00002674 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002675}
2676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002677
Fred Drakec9680921999-12-13 16:37:25 +00002678#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002680"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002681Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00002682
2683static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002684posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00002685{
2686 PyObject *result = NULL;
2687
Fred Drakec9680921999-12-13 16:37:25 +00002688#ifdef NGROUPS_MAX
2689#define MAX_GROUPS NGROUPS_MAX
2690#else
2691 /* defined to be 16 on Solaris7, so this should be a small number */
2692#define MAX_GROUPS 64
2693#endif
2694 gid_t grouplist[MAX_GROUPS];
2695 int n;
2696
2697 n = getgroups(MAX_GROUPS, grouplist);
2698 if (n < 0)
2699 posix_error();
2700 else {
2701 result = PyList_New(n);
2702 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00002703 int i;
2704 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00002705 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00002706 if (o == NULL) {
2707 Py_DECREF(result);
2708 result = NULL;
2709 break;
2710 }
2711 PyList_SET_ITEM(result, i, o);
2712 }
2713 }
2714 }
Neal Norwitze241ce82003-02-17 18:17:05 +00002715
Fred Drakec9680921999-12-13 16:37:25 +00002716 return result;
2717}
2718#endif
2719
Martin v. Löwis606edc12002-06-13 21:09:11 +00002720#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002721PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002722"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002723Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00002724
2725static PyObject *
2726posix_getpgid(PyObject *self, PyObject *args)
2727{
2728 int pid, pgid;
2729 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
2730 return NULL;
2731 pgid = getpgid(pid);
2732 if (pgid < 0)
2733 return posix_error();
2734 return PyInt_FromLong((long)pgid);
2735}
2736#endif /* HAVE_GETPGID */
2737
2738
Guido van Rossumb6775db1994-08-01 11:34:53 +00002739#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002740PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002741"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002742Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002743
Barry Warsaw53699e91996-12-10 23:23:01 +00002744static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002745posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00002746{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002747#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00002748 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002749#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00002750 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002751#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00002752}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002753#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00002754
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002755
Guido van Rossumb6775db1994-08-01 11:34:53 +00002756#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002757PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002758"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002759Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002760
Barry Warsaw53699e91996-12-10 23:23:01 +00002761static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002762posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00002763{
Guido van Rossum64933891994-10-20 21:56:42 +00002764#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00002765 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002766#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002767 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002768#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00002769 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002770 Py_INCREF(Py_None);
2771 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002772}
2773
Guido van Rossumb6775db1994-08-01 11:34:53 +00002774#endif /* HAVE_SETPGRP */
2775
Guido van Rossumad0ee831995-03-01 10:34:45 +00002776#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002777PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002778"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002780
Barry Warsaw53699e91996-12-10 23:23:01 +00002781static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002782posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002783{
Barry Warsaw53699e91996-12-10 23:23:01 +00002784 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002785}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002786#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002788
Fred Drake12c6e2d1999-12-14 21:25:03 +00002789#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002790PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002791"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002792Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00002793
2794static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002795posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00002796{
Neal Norwitze241ce82003-02-17 18:17:05 +00002797 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00002798 char *name;
2799 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00002800
Fred Drakea30680b2000-12-06 21:24:28 +00002801 errno = 0;
2802 name = getlogin();
2803 if (name == NULL) {
2804 if (errno)
2805 posix_error();
2806 else
2807 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00002808 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00002809 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00002810 else
2811 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00002812 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00002813
Fred Drake12c6e2d1999-12-14 21:25:03 +00002814 return result;
2815}
2816#endif
2817
Guido van Rossumad0ee831995-03-01 10:34:45 +00002818#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002819PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002820"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002821Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002822
Barry Warsaw53699e91996-12-10 23:23:01 +00002823static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002824posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002825{
Barry Warsaw53699e91996-12-10 23:23:01 +00002826 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002827}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002828#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002829
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002830
Guido van Rossumad0ee831995-03-01 10:34:45 +00002831#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002832PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002833"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002834Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002835
Barry Warsaw53699e91996-12-10 23:23:01 +00002836static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002837posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002838{
2839 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002840 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002841 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002842#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002843 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
2844 APIRET rc;
2845 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002846 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002847
2848 } else if (sig == XCPT_SIGNAL_KILLPROC) {
2849 APIRET rc;
2850 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002851 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002852
2853 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002854 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002855#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00002856 if (kill(pid, sig) == -1)
2857 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002858#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002859 Py_INCREF(Py_None);
2860 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002861}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002862#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002863
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002864#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002865PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002866"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002867Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002868
2869static PyObject *
2870posix_killpg(PyObject *self, PyObject *args)
2871{
2872 int pgid, sig;
2873 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
2874 return NULL;
2875 if (killpg(pgid, sig) == -1)
2876 return posix_error();
2877 Py_INCREF(Py_None);
2878 return Py_None;
2879}
2880#endif
2881
Guido van Rossumc0125471996-06-28 18:55:32 +00002882#ifdef HAVE_PLOCK
2883
2884#ifdef HAVE_SYS_LOCK_H
2885#include <sys/lock.h>
2886#endif
2887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002888PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002889"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002890Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002891
Barry Warsaw53699e91996-12-10 23:23:01 +00002892static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002893posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00002894{
2895 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002896 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00002897 return NULL;
2898 if (plock(op) == -1)
2899 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002900 Py_INCREF(Py_None);
2901 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00002902}
2903#endif
2904
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002905
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002906#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002907PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002908"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002909Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002910
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002911#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002912#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002913static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002914async_system(const char *command)
2915{
2916 char *p, errormsg[256], args[1024];
2917 RESULTCODES rcodes;
2918 APIRET rc;
2919 char *shell = getenv("COMSPEC");
2920 if (!shell)
2921 shell = "cmd";
2922
2923 strcpy(args, shell);
2924 p = &args[ strlen(args)+1 ];
2925 strcpy(p, "/c ");
2926 strcat(p, command);
2927 p += strlen(p) + 1;
2928 *p = '\0';
2929
2930 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002931 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002932 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002933 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002934 &rcodes, shell);
2935 return rc;
2936}
2937
Guido van Rossumd48f2521997-12-05 22:19:34 +00002938static FILE *
2939popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002940{
2941 HFILE rhan, whan;
2942 FILE *retfd = NULL;
2943 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2944
Guido van Rossumd48f2521997-12-05 22:19:34 +00002945 if (rc != NO_ERROR) {
2946 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002947 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002948 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002949
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002950 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2951 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002952
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002953 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2954 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002955
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002956 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
2957 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002958
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002959 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002960 }
2961
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002962 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
2963 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002964
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002965 if (rc == NO_ERROR)
2966 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
2967
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002968 close(oldfd); /* And Close Saved STDOUT Handle */
2969 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002970
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002971 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
2972 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002973
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002974 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2975 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002976
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002977 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
2978 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002979
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002980 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002981 }
2982
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002983 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
2984 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002985
Martin v. Löwisdedbe252001-11-02 23:59:11 +00002986 if (rc == NO_ERROR)
2987 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
2988
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002989 close(oldfd); /* And Close Saved STDIN Handle */
2990 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002991
Guido van Rossumd48f2521997-12-05 22:19:34 +00002992 } else {
2993 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002994 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002995 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002996}
2997
2998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002999posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003000{
3001 char *name;
3002 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003003 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003004 FILE *fp;
3005 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003006 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003007 return NULL;
3008 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003009 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003010 Py_END_ALLOW_THREADS
3011 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003012 return os2_error(err);
3013
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003014 f = PyFile_FromFile(fp, name, mode, fclose);
3015 if (f != NULL)
3016 PyFile_SetBufSize(f, bufsize);
3017 return f;
3018}
3019
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003020#elif defined(PYCC_GCC)
3021
3022/* standard posix version of popen() support */
3023static PyObject *
3024posix_popen(PyObject *self, PyObject *args)
3025{
3026 char *name;
3027 char *mode = "r";
3028 int bufsize = -1;
3029 FILE *fp;
3030 PyObject *f;
3031 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3032 return NULL;
3033 Py_BEGIN_ALLOW_THREADS
3034 fp = popen(name, mode);
3035 Py_END_ALLOW_THREADS
3036 if (fp == NULL)
3037 return posix_error();
3038 f = PyFile_FromFile(fp, name, mode, pclose);
3039 if (f != NULL)
3040 PyFile_SetBufSize(f, bufsize);
3041 return f;
3042}
3043
3044/* fork() under OS/2 has lots'o'warts
3045 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3046 * most of this code is a ripoff of the win32 code, but using the
3047 * capabilities of EMX's C library routines
3048 */
3049
3050/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3051#define POPEN_1 1
3052#define POPEN_2 2
3053#define POPEN_3 3
3054#define POPEN_4 4
3055
3056static PyObject *_PyPopen(char *, int, int, int);
3057static int _PyPclose(FILE *file);
3058
3059/*
3060 * Internal dictionary mapping popen* file pointers to process handles,
3061 * for use when retrieving the process exit code. See _PyPclose() below
3062 * for more information on this dictionary's use.
3063 */
3064static PyObject *_PyPopenProcs = NULL;
3065
3066/* os2emx version of popen2()
3067 *
3068 * The result of this function is a pipe (file) connected to the
3069 * process's stdin, and a pipe connected to the process's stdout.
3070 */
3071
3072static PyObject *
3073os2emx_popen2(PyObject *self, PyObject *args)
3074{
3075 PyObject *f;
3076 int tm=0;
3077
3078 char *cmdstring;
3079 char *mode = "t";
3080 int bufsize = -1;
3081 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3082 return NULL;
3083
3084 if (*mode == 't')
3085 tm = O_TEXT;
3086 else if (*mode != 'b') {
3087 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3088 return NULL;
3089 } else
3090 tm = O_BINARY;
3091
3092 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3093
3094 return f;
3095}
3096
3097/*
3098 * Variation on os2emx.popen2
3099 *
3100 * The result of this function is 3 pipes - the process's stdin,
3101 * stdout and stderr
3102 */
3103
3104static PyObject *
3105os2emx_popen3(PyObject *self, PyObject *args)
3106{
3107 PyObject *f;
3108 int tm = 0;
3109
3110 char *cmdstring;
3111 char *mode = "t";
3112 int bufsize = -1;
3113 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3114 return NULL;
3115
3116 if (*mode == 't')
3117 tm = O_TEXT;
3118 else if (*mode != 'b') {
3119 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3120 return NULL;
3121 } else
3122 tm = O_BINARY;
3123
3124 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3125
3126 return f;
3127}
3128
3129/*
3130 * Variation on os2emx.popen2
3131 *
Tim Peters11b23062003-04-23 02:39:17 +00003132 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003133 * and stdout+stderr combined as a single pipe.
3134 */
3135
3136static PyObject *
3137os2emx_popen4(PyObject *self, PyObject *args)
3138{
3139 PyObject *f;
3140 int tm = 0;
3141
3142 char *cmdstring;
3143 char *mode = "t";
3144 int bufsize = -1;
3145 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3146 return NULL;
3147
3148 if (*mode == 't')
3149 tm = O_TEXT;
3150 else if (*mode != 'b') {
3151 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3152 return NULL;
3153 } else
3154 tm = O_BINARY;
3155
3156 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3157
3158 return f;
3159}
3160
3161/* a couple of structures for convenient handling of multiple
3162 * file handles and pipes
3163 */
3164struct file_ref
3165{
3166 int handle;
3167 int flags;
3168};
3169
3170struct pipe_ref
3171{
3172 int rd;
3173 int wr;
3174};
3175
3176/* The following code is derived from the win32 code */
3177
3178static PyObject *
3179_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3180{
3181 struct file_ref stdio[3];
3182 struct pipe_ref p_fd[3];
3183 FILE *p_s[3];
3184 int file_count, i, pipe_err, pipe_pid;
3185 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3186 PyObject *f, *p_f[3];
3187
3188 /* file modes for subsequent fdopen's on pipe handles */
3189 if (mode == O_TEXT)
3190 {
3191 rd_mode = "rt";
3192 wr_mode = "wt";
3193 }
3194 else
3195 {
3196 rd_mode = "rb";
3197 wr_mode = "wb";
3198 }
3199
3200 /* prepare shell references */
3201 if ((shell = getenv("EMXSHELL")) == NULL)
3202 if ((shell = getenv("COMSPEC")) == NULL)
3203 {
3204 errno = ENOENT;
3205 return posix_error();
3206 }
3207
3208 sh_name = _getname(shell);
3209 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3210 opt = "/c";
3211 else
3212 opt = "-c";
3213
3214 /* save current stdio fds + their flags, and set not inheritable */
3215 i = pipe_err = 0;
3216 while (pipe_err >= 0 && i < 3)
3217 {
3218 pipe_err = stdio[i].handle = dup(i);
3219 stdio[i].flags = fcntl(i, F_GETFD, 0);
3220 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3221 i++;
3222 }
3223 if (pipe_err < 0)
3224 {
3225 /* didn't get them all saved - clean up and bail out */
3226 int saved_err = errno;
3227 while (i-- > 0)
3228 {
3229 close(stdio[i].handle);
3230 }
3231 errno = saved_err;
3232 return posix_error();
3233 }
3234
3235 /* create pipe ends */
3236 file_count = 2;
3237 if (n == POPEN_3)
3238 file_count = 3;
3239 i = pipe_err = 0;
3240 while ((pipe_err == 0) && (i < file_count))
3241 pipe_err = pipe((int *)&p_fd[i++]);
3242 if (pipe_err < 0)
3243 {
3244 /* didn't get them all made - clean up and bail out */
3245 while (i-- > 0)
3246 {
3247 close(p_fd[i].wr);
3248 close(p_fd[i].rd);
3249 }
3250 errno = EPIPE;
3251 return posix_error();
3252 }
3253
3254 /* change the actual standard IO streams over temporarily,
3255 * making the retained pipe ends non-inheritable
3256 */
3257 pipe_err = 0;
3258
3259 /* - stdin */
3260 if (dup2(p_fd[0].rd, 0) == 0)
3261 {
3262 close(p_fd[0].rd);
3263 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3264 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3265 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3266 {
3267 close(p_fd[0].wr);
3268 pipe_err = -1;
3269 }
3270 }
3271 else
3272 {
3273 pipe_err = -1;
3274 }
3275
3276 /* - stdout */
3277 if (pipe_err == 0)
3278 {
3279 if (dup2(p_fd[1].wr, 1) == 1)
3280 {
3281 close(p_fd[1].wr);
3282 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3283 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3284 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3285 {
3286 close(p_fd[1].rd);
3287 pipe_err = -1;
3288 }
3289 }
3290 else
3291 {
3292 pipe_err = -1;
3293 }
3294 }
3295
3296 /* - stderr, as required */
3297 if (pipe_err == 0)
3298 switch (n)
3299 {
3300 case POPEN_3:
3301 {
3302 if (dup2(p_fd[2].wr, 2) == 2)
3303 {
3304 close(p_fd[2].wr);
3305 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3306 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3307 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3308 {
3309 close(p_fd[2].rd);
3310 pipe_err = -1;
3311 }
3312 }
3313 else
3314 {
3315 pipe_err = -1;
3316 }
3317 break;
3318 }
3319
3320 case POPEN_4:
3321 {
3322 if (dup2(1, 2) != 2)
3323 {
3324 pipe_err = -1;
3325 }
3326 break;
3327 }
3328 }
3329
3330 /* spawn the child process */
3331 if (pipe_err == 0)
3332 {
3333 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3334 if (pipe_pid == -1)
3335 {
3336 pipe_err = -1;
3337 }
3338 else
3339 {
3340 /* save the PID into the FILE structure
3341 * NOTE: this implementation doesn't actually
3342 * take advantage of this, but do it for
3343 * completeness - AIM Apr01
3344 */
3345 for (i = 0; i < file_count; i++)
3346 p_s[i]->_pid = pipe_pid;
3347 }
3348 }
3349
3350 /* reset standard IO to normal */
3351 for (i = 0; i < 3; i++)
3352 {
3353 dup2(stdio[i].handle, i);
3354 fcntl(i, F_SETFD, stdio[i].flags);
3355 close(stdio[i].handle);
3356 }
3357
3358 /* if any remnant problems, clean up and bail out */
3359 if (pipe_err < 0)
3360 {
3361 for (i = 0; i < 3; i++)
3362 {
3363 close(p_fd[i].rd);
3364 close(p_fd[i].wr);
3365 }
3366 errno = EPIPE;
3367 return posix_error_with_filename(cmdstring);
3368 }
3369
3370 /* build tuple of file objects to return */
3371 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3372 PyFile_SetBufSize(p_f[0], bufsize);
3373 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3374 PyFile_SetBufSize(p_f[1], bufsize);
3375 if (n == POPEN_3)
3376 {
3377 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3378 PyFile_SetBufSize(p_f[0], bufsize);
3379 f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]);
3380 }
3381 else
3382 f = Py_BuildValue("OO", p_f[0], p_f[1]);
3383
3384 /*
3385 * Insert the files we've created into the process dictionary
3386 * all referencing the list with the process handle and the
3387 * initial number of files (see description below in _PyPclose).
3388 * Since if _PyPclose later tried to wait on a process when all
3389 * handles weren't closed, it could create a deadlock with the
3390 * child, we spend some energy here to try to ensure that we
3391 * either insert all file handles into the dictionary or none
3392 * at all. It's a little clumsy with the various popen modes
3393 * and variable number of files involved.
3394 */
3395 if (!_PyPopenProcs)
3396 {
3397 _PyPopenProcs = PyDict_New();
3398 }
3399
3400 if (_PyPopenProcs)
3401 {
3402 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3403 int ins_rc[3];
3404
3405 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3406 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3407
3408 procObj = PyList_New(2);
3409 pidObj = PyInt_FromLong((long) pipe_pid);
3410 intObj = PyInt_FromLong((long) file_count);
3411
3412 if (procObj && pidObj && intObj)
3413 {
3414 PyList_SetItem(procObj, 0, pidObj);
3415 PyList_SetItem(procObj, 1, intObj);
3416
3417 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3418 if (fileObj[0])
3419 {
3420 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3421 fileObj[0],
3422 procObj);
3423 }
3424 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3425 if (fileObj[1])
3426 {
3427 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3428 fileObj[1],
3429 procObj);
3430 }
3431 if (file_count >= 3)
3432 {
3433 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3434 if (fileObj[2])
3435 {
3436 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3437 fileObj[2],
3438 procObj);
3439 }
3440 }
3441
3442 if (ins_rc[0] < 0 || !fileObj[0] ||
3443 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3444 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3445 {
3446 /* Something failed - remove any dictionary
3447 * entries that did make it.
3448 */
3449 if (!ins_rc[0] && fileObj[0])
3450 {
3451 PyDict_DelItem(_PyPopenProcs,
3452 fileObj[0]);
3453 }
3454 if (!ins_rc[1] && fileObj[1])
3455 {
3456 PyDict_DelItem(_PyPopenProcs,
3457 fileObj[1]);
3458 }
3459 if (!ins_rc[2] && fileObj[2])
3460 {
3461 PyDict_DelItem(_PyPopenProcs,
3462 fileObj[2]);
3463 }
3464 }
3465 }
Tim Peters11b23062003-04-23 02:39:17 +00003466
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003467 /*
3468 * Clean up our localized references for the dictionary keys
3469 * and value since PyDict_SetItem will Py_INCREF any copies
3470 * that got placed in the dictionary.
3471 */
3472 Py_XDECREF(procObj);
3473 Py_XDECREF(fileObj[0]);
3474 Py_XDECREF(fileObj[1]);
3475 Py_XDECREF(fileObj[2]);
3476 }
3477
3478 /* Child is launched. */
3479 return f;
3480}
3481
3482/*
3483 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3484 * exit code for the child process and return as a result of the close.
3485 *
3486 * This function uses the _PyPopenProcs dictionary in order to map the
3487 * input file pointer to information about the process that was
3488 * originally created by the popen* call that created the file pointer.
3489 * The dictionary uses the file pointer as a key (with one entry
3490 * inserted for each file returned by the original popen* call) and a
3491 * single list object as the value for all files from a single call.
3492 * The list object contains the Win32 process handle at [0], and a file
3493 * count at [1], which is initialized to the total number of file
3494 * handles using that list.
3495 *
3496 * This function closes whichever handle it is passed, and decrements
3497 * the file count in the dictionary for the process handle pointed to
3498 * by this file. On the last close (when the file count reaches zero),
3499 * this function will wait for the child process and then return its
3500 * exit code as the result of the close() operation. This permits the
3501 * files to be closed in any order - it is always the close() of the
3502 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003503 *
3504 * NOTE: This function is currently called with the GIL released.
3505 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003506 */
3507
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003508static int _PyPclose(FILE *file)
3509{
3510 int result;
3511 int exit_code;
3512 int pipe_pid;
3513 PyObject *procObj, *pidObj, *intObj, *fileObj;
3514 int file_count;
3515#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003516 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003517#endif
3518
3519 /* Close the file handle first, to ensure it can't block the
3520 * child from exiting if it's the last handle.
3521 */
3522 result = fclose(file);
3523
3524#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003525 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003526#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003527 if (_PyPopenProcs)
3528 {
3529 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3530 (procObj = PyDict_GetItem(_PyPopenProcs,
3531 fileObj)) != NULL &&
3532 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3533 (intObj = PyList_GetItem(procObj,1)) != NULL)
3534 {
3535 pipe_pid = (int) PyInt_AsLong(pidObj);
3536 file_count = (int) PyInt_AsLong(intObj);
3537
3538 if (file_count > 1)
3539 {
3540 /* Still other files referencing process */
3541 file_count--;
3542 PyList_SetItem(procObj,1,
3543 PyInt_FromLong((long) file_count));
3544 }
3545 else
3546 {
3547 /* Last file for this process */
3548 if (result != EOF &&
3549 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3550 {
3551 /* extract exit status */
3552 if (WIFEXITED(exit_code))
3553 {
3554 result = WEXITSTATUS(exit_code);
3555 }
3556 else
3557 {
3558 errno = EPIPE;
3559 result = -1;
3560 }
3561 }
3562 else
3563 {
3564 /* Indicate failure - this will cause the file object
3565 * to raise an I/O error and translate the last
3566 * error code from errno. We do have a problem with
3567 * last errors that overlap the normal errno table,
3568 * but that's a consistent problem with the file object.
3569 */
3570 result = -1;
3571 }
3572 }
3573
3574 /* Remove this file pointer from dictionary */
3575 PyDict_DelItem(_PyPopenProcs, fileObj);
3576
3577 if (PyDict_Size(_PyPopenProcs) == 0)
3578 {
3579 Py_DECREF(_PyPopenProcs);
3580 _PyPopenProcs = NULL;
3581 }
3582
3583 } /* if object retrieval ok */
3584
3585 Py_XDECREF(fileObj);
3586 } /* if _PyPopenProcs */
3587
3588#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003589 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003590#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003591 return result;
3592}
3593
3594#endif /* PYCC_??? */
3595
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003596#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003597
3598/*
3599 * Portable 'popen' replacement for Win32.
3600 *
3601 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3602 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003603 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003604 */
3605
3606#include <malloc.h>
3607#include <io.h>
3608#include <fcntl.h>
3609
3610/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3611#define POPEN_1 1
3612#define POPEN_2 2
3613#define POPEN_3 3
3614#define POPEN_4 4
3615
3616static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003617static int _PyPclose(FILE *file);
3618
3619/*
3620 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003621 * for use when retrieving the process exit code. See _PyPclose() below
3622 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003623 */
3624static PyObject *_PyPopenProcs = NULL;
3625
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003626
3627/* popen that works from a GUI.
3628 *
3629 * The result of this function is a pipe (file) connected to the
3630 * processes stdin or stdout, depending on the requested mode.
3631 */
3632
3633static PyObject *
3634posix_popen(PyObject *self, PyObject *args)
3635{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003636 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003637 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003638
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003639 char *cmdstring;
3640 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003641 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003642 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003643 return NULL;
3644
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003645 if (*mode == 'r')
3646 tm = _O_RDONLY;
3647 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003648 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003649 return NULL;
3650 } else
3651 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003652
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003653 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003654 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003655 return NULL;
3656 }
3657
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003658 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003659 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003660 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003661 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003662 else
3663 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
3664
3665 return f;
3666}
3667
3668/* Variation on win32pipe.popen
3669 *
3670 * The result of this function is a pipe (file) connected to the
3671 * process's stdin, and a pipe connected to the process's stdout.
3672 */
3673
3674static PyObject *
3675win32_popen2(PyObject *self, PyObject *args)
3676{
3677 PyObject *f;
3678 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00003679
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003680 char *cmdstring;
3681 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003682 int bufsize = -1;
3683 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003684 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003685
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003686 if (*mode == 't')
3687 tm = _O_TEXT;
3688 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003689 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003690 return NULL;
3691 } else
3692 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003693
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003694 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003695 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003696 return NULL;
3697 }
3698
3699 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00003700
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003701 return f;
3702}
3703
3704/*
3705 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003706 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003707 * The result of this function is 3 pipes - the process's stdin,
3708 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003709 */
3710
3711static PyObject *
3712win32_popen3(PyObject *self, PyObject *args)
3713{
3714 PyObject *f;
3715 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003716
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003717 char *cmdstring;
3718 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003719 int bufsize = -1;
3720 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003721 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003722
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003723 if (*mode == 't')
3724 tm = _O_TEXT;
3725 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003726 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003727 return NULL;
3728 } else
3729 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003730
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003731 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003732 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003733 return NULL;
3734 }
3735
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003736 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00003737
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003738 return f;
3739}
3740
3741/*
3742 * Variation on win32pipe.popen
3743 *
Tim Peters5aa91602002-01-30 05:46:57 +00003744 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003745 * and stdout+stderr combined as a single pipe.
3746 */
3747
3748static PyObject *
3749win32_popen4(PyObject *self, PyObject *args)
3750{
3751 PyObject *f;
3752 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003753
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003754 char *cmdstring;
3755 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003756 int bufsize = -1;
3757 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003758 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003759
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003760 if (*mode == 't')
3761 tm = _O_TEXT;
3762 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003763 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003764 return NULL;
3765 } else
3766 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003767
3768 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003769 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003770 return NULL;
3771 }
3772
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003773 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003774
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003775 return f;
3776}
3777
Mark Hammond08501372001-01-31 07:30:29 +00003778static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00003779_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003780 HANDLE hStdin,
3781 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00003782 HANDLE hStderr,
3783 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003784{
3785 PROCESS_INFORMATION piProcInfo;
3786 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003787 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003788 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00003789 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003790 int i;
3791 int x;
3792
3793 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00003794 char *comshell;
3795
Tim Peters92e4dd82002-10-05 01:47:34 +00003796 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003797 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
3798 return x;
Tim Peters402d5982001-08-27 06:37:48 +00003799
3800 /* Explicitly check if we are using COMMAND.COM. If we are
3801 * then use the w9xpopen hack.
3802 */
3803 comshell = s1 + x;
3804 while (comshell >= s1 && *comshell != '\\')
3805 --comshell;
3806 ++comshell;
3807
3808 if (GetVersion() < 0x80000000 &&
3809 _stricmp(comshell, "command.com") != 0) {
3810 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003811 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00003812 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003813 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00003814 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003815 }
3816 else {
3817 /*
Tim Peters402d5982001-08-27 06:37:48 +00003818 * Oh gag, we're on Win9x or using COMMAND.COM. Use
3819 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003820 */
Mark Hammond08501372001-01-31 07:30:29 +00003821 char modulepath[_MAX_PATH];
3822 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003823 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
3824 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003825 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003826 x = i+1;
3827 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00003828 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00003829 strncat(modulepath,
3830 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003831 (sizeof(modulepath)/sizeof(modulepath[0]))
3832 -strlen(modulepath));
3833 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003834 /* Eeek - file-not-found - possibly an embedding
3835 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00003836 */
Tim Peters5aa91602002-01-30 05:46:57 +00003837 strncpy(modulepath,
3838 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00003839 sizeof(modulepath)/sizeof(modulepath[0]));
3840 if (modulepath[strlen(modulepath)-1] != '\\')
3841 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00003842 strncat(modulepath,
3843 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003844 (sizeof(modulepath)/sizeof(modulepath[0]))
3845 -strlen(modulepath));
3846 /* No where else to look - raise an easily identifiable
3847 error, rather than leaving Windows to report
3848 "file not found" - as the user is probably blissfully
3849 unaware this shim EXE is used, and it will confuse them.
3850 (well, it confused me for a while ;-)
3851 */
3852 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003853 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00003854 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00003855 "for popen to work with your shell "
3856 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00003857 szConsoleSpawn);
3858 return FALSE;
3859 }
3860 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003861 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00003862 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003863 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00003864
Tim Peters92e4dd82002-10-05 01:47:34 +00003865 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003866 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003867 /* To maintain correct argument passing semantics,
3868 we pass the command-line as it stands, and allow
3869 quoting to be applied. w9xpopen.exe will then
3870 use its argv vector, and re-quote the necessary
3871 args for the ultimate child process.
3872 */
Tim Peters75cdad52001-11-28 22:07:30 +00003873 PyOS_snprintf(
3874 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003875 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003876 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003877 s1,
3878 s3,
3879 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003880 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00003881 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003882 dialog:
3883 "Your program accessed mem currently in use at xxx"
3884 and a hopeful warning about the stability of your
3885 system.
3886 Cost is Ctrl+C wont kill children, but anyone
3887 who cares can have a go!
3888 */
3889 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003890 }
3891 }
3892
3893 /* Could be an else here to try cmd.exe / command.com in the path
3894 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00003895 else {
Tim Peters402d5982001-08-27 06:37:48 +00003896 PyErr_SetString(PyExc_RuntimeError,
3897 "Cannot locate a COMSPEC environment variable to "
3898 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00003899 return FALSE;
3900 }
Tim Peters5aa91602002-01-30 05:46:57 +00003901
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003902 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
3903 siStartInfo.cb = sizeof(STARTUPINFO);
3904 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
3905 siStartInfo.hStdInput = hStdin;
3906 siStartInfo.hStdOutput = hStdout;
3907 siStartInfo.hStdError = hStderr;
3908 siStartInfo.wShowWindow = SW_HIDE;
3909
3910 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003911 s2,
3912 NULL,
3913 NULL,
3914 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003915 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003916 NULL,
3917 NULL,
3918 &siStartInfo,
3919 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003920 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003921 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003922
Mark Hammondb37a3732000-08-14 04:47:33 +00003923 /* Return process handle */
3924 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003925 return TRUE;
3926 }
Tim Peters402d5982001-08-27 06:37:48 +00003927 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003928 return FALSE;
3929}
3930
3931/* The following code is based off of KB: Q190351 */
3932
3933static PyObject *
3934_PyPopen(char *cmdstring, int mode, int n)
3935{
3936 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
3937 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00003938 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00003939
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003940 SECURITY_ATTRIBUTES saAttr;
3941 BOOL fSuccess;
3942 int fd1, fd2, fd3;
3943 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00003944 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003945 PyObject *f;
3946
3947 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
3948 saAttr.bInheritHandle = TRUE;
3949 saAttr.lpSecurityDescriptor = NULL;
3950
3951 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
3952 return win32_error("CreatePipe", NULL);
3953
3954 /* Create new output read handle and the input write handle. Set
3955 * the inheritance properties to FALSE. Otherwise, the child inherits
3956 * the these handles; resulting in non-closeable handles to the pipes
3957 * being created. */
3958 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003959 GetCurrentProcess(), &hChildStdinWrDup, 0,
3960 FALSE,
3961 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003962 if (!fSuccess)
3963 return win32_error("DuplicateHandle", NULL);
3964
3965 /* Close the inheritable version of ChildStdin
3966 that we're using. */
3967 CloseHandle(hChildStdinWr);
3968
3969 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
3970 return win32_error("CreatePipe", NULL);
3971
3972 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003973 GetCurrentProcess(), &hChildStdoutRdDup, 0,
3974 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003975 if (!fSuccess)
3976 return win32_error("DuplicateHandle", NULL);
3977
3978 /* Close the inheritable version of ChildStdout
3979 that we're using. */
3980 CloseHandle(hChildStdoutRd);
3981
3982 if (n != POPEN_4) {
3983 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
3984 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003985 fSuccess = DuplicateHandle(GetCurrentProcess(),
3986 hChildStderrRd,
3987 GetCurrentProcess(),
3988 &hChildStderrRdDup, 0,
3989 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003990 if (!fSuccess)
3991 return win32_error("DuplicateHandle", NULL);
3992 /* Close the inheritable version of ChildStdErr that we're using. */
3993 CloseHandle(hChildStderrRd);
3994 }
Tim Peters5aa91602002-01-30 05:46:57 +00003995
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003996 switch (n) {
3997 case POPEN_1:
3998 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
3999 case _O_WRONLY | _O_TEXT:
4000 /* Case for writing to child Stdin in text mode. */
4001 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4002 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004003 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004004 PyFile_SetBufSize(f, 0);
4005 /* We don't care about these pipes anymore, so close them. */
4006 CloseHandle(hChildStdoutRdDup);
4007 CloseHandle(hChildStderrRdDup);
4008 break;
4009
4010 case _O_RDONLY | _O_TEXT:
4011 /* Case for reading from child Stdout in text mode. */
4012 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4013 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004014 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004015 PyFile_SetBufSize(f, 0);
4016 /* We don't care about these pipes anymore, so close them. */
4017 CloseHandle(hChildStdinWrDup);
4018 CloseHandle(hChildStderrRdDup);
4019 break;
4020
4021 case _O_RDONLY | _O_BINARY:
4022 /* Case for readinig from child Stdout in binary mode. */
4023 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4024 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004025 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004026 PyFile_SetBufSize(f, 0);
4027 /* We don't care about these pipes anymore, so close them. */
4028 CloseHandle(hChildStdinWrDup);
4029 CloseHandle(hChildStderrRdDup);
4030 break;
4031
4032 case _O_WRONLY | _O_BINARY:
4033 /* Case for writing to child Stdin in binary mode. */
4034 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4035 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004036 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004037 PyFile_SetBufSize(f, 0);
4038 /* We don't care about these pipes anymore, so close them. */
4039 CloseHandle(hChildStdoutRdDup);
4040 CloseHandle(hChildStderrRdDup);
4041 break;
4042 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004043 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004044 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004045
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004046 case POPEN_2:
4047 case POPEN_4:
4048 {
4049 char *m1, *m2;
4050 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004051
Tim Peters7dca21e2002-08-19 00:42:29 +00004052 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004053 m1 = "r";
4054 m2 = "w";
4055 } else {
4056 m1 = "rb";
4057 m2 = "wb";
4058 }
4059
4060 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4061 f1 = _fdopen(fd1, m2);
4062 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4063 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004064 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004065 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004066 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004067 PyFile_SetBufSize(p2, 0);
4068
4069 if (n != 4)
4070 CloseHandle(hChildStderrRdDup);
4071
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004072 f = Py_BuildValue("OO",p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004073 Py_XDECREF(p1);
4074 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004075 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004076 break;
4077 }
Tim Peters5aa91602002-01-30 05:46:57 +00004078
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004079 case POPEN_3:
4080 {
4081 char *m1, *m2;
4082 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004083
Tim Peters7dca21e2002-08-19 00:42:29 +00004084 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004085 m1 = "r";
4086 m2 = "w";
4087 } else {
4088 m1 = "rb";
4089 m2 = "wb";
4090 }
4091
4092 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4093 f1 = _fdopen(fd1, m2);
4094 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4095 f2 = _fdopen(fd2, m1);
4096 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4097 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004098 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004099 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4100 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004101 PyFile_SetBufSize(p1, 0);
4102 PyFile_SetBufSize(p2, 0);
4103 PyFile_SetBufSize(p3, 0);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004104 f = Py_BuildValue("OOO",p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004105 Py_XDECREF(p1);
4106 Py_XDECREF(p2);
4107 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004108 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004109 break;
4110 }
4111 }
4112
4113 if (n == POPEN_4) {
4114 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004115 hChildStdinRd,
4116 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004117 hChildStdoutWr,
4118 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004119 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004120 }
4121 else {
4122 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004123 hChildStdinRd,
4124 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004125 hChildStderrWr,
4126 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004127 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004128 }
4129
Mark Hammondb37a3732000-08-14 04:47:33 +00004130 /*
4131 * Insert the files we've created into the process dictionary
4132 * all referencing the list with the process handle and the
4133 * initial number of files (see description below in _PyPclose).
4134 * Since if _PyPclose later tried to wait on a process when all
4135 * handles weren't closed, it could create a deadlock with the
4136 * child, we spend some energy here to try to ensure that we
4137 * either insert all file handles into the dictionary or none
4138 * at all. It's a little clumsy with the various popen modes
4139 * and variable number of files involved.
4140 */
4141 if (!_PyPopenProcs) {
4142 _PyPopenProcs = PyDict_New();
4143 }
4144
4145 if (_PyPopenProcs) {
4146 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4147 int ins_rc[3];
4148
4149 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4150 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4151
4152 procObj = PyList_New(2);
4153 hProcessObj = PyLong_FromVoidPtr(hProcess);
4154 intObj = PyInt_FromLong(file_count);
4155
4156 if (procObj && hProcessObj && intObj) {
4157 PyList_SetItem(procObj,0,hProcessObj);
4158 PyList_SetItem(procObj,1,intObj);
4159
4160 fileObj[0] = PyLong_FromVoidPtr(f1);
4161 if (fileObj[0]) {
4162 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4163 fileObj[0],
4164 procObj);
4165 }
4166 if (file_count >= 2) {
4167 fileObj[1] = PyLong_FromVoidPtr(f2);
4168 if (fileObj[1]) {
4169 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4170 fileObj[1],
4171 procObj);
4172 }
4173 }
4174 if (file_count >= 3) {
4175 fileObj[2] = PyLong_FromVoidPtr(f3);
4176 if (fileObj[2]) {
4177 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4178 fileObj[2],
4179 procObj);
4180 }
4181 }
4182
4183 if (ins_rc[0] < 0 || !fileObj[0] ||
4184 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4185 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4186 /* Something failed - remove any dictionary
4187 * entries that did make it.
4188 */
4189 if (!ins_rc[0] && fileObj[0]) {
4190 PyDict_DelItem(_PyPopenProcs,
4191 fileObj[0]);
4192 }
4193 if (!ins_rc[1] && fileObj[1]) {
4194 PyDict_DelItem(_PyPopenProcs,
4195 fileObj[1]);
4196 }
4197 if (!ins_rc[2] && fileObj[2]) {
4198 PyDict_DelItem(_PyPopenProcs,
4199 fileObj[2]);
4200 }
4201 }
4202 }
Tim Peters5aa91602002-01-30 05:46:57 +00004203
Mark Hammondb37a3732000-08-14 04:47:33 +00004204 /*
4205 * Clean up our localized references for the dictionary keys
4206 * and value since PyDict_SetItem will Py_INCREF any copies
4207 * that got placed in the dictionary.
4208 */
4209 Py_XDECREF(procObj);
4210 Py_XDECREF(fileObj[0]);
4211 Py_XDECREF(fileObj[1]);
4212 Py_XDECREF(fileObj[2]);
4213 }
4214
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004215 /* Child is launched. Close the parents copy of those pipe
4216 * handles that only the child should have open. You need to
4217 * make sure that no handles to the write end of the output pipe
4218 * are maintained in this process or else the pipe will not close
4219 * when the child process exits and the ReadFile will hang. */
4220
4221 if (!CloseHandle(hChildStdinRd))
4222 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004223
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004224 if (!CloseHandle(hChildStdoutWr))
4225 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004226
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004227 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4228 return win32_error("CloseHandle", NULL);
4229
4230 return f;
4231}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004232
4233/*
4234 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4235 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004236 *
4237 * This function uses the _PyPopenProcs dictionary in order to map the
4238 * input file pointer to information about the process that was
4239 * originally created by the popen* call that created the file pointer.
4240 * The dictionary uses the file pointer as a key (with one entry
4241 * inserted for each file returned by the original popen* call) and a
4242 * single list object as the value for all files from a single call.
4243 * The list object contains the Win32 process handle at [0], and a file
4244 * count at [1], which is initialized to the total number of file
4245 * handles using that list.
4246 *
4247 * This function closes whichever handle it is passed, and decrements
4248 * the file count in the dictionary for the process handle pointed to
4249 * by this file. On the last close (when the file count reaches zero),
4250 * this function will wait for the child process and then return its
4251 * exit code as the result of the close() operation. This permits the
4252 * files to be closed in any order - it is always the close() of the
4253 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004254 *
4255 * NOTE: This function is currently called with the GIL released.
4256 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004257 */
Tim Peters736aa322000-09-01 06:51:24 +00004258
Fredrik Lundh56055a42000-07-23 19:47:12 +00004259static int _PyPclose(FILE *file)
4260{
Fredrik Lundh20318932000-07-26 17:29:12 +00004261 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004262 DWORD exit_code;
4263 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004264 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4265 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004266#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004267 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004268#endif
4269
Fredrik Lundh20318932000-07-26 17:29:12 +00004270 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004271 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004272 */
4273 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004274#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004275 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004276#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004277 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004278 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4279 (procObj = PyDict_GetItem(_PyPopenProcs,
4280 fileObj)) != NULL &&
4281 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4282 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4283
4284 hProcess = PyLong_AsVoidPtr(hProcessObj);
4285 file_count = PyInt_AsLong(intObj);
4286
4287 if (file_count > 1) {
4288 /* Still other files referencing process */
4289 file_count--;
4290 PyList_SetItem(procObj,1,
4291 PyInt_FromLong(file_count));
4292 } else {
4293 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004294 if (result != EOF &&
4295 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4296 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004297 /* Possible truncation here in 16-bit environments, but
4298 * real exit codes are just the lower byte in any event.
4299 */
4300 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004301 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004302 /* Indicate failure - this will cause the file object
4303 * to raise an I/O error and translate the last Win32
4304 * error code from errno. We do have a problem with
4305 * last errors that overlap the normal errno table,
4306 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004307 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004308 if (result != EOF) {
4309 /* If the error wasn't from the fclose(), then
4310 * set errno for the file object error handling.
4311 */
4312 errno = GetLastError();
4313 }
4314 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004315 }
4316
4317 /* Free up the native handle at this point */
4318 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004319 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004320
Mark Hammondb37a3732000-08-14 04:47:33 +00004321 /* Remove this file pointer from dictionary */
4322 PyDict_DelItem(_PyPopenProcs, fileObj);
4323
4324 if (PyDict_Size(_PyPopenProcs) == 0) {
4325 Py_DECREF(_PyPopenProcs);
4326 _PyPopenProcs = NULL;
4327 }
4328
4329 } /* if object retrieval ok */
4330
4331 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004332 } /* if _PyPopenProcs */
4333
Tim Peters736aa322000-09-01 06:51:24 +00004334#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004335 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004336#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004337 return result;
4338}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004339
4340#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004342posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004343{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004344 char *name;
4345 char *mode = "r";
4346 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004347 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004348 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004349 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004350 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004351 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004352 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004353 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004354 if (fp == NULL)
4355 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004356 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004357 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004358 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004359 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004360}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004361
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004362#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004363#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004364
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004365
Guido van Rossumb6775db1994-08-01 11:34:53 +00004366#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004367PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004368"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004369Set the current process's user id.");
4370
Barry Warsaw53699e91996-12-10 23:23:01 +00004371static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004372posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004373{
4374 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004375 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004376 return NULL;
4377 if (setuid(uid) < 0)
4378 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004379 Py_INCREF(Py_None);
4380 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004381}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004382#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004383
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004384
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004385#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004386PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004387"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004388Set the current process's effective user id.");
4389
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004390static PyObject *
4391posix_seteuid (PyObject *self, PyObject *args)
4392{
4393 int euid;
4394 if (!PyArg_ParseTuple(args, "i", &euid)) {
4395 return NULL;
4396 } else if (seteuid(euid) < 0) {
4397 return posix_error();
4398 } else {
4399 Py_INCREF(Py_None);
4400 return Py_None;
4401 }
4402}
4403#endif /* HAVE_SETEUID */
4404
4405#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004406PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004407"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004408Set the current process's effective group id.");
4409
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004410static PyObject *
4411posix_setegid (PyObject *self, PyObject *args)
4412{
4413 int egid;
4414 if (!PyArg_ParseTuple(args, "i", &egid)) {
4415 return NULL;
4416 } else if (setegid(egid) < 0) {
4417 return posix_error();
4418 } else {
4419 Py_INCREF(Py_None);
4420 return Py_None;
4421 }
4422}
4423#endif /* HAVE_SETEGID */
4424
4425#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004426PyDoc_STRVAR(posix_setreuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004427"seteuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004428Set the current process's real and effective user ids.");
4429
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004430static PyObject *
4431posix_setreuid (PyObject *self, PyObject *args)
4432{
4433 int ruid, euid;
4434 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4435 return NULL;
4436 } else if (setreuid(ruid, euid) < 0) {
4437 return posix_error();
4438 } else {
4439 Py_INCREF(Py_None);
4440 return Py_None;
4441 }
4442}
4443#endif /* HAVE_SETREUID */
4444
4445#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004446PyDoc_STRVAR(posix_setregid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004447"setegid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004448Set the current process's real and effective group ids.");
4449
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004450static PyObject *
4451posix_setregid (PyObject *self, PyObject *args)
4452{
4453 int rgid, egid;
4454 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4455 return NULL;
4456 } else if (setregid(rgid, egid) < 0) {
4457 return posix_error();
4458 } else {
4459 Py_INCREF(Py_None);
4460 return Py_None;
4461 }
4462}
4463#endif /* HAVE_SETREGID */
4464
Guido van Rossumb6775db1994-08-01 11:34:53 +00004465#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004466PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004467"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004468Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004469
Barry Warsaw53699e91996-12-10 23:23:01 +00004470static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004471posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004472{
4473 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004474 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004475 return NULL;
4476 if (setgid(gid) < 0)
4477 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004478 Py_INCREF(Py_None);
4479 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004480}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004481#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004482
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004483#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004484PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004485"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004486Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004487
4488static PyObject *
4489posix_setgroups(PyObject *self, PyObject *args)
4490{
4491 PyObject *groups;
4492 int i, len;
4493 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004494
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004495 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4496 return NULL;
4497 if (!PySequence_Check(groups)) {
4498 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4499 return NULL;
4500 }
4501 len = PySequence_Size(groups);
4502 if (len > MAX_GROUPS) {
4503 PyErr_SetString(PyExc_ValueError, "too many groups");
4504 return NULL;
4505 }
4506 for(i = 0; i < len; i++) {
4507 PyObject *elem;
4508 elem = PySequence_GetItem(groups, i);
4509 if (!elem)
4510 return NULL;
4511 if (!PyInt_Check(elem)) {
4512 PyErr_SetString(PyExc_TypeError,
4513 "groups must be integers");
4514 Py_DECREF(elem);
4515 return NULL;
4516 }
4517 /* XXX: check that value fits into gid_t. */
4518 grouplist[i] = PyInt_AsLong(elem);
4519 Py_DECREF(elem);
4520 }
4521
4522 if (setgroups(len, grouplist) < 0)
4523 return posix_error();
4524 Py_INCREF(Py_None);
4525 return Py_None;
4526}
4527#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004528
Guido van Rossumb6775db1994-08-01 11:34:53 +00004529#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004530PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004531"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004532Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004533
Barry Warsaw53699e91996-12-10 23:23:01 +00004534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004535posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004536{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004537 int pid, options;
4538#ifdef UNION_WAIT
4539 union wait status;
4540#define status_i (status.w_status)
4541#else
4542 int status;
4543#define status_i status
4544#endif
4545 status_i = 0;
4546
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004547 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004548 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004549 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004550 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004551 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004552 if (pid == -1)
4553 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004554 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004555 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004556}
4557
Tim Petersab034fa2002-02-01 11:27:43 +00004558#elif defined(HAVE_CWAIT)
4559
4560/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004561PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004562"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004563"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004564
4565static PyObject *
4566posix_waitpid(PyObject *self, PyObject *args)
4567{
4568 int pid, options;
4569 int status;
4570
4571 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4572 return NULL;
4573 Py_BEGIN_ALLOW_THREADS
4574 pid = _cwait(&status, pid, options);
4575 Py_END_ALLOW_THREADS
4576 if (pid == -1)
4577 return posix_error();
4578 else
4579 /* shift the status left a byte so this is more like the
4580 POSIX waitpid */
4581 return Py_BuildValue("ii", pid, status << 8);
4582}
4583#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004584
Guido van Rossumad0ee831995-03-01 10:34:45 +00004585#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004586PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004587"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004588Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004589
Barry Warsaw53699e91996-12-10 23:23:01 +00004590static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004591posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004592{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004593 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004594#ifdef UNION_WAIT
4595 union wait status;
4596#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004597#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004598 int status;
4599#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004600#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004601
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004602 status_i = 0;
4603 Py_BEGIN_ALLOW_THREADS
4604 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004605 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004606 if (pid == -1)
4607 return posix_error();
4608 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004609 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004610#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004611}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004612#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004613
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004615PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004616"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004617Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004618
Barry Warsaw53699e91996-12-10 23:23:01 +00004619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004620posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004621{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004622#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004623 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004624#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004625#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004626 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004627#else
4628 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4629#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004630#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004631}
4632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004633
Guido van Rossumb6775db1994-08-01 11:34:53 +00004634#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004635PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004636"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004637Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004638
Barry Warsaw53699e91996-12-10 23:23:01 +00004639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004640posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004641{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004642 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004643 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004644 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004645 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004646 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004647 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004648 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004649 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004650 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004651 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004652 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004653}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004654#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004655
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004656
Guido van Rossumb6775db1994-08-01 11:34:53 +00004657#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004658PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004659"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004660Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004661
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004662static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004663posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004664{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004665 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004666}
4667#endif /* HAVE_SYMLINK */
4668
4669
4670#ifdef HAVE_TIMES
4671#ifndef HZ
4672#define HZ 60 /* Universal constant :-) */
4673#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004674
Guido van Rossumd48f2521997-12-05 22:19:34 +00004675#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4676static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004677system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004678{
4679 ULONG value = 0;
4680
4681 Py_BEGIN_ALLOW_THREADS
4682 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4683 Py_END_ALLOW_THREADS
4684
4685 return value;
4686}
4687
4688static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004689posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004690{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004691 /* Currently Only Uptime is Provided -- Others Later */
4692 return Py_BuildValue("ddddd",
4693 (double)0 /* t.tms_utime / HZ */,
4694 (double)0 /* t.tms_stime / HZ */,
4695 (double)0 /* t.tms_cutime / HZ */,
4696 (double)0 /* t.tms_cstime / HZ */,
4697 (double)system_uptime() / 1000);
4698}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004699#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004700static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004701posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004702{
4703 struct tms t;
4704 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004705 errno = 0;
4706 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004707 if (c == (clock_t) -1)
4708 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004709 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004710 (double)t.tms_utime / HZ,
4711 (double)t.tms_stime / HZ,
4712 (double)t.tms_cutime / HZ,
4713 (double)t.tms_cstime / HZ,
4714 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004715}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004716#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004717#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004718
4719
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004720#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004721#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004722static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004723posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004724{
4725 FILETIME create, exit, kernel, user;
4726 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004727 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004728 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4729 /* The fields of a FILETIME structure are the hi and lo part
4730 of a 64-bit value expressed in 100 nanosecond units.
4731 1e7 is one second in such units; 1e-7 the inverse.
4732 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4733 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004734 return Py_BuildValue(
4735 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004736 (double)(kernel.dwHighDateTime*429.4967296 +
4737 kernel.dwLowDateTime*1e-7),
4738 (double)(user.dwHighDateTime*429.4967296 +
4739 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004740 (double)0,
4741 (double)0,
4742 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004743}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004744#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004745
4746#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004747PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004748"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004749Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004750#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004752
Guido van Rossumb6775db1994-08-01 11:34:53 +00004753#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004754PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004755"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004756Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004757
Barry Warsaw53699e91996-12-10 23:23:01 +00004758static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004759posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004760{
Guido van Rossum687dd131993-05-17 08:34:16 +00004761 if (setsid() < 0)
4762 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004763 Py_INCREF(Py_None);
4764 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004765}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004766#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004767
Guido van Rossumb6775db1994-08-01 11:34:53 +00004768#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004769PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004770"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004771Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004772
Barry Warsaw53699e91996-12-10 23:23:01 +00004773static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004774posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004775{
4776 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004777 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004778 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004779 if (setpgid(pid, pgrp) < 0)
4780 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004781 Py_INCREF(Py_None);
4782 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004783}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004784#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004785
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004786
Guido van Rossumb6775db1994-08-01 11:34:53 +00004787#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004788PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004789"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004790Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004791
Barry Warsaw53699e91996-12-10 23:23:01 +00004792static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004793posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004794{
4795 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004796 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004797 return NULL;
4798 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004799 if (pgid < 0)
4800 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004801 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004802}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004803#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004805
Guido van Rossumb6775db1994-08-01 11:34:53 +00004806#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004807PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004808"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004809Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004810
Barry Warsaw53699e91996-12-10 23:23:01 +00004811static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004812posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004813{
4814 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004815 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004816 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004817 if (tcsetpgrp(fd, pgid) < 0)
4818 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004819 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004820 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004821}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004822#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004823
Guido van Rossum687dd131993-05-17 08:34:16 +00004824/* Functions acting on file descriptors */
4825
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004826PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004827"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004828Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004829
Barry Warsaw53699e91996-12-10 23:23:01 +00004830static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004831posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004832{
Mark Hammondef8b6542001-05-13 08:04:26 +00004833 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004834 int flag;
4835 int mode = 0777;
4836 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004837
4838#ifdef MS_WINDOWS
4839 if (unicode_file_names()) {
4840 PyUnicodeObject *po;
4841 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4842 Py_BEGIN_ALLOW_THREADS
4843 /* PyUnicode_AS_UNICODE OK without thread
4844 lock as it is a simple dereference. */
4845 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4846 Py_END_ALLOW_THREADS
4847 if (fd < 0)
4848 return posix_error();
4849 return PyInt_FromLong((long)fd);
4850 }
4851 /* Drop the argument parsing error as narrow strings
4852 are also valid. */
4853 PyErr_Clear();
4854 }
4855#endif
4856
Tim Peters5aa91602002-01-30 05:46:57 +00004857 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004858 Py_FileSystemDefaultEncoding, &file,
4859 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004860 return NULL;
4861
Barry Warsaw53699e91996-12-10 23:23:01 +00004862 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004863 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004864 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004865 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004866 return posix_error_with_allocated_filename(file);
4867 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004868 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004869}
4870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004872PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004873"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004874Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004875
Barry Warsaw53699e91996-12-10 23:23:01 +00004876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004877posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004878{
4879 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004880 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004881 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004882 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004883 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004884 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004885 if (res < 0)
4886 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004887 Py_INCREF(Py_None);
4888 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004889}
4890
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004892PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004893"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004894Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004895
Barry Warsaw53699e91996-12-10 23:23:01 +00004896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004897posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004898{
4899 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004900 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004901 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004902 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004903 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004904 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004905 if (fd < 0)
4906 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004907 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004908}
4909
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004911PyDoc_STRVAR(posix_dup2__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004912"dup2(fd, fd2)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004913Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004914
Barry Warsaw53699e91996-12-10 23:23:01 +00004915static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004916posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004917{
4918 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004919 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004920 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004921 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004922 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004923 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004924 if (res < 0)
4925 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004926 Py_INCREF(Py_None);
4927 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004928}
4929
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004930
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004931PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004932"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004933Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004934
Barry Warsaw53699e91996-12-10 23:23:01 +00004935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004936posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004937{
4938 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004939#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00004940 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004941#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00004942 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004943#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00004944 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004945 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00004946 return NULL;
4947#ifdef SEEK_SET
4948 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4949 switch (how) {
4950 case 0: how = SEEK_SET; break;
4951 case 1: how = SEEK_CUR; break;
4952 case 2: how = SEEK_END; break;
4953 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004954#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004955
4956#if !defined(HAVE_LARGEFILE_SUPPORT)
4957 pos = PyInt_AsLong(posobj);
4958#else
4959 pos = PyLong_Check(posobj) ?
4960 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
4961#endif
4962 if (PyErr_Occurred())
4963 return NULL;
4964
Barry Warsaw53699e91996-12-10 23:23:01 +00004965 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004966#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00004967 res = _lseeki64(fd, pos, how);
4968#else
Guido van Rossum687dd131993-05-17 08:34:16 +00004969 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004970#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00004971 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004972 if (res < 0)
4973 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004974
4975#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00004976 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004977#else
4978 return PyLong_FromLongLong(res);
4979#endif
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_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004984"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004985Read a 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_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004989{
Guido van Rossum8bac5461996-06-11 18:38:48 +00004990 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00004991 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004992 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00004993 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004994 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00004995 if (buffer == NULL)
4996 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004997 Py_BEGIN_ALLOW_THREADS
4998 n = read(fd, PyString_AsString(buffer), size);
4999 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005000 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005001 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005002 return posix_error();
5003 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005004 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005005 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005006 return buffer;
5007}
5008
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005010PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005011"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005012Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005013
Barry Warsaw53699e91996-12-10 23:23:01 +00005014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005015posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005016{
5017 int fd, size;
5018 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005019 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005020 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005021 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005022 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005023 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005024 if (size < 0)
5025 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005026 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005027}
5028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005030PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005031"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005032Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005033
Barry Warsaw53699e91996-12-10 23:23:01 +00005034static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005035posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005036{
5037 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005038 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005039 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005040 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005041 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005042#ifdef __VMS
5043 /* on OpenVMS we must ensure that all bytes are written to the file */
5044 fsync(fd);
5045#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005046 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005047 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005048 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005049 if (res != 0)
5050 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005051
Fred Drake699f3522000-06-29 21:12:41 +00005052 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005053}
5054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005056PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005057"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005058Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005059
Barry Warsaw53699e91996-12-10 23:23:01 +00005060static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005061posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005062{
Guido van Rossum687dd131993-05-17 08:34:16 +00005063 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005064 char *mode = "r";
5065 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005066 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005067 PyObject *f;
5068 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005069 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005070
Thomas Heller1f043e22002-11-07 16:00:59 +00005071 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5072 PyErr_Format(PyExc_ValueError,
5073 "invalid file mode '%s'", mode);
5074 return NULL;
5075 }
5076
Barry Warsaw53699e91996-12-10 23:23:01 +00005077 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005078 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005079 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005080 if (fp == NULL)
5081 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005082 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005083 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005084 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005085 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005086}
5087
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005088PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005089"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005090Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005091connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005092
5093static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005094posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005095{
5096 int fd;
5097 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5098 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005099 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005100}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005101
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005102#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005103PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005104"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005105Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005106
Barry Warsaw53699e91996-12-10 23:23:01 +00005107static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005108posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005109{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005110#if defined(PYOS_OS2)
5111 HFILE read, write;
5112 APIRET rc;
5113
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005114 Py_BEGIN_ALLOW_THREADS
5115 rc = DosCreatePipe( &read, &write, 4096);
5116 Py_END_ALLOW_THREADS
5117 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005118 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005119
5120 return Py_BuildValue("(ii)", read, write);
5121#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005122#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005123 int fds[2];
5124 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005125 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005126 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005127 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005128 if (res != 0)
5129 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005130 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005131#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005132 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005133 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005134 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005135 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005136 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005137 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005138 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005139 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005140 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5141 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005142 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005143#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005144#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005145}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005146#endif /* HAVE_PIPE */
5147
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005148
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005149#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005150PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005151"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005152Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005153
Barry Warsaw53699e91996-12-10 23:23:01 +00005154static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005155posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005156{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005157 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005158 int mode = 0666;
5159 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005160 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005161 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005162 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005163 res = mkfifo(filename, mode);
5164 Py_END_ALLOW_THREADS
5165 if (res < 0)
5166 return posix_error();
5167 Py_INCREF(Py_None);
5168 return Py_None;
5169}
5170#endif
5171
5172
Neal Norwitz11690112002-07-30 01:08:28 +00005173#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005174PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005175"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005176Create a filesystem node (file, device special file or named pipe)\n\
5177named filename. mode specifies both the permissions to use and the\n\
5178type of node to be created, being combined (bitwise OR) with one of\n\
5179S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005180device defines the newly created device special file (probably using\n\
5181os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005182
5183
5184static PyObject *
5185posix_mknod(PyObject *self, PyObject *args)
5186{
5187 char *filename;
5188 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005189 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005190 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005191 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005192 return NULL;
5193 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005194 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005195 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005196 if (res < 0)
5197 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005198 Py_INCREF(Py_None);
5199 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005200}
5201#endif
5202
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005203#ifdef HAVE_DEVICE_MACROS
5204PyDoc_STRVAR(posix_major__doc__,
5205"major(device) -> major number\n\
5206Extracts a device major number from a raw device number.");
5207
5208static PyObject *
5209posix_major(PyObject *self, PyObject *args)
5210{
5211 int device;
5212 if (!PyArg_ParseTuple(args, "i:major", &device))
5213 return NULL;
5214 return PyInt_FromLong((long)major(device));
5215}
5216
5217PyDoc_STRVAR(posix_minor__doc__,
5218"minor(device) -> minor number\n\
5219Extracts a device minor number from a raw device number.");
5220
5221static PyObject *
5222posix_minor(PyObject *self, PyObject *args)
5223{
5224 int device;
5225 if (!PyArg_ParseTuple(args, "i:minor", &device))
5226 return NULL;
5227 return PyInt_FromLong((long)minor(device));
5228}
5229
5230PyDoc_STRVAR(posix_makedev__doc__,
5231"makedev(major, minor) -> device number\n\
5232Composes a raw device number from the major and minor device numbers.");
5233
5234static PyObject *
5235posix_makedev(PyObject *self, PyObject *args)
5236{
5237 int major, minor;
5238 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5239 return NULL;
5240 return PyInt_FromLong((long)makedev(major, minor));
5241}
5242#endif /* device macros */
5243
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005244
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005245#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005246PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005247"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005248Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005249
Barry Warsaw53699e91996-12-10 23:23:01 +00005250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005251posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005252{
5253 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005254 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005255 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005256 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005257
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005258 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005259 return NULL;
5260
5261#if !defined(HAVE_LARGEFILE_SUPPORT)
5262 length = PyInt_AsLong(lenobj);
5263#else
5264 length = PyLong_Check(lenobj) ?
5265 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5266#endif
5267 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005268 return NULL;
5269
Barry Warsaw53699e91996-12-10 23:23:01 +00005270 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005271 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005272 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005273 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005274 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005275 return NULL;
5276 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005277 Py_INCREF(Py_None);
5278 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005279}
5280#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005281
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005282#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005283PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005284"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005285Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005286
Fred Drake762e2061999-08-26 17:23:54 +00005287/* Save putenv() parameters as values here, so we can collect them when they
5288 * get re-set with another call for the same key. */
5289static PyObject *posix_putenv_garbage;
5290
Tim Peters5aa91602002-01-30 05:46:57 +00005291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005292posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005293{
5294 char *s1, *s2;
5295 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005296 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005297 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005298
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005299 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005300 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005301
5302#if defined(PYOS_OS2)
5303 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5304 APIRET rc;
5305
Guido van Rossumd48f2521997-12-05 22:19:34 +00005306 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5307 if (rc != NO_ERROR)
5308 return os2_error(rc);
5309
5310 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5311 APIRET rc;
5312
Guido van Rossumd48f2521997-12-05 22:19:34 +00005313 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5314 if (rc != NO_ERROR)
5315 return os2_error(rc);
5316 } else {
5317#endif
5318
Fred Drake762e2061999-08-26 17:23:54 +00005319 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005320 len = strlen(s1) + strlen(s2) + 2;
5321 /* len includes space for a trailing \0; the size arg to
5322 PyString_FromStringAndSize does not count that */
5323 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005324 if (newstr == NULL)
5325 return PyErr_NoMemory();
5326 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005327 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005328 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005329 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005330 posix_error();
5331 return NULL;
5332 }
Fred Drake762e2061999-08-26 17:23:54 +00005333 /* Install the first arg and newstr in posix_putenv_garbage;
5334 * this will cause previous value to be collected. This has to
5335 * happen after the real putenv() call because the old value
5336 * was still accessible until then. */
5337 if (PyDict_SetItem(posix_putenv_garbage,
5338 PyTuple_GET_ITEM(args, 0), newstr)) {
5339 /* really not much we can do; just leak */
5340 PyErr_Clear();
5341 }
5342 else {
5343 Py_DECREF(newstr);
5344 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005345
5346#if defined(PYOS_OS2)
5347 }
5348#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005349 Py_INCREF(Py_None);
5350 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005351}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005352#endif /* putenv */
5353
Guido van Rossumc524d952001-10-19 01:31:59 +00005354#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005355PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005356"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005358
5359static PyObject *
5360posix_unsetenv(PyObject *self, PyObject *args)
5361{
5362 char *s1;
5363
5364 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5365 return NULL;
5366
5367 unsetenv(s1);
5368
5369 /* Remove the key from posix_putenv_garbage;
5370 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005371 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005372 * old value was still accessible until then.
5373 */
5374 if (PyDict_DelItem(posix_putenv_garbage,
5375 PyTuple_GET_ITEM(args, 0))) {
5376 /* really not much we can do; just leak */
5377 PyErr_Clear();
5378 }
5379
5380 Py_INCREF(Py_None);
5381 return Py_None;
5382}
5383#endif /* unsetenv */
5384
Guido van Rossumb6a47161997-09-15 22:54:34 +00005385#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005387"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005388Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005389
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005390static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005391posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005392{
5393 int code;
5394 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005395 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005396 return NULL;
5397 message = strerror(code);
5398 if (message == NULL) {
5399 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005400 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005401 return NULL;
5402 }
5403 return PyString_FromString(message);
5404}
5405#endif /* strerror */
5406
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005407
Guido van Rossumc9641791998-08-04 15:26:23 +00005408#ifdef HAVE_SYS_WAIT_H
5409
Fred Drake106c1a02002-04-23 15:58:02 +00005410#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005412"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005413Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005414
5415static PyObject *
5416posix_WCOREDUMP(PyObject *self, PyObject *args)
5417{
5418#ifdef UNION_WAIT
5419 union wait status;
5420#define status_i (status.w_status)
5421#else
5422 int status;
5423#define status_i status
5424#endif
5425 status_i = 0;
5426
5427 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5428 {
5429 return NULL;
5430 }
5431
5432 return PyBool_FromLong(WCOREDUMP(status));
5433#undef status_i
5434}
5435#endif /* WCOREDUMP */
5436
5437#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005438PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005439"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005440Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005441job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005442
5443static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005444posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005445{
5446#ifdef UNION_WAIT
5447 union wait status;
5448#define status_i (status.w_status)
5449#else
5450 int status;
5451#define status_i status
5452#endif
5453 status_i = 0;
5454
5455 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5456 {
5457 return NULL;
5458 }
5459
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005460 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005461#undef status_i
5462}
5463#endif /* WIFCONTINUED */
5464
Guido van Rossumc9641791998-08-04 15:26:23 +00005465#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005466PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005467"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005468Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005469
5470static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005471posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005472{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005473#ifdef UNION_WAIT
5474 union wait status;
5475#define status_i (status.w_status)
5476#else
5477 int status;
5478#define status_i status
5479#endif
5480 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005481
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005482 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005483 {
5484 return NULL;
5485 }
Tim Peters5aa91602002-01-30 05:46:57 +00005486
Fred Drake106c1a02002-04-23 15:58:02 +00005487 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005488#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005489}
5490#endif /* WIFSTOPPED */
5491
5492#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005493PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005494"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005495Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005496
5497static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005498posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005499{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005500#ifdef UNION_WAIT
5501 union wait status;
5502#define status_i (status.w_status)
5503#else
5504 int status;
5505#define status_i status
5506#endif
5507 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005508
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005509 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005510 {
5511 return NULL;
5512 }
Tim Peters5aa91602002-01-30 05:46:57 +00005513
Fred Drake106c1a02002-04-23 15:58:02 +00005514 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005515#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005516}
5517#endif /* WIFSIGNALED */
5518
5519#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005520PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005521"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005522Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005523system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005524
5525static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005526posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005527{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005528#ifdef UNION_WAIT
5529 union wait status;
5530#define status_i (status.w_status)
5531#else
5532 int status;
5533#define status_i status
5534#endif
5535 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005536
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005537 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005538 {
5539 return NULL;
5540 }
Tim Peters5aa91602002-01-30 05:46:57 +00005541
Fred Drake106c1a02002-04-23 15:58:02 +00005542 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005543#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005544}
5545#endif /* WIFEXITED */
5546
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005547#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005548PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005549"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005550Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005551
5552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005553posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005554{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005555#ifdef UNION_WAIT
5556 union wait status;
5557#define status_i (status.w_status)
5558#else
5559 int status;
5560#define status_i status
5561#endif
5562 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005563
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005564 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005565 {
5566 return NULL;
5567 }
Tim Peters5aa91602002-01-30 05:46:57 +00005568
Guido van Rossumc9641791998-08-04 15:26:23 +00005569 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005570#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005571}
5572#endif /* WEXITSTATUS */
5573
5574#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005575PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005576"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005577Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005579
5580static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005581posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005582{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005583#ifdef UNION_WAIT
5584 union wait status;
5585#define status_i (status.w_status)
5586#else
5587 int status;
5588#define status_i status
5589#endif
5590 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005591
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005592 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005593 {
5594 return NULL;
5595 }
Tim Peters5aa91602002-01-30 05:46:57 +00005596
Guido van Rossumc9641791998-08-04 15:26:23 +00005597 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005598#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005599}
5600#endif /* WTERMSIG */
5601
5602#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005604"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005605Return the signal that stopped the process that provided\n\
5606the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005607
5608static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005609posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005610{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005611#ifdef UNION_WAIT
5612 union wait status;
5613#define status_i (status.w_status)
5614#else
5615 int status;
5616#define status_i status
5617#endif
5618 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005619
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005620 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005621 {
5622 return NULL;
5623 }
Tim Peters5aa91602002-01-30 05:46:57 +00005624
Guido van Rossumc9641791998-08-04 15:26:23 +00005625 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005626#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005627}
5628#endif /* WSTOPSIG */
5629
5630#endif /* HAVE_SYS_WAIT_H */
5631
5632
Guido van Rossum94f6f721999-01-06 18:42:14 +00005633#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005634#ifdef _SCO_DS
5635/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5636 needed definitions in sys/statvfs.h */
5637#define _SVID3
5638#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005639#include <sys/statvfs.h>
5640
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005641static PyObject*
5642_pystatvfs_fromstructstatvfs(struct statvfs st) {
5643 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5644 if (v == NULL)
5645 return NULL;
5646
5647#if !defined(HAVE_LARGEFILE_SUPPORT)
5648 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5649 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5650 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5651 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5652 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5653 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5654 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5655 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5656 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5657 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5658#else
5659 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5660 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005661 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005662 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005663 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005664 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005665 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005666 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005667 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005668 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005669 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005670 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005671 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005672 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005673 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5674 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5675#endif
5676
5677 return v;
5678}
5679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005680PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005681"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005682Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005683
5684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005685posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005686{
5687 int fd, res;
5688 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005689
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005690 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005691 return NULL;
5692 Py_BEGIN_ALLOW_THREADS
5693 res = fstatvfs(fd, &st);
5694 Py_END_ALLOW_THREADS
5695 if (res != 0)
5696 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005697
5698 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005699}
5700#endif /* HAVE_FSTATVFS */
5701
5702
5703#if defined(HAVE_STATVFS)
5704#include <sys/statvfs.h>
5705
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005706PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005707"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005708Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005709
5710static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005711posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005712{
5713 char *path;
5714 int res;
5715 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005716 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005717 return NULL;
5718 Py_BEGIN_ALLOW_THREADS
5719 res = statvfs(path, &st);
5720 Py_END_ALLOW_THREADS
5721 if (res != 0)
5722 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005723
5724 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005725}
5726#endif /* HAVE_STATVFS */
5727
5728
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005729#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005730PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005731"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005732Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005733The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005734or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005735
5736static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005737posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005738{
5739 PyObject *result = NULL;
5740 char *dir = NULL;
5741 char *pfx = NULL;
5742 char *name;
5743
5744 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5745 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005746
5747 if (PyErr_Warn(PyExc_RuntimeWarning,
5748 "tempnam is a potential security risk to your program") < 0)
5749 return NULL;
5750
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005751#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005752 name = _tempnam(dir, pfx);
5753#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005754 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005755#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005756 if (name == NULL)
5757 return PyErr_NoMemory();
5758 result = PyString_FromString(name);
5759 free(name);
5760 return result;
5761}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005762#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005763
5764
5765#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005766PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005767"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005768Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005769
5770static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005771posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005772{
5773 FILE *fp;
5774
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005775 fp = tmpfile();
5776 if (fp == NULL)
5777 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005778 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005779}
5780#endif
5781
5782
5783#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005784PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005785"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005786Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005787
5788static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005789posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005790{
5791 char buffer[L_tmpnam];
5792 char *name;
5793
Skip Montanaro95618b52001-08-18 18:52:10 +00005794 if (PyErr_Warn(PyExc_RuntimeWarning,
5795 "tmpnam is a potential security risk to your program") < 0)
5796 return NULL;
5797
Greg Wardb48bc172000-03-01 21:51:56 +00005798#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005799 name = tmpnam_r(buffer);
5800#else
5801 name = tmpnam(buffer);
5802#endif
5803 if (name == NULL) {
5804 PyErr_SetObject(PyExc_OSError,
5805 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005806#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005807 "unexpected NULL from tmpnam_r"
5808#else
5809 "unexpected NULL from tmpnam"
5810#endif
5811 ));
5812 return NULL;
5813 }
5814 return PyString_FromString(buffer);
5815}
5816#endif
5817
5818
Fred Drakec9680921999-12-13 16:37:25 +00005819/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5820 * It maps strings representing configuration variable names to
5821 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005822 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005823 * rarely-used constants. There are three separate tables that use
5824 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005825 *
5826 * This code is always included, even if none of the interfaces that
5827 * need it are included. The #if hackery needed to avoid it would be
5828 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005829 */
5830struct constdef {
5831 char *name;
5832 long value;
5833};
5834
Fred Drake12c6e2d1999-12-14 21:25:03 +00005835static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005836conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5837 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005838{
5839 if (PyInt_Check(arg)) {
5840 *valuep = PyInt_AS_LONG(arg);
5841 return 1;
5842 }
5843 if (PyString_Check(arg)) {
5844 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005845 size_t lo = 0;
5846 size_t mid;
5847 size_t hi = tablesize;
5848 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005849 char *confname = PyString_AS_STRING(arg);
5850 while (lo < hi) {
5851 mid = (lo + hi) / 2;
5852 cmp = strcmp(confname, table[mid].name);
5853 if (cmp < 0)
5854 hi = mid;
5855 else if (cmp > 0)
5856 lo = mid + 1;
5857 else {
5858 *valuep = table[mid].value;
5859 return 1;
5860 }
5861 }
5862 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5863 }
5864 else
5865 PyErr_SetString(PyExc_TypeError,
5866 "configuration names must be strings or integers");
5867 return 0;
5868}
5869
5870
5871#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5872static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005873#ifdef _PC_ABI_AIO_XFER_MAX
5874 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5875#endif
5876#ifdef _PC_ABI_ASYNC_IO
5877 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5878#endif
Fred Drakec9680921999-12-13 16:37:25 +00005879#ifdef _PC_ASYNC_IO
5880 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5881#endif
5882#ifdef _PC_CHOWN_RESTRICTED
5883 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5884#endif
5885#ifdef _PC_FILESIZEBITS
5886 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5887#endif
5888#ifdef _PC_LAST
5889 {"PC_LAST", _PC_LAST},
5890#endif
5891#ifdef _PC_LINK_MAX
5892 {"PC_LINK_MAX", _PC_LINK_MAX},
5893#endif
5894#ifdef _PC_MAX_CANON
5895 {"PC_MAX_CANON", _PC_MAX_CANON},
5896#endif
5897#ifdef _PC_MAX_INPUT
5898 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5899#endif
5900#ifdef _PC_NAME_MAX
5901 {"PC_NAME_MAX", _PC_NAME_MAX},
5902#endif
5903#ifdef _PC_NO_TRUNC
5904 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5905#endif
5906#ifdef _PC_PATH_MAX
5907 {"PC_PATH_MAX", _PC_PATH_MAX},
5908#endif
5909#ifdef _PC_PIPE_BUF
5910 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5911#endif
5912#ifdef _PC_PRIO_IO
5913 {"PC_PRIO_IO", _PC_PRIO_IO},
5914#endif
5915#ifdef _PC_SOCK_MAXBUF
5916 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5917#endif
5918#ifdef _PC_SYNC_IO
5919 {"PC_SYNC_IO", _PC_SYNC_IO},
5920#endif
5921#ifdef _PC_VDISABLE
5922 {"PC_VDISABLE", _PC_VDISABLE},
5923#endif
5924};
5925
Fred Drakec9680921999-12-13 16:37:25 +00005926static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005927conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005928{
5929 return conv_confname(arg, valuep, posix_constants_pathconf,
5930 sizeof(posix_constants_pathconf)
5931 / sizeof(struct constdef));
5932}
5933#endif
5934
5935#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005936PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005937"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005938Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005939If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005940
5941static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005942posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005943{
5944 PyObject *result = NULL;
5945 int name, fd;
5946
Fred Drake12c6e2d1999-12-14 21:25:03 +00005947 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5948 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00005949 long limit;
5950
5951 errno = 0;
5952 limit = fpathconf(fd, name);
5953 if (limit == -1 && errno != 0)
5954 posix_error();
5955 else
5956 result = PyInt_FromLong(limit);
5957 }
5958 return result;
5959}
5960#endif
5961
5962
5963#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005964PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005965"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005966Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005967If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005968
5969static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005970posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005971{
5972 PyObject *result = NULL;
5973 int name;
5974 char *path;
5975
5976 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5977 conv_path_confname, &name)) {
5978 long limit;
5979
5980 errno = 0;
5981 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005982 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00005983 if (errno == EINVAL)
5984 /* could be a path or name problem */
5985 posix_error();
5986 else
5987 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00005988 }
Fred Drakec9680921999-12-13 16:37:25 +00005989 else
5990 result = PyInt_FromLong(limit);
5991 }
5992 return result;
5993}
5994#endif
5995
5996#ifdef HAVE_CONFSTR
5997static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005998#ifdef _CS_ARCHITECTURE
5999 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6000#endif
6001#ifdef _CS_HOSTNAME
6002 {"CS_HOSTNAME", _CS_HOSTNAME},
6003#endif
6004#ifdef _CS_HW_PROVIDER
6005 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6006#endif
6007#ifdef _CS_HW_SERIAL
6008 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6009#endif
6010#ifdef _CS_INITTAB_NAME
6011 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6012#endif
Fred Drakec9680921999-12-13 16:37:25 +00006013#ifdef _CS_LFS64_CFLAGS
6014 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6015#endif
6016#ifdef _CS_LFS64_LDFLAGS
6017 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6018#endif
6019#ifdef _CS_LFS64_LIBS
6020 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6021#endif
6022#ifdef _CS_LFS64_LINTFLAGS
6023 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6024#endif
6025#ifdef _CS_LFS_CFLAGS
6026 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6027#endif
6028#ifdef _CS_LFS_LDFLAGS
6029 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6030#endif
6031#ifdef _CS_LFS_LIBS
6032 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6033#endif
6034#ifdef _CS_LFS_LINTFLAGS
6035 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6036#endif
Fred Draked86ed291999-12-15 15:34:33 +00006037#ifdef _CS_MACHINE
6038 {"CS_MACHINE", _CS_MACHINE},
6039#endif
Fred Drakec9680921999-12-13 16:37:25 +00006040#ifdef _CS_PATH
6041 {"CS_PATH", _CS_PATH},
6042#endif
Fred Draked86ed291999-12-15 15:34:33 +00006043#ifdef _CS_RELEASE
6044 {"CS_RELEASE", _CS_RELEASE},
6045#endif
6046#ifdef _CS_SRPC_DOMAIN
6047 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6048#endif
6049#ifdef _CS_SYSNAME
6050 {"CS_SYSNAME", _CS_SYSNAME},
6051#endif
6052#ifdef _CS_VERSION
6053 {"CS_VERSION", _CS_VERSION},
6054#endif
Fred Drakec9680921999-12-13 16:37:25 +00006055#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6056 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6057#endif
6058#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6059 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6060#endif
6061#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6062 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6063#endif
6064#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6065 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6066#endif
6067#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6068 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6069#endif
6070#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6071 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6072#endif
6073#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6074 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6075#endif
6076#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6077 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6078#endif
6079#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6080 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6081#endif
6082#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6083 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6084#endif
6085#ifdef _CS_XBS5_LP64_OFF64_LIBS
6086 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6087#endif
6088#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6089 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6090#endif
6091#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6092 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6093#endif
6094#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6095 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6096#endif
6097#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6098 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6099#endif
6100#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6101 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6102#endif
Fred Draked86ed291999-12-15 15:34:33 +00006103#ifdef _MIPS_CS_AVAIL_PROCESSORS
6104 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6105#endif
6106#ifdef _MIPS_CS_BASE
6107 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6108#endif
6109#ifdef _MIPS_CS_HOSTID
6110 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6111#endif
6112#ifdef _MIPS_CS_HW_NAME
6113 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6114#endif
6115#ifdef _MIPS_CS_NUM_PROCESSORS
6116 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6117#endif
6118#ifdef _MIPS_CS_OSREL_MAJ
6119 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6120#endif
6121#ifdef _MIPS_CS_OSREL_MIN
6122 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6123#endif
6124#ifdef _MIPS_CS_OSREL_PATCH
6125 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6126#endif
6127#ifdef _MIPS_CS_OS_NAME
6128 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6129#endif
6130#ifdef _MIPS_CS_OS_PROVIDER
6131 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6132#endif
6133#ifdef _MIPS_CS_PROCESSORS
6134 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6135#endif
6136#ifdef _MIPS_CS_SERIAL
6137 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6138#endif
6139#ifdef _MIPS_CS_VENDOR
6140 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6141#endif
Fred Drakec9680921999-12-13 16:37:25 +00006142};
6143
6144static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006145conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006146{
6147 return conv_confname(arg, valuep, posix_constants_confstr,
6148 sizeof(posix_constants_confstr)
6149 / sizeof(struct constdef));
6150}
6151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006152PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006153"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006154Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006155
6156static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006157posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006158{
6159 PyObject *result = NULL;
6160 int name;
6161 char buffer[64];
6162
6163 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6164 int len = confstr(name, buffer, sizeof(buffer));
6165
Fred Drakec9680921999-12-13 16:37:25 +00006166 errno = 0;
6167 if (len == 0) {
6168 if (errno != 0)
6169 posix_error();
6170 else
6171 result = PyString_FromString("");
6172 }
6173 else {
6174 if (len >= sizeof(buffer)) {
6175 result = PyString_FromStringAndSize(NULL, len);
6176 if (result != NULL)
6177 confstr(name, PyString_AS_STRING(result), len+1);
6178 }
6179 else
6180 result = PyString_FromString(buffer);
6181 }
6182 }
6183 return result;
6184}
6185#endif
6186
6187
6188#ifdef HAVE_SYSCONF
6189static struct constdef posix_constants_sysconf[] = {
6190#ifdef _SC_2_CHAR_TERM
6191 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6192#endif
6193#ifdef _SC_2_C_BIND
6194 {"SC_2_C_BIND", _SC_2_C_BIND},
6195#endif
6196#ifdef _SC_2_C_DEV
6197 {"SC_2_C_DEV", _SC_2_C_DEV},
6198#endif
6199#ifdef _SC_2_C_VERSION
6200 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6201#endif
6202#ifdef _SC_2_FORT_DEV
6203 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6204#endif
6205#ifdef _SC_2_FORT_RUN
6206 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6207#endif
6208#ifdef _SC_2_LOCALEDEF
6209 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6210#endif
6211#ifdef _SC_2_SW_DEV
6212 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6213#endif
6214#ifdef _SC_2_UPE
6215 {"SC_2_UPE", _SC_2_UPE},
6216#endif
6217#ifdef _SC_2_VERSION
6218 {"SC_2_VERSION", _SC_2_VERSION},
6219#endif
Fred Draked86ed291999-12-15 15:34:33 +00006220#ifdef _SC_ABI_ASYNCHRONOUS_IO
6221 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6222#endif
6223#ifdef _SC_ACL
6224 {"SC_ACL", _SC_ACL},
6225#endif
Fred Drakec9680921999-12-13 16:37:25 +00006226#ifdef _SC_AIO_LISTIO_MAX
6227 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6228#endif
Fred Drakec9680921999-12-13 16:37:25 +00006229#ifdef _SC_AIO_MAX
6230 {"SC_AIO_MAX", _SC_AIO_MAX},
6231#endif
6232#ifdef _SC_AIO_PRIO_DELTA_MAX
6233 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6234#endif
6235#ifdef _SC_ARG_MAX
6236 {"SC_ARG_MAX", _SC_ARG_MAX},
6237#endif
6238#ifdef _SC_ASYNCHRONOUS_IO
6239 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6240#endif
6241#ifdef _SC_ATEXIT_MAX
6242 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6243#endif
Fred Draked86ed291999-12-15 15:34:33 +00006244#ifdef _SC_AUDIT
6245 {"SC_AUDIT", _SC_AUDIT},
6246#endif
Fred Drakec9680921999-12-13 16:37:25 +00006247#ifdef _SC_AVPHYS_PAGES
6248 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6249#endif
6250#ifdef _SC_BC_BASE_MAX
6251 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6252#endif
6253#ifdef _SC_BC_DIM_MAX
6254 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6255#endif
6256#ifdef _SC_BC_SCALE_MAX
6257 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6258#endif
6259#ifdef _SC_BC_STRING_MAX
6260 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6261#endif
Fred Draked86ed291999-12-15 15:34:33 +00006262#ifdef _SC_CAP
6263 {"SC_CAP", _SC_CAP},
6264#endif
Fred Drakec9680921999-12-13 16:37:25 +00006265#ifdef _SC_CHARCLASS_NAME_MAX
6266 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6267#endif
6268#ifdef _SC_CHAR_BIT
6269 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6270#endif
6271#ifdef _SC_CHAR_MAX
6272 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6273#endif
6274#ifdef _SC_CHAR_MIN
6275 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6276#endif
6277#ifdef _SC_CHILD_MAX
6278 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6279#endif
6280#ifdef _SC_CLK_TCK
6281 {"SC_CLK_TCK", _SC_CLK_TCK},
6282#endif
6283#ifdef _SC_COHER_BLKSZ
6284 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6285#endif
6286#ifdef _SC_COLL_WEIGHTS_MAX
6287 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6288#endif
6289#ifdef _SC_DCACHE_ASSOC
6290 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6291#endif
6292#ifdef _SC_DCACHE_BLKSZ
6293 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6294#endif
6295#ifdef _SC_DCACHE_LINESZ
6296 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6297#endif
6298#ifdef _SC_DCACHE_SZ
6299 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6300#endif
6301#ifdef _SC_DCACHE_TBLKSZ
6302 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6303#endif
6304#ifdef _SC_DELAYTIMER_MAX
6305 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6306#endif
6307#ifdef _SC_EQUIV_CLASS_MAX
6308 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6309#endif
6310#ifdef _SC_EXPR_NEST_MAX
6311 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6312#endif
6313#ifdef _SC_FSYNC
6314 {"SC_FSYNC", _SC_FSYNC},
6315#endif
6316#ifdef _SC_GETGR_R_SIZE_MAX
6317 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6318#endif
6319#ifdef _SC_GETPW_R_SIZE_MAX
6320 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6321#endif
6322#ifdef _SC_ICACHE_ASSOC
6323 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6324#endif
6325#ifdef _SC_ICACHE_BLKSZ
6326 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6327#endif
6328#ifdef _SC_ICACHE_LINESZ
6329 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6330#endif
6331#ifdef _SC_ICACHE_SZ
6332 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6333#endif
Fred Draked86ed291999-12-15 15:34:33 +00006334#ifdef _SC_INF
6335 {"SC_INF", _SC_INF},
6336#endif
Fred Drakec9680921999-12-13 16:37:25 +00006337#ifdef _SC_INT_MAX
6338 {"SC_INT_MAX", _SC_INT_MAX},
6339#endif
6340#ifdef _SC_INT_MIN
6341 {"SC_INT_MIN", _SC_INT_MIN},
6342#endif
6343#ifdef _SC_IOV_MAX
6344 {"SC_IOV_MAX", _SC_IOV_MAX},
6345#endif
Fred Draked86ed291999-12-15 15:34:33 +00006346#ifdef _SC_IP_SECOPTS
6347 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6348#endif
Fred Drakec9680921999-12-13 16:37:25 +00006349#ifdef _SC_JOB_CONTROL
6350 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6351#endif
Fred Draked86ed291999-12-15 15:34:33 +00006352#ifdef _SC_KERN_POINTERS
6353 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6354#endif
6355#ifdef _SC_KERN_SIM
6356 {"SC_KERN_SIM", _SC_KERN_SIM},
6357#endif
Fred Drakec9680921999-12-13 16:37:25 +00006358#ifdef _SC_LINE_MAX
6359 {"SC_LINE_MAX", _SC_LINE_MAX},
6360#endif
6361#ifdef _SC_LOGIN_NAME_MAX
6362 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6363#endif
6364#ifdef _SC_LOGNAME_MAX
6365 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6366#endif
6367#ifdef _SC_LONG_BIT
6368 {"SC_LONG_BIT", _SC_LONG_BIT},
6369#endif
Fred Draked86ed291999-12-15 15:34:33 +00006370#ifdef _SC_MAC
6371 {"SC_MAC", _SC_MAC},
6372#endif
Fred Drakec9680921999-12-13 16:37:25 +00006373#ifdef _SC_MAPPED_FILES
6374 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6375#endif
6376#ifdef _SC_MAXPID
6377 {"SC_MAXPID", _SC_MAXPID},
6378#endif
6379#ifdef _SC_MB_LEN_MAX
6380 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6381#endif
6382#ifdef _SC_MEMLOCK
6383 {"SC_MEMLOCK", _SC_MEMLOCK},
6384#endif
6385#ifdef _SC_MEMLOCK_RANGE
6386 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6387#endif
6388#ifdef _SC_MEMORY_PROTECTION
6389 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6390#endif
6391#ifdef _SC_MESSAGE_PASSING
6392 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6393#endif
Fred Draked86ed291999-12-15 15:34:33 +00006394#ifdef _SC_MMAP_FIXED_ALIGNMENT
6395 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6396#endif
Fred Drakec9680921999-12-13 16:37:25 +00006397#ifdef _SC_MQ_OPEN_MAX
6398 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6399#endif
6400#ifdef _SC_MQ_PRIO_MAX
6401 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6402#endif
Fred Draked86ed291999-12-15 15:34:33 +00006403#ifdef _SC_NACLS_MAX
6404 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6405#endif
Fred Drakec9680921999-12-13 16:37:25 +00006406#ifdef _SC_NGROUPS_MAX
6407 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6408#endif
6409#ifdef _SC_NL_ARGMAX
6410 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6411#endif
6412#ifdef _SC_NL_LANGMAX
6413 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6414#endif
6415#ifdef _SC_NL_MSGMAX
6416 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6417#endif
6418#ifdef _SC_NL_NMAX
6419 {"SC_NL_NMAX", _SC_NL_NMAX},
6420#endif
6421#ifdef _SC_NL_SETMAX
6422 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6423#endif
6424#ifdef _SC_NL_TEXTMAX
6425 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6426#endif
6427#ifdef _SC_NPROCESSORS_CONF
6428 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6429#endif
6430#ifdef _SC_NPROCESSORS_ONLN
6431 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6432#endif
Fred Draked86ed291999-12-15 15:34:33 +00006433#ifdef _SC_NPROC_CONF
6434 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6435#endif
6436#ifdef _SC_NPROC_ONLN
6437 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6438#endif
Fred Drakec9680921999-12-13 16:37:25 +00006439#ifdef _SC_NZERO
6440 {"SC_NZERO", _SC_NZERO},
6441#endif
6442#ifdef _SC_OPEN_MAX
6443 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6444#endif
6445#ifdef _SC_PAGESIZE
6446 {"SC_PAGESIZE", _SC_PAGESIZE},
6447#endif
6448#ifdef _SC_PAGE_SIZE
6449 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6450#endif
6451#ifdef _SC_PASS_MAX
6452 {"SC_PASS_MAX", _SC_PASS_MAX},
6453#endif
6454#ifdef _SC_PHYS_PAGES
6455 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6456#endif
6457#ifdef _SC_PII
6458 {"SC_PII", _SC_PII},
6459#endif
6460#ifdef _SC_PII_INTERNET
6461 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6462#endif
6463#ifdef _SC_PII_INTERNET_DGRAM
6464 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6465#endif
6466#ifdef _SC_PII_INTERNET_STREAM
6467 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6468#endif
6469#ifdef _SC_PII_OSI
6470 {"SC_PII_OSI", _SC_PII_OSI},
6471#endif
6472#ifdef _SC_PII_OSI_CLTS
6473 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6474#endif
6475#ifdef _SC_PII_OSI_COTS
6476 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6477#endif
6478#ifdef _SC_PII_OSI_M
6479 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6480#endif
6481#ifdef _SC_PII_SOCKET
6482 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6483#endif
6484#ifdef _SC_PII_XTI
6485 {"SC_PII_XTI", _SC_PII_XTI},
6486#endif
6487#ifdef _SC_POLL
6488 {"SC_POLL", _SC_POLL},
6489#endif
6490#ifdef _SC_PRIORITIZED_IO
6491 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6492#endif
6493#ifdef _SC_PRIORITY_SCHEDULING
6494 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6495#endif
6496#ifdef _SC_REALTIME_SIGNALS
6497 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6498#endif
6499#ifdef _SC_RE_DUP_MAX
6500 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6501#endif
6502#ifdef _SC_RTSIG_MAX
6503 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6504#endif
6505#ifdef _SC_SAVED_IDS
6506 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6507#endif
6508#ifdef _SC_SCHAR_MAX
6509 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6510#endif
6511#ifdef _SC_SCHAR_MIN
6512 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6513#endif
6514#ifdef _SC_SELECT
6515 {"SC_SELECT", _SC_SELECT},
6516#endif
6517#ifdef _SC_SEMAPHORES
6518 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6519#endif
6520#ifdef _SC_SEM_NSEMS_MAX
6521 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6522#endif
6523#ifdef _SC_SEM_VALUE_MAX
6524 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6525#endif
6526#ifdef _SC_SHARED_MEMORY_OBJECTS
6527 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6528#endif
6529#ifdef _SC_SHRT_MAX
6530 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6531#endif
6532#ifdef _SC_SHRT_MIN
6533 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6534#endif
6535#ifdef _SC_SIGQUEUE_MAX
6536 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6537#endif
6538#ifdef _SC_SIGRT_MAX
6539 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6540#endif
6541#ifdef _SC_SIGRT_MIN
6542 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6543#endif
Fred Draked86ed291999-12-15 15:34:33 +00006544#ifdef _SC_SOFTPOWER
6545 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6546#endif
Fred Drakec9680921999-12-13 16:37:25 +00006547#ifdef _SC_SPLIT_CACHE
6548 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6549#endif
6550#ifdef _SC_SSIZE_MAX
6551 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6552#endif
6553#ifdef _SC_STACK_PROT
6554 {"SC_STACK_PROT", _SC_STACK_PROT},
6555#endif
6556#ifdef _SC_STREAM_MAX
6557 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6558#endif
6559#ifdef _SC_SYNCHRONIZED_IO
6560 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6561#endif
6562#ifdef _SC_THREADS
6563 {"SC_THREADS", _SC_THREADS},
6564#endif
6565#ifdef _SC_THREAD_ATTR_STACKADDR
6566 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6567#endif
6568#ifdef _SC_THREAD_ATTR_STACKSIZE
6569 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6570#endif
6571#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6572 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6573#endif
6574#ifdef _SC_THREAD_KEYS_MAX
6575 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6576#endif
6577#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6578 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6579#endif
6580#ifdef _SC_THREAD_PRIO_INHERIT
6581 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6582#endif
6583#ifdef _SC_THREAD_PRIO_PROTECT
6584 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6585#endif
6586#ifdef _SC_THREAD_PROCESS_SHARED
6587 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6588#endif
6589#ifdef _SC_THREAD_SAFE_FUNCTIONS
6590 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6591#endif
6592#ifdef _SC_THREAD_STACK_MIN
6593 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6594#endif
6595#ifdef _SC_THREAD_THREADS_MAX
6596 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6597#endif
6598#ifdef _SC_TIMERS
6599 {"SC_TIMERS", _SC_TIMERS},
6600#endif
6601#ifdef _SC_TIMER_MAX
6602 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6603#endif
6604#ifdef _SC_TTY_NAME_MAX
6605 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6606#endif
6607#ifdef _SC_TZNAME_MAX
6608 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6609#endif
6610#ifdef _SC_T_IOV_MAX
6611 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6612#endif
6613#ifdef _SC_UCHAR_MAX
6614 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6615#endif
6616#ifdef _SC_UINT_MAX
6617 {"SC_UINT_MAX", _SC_UINT_MAX},
6618#endif
6619#ifdef _SC_UIO_MAXIOV
6620 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6621#endif
6622#ifdef _SC_ULONG_MAX
6623 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6624#endif
6625#ifdef _SC_USHRT_MAX
6626 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6627#endif
6628#ifdef _SC_VERSION
6629 {"SC_VERSION", _SC_VERSION},
6630#endif
6631#ifdef _SC_WORD_BIT
6632 {"SC_WORD_BIT", _SC_WORD_BIT},
6633#endif
6634#ifdef _SC_XBS5_ILP32_OFF32
6635 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6636#endif
6637#ifdef _SC_XBS5_ILP32_OFFBIG
6638 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6639#endif
6640#ifdef _SC_XBS5_LP64_OFF64
6641 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6642#endif
6643#ifdef _SC_XBS5_LPBIG_OFFBIG
6644 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6645#endif
6646#ifdef _SC_XOPEN_CRYPT
6647 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6648#endif
6649#ifdef _SC_XOPEN_ENH_I18N
6650 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6651#endif
6652#ifdef _SC_XOPEN_LEGACY
6653 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6654#endif
6655#ifdef _SC_XOPEN_REALTIME
6656 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6657#endif
6658#ifdef _SC_XOPEN_REALTIME_THREADS
6659 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6660#endif
6661#ifdef _SC_XOPEN_SHM
6662 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6663#endif
6664#ifdef _SC_XOPEN_UNIX
6665 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6666#endif
6667#ifdef _SC_XOPEN_VERSION
6668 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6669#endif
6670#ifdef _SC_XOPEN_XCU_VERSION
6671 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6672#endif
6673#ifdef _SC_XOPEN_XPG2
6674 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6675#endif
6676#ifdef _SC_XOPEN_XPG3
6677 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6678#endif
6679#ifdef _SC_XOPEN_XPG4
6680 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6681#endif
6682};
6683
6684static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006685conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006686{
6687 return conv_confname(arg, valuep, posix_constants_sysconf,
6688 sizeof(posix_constants_sysconf)
6689 / sizeof(struct constdef));
6690}
6691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006692PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006693"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006694Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006695
6696static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006697posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006698{
6699 PyObject *result = NULL;
6700 int name;
6701
6702 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6703 int value;
6704
6705 errno = 0;
6706 value = sysconf(name);
6707 if (value == -1 && errno != 0)
6708 posix_error();
6709 else
6710 result = PyInt_FromLong(value);
6711 }
6712 return result;
6713}
6714#endif
6715
6716
Fred Drakebec628d1999-12-15 18:31:10 +00006717/* This code is used to ensure that the tables of configuration value names
6718 * are in sorted order as required by conv_confname(), and also to build the
6719 * the exported dictionaries that are used to publish information about the
6720 * names available on the host platform.
6721 *
6722 * Sorting the table at runtime ensures that the table is properly ordered
6723 * when used, even for platforms we're not able to test on. It also makes
6724 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006725 */
Fred Drakebec628d1999-12-15 18:31:10 +00006726
6727static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006728cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006729{
6730 const struct constdef *c1 =
6731 (const struct constdef *) v1;
6732 const struct constdef *c2 =
6733 (const struct constdef *) v2;
6734
6735 return strcmp(c1->name, c2->name);
6736}
6737
6738static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006739setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006740 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006741{
Fred Drakebec628d1999-12-15 18:31:10 +00006742 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006743 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006744
6745 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6746 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006747 if (d == NULL)
6748 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006749
Barry Warsaw3155db32000-04-13 15:20:40 +00006750 for (i=0; i < tablesize; ++i) {
6751 PyObject *o = PyInt_FromLong(table[i].value);
6752 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6753 Py_XDECREF(o);
6754 Py_DECREF(d);
6755 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006756 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006757 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006758 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006759 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006760}
6761
Fred Drakebec628d1999-12-15 18:31:10 +00006762/* Return -1 on failure, 0 on success. */
6763static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006764setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006765{
6766#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006767 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006768 sizeof(posix_constants_pathconf)
6769 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006770 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006771 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006772#endif
6773#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006774 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006775 sizeof(posix_constants_confstr)
6776 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006777 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006778 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006779#endif
6780#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006781 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006782 sizeof(posix_constants_sysconf)
6783 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006784 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006785 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006786#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006787 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006788}
Fred Draked86ed291999-12-15 15:34:33 +00006789
6790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006791PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006792"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006793Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006794in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006795
6796static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006797posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006798{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006799 abort();
6800 /*NOTREACHED*/
6801 Py_FatalError("abort() called from Python code didn't abort!");
6802 return NULL;
6803}
Fred Drakebec628d1999-12-15 18:31:10 +00006804
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006805#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006806PyDoc_STRVAR(win32_startfile__doc__,
6807"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006808\n\
6809This acts like double-clicking the file in Explorer, or giving the file\n\
6810name as an argument to the DOS \"start\" command: the file is opened\n\
6811with whatever application (if any) its extension is associated.\n\
6812\n\
6813startfile returns as soon as the associated application is launched.\n\
6814There is no option to wait for the application to close, and no way\n\
6815to retrieve the application's exit status.\n\
6816\n\
6817The filepath is relative to the current directory. If you want to use\n\
6818an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006819the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006820
6821static PyObject *
6822win32_startfile(PyObject *self, PyObject *args)
6823{
6824 char *filepath;
6825 HINSTANCE rc;
6826 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
6827 return NULL;
6828 Py_BEGIN_ALLOW_THREADS
6829 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
6830 Py_END_ALLOW_THREADS
6831 if (rc <= (HINSTANCE)32)
6832 return win32_error("startfile", filepath);
6833 Py_INCREF(Py_None);
6834 return Py_None;
6835}
6836#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006837
Martin v. Löwis438b5342002-12-27 10:16:42 +00006838#ifdef HAVE_GETLOADAVG
6839PyDoc_STRVAR(posix_getloadavg__doc__,
6840"getloadavg() -> (float, float, float)\n\n\
6841Return the number of processes in the system run queue averaged over\n\
6842the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6843was unobtainable");
6844
6845static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006846posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006847{
6848 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006849 if (getloadavg(loadavg, 3)!=3) {
6850 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6851 return NULL;
6852 } else
6853 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6854}
6855#endif
6856
6857
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006858static PyMethodDef posix_methods[] = {
6859 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6860#ifdef HAVE_TTYNAME
6861 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6862#endif
6863 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
6864 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006865#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006866 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006867#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006868#ifdef HAVE_LCHOWN
6869 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6870#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006871#ifdef HAVE_CHROOT
6872 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6873#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006874#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006875 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006876#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006877#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006878 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00006879#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00006880 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006881#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00006882#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006883#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006884 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006885#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006886 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6887 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6888 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006889#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006890 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006891#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006892#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006893 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006894#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006895 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6896 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6897 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006898 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006899#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006900 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006901#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006902#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006903 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006904#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006905 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006906#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006907 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006908#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006909 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6910 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6911 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006912#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006913 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006914#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006915 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006916#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006917 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6918 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006919#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006920#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006921 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6922 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00006923#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006924#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006925 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006926#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006927#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006928 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006929#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006930#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00006931 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006932#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006933#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00006934 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00006935#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006936#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006937 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006938#endif /* HAVE_GETEGID */
6939#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006940 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006941#endif /* HAVE_GETEUID */
6942#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00006943 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006944#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00006945#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00006946 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00006947#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00006948 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006949#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00006950 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006951#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006952#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00006953 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006954#endif /* HAVE_GETPPID */
6955#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00006956 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006957#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006958#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00006959 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00006960#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00006961#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006962 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00006963#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006964#ifdef HAVE_KILLPG
6965 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
6966#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00006967#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006968 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00006969#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006970#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006971 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006972#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006973 {"popen2", win32_popen2, METH_VARARGS},
6974 {"popen3", win32_popen3, METH_VARARGS},
6975 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00006976 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006977#else
6978#if defined(PYOS_OS2) && defined(PYCC_GCC)
6979 {"popen2", os2emx_popen2, METH_VARARGS},
6980 {"popen3", os2emx_popen3, METH_VARARGS},
6981 {"popen4", os2emx_popen4, METH_VARARGS},
6982#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00006983#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006984#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006985#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006986 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006987#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006988#ifdef HAVE_SETEUID
6989 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
6990#endif /* HAVE_SETEUID */
6991#ifdef HAVE_SETEGID
6992 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
6993#endif /* HAVE_SETEGID */
6994#ifdef HAVE_SETREUID
6995 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
6996#endif /* HAVE_SETREUID */
6997#ifdef HAVE_SETREGID
6998 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
6999#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007000#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007001 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007002#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007003#ifdef HAVE_SETGROUPS
7004 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7005#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007006#ifdef HAVE_GETPGID
7007 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7008#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007009#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007010 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007011#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007012#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007013 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007014#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007015#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007016 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007017#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007018#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007019 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007020#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007021#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007022 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007023#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007024#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007025 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007026#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007027#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007028 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007029#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007030 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7031 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7032 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7033 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7034 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7035 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7036 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7037 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7038 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007039 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007040#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007041 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007042#endif
7043#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007044 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007045#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007046#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007047 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7048#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007049#ifdef HAVE_DEVICE_MACROS
7050 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7051 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7052 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7053#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007054#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007055 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007056#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007057#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007058 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007059#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007060#ifdef HAVE_UNSETENV
7061 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7062#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007063#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007064 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007065#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007066#ifdef HAVE_FCHDIR
7067 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7068#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007069#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007070 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007071#endif
7072#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007073 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007074#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007075#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007076#ifdef WCOREDUMP
7077 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7078#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007079#ifdef WIFCONTINUED
7080 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7081#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007082#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007083 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007084#endif /* WIFSTOPPED */
7085#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007086 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007087#endif /* WIFSIGNALED */
7088#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007089 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007090#endif /* WIFEXITED */
7091#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007092 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007093#endif /* WEXITSTATUS */
7094#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007095 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007096#endif /* WTERMSIG */
7097#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007098 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007099#endif /* WSTOPSIG */
7100#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007101#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007102 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007103#endif
7104#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007105 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007106#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007107#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007108 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007109#endif
7110#ifdef HAVE_TEMPNAM
7111 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7112#endif
7113#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007114 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007115#endif
Fred Drakec9680921999-12-13 16:37:25 +00007116#ifdef HAVE_CONFSTR
7117 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7118#endif
7119#ifdef HAVE_SYSCONF
7120 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7121#endif
7122#ifdef HAVE_FPATHCONF
7123 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7124#endif
7125#ifdef HAVE_PATHCONF
7126 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7127#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007128 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007129#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007130 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7131#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007132#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007133 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007134#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007135 {NULL, NULL} /* Sentinel */
7136};
7137
7138
Barry Warsaw4a342091996-12-19 23:50:02 +00007139static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007140ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007141{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007142 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007143}
7144
Guido van Rossumd48f2521997-12-05 22:19:34 +00007145#if defined(PYOS_OS2)
7146/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007147static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007148{
7149 APIRET rc;
7150 ULONG values[QSV_MAX+1];
7151 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007152 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007153
7154 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007155 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007156 Py_END_ALLOW_THREADS
7157
7158 if (rc != NO_ERROR) {
7159 os2_error(rc);
7160 return -1;
7161 }
7162
Fred Drake4d1e64b2002-04-15 19:40:07 +00007163 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7164 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7165 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7166 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7167 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7168 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7169 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007170
7171 switch (values[QSV_VERSION_MINOR]) {
7172 case 0: ver = "2.00"; break;
7173 case 10: ver = "2.10"; break;
7174 case 11: ver = "2.11"; break;
7175 case 30: ver = "3.00"; break;
7176 case 40: ver = "4.00"; break;
7177 case 50: ver = "5.00"; break;
7178 default:
Tim Peters885d4572001-11-28 20:27:42 +00007179 PyOS_snprintf(tmp, sizeof(tmp),
7180 "%d-%d", values[QSV_VERSION_MAJOR],
7181 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007182 ver = &tmp[0];
7183 }
7184
7185 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007186 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007187 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007188
7189 /* Add Indicator of Which Drive was Used to Boot the System */
7190 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7191 tmp[1] = ':';
7192 tmp[2] = '\0';
7193
Fred Drake4d1e64b2002-04-15 19:40:07 +00007194 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007195}
7196#endif
7197
Barry Warsaw4a342091996-12-19 23:50:02 +00007198static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007199all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007200{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007201#ifdef F_OK
7202 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007203#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007204#ifdef R_OK
7205 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007206#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007207#ifdef W_OK
7208 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007209#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007210#ifdef X_OK
7211 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007212#endif
Fred Drakec9680921999-12-13 16:37:25 +00007213#ifdef NGROUPS_MAX
7214 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7215#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007216#ifdef TMP_MAX
7217 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7218#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007219#ifdef WCONTINUED
7220 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7221#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007222#ifdef WNOHANG
7223 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007224#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007225#ifdef WUNTRACED
7226 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7227#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007228#ifdef O_RDONLY
7229 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7230#endif
7231#ifdef O_WRONLY
7232 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7233#endif
7234#ifdef O_RDWR
7235 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7236#endif
7237#ifdef O_NDELAY
7238 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7239#endif
7240#ifdef O_NONBLOCK
7241 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7242#endif
7243#ifdef O_APPEND
7244 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7245#endif
7246#ifdef O_DSYNC
7247 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7248#endif
7249#ifdef O_RSYNC
7250 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7251#endif
7252#ifdef O_SYNC
7253 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7254#endif
7255#ifdef O_NOCTTY
7256 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7257#endif
7258#ifdef O_CREAT
7259 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7260#endif
7261#ifdef O_EXCL
7262 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7263#endif
7264#ifdef O_TRUNC
7265 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7266#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007267#ifdef O_BINARY
7268 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7269#endif
7270#ifdef O_TEXT
7271 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7272#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007273#ifdef O_LARGEFILE
7274 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7275#endif
7276
Tim Peters5aa91602002-01-30 05:46:57 +00007277/* MS Windows */
7278#ifdef O_NOINHERIT
7279 /* Don't inherit in child processes. */
7280 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7281#endif
7282#ifdef _O_SHORT_LIVED
7283 /* Optimize for short life (keep in memory). */
7284 /* MS forgot to define this one with a non-underscore form too. */
7285 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7286#endif
7287#ifdef O_TEMPORARY
7288 /* Automatically delete when last handle is closed. */
7289 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7290#endif
7291#ifdef O_RANDOM
7292 /* Optimize for random access. */
7293 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7294#endif
7295#ifdef O_SEQUENTIAL
7296 /* Optimize for sequential access. */
7297 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7298#endif
7299
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007300/* GNU extensions. */
7301#ifdef O_DIRECT
7302 /* Direct disk access. */
7303 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7304#endif
7305#ifdef O_DIRECTORY
7306 /* Must be a directory. */
7307 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7308#endif
7309#ifdef O_NOFOLLOW
7310 /* Do not follow links. */
7311 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7312#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007313
Barry Warsaw5676bd12003-01-07 20:57:09 +00007314 /* These come from sysexits.h */
7315#ifdef EX_OK
7316 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007317#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007318#ifdef EX_USAGE
7319 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007320#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007321#ifdef EX_DATAERR
7322 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007323#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007324#ifdef EX_NOINPUT
7325 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007326#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007327#ifdef EX_NOUSER
7328 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007329#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007330#ifdef EX_NOHOST
7331 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007332#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007333#ifdef EX_UNAVAILABLE
7334 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007335#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007336#ifdef EX_SOFTWARE
7337 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007338#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007339#ifdef EX_OSERR
7340 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007341#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007342#ifdef EX_OSFILE
7343 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007344#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007345#ifdef EX_CANTCREAT
7346 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007347#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007348#ifdef EX_IOERR
7349 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007350#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007351#ifdef EX_TEMPFAIL
7352 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007353#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007354#ifdef EX_PROTOCOL
7355 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007356#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007357#ifdef EX_NOPERM
7358 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007359#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007360#ifdef EX_CONFIG
7361 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007362#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007363#ifdef EX_NOTFOUND
7364 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007365#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007366
Guido van Rossum246bc171999-02-01 23:54:31 +00007367#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007368#if defined(PYOS_OS2) && defined(PYCC_GCC)
7369 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7370 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7371 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7372 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7373 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7374 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7375 if (ins(d, "P_PM", (long)P_PM)) return -1;
7376 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7377 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7378 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7379 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7380 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7381 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7382 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7383 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7384 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7385 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7386 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7387 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7388 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7389#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007390 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7391 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7392 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7393 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7394 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007395#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007396#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007397
Guido van Rossumd48f2521997-12-05 22:19:34 +00007398#if defined(PYOS_OS2)
7399 if (insertvalues(d)) return -1;
7400#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007401 return 0;
7402}
7403
7404
Tim Peters5aa91602002-01-30 05:46:57 +00007405#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007406#define INITFUNC initnt
7407#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007408
7409#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007410#define INITFUNC initos2
7411#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007412
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007413#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007414#define INITFUNC initposix
7415#define MODNAME "posix"
7416#endif
7417
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007418PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007419INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007420{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007421 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007422
Fred Drake4d1e64b2002-04-15 19:40:07 +00007423 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007424 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007425 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007426
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007427 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007428 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007429 Py_XINCREF(v);
7430 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007431 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007432 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007433
Fred Drake4d1e64b2002-04-15 19:40:07 +00007434 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007435 return;
7436
Fred Drake4d1e64b2002-04-15 19:40:07 +00007437 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007438 return;
7439
Fred Drake4d1e64b2002-04-15 19:40:07 +00007440 Py_INCREF(PyExc_OSError);
7441 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007442
Guido van Rossumb3d39562000-01-31 18:41:26 +00007443#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007444 if (posix_putenv_garbage == NULL)
7445 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007446#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007447
Guido van Rossum14648392001-12-08 18:02:58 +00007448 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007449 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7450 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7451 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007452 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007453 structseq_new = StatResultType.tp_new;
7454 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007455 Py_INCREF((PyObject*) &StatResultType);
7456 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007457
Guido van Rossum14648392001-12-08 18:02:58 +00007458 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007459 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007460 Py_INCREF((PyObject*) &StatVFSResultType);
7461 PyModule_AddObject(m, "statvfs_result",
7462 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007463}