blob: 52977feffcf377b78cb13bb18e612775221df64c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000016#include "Python.h"
17#include "structseq.h"
18
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000019#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000020# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000021#endif /* defined(__VMS) */
22
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000023PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000024"This module provides access to operating system functionality that is\n\
25standardized by the C Standard and the POSIX standard (a thinly\n\
26disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000029#ifndef Py_USING_UNICODE
30/* This is used in signatures of functions. */
31#define Py_UNICODE void
32#endif
33
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000034#if defined(PYOS_OS2)
35#define INCL_DOS
36#define INCL_DOSERRORS
37#define INCL_DOSPROCESS
38#define INCL_NOPMAPI
39#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000040#if defined(PYCC_GCC)
41#include <ctype.h>
42#include <io.h>
43#include <stdio.h>
44#include <process.h>
45#include "osdefs.h"
46#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#endif
48
Guido van Rossumb6775db1994-08-01 11:34:53 +000049#include <sys/types.h>
50#include <sys/stat.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000051
Guido van Rossum36bc6801995-06-14 22:54:23 +000052#ifdef HAVE_SYS_WAIT_H
53#include <sys/wait.h> /* For WNOHANG */
54#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000055
Guido van Rossuma376cc51996-12-05 23:43:35 +000056#ifdef HAVE_SIGNAL_H
57#include <signal.h>
58#endif
59
Guido van Rossumb6775db1994-08-01 11:34:53 +000060#ifdef HAVE_FCNTL_H
61#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000062#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000063
Guido van Rossuma6535fd2001-10-18 19:44:10 +000064#ifdef HAVE_GRP_H
65#include <grp.h>
66#endif
67
Barry Warsaw5676bd12003-01-07 20:57:09 +000068#ifdef HAVE_SYSEXITS_H
69#include <sysexits.h>
70#endif /* HAVE_SYSEXITS_H */
71
Guido van Rossuma4916fa1996-05-23 22:58:55 +000072/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000073/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000074#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000075#include <process.h>
76#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000077#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000078#define HAVE_GETCWD 1
79#define HAVE_OPENDIR 1
80#define HAVE_SYSTEM 1
81#if defined(__OS2__)
82#define HAVE_EXECV 1
83#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000084#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000085#include <process.h>
86#else
87#ifdef __BORLANDC__ /* Borland compiler */
88#define HAVE_EXECV 1
89#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000090#define HAVE_OPENDIR 1
91#define HAVE_PIPE 1
92#define HAVE_POPEN 1
93#define HAVE_SYSTEM 1
94#define HAVE_WAIT 1
95#else
96#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000097#define HAVE_GETCWD 1
Guido van Rossuma1065681999-01-25 23:20:23 +000098#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099#define HAVE_EXECV 1
100#define HAVE_PIPE 1
101#define HAVE_POPEN 1
102#define HAVE_SYSTEM 1
Tim Petersab034fa2002-02-01 11:27:43 +0000103#define HAVE_CWAIT 1
Tim Peters11b23062003-04-23 02:39:17 +0000104#define HAVE_FSYNC 1
105#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000106#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000107#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
108/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#else /* all other compilers */
110/* Unix functions that the configure script doesn't check for */
111#define HAVE_EXECV 1
112#define HAVE_FORK 1
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000113#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
114#define HAVE_FORK1 1
115#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_GETCWD 1
117#define HAVE_GETEGID 1
118#define HAVE_GETEUID 1
119#define HAVE_GETGID 1
120#define HAVE_GETPPID 1
121#define HAVE_GETUID 1
122#define HAVE_KILL 1
123#define HAVE_OPENDIR 1
124#define HAVE_PIPE 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000125#ifndef __rtems__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_POPEN 1
Martin v. Löwis212ede62003-09-20 11:20:30 +0000127#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128#define HAVE_SYSTEM 1
129#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000130#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000131#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#endif /* _MSC_VER */
133#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000134#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000135#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000138
Guido van Rossumb00adfb2000-09-25 13:22:00 +0000139#if defined(sun) && !defined(__SVR4)
140/* SunOS 4.1.4 doesn't have prototypes for these: */
141extern int rename(const char *, const char *);
142extern int pclose(FILE *);
143extern int fclose(FILE *);
Thomas Wouters0f954a42001-02-15 08:46:56 +0000144extern int fsync(int);
145extern int lstat(const char *, struct stat *);
146extern int symlink(const char *, const char *);
Guido van Rossumb00adfb2000-09-25 13:22:00 +0000147#endif
148
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000149#if defined(__sgi)&&_COMPILER_VERSION>=700
150/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
151 (default) */
152extern char *ctermid_r(char *);
153#endif
154
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000155#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000157extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000159#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000160extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000162extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000163#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000164#endif
165#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000166extern int chdir(char *);
167extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int chdir(const char *);
170extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000172#ifdef __BORLANDC__
173extern int chmod(const char *, int);
174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000176#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000177extern int chown(const char *, uid_t, gid_t);
178extern char *getcwd(char *, int);
179extern char *strerror(int);
180extern int link(const char *, const char *);
181extern int rename(const char *, const char *);
182extern int stat(const char *, struct stat *);
183extern int unlink(const char *);
184extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000187#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000190#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000192
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#ifdef HAVE_UTIME_H
196#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000197#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000199#ifdef HAVE_SYS_UTIME_H
200#include <sys/utime.h>
201#define HAVE_UTIME_H /* pretend we do for the rest of this file */
202#endif /* HAVE_SYS_UTIME_H */
203
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_SYS_TIMES_H
205#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
208#ifdef HAVE_SYS_PARAM_H
209#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
212#ifdef HAVE_SYS_UTSNAME_H
213#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000216#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000218#define NAMLEN(dirent) strlen((dirent)->d_name)
219#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000220#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#include <direct.h>
222#define NAMLEN(dirent) strlen((dirent)->d_name)
223#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000225#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#endif
230#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232#endif
233#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#endif
236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <direct.h>
240#include <io.h>
241#include <process.h>
Tim Petersbc2e10e2002-03-03 23:17:02 +0000242#include "osdefs.h"
Tim Petersee66d0c2002-07-15 16:10:55 +0000243#define WIN32_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#include <windows.h>
Tim Petersee66d0c2002-07-15 16:10:55 +0000245#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000246#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000247#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249
Guido van Rossumd48f2521997-12-05 22:19:34 +0000250#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000252#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253
Tim Petersbc2e10e2002-03-03 23:17:02 +0000254#ifndef MAXPATHLEN
255#define MAXPATHLEN 1024
256#endif /* MAXPATHLEN */
257
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000258#ifdef UNION_WAIT
259/* Emulate some macros on systems that have a union instead of macros */
260
261#ifndef WIFEXITED
262#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
263#endif
264
265#ifndef WEXITSTATUS
266#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
267#endif
268
269#ifndef WTERMSIG
270#define WTERMSIG(u_wait) ((u_wait).w_termsig)
271#endif
272
273#endif /* UNION_WAIT */
274
Greg Wardb48bc172000-03-01 21:51:56 +0000275/* Don't use the "_r" form if we don't need it (also, won't have a
276 prototype for it, at least on Solaris -- maybe others as well?). */
277#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
278#define USE_CTERMID_R
279#endif
280
281#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
282#define USE_TMPNAM_R
283#endif
284
Fred Drake699f3522000-06-29 21:12:41 +0000285/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000286#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000287#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +0000288# define STAT _stati64
289# define FSTAT _fstati64
290# define STRUCT_STAT struct _stati64
291#else
292# define STAT stat
293# define FSTAT fstat
294# define STRUCT_STAT struct stat
295#endif
296
Tim Peters11b23062003-04-23 02:39:17 +0000297#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000298#include <sys/mkdev.h>
299#else
300#if defined(MAJOR_IN_SYSMACROS)
301#include <sys/sysmacros.h>
302#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000303#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
304#include <sys/mkdev.h>
305#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000306#endif
Fred Drake699f3522000-06-29 21:12:41 +0000307
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000309#ifdef WITH_NEXT_FRAMEWORK
310/* On Darwin/MacOSX a shared library or framework has no access to
311** environ directly, we must obtain it with _NSGetEnviron().
312*/
313#include <crt_externs.h>
314static char **environ;
315#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000317#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318
Barry Warsaw53699e91996-12-10 23:23:01 +0000319static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000320convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321{
Barry Warsaw53699e91996-12-10 23:23:01 +0000322 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000324 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 if (d == NULL)
326 return NULL;
Jack Jansenea0c3822002-08-01 21:57:49 +0000327#ifdef WITH_NEXT_FRAMEWORK
328 if (environ == NULL)
329 environ = *_NSGetEnviron();
330#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 if (environ == NULL)
332 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000333 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000335 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000336 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337 char *p = strchr(*e, '=');
338 if (p == NULL)
339 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000340 k = PyString_FromStringAndSize(*e, (int)(p-*e));
341 if (k == NULL) {
342 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000344 }
345 v = PyString_FromString(p+1);
346 if (v == NULL) {
347 PyErr_Clear();
348 Py_DECREF(k);
349 continue;
350 }
351 if (PyDict_GetItem(d, k) == NULL) {
352 if (PyDict_SetItem(d, k, v) != 0)
353 PyErr_Clear();
354 }
355 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000356 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 }
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000358#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000359 {
360 APIRET rc;
361 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
362
363 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000364 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000365 PyObject *v = PyString_FromString(buffer);
366 PyDict_SetItemString(d, "BEGINLIBPATH", v);
367 Py_DECREF(v);
368 }
369 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
370 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
371 PyObject *v = PyString_FromString(buffer);
372 PyDict_SetItemString(d, "ENDLIBPATH", v);
373 Py_DECREF(v);
374 }
375 }
376#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 return d;
378}
379
380
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381/* Set a POSIX-specific error from errno, and return NULL */
382
Barry Warsawd58d7641998-07-23 16:14:40 +0000383static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000384posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000385{
Barry Warsawca74da41999-02-09 19:31:45 +0000386 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387}
Barry Warsawd58d7641998-07-23 16:14:40 +0000388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000389posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000390{
Barry Warsawca74da41999-02-09 19:31:45 +0000391 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000392}
393
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000394#ifdef Py_WIN_WIDE_FILENAMES
395static PyObject *
396posix_error_with_unicode_filename(Py_UNICODE* name)
397{
398 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
399}
400#endif /* Py_WIN_WIDE_FILENAMES */
401
402
Mark Hammondef8b6542001-05-13 08:04:26 +0000403static PyObject *
404posix_error_with_allocated_filename(char* name)
405{
406 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
407 PyMem_Free(name);
408 return rc;
409}
410
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000411#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000412static PyObject *
413win32_error(char* function, char* filename)
414{
Mark Hammond33a6da92000-08-15 00:46:38 +0000415 /* XXX We should pass the function name along in the future.
416 (_winreg.c also wants to pass the function name.)
Tim Peters5aa91602002-01-30 05:46:57 +0000417 This would however require an additional param to the
Mark Hammond33a6da92000-08-15 00:46:38 +0000418 Windows error object, which is non-trivial.
419 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000420 errno = GetLastError();
421 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000422 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000423 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000424 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000425}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000426
427#ifdef Py_WIN_WIDE_FILENAMES
428static PyObject *
429win32_error_unicode(char* function, Py_UNICODE* filename)
430{
431 /* XXX - see win32_error for comments on 'function' */
432 errno = GetLastError();
433 if (filename)
434 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
435 else
436 return PyErr_SetFromWindowsErr(errno);
437}
438
439static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
440{
441 /* XXX Perhaps we should make this API an alias of
442 PyObject_Unicode() instead ?! */
443 if (PyUnicode_CheckExact(obj)) {
444 Py_INCREF(obj);
445 return obj;
446 }
447 if (PyUnicode_Check(obj)) {
448 /* For a Unicode subtype that's not a Unicode object,
449 return a true Unicode object with the same data. */
450 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
451 PyUnicode_GET_SIZE(obj));
452 }
Tim Peters11b23062003-04-23 02:39:17 +0000453 return PyUnicode_FromEncodedObject(obj,
454 Py_FileSystemDefaultEncoding,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000455 "strict");
456}
457
458#endif /* Py_WIN_WIDE_FILENAMES */
459
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000460#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461
Guido van Rossumd48f2521997-12-05 22:19:34 +0000462#if defined(PYOS_OS2)
463/**********************************************************************
464 * Helper Function to Trim and Format OS/2 Messages
465 **********************************************************************/
466 static void
467os2_formatmsg(char *msgbuf, int msglen, char *reason)
468{
469 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
470
471 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
472 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
473
474 while (lastc > msgbuf && isspace(*lastc))
475 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
476 }
477
478 /* Add Optional Reason Text */
479 if (reason) {
480 strcat(msgbuf, " : ");
481 strcat(msgbuf, reason);
482 }
483}
484
485/**********************************************************************
486 * Decode an OS/2 Operating System Error Code
487 *
488 * A convenience function to lookup an OS/2 error code and return a
489 * text message we can use to raise a Python exception.
490 *
491 * Notes:
492 * The messages for errors returned from the OS/2 kernel reside in
493 * the file OSO001.MSG in the \OS2 directory hierarchy.
494 *
495 **********************************************************************/
496 static char *
497os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
498{
499 APIRET rc;
500 ULONG msglen;
501
502 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
503 Py_BEGIN_ALLOW_THREADS
504 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
505 errorcode, "oso001.msg", &msglen);
506 Py_END_ALLOW_THREADS
507
508 if (rc == NO_ERROR)
509 os2_formatmsg(msgbuf, msglen, reason);
510 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000511 PyOS_snprintf(msgbuf, msgbuflen,
Tim Peters885d4572001-11-28 20:27:42 +0000512 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000513
514 return msgbuf;
515}
516
517/* Set an OS/2-specific error and return NULL. OS/2 kernel
518 errors are not in a global variable e.g. 'errno' nor are
519 they congruent with posix error numbers. */
520
521static PyObject * os2_error(int code)
522{
523 char text[1024];
524 PyObject *v;
525
526 os2_strerror(text, sizeof(text), code, "");
527
528 v = Py_BuildValue("(is)", code, text);
529 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000530 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000531 Py_DECREF(v);
532 }
533 return NULL; /* Signal to Python that an Exception is Pending */
534}
535
536#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537
538/* POSIX generic methods */
539
Barry Warsaw53699e91996-12-10 23:23:01 +0000540static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000541posix_fildes(PyObject *fdobj, int (*func)(int))
542{
543 int fd;
544 int res;
545 fd = PyObject_AsFileDescriptor(fdobj);
546 if (fd < 0)
547 return NULL;
548 Py_BEGIN_ALLOW_THREADS
549 res = (*func)(fd);
550 Py_END_ALLOW_THREADS
551 if (res < 0)
552 return posix_error();
553 Py_INCREF(Py_None);
554 return Py_None;
555}
Guido van Rossum21142a01999-01-08 21:05:37 +0000556
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557#ifdef Py_WIN_WIDE_FILENAMES
Tim Peters11b23062003-04-23 02:39:17 +0000558static int
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000559unicode_file_names(void)
560{
561 static int canusewide = -1;
562 if (canusewide == -1) {
Tim Peters11b23062003-04-23 02:39:17 +0000563 /* As per doc for ::GetVersion(), this is the correct test for
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000564 the Windows NT family. */
565 canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
566 }
567 return canusewide;
568}
569#endif
Tim Peters11b23062003-04-23 02:39:17 +0000570
Guido van Rossum21142a01999-01-08 21:05:37 +0000571static PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000572posix_1str(PyObject *args, char *format, int (*func)(const char*),
573 char *wformat, int (*wfunc)(const Py_UNICODE*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574{
Mark Hammondef8b6542001-05-13 08:04:26 +0000575 char *path1 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000576 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000577#ifdef Py_WIN_WIDE_FILENAMES
578 if (unicode_file_names()) {
579 PyUnicodeObject *po;
580 if (PyArg_ParseTuple(args, wformat, &po)) {
581 Py_BEGIN_ALLOW_THREADS
582 /* PyUnicode_AS_UNICODE OK without thread
583 lock as it is a simple dereference. */
584 res = (*wfunc)(PyUnicode_AS_UNICODE(po));
585 Py_END_ALLOW_THREADS
586 if (res < 0)
Mark Hammondd3890362002-10-03 07:24:48 +0000587 return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000588 Py_INCREF(Py_None);
589 return Py_None;
590 }
591 /* Drop the argument parsing error as narrow
592 strings are also valid. */
593 PyErr_Clear();
594 }
595#else
596 /* Platforms that don't support Unicode filenames
597 shouldn't be passing these extra params */
598 assert(wformat==NULL && wfunc == NULL);
599#endif
600
Tim Peters5aa91602002-01-30 05:46:57 +0000601 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000602 Py_FileSystemDefaultEncoding, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000603 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000604 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000605 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000606 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000607 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +0000608 return posix_error_with_allocated_filename(path1);
609 PyMem_Free(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000610 Py_INCREF(Py_None);
611 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612}
613
Barry Warsaw53699e91996-12-10 23:23:01 +0000614static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000615posix_2str(PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000616 char *format,
617 int (*func)(const char *, const char *),
618 char *wformat,
619 int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620{
Mark Hammondef8b6542001-05-13 08:04:26 +0000621 char *path1 = NULL, *path2 = NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000622 int res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623#ifdef Py_WIN_WIDE_FILENAMES
624 if (unicode_file_names()) {
625 PyObject *po1;
626 PyObject *po2;
627 if (PyArg_ParseTuple(args, wformat, &po1, &po2)) {
628 if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) {
629 PyObject *wpath1;
630 PyObject *wpath2;
631 wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1);
632 wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2);
633 if (!wpath1 || !wpath2) {
634 Py_XDECREF(wpath1);
635 Py_XDECREF(wpath2);
636 return NULL;
637 }
638 Py_BEGIN_ALLOW_THREADS
639 /* PyUnicode_AS_UNICODE OK without thread
640 lock as it is a simple dereference. */
641 res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1),
642 PyUnicode_AS_UNICODE(wpath2));
643 Py_END_ALLOW_THREADS
644 Py_XDECREF(wpath1);
645 Py_XDECREF(wpath2);
646 if (res != 0)
647 return posix_error();
648 Py_INCREF(Py_None);
649 return Py_None;
650 }
651 /* Else flow through as neither is Unicode. */
652 }
653 /* Drop the argument parsing error as narrow
654 strings are also valid. */
655 PyErr_Clear();
656 }
657#else
658 /* Platforms that don't support Unicode filenames
659 shouldn't be passing these extra params */
660 assert(wformat==NULL && wfunc == NULL);
661#endif
662
Mark Hammondef8b6542001-05-13 08:04:26 +0000663 if (!PyArg_ParseTuple(args, format,
Tim Peters5aa91602002-01-30 05:46:57 +0000664 Py_FileSystemDefaultEncoding, &path1,
Mark Hammondef8b6542001-05-13 08:04:26 +0000665 Py_FileSystemDefaultEncoding, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000667 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000668 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_END_ALLOW_THREADS
Mark Hammondef8b6542001-05-13 08:04:26 +0000670 PyMem_Free(path1);
671 PyMem_Free(path2);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000672 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000673 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000675 Py_INCREF(Py_None);
676 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677}
678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000679PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000680"stat_result: Result from stat or lstat.\n\n\
681This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000682 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000683or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
684\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000685Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000686they are available as attributes only.\n\
687\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000688See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000689
690static PyStructSequence_Field stat_result_fields[] = {
691 {"st_mode", "protection bits"},
692 {"st_ino", "inode"},
693 {"st_dev", "device"},
694 {"st_nlink", "number of hard links"},
695 {"st_uid", "user ID of owner"},
696 {"st_gid", "group ID of owner"},
697 {"st_size", "total size, in bytes"},
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000698 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
699 {NULL, "integer time of last access"},
700 {NULL, "integer time of last modification"},
701 {NULL, "integer time of last change"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000702 {"st_atime", "time of last access"},
703 {"st_mtime", "time of last modification"},
704 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000705#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000706 {"st_blksize", "blocksize for filesystem I/O"},
707#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000708#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000709 {"st_blocks", "number of blocks allocated"},
710#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000711#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712 {"st_rdev", "device type (if inode device)"},
713#endif
714 {0}
715};
716
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000717#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000718#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000719#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000720#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000721#endif
722
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000723#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000724#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
725#else
726#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
727#endif
728
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000729#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000730#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
731#else
732#define ST_RDEV_IDX ST_BLOCKS_IDX
733#endif
734
735static PyStructSequence_Desc stat_result_desc = {
736 "stat_result", /* name */
737 stat_result__doc__, /* doc */
738 stat_result_fields,
739 10
740};
741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000742PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000743"statvfs_result: Result from statvfs or fstatvfs.\n\n\
744This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +0000745 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +0000746or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000747\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000748See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000749
750static PyStructSequence_Field statvfs_result_fields[] = {
751 {"f_bsize", },
752 {"f_frsize", },
753 {"f_blocks", },
754 {"f_bfree", },
755 {"f_bavail", },
756 {"f_files", },
757 {"f_ffree", },
758 {"f_favail", },
759 {"f_flag", },
760 {"f_namemax",},
761 {0}
762};
763
764static PyStructSequence_Desc statvfs_result_desc = {
765 "statvfs_result", /* name */
766 statvfs_result__doc__, /* doc */
767 statvfs_result_fields,
768 10
769};
770
771static PyTypeObject StatResultType;
772static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000773static newfunc structseq_new;
774
775static PyObject *
776statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
777{
778 PyStructSequence *result;
779 int i;
780
781 result = (PyStructSequence*)structseq_new(type, args, kwds);
782 if (!result)
783 return NULL;
784 /* If we have been initialized from a tuple,
785 st_?time might be set to None. Initialize it
786 from the int slots. */
787 for (i = 7; i <= 9; i++) {
788 if (result->ob_item[i+3] == Py_None) {
789 Py_DECREF(Py_None);
790 Py_INCREF(result->ob_item[i]);
791 result->ob_item[i+3] = result->ob_item[i];
792 }
793 }
794 return (PyObject*)result;
795}
796
797
798
799/* If true, st_?time is float. */
800static int _stat_float_times = 0;
801
802PyDoc_STRVAR(stat_float_times__doc__,
803"stat_float_times([newval]) -> oldval\n\n\
804Determine whether os.[lf]stat represents time stamps as float objects.\n\
805If newval is True, future calls to stat() return floats, if it is False,\n\
806future calls return ints. \n\
807If newval is omitted, return the current setting.\n");
808
809static PyObject*
810stat_float_times(PyObject* self, PyObject *args)
811{
812 int newval = -1;
813 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
814 return NULL;
815 if (newval == -1)
816 /* Return old value */
817 return PyBool_FromLong(_stat_float_times);
818 _stat_float_times = newval;
819 Py_INCREF(Py_None);
820 return Py_None;
821}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000822
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000823static void
824fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
825{
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000826 PyObject *fval,*ival;
827#if SIZEOF_TIME_T > SIZEOF_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000828 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000829#else
830 ival = PyInt_FromLong((long)sec);
831#endif
832 if (_stat_float_times) {
833 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
834 } else {
835 fval = ival;
836 Py_INCREF(fval);
837 }
838 PyStructSequence_SET_ITEM(v, index, ival);
839 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000840}
841
Tim Peters5aa91602002-01-30 05:46:57 +0000842/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +0000843 (used by posix_stat() and posix_fstat()) */
844static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000845_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000846{
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000847 unsigned long ansec, mnsec, cnsec;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000848 PyObject *v = PyStructSequence_New(&StatResultType);
Fred Drake699f3522000-06-29 21:12:41 +0000849 if (v == NULL)
850 return NULL;
851
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000852 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode));
Fred Drake699f3522000-06-29 21:12:41 +0000853#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000854 PyStructSequence_SET_ITEM(v, 1,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000855 PyLong_FromLongLong((PY_LONG_LONG)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000856#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000857 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino));
Fred Drake699f3522000-06-29 21:12:41 +0000858#endif
859#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Tim Peters5aa91602002-01-30 05:46:57 +0000860 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000861 PyLong_FromLongLong((PY_LONG_LONG)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000862#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000863 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev));
Fred Drake699f3522000-06-29 21:12:41 +0000864#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000865 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink));
866 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid));
867 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid));
Fred Drake699f3522000-06-29 21:12:41 +0000868#ifdef HAVE_LARGEFILE_SUPPORT
Tim Peters5aa91602002-01-30 05:46:57 +0000869 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000870 PyLong_FromLongLong((PY_LONG_LONG)st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000871#else
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000872 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size));
Fred Drake699f3522000-06-29 21:12:41 +0000873#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000874
875#ifdef HAVE_STAT_TV_NSEC
876 ansec = st.st_atim.tv_nsec;
877 mnsec = st.st_mtim.tv_nsec;
878 cnsec = st.st_ctim.tv_nsec;
Fred Drake699f3522000-06-29 21:12:41 +0000879#else
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000880 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000881#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +0000882 fill_time(v, 7, st.st_atime, ansec);
883 fill_time(v, 8, st.st_mtime, mnsec);
884 fill_time(v, 9, st.st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000885
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000886#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Tim Peters5aa91602002-01-30 05:46:57 +0000887 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000888 PyInt_FromLong((long)st.st_blksize));
889#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000890#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Tim Peters5aa91602002-01-30 05:46:57 +0000891 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000892 PyInt_FromLong((long)st.st_blocks));
893#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000894#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000895 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
896 PyInt_FromLong((long)st.st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +0000897#endif
898
899 if (PyErr_Occurred()) {
900 Py_DECREF(v);
901 return NULL;
902 }
903
904 return v;
905}
906
Barry Warsaw53699e91996-12-10 23:23:01 +0000907static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000908posix_do_stat(PyObject *self, PyObject *args,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000909 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000910#ifdef __VMS
911 int (*statfunc)(const char *, STRUCT_STAT *, ...),
912#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000913 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000914#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000915 char *wformat,
916 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917{
Fred Drake699f3522000-06-29 21:12:41 +0000918 STRUCT_STAT st;
Tim Peters500bd032001-12-19 19:05:01 +0000919 char *path = NULL; /* pass this to stat; do not free() it */
920 char *pathfree = NULL; /* this memory must be free'd */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000921 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000922
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000923#ifdef MS_WINDOWS
Tim Peters500bd032001-12-19 19:05:01 +0000924 int pathlen;
925 char pathcopy[MAX_PATH];
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000926#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000927
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000928
929#ifdef Py_WIN_WIDE_FILENAMES
930 /* If on wide-character-capable OS see if argument
931 is Unicode and if so use wide API. */
932 if (unicode_file_names()) {
933 PyUnicodeObject *po;
934 if (PyArg_ParseTuple(args, wformat, &po)) {
935 Py_UNICODE wpath[MAX_PATH+1];
936 pathlen = wcslen(PyUnicode_AS_UNICODE(po));
937 /* the library call can blow up if the file name is too long! */
938 if (pathlen > MAX_PATH) {
939 errno = ENAMETOOLONG;
940 return posix_error();
941 }
942 wcscpy(wpath, PyUnicode_AS_UNICODE(po));
943 /* Remove trailing slash or backslash, unless it's the current
944 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
945 */
946 if (pathlen > 0 &&
947 (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) {
948 /* It does end with a slash -- exempt the root drive cases. */
949 /* XXX UNC root drives should also be exempted? */
950 if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':'))
951 /* leave it alone */;
952 else {
953 /* nuke the trailing backslash */
954 wpath[pathlen-1] = L'\0';
955 }
956 }
957 Py_BEGIN_ALLOW_THREADS
958 /* PyUnicode_AS_UNICODE result OK without
959 thread lock as it is a simple dereference. */
960 res = wstatfunc(wpath, &st);
961 Py_END_ALLOW_THREADS
962 if (res != 0)
963 return posix_error_with_unicode_filename(wpath);
964 return _pystat_fromstructstat(st);
965 }
966 /* Drop the argument parsing error as narrow strings
967 are also valid. */
968 PyErr_Clear();
969 }
970#endif
971
Tim Peters5aa91602002-01-30 05:46:57 +0000972 if (!PyArg_ParseTuple(args, format,
Mark Hammondef8b6542001-05-13 08:04:26 +0000973 Py_FileSystemDefaultEncoding, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000974 return NULL;
Tim Peters500bd032001-12-19 19:05:01 +0000975 pathfree = path;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000976
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000977#ifdef MS_WINDOWS
Guido van Rossumace88ae2000-04-21 18:54:45 +0000978 pathlen = strlen(path);
979 /* the library call can blow up if the file name is too long! */
980 if (pathlen > MAX_PATH) {
Tim Peters500bd032001-12-19 19:05:01 +0000981 PyMem_Free(pathfree);
Guido van Rossumace88ae2000-04-21 18:54:45 +0000982 errno = ENAMETOOLONG;
983 return posix_error();
984 }
985
Tim Peters500bd032001-12-19 19:05:01 +0000986 /* Remove trailing slash or backslash, unless it's the current
987 drive root (/ or \) or a specific drive's root (like c:\ or c:/).
988 */
989 if (pathlen > 0 &&
990 (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) {
991 /* It does end with a slash -- exempt the root drive cases. */
992 /* XXX UNC root drives should also be exempted? */
993 if (pathlen == 1 || (pathlen == 3 && path[1] == ':'))
994 /* leave it alone */;
995 else {
996 /* nuke the trailing backslash */
Guido van Rossumace88ae2000-04-21 18:54:45 +0000997 strncpy(pathcopy, path, pathlen);
Tim Peters500bd032001-12-19 19:05:01 +0000998 pathcopy[pathlen-1] = '\0';
Guido van Rossumace88ae2000-04-21 18:54:45 +0000999 path = pathcopy;
1000 }
1001 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001002#endif /* MS_WINDOWS */
Guido van Rossumace88ae2000-04-21 18:54:45 +00001003
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001005 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00001006 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001007 if (res != 0)
Tim Peters500bd032001-12-19 19:05:01 +00001008 return posix_error_with_allocated_filename(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001009
Tim Peters500bd032001-12-19 19:05:01 +00001010 PyMem_Free(pathfree);
Fred Drake699f3522000-06-29 21:12:41 +00001011 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001012}
1013
1014
1015/* POSIX methods */
1016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017PyDoc_STRVAR(posix_access__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001018"access(path, mode) -> 1 if granted, 0 otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001019Use the real uid/gid to test for access to a path. Note that most\n\
1020operations will use the effective uid/gid, therefore this routine can\n\
1021be used in a suid/sgid environment to test if the invoking user has the\n\
1022specified access to the path. The mode argument can be F_OK to test\n\
1023existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001024
1025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001026posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001027{
Guido van Rossum015f22a1999-01-06 22:52:38 +00001028 char *path;
1029 int mode;
1030 int res;
1031
Martin v. Löwis1b699a52003-09-12 16:25:38 +00001032#ifdef Py_WIN_WIDE_FILENAMES
1033 if (unicode_file_names()) {
1034 PyUnicodeObject *po;
1035 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1036 Py_BEGIN_ALLOW_THREADS
1037 /* PyUnicode_AS_UNICODE OK without thread lock as
1038 it is a simple dereference. */
1039 res = _waccess(PyUnicode_AS_UNICODE(po), mode);
1040 Py_END_ALLOW_THREADS
1041 return(PyBool_FromLong(res == 0));
1042 }
1043 /* Drop the argument parsing error as narrow strings
1044 are also valid. */
1045 PyErr_Clear();
1046 }
1047#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001048 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +00001049 return NULL;
1050 Py_BEGIN_ALLOW_THREADS
1051 res = access(path, mode);
1052 Py_END_ALLOW_THREADS
Guido van Rossum674deb22002-09-01 15:06:28 +00001053 return(PyBool_FromLong(res == 0));
Guido van Rossum94f6f721999-01-06 18:42:14 +00001054}
1055
Guido van Rossumd371ff11999-01-25 16:12:23 +00001056#ifndef F_OK
1057#define F_OK 0
1058#endif
1059#ifndef R_OK
1060#define R_OK 4
1061#endif
1062#ifndef W_OK
1063#define W_OK 2
1064#endif
1065#ifndef X_OK
1066#define X_OK 1
1067#endif
1068
1069#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001071"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001073
1074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001075posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001076{
Guido van Rossum94f6f721999-01-06 18:42:14 +00001077 int id;
1078 char *ret;
1079
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001080 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +00001081 return NULL;
1082
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001083#if defined(__VMS)
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00001084 /* file descriptor 0 only, the default input device (stdin) */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001085 if (id == 0) {
1086 ret = ttyname();
1087 }
1088 else {
1089 ret = NULL;
1090 }
1091#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00001092 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001093#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001094 if (ret == NULL)
1095 return(posix_error());
1096 return(PyString_FromString(ret));
1097}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001098#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001099
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001100#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001102"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001103Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001104
1105static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001106posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001107{
1108 char *ret;
1109 char buffer[L_ctermid];
1110
Greg Wardb48bc172000-03-01 21:51:56 +00001111#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001112 ret = ctermid_r(buffer);
1113#else
1114 ret = ctermid(buffer);
1115#endif
1116 if (ret == NULL)
1117 return(posix_error());
1118 return(PyString_FromString(buffer));
1119}
1120#endif
1121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001123"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001124Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001125
Barry Warsaw53699e91996-12-10 23:23:01 +00001126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001127posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001129#ifdef MS_WINDOWS
1130 return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir);
1131#elif defined(PYOS_OS2) && defined(PYCC_GCC)
1132 return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001133#elif defined(__VMS)
Tim Peters11b23062003-04-23 02:39:17 +00001134 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001135 NULL, NULL);
1136#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001137 return posix_1str(args, "et:chdir", chdir, NULL, NULL);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001138#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001139}
1140
Fred Drake4d1e64b2002-04-15 19:40:07 +00001141#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001143"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001144Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001146
1147static PyObject *
1148posix_fchdir(PyObject *self, PyObject *fdobj)
1149{
1150 return posix_fildes(fdobj, fchdir);
1151}
1152#endif /* HAVE_FCHDIR */
1153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001155PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001156"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001157Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001158
Barry Warsaw53699e91996-12-10 23:23:01 +00001159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001160posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161{
Mark Hammondef8b6542001-05-13 08:04:26 +00001162 char *path = NULL;
Guido van Rossumffd15f52000-03-31 00:47:28 +00001163 int i;
1164 int res;
Mark Hammond817c9292003-12-03 01:22:38 +00001165#ifdef Py_WIN_WIDE_FILENAMES
1166 if (unicode_file_names()) {
1167 PyUnicodeObject *po;
1168 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1169 Py_BEGIN_ALLOW_THREADS
1170 res = _wchmod(PyUnicode_AS_UNICODE(po), i);
1171 Py_END_ALLOW_THREADS
1172 if (res < 0)
1173 return posix_error_with_unicode_filename(
1174 PyUnicode_AS_UNICODE(po));
1175 Py_INCREF(Py_None);
1176 return Py_None;
1177 }
1178 /* Drop the argument parsing error as narrow strings
1179 are also valid. */
1180 PyErr_Clear();
1181 }
1182#endif /* Py_WIN_WIDE_FILENAMES */
1183 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
Mark Hammondef8b6542001-05-13 08:04:26 +00001184 &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +00001185 return NULL;
1186 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +00001187 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001188 Py_END_ALLOW_THREADS
1189 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001190 return posix_error_with_allocated_filename(path);
1191 PyMem_Free(path);
Guido van Rossumffd15f52000-03-31 00:47:28 +00001192 Py_INCREF(Py_None);
1193 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001194}
1195
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001196
Martin v. Löwis244edc82001-10-04 22:44:26 +00001197#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001199"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001200Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001201
1202static PyObject *
1203posix_chroot(PyObject *self, PyObject *args)
1204{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001205 return posix_1str(args, "et:chroot", chroot, NULL, NULL);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001206}
1207#endif
1208
Guido van Rossum21142a01999-01-08 21:05:37 +00001209#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001210PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001211"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001212force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001213
1214static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001215posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001216{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001217 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001218}
1219#endif /* HAVE_FSYNC */
1220
1221#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001222
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001223#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001224extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1225#endif
1226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001227PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001228"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001229force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001230 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001231
1232static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001233posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001234{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001235 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001236}
1237#endif /* HAVE_FDATASYNC */
1238
1239
Fredrik Lundh10723342000-07-10 16:38:09 +00001240#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001241PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001242"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001243Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001244
Barry Warsaw53699e91996-12-10 23:23:01 +00001245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001246posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001247{
Mark Hammondef8b6542001-05-13 08:04:26 +00001248 char *path = NULL;
Fredrik Lundh44328e62000-07-10 15:59:30 +00001249 int uid, gid;
1250 int res;
Tim Peters5aa91602002-01-30 05:46:57 +00001251 if (!PyArg_ParseTuple(args, "etii:chown",
1252 Py_FileSystemDefaultEncoding, &path,
Mark Hammondef8b6542001-05-13 08:04:26 +00001253 &uid, &gid))
Fredrik Lundh44328e62000-07-10 15:59:30 +00001254 return NULL;
1255 Py_BEGIN_ALLOW_THREADS
1256 res = chown(path, (uid_t) uid, (gid_t) gid);
1257 Py_END_ALLOW_THREADS
1258 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001259 return posix_error_with_allocated_filename(path);
1260 PyMem_Free(path);
Fredrik Lundh44328e62000-07-10 15:59:30 +00001261 Py_INCREF(Py_None);
1262 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001263}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001264#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001265
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001266#ifdef HAVE_LCHOWN
1267PyDoc_STRVAR(posix_lchown__doc__,
1268"lchown(path, uid, gid)\n\n\
1269Change the owner and group id of path to the numeric uid and gid.\n\
1270This function will not follow symbolic links.");
1271
1272static PyObject *
1273posix_lchown(PyObject *self, PyObject *args)
1274{
1275 char *path = NULL;
1276 int uid, gid;
1277 int res;
1278 if (!PyArg_ParseTuple(args, "etii:lchown",
1279 Py_FileSystemDefaultEncoding, &path,
1280 &uid, &gid))
1281 return NULL;
1282 Py_BEGIN_ALLOW_THREADS
1283 res = lchown(path, (uid_t) uid, (gid_t) gid);
1284 Py_END_ALLOW_THREADS
Tim Peters11b23062003-04-23 02:39:17 +00001285 if (res < 0)
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001286 return posix_error_with_allocated_filename(path);
1287 PyMem_Free(path);
1288 Py_INCREF(Py_None);
1289 return Py_None;
1290}
1291#endif /* HAVE_LCHOWN */
1292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001293
Guido van Rossum36bc6801995-06-14 22:54:23 +00001294#ifdef HAVE_GETCWD
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001295PyDoc_STRVAR(posix_getcwd__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001296"getcwd() -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001297Return a string representing the current working directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001298
Barry Warsaw53699e91996-12-10 23:23:01 +00001299static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001300posix_getcwd(PyObject *self, PyObject *noargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001301{
1302 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +00001303 char *res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001304
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001306#if defined(PYOS_OS2) && defined(PYCC_GCC)
1307 res = _getcwd2(buf, sizeof buf);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001308#else
Guido van Rossumff4949e1992-08-05 19:58:53 +00001309 res = getcwd(buf, sizeof buf);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001310#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001311 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001312 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001314 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001315}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001316
Walter Dörwald3b918c32002-11-21 20:18:46 +00001317#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001318PyDoc_STRVAR(posix_getcwdu__doc__,
1319"getcwdu() -> path\n\n\
1320Return a unicode string representing the current working directory.");
1321
1322static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001323posix_getcwdu(PyObject *self, PyObject *noargs)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001324{
1325 char buf[1026];
1326 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001327
1328#ifdef Py_WIN_WIDE_FILENAMES
1329 if (unicode_file_names()) {
1330 wchar_t *wres;
1331 wchar_t wbuf[1026];
1332 Py_BEGIN_ALLOW_THREADS
1333 wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]);
1334 Py_END_ALLOW_THREADS
1335 if (wres == NULL)
1336 return posix_error();
1337 return PyUnicode_FromWideChar(wbuf, wcslen(wbuf));
1338 }
1339#endif
1340
1341 Py_BEGIN_ALLOW_THREADS
1342#if defined(PYOS_OS2) && defined(PYCC_GCC)
1343 res = _getcwd2(buf, sizeof buf);
1344#else
1345 res = getcwd(buf, sizeof buf);
1346#endif
1347 Py_END_ALLOW_THREADS
1348 if (res == NULL)
1349 return posix_error();
1350 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
1351}
Guido van Rossum36bc6801995-06-14 22:54:23 +00001352#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00001353#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001354
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001355
Guido van Rossumb6775db1994-08-01 11:34:53 +00001356#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001358"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001360
Barry Warsaw53699e91996-12-10 23:23:01 +00001361static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001362posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001363{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001364 return posix_2str(args, "etet:link", link, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001365}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001366#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001367
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001370"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001371Return a list containing the names of the entries in the directory.\n\
1372\n\
1373 path: path of directory to list\n\
1374\n\
1375The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001376entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001377
Barry Warsaw53699e91996-12-10 23:23:01 +00001378static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001379posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001380{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001381 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001382 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001383#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001384
Barry Warsaw53699e91996-12-10 23:23:01 +00001385 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001386 HANDLE hFindFile;
1387 WIN32_FIND_DATA FileData;
Mark Hammondef8b6542001-05-13 08:04:26 +00001388 /* MAX_PATH characters could mean a bigger encoded string */
1389 char namebuf[MAX_PATH*2+5];
1390 char *bufptr = namebuf;
1391 int len = sizeof(namebuf)/sizeof(namebuf[0]);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001392
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001393#ifdef Py_WIN_WIDE_FILENAMES
1394 /* If on wide-character-capable OS see if argument
1395 is Unicode and if so use wide API. */
1396 if (unicode_file_names()) {
1397 PyUnicodeObject *po;
1398 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
1399 WIN32_FIND_DATAW wFileData;
1400 Py_UNICODE wnamebuf[MAX_PATH*2+5];
1401 Py_UNICODE wch;
1402 wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH);
1403 wnamebuf[MAX_PATH] = L'\0';
1404 len = wcslen(wnamebuf);
1405 wch = (len > 0) ? wnamebuf[len-1] : L'\0';
1406 if (wch != L'/' && wch != L'\\' && wch != L':')
1407 wnamebuf[len++] = L'/';
1408 wcscpy(wnamebuf + len, L"*.*");
1409 if ((d = PyList_New(0)) == NULL)
1410 return NULL;
1411 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
1412 if (hFindFile == INVALID_HANDLE_VALUE) {
1413 errno = GetLastError();
1414 if (errno == ERROR_FILE_NOT_FOUND) {
1415 return d;
1416 }
1417 Py_DECREF(d);
1418 return win32_error_unicode("FindFirstFileW", wnamebuf);
1419 }
1420 do {
1421 if (wFileData.cFileName[0] == L'.' &&
1422 (wFileData.cFileName[1] == L'\0' ||
1423 wFileData.cFileName[1] == L'.' &&
1424 wFileData.cFileName[2] == L'\0'))
1425 continue;
1426 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
1427 if (v == NULL) {
1428 Py_DECREF(d);
1429 d = NULL;
1430 break;
1431 }
1432 if (PyList_Append(d, v) != 0) {
1433 Py_DECREF(v);
1434 Py_DECREF(d);
1435 d = NULL;
1436 break;
1437 }
1438 Py_DECREF(v);
1439 } while (FindNextFileW(hFindFile, &wFileData) == TRUE);
1440
1441 if (FindClose(hFindFile) == FALSE) {
1442 Py_DECREF(d);
1443 return win32_error_unicode("FindClose", wnamebuf);
1444 }
1445 return d;
1446 }
1447 /* Drop the argument parsing error as narrow strings
1448 are also valid. */
1449 PyErr_Clear();
1450 }
1451#endif
1452
Tim Peters5aa91602002-01-30 05:46:57 +00001453 if (!PyArg_ParseTuple(args, "et#:listdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001454 Py_FileSystemDefaultEncoding, &bufptr, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +00001455 return NULL;
Neil Schemenauer94b866a2002-03-22 20:51:58 +00001456 if (len > 0) {
1457 char ch = namebuf[len-1];
1458 if (ch != SEP && ch != ALTSEP && ch != ':')
1459 namebuf[len++] = '/';
1460 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001461 strcpy(namebuf + len, "*.*");
1462
Barry Warsaw53699e91996-12-10 23:23:01 +00001463 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001464 return NULL;
1465
1466 hFindFile = FindFirstFile(namebuf, &FileData);
1467 if (hFindFile == INVALID_HANDLE_VALUE) {
1468 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +00001469 if (errno == ERROR_FILE_NOT_FOUND)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001470 return d;
1471 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001472 return win32_error("FindFirstFile", namebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001473 }
1474 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001475 if (FileData.cFileName[0] == '.' &&
1476 (FileData.cFileName[1] == '\0' ||
1477 FileData.cFileName[1] == '.' &&
1478 FileData.cFileName[2] == '\0'))
1479 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001480 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001481 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001482 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001483 d = NULL;
1484 break;
1485 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001486 if (PyList_Append(d, v) != 0) {
1487 Py_DECREF(v);
1488 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001489 d = NULL;
1490 break;
1491 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001492 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001493 } while (FindNextFile(hFindFile, &FileData) == TRUE);
1494
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001495 if (FindClose(hFindFile) == FALSE) {
1496 Py_DECREF(d);
Mark Hammondef8b6542001-05-13 08:04:26 +00001497 return win32_error("FindClose", namebuf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001498 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001499
1500 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001501
Tim Peters0bb44a42000-09-15 07:44:49 +00001502#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001503
1504#ifndef MAX_PATH
1505#define MAX_PATH CCHMAXPATH
1506#endif
1507 char *name, *pt;
1508 int len;
1509 PyObject *d, *v;
1510 char namebuf[MAX_PATH+5];
1511 HDIR hdir = 1;
1512 ULONG srchcnt = 1;
1513 FILEFINDBUF3 ep;
1514 APIRET rc;
1515
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001516 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001517 return NULL;
1518 if (len >= MAX_PATH) {
1519 PyErr_SetString(PyExc_ValueError, "path too long");
1520 return NULL;
1521 }
1522 strcpy(namebuf, name);
1523 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001524 if (*pt == ALTSEP)
1525 *pt = SEP;
1526 if (namebuf[len-1] != SEP)
1527 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001528 strcpy(namebuf + len, "*.*");
1529
1530 if ((d = PyList_New(0)) == NULL)
1531 return NULL;
1532
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001533 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
1534 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001535 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001536 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
1537 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
1538 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001539
1540 if (rc != NO_ERROR) {
1541 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001542 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001543 }
1544
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001545 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001546 do {
1547 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001548 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001549 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001550
1551 strcpy(namebuf, ep.achName);
1552
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001553 /* Leave Case of Name Alone -- In Native Form */
1554 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001555
1556 v = PyString_FromString(namebuf);
1557 if (v == NULL) {
1558 Py_DECREF(d);
1559 d = NULL;
1560 break;
1561 }
1562 if (PyList_Append(d, v) != 0) {
1563 Py_DECREF(v);
1564 Py_DECREF(d);
1565 d = NULL;
1566 break;
1567 }
1568 Py_DECREF(v);
1569 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1570 }
1571
1572 return d;
1573#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001574
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001575 char *name = NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001576 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001577 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001578 struct dirent *ep;
Just van Rossum96b1c902003-03-03 17:32:15 +00001579 int arg_is_unicode = 1;
1580
1581 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
1582 arg_is_unicode = 0;
1583 PyErr_Clear();
1584 }
1585 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001586 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001587 if ((dirp = opendir(name)) == NULL) {
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001588 return posix_error_with_allocated_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001589 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001590 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001591 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001592 PyMem_Free(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593 return NULL;
1594 }
1595 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001596 if (ep->d_name[0] == '.' &&
1597 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001598 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001599 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001600 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001601 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001602 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001603 d = NULL;
1604 break;
1605 }
Just van Rossum46c97842003-02-25 21:42:15 +00001606#ifdef Py_USING_UNICODE
Just van Rossum96b1c902003-03-03 17:32:15 +00001607 if (arg_is_unicode) {
Just van Rossum46c97842003-02-25 21:42:15 +00001608 PyObject *w;
1609
1610 w = PyUnicode_FromEncodedObject(v,
Tim Peters11b23062003-04-23 02:39:17 +00001611 Py_FileSystemDefaultEncoding,
Just van Rossum46c97842003-02-25 21:42:15 +00001612 "strict");
Just van Rossum6a421832003-03-04 19:30:44 +00001613 if (w != NULL) {
1614 Py_DECREF(v);
1615 v = w;
1616 }
1617 else {
1618 /* fall back to the original byte string, as
1619 discussed in patch #683592 */
1620 PyErr_Clear();
Just van Rossum46c97842003-02-25 21:42:15 +00001621 }
Just van Rossum46c97842003-02-25 21:42:15 +00001622 }
1623#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001624 if (PyList_Append(d, v) != 0) {
1625 Py_DECREF(v);
1626 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627 d = NULL;
1628 break;
1629 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001630 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001631 }
1632 closedir(dirp);
Just van Rossum2fe07fd2003-03-03 19:07:13 +00001633 PyMem_Free(name);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001634
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001635 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001636
Tim Peters0bb44a42000-09-15 07:44:49 +00001637#endif /* which OS */
1638} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001639
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001640#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00001641/* A helper function for abspath on win32 */
1642static PyObject *
1643posix__getfullpathname(PyObject *self, PyObject *args)
1644{
1645 /* assume encoded strings wont more than double no of chars */
1646 char inbuf[MAX_PATH*2];
1647 char *inbufp = inbuf;
1648 int insize = sizeof(inbuf)/sizeof(inbuf[0]);
1649 char outbuf[MAX_PATH*2];
1650 char *temp;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001651#ifdef Py_WIN_WIDE_FILENAMES
1652 if (unicode_file_names()) {
1653 PyUnicodeObject *po;
1654 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
1655 Py_UNICODE woutbuf[MAX_PATH*2];
1656 Py_UNICODE *wtemp;
Tim Peters11b23062003-04-23 02:39:17 +00001657 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001658 sizeof(woutbuf)/sizeof(woutbuf[0]),
1659 woutbuf, &wtemp))
1660 return win32_error("GetFullPathName", "");
1661 return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf));
1662 }
1663 /* Drop the argument parsing error as narrow strings
1664 are also valid. */
1665 PyErr_Clear();
1666 }
1667#endif
Tim Peters5aa91602002-01-30 05:46:57 +00001668 if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
1669 Py_FileSystemDefaultEncoding, &inbufp,
Mark Hammondef8b6542001-05-13 08:04:26 +00001670 &insize))
1671 return NULL;
1672 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
1673 outbuf, &temp))
1674 return win32_error("GetFullPathName", inbuf);
1675 return PyString_FromString(outbuf);
1676} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001677#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00001678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001679PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001680"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682
Barry Warsaw53699e91996-12-10 23:23:01 +00001683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001684posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001685{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001686 int res;
Mark Hammondef8b6542001-05-13 08:04:26 +00001687 char *path = NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001688 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001689
1690#ifdef Py_WIN_WIDE_FILENAMES
1691 if (unicode_file_names()) {
1692 PyUnicodeObject *po;
Mark Hammond05107b62003-02-19 04:08:27 +00001693 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001694 Py_BEGIN_ALLOW_THREADS
1695 /* PyUnicode_AS_UNICODE OK without thread lock as
1696 it is a simple dereference. */
1697 res = _wmkdir(PyUnicode_AS_UNICODE(po));
1698 Py_END_ALLOW_THREADS
1699 if (res < 0)
1700 return posix_error();
1701 Py_INCREF(Py_None);
1702 return Py_None;
1703 }
1704 /* Drop the argument parsing error as narrow strings
1705 are also valid. */
1706 PyErr_Clear();
1707 }
1708#endif
1709
Tim Peters5aa91602002-01-30 05:46:57 +00001710 if (!PyArg_ParseTuple(args, "et|i:mkdir",
Mark Hammondef8b6542001-05-13 08:04:26 +00001711 Py_FileSystemDefaultEncoding, &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001712 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001713 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001714#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001715 res = mkdir(path);
1716#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001717 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001718#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001719 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001720 if (res < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00001721 return posix_error_with_allocated_filename(path);
1722 PyMem_Free(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001723 Py_INCREF(Py_None);
1724 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001725}
1726
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001727
Guido van Rossumb6775db1994-08-01 11:34:53 +00001728#ifdef HAVE_NICE
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001729#if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H)
1730#if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS)
1731#include <sys/resource.h>
1732#endif
1733#endif
1734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001735PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001736"nice(inc) -> new_priority\n\n\
1737Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001738
Barry Warsaw53699e91996-12-10 23:23:01 +00001739static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001740posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001741{
1742 int increment, value;
1743
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001744 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001745 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001746
1747 /* There are two flavours of 'nice': one that returns the new
1748 priority (as required by almost all standards out there) and the
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001749 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1750 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00001751
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001752 If we are of the nice family that returns the new priority, we
1753 need to clear errno before the call, and check if errno is filled
1754 before calling posix_error() on a returnvalue of -1, because the
1755 -1 may be the actual new priority! */
1756
1757 errno = 0;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001758 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00001759#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00001760 if (value == 0)
1761 value = getpriority(PRIO_PROCESS, 0);
1762#endif
1763 if (value == -1 && errno != 0)
1764 /* either nice() or getpriority() returned an error */
Guido van Rossum775f4da1993-01-09 17:18:52 +00001765 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001766 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001767}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001768#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001769
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001771PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001772"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001773Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001774
Barry Warsaw53699e91996-12-10 23:23:01 +00001775static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001776posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001777{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001778#ifdef MS_WINDOWS
1779 return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename);
1780#else
1781 return posix_2str(args, "etet:rename", rename, NULL, NULL);
1782#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001783}
1784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001785
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001786PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001787"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001789
Barry Warsaw53699e91996-12-10 23:23:01 +00001790static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001791posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001792{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001793#ifdef MS_WINDOWS
1794 return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir);
1795#else
1796 return posix_1str(args, "et:rmdir", rmdir, NULL, NULL);
1797#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001798}
1799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001802"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001804
Barry Warsaw53699e91996-12-10 23:23:01 +00001805static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001806posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001807{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001808#ifdef MS_WINDOWS
1809 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64);
1810#else
1811 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
1812#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001813}
1814
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001815
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001816#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001818"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001820
Barry Warsaw53699e91996-12-10 23:23:01 +00001821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001822posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001823{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001824 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001825 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001826 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001827 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001828 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001829 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001830 Py_END_ALLOW_THREADS
1831 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001832}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001833#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001835
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001836PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001837"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001838Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001839
Barry Warsaw53699e91996-12-10 23:23:01 +00001840static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001841posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001842{
1843 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001844 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845 return NULL;
Fred Drake0368bc42001-07-19 20:48:32 +00001846 i = (int)umask(i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001847 if (i < 0)
1848 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001849 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001850}
1851
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001853PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001854"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001858"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001859Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001860
Barry Warsaw53699e91996-12-10 23:23:01 +00001861static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001862posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001863{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001864#ifdef MS_WINDOWS
1865 return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink);
1866#else
1867 return posix_1str(args, "et:remove", unlink, NULL, NULL);
1868#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001869}
1870
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001871
Guido van Rossumb6775db1994-08-01 11:34:53 +00001872#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001874"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001878posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001879{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001880 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001881 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00001882
Barry Warsaw53699e91996-12-10 23:23:01 +00001883 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001884 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001885 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001886 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001887 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001888 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001889 u.sysname,
1890 u.nodename,
1891 u.release,
1892 u.version,
1893 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001894}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001895#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001896
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001897static int
1898extract_time(PyObject *t, long* sec, long* usec)
1899{
1900 long intval;
1901 if (PyFloat_Check(t)) {
1902 double tval = PyFloat_AsDouble(t);
1903 PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
1904 if (!intobj)
1905 return -1;
1906 intval = PyInt_AsLong(intobj);
1907 Py_DECREF(intobj);
1908 *sec = intval;
Tim Peters96940cf2002-09-10 15:37:28 +00001909 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001910 if (*usec < 0)
1911 /* If rounding gave us a negative number,
1912 truncate. */
1913 *usec = 0;
1914 return 0;
1915 }
1916 intval = PyInt_AsLong(t);
1917 if (intval == -1 && PyErr_Occurred())
1918 return -1;
1919 *sec = intval;
1920 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00001921 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001922}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(posix_utime__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925"utime(path, (atime, utime))\n\
1926utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001927Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001928second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001929
Barry Warsaw53699e91996-12-10 23:23:01 +00001930static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001931posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001932{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001933 char *path;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001934 long atime, mtime, ausec, musec;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001935 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001936 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001937
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001938#if defined(HAVE_UTIMES)
1939 struct timeval buf[2];
1940#define ATIME buf[0].tv_sec
1941#define MTIME buf[1].tv_sec
1942#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001943/* XXX should define struct utimbuf instead, above */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001944 struct utimbuf buf;
1945#define ATIME buf.actime
1946#define MTIME buf.modtime
1947#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001948#else /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001949 time_t buf[2];
1950#define ATIME buf[0]
1951#define MTIME buf[1]
1952#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001953#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001954
Mark Hammond817c9292003-12-03 01:22:38 +00001955 int have_unicode_filename = 0;
1956#ifdef Py_WIN_WIDE_FILENAMES
1957 PyUnicodeObject *obwpath;
1958 wchar_t *wpath;
1959 if (unicode_file_names()) {
1960 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
1961 wpath = PyUnicode_AS_UNICODE(obwpath);
1962 have_unicode_filename = 1;
1963 } else
1964 /* Drop the argument parsing error as narrow strings
1965 are also valid. */
1966 PyErr_Clear();
1967 }
1968#endif /* Py_WIN_WIDE_FILENAMES */
1969
1970 if (!have_unicode_filename && \
1971 !PyArg_ParseTuple(args, "sO:utime", &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001972 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001973 if (arg == Py_None) {
1974 /* optional time values not given */
1975 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00001976#ifdef Py_WIN_WIDE_FILENAMES
1977 if (have_unicode_filename)
1978 res = _wutime(wpath, NULL);
1979 else
1980#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00001981 res = utime(path, NULL);
1982 Py_END_ALLOW_THREADS
1983 }
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001984 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
Barry Warsaw3cef8562000-05-01 16:17:24 +00001985 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001986 "utime() arg 2 must be a tuple (atime, mtime)");
Barry Warsaw3cef8562000-05-01 16:17:24 +00001987 return NULL;
1988 }
1989 else {
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001990 if (extract_time(PyTuple_GET_ITEM(arg, 0),
1991 &atime, &ausec) == -1)
1992 return NULL;
1993 if (extract_time(PyTuple_GET_ITEM(arg, 1),
1994 &mtime, &musec) == -1)
1995 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001996 ATIME = atime;
1997 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00001998#ifdef HAVE_UTIMES
1999 buf[0].tv_usec = ausec;
2000 buf[1].tv_usec = musec;
2001 Py_BEGIN_ALLOW_THREADS
2002 res = utimes(path, buf);
2003 Py_END_ALLOW_THREADS
2004#else
Barry Warsaw3cef8562000-05-01 16:17:24 +00002005 Py_BEGIN_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002006#ifdef Py_WIN_WIDE_FILENAMES
2007 if (have_unicode_filename)
2008 /* utime is OK with utimbuf, but _wutime insists
2009 on _utimbuf (the msvc headers assert the
2010 underscore version is ansi) */
2011 res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG);
2012 else
2013#endif /* Py_WIN_WIDE_FILENAMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002014 res = utime(path, UTIME_ARG);
2015 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002016#endif /* HAVE_UTIMES */
Barry Warsaw3cef8562000-05-01 16:17:24 +00002017 }
Guido van Rossumff4949e1992-08-05 19:58:53 +00002018 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002019 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002020 Py_INCREF(Py_None);
2021 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002022#undef UTIME_ARG
2023#undef ATIME
2024#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002025}
2026
Guido van Rossum85e3b011991-06-03 12:42:10 +00002027
Guido van Rossum3b066191991-06-04 19:40:25 +00002028/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002030PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002031"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002032Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002033
Barry Warsaw53699e91996-12-10 23:23:01 +00002034static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002035posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002036{
2037 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002038 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002039 return NULL;
2040 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00002041 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002042}
2043
Martin v. Löwis114619e2002-10-07 06:44:21 +00002044#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2045static void
2046free_string_array(char **array, int count)
2047{
2048 int i;
2049 for (i = 0; i < count; i++)
2050 PyMem_Free(array[i]);
2051 PyMem_DEL(array);
2052}
2053#endif
2054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002055
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002056#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002058"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002059Execute an executable path with arguments, replacing current process.\n\
2060\n\
2061 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002063
Barry Warsaw53699e91996-12-10 23:23:01 +00002064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002065posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002066{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002067 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002068 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002069 char **argvlist;
2070 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002071 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002072
Guido van Rossum89b33251993-10-22 14:26:06 +00002073 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00002074 argv is a list or tuple of strings. */
2075
Martin v. Löwis114619e2002-10-07 06:44:21 +00002076 if (!PyArg_ParseTuple(args, "etO:execv",
2077 Py_FileSystemDefaultEncoding,
2078 &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002079 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002080 if (PyList_Check(argv)) {
2081 argc = PyList_Size(argv);
2082 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002083 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002084 else if (PyTuple_Check(argv)) {
2085 argc = PyTuple_Size(argv);
2086 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002087 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002088 else {
Fred Drake661ea262000-10-24 19:57:45 +00002089 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002090 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002091 return NULL;
2092 }
2093
2094 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00002095 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002096 PyMem_Free(path);
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002097 return NULL;
2098 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002099
Barry Warsaw53699e91996-12-10 23:23:01 +00002100 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002101 if (argvlist == NULL) {
2102 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002103 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002104 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002105 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002106 if (!PyArg_Parse((*getitem)(argv, i), "et",
2107 Py_FileSystemDefaultEncoding,
2108 &argvlist[i])) {
2109 free_string_array(argvlist, i);
Tim Peters5aa91602002-01-30 05:46:57 +00002110 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002111 "execv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002112 PyMem_Free(path);
Guido van Rossum50422b42000-04-26 20:34:28 +00002113 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00002114
Guido van Rossum85e3b011991-06-03 12:42:10 +00002115 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00002116 }
2117 argvlist[argc] = NULL;
2118
Guido van Rossumb6775db1994-08-01 11:34:53 +00002119#ifdef BAD_EXEC_PROTOTYPES
2120 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002121#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002122 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002123#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002124
Guido van Rossum85e3b011991-06-03 12:42:10 +00002125 /* If we get here it's definitely an error */
2126
Martin v. Löwis114619e2002-10-07 06:44:21 +00002127 free_string_array(argvlist, argc);
2128 PyMem_Free(path);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002129 return posix_error();
2130}
2131
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002133PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002134"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002135Execute a path with arguments and environment, replacing current process.\n\
2136\n\
2137 path: path of executable file\n\
2138 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002139 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002140
Barry Warsaw53699e91996-12-10 23:23:01 +00002141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002142posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002143{
2144 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00002145 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002146 char **argvlist;
2147 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002148 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002149 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002150 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002151 int lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002152
2153 /* execve has three arguments: (path, argv, env), where
2154 argv is a list or tuple of strings and env is a dictionary
2155 like posix.environ. */
2156
Martin v. Löwis114619e2002-10-07 06:44:21 +00002157 if (!PyArg_ParseTuple(args, "etOO:execve",
2158 Py_FileSystemDefaultEncoding,
2159 &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002160 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002161 if (PyList_Check(argv)) {
2162 argc = PyList_Size(argv);
2163 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002164 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002165 else if (PyTuple_Check(argv)) {
2166 argc = PyTuple_Size(argv);
2167 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002168 }
2169 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002170 PyErr_SetString(PyExc_TypeError,
2171 "execve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002172 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002173 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002174 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002175 PyErr_SetString(PyExc_TypeError,
2176 "execve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002177 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002178 }
2179
Guido van Rossum50422b42000-04-26 20:34:28 +00002180 if (argc == 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00002181 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00002182 "execve() arg 2 must not be empty");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002183 goto fail_0;
Guido van Rossum50422b42000-04-26 20:34:28 +00002184 }
2185
Barry Warsaw53699e91996-12-10 23:23:01 +00002186 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002187 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002188 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002189 goto fail_0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002190 }
2191 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002192 if (!PyArg_Parse((*getitem)(argv, i),
Martin v. Löwis114619e2002-10-07 06:44:21 +00002193 "et;execve() arg 2 must contain only strings",
Guido van Rossum1e700d22002-10-16 16:52:11 +00002194 Py_FileSystemDefaultEncoding,
Barry Warsaw43d68b81996-12-19 22:10:44 +00002195 &argvlist[i]))
2196 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002197 lastarg = i;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002198 goto fail_1;
2199 }
2200 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002201 lastarg = argc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002202 argvlist[argc] = NULL;
2203
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002204 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002205 if (i < 0)
2206 goto fail_1;
Barry Warsaw53699e91996-12-10 23:23:01 +00002207 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002208 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002209 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002210 goto fail_1;
2211 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002212 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002213 keys = PyMapping_Keys(env);
2214 vals = PyMapping_Values(env);
2215 if (!keys || !vals)
2216 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002217 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2218 PyErr_SetString(PyExc_TypeError,
2219 "execve(): env.keys() or env.values() is not a list");
2220 goto fail_2;
2221 }
Tim Peters5aa91602002-01-30 05:46:57 +00002222
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002223 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002224 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002225 size_t len;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002226
2227 key = PyList_GetItem(keys, pos);
2228 val = PyList_GetItem(vals, pos);
2229 if (!key || !val)
2230 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002231
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002232 if (!PyArg_Parse(
2233 key,
2234 "s;execve() arg 3 contains a non-string key",
2235 &k) ||
2236 !PyArg_Parse(
2237 val,
2238 "s;execve() arg 3 contains a non-string value",
2239 &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00002240 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002241 goto fail_2;
2242 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002243
2244#if defined(PYOS_OS2)
2245 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
2246 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
2247#endif
Tim Petersc8996f52001-12-03 20:41:00 +00002248 len = PyString_Size(key) + PyString_Size(val) + 2;
2249 p = PyMem_NEW(char, len);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002250 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002251 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002252 goto fail_2;
2253 }
Tim Petersc8996f52001-12-03 20:41:00 +00002254 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002255 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002256#if defined(PYOS_OS2)
2257 }
2258#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002259 }
2260 envlist[envc] = 0;
2261
Guido van Rossumb6775db1994-08-01 11:34:53 +00002262
2263#ifdef BAD_EXEC_PROTOTYPES
2264 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002265#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002266 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002267#endif /* BAD_EXEC_PROTOTYPES */
Tim Peters5aa91602002-01-30 05:46:57 +00002268
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002269 /* If we get here it's definitely an error */
2270
2271 (void) posix_error();
2272
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002273 fail_2:
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002274 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00002275 PyMem_DEL(envlist[envc]);
2276 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002277 fail_1:
2278 free_string_array(argvlist, lastarg);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00002279 Py_XDECREF(vals);
2280 Py_XDECREF(keys);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002281 fail_0:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002282 PyMem_Free(path);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002283 return NULL;
2284}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002285#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00002286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002287
Guido van Rossuma1065681999-01-25 23:20:23 +00002288#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002289PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002290"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002291Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002292\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002293 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002294 path: path of executable file\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002295 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002296
2297static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002298posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002299{
2300 char *path;
2301 PyObject *argv;
2302 char **argvlist;
2303 int mode, i, argc;
Tim Peters79248aa2001-08-29 21:37:10 +00002304 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002305 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00002306
2307 /* spawnv has three arguments: (mode, path, argv), where
2308 argv is a list or tuple of strings. */
2309
Martin v. Löwis114619e2002-10-07 06:44:21 +00002310 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
2311 Py_FileSystemDefaultEncoding,
2312 &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00002313 return NULL;
2314 if (PyList_Check(argv)) {
2315 argc = PyList_Size(argv);
2316 getitem = PyList_GetItem;
2317 }
2318 else if (PyTuple_Check(argv)) {
2319 argc = PyTuple_Size(argv);
2320 getitem = PyTuple_GetItem;
2321 }
2322 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002323 PyErr_SetString(PyExc_TypeError,
2324 "spawnv() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002325 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002326 return NULL;
2327 }
2328
2329 argvlist = PyMem_NEW(char *, argc+1);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002330 if (argvlist == NULL) {
2331 PyMem_Free(path);
Neal Norwitzec74f2f2003-02-11 23:05:40 +00002332 return PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002333 }
Guido van Rossuma1065681999-01-25 23:20:23 +00002334 for (i = 0; i < argc; i++) {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002335 if (!PyArg_Parse((*getitem)(argv, i), "et",
2336 Py_FileSystemDefaultEncoding,
2337 &argvlist[i])) {
2338 free_string_array(argvlist, i);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002339 PyErr_SetString(
2340 PyExc_TypeError,
2341 "spawnv() arg 2 must contain only strings");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002342 PyMem_Free(path);
Fred Drake137507e2000-06-01 02:02:46 +00002343 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00002344 }
2345 }
2346 argvlist[argc] = NULL;
2347
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002348#if defined(PYOS_OS2) && defined(PYCC_GCC)
2349 Py_BEGIN_ALLOW_THREADS
2350 spawnval = spawnv(mode, path, argvlist);
2351 Py_END_ALLOW_THREADS
2352#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002353 if (mode == _OLD_P_OVERLAY)
2354 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00002355
Tim Peters25059d32001-12-07 20:35:43 +00002356 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002357 spawnval = _spawnv(mode, path, argvlist);
Tim Peters25059d32001-12-07 20:35:43 +00002358 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002359#endif
Tim Peters5aa91602002-01-30 05:46:57 +00002360
Martin v. Löwis114619e2002-10-07 06:44:21 +00002361 free_string_array(argvlist, argc);
2362 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002363
Fred Drake699f3522000-06-29 21:12:41 +00002364 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002365 return posix_error();
2366 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002367#if SIZEOF_LONG == SIZEOF_VOID_P
2368 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002369#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002370 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002371#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002372}
2373
2374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002375PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002376"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00002377Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002378\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00002379 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00002380 path: path of executable file\n\
2381 args: tuple or list of arguments\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002382 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00002383
2384static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002385posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00002386{
2387 char *path;
2388 PyObject *argv, *env;
2389 char **argvlist;
2390 char **envlist;
2391 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
2392 int mode, i, pos, argc, envc;
Tim Peters79248aa2001-08-29 21:37:10 +00002393 Py_intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002394 PyObject *(*getitem)(PyObject *, int);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002395 int lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002396
2397 /* spawnve has four arguments: (mode, path, argv, env), where
2398 argv is a list or tuple of strings and env is a dictionary
2399 like posix.environ. */
2400
Martin v. Löwis114619e2002-10-07 06:44:21 +00002401 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
2402 Py_FileSystemDefaultEncoding,
2403 &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00002404 return NULL;
2405 if (PyList_Check(argv)) {
2406 argc = PyList_Size(argv);
2407 getitem = PyList_GetItem;
2408 }
2409 else if (PyTuple_Check(argv)) {
2410 argc = PyTuple_Size(argv);
2411 getitem = PyTuple_GetItem;
2412 }
2413 else {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002414 PyErr_SetString(PyExc_TypeError,
2415 "spawnve() arg 2 must be a tuple or list");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002416 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002417 }
2418 if (!PyMapping_Check(env)) {
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002419 PyErr_SetString(PyExc_TypeError,
2420 "spawnve() arg 3 must be a mapping object");
Martin v. Löwis114619e2002-10-07 06:44:21 +00002421 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002422 }
2423
2424 argvlist = PyMem_NEW(char *, argc+1);
2425 if (argvlist == NULL) {
2426 PyErr_NoMemory();
Martin v. Löwis114619e2002-10-07 06:44:21 +00002427 goto fail_0;
Guido van Rossuma1065681999-01-25 23:20:23 +00002428 }
2429 for (i = 0; i < argc; i++) {
2430 if (!PyArg_Parse((*getitem)(argv, i),
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002431 "et;spawnve() arg 2 must contain only strings",
Martin v. Löwis114619e2002-10-07 06:44:21 +00002432 Py_FileSystemDefaultEncoding,
Guido van Rossuma1065681999-01-25 23:20:23 +00002433 &argvlist[i]))
2434 {
Martin v. Löwis114619e2002-10-07 06:44:21 +00002435 lastarg = i;
Guido van Rossuma1065681999-01-25 23:20:23 +00002436 goto fail_1;
2437 }
2438 }
Martin v. Löwis114619e2002-10-07 06:44:21 +00002439 lastarg = argc;
Guido van Rossuma1065681999-01-25 23:20:23 +00002440 argvlist[argc] = NULL;
2441
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002442 i = PyMapping_Size(env);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002443 if (i < 0)
2444 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00002445 envlist = PyMem_NEW(char *, i + 1);
2446 if (envlist == NULL) {
2447 PyErr_NoMemory();
2448 goto fail_1;
2449 }
2450 envc = 0;
2451 keys = PyMapping_Keys(env);
2452 vals = PyMapping_Values(env);
2453 if (!keys || !vals)
2454 goto fail_2;
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002455 if (!PyList_Check(keys) || !PyList_Check(vals)) {
2456 PyErr_SetString(PyExc_TypeError,
2457 "spawnve(): env.keys() or env.values() is not a list");
2458 goto fail_2;
2459 }
Tim Peters5aa91602002-01-30 05:46:57 +00002460
Guido van Rossuma1065681999-01-25 23:20:23 +00002461 for (pos = 0; pos < i; pos++) {
2462 char *p, *k, *v;
Tim Petersc8996f52001-12-03 20:41:00 +00002463 size_t len;
Guido van Rossuma1065681999-01-25 23:20:23 +00002464
2465 key = PyList_GetItem(keys, pos);
2466 val = PyList_GetItem(vals, pos);
2467 if (!key || !val)
2468 goto fail_2;
Tim Peters5aa91602002-01-30 05:46:57 +00002469
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002470 if (!PyArg_Parse(
2471 key,
2472 "s;spawnve() arg 3 contains a non-string key",
2473 &k) ||
2474 !PyArg_Parse(
2475 val,
2476 "s;spawnve() arg 3 contains a non-string value",
2477 &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00002478 {
2479 goto fail_2;
2480 }
Tim Petersc8996f52001-12-03 20:41:00 +00002481 len = PyString_Size(key) + PyString_Size(val) + 2;
2482 p = PyMem_NEW(char, len);
Guido van Rossuma1065681999-01-25 23:20:23 +00002483 if (p == NULL) {
2484 PyErr_NoMemory();
2485 goto fail_2;
2486 }
Tim Petersc8996f52001-12-03 20:41:00 +00002487 PyOS_snprintf(p, len, "%s=%s", k, v);
Guido van Rossuma1065681999-01-25 23:20:23 +00002488 envlist[envc++] = p;
2489 }
2490 envlist[envc] = 0;
2491
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002492#if defined(PYOS_OS2) && defined(PYCC_GCC)
2493 Py_BEGIN_ALLOW_THREADS
2494 spawnval = spawnve(mode, path, argvlist, envlist);
2495 Py_END_ALLOW_THREADS
2496#else
Guido van Rossum246bc171999-02-01 23:54:31 +00002497 if (mode == _OLD_P_OVERLAY)
2498 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00002499
2500 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00002501 spawnval = _spawnve(mode, path, argvlist, envlist);
Tim Peters25059d32001-12-07 20:35:43 +00002502 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002503#endif
Tim Peters25059d32001-12-07 20:35:43 +00002504
Fred Drake699f3522000-06-29 21:12:41 +00002505 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00002506 (void) posix_error();
2507 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00002508#if SIZEOF_LONG == SIZEOF_VOID_P
2509 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002510#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002511 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00002512#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00002513
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002514 fail_2:
Guido van Rossuma1065681999-01-25 23:20:23 +00002515 while (--envc >= 0)
2516 PyMem_DEL(envlist[envc]);
2517 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00002518 fail_1:
Martin v. Löwis114619e2002-10-07 06:44:21 +00002519 free_string_array(argvlist, lastarg);
Guido van Rossuma1065681999-01-25 23:20:23 +00002520 Py_XDECREF(vals);
2521 Py_XDECREF(keys);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002522 fail_0:
2523 PyMem_Free(path);
Guido van Rossuma1065681999-01-25 23:20:23 +00002524 return res;
2525}
2526#endif /* HAVE_SPAWNV */
2527
2528
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002529#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002530PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002531"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002532Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2533\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002535
2536static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002537posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002538{
Neal Norwitze241ce82003-02-17 18:17:05 +00002539 int pid = fork1();
Guido van Rossum2242f2f2001-04-11 20:58:20 +00002540 if (pid == -1)
2541 return posix_error();
2542 PyOS_AfterFork();
2543 return PyInt_FromLong((long)pid);
2544}
2545#endif
2546
2547
Guido van Rossumad0ee831995-03-01 10:34:45 +00002548#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002549PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002550"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002552Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002553
Barry Warsaw53699e91996-12-10 23:23:01 +00002554static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002555posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002556{
Neal Norwitze241ce82003-02-17 18:17:05 +00002557 int pid = fork();
Guido van Rossum85e3b011991-06-03 12:42:10 +00002558 if (pid == -1)
2559 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002560 if (pid == 0)
2561 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00002562 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002563}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002564#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002565
Neal Norwitzb59798b2003-03-21 01:43:31 +00002566/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00002567/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2568#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00002569#define DEV_PTY_FILE "/dev/ptc"
2570#define HAVE_DEV_PTMX
2571#else
2572#define DEV_PTY_FILE "/dev/ptmx"
2573#endif
2574
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002575#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002576#ifdef HAVE_PTY_H
2577#include <pty.h>
2578#else
2579#ifdef HAVE_LIBUTIL_H
2580#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00002581#endif /* HAVE_LIBUTIL_H */
2582#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00002583#ifdef HAVE_STROPTS_H
2584#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002585#endif
2586#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002587
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002588#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002589PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002590"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002591Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002592
2593static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002594posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002595{
2596 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002597#ifndef HAVE_OPENPTY
2598 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00002599#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002600#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002601 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002602#ifdef sun
2603 extern char *ptsname();
2604#endif
2605#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00002606
Thomas Wouters70c21a12000-07-14 14:28:33 +00002607#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00002608 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
2609 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002610#elif defined(HAVE__GETPTY)
Thomas Wouters70c21a12000-07-14 14:28:33 +00002611 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
2612 if (slave_name == NULL)
2613 return posix_error();
2614
2615 slave_fd = open(slave_name, O_RDWR);
2616 if (slave_fd < 0)
2617 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002618#else
Neal Norwitzb59798b2003-03-21 01:43:31 +00002619 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002620 if (master_fd < 0)
2621 return posix_error();
2622 sig_saved = signal(SIGCHLD, SIG_DFL);
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002623 /* change permission of slave */
2624 if (grantpt(master_fd) < 0) {
2625 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002626 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002627 }
2628 /* unlock slave */
2629 if (unlockpt(master_fd) < 0) {
2630 signal(SIGCHLD, sig_saved);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002631 return posix_error();
Martin v. Löwisc8b2e772002-12-31 14:30:26 +00002632 }
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002633 signal(SIGCHLD, sig_saved);
2634 slave_name = ptsname(master_fd); /* get name of slave */
2635 if (slave_name == NULL)
2636 return posix_error();
2637 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
2638 if (slave_fd < 0)
2639 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00002640#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002641 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
2642 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00002643#ifndef __hpux
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002644 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00002645#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002646#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00002647#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00002648
Fred Drake8cef4cf2000-06-28 16:40:38 +00002649 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00002650
Fred Drake8cef4cf2000-06-28 16:40:38 +00002651}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00002652#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00002653
2654#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002655PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002656"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00002657Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2658Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002659To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00002660
2661static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002662posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00002663{
2664 int master_fd, pid;
Tim Peters5aa91602002-01-30 05:46:57 +00002665
Fred Drake8cef4cf2000-06-28 16:40:38 +00002666 pid = forkpty(&master_fd, NULL, NULL, NULL);
2667 if (pid == -1)
2668 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00002669 if (pid == 0)
2670 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00002671 return Py_BuildValue("(ii)", pid, master_fd);
2672}
2673#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002674
Guido van Rossumad0ee831995-03-01 10:34:45 +00002675#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002676PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002677"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002679
Barry Warsaw53699e91996-12-10 23:23:01 +00002680static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002681posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002682{
Barry Warsaw53699e91996-12-10 23:23:01 +00002683 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002684}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002685#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002686
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002687
Guido van Rossumad0ee831995-03-01 10:34:45 +00002688#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002689PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002690"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002691Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002692
Barry Warsaw53699e91996-12-10 23:23:01 +00002693static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002694posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002695{
Barry Warsaw53699e91996-12-10 23:23:01 +00002696 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002697}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002698#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002699
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002700
Guido van Rossumad0ee831995-03-01 10:34:45 +00002701#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002703"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002704Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002705
Barry Warsaw53699e91996-12-10 23:23:01 +00002706static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002707posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002708{
Barry Warsaw53699e91996-12-10 23:23:01 +00002709 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002710}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002711#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002714PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002715"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002716Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002717
Barry Warsaw53699e91996-12-10 23:23:01 +00002718static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002719posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002720{
Barry Warsaw53699e91996-12-10 23:23:01 +00002721 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002722}
2723
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002724
Fred Drakec9680921999-12-13 16:37:25 +00002725#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002726PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002727"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002728Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00002729
2730static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002731posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00002732{
2733 PyObject *result = NULL;
2734
Fred Drakec9680921999-12-13 16:37:25 +00002735#ifdef NGROUPS_MAX
2736#define MAX_GROUPS NGROUPS_MAX
2737#else
2738 /* defined to be 16 on Solaris7, so this should be a small number */
2739#define MAX_GROUPS 64
2740#endif
2741 gid_t grouplist[MAX_GROUPS];
2742 int n;
2743
2744 n = getgroups(MAX_GROUPS, grouplist);
2745 if (n < 0)
2746 posix_error();
2747 else {
2748 result = PyList_New(n);
2749 if (result != NULL) {
Fred Drakec9680921999-12-13 16:37:25 +00002750 int i;
2751 for (i = 0; i < n; ++i) {
Neal Norwitze241ce82003-02-17 18:17:05 +00002752 PyObject *o = PyInt_FromLong((long)grouplist[i]);
Fred Drakec9680921999-12-13 16:37:25 +00002753 if (o == NULL) {
2754 Py_DECREF(result);
2755 result = NULL;
2756 break;
2757 }
2758 PyList_SET_ITEM(result, i, o);
2759 }
2760 }
2761 }
Neal Norwitze241ce82003-02-17 18:17:05 +00002762
Fred Drakec9680921999-12-13 16:37:25 +00002763 return result;
2764}
2765#endif
2766
Martin v. Löwis606edc12002-06-13 21:09:11 +00002767#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002768PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002769"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00002770Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00002771
2772static PyObject *
2773posix_getpgid(PyObject *self, PyObject *args)
2774{
2775 int pid, pgid;
2776 if (!PyArg_ParseTuple(args, "i:getpgid", &pid))
2777 return NULL;
2778 pgid = getpgid(pid);
2779 if (pgid < 0)
2780 return posix_error();
2781 return PyInt_FromLong((long)pgid);
2782}
2783#endif /* HAVE_GETPGID */
2784
2785
Guido van Rossumb6775db1994-08-01 11:34:53 +00002786#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002787PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002788"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002789Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002790
Barry Warsaw53699e91996-12-10 23:23:01 +00002791static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002792posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00002793{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002794#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00002795 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002796#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00002797 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002798#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00002799}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002800#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00002801
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002802
Guido van Rossumb6775db1994-08-01 11:34:53 +00002803#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002804PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002805"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002806Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002807
Barry Warsaw53699e91996-12-10 23:23:01 +00002808static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002809posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00002810{
Guido van Rossum64933891994-10-20 21:56:42 +00002811#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00002812 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002813#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002814 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00002815#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00002816 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002817 Py_INCREF(Py_None);
2818 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002819}
2820
Guido van Rossumb6775db1994-08-01 11:34:53 +00002821#endif /* HAVE_SETPGRP */
2822
Guido van Rossumad0ee831995-03-01 10:34:45 +00002823#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002824PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002825"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002827
Barry Warsaw53699e91996-12-10 23:23:01 +00002828static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002829posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002830{
Barry Warsaw53699e91996-12-10 23:23:01 +00002831 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00002832}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002833#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002835
Fred Drake12c6e2d1999-12-14 21:25:03 +00002836#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002837PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002838"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002839Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00002840
2841static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002842posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00002843{
Neal Norwitze241ce82003-02-17 18:17:05 +00002844 PyObject *result = NULL;
Fred Drakea30680b2000-12-06 21:24:28 +00002845 char *name;
2846 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00002847
Fred Drakea30680b2000-12-06 21:24:28 +00002848 errno = 0;
2849 name = getlogin();
2850 if (name == NULL) {
2851 if (errno)
2852 posix_error();
2853 else
2854 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00002855 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00002856 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00002857 else
2858 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00002859 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00002860
Fred Drake12c6e2d1999-12-14 21:25:03 +00002861 return result;
2862}
2863#endif
2864
Guido van Rossumad0ee831995-03-01 10:34:45 +00002865#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002866PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002867"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002868Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002869
Barry Warsaw53699e91996-12-10 23:23:01 +00002870static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002871posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00002872{
Barry Warsaw53699e91996-12-10 23:23:01 +00002873 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00002874}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002875#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00002876
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002877
Guido van Rossumad0ee831995-03-01 10:34:45 +00002878#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002879PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002880"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002881Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002882
Barry Warsaw53699e91996-12-10 23:23:01 +00002883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002884posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002885{
2886 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002887 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00002888 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002889#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002890 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
2891 APIRET rc;
2892 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002893 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002894
2895 } else if (sig == XCPT_SIGNAL_KILLPROC) {
2896 APIRET rc;
2897 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002898 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002899
2900 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002901 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002902#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00002903 if (kill(pid, sig) == -1)
2904 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002905#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002906 Py_INCREF(Py_None);
2907 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002908}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002909#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002910
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002911#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002912PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002913"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002914Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00002915
2916static PyObject *
2917posix_killpg(PyObject *self, PyObject *args)
2918{
2919 int pgid, sig;
2920 if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig))
2921 return NULL;
2922 if (killpg(pgid, sig) == -1)
2923 return posix_error();
2924 Py_INCREF(Py_None);
2925 return Py_None;
2926}
2927#endif
2928
Guido van Rossumc0125471996-06-28 18:55:32 +00002929#ifdef HAVE_PLOCK
2930
2931#ifdef HAVE_SYS_LOCK_H
2932#include <sys/lock.h>
2933#endif
2934
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002935PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002936"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002937Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002938
Barry Warsaw53699e91996-12-10 23:23:01 +00002939static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002940posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00002941{
2942 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002943 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00002944 return NULL;
2945 if (plock(op) == -1)
2946 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002947 Py_INCREF(Py_None);
2948 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00002949}
2950#endif
2951
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002952
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002953#ifdef HAVE_POPEN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002954PyDoc_STRVAR(posix_popen__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002955"popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002956Open a pipe to/from a command returning a file object.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002957
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002958#if defined(PYOS_OS2)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002959#if defined(PYCC_VACPP)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002960static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002961async_system(const char *command)
2962{
2963 char *p, errormsg[256], args[1024];
2964 RESULTCODES rcodes;
2965 APIRET rc;
2966 char *shell = getenv("COMSPEC");
2967 if (!shell)
2968 shell = "cmd";
2969
2970 strcpy(args, shell);
2971 p = &args[ strlen(args)+1 ];
2972 strcpy(p, "/c ");
2973 strcat(p, command);
2974 p += strlen(p) + 1;
2975 *p = '\0';
2976
2977 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002978 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002979 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002980 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002981 &rcodes, shell);
2982 return rc;
2983}
2984
Guido van Rossumd48f2521997-12-05 22:19:34 +00002985static FILE *
2986popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002987{
2988 HFILE rhan, whan;
2989 FILE *retfd = NULL;
2990 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2991
Guido van Rossumd48f2521997-12-05 22:19:34 +00002992 if (rc != NO_ERROR) {
2993 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002994 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002995 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002996
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002997 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2998 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002999
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003000 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3001 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003002
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003003 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
3004 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003005
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003006 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003007 }
3008
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003009 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
3010 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003011
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003012 if (rc == NO_ERROR)
3013 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
3014
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003015 close(oldfd); /* And Close Saved STDOUT Handle */
3016 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003017
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003018 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
3019 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003020
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003021 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
3022 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003023
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003024 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
3025 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003026
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003027 rc = async_system(command);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003028 }
3029
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003030 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
3031 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003032
Martin v. Löwisdedbe252001-11-02 23:59:11 +00003033 if (rc == NO_ERROR)
3034 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
3035
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003036 close(oldfd); /* And Close Saved STDIN Handle */
3037 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003038
Guido van Rossumd48f2521997-12-05 22:19:34 +00003039 } else {
3040 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003041 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00003042 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003043}
3044
3045static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003046posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003047{
3048 char *name;
3049 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00003050 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003051 FILE *fp;
3052 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003053 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003054 return NULL;
3055 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00003056 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003057 Py_END_ALLOW_THREADS
3058 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003059 return os2_error(err);
3060
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003061 f = PyFile_FromFile(fp, name, mode, fclose);
3062 if (f != NULL)
3063 PyFile_SetBufSize(f, bufsize);
3064 return f;
3065}
3066
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003067#elif defined(PYCC_GCC)
3068
3069/* standard posix version of popen() support */
3070static PyObject *
3071posix_popen(PyObject *self, PyObject *args)
3072{
3073 char *name;
3074 char *mode = "r";
3075 int bufsize = -1;
3076 FILE *fp;
3077 PyObject *f;
3078 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
3079 return NULL;
3080 Py_BEGIN_ALLOW_THREADS
3081 fp = popen(name, mode);
3082 Py_END_ALLOW_THREADS
3083 if (fp == NULL)
3084 return posix_error();
3085 f = PyFile_FromFile(fp, name, mode, pclose);
3086 if (f != NULL)
3087 PyFile_SetBufSize(f, bufsize);
3088 return f;
3089}
3090
3091/* fork() under OS/2 has lots'o'warts
3092 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
3093 * most of this code is a ripoff of the win32 code, but using the
3094 * capabilities of EMX's C library routines
3095 */
3096
3097/* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
3098#define POPEN_1 1
3099#define POPEN_2 2
3100#define POPEN_3 3
3101#define POPEN_4 4
3102
3103static PyObject *_PyPopen(char *, int, int, int);
3104static int _PyPclose(FILE *file);
3105
3106/*
3107 * Internal dictionary mapping popen* file pointers to process handles,
3108 * for use when retrieving the process exit code. See _PyPclose() below
3109 * for more information on this dictionary's use.
3110 */
3111static PyObject *_PyPopenProcs = NULL;
3112
3113/* os2emx version of popen2()
3114 *
3115 * The result of this function is a pipe (file) connected to the
3116 * process's stdin, and a pipe connected to the process's stdout.
3117 */
3118
3119static PyObject *
3120os2emx_popen2(PyObject *self, PyObject *args)
3121{
3122 PyObject *f;
3123 int tm=0;
3124
3125 char *cmdstring;
3126 char *mode = "t";
3127 int bufsize = -1;
3128 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
3129 return NULL;
3130
3131 if (*mode == 't')
3132 tm = O_TEXT;
3133 else if (*mode != 'b') {
3134 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3135 return NULL;
3136 } else
3137 tm = O_BINARY;
3138
3139 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
3140
3141 return f;
3142}
3143
3144/*
3145 * Variation on os2emx.popen2
3146 *
3147 * The result of this function is 3 pipes - the process's stdin,
3148 * stdout and stderr
3149 */
3150
3151static PyObject *
3152os2emx_popen3(PyObject *self, PyObject *args)
3153{
3154 PyObject *f;
3155 int tm = 0;
3156
3157 char *cmdstring;
3158 char *mode = "t";
3159 int bufsize = -1;
3160 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
3161 return NULL;
3162
3163 if (*mode == 't')
3164 tm = O_TEXT;
3165 else if (*mode != 'b') {
3166 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3167 return NULL;
3168 } else
3169 tm = O_BINARY;
3170
3171 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
3172
3173 return f;
3174}
3175
3176/*
3177 * Variation on os2emx.popen2
3178 *
Tim Peters11b23062003-04-23 02:39:17 +00003179 * The result of this function is 2 pipes - the processes stdin,
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003180 * and stdout+stderr combined as a single pipe.
3181 */
3182
3183static PyObject *
3184os2emx_popen4(PyObject *self, PyObject *args)
3185{
3186 PyObject *f;
3187 int tm = 0;
3188
3189 char *cmdstring;
3190 char *mode = "t";
3191 int bufsize = -1;
3192 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
3193 return NULL;
3194
3195 if (*mode == 't')
3196 tm = O_TEXT;
3197 else if (*mode != 'b') {
3198 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
3199 return NULL;
3200 } else
3201 tm = O_BINARY;
3202
3203 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
3204
3205 return f;
3206}
3207
3208/* a couple of structures for convenient handling of multiple
3209 * file handles and pipes
3210 */
3211struct file_ref
3212{
3213 int handle;
3214 int flags;
3215};
3216
3217struct pipe_ref
3218{
3219 int rd;
3220 int wr;
3221};
3222
3223/* The following code is derived from the win32 code */
3224
3225static PyObject *
3226_PyPopen(char *cmdstring, int mode, int n, int bufsize)
3227{
3228 struct file_ref stdio[3];
3229 struct pipe_ref p_fd[3];
3230 FILE *p_s[3];
3231 int file_count, i, pipe_err, pipe_pid;
3232 char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
3233 PyObject *f, *p_f[3];
3234
3235 /* file modes for subsequent fdopen's on pipe handles */
3236 if (mode == O_TEXT)
3237 {
3238 rd_mode = "rt";
3239 wr_mode = "wt";
3240 }
3241 else
3242 {
3243 rd_mode = "rb";
3244 wr_mode = "wb";
3245 }
3246
3247 /* prepare shell references */
3248 if ((shell = getenv("EMXSHELL")) == NULL)
3249 if ((shell = getenv("COMSPEC")) == NULL)
3250 {
3251 errno = ENOENT;
3252 return posix_error();
3253 }
3254
3255 sh_name = _getname(shell);
3256 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
3257 opt = "/c";
3258 else
3259 opt = "-c";
3260
3261 /* save current stdio fds + their flags, and set not inheritable */
3262 i = pipe_err = 0;
3263 while (pipe_err >= 0 && i < 3)
3264 {
3265 pipe_err = stdio[i].handle = dup(i);
3266 stdio[i].flags = fcntl(i, F_GETFD, 0);
3267 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
3268 i++;
3269 }
3270 if (pipe_err < 0)
3271 {
3272 /* didn't get them all saved - clean up and bail out */
3273 int saved_err = errno;
3274 while (i-- > 0)
3275 {
3276 close(stdio[i].handle);
3277 }
3278 errno = saved_err;
3279 return posix_error();
3280 }
3281
3282 /* create pipe ends */
3283 file_count = 2;
3284 if (n == POPEN_3)
3285 file_count = 3;
3286 i = pipe_err = 0;
3287 while ((pipe_err == 0) && (i < file_count))
3288 pipe_err = pipe((int *)&p_fd[i++]);
3289 if (pipe_err < 0)
3290 {
3291 /* didn't get them all made - clean up and bail out */
3292 while (i-- > 0)
3293 {
3294 close(p_fd[i].wr);
3295 close(p_fd[i].rd);
3296 }
3297 errno = EPIPE;
3298 return posix_error();
3299 }
3300
3301 /* change the actual standard IO streams over temporarily,
3302 * making the retained pipe ends non-inheritable
3303 */
3304 pipe_err = 0;
3305
3306 /* - stdin */
3307 if (dup2(p_fd[0].rd, 0) == 0)
3308 {
3309 close(p_fd[0].rd);
3310 i = fcntl(p_fd[0].wr, F_GETFD, 0);
3311 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
3312 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
3313 {
3314 close(p_fd[0].wr);
3315 pipe_err = -1;
3316 }
3317 }
3318 else
3319 {
3320 pipe_err = -1;
3321 }
3322
3323 /* - stdout */
3324 if (pipe_err == 0)
3325 {
3326 if (dup2(p_fd[1].wr, 1) == 1)
3327 {
3328 close(p_fd[1].wr);
3329 i = fcntl(p_fd[1].rd, F_GETFD, 0);
3330 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
3331 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
3332 {
3333 close(p_fd[1].rd);
3334 pipe_err = -1;
3335 }
3336 }
3337 else
3338 {
3339 pipe_err = -1;
3340 }
3341 }
3342
3343 /* - stderr, as required */
3344 if (pipe_err == 0)
3345 switch (n)
3346 {
3347 case POPEN_3:
3348 {
3349 if (dup2(p_fd[2].wr, 2) == 2)
3350 {
3351 close(p_fd[2].wr);
3352 i = fcntl(p_fd[2].rd, F_GETFD, 0);
3353 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
3354 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
3355 {
3356 close(p_fd[2].rd);
3357 pipe_err = -1;
3358 }
3359 }
3360 else
3361 {
3362 pipe_err = -1;
3363 }
3364 break;
3365 }
3366
3367 case POPEN_4:
3368 {
3369 if (dup2(1, 2) != 2)
3370 {
3371 pipe_err = -1;
3372 }
3373 break;
3374 }
3375 }
3376
3377 /* spawn the child process */
3378 if (pipe_err == 0)
3379 {
3380 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
3381 if (pipe_pid == -1)
3382 {
3383 pipe_err = -1;
3384 }
3385 else
3386 {
3387 /* save the PID into the FILE structure
3388 * NOTE: this implementation doesn't actually
3389 * take advantage of this, but do it for
3390 * completeness - AIM Apr01
3391 */
3392 for (i = 0; i < file_count; i++)
3393 p_s[i]->_pid = pipe_pid;
3394 }
3395 }
3396
3397 /* reset standard IO to normal */
3398 for (i = 0; i < 3; i++)
3399 {
3400 dup2(stdio[i].handle, i);
3401 fcntl(i, F_SETFD, stdio[i].flags);
3402 close(stdio[i].handle);
3403 }
3404
3405 /* if any remnant problems, clean up and bail out */
3406 if (pipe_err < 0)
3407 {
3408 for (i = 0; i < 3; i++)
3409 {
3410 close(p_fd[i].rd);
3411 close(p_fd[i].wr);
3412 }
3413 errno = EPIPE;
3414 return posix_error_with_filename(cmdstring);
3415 }
3416
3417 /* build tuple of file objects to return */
3418 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
3419 PyFile_SetBufSize(p_f[0], bufsize);
3420 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
3421 PyFile_SetBufSize(p_f[1], bufsize);
3422 if (n == POPEN_3)
3423 {
3424 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
3425 PyFile_SetBufSize(p_f[0], bufsize);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003426 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003427 }
3428 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003429 f = PyTuple_Pack(2, p_f[0], p_f[1]);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003430
3431 /*
3432 * Insert the files we've created into the process dictionary
3433 * all referencing the list with the process handle and the
3434 * initial number of files (see description below in _PyPclose).
3435 * Since if _PyPclose later tried to wait on a process when all
3436 * handles weren't closed, it could create a deadlock with the
3437 * child, we spend some energy here to try to ensure that we
3438 * either insert all file handles into the dictionary or none
3439 * at all. It's a little clumsy with the various popen modes
3440 * and variable number of files involved.
3441 */
3442 if (!_PyPopenProcs)
3443 {
3444 _PyPopenProcs = PyDict_New();
3445 }
3446
3447 if (_PyPopenProcs)
3448 {
3449 PyObject *procObj, *pidObj, *intObj, *fileObj[3];
3450 int ins_rc[3];
3451
3452 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
3453 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
3454
3455 procObj = PyList_New(2);
3456 pidObj = PyInt_FromLong((long) pipe_pid);
3457 intObj = PyInt_FromLong((long) file_count);
3458
3459 if (procObj && pidObj && intObj)
3460 {
3461 PyList_SetItem(procObj, 0, pidObj);
3462 PyList_SetItem(procObj, 1, intObj);
3463
3464 fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
3465 if (fileObj[0])
3466 {
3467 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
3468 fileObj[0],
3469 procObj);
3470 }
3471 fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
3472 if (fileObj[1])
3473 {
3474 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
3475 fileObj[1],
3476 procObj);
3477 }
3478 if (file_count >= 3)
3479 {
3480 fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
3481 if (fileObj[2])
3482 {
3483 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
3484 fileObj[2],
3485 procObj);
3486 }
3487 }
3488
3489 if (ins_rc[0] < 0 || !fileObj[0] ||
3490 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
3491 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
3492 {
3493 /* Something failed - remove any dictionary
3494 * entries that did make it.
3495 */
3496 if (!ins_rc[0] && fileObj[0])
3497 {
3498 PyDict_DelItem(_PyPopenProcs,
3499 fileObj[0]);
3500 }
3501 if (!ins_rc[1] && fileObj[1])
3502 {
3503 PyDict_DelItem(_PyPopenProcs,
3504 fileObj[1]);
3505 }
3506 if (!ins_rc[2] && fileObj[2])
3507 {
3508 PyDict_DelItem(_PyPopenProcs,
3509 fileObj[2]);
3510 }
3511 }
3512 }
Tim Peters11b23062003-04-23 02:39:17 +00003513
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003514 /*
3515 * Clean up our localized references for the dictionary keys
3516 * and value since PyDict_SetItem will Py_INCREF any copies
3517 * that got placed in the dictionary.
3518 */
3519 Py_XDECREF(procObj);
3520 Py_XDECREF(fileObj[0]);
3521 Py_XDECREF(fileObj[1]);
3522 Py_XDECREF(fileObj[2]);
3523 }
3524
3525 /* Child is launched. */
3526 return f;
3527}
3528
3529/*
3530 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3531 * exit code for the child process and return as a result of the close.
3532 *
3533 * This function uses the _PyPopenProcs dictionary in order to map the
3534 * input file pointer to information about the process that was
3535 * originally created by the popen* call that created the file pointer.
3536 * The dictionary uses the file pointer as a key (with one entry
3537 * inserted for each file returned by the original popen* call) and a
3538 * single list object as the value for all files from a single call.
3539 * The list object contains the Win32 process handle at [0], and a file
3540 * count at [1], which is initialized to the total number of file
3541 * handles using that list.
3542 *
3543 * This function closes whichever handle it is passed, and decrements
3544 * the file count in the dictionary for the process handle pointed to
3545 * by this file. On the last close (when the file count reaches zero),
3546 * this function will wait for the child process and then return its
3547 * exit code as the result of the close() operation. This permits the
3548 * files to be closed in any order - it is always the close() of the
3549 * final handle that will return the exit code.
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003550 *
3551 * NOTE: This function is currently called with the GIL released.
3552 * hence we use the GILState API to manage our state.
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003553 */
3554
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003555static int _PyPclose(FILE *file)
3556{
3557 int result;
3558 int exit_code;
3559 int pipe_pid;
3560 PyObject *procObj, *pidObj, *intObj, *fileObj;
3561 int file_count;
3562#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003563 PyGILState_STATE state;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003564#endif
3565
3566 /* Close the file handle first, to ensure it can't block the
3567 * child from exiting if it's the last handle.
3568 */
3569 result = fclose(file);
3570
3571#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003572 state = PyGILState_Ensure();
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003573#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003574 if (_PyPopenProcs)
3575 {
3576 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
3577 (procObj = PyDict_GetItem(_PyPopenProcs,
3578 fileObj)) != NULL &&
3579 (pidObj = PyList_GetItem(procObj,0)) != NULL &&
3580 (intObj = PyList_GetItem(procObj,1)) != NULL)
3581 {
3582 pipe_pid = (int) PyInt_AsLong(pidObj);
3583 file_count = (int) PyInt_AsLong(intObj);
3584
3585 if (file_count > 1)
3586 {
3587 /* Still other files referencing process */
3588 file_count--;
3589 PyList_SetItem(procObj,1,
3590 PyInt_FromLong((long) file_count));
3591 }
3592 else
3593 {
3594 /* Last file for this process */
3595 if (result != EOF &&
3596 waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
3597 {
3598 /* extract exit status */
3599 if (WIFEXITED(exit_code))
3600 {
3601 result = WEXITSTATUS(exit_code);
3602 }
3603 else
3604 {
3605 errno = EPIPE;
3606 result = -1;
3607 }
3608 }
3609 else
3610 {
3611 /* Indicate failure - this will cause the file object
3612 * to raise an I/O error and translate the last
3613 * error code from errno. We do have a problem with
3614 * last errors that overlap the normal errno table,
3615 * but that's a consistent problem with the file object.
3616 */
3617 result = -1;
3618 }
3619 }
3620
3621 /* Remove this file pointer from dictionary */
3622 PyDict_DelItem(_PyPopenProcs, fileObj);
3623
3624 if (PyDict_Size(_PyPopenProcs) == 0)
3625 {
3626 Py_DECREF(_PyPopenProcs);
3627 _PyPopenProcs = NULL;
3628 }
3629
3630 } /* if object retrieval ok */
3631
3632 Py_XDECREF(fileObj);
3633 } /* if _PyPopenProcs */
3634
3635#ifdef WITH_THREAD
Andrew MacIntyrebaf25b02003-04-21 14:22:36 +00003636 PyGILState_Release(state);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003637#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003638 return result;
3639}
3640
3641#endif /* PYCC_??? */
3642
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003643#elif defined(MS_WINDOWS)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003644
3645/*
3646 * Portable 'popen' replacement for Win32.
3647 *
3648 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3649 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00003650 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003651 */
3652
3653#include <malloc.h>
3654#include <io.h>
3655#include <fcntl.h>
3656
3657/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3658#define POPEN_1 1
3659#define POPEN_2 2
3660#define POPEN_3 3
3661#define POPEN_4 4
3662
3663static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003664static int _PyPclose(FILE *file);
3665
3666/*
3667 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00003668 * for use when retrieving the process exit code. See _PyPclose() below
3669 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00003670 */
3671static PyObject *_PyPopenProcs = NULL;
3672
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003673
3674/* popen that works from a GUI.
3675 *
3676 * The result of this function is a pipe (file) connected to the
3677 * processes stdin or stdout, depending on the requested mode.
3678 */
3679
3680static PyObject *
3681posix_popen(PyObject *self, PyObject *args)
3682{
Raymond Hettingerb5cb6652003-09-01 22:25:41 +00003683 PyObject *f;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003684 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003685
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003686 char *cmdstring;
3687 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003688 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003689 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003690 return NULL;
3691
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003692 if (*mode == 'r')
3693 tm = _O_RDONLY;
3694 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00003695 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003696 return NULL;
3697 } else
3698 tm = _O_WRONLY;
Tim Peters5aa91602002-01-30 05:46:57 +00003699
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003700 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003701 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003702 return NULL;
3703 }
3704
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003705 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003706 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003707 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003708 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003709 else
3710 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
3711
3712 return f;
3713}
3714
3715/* Variation on win32pipe.popen
3716 *
3717 * The result of this function is a pipe (file) connected to the
3718 * process's stdin, and a pipe connected to the process's stdout.
3719 */
3720
3721static PyObject *
3722win32_popen2(PyObject *self, PyObject *args)
3723{
3724 PyObject *f;
3725 int tm=0;
Tim Peters5aa91602002-01-30 05:46:57 +00003726
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003727 char *cmdstring;
3728 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003729 int bufsize = -1;
3730 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003731 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003732
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003733 if (*mode == 't')
3734 tm = _O_TEXT;
3735 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003736 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003737 return NULL;
3738 } else
3739 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003740
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003741 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003742 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003743 return NULL;
3744 }
3745
3746 f = _PyPopen(cmdstring, tm, POPEN_2);
Tim Peters5aa91602002-01-30 05:46:57 +00003747
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003748 return f;
3749}
3750
3751/*
3752 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003753 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003754 * The result of this function is 3 pipes - the process's stdin,
3755 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003756 */
3757
3758static PyObject *
3759win32_popen3(PyObject *self, PyObject *args)
3760{
3761 PyObject *f;
3762 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003763
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003764 char *cmdstring;
3765 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003766 int bufsize = -1;
3767 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003768 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003769
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003770 if (*mode == 't')
3771 tm = _O_TEXT;
3772 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003773 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003774 return NULL;
3775 } else
3776 tm = _O_BINARY;
Tim Peters5aa91602002-01-30 05:46:57 +00003777
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003778 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003779 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003780 return NULL;
3781 }
3782
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003783 f = _PyPopen(cmdstring, tm, POPEN_3);
Tim Peters5aa91602002-01-30 05:46:57 +00003784
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003785 return f;
3786}
3787
3788/*
3789 * Variation on win32pipe.popen
3790 *
Tim Peters5aa91602002-01-30 05:46:57 +00003791 * The result of this function is 2 pipes - the processes stdin,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003792 * and stdout+stderr combined as a single pipe.
3793 */
3794
3795static PyObject *
3796win32_popen4(PyObject *self, PyObject *args)
3797{
3798 PyObject *f;
3799 int tm = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00003800
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003801 char *cmdstring;
3802 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003803 int bufsize = -1;
3804 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003805 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003806
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003807 if (*mode == 't')
3808 tm = _O_TEXT;
3809 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00003810 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003811 return NULL;
3812 } else
3813 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003814
3815 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00003816 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003817 return NULL;
3818 }
3819
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00003820 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00003821
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003822 return f;
3823}
3824
Mark Hammond08501372001-01-31 07:30:29 +00003825static BOOL
Mark Hammondb37a3732000-08-14 04:47:33 +00003826_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003827 HANDLE hStdin,
3828 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00003829 HANDLE hStderr,
3830 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003831{
3832 PROCESS_INFORMATION piProcInfo;
3833 STARTUPINFO siStartInfo;
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003834 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003835 char *s1,*s2, *s3 = " /c ";
Mark Hammond08501372001-01-31 07:30:29 +00003836 const char *szConsoleSpawn = "w9xpopen.exe";
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003837 int i;
3838 int x;
3839
3840 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
Tim Peters402d5982001-08-27 06:37:48 +00003841 char *comshell;
3842
Tim Peters92e4dd82002-10-05 01:47:34 +00003843 s1 = (char *)alloca(i);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003844 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
3845 return x;
Tim Peters402d5982001-08-27 06:37:48 +00003846
3847 /* Explicitly check if we are using COMMAND.COM. If we are
3848 * then use the w9xpopen hack.
3849 */
3850 comshell = s1 + x;
3851 while (comshell >= s1 && *comshell != '\\')
3852 --comshell;
3853 ++comshell;
3854
3855 if (GetVersion() < 0x80000000 &&
3856 _stricmp(comshell, "command.com") != 0) {
3857 /* NT/2000 and not using command.com. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003858 x = i + strlen(s3) + strlen(cmdstring) + 1;
Tim Peters92e4dd82002-10-05 01:47:34 +00003859 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003860 ZeroMemory(s2, x);
Tim Peters75cdad52001-11-28 22:07:30 +00003861 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003862 }
3863 else {
3864 /*
Tim Peters402d5982001-08-27 06:37:48 +00003865 * Oh gag, we're on Win9x or using COMMAND.COM. Use
3866 * the workaround listed in KB: Q150956
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003867 */
Mark Hammond08501372001-01-31 07:30:29 +00003868 char modulepath[_MAX_PATH];
3869 struct stat statinfo;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003870 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
3871 for (i = x = 0; modulepath[i]; i++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003872 if (modulepath[i] == SEP)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003873 x = i+1;
3874 modulepath[x] = '\0';
Mark Hammond08501372001-01-31 07:30:29 +00003875 /* Create the full-name to w9xpopen, so we can test it exists */
Tim Peters5aa91602002-01-30 05:46:57 +00003876 strncat(modulepath,
3877 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003878 (sizeof(modulepath)/sizeof(modulepath[0]))
3879 -strlen(modulepath));
3880 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003881 /* Eeek - file-not-found - possibly an embedding
3882 situation - see if we can locate it in sys.prefix
Mark Hammond08501372001-01-31 07:30:29 +00003883 */
Tim Peters5aa91602002-01-30 05:46:57 +00003884 strncpy(modulepath,
3885 Py_GetExecPrefix(),
Mark Hammond08501372001-01-31 07:30:29 +00003886 sizeof(modulepath)/sizeof(modulepath[0]));
3887 if (modulepath[strlen(modulepath)-1] != '\\')
3888 strcat(modulepath, "\\");
Tim Peters5aa91602002-01-30 05:46:57 +00003889 strncat(modulepath,
3890 szConsoleSpawn,
Mark Hammond08501372001-01-31 07:30:29 +00003891 (sizeof(modulepath)/sizeof(modulepath[0]))
3892 -strlen(modulepath));
3893 /* No where else to look - raise an easily identifiable
3894 error, rather than leaving Windows to report
3895 "file not found" - as the user is probably blissfully
3896 unaware this shim EXE is used, and it will confuse them.
3897 (well, it confused me for a while ;-)
3898 */
3899 if (stat(modulepath, &statinfo) != 0) {
Tim Peters5aa91602002-01-30 05:46:57 +00003900 PyErr_Format(PyExc_RuntimeError,
Mark Hammond08501372001-01-31 07:30:29 +00003901 "Can not locate '%s' which is needed "
Tim Peters402d5982001-08-27 06:37:48 +00003902 "for popen to work with your shell "
3903 "or platform.",
Mark Hammond08501372001-01-31 07:30:29 +00003904 szConsoleSpawn);
3905 return FALSE;
3906 }
3907 }
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003908 x = i + strlen(s3) + strlen(cmdstring) + 1 +
Tim Peters5aa91602002-01-30 05:46:57 +00003909 strlen(modulepath) +
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003910 strlen(szConsoleSpawn) + 1;
Mark Hammond08501372001-01-31 07:30:29 +00003911
Tim Peters92e4dd82002-10-05 01:47:34 +00003912 s2 = (char *)alloca(x);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003913 ZeroMemory(s2, x);
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003914 /* To maintain correct argument passing semantics,
3915 we pass the command-line as it stands, and allow
3916 quoting to be applied. w9xpopen.exe will then
3917 use its argv vector, and re-quote the necessary
3918 args for the ultimate child process.
3919 */
Tim Peters75cdad52001-11-28 22:07:30 +00003920 PyOS_snprintf(
3921 s2, x,
Mark Hammonde7fefbf2002-04-03 01:47:00 +00003922 "\"%s\" %s%s%s",
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003923 modulepath,
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003924 s1,
3925 s3,
3926 cmdstring);
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003927 /* Not passing CREATE_NEW_CONSOLE has been known to
Tim Peters11b23062003-04-23 02:39:17 +00003928 cause random failures on win9x. Specifically a
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003929 dialog:
3930 "Your program accessed mem currently in use at xxx"
3931 and a hopeful warning about the stability of your
3932 system.
3933 Cost is Ctrl+C wont kill children, but anyone
3934 who cares can have a go!
3935 */
3936 dwProcessFlags |= CREATE_NEW_CONSOLE;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003937 }
3938 }
3939
3940 /* Could be an else here to try cmd.exe / command.com in the path
3941 Now we'll just error out.. */
Mark Hammond08501372001-01-31 07:30:29 +00003942 else {
Tim Peters402d5982001-08-27 06:37:48 +00003943 PyErr_SetString(PyExc_RuntimeError,
3944 "Cannot locate a COMSPEC environment variable to "
3945 "use as the shell");
Mark Hammond08501372001-01-31 07:30:29 +00003946 return FALSE;
3947 }
Tim Peters5aa91602002-01-30 05:46:57 +00003948
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003949 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
3950 siStartInfo.cb = sizeof(STARTUPINFO);
3951 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
3952 siStartInfo.hStdInput = hStdin;
3953 siStartInfo.hStdOutput = hStdout;
3954 siStartInfo.hStdError = hStderr;
3955 siStartInfo.wShowWindow = SW_HIDE;
3956
3957 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003958 s2,
3959 NULL,
3960 NULL,
3961 TRUE,
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003962 dwProcessFlags,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00003963 NULL,
3964 NULL,
3965 &siStartInfo,
3966 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003967 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003968 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00003969
Mark Hammondb37a3732000-08-14 04:47:33 +00003970 /* Return process handle */
3971 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003972 return TRUE;
3973 }
Tim Peters402d5982001-08-27 06:37:48 +00003974 win32_error("CreateProcess", s2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003975 return FALSE;
3976}
3977
3978/* The following code is based off of KB: Q190351 */
3979
3980static PyObject *
3981_PyPopen(char *cmdstring, int mode, int n)
3982{
3983 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
3984 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00003985 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Tim Peters5aa91602002-01-30 05:46:57 +00003986
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003987 SECURITY_ATTRIBUTES saAttr;
3988 BOOL fSuccess;
3989 int fd1, fd2, fd3;
3990 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00003991 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003992 PyObject *f;
3993
3994 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
3995 saAttr.bInheritHandle = TRUE;
3996 saAttr.lpSecurityDescriptor = NULL;
3997
3998 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
3999 return win32_error("CreatePipe", NULL);
4000
4001 /* Create new output read handle and the input write handle. Set
4002 * the inheritance properties to FALSE. Otherwise, the child inherits
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00004003 * these handles; resulting in non-closeable handles to the pipes
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004004 * being created. */
4005 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004006 GetCurrentProcess(), &hChildStdinWrDup, 0,
4007 FALSE,
4008 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004009 if (!fSuccess)
4010 return win32_error("DuplicateHandle", NULL);
4011
4012 /* Close the inheritable version of ChildStdin
4013 that we're using. */
4014 CloseHandle(hChildStdinWr);
4015
4016 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
4017 return win32_error("CreatePipe", NULL);
4018
4019 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004020 GetCurrentProcess(), &hChildStdoutRdDup, 0,
4021 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004022 if (!fSuccess)
4023 return win32_error("DuplicateHandle", NULL);
4024
4025 /* Close the inheritable version of ChildStdout
4026 that we're using. */
4027 CloseHandle(hChildStdoutRd);
4028
4029 if (n != POPEN_4) {
4030 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
4031 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004032 fSuccess = DuplicateHandle(GetCurrentProcess(),
4033 hChildStderrRd,
4034 GetCurrentProcess(),
4035 &hChildStderrRdDup, 0,
4036 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004037 if (!fSuccess)
4038 return win32_error("DuplicateHandle", NULL);
4039 /* Close the inheritable version of ChildStdErr that we're using. */
4040 CloseHandle(hChildStderrRd);
4041 }
Tim Peters5aa91602002-01-30 05:46:57 +00004042
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004043 switch (n) {
4044 case POPEN_1:
4045 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
4046 case _O_WRONLY | _O_TEXT:
4047 /* Case for writing to child Stdin in text mode. */
4048 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4049 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004050 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004051 PyFile_SetBufSize(f, 0);
4052 /* We don't care about these pipes anymore, so close them. */
4053 CloseHandle(hChildStdoutRdDup);
4054 CloseHandle(hChildStderrRdDup);
4055 break;
4056
4057 case _O_RDONLY | _O_TEXT:
4058 /* Case for reading from child Stdout in text mode. */
4059 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4060 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004061 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004062 PyFile_SetBufSize(f, 0);
4063 /* We don't care about these pipes anymore, so close them. */
4064 CloseHandle(hChildStdinWrDup);
4065 CloseHandle(hChildStderrRdDup);
4066 break;
4067
4068 case _O_RDONLY | _O_BINARY:
4069 /* Case for readinig from child Stdout in binary mode. */
4070 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4071 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004072 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004073 PyFile_SetBufSize(f, 0);
4074 /* We don't care about these pipes anymore, so close them. */
4075 CloseHandle(hChildStdinWrDup);
4076 CloseHandle(hChildStderrRdDup);
4077 break;
4078
4079 case _O_WRONLY | _O_BINARY:
4080 /* Case for writing to child Stdin in binary mode. */
4081 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4082 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00004083 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004084 PyFile_SetBufSize(f, 0);
4085 /* We don't care about these pipes anymore, so close them. */
4086 CloseHandle(hChildStdoutRdDup);
4087 CloseHandle(hChildStderrRdDup);
4088 break;
4089 }
Mark Hammondb37a3732000-08-14 04:47:33 +00004090 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004091 break;
Tim Peters5aa91602002-01-30 05:46:57 +00004092
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004093 case POPEN_2:
4094 case POPEN_4:
4095 {
4096 char *m1, *m2;
4097 PyObject *p1, *p2;
Tim Peters5aa91602002-01-30 05:46:57 +00004098
Tim Peters7dca21e2002-08-19 00:42:29 +00004099 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004100 m1 = "r";
4101 m2 = "w";
4102 } else {
4103 m1 = "rb";
4104 m2 = "wb";
4105 }
4106
4107 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4108 f1 = _fdopen(fd1, m2);
4109 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4110 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004111 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004112 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00004113 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004114 PyFile_SetBufSize(p2, 0);
4115
4116 if (n != 4)
4117 CloseHandle(hChildStderrRdDup);
4118
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004119 f = PyTuple_Pack(2,p1,p2);
Mark Hammond64aae662001-01-31 05:38:47 +00004120 Py_XDECREF(p1);
4121 Py_XDECREF(p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00004122 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004123 break;
4124 }
Tim Peters5aa91602002-01-30 05:46:57 +00004125
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004126 case POPEN_3:
4127 {
4128 char *m1, *m2;
4129 PyObject *p1, *p2, *p3;
Tim Peters5aa91602002-01-30 05:46:57 +00004130
Tim Peters7dca21e2002-08-19 00:42:29 +00004131 if (mode & _O_TEXT) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004132 m1 = "r";
4133 m2 = "w";
4134 } else {
4135 m1 = "rb";
4136 m2 = "wb";
4137 }
4138
4139 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
4140 f1 = _fdopen(fd1, m2);
4141 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
4142 f2 = _fdopen(fd2, m1);
4143 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
4144 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004145 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00004146 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
4147 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004148 PyFile_SetBufSize(p1, 0);
4149 PyFile_SetBufSize(p2, 0);
4150 PyFile_SetBufSize(p3, 0);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004151 f = PyTuple_Pack(3,p1,p2,p3);
Mark Hammond64aae662001-01-31 05:38:47 +00004152 Py_XDECREF(p1);
4153 Py_XDECREF(p2);
4154 Py_XDECREF(p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00004155 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004156 break;
4157 }
4158 }
4159
4160 if (n == POPEN_4) {
4161 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004162 hChildStdinRd,
4163 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004164 hChildStdoutWr,
4165 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004166 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004167 }
4168 else {
4169 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00004170 hChildStdinRd,
4171 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00004172 hChildStderrWr,
4173 &hProcess))
Mark Hammond08501372001-01-31 07:30:29 +00004174 return NULL;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004175 }
4176
Mark Hammondb37a3732000-08-14 04:47:33 +00004177 /*
4178 * Insert the files we've created into the process dictionary
4179 * all referencing the list with the process handle and the
4180 * initial number of files (see description below in _PyPclose).
4181 * Since if _PyPclose later tried to wait on a process when all
4182 * handles weren't closed, it could create a deadlock with the
4183 * child, we spend some energy here to try to ensure that we
4184 * either insert all file handles into the dictionary or none
4185 * at all. It's a little clumsy with the various popen modes
4186 * and variable number of files involved.
4187 */
4188 if (!_PyPopenProcs) {
4189 _PyPopenProcs = PyDict_New();
4190 }
4191
4192 if (_PyPopenProcs) {
4193 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
4194 int ins_rc[3];
4195
4196 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
4197 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
4198
4199 procObj = PyList_New(2);
4200 hProcessObj = PyLong_FromVoidPtr(hProcess);
4201 intObj = PyInt_FromLong(file_count);
4202
4203 if (procObj && hProcessObj && intObj) {
4204 PyList_SetItem(procObj,0,hProcessObj);
4205 PyList_SetItem(procObj,1,intObj);
4206
4207 fileObj[0] = PyLong_FromVoidPtr(f1);
4208 if (fileObj[0]) {
4209 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
4210 fileObj[0],
4211 procObj);
4212 }
4213 if (file_count >= 2) {
4214 fileObj[1] = PyLong_FromVoidPtr(f2);
4215 if (fileObj[1]) {
4216 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
4217 fileObj[1],
4218 procObj);
4219 }
4220 }
4221 if (file_count >= 3) {
4222 fileObj[2] = PyLong_FromVoidPtr(f3);
4223 if (fileObj[2]) {
4224 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
4225 fileObj[2],
4226 procObj);
4227 }
4228 }
4229
4230 if (ins_rc[0] < 0 || !fileObj[0] ||
4231 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
4232 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
4233 /* Something failed - remove any dictionary
4234 * entries that did make it.
4235 */
4236 if (!ins_rc[0] && fileObj[0]) {
4237 PyDict_DelItem(_PyPopenProcs,
4238 fileObj[0]);
4239 }
4240 if (!ins_rc[1] && fileObj[1]) {
4241 PyDict_DelItem(_PyPopenProcs,
4242 fileObj[1]);
4243 }
4244 if (!ins_rc[2] && fileObj[2]) {
4245 PyDict_DelItem(_PyPopenProcs,
4246 fileObj[2]);
4247 }
4248 }
4249 }
Tim Peters5aa91602002-01-30 05:46:57 +00004250
Mark Hammondb37a3732000-08-14 04:47:33 +00004251 /*
4252 * Clean up our localized references for the dictionary keys
4253 * and value since PyDict_SetItem will Py_INCREF any copies
4254 * that got placed in the dictionary.
4255 */
4256 Py_XDECREF(procObj);
4257 Py_XDECREF(fileObj[0]);
4258 Py_XDECREF(fileObj[1]);
4259 Py_XDECREF(fileObj[2]);
4260 }
4261
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004262 /* Child is launched. Close the parents copy of those pipe
4263 * handles that only the child should have open. You need to
4264 * make sure that no handles to the write end of the output pipe
4265 * are maintained in this process or else the pipe will not close
4266 * when the child process exits and the ReadFile will hang. */
4267
4268 if (!CloseHandle(hChildStdinRd))
4269 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004270
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004271 if (!CloseHandle(hChildStdoutWr))
4272 return win32_error("CloseHandle", NULL);
Tim Peters5aa91602002-01-30 05:46:57 +00004273
Fredrik Lundhffb9c772000-07-09 14:49:51 +00004274 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
4275 return win32_error("CloseHandle", NULL);
4276
4277 return f;
4278}
Fredrik Lundh56055a42000-07-23 19:47:12 +00004279
4280/*
4281 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4282 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00004283 *
4284 * This function uses the _PyPopenProcs dictionary in order to map the
4285 * input file pointer to information about the process that was
4286 * originally created by the popen* call that created the file pointer.
4287 * The dictionary uses the file pointer as a key (with one entry
4288 * inserted for each file returned by the original popen* call) and a
4289 * single list object as the value for all files from a single call.
4290 * The list object contains the Win32 process handle at [0], and a file
4291 * count at [1], which is initialized to the total number of file
4292 * handles using that list.
4293 *
4294 * This function closes whichever handle it is passed, and decrements
4295 * the file count in the dictionary for the process handle pointed to
4296 * by this file. On the last close (when the file count reaches zero),
4297 * this function will wait for the child process and then return its
4298 * exit code as the result of the close() operation. This permits the
4299 * files to be closed in any order - it is always the close() of the
4300 * final handle that will return the exit code.
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004301 *
4302 * NOTE: This function is currently called with the GIL released.
4303 * hence we use the GILState API to manage our state.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004304 */
Tim Peters736aa322000-09-01 06:51:24 +00004305
Fredrik Lundh56055a42000-07-23 19:47:12 +00004306static int _PyPclose(FILE *file)
4307{
Fredrik Lundh20318932000-07-26 17:29:12 +00004308 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004309 DWORD exit_code;
4310 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00004311 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
4312 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00004313#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004314 PyGILState_STATE state;
Tim Peters736aa322000-09-01 06:51:24 +00004315#endif
4316
Fredrik Lundh20318932000-07-26 17:29:12 +00004317 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00004318 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00004319 */
4320 result = fclose(file);
Tim Peters736aa322000-09-01 06:51:24 +00004321#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004322 state = PyGILState_Ensure();
Tim Peters736aa322000-09-01 06:51:24 +00004323#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004324 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00004325 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
4326 (procObj = PyDict_GetItem(_PyPopenProcs,
4327 fileObj)) != NULL &&
4328 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
4329 (intObj = PyList_GetItem(procObj,1)) != NULL) {
4330
4331 hProcess = PyLong_AsVoidPtr(hProcessObj);
4332 file_count = PyInt_AsLong(intObj);
4333
4334 if (file_count > 1) {
4335 /* Still other files referencing process */
4336 file_count--;
4337 PyList_SetItem(procObj,1,
4338 PyInt_FromLong(file_count));
4339 } else {
4340 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00004341 if (result != EOF &&
4342 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
4343 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00004344 /* Possible truncation here in 16-bit environments, but
4345 * real exit codes are just the lower byte in any event.
4346 */
4347 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004348 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00004349 /* Indicate failure - this will cause the file object
4350 * to raise an I/O error and translate the last Win32
4351 * error code from errno. We do have a problem with
4352 * last errors that overlap the normal errno table,
4353 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00004354 */
Fredrik Lundh20318932000-07-26 17:29:12 +00004355 if (result != EOF) {
4356 /* If the error wasn't from the fclose(), then
4357 * set errno for the file object error handling.
4358 */
4359 errno = GetLastError();
4360 }
4361 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00004362 }
4363
4364 /* Free up the native handle at this point */
4365 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00004366 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00004367
Mark Hammondb37a3732000-08-14 04:47:33 +00004368 /* Remove this file pointer from dictionary */
4369 PyDict_DelItem(_PyPopenProcs, fileObj);
4370
4371 if (PyDict_Size(_PyPopenProcs) == 0) {
4372 Py_DECREF(_PyPopenProcs);
4373 _PyPopenProcs = NULL;
4374 }
4375
4376 } /* if object retrieval ok */
4377
4378 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00004379 } /* if _PyPopenProcs */
4380
Tim Peters736aa322000-09-01 06:51:24 +00004381#ifdef WITH_THREAD
Mark Hammond8d98d2c2003-04-19 15:41:53 +00004382 PyGILState_Release(state);
Tim Peters736aa322000-09-01 06:51:24 +00004383#endif
Fredrik Lundh56055a42000-07-23 19:47:12 +00004384 return result;
4385}
Tim Peters9acdd3a2000-09-01 19:26:36 +00004386
4387#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00004388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004389posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00004390{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004391 char *name;
4392 char *mode = "r";
4393 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00004394 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00004395 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004396 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00004397 return NULL;
Martin v. Löwis9ad853b2003-10-31 10:01:53 +00004398 /* Strip mode of binary or text modifiers */
4399 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
4400 mode = "r";
4401 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
4402 mode = "w";
Barry Warsaw53699e91996-12-10 23:23:01 +00004403 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004404 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004405 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00004406 if (fp == NULL)
4407 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004408 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004409 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00004410 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00004411 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00004412}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004413
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004414#endif /* PYOS_??? */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004415#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00004416
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004417
Guido van Rossumb6775db1994-08-01 11:34:53 +00004418#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004419PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004420"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004421Set the current process's user id.");
4422
Barry Warsaw53699e91996-12-10 23:23:01 +00004423static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004424posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004425{
4426 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004427 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004428 return NULL;
4429 if (setuid(uid) < 0)
4430 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004431 Py_INCREF(Py_None);
4432 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004433}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004434#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004435
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004436
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004437#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004438PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004439"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004440Set the current process's effective user id.");
4441
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004442static PyObject *
4443posix_seteuid (PyObject *self, PyObject *args)
4444{
4445 int euid;
4446 if (!PyArg_ParseTuple(args, "i", &euid)) {
4447 return NULL;
4448 } else if (seteuid(euid) < 0) {
4449 return posix_error();
4450 } else {
4451 Py_INCREF(Py_None);
4452 return Py_None;
4453 }
4454}
4455#endif /* HAVE_SETEUID */
4456
4457#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004458PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004459"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004460Set the current process's effective group id.");
4461
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004462static PyObject *
4463posix_setegid (PyObject *self, PyObject *args)
4464{
4465 int egid;
4466 if (!PyArg_ParseTuple(args, "i", &egid)) {
4467 return NULL;
4468 } else if (setegid(egid) < 0) {
4469 return posix_error();
4470 } else {
4471 Py_INCREF(Py_None);
4472 return Py_None;
4473 }
4474}
4475#endif /* HAVE_SETEGID */
4476
4477#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004478PyDoc_STRVAR(posix_setreuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004479"seteuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004480Set the current process's real and effective user ids.");
4481
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004482static PyObject *
4483posix_setreuid (PyObject *self, PyObject *args)
4484{
4485 int ruid, euid;
4486 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
4487 return NULL;
4488 } else if (setreuid(ruid, euid) < 0) {
4489 return posix_error();
4490 } else {
4491 Py_INCREF(Py_None);
4492 return Py_None;
4493 }
4494}
4495#endif /* HAVE_SETREUID */
4496
4497#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004498PyDoc_STRVAR(posix_setregid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004499"setegid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004500Set the current process's real and effective group ids.");
4501
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004502static PyObject *
4503posix_setregid (PyObject *self, PyObject *args)
4504{
4505 int rgid, egid;
4506 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
4507 return NULL;
4508 } else if (setregid(rgid, egid) < 0) {
4509 return posix_error();
4510 } else {
4511 Py_INCREF(Py_None);
4512 return Py_None;
4513 }
4514}
4515#endif /* HAVE_SETREGID */
4516
Guido van Rossumb6775db1994-08-01 11:34:53 +00004517#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004518PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004519"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004520Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004521
Barry Warsaw53699e91996-12-10 23:23:01 +00004522static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004523posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004524{
4525 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004526 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004527 return NULL;
4528 if (setgid(gid) < 0)
4529 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004530 Py_INCREF(Py_None);
4531 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004532}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004533#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004534
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004535#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004536PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004537"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004538Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004539
4540static PyObject *
4541posix_setgroups(PyObject *self, PyObject *args)
4542{
4543 PyObject *groups;
4544 int i, len;
4545 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004546
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004547 if (!PyArg_ParseTuple(args, "O:setgid", &groups))
4548 return NULL;
4549 if (!PySequence_Check(groups)) {
4550 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4551 return NULL;
4552 }
4553 len = PySequence_Size(groups);
4554 if (len > MAX_GROUPS) {
4555 PyErr_SetString(PyExc_ValueError, "too many groups");
4556 return NULL;
4557 }
4558 for(i = 0; i < len; i++) {
4559 PyObject *elem;
4560 elem = PySequence_GetItem(groups, i);
4561 if (!elem)
4562 return NULL;
4563 if (!PyInt_Check(elem)) {
4564 PyErr_SetString(PyExc_TypeError,
4565 "groups must be integers");
4566 Py_DECREF(elem);
4567 return NULL;
4568 }
4569 /* XXX: check that value fits into gid_t. */
4570 grouplist[i] = PyInt_AsLong(elem);
4571 Py_DECREF(elem);
4572 }
4573
4574 if (setgroups(len, grouplist) < 0)
4575 return posix_error();
4576 Py_INCREF(Py_None);
4577 return Py_None;
4578}
4579#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004580
Guido van Rossumb6775db1994-08-01 11:34:53 +00004581#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004582PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004583"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004584Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004585
Barry Warsaw53699e91996-12-10 23:23:01 +00004586static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004587posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004588{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004589 int pid, options;
4590#ifdef UNION_WAIT
4591 union wait status;
4592#define status_i (status.w_status)
4593#else
4594 int status;
4595#define status_i status
4596#endif
4597 status_i = 0;
4598
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004599 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00004600 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004601 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00004602 pid = waitpid(pid, &status, options);
Barry Warsaw53699e91996-12-10 23:23:01 +00004603 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00004604 if (pid == -1)
4605 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00004606 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004607 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00004608}
4609
Tim Petersab034fa2002-02-01 11:27:43 +00004610#elif defined(HAVE_CWAIT)
4611
4612/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004613PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004614"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004615"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004616
4617static PyObject *
4618posix_waitpid(PyObject *self, PyObject *args)
4619{
4620 int pid, options;
4621 int status;
4622
4623 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
4624 return NULL;
4625 Py_BEGIN_ALLOW_THREADS
4626 pid = _cwait(&status, pid, options);
4627 Py_END_ALLOW_THREADS
4628 if (pid == -1)
4629 return posix_error();
4630 else
4631 /* shift the status left a byte so this is more like the
4632 POSIX waitpid */
4633 return Py_BuildValue("ii", pid, status << 8);
4634}
4635#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004636
Guido van Rossumad0ee831995-03-01 10:34:45 +00004637#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004638PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004639"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004640Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004641
Barry Warsaw53699e91996-12-10 23:23:01 +00004642static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004643posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004644{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004645 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004646#ifdef UNION_WAIT
4647 union wait status;
4648#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004649#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004650 int status;
4651#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00004652#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00004653
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004654 status_i = 0;
4655 Py_BEGIN_ALLOW_THREADS
4656 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00004657 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00004658 if (pid == -1)
4659 return posix_error();
4660 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004661 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004662#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00004663}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004664#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004667PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004668"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004669Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004670
Barry Warsaw53699e91996-12-10 23:23:01 +00004671static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004672posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004673{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004674#ifdef HAVE_LSTAT
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004675 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004676#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004677#ifdef MS_WINDOWS
Mark Hammond7edd0a92003-08-06 02:46:58 +00004678 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004679#else
4680 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
4681#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004682#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004683}
4684
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004685
Guido van Rossumb6775db1994-08-01 11:34:53 +00004686#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004687PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004688"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004689Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004690
Barry Warsaw53699e91996-12-10 23:23:01 +00004691static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004692posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004693{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004694 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00004695 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004696 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004697 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004698 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004699 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00004700 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00004701 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004702 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00004703 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00004704 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004705}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004706#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004707
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004708
Guido van Rossumb6775db1994-08-01 11:34:53 +00004709#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004710PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004711"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004712Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004713
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004715posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004716{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004717 return posix_2str(args, "etet:symlink", symlink, NULL, NULL);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004718}
4719#endif /* HAVE_SYMLINK */
4720
4721
4722#ifdef HAVE_TIMES
4723#ifndef HZ
4724#define HZ 60 /* Universal constant :-) */
4725#endif /* HZ */
Tim Peters5aa91602002-01-30 05:46:57 +00004726
Guido van Rossumd48f2521997-12-05 22:19:34 +00004727#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4728static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004729system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004730{
4731 ULONG value = 0;
4732
4733 Py_BEGIN_ALLOW_THREADS
4734 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4735 Py_END_ALLOW_THREADS
4736
4737 return value;
4738}
4739
4740static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004741posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004742{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004743 /* Currently Only Uptime is Provided -- Others Later */
4744 return Py_BuildValue("ddddd",
4745 (double)0 /* t.tms_utime / HZ */,
4746 (double)0 /* t.tms_stime / HZ */,
4747 (double)0 /* t.tms_cutime / HZ */,
4748 (double)0 /* t.tms_cstime / HZ */,
4749 (double)system_uptime() / 1000);
4750}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004751#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004752static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004753posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004754{
4755 struct tms t;
4756 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +00004757 errno = 0;
4758 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00004759 if (c == (clock_t) -1)
4760 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004761 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00004762 (double)t.tms_utime / HZ,
4763 (double)t.tms_stime / HZ,
4764 (double)t.tms_cutime / HZ,
4765 (double)t.tms_cstime / HZ,
4766 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004767}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004768#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004769#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004770
4771
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004772#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004773#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004774static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004775posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004776{
4777 FILETIME create, exit, kernel, user;
4778 HANDLE hProc;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004779 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004780 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4781 /* The fields of a FILETIME structure are the hi and lo part
4782 of a 64-bit value expressed in 100 nanosecond units.
4783 1e7 is one second in such units; 1e-7 the inverse.
4784 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4785 */
Barry Warsaw53699e91996-12-10 23:23:01 +00004786 return Py_BuildValue(
4787 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00004788 (double)(kernel.dwHighDateTime*429.4967296 +
4789 kernel.dwLowDateTime*1e-7),
4790 (double)(user.dwHighDateTime*429.4967296 +
4791 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00004792 (double)0,
4793 (double)0,
4794 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004795}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004796#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004797
4798#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004799PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004800"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004801Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004802#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004803
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004804
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004805#ifdef HAVE_GETSID
4806PyDoc_STRVAR(posix_getsid__doc__,
4807"getsid(pid) -> sid\n\n\
4808Call the system call getsid().");
4809
4810static PyObject *
4811posix_getsid(PyObject *self, PyObject *args)
4812{
4813 int pid, sid;
4814 if (!PyArg_ParseTuple(args, "i:getsid", &pid))
4815 return NULL;
4816 sid = getsid(pid);
4817 if (sid < 0)
4818 return posix_error();
4819 return PyInt_FromLong((long)sid);
4820}
4821#endif /* HAVE_GETSID */
4822
4823
Guido van Rossumb6775db1994-08-01 11:34:53 +00004824#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004825PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004826"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004827Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004828
Barry Warsaw53699e91996-12-10 23:23:01 +00004829static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004830posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004831{
Guido van Rossum687dd131993-05-17 08:34:16 +00004832 if (setsid() < 0)
4833 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004834 Py_INCREF(Py_None);
4835 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004836}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004837#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004838
Guido van Rossumb6775db1994-08-01 11:34:53 +00004839#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004840PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004841"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004842Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004843
Barry Warsaw53699e91996-12-10 23:23:01 +00004844static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004845posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004846{
4847 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004848 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00004849 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004850 if (setpgid(pid, pgrp) < 0)
4851 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004852 Py_INCREF(Py_None);
4853 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004854}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004855#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004856
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004857
Guido van Rossumb6775db1994-08-01 11:34:53 +00004858#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004859PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004860"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004861Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004862
Barry Warsaw53699e91996-12-10 23:23:01 +00004863static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004864posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004865{
4866 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004867 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004868 return NULL;
4869 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004870 if (pgid < 0)
4871 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004872 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004873}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004874#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004875
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004876
Guido van Rossumb6775db1994-08-01 11:34:53 +00004877#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004878PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004879"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004880Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004881
Barry Warsaw53699e91996-12-10 23:23:01 +00004882static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004883posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004884{
4885 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004886 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00004887 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004888 if (tcsetpgrp(fd, pgid) < 0)
4889 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00004890 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00004891 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004892}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004893#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004894
Guido van Rossum687dd131993-05-17 08:34:16 +00004895/* Functions acting on file descriptors */
4896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004897PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004898"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004900
Barry Warsaw53699e91996-12-10 23:23:01 +00004901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004902posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004903{
Mark Hammondef8b6542001-05-13 08:04:26 +00004904 char *file = NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004905 int flag;
4906 int mode = 0777;
4907 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004908
4909#ifdef MS_WINDOWS
4910 if (unicode_file_names()) {
4911 PyUnicodeObject *po;
4912 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4913 Py_BEGIN_ALLOW_THREADS
4914 /* PyUnicode_AS_UNICODE OK without thread
4915 lock as it is a simple dereference. */
4916 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4917 Py_END_ALLOW_THREADS
4918 if (fd < 0)
4919 return posix_error();
4920 return PyInt_FromLong((long)fd);
4921 }
4922 /* Drop the argument parsing error as narrow strings
4923 are also valid. */
4924 PyErr_Clear();
4925 }
4926#endif
4927
Tim Peters5aa91602002-01-30 05:46:57 +00004928 if (!PyArg_ParseTuple(args, "eti|i",
Mark Hammondef8b6542001-05-13 08:04:26 +00004929 Py_FileSystemDefaultEncoding, &file,
4930 &flag, &mode))
Barry Warsaw43d68b81996-12-19 22:10:44 +00004931 return NULL;
4932
Barry Warsaw53699e91996-12-10 23:23:01 +00004933 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004934 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00004935 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004936 if (fd < 0)
Mark Hammondef8b6542001-05-13 08:04:26 +00004937 return posix_error_with_allocated_filename(file);
4938 PyMem_Free(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00004939 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004940}
4941
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004943PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004944"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004945Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004946
Barry Warsaw53699e91996-12-10 23:23:01 +00004947static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004948posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004949{
4950 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004951 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004952 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004953 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004954 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004955 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004956 if (res < 0)
4957 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004958 Py_INCREF(Py_None);
4959 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004960}
4961
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004963PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004964"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004965Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004966
Barry Warsaw53699e91996-12-10 23:23:01 +00004967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004968posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004969{
4970 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004971 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00004972 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004973 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004974 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00004975 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004976 if (fd < 0)
4977 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004978 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004979}
4980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004981
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004982PyDoc_STRVAR(posix_dup2__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004983"dup2(fd, fd2)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004984Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004985
Barry Warsaw53699e91996-12-10 23:23:01 +00004986static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004987posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004988{
4989 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004990 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00004991 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00004992 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004993 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00004994 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00004995 if (res < 0)
4996 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00004997 Py_INCREF(Py_None);
4998 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004999}
5000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005002PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005003"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005004Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005005
Barry Warsaw53699e91996-12-10 23:23:01 +00005006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005007posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005008{
5009 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005010#if defined(MS_WIN64) || defined(MS_WINDOWS)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005011 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005012#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00005013 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005014#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005015 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005016 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00005017 return NULL;
5018#ifdef SEEK_SET
5019 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5020 switch (how) {
5021 case 0: how = SEEK_SET; break;
5022 case 1: how = SEEK_CUR; break;
5023 case 2: how = SEEK_END; break;
5024 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005025#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005026
5027#if !defined(HAVE_LARGEFILE_SUPPORT)
5028 pos = PyInt_AsLong(posobj);
5029#else
5030 pos = PyLong_Check(posobj) ?
5031 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
5032#endif
5033 if (PyErr_Occurred())
5034 return NULL;
5035
Barry Warsaw53699e91996-12-10 23:23:01 +00005036 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005037#if defined(MS_WIN64) || defined(MS_WINDOWS)
Fred Drake699f3522000-06-29 21:12:41 +00005038 res = _lseeki64(fd, pos, how);
5039#else
Guido van Rossum687dd131993-05-17 08:34:16 +00005040 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005041#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005042 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005043 if (res < 0)
5044 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005045
5046#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00005047 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005048#else
5049 return PyLong_FromLongLong(res);
5050#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005051}
5052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005054PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005055"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005056Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005057
Barry Warsaw53699e91996-12-10 23:23:01 +00005058static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005059posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005060{
Guido van Rossum8bac5461996-06-11 18:38:48 +00005061 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00005062 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005063 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005064 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005065 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005066 if (buffer == NULL)
5067 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005068 Py_BEGIN_ALLOW_THREADS
5069 n = read(fd, PyString_AsString(buffer), size);
5070 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00005071 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005072 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00005073 return posix_error();
5074 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00005075 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00005076 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00005077 return buffer;
5078}
5079
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005081PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005082"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005083Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005084
Barry Warsaw53699e91996-12-10 23:23:01 +00005085static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005086posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005087{
5088 int fd, size;
5089 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005090 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00005091 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005092 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005093 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00005094 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005095 if (size < 0)
5096 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005097 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005098}
5099
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005101PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005102"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005103Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005104
Barry Warsaw53699e91996-12-10 23:23:01 +00005105static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005106posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005107{
5108 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00005109 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00005110 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005111 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00005112 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005113#ifdef __VMS
5114 /* on OpenVMS we must ensure that all bytes are written to the file */
5115 fsync(fd);
5116#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005117 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00005118 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00005119 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005120 if (res != 0)
5121 return posix_error();
Tim Peters5aa91602002-01-30 05:46:57 +00005122
Fred Drake699f3522000-06-29 21:12:41 +00005123 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005124}
5125
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005127PyDoc_STRVAR(posix_fdopen__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005128"fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005129Return an open file object connected to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005130
Barry Warsaw53699e91996-12-10 23:23:01 +00005131static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005132posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005133{
Guido van Rossum687dd131993-05-17 08:34:16 +00005134 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005135 char *mode = "r";
5136 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00005137 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00005138 PyObject *f;
5139 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00005140 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00005141
Thomas Heller1f043e22002-11-07 16:00:59 +00005142 if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
5143 PyErr_Format(PyExc_ValueError,
5144 "invalid file mode '%s'", mode);
5145 return NULL;
5146 }
5147
Barry Warsaw53699e91996-12-10 23:23:01 +00005148 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005149 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00005150 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005151 if (fp == NULL)
5152 return posix_error();
Guido van Rossumbd6be7a2002-09-15 18:45:46 +00005153 f = PyFile_FromFile(fp, "<fdopen>", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005154 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00005155 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00005156 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00005157}
5158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005159PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005160"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005161Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005162connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005163
5164static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005165posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005166{
5167 int fd;
5168 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5169 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005170 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005171}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005172
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005173#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005174PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005175"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005176Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005177
Barry Warsaw53699e91996-12-10 23:23:01 +00005178static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005179posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005180{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005181#if defined(PYOS_OS2)
5182 HFILE read, write;
5183 APIRET rc;
5184
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005185 Py_BEGIN_ALLOW_THREADS
5186 rc = DosCreatePipe( &read, &write, 4096);
5187 Py_END_ALLOW_THREADS
5188 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005189 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005190
5191 return Py_BuildValue("(ii)", read, write);
5192#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005193#if !defined(MS_WINDOWS)
Guido van Rossum687dd131993-05-17 08:34:16 +00005194 int fds[2];
5195 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00005196 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005197 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00005198 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00005199 if (res != 0)
5200 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005201 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005202#else /* MS_WINDOWS */
Guido van Rossum794d8131994-08-23 13:48:48 +00005203 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005204 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00005205 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00005206 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005207 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00005208 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00005209 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005210 return win32_error("CreatePipe", NULL);
Tim Peters79248aa2001-08-29 21:37:10 +00005211 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5212 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00005213 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005214#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005215#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005216}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005217#endif /* HAVE_PIPE */
5218
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005219
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005220#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005221PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005222"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005223Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005224
Barry Warsaw53699e91996-12-10 23:23:01 +00005225static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005226posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005227{
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005228 char *filename;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005229 int mode = 0666;
5230 int res;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005231 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005232 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00005233 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005234 res = mkfifo(filename, mode);
5235 Py_END_ALLOW_THREADS
5236 if (res < 0)
5237 return posix_error();
5238 Py_INCREF(Py_None);
5239 return Py_None;
5240}
5241#endif
5242
5243
Neal Norwitz11690112002-07-30 01:08:28 +00005244#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005245PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005246"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005247Create a filesystem node (file, device special file or named pipe)\n\
5248named filename. mode specifies both the permissions to use and the\n\
5249type of node to be created, being combined (bitwise OR) with one of\n\
5250S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005251device defines the newly created device special file (probably using\n\
5252os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005253
5254
5255static PyObject *
5256posix_mknod(PyObject *self, PyObject *args)
5257{
5258 char *filename;
5259 int mode = 0600;
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005260 int device = 0;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005261 int res;
Martin v. Löwisd631ebe2002-11-02 17:42:33 +00005262 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005263 return NULL;
5264 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005265 res = mknod(filename, mode, device);
Barry Warsaw53699e91996-12-10 23:23:01 +00005266 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005267 if (res < 0)
5268 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00005269 Py_INCREF(Py_None);
5270 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005271}
5272#endif
5273
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005274#ifdef HAVE_DEVICE_MACROS
5275PyDoc_STRVAR(posix_major__doc__,
5276"major(device) -> major number\n\
5277Extracts a device major number from a raw device number.");
5278
5279static PyObject *
5280posix_major(PyObject *self, PyObject *args)
5281{
5282 int device;
5283 if (!PyArg_ParseTuple(args, "i:major", &device))
5284 return NULL;
5285 return PyInt_FromLong((long)major(device));
5286}
5287
5288PyDoc_STRVAR(posix_minor__doc__,
5289"minor(device) -> minor number\n\
5290Extracts a device minor number from a raw device number.");
5291
5292static PyObject *
5293posix_minor(PyObject *self, PyObject *args)
5294{
5295 int device;
5296 if (!PyArg_ParseTuple(args, "i:minor", &device))
5297 return NULL;
5298 return PyInt_FromLong((long)minor(device));
5299}
5300
5301PyDoc_STRVAR(posix_makedev__doc__,
5302"makedev(major, minor) -> device number\n\
5303Composes a raw device number from the major and minor device numbers.");
5304
5305static PyObject *
5306posix_makedev(PyObject *self, PyObject *args)
5307{
5308 int major, minor;
5309 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5310 return NULL;
5311 return PyInt_FromLong((long)makedev(major, minor));
5312}
5313#endif /* device macros */
5314
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005315
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005316#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005317PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005318"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005319Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005320
Barry Warsaw53699e91996-12-10 23:23:01 +00005321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005322posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005323{
5324 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005325 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005326 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005327 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005328
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005329 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005330 return NULL;
5331
5332#if !defined(HAVE_LARGEFILE_SUPPORT)
5333 length = PyInt_AsLong(lenobj);
5334#else
5335 length = PyLong_Check(lenobj) ?
5336 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
5337#endif
5338 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005339 return NULL;
5340
Barry Warsaw53699e91996-12-10 23:23:01 +00005341 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005342 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00005343 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005344 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00005345 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005346 return NULL;
5347 }
Barry Warsaw53699e91996-12-10 23:23:01 +00005348 Py_INCREF(Py_None);
5349 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005350}
5351#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005352
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005353#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005354PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005355"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005356Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005357
Fred Drake762e2061999-08-26 17:23:54 +00005358/* Save putenv() parameters as values here, so we can collect them when they
5359 * get re-set with another call for the same key. */
5360static PyObject *posix_putenv_garbage;
5361
Tim Peters5aa91602002-01-30 05:46:57 +00005362static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005363posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005364{
5365 char *s1, *s2;
5366 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00005367 PyObject *newstr;
Tim Petersc8996f52001-12-03 20:41:00 +00005368 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005369
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005370 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005371 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00005372
5373#if defined(PYOS_OS2)
5374 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5375 APIRET rc;
5376
Guido van Rossumd48f2521997-12-05 22:19:34 +00005377 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5378 if (rc != NO_ERROR)
5379 return os2_error(rc);
5380
5381 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5382 APIRET rc;
5383
Guido van Rossumd48f2521997-12-05 22:19:34 +00005384 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5385 if (rc != NO_ERROR)
5386 return os2_error(rc);
5387 } else {
5388#endif
5389
Fred Drake762e2061999-08-26 17:23:54 +00005390 /* XXX This can leak memory -- not easy to fix :-( */
Tim Petersc8996f52001-12-03 20:41:00 +00005391 len = strlen(s1) + strlen(s2) + 2;
5392 /* len includes space for a trailing \0; the size arg to
5393 PyString_FromStringAndSize does not count that */
5394 newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
Fred Drake762e2061999-08-26 17:23:54 +00005395 if (newstr == NULL)
5396 return PyErr_NoMemory();
5397 new = PyString_AS_STRING(newstr);
Tim Petersc8996f52001-12-03 20:41:00 +00005398 PyOS_snprintf(new, len, "%s=%s", s1, s2);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005399 if (putenv(new)) {
Neal Norwitz4adc9ab2003-02-10 03:10:43 +00005400 Py_DECREF(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005401 posix_error();
5402 return NULL;
5403 }
Fred Drake762e2061999-08-26 17:23:54 +00005404 /* Install the first arg and newstr in posix_putenv_garbage;
5405 * this will cause previous value to be collected. This has to
5406 * happen after the real putenv() call because the old value
5407 * was still accessible until then. */
5408 if (PyDict_SetItem(posix_putenv_garbage,
5409 PyTuple_GET_ITEM(args, 0), newstr)) {
5410 /* really not much we can do; just leak */
5411 PyErr_Clear();
5412 }
5413 else {
5414 Py_DECREF(newstr);
5415 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005416
5417#if defined(PYOS_OS2)
5418 }
5419#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00005420 Py_INCREF(Py_None);
5421 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005422}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005423#endif /* putenv */
5424
Guido van Rossumc524d952001-10-19 01:31:59 +00005425#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005427"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005428Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005429
5430static PyObject *
5431posix_unsetenv(PyObject *self, PyObject *args)
5432{
5433 char *s1;
5434
5435 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5436 return NULL;
5437
5438 unsetenv(s1);
5439
5440 /* Remove the key from posix_putenv_garbage;
5441 * this will cause it to be collected. This has to
Tim Peters5aa91602002-01-30 05:46:57 +00005442 * happen after the real unsetenv() call because the
Guido van Rossumc524d952001-10-19 01:31:59 +00005443 * old value was still accessible until then.
5444 */
5445 if (PyDict_DelItem(posix_putenv_garbage,
5446 PyTuple_GET_ITEM(args, 0))) {
5447 /* really not much we can do; just leak */
5448 PyErr_Clear();
5449 }
5450
5451 Py_INCREF(Py_None);
5452 return Py_None;
5453}
5454#endif /* unsetenv */
5455
Guido van Rossumb6a47161997-09-15 22:54:34 +00005456#ifdef HAVE_STRERROR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005457PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005458"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005459Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005460
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005461static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005462posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005463{
5464 int code;
5465 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005466 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00005467 return NULL;
5468 message = strerror(code);
5469 if (message == NULL) {
5470 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00005471 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005472 return NULL;
5473 }
5474 return PyString_FromString(message);
5475}
5476#endif /* strerror */
5477
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005478
Guido van Rossumc9641791998-08-04 15:26:23 +00005479#ifdef HAVE_SYS_WAIT_H
5480
Fred Drake106c1a02002-04-23 15:58:02 +00005481#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005482PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005483"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005484Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005485
5486static PyObject *
5487posix_WCOREDUMP(PyObject *self, PyObject *args)
5488{
5489#ifdef UNION_WAIT
5490 union wait status;
5491#define status_i (status.w_status)
5492#else
5493 int status;
5494#define status_i status
5495#endif
5496 status_i = 0;
5497
5498 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i))
5499 {
5500 return NULL;
5501 }
5502
5503 return PyBool_FromLong(WCOREDUMP(status));
5504#undef status_i
5505}
5506#endif /* WCOREDUMP */
5507
5508#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005509PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005510"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005511Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005512job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005513
5514static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005515posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005516{
5517#ifdef UNION_WAIT
5518 union wait status;
5519#define status_i (status.w_status)
5520#else
5521 int status;
5522#define status_i status
5523#endif
5524 status_i = 0;
5525
5526 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i))
5527 {
5528 return NULL;
5529 }
5530
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005531 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005532#undef status_i
5533}
5534#endif /* WIFCONTINUED */
5535
Guido van Rossumc9641791998-08-04 15:26:23 +00005536#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005537PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005538"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005539Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005540
5541static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005542posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005543{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005544#ifdef UNION_WAIT
5545 union wait status;
5546#define status_i (status.w_status)
5547#else
5548 int status;
5549#define status_i status
5550#endif
5551 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005552
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005553 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005554 {
5555 return NULL;
5556 }
Tim Peters5aa91602002-01-30 05:46:57 +00005557
Fred Drake106c1a02002-04-23 15:58:02 +00005558 return PyBool_FromLong(WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005559#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005560}
5561#endif /* WIFSTOPPED */
5562
5563#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005565"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005567
5568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005569posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005570{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005571#ifdef UNION_WAIT
5572 union wait status;
5573#define status_i (status.w_status)
5574#else
5575 int status;
5576#define status_i status
5577#endif
5578 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005579
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005580 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005581 {
5582 return NULL;
5583 }
Tim Peters5aa91602002-01-30 05:46:57 +00005584
Fred Drake106c1a02002-04-23 15:58:02 +00005585 return PyBool_FromLong(WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005586#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005587}
5588#endif /* WIFSIGNALED */
5589
5590#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005591PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005592"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005593Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005594system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005595
5596static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005597posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005598{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005599#ifdef UNION_WAIT
5600 union wait status;
5601#define status_i (status.w_status)
5602#else
5603 int status;
5604#define status_i status
5605#endif
5606 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005607
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005608 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005609 {
5610 return NULL;
5611 }
Tim Peters5aa91602002-01-30 05:46:57 +00005612
Fred Drake106c1a02002-04-23 15:58:02 +00005613 return PyBool_FromLong(WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005614#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005615}
5616#endif /* WIFEXITED */
5617
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005618#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005619PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005620"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005621Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005622
5623static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005624posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005625{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005626#ifdef UNION_WAIT
5627 union wait status;
5628#define status_i (status.w_status)
5629#else
5630 int status;
5631#define status_i status
5632#endif
5633 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005634
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005635 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005636 {
5637 return NULL;
5638 }
Tim Peters5aa91602002-01-30 05:46:57 +00005639
Guido van Rossumc9641791998-08-04 15:26:23 +00005640 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005641#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005642}
5643#endif /* WEXITSTATUS */
5644
5645#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005646PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005647"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005648Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005649value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005650
5651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005652posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005653{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005654#ifdef UNION_WAIT
5655 union wait status;
5656#define status_i (status.w_status)
5657#else
5658 int status;
5659#define status_i status
5660#endif
5661 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005662
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005663 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005664 {
5665 return NULL;
5666 }
Tim Peters5aa91602002-01-30 05:46:57 +00005667
Guido van Rossumc9641791998-08-04 15:26:23 +00005668 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005669#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005670}
5671#endif /* WTERMSIG */
5672
5673#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005674PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005675"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005676Return the signal that stopped the process that provided\n\
5677the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005678
5679static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005680posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005681{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005682#ifdef UNION_WAIT
5683 union wait status;
5684#define status_i (status.w_status)
5685#else
5686 int status;
5687#define status_i status
5688#endif
5689 status_i = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005690
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005691 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00005692 {
5693 return NULL;
5694 }
Tim Peters5aa91602002-01-30 05:46:57 +00005695
Guido van Rossumc9641791998-08-04 15:26:23 +00005696 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005697#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00005698}
5699#endif /* WSTOPSIG */
5700
5701#endif /* HAVE_SYS_WAIT_H */
5702
5703
Guido van Rossum94f6f721999-01-06 18:42:14 +00005704#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005705#ifdef _SCO_DS
5706/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5707 needed definitions in sys/statvfs.h */
5708#define _SVID3
5709#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005710#include <sys/statvfs.h>
5711
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005712static PyObject*
5713_pystatvfs_fromstructstatvfs(struct statvfs st) {
5714 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5715 if (v == NULL)
5716 return NULL;
5717
5718#if !defined(HAVE_LARGEFILE_SUPPORT)
5719 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5720 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
5721 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
5722 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
5723 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
5724 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
5725 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
5726 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
5727 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5728 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5729#else
5730 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
5731 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
Tim Peters5aa91602002-01-30 05:46:57 +00005732 PyStructSequence_SET_ITEM(v, 2,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005733 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
Tim Peters5aa91602002-01-30 05:46:57 +00005734 PyStructSequence_SET_ITEM(v, 3,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005735 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005736 PyStructSequence_SET_ITEM(v, 4,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005737 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
Tim Peters5aa91602002-01-30 05:46:57 +00005738 PyStructSequence_SET_ITEM(v, 5,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005739 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
Tim Peters5aa91602002-01-30 05:46:57 +00005740 PyStructSequence_SET_ITEM(v, 6,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005741 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
Tim Peters5aa91602002-01-30 05:46:57 +00005742 PyStructSequence_SET_ITEM(v, 7,
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00005743 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005744 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
5745 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
5746#endif
5747
5748 return v;
5749}
5750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005751PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005752"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005753Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005754
5755static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005756posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005757{
5758 int fd, res;
5759 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005760
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005761 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005762 return NULL;
5763 Py_BEGIN_ALLOW_THREADS
5764 res = fstatvfs(fd, &st);
5765 Py_END_ALLOW_THREADS
5766 if (res != 0)
5767 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005768
5769 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005770}
5771#endif /* HAVE_FSTATVFS */
5772
5773
5774#if defined(HAVE_STATVFS)
5775#include <sys/statvfs.h>
5776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005777PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005778"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005779Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005780
5781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005782posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005783{
5784 char *path;
5785 int res;
5786 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005787 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00005788 return NULL;
5789 Py_BEGIN_ALLOW_THREADS
5790 res = statvfs(path, &st);
5791 Py_END_ALLOW_THREADS
5792 if (res != 0)
5793 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005794
5795 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005796}
5797#endif /* HAVE_STATVFS */
5798
5799
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005800#ifdef HAVE_TEMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801PyDoc_STRVAR(posix_tempnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005802"tempnam([dir[, prefix]]) -> string\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005803Return a unique name for a temporary file.\n\
Neal Norwitz50d5d4f2002-07-30 01:17:43 +00005804The directory and a prefix may be specified as strings; they may be omitted\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005805or None if not needed.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005806
5807static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005808posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005809{
5810 PyObject *result = NULL;
5811 char *dir = NULL;
5812 char *pfx = NULL;
5813 char *name;
5814
5815 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
5816 return NULL;
Skip Montanaro95618b52001-08-18 18:52:10 +00005817
5818 if (PyErr_Warn(PyExc_RuntimeWarning,
5819 "tempnam is a potential security risk to your program") < 0)
5820 return NULL;
5821
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005822#ifdef MS_WINDOWS
Fred Drake78b71c22001-07-17 20:37:36 +00005823 name = _tempnam(dir, pfx);
5824#else
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005825 name = tempnam(dir, pfx);
Fred Drake78b71c22001-07-17 20:37:36 +00005826#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005827 if (name == NULL)
5828 return PyErr_NoMemory();
5829 result = PyString_FromString(name);
5830 free(name);
5831 return result;
5832}
Guido van Rossumd371ff11999-01-25 16:12:23 +00005833#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005834
5835
5836#ifdef HAVE_TMPFILE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837PyDoc_STRVAR(posix_tmpfile__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005838"tmpfile() -> file object\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005839Create a temporary file with no directory entries.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005840
5841static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005842posix_tmpfile(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005843{
5844 FILE *fp;
5845
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005846 fp = tmpfile();
5847 if (fp == NULL)
5848 return posix_error();
Guido van Rossumdb9198a2002-06-10 19:23:22 +00005849 return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005850}
5851#endif
5852
5853
5854#ifdef HAVE_TMPNAM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005855PyDoc_STRVAR(posix_tmpnam__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005856"tmpnam() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005857Return a unique name for a temporary file.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005858
5859static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005860posix_tmpnam(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005861{
5862 char buffer[L_tmpnam];
5863 char *name;
5864
Skip Montanaro95618b52001-08-18 18:52:10 +00005865 if (PyErr_Warn(PyExc_RuntimeWarning,
5866 "tmpnam is a potential security risk to your program") < 0)
5867 return NULL;
5868
Greg Wardb48bc172000-03-01 21:51:56 +00005869#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005870 name = tmpnam_r(buffer);
5871#else
5872 name = tmpnam(buffer);
5873#endif
5874 if (name == NULL) {
5875 PyErr_SetObject(PyExc_OSError,
5876 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00005877#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005878 "unexpected NULL from tmpnam_r"
5879#else
5880 "unexpected NULL from tmpnam"
5881#endif
5882 ));
5883 return NULL;
5884 }
5885 return PyString_FromString(buffer);
5886}
5887#endif
5888
5889
Fred Drakec9680921999-12-13 16:37:25 +00005890/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5891 * It maps strings representing configuration variable names to
5892 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005893 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005894 * rarely-used constants. There are three separate tables that use
5895 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005896 *
5897 * This code is always included, even if none of the interfaces that
5898 * need it are included. The #if hackery needed to avoid it would be
5899 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005900 */
5901struct constdef {
5902 char *name;
5903 long value;
5904};
5905
Fred Drake12c6e2d1999-12-14 21:25:03 +00005906static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005907conv_confname(PyObject *arg, int *valuep, struct constdef *table,
5908 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005909{
5910 if (PyInt_Check(arg)) {
5911 *valuep = PyInt_AS_LONG(arg);
5912 return 1;
5913 }
5914 if (PyString_Check(arg)) {
5915 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00005916 size_t lo = 0;
5917 size_t mid;
5918 size_t hi = tablesize;
5919 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005920 char *confname = PyString_AS_STRING(arg);
5921 while (lo < hi) {
5922 mid = (lo + hi) / 2;
5923 cmp = strcmp(confname, table[mid].name);
5924 if (cmp < 0)
5925 hi = mid;
5926 else if (cmp > 0)
5927 lo = mid + 1;
5928 else {
5929 *valuep = table[mid].value;
5930 return 1;
5931 }
5932 }
5933 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5934 }
5935 else
5936 PyErr_SetString(PyExc_TypeError,
5937 "configuration names must be strings or integers");
5938 return 0;
5939}
5940
5941
5942#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5943static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005944#ifdef _PC_ABI_AIO_XFER_MAX
5945 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
5946#endif
5947#ifdef _PC_ABI_ASYNC_IO
5948 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
5949#endif
Fred Drakec9680921999-12-13 16:37:25 +00005950#ifdef _PC_ASYNC_IO
5951 {"PC_ASYNC_IO", _PC_ASYNC_IO},
5952#endif
5953#ifdef _PC_CHOWN_RESTRICTED
5954 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
5955#endif
5956#ifdef _PC_FILESIZEBITS
5957 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
5958#endif
5959#ifdef _PC_LAST
5960 {"PC_LAST", _PC_LAST},
5961#endif
5962#ifdef _PC_LINK_MAX
5963 {"PC_LINK_MAX", _PC_LINK_MAX},
5964#endif
5965#ifdef _PC_MAX_CANON
5966 {"PC_MAX_CANON", _PC_MAX_CANON},
5967#endif
5968#ifdef _PC_MAX_INPUT
5969 {"PC_MAX_INPUT", _PC_MAX_INPUT},
5970#endif
5971#ifdef _PC_NAME_MAX
5972 {"PC_NAME_MAX", _PC_NAME_MAX},
5973#endif
5974#ifdef _PC_NO_TRUNC
5975 {"PC_NO_TRUNC", _PC_NO_TRUNC},
5976#endif
5977#ifdef _PC_PATH_MAX
5978 {"PC_PATH_MAX", _PC_PATH_MAX},
5979#endif
5980#ifdef _PC_PIPE_BUF
5981 {"PC_PIPE_BUF", _PC_PIPE_BUF},
5982#endif
5983#ifdef _PC_PRIO_IO
5984 {"PC_PRIO_IO", _PC_PRIO_IO},
5985#endif
5986#ifdef _PC_SOCK_MAXBUF
5987 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
5988#endif
5989#ifdef _PC_SYNC_IO
5990 {"PC_SYNC_IO", _PC_SYNC_IO},
5991#endif
5992#ifdef _PC_VDISABLE
5993 {"PC_VDISABLE", _PC_VDISABLE},
5994#endif
5995};
5996
Fred Drakec9680921999-12-13 16:37:25 +00005997static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005998conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005999{
6000 return conv_confname(arg, valuep, posix_constants_pathconf,
6001 sizeof(posix_constants_pathconf)
6002 / sizeof(struct constdef));
6003}
6004#endif
6005
6006#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006007PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006008"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006009Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006010If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006011
6012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006013posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006014{
6015 PyObject *result = NULL;
6016 int name, fd;
6017
Fred Drake12c6e2d1999-12-14 21:25:03 +00006018 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6019 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00006020 long limit;
6021
6022 errno = 0;
6023 limit = fpathconf(fd, name);
6024 if (limit == -1 && errno != 0)
6025 posix_error();
6026 else
6027 result = PyInt_FromLong(limit);
6028 }
6029 return result;
6030}
6031#endif
6032
6033
6034#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006035PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006036"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006037Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006038If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006039
6040static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006041posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006042{
6043 PyObject *result = NULL;
6044 int name;
6045 char *path;
6046
6047 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6048 conv_path_confname, &name)) {
6049 long limit;
6050
6051 errno = 0;
6052 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006053 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00006054 if (errno == EINVAL)
6055 /* could be a path or name problem */
6056 posix_error();
6057 else
6058 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00006059 }
Fred Drakec9680921999-12-13 16:37:25 +00006060 else
6061 result = PyInt_FromLong(limit);
6062 }
6063 return result;
6064}
6065#endif
6066
6067#ifdef HAVE_CONFSTR
6068static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006069#ifdef _CS_ARCHITECTURE
6070 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
6071#endif
6072#ifdef _CS_HOSTNAME
6073 {"CS_HOSTNAME", _CS_HOSTNAME},
6074#endif
6075#ifdef _CS_HW_PROVIDER
6076 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
6077#endif
6078#ifdef _CS_HW_SERIAL
6079 {"CS_HW_SERIAL", _CS_HW_SERIAL},
6080#endif
6081#ifdef _CS_INITTAB_NAME
6082 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
6083#endif
Fred Drakec9680921999-12-13 16:37:25 +00006084#ifdef _CS_LFS64_CFLAGS
6085 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
6086#endif
6087#ifdef _CS_LFS64_LDFLAGS
6088 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
6089#endif
6090#ifdef _CS_LFS64_LIBS
6091 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
6092#endif
6093#ifdef _CS_LFS64_LINTFLAGS
6094 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
6095#endif
6096#ifdef _CS_LFS_CFLAGS
6097 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
6098#endif
6099#ifdef _CS_LFS_LDFLAGS
6100 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
6101#endif
6102#ifdef _CS_LFS_LIBS
6103 {"CS_LFS_LIBS", _CS_LFS_LIBS},
6104#endif
6105#ifdef _CS_LFS_LINTFLAGS
6106 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
6107#endif
Fred Draked86ed291999-12-15 15:34:33 +00006108#ifdef _CS_MACHINE
6109 {"CS_MACHINE", _CS_MACHINE},
6110#endif
Fred Drakec9680921999-12-13 16:37:25 +00006111#ifdef _CS_PATH
6112 {"CS_PATH", _CS_PATH},
6113#endif
Fred Draked86ed291999-12-15 15:34:33 +00006114#ifdef _CS_RELEASE
6115 {"CS_RELEASE", _CS_RELEASE},
6116#endif
6117#ifdef _CS_SRPC_DOMAIN
6118 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
6119#endif
6120#ifdef _CS_SYSNAME
6121 {"CS_SYSNAME", _CS_SYSNAME},
6122#endif
6123#ifdef _CS_VERSION
6124 {"CS_VERSION", _CS_VERSION},
6125#endif
Fred Drakec9680921999-12-13 16:37:25 +00006126#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
6127 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
6128#endif
6129#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
6130 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
6131#endif
6132#ifdef _CS_XBS5_ILP32_OFF32_LIBS
6133 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
6134#endif
6135#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
6136 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
6137#endif
6138#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
6139 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
6140#endif
6141#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
6142 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
6143#endif
6144#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
6145 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
6146#endif
6147#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
6148 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
6149#endif
6150#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
6151 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
6152#endif
6153#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
6154 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
6155#endif
6156#ifdef _CS_XBS5_LP64_OFF64_LIBS
6157 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
6158#endif
6159#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6160 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
6161#endif
6162#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6163 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
6164#endif
6165#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6166 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
6167#endif
6168#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6169 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
6170#endif
6171#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6172 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
6173#endif
Fred Draked86ed291999-12-15 15:34:33 +00006174#ifdef _MIPS_CS_AVAIL_PROCESSORS
6175 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
6176#endif
6177#ifdef _MIPS_CS_BASE
6178 {"MIPS_CS_BASE", _MIPS_CS_BASE},
6179#endif
6180#ifdef _MIPS_CS_HOSTID
6181 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
6182#endif
6183#ifdef _MIPS_CS_HW_NAME
6184 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
6185#endif
6186#ifdef _MIPS_CS_NUM_PROCESSORS
6187 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
6188#endif
6189#ifdef _MIPS_CS_OSREL_MAJ
6190 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
6191#endif
6192#ifdef _MIPS_CS_OSREL_MIN
6193 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
6194#endif
6195#ifdef _MIPS_CS_OSREL_PATCH
6196 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
6197#endif
6198#ifdef _MIPS_CS_OS_NAME
6199 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
6200#endif
6201#ifdef _MIPS_CS_OS_PROVIDER
6202 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
6203#endif
6204#ifdef _MIPS_CS_PROCESSORS
6205 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
6206#endif
6207#ifdef _MIPS_CS_SERIAL
6208 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
6209#endif
6210#ifdef _MIPS_CS_VENDOR
6211 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
6212#endif
Fred Drakec9680921999-12-13 16:37:25 +00006213};
6214
6215static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006216conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006217{
6218 return conv_confname(arg, valuep, posix_constants_confstr,
6219 sizeof(posix_constants_confstr)
6220 / sizeof(struct constdef));
6221}
6222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006223PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006224"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006225Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006226
6227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006228posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006229{
6230 PyObject *result = NULL;
6231 int name;
6232 char buffer[64];
6233
6234 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
6235 int len = confstr(name, buffer, sizeof(buffer));
6236
Fred Drakec9680921999-12-13 16:37:25 +00006237 errno = 0;
6238 if (len == 0) {
6239 if (errno != 0)
6240 posix_error();
6241 else
6242 result = PyString_FromString("");
6243 }
6244 else {
6245 if (len >= sizeof(buffer)) {
6246 result = PyString_FromStringAndSize(NULL, len);
6247 if (result != NULL)
6248 confstr(name, PyString_AS_STRING(result), len+1);
6249 }
6250 else
6251 result = PyString_FromString(buffer);
6252 }
6253 }
6254 return result;
6255}
6256#endif
6257
6258
6259#ifdef HAVE_SYSCONF
6260static struct constdef posix_constants_sysconf[] = {
6261#ifdef _SC_2_CHAR_TERM
6262 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
6263#endif
6264#ifdef _SC_2_C_BIND
6265 {"SC_2_C_BIND", _SC_2_C_BIND},
6266#endif
6267#ifdef _SC_2_C_DEV
6268 {"SC_2_C_DEV", _SC_2_C_DEV},
6269#endif
6270#ifdef _SC_2_C_VERSION
6271 {"SC_2_C_VERSION", _SC_2_C_VERSION},
6272#endif
6273#ifdef _SC_2_FORT_DEV
6274 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
6275#endif
6276#ifdef _SC_2_FORT_RUN
6277 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
6278#endif
6279#ifdef _SC_2_LOCALEDEF
6280 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
6281#endif
6282#ifdef _SC_2_SW_DEV
6283 {"SC_2_SW_DEV", _SC_2_SW_DEV},
6284#endif
6285#ifdef _SC_2_UPE
6286 {"SC_2_UPE", _SC_2_UPE},
6287#endif
6288#ifdef _SC_2_VERSION
6289 {"SC_2_VERSION", _SC_2_VERSION},
6290#endif
Fred Draked86ed291999-12-15 15:34:33 +00006291#ifdef _SC_ABI_ASYNCHRONOUS_IO
6292 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
6293#endif
6294#ifdef _SC_ACL
6295 {"SC_ACL", _SC_ACL},
6296#endif
Fred Drakec9680921999-12-13 16:37:25 +00006297#ifdef _SC_AIO_LISTIO_MAX
6298 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
6299#endif
Fred Drakec9680921999-12-13 16:37:25 +00006300#ifdef _SC_AIO_MAX
6301 {"SC_AIO_MAX", _SC_AIO_MAX},
6302#endif
6303#ifdef _SC_AIO_PRIO_DELTA_MAX
6304 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
6305#endif
6306#ifdef _SC_ARG_MAX
6307 {"SC_ARG_MAX", _SC_ARG_MAX},
6308#endif
6309#ifdef _SC_ASYNCHRONOUS_IO
6310 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
6311#endif
6312#ifdef _SC_ATEXIT_MAX
6313 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
6314#endif
Fred Draked86ed291999-12-15 15:34:33 +00006315#ifdef _SC_AUDIT
6316 {"SC_AUDIT", _SC_AUDIT},
6317#endif
Fred Drakec9680921999-12-13 16:37:25 +00006318#ifdef _SC_AVPHYS_PAGES
6319 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
6320#endif
6321#ifdef _SC_BC_BASE_MAX
6322 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
6323#endif
6324#ifdef _SC_BC_DIM_MAX
6325 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
6326#endif
6327#ifdef _SC_BC_SCALE_MAX
6328 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
6329#endif
6330#ifdef _SC_BC_STRING_MAX
6331 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
6332#endif
Fred Draked86ed291999-12-15 15:34:33 +00006333#ifdef _SC_CAP
6334 {"SC_CAP", _SC_CAP},
6335#endif
Fred Drakec9680921999-12-13 16:37:25 +00006336#ifdef _SC_CHARCLASS_NAME_MAX
6337 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
6338#endif
6339#ifdef _SC_CHAR_BIT
6340 {"SC_CHAR_BIT", _SC_CHAR_BIT},
6341#endif
6342#ifdef _SC_CHAR_MAX
6343 {"SC_CHAR_MAX", _SC_CHAR_MAX},
6344#endif
6345#ifdef _SC_CHAR_MIN
6346 {"SC_CHAR_MIN", _SC_CHAR_MIN},
6347#endif
6348#ifdef _SC_CHILD_MAX
6349 {"SC_CHILD_MAX", _SC_CHILD_MAX},
6350#endif
6351#ifdef _SC_CLK_TCK
6352 {"SC_CLK_TCK", _SC_CLK_TCK},
6353#endif
6354#ifdef _SC_COHER_BLKSZ
6355 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
6356#endif
6357#ifdef _SC_COLL_WEIGHTS_MAX
6358 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
6359#endif
6360#ifdef _SC_DCACHE_ASSOC
6361 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
6362#endif
6363#ifdef _SC_DCACHE_BLKSZ
6364 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
6365#endif
6366#ifdef _SC_DCACHE_LINESZ
6367 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
6368#endif
6369#ifdef _SC_DCACHE_SZ
6370 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
6371#endif
6372#ifdef _SC_DCACHE_TBLKSZ
6373 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
6374#endif
6375#ifdef _SC_DELAYTIMER_MAX
6376 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
6377#endif
6378#ifdef _SC_EQUIV_CLASS_MAX
6379 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
6380#endif
6381#ifdef _SC_EXPR_NEST_MAX
6382 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
6383#endif
6384#ifdef _SC_FSYNC
6385 {"SC_FSYNC", _SC_FSYNC},
6386#endif
6387#ifdef _SC_GETGR_R_SIZE_MAX
6388 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
6389#endif
6390#ifdef _SC_GETPW_R_SIZE_MAX
6391 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
6392#endif
6393#ifdef _SC_ICACHE_ASSOC
6394 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
6395#endif
6396#ifdef _SC_ICACHE_BLKSZ
6397 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
6398#endif
6399#ifdef _SC_ICACHE_LINESZ
6400 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
6401#endif
6402#ifdef _SC_ICACHE_SZ
6403 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
6404#endif
Fred Draked86ed291999-12-15 15:34:33 +00006405#ifdef _SC_INF
6406 {"SC_INF", _SC_INF},
6407#endif
Fred Drakec9680921999-12-13 16:37:25 +00006408#ifdef _SC_INT_MAX
6409 {"SC_INT_MAX", _SC_INT_MAX},
6410#endif
6411#ifdef _SC_INT_MIN
6412 {"SC_INT_MIN", _SC_INT_MIN},
6413#endif
6414#ifdef _SC_IOV_MAX
6415 {"SC_IOV_MAX", _SC_IOV_MAX},
6416#endif
Fred Draked86ed291999-12-15 15:34:33 +00006417#ifdef _SC_IP_SECOPTS
6418 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
6419#endif
Fred Drakec9680921999-12-13 16:37:25 +00006420#ifdef _SC_JOB_CONTROL
6421 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
6422#endif
Fred Draked86ed291999-12-15 15:34:33 +00006423#ifdef _SC_KERN_POINTERS
6424 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
6425#endif
6426#ifdef _SC_KERN_SIM
6427 {"SC_KERN_SIM", _SC_KERN_SIM},
6428#endif
Fred Drakec9680921999-12-13 16:37:25 +00006429#ifdef _SC_LINE_MAX
6430 {"SC_LINE_MAX", _SC_LINE_MAX},
6431#endif
6432#ifdef _SC_LOGIN_NAME_MAX
6433 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
6434#endif
6435#ifdef _SC_LOGNAME_MAX
6436 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
6437#endif
6438#ifdef _SC_LONG_BIT
6439 {"SC_LONG_BIT", _SC_LONG_BIT},
6440#endif
Fred Draked86ed291999-12-15 15:34:33 +00006441#ifdef _SC_MAC
6442 {"SC_MAC", _SC_MAC},
6443#endif
Fred Drakec9680921999-12-13 16:37:25 +00006444#ifdef _SC_MAPPED_FILES
6445 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
6446#endif
6447#ifdef _SC_MAXPID
6448 {"SC_MAXPID", _SC_MAXPID},
6449#endif
6450#ifdef _SC_MB_LEN_MAX
6451 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
6452#endif
6453#ifdef _SC_MEMLOCK
6454 {"SC_MEMLOCK", _SC_MEMLOCK},
6455#endif
6456#ifdef _SC_MEMLOCK_RANGE
6457 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
6458#endif
6459#ifdef _SC_MEMORY_PROTECTION
6460 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
6461#endif
6462#ifdef _SC_MESSAGE_PASSING
6463 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
6464#endif
Fred Draked86ed291999-12-15 15:34:33 +00006465#ifdef _SC_MMAP_FIXED_ALIGNMENT
6466 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
6467#endif
Fred Drakec9680921999-12-13 16:37:25 +00006468#ifdef _SC_MQ_OPEN_MAX
6469 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
6470#endif
6471#ifdef _SC_MQ_PRIO_MAX
6472 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
6473#endif
Fred Draked86ed291999-12-15 15:34:33 +00006474#ifdef _SC_NACLS_MAX
6475 {"SC_NACLS_MAX", _SC_NACLS_MAX},
6476#endif
Fred Drakec9680921999-12-13 16:37:25 +00006477#ifdef _SC_NGROUPS_MAX
6478 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
6479#endif
6480#ifdef _SC_NL_ARGMAX
6481 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
6482#endif
6483#ifdef _SC_NL_LANGMAX
6484 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
6485#endif
6486#ifdef _SC_NL_MSGMAX
6487 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
6488#endif
6489#ifdef _SC_NL_NMAX
6490 {"SC_NL_NMAX", _SC_NL_NMAX},
6491#endif
6492#ifdef _SC_NL_SETMAX
6493 {"SC_NL_SETMAX", _SC_NL_SETMAX},
6494#endif
6495#ifdef _SC_NL_TEXTMAX
6496 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
6497#endif
6498#ifdef _SC_NPROCESSORS_CONF
6499 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
6500#endif
6501#ifdef _SC_NPROCESSORS_ONLN
6502 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
6503#endif
Fred Draked86ed291999-12-15 15:34:33 +00006504#ifdef _SC_NPROC_CONF
6505 {"SC_NPROC_CONF", _SC_NPROC_CONF},
6506#endif
6507#ifdef _SC_NPROC_ONLN
6508 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
6509#endif
Fred Drakec9680921999-12-13 16:37:25 +00006510#ifdef _SC_NZERO
6511 {"SC_NZERO", _SC_NZERO},
6512#endif
6513#ifdef _SC_OPEN_MAX
6514 {"SC_OPEN_MAX", _SC_OPEN_MAX},
6515#endif
6516#ifdef _SC_PAGESIZE
6517 {"SC_PAGESIZE", _SC_PAGESIZE},
6518#endif
6519#ifdef _SC_PAGE_SIZE
6520 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
6521#endif
6522#ifdef _SC_PASS_MAX
6523 {"SC_PASS_MAX", _SC_PASS_MAX},
6524#endif
6525#ifdef _SC_PHYS_PAGES
6526 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
6527#endif
6528#ifdef _SC_PII
6529 {"SC_PII", _SC_PII},
6530#endif
6531#ifdef _SC_PII_INTERNET
6532 {"SC_PII_INTERNET", _SC_PII_INTERNET},
6533#endif
6534#ifdef _SC_PII_INTERNET_DGRAM
6535 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
6536#endif
6537#ifdef _SC_PII_INTERNET_STREAM
6538 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
6539#endif
6540#ifdef _SC_PII_OSI
6541 {"SC_PII_OSI", _SC_PII_OSI},
6542#endif
6543#ifdef _SC_PII_OSI_CLTS
6544 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
6545#endif
6546#ifdef _SC_PII_OSI_COTS
6547 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
6548#endif
6549#ifdef _SC_PII_OSI_M
6550 {"SC_PII_OSI_M", _SC_PII_OSI_M},
6551#endif
6552#ifdef _SC_PII_SOCKET
6553 {"SC_PII_SOCKET", _SC_PII_SOCKET},
6554#endif
6555#ifdef _SC_PII_XTI
6556 {"SC_PII_XTI", _SC_PII_XTI},
6557#endif
6558#ifdef _SC_POLL
6559 {"SC_POLL", _SC_POLL},
6560#endif
6561#ifdef _SC_PRIORITIZED_IO
6562 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
6563#endif
6564#ifdef _SC_PRIORITY_SCHEDULING
6565 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
6566#endif
6567#ifdef _SC_REALTIME_SIGNALS
6568 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
6569#endif
6570#ifdef _SC_RE_DUP_MAX
6571 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
6572#endif
6573#ifdef _SC_RTSIG_MAX
6574 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
6575#endif
6576#ifdef _SC_SAVED_IDS
6577 {"SC_SAVED_IDS", _SC_SAVED_IDS},
6578#endif
6579#ifdef _SC_SCHAR_MAX
6580 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
6581#endif
6582#ifdef _SC_SCHAR_MIN
6583 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
6584#endif
6585#ifdef _SC_SELECT
6586 {"SC_SELECT", _SC_SELECT},
6587#endif
6588#ifdef _SC_SEMAPHORES
6589 {"SC_SEMAPHORES", _SC_SEMAPHORES},
6590#endif
6591#ifdef _SC_SEM_NSEMS_MAX
6592 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
6593#endif
6594#ifdef _SC_SEM_VALUE_MAX
6595 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
6596#endif
6597#ifdef _SC_SHARED_MEMORY_OBJECTS
6598 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
6599#endif
6600#ifdef _SC_SHRT_MAX
6601 {"SC_SHRT_MAX", _SC_SHRT_MAX},
6602#endif
6603#ifdef _SC_SHRT_MIN
6604 {"SC_SHRT_MIN", _SC_SHRT_MIN},
6605#endif
6606#ifdef _SC_SIGQUEUE_MAX
6607 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
6608#endif
6609#ifdef _SC_SIGRT_MAX
6610 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
6611#endif
6612#ifdef _SC_SIGRT_MIN
6613 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
6614#endif
Fred Draked86ed291999-12-15 15:34:33 +00006615#ifdef _SC_SOFTPOWER
6616 {"SC_SOFTPOWER", _SC_SOFTPOWER},
6617#endif
Fred Drakec9680921999-12-13 16:37:25 +00006618#ifdef _SC_SPLIT_CACHE
6619 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
6620#endif
6621#ifdef _SC_SSIZE_MAX
6622 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
6623#endif
6624#ifdef _SC_STACK_PROT
6625 {"SC_STACK_PROT", _SC_STACK_PROT},
6626#endif
6627#ifdef _SC_STREAM_MAX
6628 {"SC_STREAM_MAX", _SC_STREAM_MAX},
6629#endif
6630#ifdef _SC_SYNCHRONIZED_IO
6631 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
6632#endif
6633#ifdef _SC_THREADS
6634 {"SC_THREADS", _SC_THREADS},
6635#endif
6636#ifdef _SC_THREAD_ATTR_STACKADDR
6637 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
6638#endif
6639#ifdef _SC_THREAD_ATTR_STACKSIZE
6640 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
6641#endif
6642#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6643 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
6644#endif
6645#ifdef _SC_THREAD_KEYS_MAX
6646 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
6647#endif
6648#ifdef _SC_THREAD_PRIORITY_SCHEDULING
6649 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
6650#endif
6651#ifdef _SC_THREAD_PRIO_INHERIT
6652 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
6653#endif
6654#ifdef _SC_THREAD_PRIO_PROTECT
6655 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
6656#endif
6657#ifdef _SC_THREAD_PROCESS_SHARED
6658 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
6659#endif
6660#ifdef _SC_THREAD_SAFE_FUNCTIONS
6661 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
6662#endif
6663#ifdef _SC_THREAD_STACK_MIN
6664 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
6665#endif
6666#ifdef _SC_THREAD_THREADS_MAX
6667 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
6668#endif
6669#ifdef _SC_TIMERS
6670 {"SC_TIMERS", _SC_TIMERS},
6671#endif
6672#ifdef _SC_TIMER_MAX
6673 {"SC_TIMER_MAX", _SC_TIMER_MAX},
6674#endif
6675#ifdef _SC_TTY_NAME_MAX
6676 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
6677#endif
6678#ifdef _SC_TZNAME_MAX
6679 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
6680#endif
6681#ifdef _SC_T_IOV_MAX
6682 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
6683#endif
6684#ifdef _SC_UCHAR_MAX
6685 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
6686#endif
6687#ifdef _SC_UINT_MAX
6688 {"SC_UINT_MAX", _SC_UINT_MAX},
6689#endif
6690#ifdef _SC_UIO_MAXIOV
6691 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
6692#endif
6693#ifdef _SC_ULONG_MAX
6694 {"SC_ULONG_MAX", _SC_ULONG_MAX},
6695#endif
6696#ifdef _SC_USHRT_MAX
6697 {"SC_USHRT_MAX", _SC_USHRT_MAX},
6698#endif
6699#ifdef _SC_VERSION
6700 {"SC_VERSION", _SC_VERSION},
6701#endif
6702#ifdef _SC_WORD_BIT
6703 {"SC_WORD_BIT", _SC_WORD_BIT},
6704#endif
6705#ifdef _SC_XBS5_ILP32_OFF32
6706 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
6707#endif
6708#ifdef _SC_XBS5_ILP32_OFFBIG
6709 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
6710#endif
6711#ifdef _SC_XBS5_LP64_OFF64
6712 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
6713#endif
6714#ifdef _SC_XBS5_LPBIG_OFFBIG
6715 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
6716#endif
6717#ifdef _SC_XOPEN_CRYPT
6718 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
6719#endif
6720#ifdef _SC_XOPEN_ENH_I18N
6721 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
6722#endif
6723#ifdef _SC_XOPEN_LEGACY
6724 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
6725#endif
6726#ifdef _SC_XOPEN_REALTIME
6727 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
6728#endif
6729#ifdef _SC_XOPEN_REALTIME_THREADS
6730 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
6731#endif
6732#ifdef _SC_XOPEN_SHM
6733 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
6734#endif
6735#ifdef _SC_XOPEN_UNIX
6736 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
6737#endif
6738#ifdef _SC_XOPEN_VERSION
6739 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
6740#endif
6741#ifdef _SC_XOPEN_XCU_VERSION
6742 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
6743#endif
6744#ifdef _SC_XOPEN_XPG2
6745 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
6746#endif
6747#ifdef _SC_XOPEN_XPG3
6748 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
6749#endif
6750#ifdef _SC_XOPEN_XPG4
6751 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
6752#endif
6753};
6754
6755static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006756conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006757{
6758 return conv_confname(arg, valuep, posix_constants_sysconf,
6759 sizeof(posix_constants_sysconf)
6760 / sizeof(struct constdef));
6761}
6762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006763PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006764"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006765Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006766
6767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006768posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006769{
6770 PyObject *result = NULL;
6771 int name;
6772
6773 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6774 int value;
6775
6776 errno = 0;
6777 value = sysconf(name);
6778 if (value == -1 && errno != 0)
6779 posix_error();
6780 else
6781 result = PyInt_FromLong(value);
6782 }
6783 return result;
6784}
6785#endif
6786
6787
Fred Drakebec628d1999-12-15 18:31:10 +00006788/* This code is used to ensure that the tables of configuration value names
6789 * are in sorted order as required by conv_confname(), and also to build the
6790 * the exported dictionaries that are used to publish information about the
6791 * names available on the host platform.
6792 *
6793 * Sorting the table at runtime ensures that the table is properly ordered
6794 * when used, even for platforms we're not able to test on. It also makes
6795 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006796 */
Fred Drakebec628d1999-12-15 18:31:10 +00006797
6798static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006799cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006800{
6801 const struct constdef *c1 =
6802 (const struct constdef *) v1;
6803 const struct constdef *c2 =
6804 (const struct constdef *) v2;
6805
6806 return strcmp(c1->name, c2->name);
6807}
6808
6809static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006810setup_confname_table(struct constdef *table, size_t tablesize,
Fred Drake4d1e64b2002-04-15 19:40:07 +00006811 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006812{
Fred Drakebec628d1999-12-15 18:31:10 +00006813 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006814 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006815
6816 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6817 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006818 if (d == NULL)
6819 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006820
Barry Warsaw3155db32000-04-13 15:20:40 +00006821 for (i=0; i < tablesize; ++i) {
6822 PyObject *o = PyInt_FromLong(table[i].value);
6823 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6824 Py_XDECREF(o);
6825 Py_DECREF(d);
6826 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006827 }
Barry Warsaw3155db32000-04-13 15:20:40 +00006828 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006829 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006830 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006831}
6832
Fred Drakebec628d1999-12-15 18:31:10 +00006833/* Return -1 on failure, 0 on success. */
6834static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006835setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006836{
6837#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006838 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006839 sizeof(posix_constants_pathconf)
6840 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006841 "pathconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006842 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006843#endif
6844#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006845 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006846 sizeof(posix_constants_confstr)
6847 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006848 "confstr_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006849 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006850#endif
6851#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006852 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006853 sizeof(posix_constants_sysconf)
6854 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006855 "sysconf_names", module))
Fred Drakebec628d1999-12-15 18:31:10 +00006856 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006857#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006858 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006859}
Fred Draked86ed291999-12-15 15:34:33 +00006860
6861
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006862PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006863"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006864Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006865in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006866
6867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006868posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006869{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006870 abort();
6871 /*NOTREACHED*/
6872 Py_FatalError("abort() called from Python code didn't abort!");
6873 return NULL;
6874}
Fred Drakebec628d1999-12-15 18:31:10 +00006875
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006876#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006877PyDoc_STRVAR(win32_startfile__doc__,
6878"startfile(filepath) - Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006879\n\
6880This acts like double-clicking the file in Explorer, or giving the file\n\
6881name as an argument to the DOS \"start\" command: the file is opened\n\
6882with whatever application (if any) its extension is associated.\n\
6883\n\
6884startfile returns as soon as the associated application is launched.\n\
6885There is no option to wait for the application to close, and no way\n\
6886to retrieve the application's exit status.\n\
6887\n\
6888The filepath is relative to the current directory. If you want to use\n\
6889an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006890the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006891
6892static PyObject *
6893win32_startfile(PyObject *self, PyObject *args)
6894{
6895 char *filepath;
6896 HINSTANCE rc;
6897 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
6898 return NULL;
6899 Py_BEGIN_ALLOW_THREADS
6900 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
6901 Py_END_ALLOW_THREADS
6902 if (rc <= (HINSTANCE)32)
6903 return win32_error("startfile", filepath);
6904 Py_INCREF(Py_None);
6905 return Py_None;
6906}
6907#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006908
Martin v. Löwis438b5342002-12-27 10:16:42 +00006909#ifdef HAVE_GETLOADAVG
6910PyDoc_STRVAR(posix_getloadavg__doc__,
6911"getloadavg() -> (float, float, float)\n\n\
6912Return the number of processes in the system run queue averaged over\n\
6913the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6914was unobtainable");
6915
6916static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006917posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006918{
6919 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006920 if (getloadavg(loadavg, 3)!=3) {
6921 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6922 return NULL;
6923 } else
6924 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6925}
6926#endif
6927
6928
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006929static PyMethodDef posix_methods[] = {
6930 {"access", posix_access, METH_VARARGS, posix_access__doc__},
6931#ifdef HAVE_TTYNAME
6932 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
6933#endif
6934 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
6935 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006936#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006937 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006938#endif /* HAVE_CHOWN */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00006939#ifdef HAVE_LCHOWN
6940 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6941#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00006942#ifdef HAVE_CHROOT
6943 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6944#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006945#ifdef HAVE_CTERMID
Neal Norwitze241ce82003-02-17 18:17:05 +00006946 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006947#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00006948#ifdef HAVE_GETCWD
Neal Norwitze241ce82003-02-17 18:17:05 +00006949 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
Walter Dörwald3b918c32002-11-21 20:18:46 +00006950#ifdef Py_USING_UNICODE
Neal Norwitze241ce82003-02-17 18:17:05 +00006951 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00006952#endif
Walter Dörwald3b918c32002-11-21 20:18:46 +00006953#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006954#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006955 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006956#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006957 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6958 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6959 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006960#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006961 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006962#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006963#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006964 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006965#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006966 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
6967 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
6968 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Martin v. Löwisf607bda2002-10-16 18:27:39 +00006969 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006970#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006971 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006972#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006973#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006974 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006975#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006976 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006977#ifdef HAVE_UNAME
Neal Norwitze241ce82003-02-17 18:17:05 +00006978 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006979#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006980 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
6981 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
6982 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00006983#ifdef HAVE_TIMES
Neal Norwitze241ce82003-02-17 18:17:05 +00006984 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006985#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006986 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006987#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006988 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
6989 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006990#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00006991#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006992 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
6993 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00006994#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006995#ifdef HAVE_FORK1
Neal Norwitze241ce82003-02-17 18:17:05 +00006996 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006997#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00006998#ifdef HAVE_FORK
Neal Norwitze241ce82003-02-17 18:17:05 +00006999 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007000#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007001#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Neal Norwitze241ce82003-02-17 18:17:05 +00007002 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007003#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007004#ifdef HAVE_FORKPTY
Neal Norwitze241ce82003-02-17 18:17:05 +00007005 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007006#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007007#ifdef HAVE_GETEGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007008 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007009#endif /* HAVE_GETEGID */
7010#ifdef HAVE_GETEUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007011 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007012#endif /* HAVE_GETEUID */
7013#ifdef HAVE_GETGID
Neal Norwitze241ce82003-02-17 18:17:05 +00007014 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007015#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007016#ifdef HAVE_GETGROUPS
Neal Norwitze241ce82003-02-17 18:17:05 +00007017 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007018#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007019 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007020#ifdef HAVE_GETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007021 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007022#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007023#ifdef HAVE_GETPPID
Neal Norwitze241ce82003-02-17 18:17:05 +00007024 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007025#endif /* HAVE_GETPPID */
7026#ifdef HAVE_GETUID
Neal Norwitze241ce82003-02-17 18:17:05 +00007027 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007028#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007029#ifdef HAVE_GETLOGIN
Neal Norwitze241ce82003-02-17 18:17:05 +00007030 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007031#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007032#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007033 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007034#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007035#ifdef HAVE_KILLPG
7036 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
7037#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007038#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007039 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007040#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007041#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007042 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007043#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007044 {"popen2", win32_popen2, METH_VARARGS},
7045 {"popen3", win32_popen3, METH_VARARGS},
7046 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00007047 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007048#else
7049#if defined(PYOS_OS2) && defined(PYCC_GCC)
7050 {"popen2", os2emx_popen2, METH_VARARGS},
7051 {"popen3", os2emx_popen3, METH_VARARGS},
7052 {"popen4", os2emx_popen4, METH_VARARGS},
7053#endif
Fredrik Lundhffb9c772000-07-09 14:49:51 +00007054#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007055#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007056#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007057 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007058#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007059#ifdef HAVE_SETEUID
7060 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
7061#endif /* HAVE_SETEUID */
7062#ifdef HAVE_SETEGID
7063 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
7064#endif /* HAVE_SETEGID */
7065#ifdef HAVE_SETREUID
7066 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
7067#endif /* HAVE_SETREUID */
7068#ifdef HAVE_SETREGID
7069 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
7070#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007071#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007072 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007073#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007074#ifdef HAVE_SETGROUPS
7075 {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__},
7076#endif /* HAVE_SETGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007077#ifdef HAVE_GETPGID
7078 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
7079#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007080#ifdef HAVE_SETPGRP
Neal Norwitze241ce82003-02-17 18:17:05 +00007081 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007082#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007083#ifdef HAVE_WAIT
Neal Norwitze241ce82003-02-17 18:17:05 +00007084 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007085#endif /* HAVE_WAIT */
Tim Petersab034fa2002-02-01 11:27:43 +00007086#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007087 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007088#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007089#ifdef HAVE_GETSID
7090 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
7091#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007092#ifdef HAVE_SETSID
Neal Norwitze241ce82003-02-17 18:17:05 +00007093 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007094#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007095#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007096 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007097#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007098#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007099 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007100#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007101#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007102 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007103#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007104 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7105 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7106 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7107 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7108 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7109 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7110 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7111 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7112 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00007113 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007114#ifdef HAVE_PIPE
Neal Norwitze241ce82003-02-17 18:17:05 +00007115 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007116#endif
7117#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007118 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007119#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007120#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007121 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
7122#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007123#ifdef HAVE_DEVICE_MACROS
7124 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7125 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7126 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
7127#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007128#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007129 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007130#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007131#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007132 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007133#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007134#ifdef HAVE_UNSETENV
7135 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
7136#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00007137#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007138 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00007139#endif
Fred Drake4d1e64b2002-04-15 19:40:07 +00007140#ifdef HAVE_FCHDIR
7141 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
7142#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007143#ifdef HAVE_FSYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007144 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007145#endif
7146#ifdef HAVE_FDATASYNC
Fred Drake4d1e64b2002-04-15 19:40:07 +00007147 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007148#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007149#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007150#ifdef WCOREDUMP
7151 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
7152#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007153#ifdef WIFCONTINUED
7154 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
7155#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007156#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007157 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007158#endif /* WIFSTOPPED */
7159#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007160 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007161#endif /* WIFSIGNALED */
7162#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007163 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007164#endif /* WIFEXITED */
7165#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007166 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007167#endif /* WEXITSTATUS */
7168#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007169 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007170#endif /* WTERMSIG */
7171#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007172 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007173#endif /* WSTOPSIG */
7174#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007175#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007176 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007177#endif
7178#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007179 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007180#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00007181#ifdef HAVE_TMPFILE
Neal Norwitze241ce82003-02-17 18:17:05 +00007182 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007183#endif
7184#ifdef HAVE_TEMPNAM
7185 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
7186#endif
7187#ifdef HAVE_TMPNAM
Neal Norwitze241ce82003-02-17 18:17:05 +00007188 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007189#endif
Fred Drakec9680921999-12-13 16:37:25 +00007190#ifdef HAVE_CONFSTR
7191 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
7192#endif
7193#ifdef HAVE_SYSCONF
7194 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
7195#endif
7196#ifdef HAVE_FPATHCONF
7197 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
7198#endif
7199#ifdef HAVE_PATHCONF
7200 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7201#endif
Neal Norwitze241ce82003-02-17 18:17:05 +00007202 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007203#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00007204 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7205#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007206#ifdef HAVE_GETLOADAVG
Neal Norwitze241ce82003-02-17 18:17:05 +00007207 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007208#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007209 {NULL, NULL} /* Sentinel */
7210};
7211
7212
Barry Warsaw4a342091996-12-19 23:50:02 +00007213static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007214ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007215{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007216 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007217}
7218
Guido van Rossumd48f2521997-12-05 22:19:34 +00007219#if defined(PYOS_OS2)
7220/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007221static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007222{
7223 APIRET rc;
7224 ULONG values[QSV_MAX+1];
7225 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007226 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007227
7228 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007229 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007230 Py_END_ALLOW_THREADS
7231
7232 if (rc != NO_ERROR) {
7233 os2_error(rc);
7234 return -1;
7235 }
7236
Fred Drake4d1e64b2002-04-15 19:40:07 +00007237 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7238 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7239 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7240 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7241 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7242 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7243 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007244
7245 switch (values[QSV_VERSION_MINOR]) {
7246 case 0: ver = "2.00"; break;
7247 case 10: ver = "2.10"; break;
7248 case 11: ver = "2.11"; break;
7249 case 30: ver = "3.00"; break;
7250 case 40: ver = "4.00"; break;
7251 case 50: ver = "5.00"; break;
7252 default:
Tim Peters885d4572001-11-28 20:27:42 +00007253 PyOS_snprintf(tmp, sizeof(tmp),
7254 "%d-%d", values[QSV_VERSION_MAJOR],
7255 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007256 ver = &tmp[0];
7257 }
7258
7259 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007260 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007261 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007262
7263 /* Add Indicator of Which Drive was Used to Boot the System */
7264 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7265 tmp[1] = ':';
7266 tmp[2] = '\0';
7267
Fred Drake4d1e64b2002-04-15 19:40:07 +00007268 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007269}
7270#endif
7271
Barry Warsaw4a342091996-12-19 23:50:02 +00007272static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007273all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007274{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007275#ifdef F_OK
7276 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007277#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007278#ifdef R_OK
7279 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007280#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007281#ifdef W_OK
7282 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007283#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007284#ifdef X_OK
7285 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007286#endif
Fred Drakec9680921999-12-13 16:37:25 +00007287#ifdef NGROUPS_MAX
7288 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
7289#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007290#ifdef TMP_MAX
7291 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
7292#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007293#ifdef WCONTINUED
7294 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
7295#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007296#ifdef WNOHANG
7297 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007298#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007299#ifdef WUNTRACED
7300 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
7301#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007302#ifdef O_RDONLY
7303 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
7304#endif
7305#ifdef O_WRONLY
7306 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
7307#endif
7308#ifdef O_RDWR
7309 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
7310#endif
7311#ifdef O_NDELAY
7312 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
7313#endif
7314#ifdef O_NONBLOCK
7315 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
7316#endif
7317#ifdef O_APPEND
7318 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
7319#endif
7320#ifdef O_DSYNC
7321 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
7322#endif
7323#ifdef O_RSYNC
7324 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
7325#endif
7326#ifdef O_SYNC
7327 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
7328#endif
7329#ifdef O_NOCTTY
7330 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
7331#endif
7332#ifdef O_CREAT
7333 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
7334#endif
7335#ifdef O_EXCL
7336 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
7337#endif
7338#ifdef O_TRUNC
7339 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
7340#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007341#ifdef O_BINARY
7342 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
7343#endif
7344#ifdef O_TEXT
7345 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
7346#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007347#ifdef O_LARGEFILE
7348 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
7349#endif
7350
Tim Peters5aa91602002-01-30 05:46:57 +00007351/* MS Windows */
7352#ifdef O_NOINHERIT
7353 /* Don't inherit in child processes. */
7354 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
7355#endif
7356#ifdef _O_SHORT_LIVED
7357 /* Optimize for short life (keep in memory). */
7358 /* MS forgot to define this one with a non-underscore form too. */
7359 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
7360#endif
7361#ifdef O_TEMPORARY
7362 /* Automatically delete when last handle is closed. */
7363 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
7364#endif
7365#ifdef O_RANDOM
7366 /* Optimize for random access. */
7367 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
7368#endif
7369#ifdef O_SEQUENTIAL
7370 /* Optimize for sequential access. */
7371 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
7372#endif
7373
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007374/* GNU extensions. */
7375#ifdef O_DIRECT
7376 /* Direct disk access. */
7377 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
7378#endif
7379#ifdef O_DIRECTORY
7380 /* Must be a directory. */
7381 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
7382#endif
7383#ifdef O_NOFOLLOW
7384 /* Do not follow links. */
7385 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
7386#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007387
Barry Warsaw5676bd12003-01-07 20:57:09 +00007388 /* These come from sysexits.h */
7389#ifdef EX_OK
7390 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007391#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007392#ifdef EX_USAGE
7393 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007394#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007395#ifdef EX_DATAERR
7396 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007397#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007398#ifdef EX_NOINPUT
7399 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007400#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007401#ifdef EX_NOUSER
7402 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007403#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007404#ifdef EX_NOHOST
7405 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007406#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007407#ifdef EX_UNAVAILABLE
7408 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007409#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007410#ifdef EX_SOFTWARE
7411 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007412#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007413#ifdef EX_OSERR
7414 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007415#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007416#ifdef EX_OSFILE
7417 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007418#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007419#ifdef EX_CANTCREAT
7420 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007421#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007422#ifdef EX_IOERR
7423 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007424#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007425#ifdef EX_TEMPFAIL
7426 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007427#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007428#ifdef EX_PROTOCOL
7429 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007430#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007431#ifdef EX_NOPERM
7432 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007433#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007434#ifdef EX_CONFIG
7435 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007436#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007437#ifdef EX_NOTFOUND
7438 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007439#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007440
Guido van Rossum246bc171999-02-01 23:54:31 +00007441#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007442#if defined(PYOS_OS2) && defined(PYCC_GCC)
7443 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7444 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7445 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7446 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7447 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7448 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7449 if (ins(d, "P_PM", (long)P_PM)) return -1;
7450 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7451 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7452 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7453 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7454 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7455 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7456 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7457 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7458 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7459 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7460 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7461 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7462 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
7463#else
Guido van Rossum7d385291999-02-16 19:38:04 +00007464 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7465 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7466 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7467 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7468 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007469#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007470#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007471
Guido van Rossumd48f2521997-12-05 22:19:34 +00007472#if defined(PYOS_OS2)
7473 if (insertvalues(d)) return -1;
7474#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007475 return 0;
7476}
7477
7478
Tim Peters5aa91602002-01-30 05:46:57 +00007479#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007480#define INITFUNC initnt
7481#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007482
7483#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007484#define INITFUNC initos2
7485#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007486
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007487#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007488#define INITFUNC initposix
7489#define MODNAME "posix"
7490#endif
7491
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007492PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007493INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007494{
Fred Drake4d1e64b2002-04-15 19:40:07 +00007495 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007496
Fred Drake4d1e64b2002-04-15 19:40:07 +00007497 m = Py_InitModule3(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007498 posix_methods,
Fred Drake4d1e64b2002-04-15 19:40:07 +00007499 posix__doc__);
Tim Peters5aa91602002-01-30 05:46:57 +00007500
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007501 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007502 v = convertenviron();
Fred Drake4d1e64b2002-04-15 19:40:07 +00007503 Py_XINCREF(v);
7504 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007505 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00007506 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007507
Fred Drake4d1e64b2002-04-15 19:40:07 +00007508 if (all_ins(m))
Barry Warsaw4a342091996-12-19 23:50:02 +00007509 return;
7510
Fred Drake4d1e64b2002-04-15 19:40:07 +00007511 if (setup_confname_tables(m))
Fred Drakebec628d1999-12-15 18:31:10 +00007512 return;
7513
Fred Drake4d1e64b2002-04-15 19:40:07 +00007514 Py_INCREF(PyExc_OSError);
7515 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007516
Guido van Rossumb3d39562000-01-31 18:41:26 +00007517#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00007518 if (posix_putenv_garbage == NULL)
7519 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007520#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007521
Guido van Rossum14648392001-12-08 18:02:58 +00007522 stat_result_desc.name = MODNAME ".stat_result";
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007523 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7524 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7525 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007526 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00007527 structseq_new = StatResultType.tp_new;
7528 StatResultType.tp_new = statresult_new;
Fred Drake4d1e64b2002-04-15 19:40:07 +00007529 Py_INCREF((PyObject*) &StatResultType);
7530 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007531
Guido van Rossum14648392001-12-08 18:02:58 +00007532 statvfs_result_desc.name = MODNAME ".statvfs_result";
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007533 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Fred Drake4d1e64b2002-04-15 19:40:07 +00007534 Py_INCREF((PyObject*) &StatVFSResultType);
7535 PyModule_AddObject(m, "statvfs_result",
7536 (PyObject*) &StatVFSResultType);
Guido van Rossumb6775db1994-08-01 11:34:53 +00007537}