blob: cb3c72dd5c4faf9edce3657ea8b5250d4cbe8efb [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
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004/* This file is also used for Windows NT and MS-Win. In that case the module
Guido van Rossumad0ee831995-03-01 10:34:45 +00005 actually calls itself 'nt', not 'posix', and a few functions are
6 either unimplemented or implemented differently. The source
Guido van Rossum8d665e61996-06-26 18:22:49 +00007 assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000013static char posix__doc__ [] =
14"This module provides access to operating system functionality that is\n\
15standardized by the C Standard and the POSIX standard (a thinly\n\
16disguised Unix interface). Refer to the library manual and\n\
17corresponding Unix manual entries for more information on calls.";
18
Barry Warsaw53699e91996-12-10 23:23:01 +000019#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000021#if defined(PYOS_OS2)
22#define INCL_DOS
23#define INCL_DOSERRORS
24#define INCL_DOSPROCESS
25#define INCL_NOPMAPI
26#include <os2.h>
27#endif
28
Guido van Rossumb6775db1994-08-01 11:34:53 +000029#include <sys/types.h>
30#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000031#ifdef HAVE_SYS_WAIT_H
32#include <sys/wait.h> /* For WNOHANG */
33#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000034
Guido van Rossuma376cc51996-12-05 23:43:35 +000035#ifdef HAVE_SIGNAL_H
36#include <signal.h>
37#endif
38
Guido van Rossumb6775db1994-08-01 11:34:53 +000039#ifdef HAVE_FCNTL_H
40#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000041#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000042
Guido van Rossuma4916fa1996-05-23 22:58:55 +000043/* Various compilers have only certain posix functions */
Guido van Rossum6d8841c1997-08-14 19:57:39 +000044/* XXX Gosh I wish these were all moved into config.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000045#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046#include <process.h>
47#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000048#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000049#define HAVE_GETCWD 1
50#define HAVE_OPENDIR 1
51#define HAVE_SYSTEM 1
52#if defined(__OS2__)
53#define HAVE_EXECV 1
54#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000055#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000056#include <process.h>
57#else
58#ifdef __BORLANDC__ /* Borland compiler */
59#define HAVE_EXECV 1
60#define HAVE_GETCWD 1
61#define HAVE_GETEGID 1
62#define HAVE_GETEUID 1
63#define HAVE_GETGID 1
64#define HAVE_GETPPID 1
65#define HAVE_GETUID 1
66#define HAVE_KILL 1
67#define HAVE_OPENDIR 1
68#define HAVE_PIPE 1
69#define HAVE_POPEN 1
70#define HAVE_SYSTEM 1
71#define HAVE_WAIT 1
72#else
73#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000074#define HAVE_GETCWD 1
75#ifdef MS_WIN32
Guido van Rossuma1065681999-01-25 23:20:23 +000076#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000077#define HAVE_EXECV 1
78#define HAVE_PIPE 1
79#define HAVE_POPEN 1
80#define HAVE_SYSTEM 1
81#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +000082#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000083#else /* all other compilers */
84/* Unix functions that the configure script doesn't check for */
85#define HAVE_EXECV 1
86#define HAVE_FORK 1
87#define HAVE_GETCWD 1
88#define HAVE_GETEGID 1
89#define HAVE_GETEUID 1
90#define HAVE_GETGID 1
91#define HAVE_GETPPID 1
92#define HAVE_GETUID 1
93#define HAVE_KILL 1
94#define HAVE_OPENDIR 1
95#define HAVE_PIPE 1
96#define HAVE_POPEN 1
97#define HAVE_SYSTEM 1
98#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +000099#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000100#endif /* _MSC_VER */
101#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000102#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000103#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000104
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000106
Guido van Rossumb6775db1994-08-01 11:34:53 +0000107#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000108#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000109#endif
110
111#ifdef NeXT
112/* NeXT's <unistd.h> and <utime.h> aren't worth much */
113#undef HAVE_UNISTD_H
114#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000115#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000116/* #undef HAVE_GETCWD */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000117#define UNION_WAIT /* This should really be checked for by autoconf */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000118#endif
119
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000120#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000121#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000122extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000123#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000124#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000125extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000127extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000129#endif
130#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000131extern int chdir(char *);
132extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000133#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000134extern int chdir(const char *);
135extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000136#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000137extern int chmod(const char *, mode_t);
138extern int chown(const char *, uid_t, gid_t);
139extern char *getcwd(char *, int);
140extern char *strerror(int);
141extern int link(const char *, const char *);
142extern int rename(const char *, const char *);
143extern int stat(const char *, struct stat *);
144extern int unlink(const char *);
145extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000147extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000148#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000150extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000151#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000153
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156#ifdef HAVE_UTIME_H
157#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000158#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000160#ifdef HAVE_SYS_UTIME_H
161#include <sys/utime.h>
162#define HAVE_UTIME_H /* pretend we do for the rest of this file */
163#endif /* HAVE_SYS_UTIME_H */
164
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165#ifdef HAVE_SYS_TIMES_H
166#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000167#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168
169#ifdef HAVE_SYS_PARAM_H
170#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000171#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000172
173#ifdef HAVE_SYS_UTSNAME_H
174#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000175#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176
177#ifndef MAXPATHLEN
178#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000179#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000181#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000183#define NAMLEN(dirent) strlen((dirent)->d_name)
184#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000185#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000186#include <direct.h>
187#define NAMLEN(dirent) strlen((dirent)->d_name)
188#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000190#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000191#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000192#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000193#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000194#endif
195#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000197#endif
198#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000200#endif
201#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000203#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#include <direct.h>
205#include <io.h>
206#include <process.h>
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000207#define WINDOWS_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000209#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000211#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000212#else /* 16-bit Windows */
213#include <dos.h>
214#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000215#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217
Guido van Rossumd48f2521997-12-05 22:19:34 +0000218#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000220#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000222#ifdef UNION_WAIT
223/* Emulate some macros on systems that have a union instead of macros */
224
225#ifndef WIFEXITED
226#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
227#endif
228
229#ifndef WEXITSTATUS
230#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
231#endif
232
233#ifndef WTERMSIG
234#define WTERMSIG(u_wait) ((u_wait).w_termsig)
235#endif
236
237#endif /* UNION_WAIT */
238
Greg Wardb48bc172000-03-01 21:51:56 +0000239/* Don't use the "_r" form if we don't need it (also, won't have a
240 prototype for it, at least on Solaris -- maybe others as well?). */
241#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
242#define USE_CTERMID_R
243#endif
244
245#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
246#define USE_TMPNAM_R
247#endif
248
Fred Drake699f3522000-06-29 21:12:41 +0000249/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000250#undef STAT
Fred Drake699f3522000-06-29 21:12:41 +0000251#ifdef MS_WIN64
252# define STAT _stati64
253# define FSTAT _fstati64
254# define STRUCT_STAT struct _stati64
255#else
256# define STAT stat
257# define FSTAT fstat
258# define STRUCT_STAT struct stat
259#endif
260
261
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262/* Return a dictionary corresponding to the POSIX environment table */
263
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000264#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000266#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267
Barry Warsaw53699e91996-12-10 23:23:01 +0000268static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000269convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
Barry Warsaw53699e91996-12-10 23:23:01 +0000271 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000273 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274 if (d == NULL)
275 return NULL;
276 if (environ == NULL)
277 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000278 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000280 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000281 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282 char *p = strchr(*e, '=');
283 if (p == NULL)
284 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000285 k = PyString_FromStringAndSize(*e, (int)(p-*e));
286 if (k == NULL) {
287 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000289 }
290 v = PyString_FromString(p+1);
291 if (v == NULL) {
292 PyErr_Clear();
293 Py_DECREF(k);
294 continue;
295 }
296 if (PyDict_GetItem(d, k) == NULL) {
297 if (PyDict_SetItem(d, k, v) != 0)
298 PyErr_Clear();
299 }
300 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000301 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000303#if defined(PYOS_OS2)
304 {
305 APIRET rc;
306 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
307
308 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000309 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000310 PyObject *v = PyString_FromString(buffer);
311 PyDict_SetItemString(d, "BEGINLIBPATH", v);
312 Py_DECREF(v);
313 }
314 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
315 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
316 PyObject *v = PyString_FromString(buffer);
317 PyDict_SetItemString(d, "ENDLIBPATH", v);
318 Py_DECREF(v);
319 }
320 }
321#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 return d;
323}
324
325
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326/* Set a POSIX-specific error from errno, and return NULL */
327
Barry Warsawd58d7641998-07-23 16:14:40 +0000328static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000329posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000330{
Barry Warsawca74da41999-02-09 19:31:45 +0000331 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332}
Barry Warsawd58d7641998-07-23 16:14:40 +0000333static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000334posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000335{
Barry Warsawca74da41999-02-09 19:31:45 +0000336 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000337}
338
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000339#ifdef MS_WIN32
340static PyObject *
341win32_error(char* function, char* filename)
342{
Mark Hammond33a6da92000-08-15 00:46:38 +0000343 /* XXX We should pass the function name along in the future.
344 (_winreg.c also wants to pass the function name.)
345 This would however require an additional param to the
346 Windows error object, which is non-trivial.
347 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000348 errno = GetLastError();
349 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000350 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000351 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000352 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000353}
354#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355
Guido van Rossumd48f2521997-12-05 22:19:34 +0000356#if defined(PYOS_OS2)
357/**********************************************************************
358 * Helper Function to Trim and Format OS/2 Messages
359 **********************************************************************/
360 static void
361os2_formatmsg(char *msgbuf, int msglen, char *reason)
362{
363 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
364
365 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
366 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
367
368 while (lastc > msgbuf && isspace(*lastc))
369 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
370 }
371
372 /* Add Optional Reason Text */
373 if (reason) {
374 strcat(msgbuf, " : ");
375 strcat(msgbuf, reason);
376 }
377}
378
379/**********************************************************************
380 * Decode an OS/2 Operating System Error Code
381 *
382 * A convenience function to lookup an OS/2 error code and return a
383 * text message we can use to raise a Python exception.
384 *
385 * Notes:
386 * The messages for errors returned from the OS/2 kernel reside in
387 * the file OSO001.MSG in the \OS2 directory hierarchy.
388 *
389 **********************************************************************/
390 static char *
391os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
392{
393 APIRET rc;
394 ULONG msglen;
395
396 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
397 Py_BEGIN_ALLOW_THREADS
398 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
399 errorcode, "oso001.msg", &msglen);
400 Py_END_ALLOW_THREADS
401
402 if (rc == NO_ERROR)
403 os2_formatmsg(msgbuf, msglen, reason);
404 else
405 sprintf(msgbuf, "unknown OS error #%d", errorcode);
406
407 return msgbuf;
408}
409
410/* Set an OS/2-specific error and return NULL. OS/2 kernel
411 errors are not in a global variable e.g. 'errno' nor are
412 they congruent with posix error numbers. */
413
414static PyObject * os2_error(int code)
415{
416 char text[1024];
417 PyObject *v;
418
419 os2_strerror(text, sizeof(text), code, "");
420
421 v = Py_BuildValue("(is)", code, text);
422 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000423 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000424 Py_DECREF(v);
425 }
426 return NULL; /* Signal to Python that an Exception is Pending */
427}
428
429#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430
431/* POSIX generic methods */
432
Barry Warsaw53699e91996-12-10 23:23:01 +0000433static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000434posix_int(PyObject *args, char *format, int (*func)(int))
Guido van Rossum21142a01999-01-08 21:05:37 +0000435{
436 int fd;
437 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000438 if (!PyArg_ParseTuple(args, format, &fd))
Guido van Rossum21142a01999-01-08 21:05:37 +0000439 return NULL;
440 Py_BEGIN_ALLOW_THREADS
441 res = (*func)(fd);
442 Py_END_ALLOW_THREADS
443 if (res < 0)
444 return posix_error();
445 Py_INCREF(Py_None);
446 return Py_None;
447}
448
449
450static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000451posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000453 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000454 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000455 if (!PyArg_ParseTuple(args, format, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000457 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000458 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000459 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000460 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000461 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000462 Py_INCREF(Py_None);
463 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464}
465
Barry Warsaw53699e91996-12-10 23:23:01 +0000466static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000467posix_2str(PyObject *args, char *format,
468 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000470 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000471 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000472 if (!PyArg_ParseTuple(args, format, &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000474 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000475 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000476 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000477 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000478 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000480 Py_INCREF(Py_None);
481 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482}
483
Fred Drake699f3522000-06-29 21:12:41 +0000484/* pack a system stat C structure into the Python stat tuple
485 (used by posix_stat() and posix_fstat()) */
486static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000487_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000488{
489 PyObject *v = PyTuple_New(10);
490 if (v == NULL)
491 return NULL;
492
493 PyTuple_SetItem(v, 0, PyInt_FromLong((long)st.st_mode));
494#ifdef HAVE_LARGEFILE_SUPPORT
495 PyTuple_SetItem(v, 1, PyLong_FromLongLong((LONG_LONG)st.st_ino));
496#else
497 PyTuple_SetItem(v, 1, PyInt_FromLong((long)st.st_ino));
498#endif
499#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
500 PyTuple_SetItem(v, 2, PyLong_FromLongLong((LONG_LONG)st.st_dev));
501#else
502 PyTuple_SetItem(v, 2, PyInt_FromLong((long)st.st_dev));
503#endif
504 PyTuple_SetItem(v, 3, PyInt_FromLong((long)st.st_nlink));
505 PyTuple_SetItem(v, 4, PyInt_FromLong((long)st.st_uid));
506 PyTuple_SetItem(v, 5, PyInt_FromLong((long)st.st_gid));
507#ifdef HAVE_LARGEFILE_SUPPORT
508 PyTuple_SetItem(v, 6, PyLong_FromLongLong((LONG_LONG)st.st_size));
509#else
510 PyTuple_SetItem(v, 6, PyInt_FromLong(st.st_size));
511#endif
512#if SIZEOF_TIME_T > SIZEOF_LONG
513 PyTuple_SetItem(v, 7, PyLong_FromLongLong((LONG_LONG)st.st_atime));
514 PyTuple_SetItem(v, 8, PyLong_FromLongLong((LONG_LONG)st.st_mtime));
515 PyTuple_SetItem(v, 9, PyLong_FromLongLong((LONG_LONG)st.st_ctime));
516#else
517 PyTuple_SetItem(v, 7, PyInt_FromLong((long)st.st_atime));
518 PyTuple_SetItem(v, 8, PyInt_FromLong((long)st.st_mtime));
519 PyTuple_SetItem(v, 9, PyInt_FromLong((long)st.st_ctime));
520#endif
521
522 if (PyErr_Occurred()) {
523 Py_DECREF(v);
524 return NULL;
525 }
526
527 return v;
528}
529
530
Barry Warsaw53699e91996-12-10 23:23:01 +0000531static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000532posix_do_stat(PyObject *self, PyObject *args, char *format,
533 int (*statfunc)(const char *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534{
Fred Drake699f3522000-06-29 21:12:41 +0000535 STRUCT_STAT st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000536 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000537 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000538
539#ifdef MS_WIN32
540 int pathlen;
541 char pathcopy[MAX_PATH];
542#endif /* MS_WIN32 */
543
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000544 if (!PyArg_ParseTuple(args, format, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545 return NULL;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000546
547#ifdef MS_WIN32
548 pathlen = strlen(path);
549 /* the library call can blow up if the file name is too long! */
550 if (pathlen > MAX_PATH) {
551 errno = ENAMETOOLONG;
552 return posix_error();
553 }
554
555 if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) {
Guido van Rossum19dde102000-05-03 02:44:55 +0000556 /* exception for specific or current drive root */
557 if (!((pathlen == 1) ||
558 ((pathlen == 3) &&
Guido van Rossumace88ae2000-04-21 18:54:45 +0000559 (path[1] == ':') &&
Guido van Rossum19dde102000-05-03 02:44:55 +0000560 (path[2] == '\\' || path[2] == '/'))))
Guido van Rossumace88ae2000-04-21 18:54:45 +0000561 {
562 strncpy(pathcopy, path, pathlen);
563 pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */
564 path = pathcopy;
565 }
566 }
567#endif /* MS_WIN32 */
568
Barry Warsaw53699e91996-12-10 23:23:01 +0000569 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000570 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000571 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000572 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000573 return posix_error_with_filename(path);
Fred Drake699f3522000-06-29 21:12:41 +0000574
575 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576}
577
578
579/* POSIX methods */
580
Guido van Rossum94f6f721999-01-06 18:42:14 +0000581static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000582"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000583Test for access to a file.";
584
585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000586posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +0000587{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000588 char *path;
589 int mode;
590 int res;
591
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000592 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +0000593 return NULL;
594 Py_BEGIN_ALLOW_THREADS
595 res = access(path, mode);
596 Py_END_ALLOW_THREADS
597 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000598}
599
Guido van Rossumd371ff11999-01-25 16:12:23 +0000600#ifndef F_OK
601#define F_OK 0
602#endif
603#ifndef R_OK
604#define R_OK 4
605#endif
606#ifndef W_OK
607#define W_OK 2
608#endif
609#ifndef X_OK
610#define X_OK 1
611#endif
612
613#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000614static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000615"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000616Return the name of the terminal device connected to 'fd'.";
617
618static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000619posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +0000620{
Guido van Rossum94f6f721999-01-06 18:42:14 +0000621 int id;
622 char *ret;
623
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000624 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +0000625 return NULL;
626
Guido van Rossum94f6f721999-01-06 18:42:14 +0000627 ret = ttyname(id);
628 if (ret == NULL)
629 return(posix_error());
630 return(PyString_FromString(ret));
631}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000632#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000633
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000634#ifdef HAVE_CTERMID
635static char posix_ctermid__doc__[] =
636"ctermid() -> String\n\
637Return the name of the controlling terminal for this process.";
638
639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000640posix_ctermid(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000641{
642 char *ret;
643 char buffer[L_ctermid];
644
645 if (!PyArg_ParseTuple(args, ":ctermid"))
646 return NULL;
647
Greg Wardb48bc172000-03-01 21:51:56 +0000648#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000649 ret = ctermid_r(buffer);
650#else
651 ret = ctermid(buffer);
652#endif
653 if (ret == NULL)
654 return(posix_error());
655 return(PyString_FromString(buffer));
656}
657#endif
658
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000659static char posix_chdir__doc__[] =
660"chdir(path) -> None\n\
661Change the current working directory to the specified path.";
662
Barry Warsaw53699e91996-12-10 23:23:01 +0000663static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000664posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000666 return posix_1str(args, "s:chdir", chdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000669
670static char posix_chmod__doc__[] =
671"chmod(path, mode) -> None\n\
672Change the access permissions of a file.";
673
Barry Warsaw53699e91996-12-10 23:23:01 +0000674static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000675posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676{
Guido van Rossumffd15f52000-03-31 00:47:28 +0000677 char *path;
678 int i;
679 int res;
Guido van Rossum49679b42000-03-31 00:48:21 +0000680 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +0000681 return NULL;
682 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +0000683 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +0000684 Py_END_ALLOW_THREADS
685 if (res < 0)
686 return posix_error_with_filename(path);
687 Py_INCREF(Py_None);
688 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000689}
690
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000691
Guido van Rossum21142a01999-01-08 21:05:37 +0000692#ifdef HAVE_FSYNC
693static char posix_fsync__doc__[] =
694"fsync(fildes) -> None\n\
695force write of file with filedescriptor to disk.";
696
697static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000698posix_fsync(PyObject *self, PyObject *args)
Guido van Rossum21142a01999-01-08 21:05:37 +0000699{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000700 return posix_int(args, "i:fsync", fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000701}
702#endif /* HAVE_FSYNC */
703
704#ifdef HAVE_FDATASYNC
705static char posix_fdatasync__doc__[] =
706"fdatasync(fildes) -> None\n\
707force write of file with filedescriptor to disk.\n\
708 does not force update of metadata.";
709
710static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000711posix_fdatasync(PyObject *self, PyObject *args)
Guido van Rossum21142a01999-01-08 21:05:37 +0000712{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000713 return posix_int(args, "i:fdatasync", fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000714}
715#endif /* HAVE_FDATASYNC */
716
717
Fredrik Lundh10723342000-07-10 16:38:09 +0000718#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000719static char posix_chown__doc__[] =
720"chown(path, uid, gid) -> None\n\
721Change the owner and group id of path to the numeric uid and gid.";
722
Barry Warsaw53699e91996-12-10 23:23:01 +0000723static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000724posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000725{
Fredrik Lundh44328e62000-07-10 15:59:30 +0000726 char *path;
727 int uid, gid;
728 int res;
729 if (!PyArg_ParseTuple(args, "sii:chown", &path, &uid, &gid))
730 return NULL;
731 Py_BEGIN_ALLOW_THREADS
732 res = chown(path, (uid_t) uid, (gid_t) gid);
733 Py_END_ALLOW_THREADS
734 if (res < 0)
735 return posix_error_with_filename(path);
736 Py_INCREF(Py_None);
737 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000738}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000739#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000740
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000741
Guido van Rossum36bc6801995-06-14 22:54:23 +0000742#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000743static char posix_getcwd__doc__[] =
744"getcwd() -> path\n\
745Return a string representing the current working directory.";
746
Barry Warsaw53699e91996-12-10 23:23:01 +0000747static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000748posix_getcwd(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000749{
750 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000751 char *res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000752 if (!PyArg_ParseTuple(args, ":getcwd"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000754 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000755 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000756 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000757 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000759 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000760}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000761#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000763
Guido van Rossumb6775db1994-08-01 11:34:53 +0000764#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000765static char posix_link__doc__[] =
766"link(src, dst) -> None\n\
767Create a hard link to a file.";
768
Barry Warsaw53699e91996-12-10 23:23:01 +0000769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000770posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000772 return posix_2str(args, "ss:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000774#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000776
777static char posix_listdir__doc__[] =
778"listdir(path) -> list_of_strings\n\
779Return a list containing the names of the entries in the directory.\n\
780\n\
781 path: path of directory to list\n\
782\n\
783The list is in arbitrary order. It does not include the special\n\
784entries '.' and '..' even if they are present in the directory.";
785
Barry Warsaw53699e91996-12-10 23:23:01 +0000786static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000787posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000789 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000790 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000791#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000792
Guido van Rossumb6775db1994-08-01 11:34:53 +0000793 char *name;
794 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000795 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 HANDLE hFindFile;
797 WIN32_FIND_DATA FileData;
798 char namebuf[MAX_PATH+5];
799
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000800 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000801 return NULL;
802 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000803 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000804 return NULL;
805 }
806 strcpy(namebuf, name);
807 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
808 namebuf[len++] = '/';
809 strcpy(namebuf + len, "*.*");
810
Barry Warsaw53699e91996-12-10 23:23:01 +0000811 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812 return NULL;
813
814 hFindFile = FindFirstFile(namebuf, &FileData);
815 if (hFindFile == INVALID_HANDLE_VALUE) {
816 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000817 if (errno == ERROR_FILE_NOT_FOUND)
818 return PyList_New(0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000819 return win32_error("FindFirstFile", name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000820 }
821 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000822 if (FileData.cFileName[0] == '.' &&
823 (FileData.cFileName[1] == '\0' ||
824 FileData.cFileName[1] == '.' &&
825 FileData.cFileName[2] == '\0'))
826 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000827 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000828 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000829 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000830 d = NULL;
831 break;
832 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000833 if (PyList_Append(d, v) != 0) {
834 Py_DECREF(v);
835 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000836 d = NULL;
837 break;
838 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000839 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000840 } while (FindNextFile(hFindFile, &FileData) == TRUE);
841
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000842 if (FindClose(hFindFile) == FALSE)
843 return win32_error("FindClose", name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844
845 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000846
Guido van Rossum8d665e61996-06-26 18:22:49 +0000847#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000848#ifdef _MSC_VER /* 16-bit Windows */
849
850#ifndef MAX_PATH
851#define MAX_PATH 250
852#endif
853 char *name, *pt;
854 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000855 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000856 char namebuf[MAX_PATH+5];
857 struct _find_t ep;
858
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000859 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000860 return NULL;
861 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000862 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000863 return NULL;
864 }
865 strcpy(namebuf, name);
866 for (pt = namebuf; *pt; pt++)
867 if (*pt == '/')
868 *pt = '\\';
869 if (namebuf[len-1] != '\\')
870 namebuf[len++] = '\\';
871 strcpy(namebuf + len, "*.*");
872
Barry Warsaw53699e91996-12-10 23:23:01 +0000873 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000874 return NULL;
875
876 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000877 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
878 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000879 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000880 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000881 }
882 do {
883 if (ep.name[0] == '.' &&
884 (ep.name[1] == '\0' ||
885 ep.name[1] == '.' &&
886 ep.name[2] == '\0'))
887 continue;
888 strcpy(namebuf, ep.name);
889 for (pt = namebuf; *pt; pt++)
890 if (isupper(*pt))
891 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000892 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000893 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000894 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000895 d = NULL;
896 break;
897 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000898 if (PyList_Append(d, v) != 0) {
899 Py_DECREF(v);
900 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000901 d = NULL;
902 break;
903 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000904 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000905 } while (_dos_findnext(&ep) == 0);
906
907 return d;
908
909#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000910#if defined(PYOS_OS2)
911
912#ifndef MAX_PATH
913#define MAX_PATH CCHMAXPATH
914#endif
915 char *name, *pt;
916 int len;
917 PyObject *d, *v;
918 char namebuf[MAX_PATH+5];
919 HDIR hdir = 1;
920 ULONG srchcnt = 1;
921 FILEFINDBUF3 ep;
922 APIRET rc;
923
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000924 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000925 return NULL;
926 if (len >= MAX_PATH) {
927 PyErr_SetString(PyExc_ValueError, "path too long");
928 return NULL;
929 }
930 strcpy(namebuf, name);
931 for (pt = namebuf; *pt; pt++)
932 if (*pt == '/')
933 *pt = '\\';
934 if (namebuf[len-1] != '\\')
935 namebuf[len++] = '\\';
936 strcpy(namebuf + len, "*.*");
937
938 if ((d = PyList_New(0)) == NULL)
939 return NULL;
940
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000941 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
942 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000943 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000944 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
945 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
946 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000947
948 if (rc != NO_ERROR) {
949 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000950 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000951 }
952
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000953 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000954 do {
955 if (ep.achName[0] == '.'
956 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000957 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000958
959 strcpy(namebuf, ep.achName);
960
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000961 /* Leave Case of Name Alone -- In Native Form */
962 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000963
964 v = PyString_FromString(namebuf);
965 if (v == NULL) {
966 Py_DECREF(d);
967 d = NULL;
968 break;
969 }
970 if (PyList_Append(d, v) != 0) {
971 Py_DECREF(v);
972 Py_DECREF(d);
973 d = NULL;
974 break;
975 }
976 Py_DECREF(v);
977 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
978 }
979
980 return d;
981#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000982
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000983 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000984 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000985 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000986 struct dirent *ep;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000987 if (!PyArg_ParseTuple(args, "s:listdir", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000988 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000989 if ((dirp = opendir(name)) == NULL) {
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000990 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000991 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000992 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000993 closedir(dirp);
994 return NULL;
995 }
996 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000997 if (ep->d_name[0] == '.' &&
998 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000999 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001000 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001001 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001002 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001003 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001004 d = NULL;
1005 break;
1006 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001007 if (PyList_Append(d, v) != 0) {
1008 Py_DECREF(v);
1009 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001010 d = NULL;
1011 break;
1012 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001013 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001014 }
1015 closedir(dirp);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001016
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001017 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001018
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001019#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001020#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +00001021#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001022}
1023
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001024static char posix_mkdir__doc__[] =
1025"mkdir(path [, mode=0777]) -> None\n\
1026Create a directory.";
1027
Barry Warsaw53699e91996-12-10 23:23:01 +00001028static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001029posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001030{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001031 int res;
1032 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001033 int mode = 0777;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001034 if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001035 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001036 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001037#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001038 res = mkdir(path);
1039#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001040 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001041#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001042 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001043 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001044 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001045 Py_INCREF(Py_None);
1046 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001047}
1048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001049
Guido van Rossumb6775db1994-08-01 11:34:53 +00001050#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001051static char posix_nice__doc__[] =
1052"nice(inc) -> new_priority\n\
1053Decrease the priority of process and return new priority.";
1054
Barry Warsaw53699e91996-12-10 23:23:01 +00001055static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001056posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001057{
1058 int increment, value;
1059
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001060 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001061 return NULL;
1062 value = nice(increment);
1063 if (value == -1)
1064 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001065 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001066}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001067#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001069
1070static char posix_rename__doc__[] =
1071"rename(old, new) -> None\n\
1072Rename a file or directory.";
1073
Barry Warsaw53699e91996-12-10 23:23:01 +00001074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001075posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001076{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001077 return posix_2str(args, "ss:rename", rename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001078}
1079
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001080
1081static char posix_rmdir__doc__[] =
1082"rmdir(path) -> None\n\
1083Remove a directory.";
1084
Barry Warsaw53699e91996-12-10 23:23:01 +00001085static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001086posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001087{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001088 return posix_1str(args, "s:rmdir", rmdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001089}
1090
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001091
1092static char posix_stat__doc__[] =
1093"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1094Perform a stat system call on the given path.";
1095
Barry Warsaw53699e91996-12-10 23:23:01 +00001096static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001097posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001098{
Fred Drake699f3522000-06-29 21:12:41 +00001099 return posix_do_stat(self, args, "s:stat", STAT);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001100}
1101
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001102
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001103#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001104static char posix_system__doc__[] =
1105"system(command) -> exit_status\n\
1106Execute the command (a string) in a subshell.";
1107
Barry Warsaw53699e91996-12-10 23:23:01 +00001108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001109posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001111 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001112 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001113 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001114 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001115 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001116 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001117 Py_END_ALLOW_THREADS
1118 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001119}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001120#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001121
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001122
1123static char posix_umask__doc__[] =
1124"umask(new_mask) -> old_mask\n\
1125Set the current numeric umask and return the previous umask.";
1126
Barry Warsaw53699e91996-12-10 23:23:01 +00001127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001128posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001129{
1130 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001131 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001132 return NULL;
1133 i = umask(i);
1134 if (i < 0)
1135 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001136 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001137}
1138
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001139
1140static char posix_unlink__doc__[] =
1141"unlink(path) -> None\n\
1142Remove a file (same as remove(path)).";
1143
1144static char posix_remove__doc__[] =
1145"remove(path) -> None\n\
1146Remove a file (same as unlink(path)).";
1147
Barry Warsaw53699e91996-12-10 23:23:01 +00001148static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001149posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001150{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001151 return posix_1str(args, "s:remove", unlink);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001152}
1153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001154
Guido van Rossumb6775db1994-08-01 11:34:53 +00001155#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001156static char posix_uname__doc__[] =
1157"uname() -> (sysname, nodename, release, version, machine)\n\
1158Return a tuple identifying the current operating system.";
1159
Barry Warsaw53699e91996-12-10 23:23:01 +00001160static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001161posix_uname(PyObject *self, PyObject *args)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001162{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001163 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001164 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001165 if (!PyArg_ParseTuple(args, ":uname"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001166 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001167 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001168 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001169 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001170 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001171 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001172 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001173 u.sysname,
1174 u.nodename,
1175 u.release,
1176 u.version,
1177 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001178}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001179#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001180
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001181
1182static char posix_utime__doc__[] =
1183"utime(path, (atime, utime)) -> None\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001184utime(path, None) -> None\n\
1185Set the access and modified time of the file to the given values. If the\n\
1186second form is used, set the access and modified times to the current time.";
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001187
Barry Warsaw53699e91996-12-10 23:23:01 +00001188static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001189posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001190{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001191 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001192 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001193 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001194 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001195
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001196/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001197#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001198 struct utimbuf buf;
1199#define ATIME buf.actime
1200#define MTIME buf.modtime
1201#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001202#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001203 time_t buf[2];
1204#define ATIME buf[0]
1205#define MTIME buf[1]
1206#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001207#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001208
Barry Warsaw3cef8562000-05-01 16:17:24 +00001209 if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001210 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001211 if (arg == Py_None) {
1212 /* optional time values not given */
1213 Py_BEGIN_ALLOW_THREADS
1214 res = utime(path, NULL);
1215 Py_END_ALLOW_THREADS
1216 }
1217 else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) {
1218 PyErr_SetString(PyExc_TypeError,
1219 "Second argument must be a 2-tuple of numbers.");
1220 return NULL;
1221 }
1222 else {
1223 ATIME = atime;
1224 MTIME = mtime;
1225 Py_BEGIN_ALLOW_THREADS
1226 res = utime(path, UTIME_ARG);
1227 Py_END_ALLOW_THREADS
1228 }
Guido van Rossumff4949e1992-08-05 19:58:53 +00001229 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001230 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001231 Py_INCREF(Py_None);
1232 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001233#undef UTIME_ARG
1234#undef ATIME
1235#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001236}
1237
Guido van Rossum85e3b011991-06-03 12:42:10 +00001238
Guido van Rossum3b066191991-06-04 19:40:25 +00001239/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001240
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001241static char posix__exit__doc__[] =
1242"_exit(status)\n\
1243Exit to the system with specified status, without normal exit processing.";
1244
Barry Warsaw53699e91996-12-10 23:23:01 +00001245static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001246posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001247{
1248 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001249 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001250 return NULL;
1251 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001252 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001253}
1254
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001255
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001256#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001257static char posix_execv__doc__[] =
1258"execv(path, args)\n\
1259Execute an executable path with arguments, replacing current process.\n\
1260\n\
1261 path: path of executable file\n\
1262 args: tuple or list of strings";
1263
Barry Warsaw53699e91996-12-10 23:23:01 +00001264static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001265posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001266{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001267 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001268 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001269 char **argvlist;
1270 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001271 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001272
Guido van Rossum89b33251993-10-22 14:26:06 +00001273 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001274 argv is a list or tuple of strings. */
1275
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001276 if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001277 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001278 if (PyList_Check(argv)) {
1279 argc = PyList_Size(argv);
1280 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001281 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001282 else if (PyTuple_Check(argv)) {
1283 argc = PyTuple_Size(argv);
1284 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001285 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001286 else {
Guido van Rossum50422b42000-04-26 20:34:28 +00001287 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1288 return NULL;
1289 }
1290
1291 if (argc == 0) {
1292 PyErr_SetString(PyExc_ValueError, "empty argument list");
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001293 return NULL;
1294 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001295
Barry Warsaw53699e91996-12-10 23:23:01 +00001296 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001297 if (argvlist == NULL)
1298 return NULL;
1299 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001300 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1301 PyMem_DEL(argvlist);
Guido van Rossum50422b42000-04-26 20:34:28 +00001302 PyErr_SetString(PyExc_TypeError,
1303 "all arguments must be strings");
1304 return NULL;
1305
Guido van Rossum85e3b011991-06-03 12:42:10 +00001306 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001307 }
1308 argvlist[argc] = NULL;
1309
Guido van Rossumb6775db1994-08-01 11:34:53 +00001310#ifdef BAD_EXEC_PROTOTYPES
1311 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001312#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001313 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001314#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001315
Guido van Rossum85e3b011991-06-03 12:42:10 +00001316 /* If we get here it's definitely an error */
1317
Barry Warsaw53699e91996-12-10 23:23:01 +00001318 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001319 return posix_error();
1320}
1321
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001322
1323static char posix_execve__doc__[] =
1324"execve(path, args, env)\n\
1325Execute a path with arguments and environment, replacing current process.\n\
1326\n\
1327 path: path of executable file\n\
1328 args: tuple or list of arguments\n\
Thomas Wouters7e474022000-07-16 12:04:32 +00001329 env: dictionary of strings mapping to strings";
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001330
Barry Warsaw53699e91996-12-10 23:23:01 +00001331static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001332posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001333{
1334 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001335 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001336 char **argvlist;
1337 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001338 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001339 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001340 PyObject *(*getitem)(PyObject *, int);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001341
1342 /* execve has three arguments: (path, argv, env), where
1343 argv is a list or tuple of strings and env is a dictionary
1344 like posix.environ. */
1345
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001346 if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001347 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001348 if (PyList_Check(argv)) {
1349 argc = PyList_Size(argv);
1350 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001351 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001352 else if (PyTuple_Check(argv)) {
1353 argc = PyTuple_Size(argv);
1354 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001355 }
1356 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001357 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001358 return NULL;
1359 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001360 if (!PyMapping_Check(env)) {
1361 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001362 return NULL;
1363 }
1364
Guido van Rossum50422b42000-04-26 20:34:28 +00001365 if (argc == 0) {
1366 PyErr_SetString(PyExc_ValueError,
1367 "empty argument list");
1368 return NULL;
1369 }
1370
Barry Warsaw53699e91996-12-10 23:23:01 +00001371 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001372 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001373 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001374 return NULL;
1375 }
1376 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001377 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001378 "s;argv must be list of strings",
1379 &argvlist[i]))
1380 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001381 goto fail_1;
1382 }
1383 }
1384 argvlist[argc] = NULL;
1385
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001386 i = PyMapping_Size(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001387 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001388 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001389 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001390 goto fail_1;
1391 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001392 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001393 keys = PyMapping_Keys(env);
1394 vals = PyMapping_Values(env);
1395 if (!keys || !vals)
1396 goto fail_2;
1397
1398 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001399 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001400
1401 key = PyList_GetItem(keys, pos);
1402 val = PyList_GetItem(vals, pos);
1403 if (!key || !val)
1404 goto fail_2;
1405
Barry Warsaw53699e91996-12-10 23:23:01 +00001406 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001407 !PyArg_Parse(val, "s;non-string value in env", &v))
1408 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001409 goto fail_2;
1410 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001411
1412#if defined(PYOS_OS2)
1413 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1414 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1415#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001416 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001417 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001418 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001419 goto fail_2;
1420 }
1421 sprintf(p, "%s=%s", k, v);
1422 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001423#if defined(PYOS_OS2)
1424 }
1425#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001426 }
1427 envlist[envc] = 0;
1428
Guido van Rossumb6775db1994-08-01 11:34:53 +00001429
1430#ifdef BAD_EXEC_PROTOTYPES
1431 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001432#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001433 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001434#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001435
1436 /* If we get here it's definitely an error */
1437
1438 (void) posix_error();
1439
1440 fail_2:
1441 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001442 PyMem_DEL(envlist[envc]);
1443 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001444 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001445 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001446 Py_XDECREF(vals);
1447 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001448 return NULL;
1449}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001450#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001451
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001452
Guido van Rossuma1065681999-01-25 23:20:23 +00001453#ifdef HAVE_SPAWNV
1454static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001455"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001456Execute an executable path with arguments, replacing current process.\n\
1457\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001458 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001459 path: path of executable file\n\
1460 args: tuple or list of strings";
1461
1462static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001463posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00001464{
1465 char *path;
1466 PyObject *argv;
1467 char **argvlist;
1468 int mode, i, argc;
Fred Drake699f3522000-06-29 21:12:41 +00001469 intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001470 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00001471
1472 /* spawnv has three arguments: (mode, path, argv), where
1473 argv is a list or tuple of strings. */
1474
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001475 if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00001476 return NULL;
1477 if (PyList_Check(argv)) {
1478 argc = PyList_Size(argv);
1479 getitem = PyList_GetItem;
1480 }
1481 else if (PyTuple_Check(argv)) {
1482 argc = PyTuple_Size(argv);
1483 getitem = PyTuple_GetItem;
1484 }
1485 else {
Fred Drake137507e2000-06-01 02:02:46 +00001486 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossuma1065681999-01-25 23:20:23 +00001487 return NULL;
1488 }
1489
1490 argvlist = PyMem_NEW(char *, argc+1);
1491 if (argvlist == NULL)
1492 return NULL;
1493 for (i = 0; i < argc; i++) {
1494 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1495 PyMem_DEL(argvlist);
Fred Drake137507e2000-06-01 02:02:46 +00001496 PyErr_SetString(PyExc_TypeError,
1497 "all arguments must be strings");
1498 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00001499 }
1500 }
1501 argvlist[argc] = NULL;
1502
Guido van Rossum246bc171999-02-01 23:54:31 +00001503 if (mode == _OLD_P_OVERLAY)
1504 mode = _P_OVERLAY;
Fred Drake699f3522000-06-29 21:12:41 +00001505 spawnval = _spawnv(mode, path, argvlist);
Guido van Rossuma1065681999-01-25 23:20:23 +00001506
1507 PyMem_DEL(argvlist);
1508
Fred Drake699f3522000-06-29 21:12:41 +00001509 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00001510 return posix_error();
1511 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001512#if SIZEOF_LONG == SIZEOF_VOID_P
1513 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001514#else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001515 return Py_BuildValue("L", (LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001516#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00001517}
1518
1519
1520static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001521"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001522Execute a path with arguments and environment, replacing current process.\n\
1523\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001524 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001525 path: path of executable file\n\
1526 args: tuple or list of arguments\n\
Thomas Wouters7e474022000-07-16 12:04:32 +00001527 env: dictionary of strings mapping to strings";
Guido van Rossuma1065681999-01-25 23:20:23 +00001528
1529static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001530posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00001531{
1532 char *path;
1533 PyObject *argv, *env;
1534 char **argvlist;
1535 char **envlist;
1536 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1537 int mode, i, pos, argc, envc;
Fred Drake699f3522000-06-29 21:12:41 +00001538 intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001539 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00001540
1541 /* spawnve has four arguments: (mode, path, argv, env), where
1542 argv is a list or tuple of strings and env is a dictionary
1543 like posix.environ. */
1544
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001545 if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00001546 return NULL;
1547 if (PyList_Check(argv)) {
1548 argc = PyList_Size(argv);
1549 getitem = PyList_GetItem;
1550 }
1551 else if (PyTuple_Check(argv)) {
1552 argc = PyTuple_Size(argv);
1553 getitem = PyTuple_GetItem;
1554 }
1555 else {
1556 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1557 return NULL;
1558 }
1559 if (!PyMapping_Check(env)) {
1560 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1561 return NULL;
1562 }
1563
1564 argvlist = PyMem_NEW(char *, argc+1);
1565 if (argvlist == NULL) {
1566 PyErr_NoMemory();
1567 return NULL;
1568 }
1569 for (i = 0; i < argc; i++) {
1570 if (!PyArg_Parse((*getitem)(argv, i),
1571 "s;argv must be list of strings",
1572 &argvlist[i]))
1573 {
1574 goto fail_1;
1575 }
1576 }
1577 argvlist[argc] = NULL;
1578
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001579 i = PyMapping_Size(env);
Guido van Rossuma1065681999-01-25 23:20:23 +00001580 envlist = PyMem_NEW(char *, i + 1);
1581 if (envlist == NULL) {
1582 PyErr_NoMemory();
1583 goto fail_1;
1584 }
1585 envc = 0;
1586 keys = PyMapping_Keys(env);
1587 vals = PyMapping_Values(env);
1588 if (!keys || !vals)
1589 goto fail_2;
1590
1591 for (pos = 0; pos < i; pos++) {
1592 char *p, *k, *v;
1593
1594 key = PyList_GetItem(keys, pos);
1595 val = PyList_GetItem(vals, pos);
1596 if (!key || !val)
1597 goto fail_2;
1598
1599 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1600 !PyArg_Parse(val, "s;non-string value in env", &v))
1601 {
1602 goto fail_2;
1603 }
1604 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1605 if (p == NULL) {
1606 PyErr_NoMemory();
1607 goto fail_2;
1608 }
1609 sprintf(p, "%s=%s", k, v);
1610 envlist[envc++] = p;
1611 }
1612 envlist[envc] = 0;
1613
Guido van Rossum246bc171999-02-01 23:54:31 +00001614 if (mode == _OLD_P_OVERLAY)
1615 mode = _P_OVERLAY;
Fred Drake699f3522000-06-29 21:12:41 +00001616 spawnval = _spawnve(mode, path, argvlist, envlist);
1617 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00001618 (void) posix_error();
1619 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001620#if SIZEOF_LONG == SIZEOF_VOID_P
1621 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001622#else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001623 res = Py_BuildValue("L", (LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001624#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00001625
1626 fail_2:
1627 while (--envc >= 0)
1628 PyMem_DEL(envlist[envc]);
1629 PyMem_DEL(envlist);
1630 fail_1:
1631 PyMem_DEL(argvlist);
1632 Py_XDECREF(vals);
1633 Py_XDECREF(keys);
1634 return res;
1635}
1636#endif /* HAVE_SPAWNV */
1637
1638
Guido van Rossumad0ee831995-03-01 10:34:45 +00001639#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001640static char posix_fork__doc__[] =
1641"fork() -> pid\n\
1642Fork a child process.\n\
1643\n\
1644Return 0 to child process and PID of child to parent process.";
1645
Barry Warsaw53699e91996-12-10 23:23:01 +00001646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001647posix_fork(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001648{
1649 int pid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001650 if (!PyArg_ParseTuple(args, ":fork"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001651 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001652 pid = fork();
1653 if (pid == -1)
1654 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00001655 if (pid == 0)
1656 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001657 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001658}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001659#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001660
Fred Drake8cef4cf2000-06-28 16:40:38 +00001661#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
1662#ifdef HAVE_PTY_H
1663#include <pty.h>
1664#else
1665#ifdef HAVE_LIBUTIL_H
1666#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00001667#endif /* HAVE_LIBUTIL_H */
1668#endif /* HAVE_PTY_H */
Thomas Wouters70c21a12000-07-14 14:28:33 +00001669#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00001670
Thomas Wouters70c21a12000-07-14 14:28:33 +00001671#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001672static char posix_openpty__doc__[] =
1673"openpty() -> (master_fd, slave_fd)\n\
1674Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
1675
1676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001677posix_openpty(PyObject *self, PyObject *args)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001678{
1679 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001680#ifndef HAVE_OPENPTY
1681 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001682#endif
1683
Fred Drake8cef4cf2000-06-28 16:40:38 +00001684 if (!PyArg_ParseTuple(args, ":openpty"))
1685 return NULL;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001686
1687#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00001688 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
1689 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00001690#else
1691 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
1692 if (slave_name == NULL)
1693 return posix_error();
1694
1695 slave_fd = open(slave_name, O_RDWR);
1696 if (slave_fd < 0)
1697 return posix_error();
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00001698#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00001699
Fred Drake8cef4cf2000-06-28 16:40:38 +00001700 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00001701
Fred Drake8cef4cf2000-06-28 16:40:38 +00001702}
Thomas Wouters70c21a12000-07-14 14:28:33 +00001703#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00001704
1705#ifdef HAVE_FORKPTY
1706static char posix_forkpty__doc__[] =
1707"forkpty() -> (pid, master_fd)\n\
1708Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
1709Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
1710To both, return fd of newly opened pseudo-terminal.\n";
1711
1712static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001713posix_forkpty(PyObject *self, PyObject *args)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001714{
1715 int master_fd, pid;
1716
1717 if (!PyArg_ParseTuple(args, ":forkpty"))
1718 return NULL;
1719 pid = forkpty(&master_fd, NULL, NULL, NULL);
1720 if (pid == -1)
1721 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00001722 if (pid == 0)
1723 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00001724 return Py_BuildValue("(ii)", pid, master_fd);
1725}
1726#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001727
Guido van Rossumad0ee831995-03-01 10:34:45 +00001728#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001729static char posix_getegid__doc__[] =
1730"getegid() -> egid\n\
1731Return the current process's effective group id.";
1732
Barry Warsaw53699e91996-12-10 23:23:01 +00001733static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001734posix_getegid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001735{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001736 if (!PyArg_ParseTuple(args, ":getegid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001737 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001739}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001740#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001741
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001742
Guido van Rossumad0ee831995-03-01 10:34:45 +00001743#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001744static char posix_geteuid__doc__[] =
1745"geteuid() -> euid\n\
1746Return the current process's effective user id.";
1747
Barry Warsaw53699e91996-12-10 23:23:01 +00001748static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001749posix_geteuid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001750{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001751 if (!PyArg_ParseTuple(args, ":geteuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001752 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001753 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001754}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001755#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001757
Guido van Rossumad0ee831995-03-01 10:34:45 +00001758#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001759static char posix_getgid__doc__[] =
1760"getgid() -> gid\n\
1761Return the current process's group id.";
1762
Barry Warsaw53699e91996-12-10 23:23:01 +00001763static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001764posix_getgid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001765{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001766 if (!PyArg_ParseTuple(args, ":getgid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001767 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001768 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001769}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001770#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001771
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001772
1773static char posix_getpid__doc__[] =
1774"getpid() -> pid\n\
1775Return the current process id";
1776
Barry Warsaw53699e91996-12-10 23:23:01 +00001777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001778posix_getpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001779{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001780 if (!PyArg_ParseTuple(args, ":getpid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001781 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001782 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001783}
1784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001785
Fred Drakec9680921999-12-13 16:37:25 +00001786#ifdef HAVE_GETGROUPS
1787static char posix_getgroups__doc__[] = "\
1788getgroups() -> list of group IDs\n\
1789Return list of supplemental group IDs for the process.";
1790
1791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001792posix_getgroups(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00001793{
1794 PyObject *result = NULL;
1795
1796 if (PyArg_ParseTuple(args, ":getgroups")) {
1797#ifdef NGROUPS_MAX
1798#define MAX_GROUPS NGROUPS_MAX
1799#else
1800 /* defined to be 16 on Solaris7, so this should be a small number */
1801#define MAX_GROUPS 64
1802#endif
1803 gid_t grouplist[MAX_GROUPS];
1804 int n;
1805
1806 n = getgroups(MAX_GROUPS, grouplist);
1807 if (n < 0)
1808 posix_error();
1809 else {
1810 result = PyList_New(n);
1811 if (result != NULL) {
1812 PyObject *o;
1813 int i;
1814 for (i = 0; i < n; ++i) {
1815 o = PyInt_FromLong((long)grouplist[i]);
1816 if (o == NULL) {
1817 Py_DECREF(result);
1818 result = NULL;
1819 break;
1820 }
1821 PyList_SET_ITEM(result, i, o);
1822 }
1823 }
1824 }
1825 }
1826 return result;
1827}
1828#endif
1829
Guido van Rossumb6775db1994-08-01 11:34:53 +00001830#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001831static char posix_getpgrp__doc__[] =
1832"getpgrp() -> pgrp\n\
1833Return the current process group id.";
1834
Barry Warsaw53699e91996-12-10 23:23:01 +00001835static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001836posix_getpgrp(PyObject *self, PyObject *args)
Guido van Rossum04814471991-06-04 20:23:49 +00001837{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001838 if (!PyArg_ParseTuple(args, ":getpgrp"))
Guido van Rossum04814471991-06-04 20:23:49 +00001839 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001840#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001841 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001842#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001843 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001844#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001845}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001846#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001847
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001848
Guido van Rossumb6775db1994-08-01 11:34:53 +00001849#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001850static char posix_setpgrp__doc__[] =
1851"setpgrp() -> None\n\
1852Make this process a session leader.";
1853
Barry Warsaw53699e91996-12-10 23:23:01 +00001854static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001855posix_setpgrp(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00001856{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001857 if (!PyArg_ParseTuple(args, ":setpgrp"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001858 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001859#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001860 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001861#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001862 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001863#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001864 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001865 Py_INCREF(Py_None);
1866 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001867}
1868
Guido van Rossumb6775db1994-08-01 11:34:53 +00001869#endif /* HAVE_SETPGRP */
1870
Guido van Rossumad0ee831995-03-01 10:34:45 +00001871#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001872static char posix_getppid__doc__[] =
1873"getppid() -> ppid\n\
1874Return the parent's process id.";
1875
Barry Warsaw53699e91996-12-10 23:23:01 +00001876static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001877posix_getppid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001878{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001879 if (!PyArg_ParseTuple(args, ":getppid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001880 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001881 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001882}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001883#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001884
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001885
Fred Drake12c6e2d1999-12-14 21:25:03 +00001886#ifdef HAVE_GETLOGIN
1887static char posix_getlogin__doc__[] = "\
1888getlogin() -> string\n\
1889Return the actual login name.";
1890
1891static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001892posix_getlogin(PyObject *self, PyObject *args)
Fred Drake12c6e2d1999-12-14 21:25:03 +00001893{
1894 PyObject *result = NULL;
1895
1896 if (PyArg_ParseTuple(args, ":getlogin")) {
1897 char *name = getlogin();
1898
1899 if (name == NULL)
1900 posix_error();
1901 else
1902 result = PyString_FromString(name);
1903 }
1904 return result;
1905}
1906#endif
1907
Guido van Rossumad0ee831995-03-01 10:34:45 +00001908#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001909static char posix_getuid__doc__[] =
1910"getuid() -> uid\n\
1911Return the current process's user id.";
1912
Barry Warsaw53699e91996-12-10 23:23:01 +00001913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001914posix_getuid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001915{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001916 if (!PyArg_ParseTuple(args, ":getuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001917 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001918 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001919}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001920#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001921
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001922
Guido van Rossumad0ee831995-03-01 10:34:45 +00001923#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001924static char posix_kill__doc__[] =
1925"kill(pid, sig) -> None\n\
1926Kill a process with a signal.";
1927
Barry Warsaw53699e91996-12-10 23:23:01 +00001928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001929posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001930{
1931 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001932 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001933 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001934#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001935 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1936 APIRET rc;
1937 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001938 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001939
1940 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1941 APIRET rc;
1942 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001943 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001944
1945 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001946 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001947#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001948 if (kill(pid, sig) == -1)
1949 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001950#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001951 Py_INCREF(Py_None);
1952 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001953}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001954#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001955
Guido van Rossumc0125471996-06-28 18:55:32 +00001956#ifdef HAVE_PLOCK
1957
1958#ifdef HAVE_SYS_LOCK_H
1959#include <sys/lock.h>
1960#endif
1961
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001962static char posix_plock__doc__[] =
1963"plock(op) -> None\n\
1964Lock program segments into memory.";
1965
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001967posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00001968{
1969 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001970 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001971 return NULL;
1972 if (plock(op) == -1)
1973 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001974 Py_INCREF(Py_None);
1975 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001976}
1977#endif
1978
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001980#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001981static char posix_popen__doc__[] =
1982"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1983Open a pipe to/from a command returning a file object.";
1984
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001985#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001986static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001987async_system(const char *command)
1988{
1989 char *p, errormsg[256], args[1024];
1990 RESULTCODES rcodes;
1991 APIRET rc;
1992 char *shell = getenv("COMSPEC");
1993 if (!shell)
1994 shell = "cmd";
1995
1996 strcpy(args, shell);
1997 p = &args[ strlen(args)+1 ];
1998 strcpy(p, "/c ");
1999 strcat(p, command);
2000 p += strlen(p) + 1;
2001 *p = '\0';
2002
2003 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002004 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002005 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002006 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002007 &rcodes, shell);
2008 return rc;
2009}
2010
Guido van Rossumd48f2521997-12-05 22:19:34 +00002011static FILE *
2012popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002013{
2014 HFILE rhan, whan;
2015 FILE *retfd = NULL;
2016 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2017
Guido van Rossumd48f2521997-12-05 22:19:34 +00002018 if (rc != NO_ERROR) {
2019 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002020 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002021 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002022
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002023 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2024 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002025
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002026 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2027 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002028
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002029 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
2030 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002031
2032 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002033 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002034 }
2035
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002036 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
2037 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002038
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002039 close(oldfd); /* And Close Saved STDOUT Handle */
2040 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002041
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002042 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
2043 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002044
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002045 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2046 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002047
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002048 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
2049 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002050
2051 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002052 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002053 }
2054
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002055 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
2056 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002057
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002058 close(oldfd); /* And Close Saved STDIN Handle */
2059 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002060
Guido van Rossumd48f2521997-12-05 22:19:34 +00002061 } else {
2062 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002063 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002064 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002065}
2066
2067static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002068posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002069{
2070 char *name;
2071 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00002072 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002073 FILE *fp;
2074 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002075 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002076 return NULL;
2077 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00002078 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002079 Py_END_ALLOW_THREADS
2080 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002081 return os2_error(err);
2082
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002083 f = PyFile_FromFile(fp, name, mode, fclose);
2084 if (f != NULL)
2085 PyFile_SetBufSize(f, bufsize);
2086 return f;
2087}
2088
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002089#elif defined(MS_WIN32)
2090
2091/*
2092 * Portable 'popen' replacement for Win32.
2093 *
2094 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
2095 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00002096 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002097 */
2098
2099#include <malloc.h>
2100#include <io.h>
2101#include <fcntl.h>
2102
2103/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
2104#define POPEN_1 1
2105#define POPEN_2 2
2106#define POPEN_3 3
2107#define POPEN_4 4
2108
2109static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002110static int _PyPclose(FILE *file);
2111
2112/*
2113 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00002114 * for use when retrieving the process exit code. See _PyPclose() below
2115 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002116 */
2117static PyObject *_PyPopenProcs = NULL;
2118
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002119
2120/* popen that works from a GUI.
2121 *
2122 * The result of this function is a pipe (file) connected to the
2123 * processes stdin or stdout, depending on the requested mode.
2124 */
2125
2126static PyObject *
2127posix_popen(PyObject *self, PyObject *args)
2128{
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002129 PyObject *f, *s;
2130 int tm = 0;
2131
2132 char *cmdstring;
2133 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002134 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002135 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002136 return NULL;
2137
2138 s = PyTuple_New(0);
2139
2140 if (*mode == 'r')
2141 tm = _O_RDONLY;
2142 else if (*mode != 'w') {
2143 PyErr_SetString(PyExc_ValueError, "mode must be 'r' or 'w'");
2144 return NULL;
2145 } else
2146 tm = _O_WRONLY;
2147
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002148 if (bufsize != -1) {
2149 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2150 return NULL;
2151 }
2152
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002153 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002154 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002155 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002156 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002157 else
2158 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
2159
2160 return f;
2161}
2162
2163/* Variation on win32pipe.popen
2164 *
2165 * The result of this function is a pipe (file) connected to the
2166 * process's stdin, and a pipe connected to the process's stdout.
2167 */
2168
2169static PyObject *
2170win32_popen2(PyObject *self, PyObject *args)
2171{
2172 PyObject *f;
2173 int tm=0;
2174
2175 char *cmdstring;
2176 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002177 int bufsize = -1;
2178 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002179 return NULL;
2180
2181 if (*mode == 't')
2182 tm = _O_TEXT;
2183 else if (*mode != 'b') {
2184 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
2185 return NULL;
2186 } else
2187 tm = _O_BINARY;
2188
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002189 if (bufsize != -1) {
2190 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2191 return NULL;
2192 }
2193
2194 f = _PyPopen(cmdstring, tm, POPEN_2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002195
2196 return f;
2197}
2198
2199/*
2200 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002201 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002202 * The result of this function is 3 pipes - the process's stdin,
2203 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002204 */
2205
2206static PyObject *
2207win32_popen3(PyObject *self, PyObject *args)
2208{
2209 PyObject *f;
2210 int tm = 0;
2211
2212 char *cmdstring;
2213 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002214 int bufsize = -1;
2215 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002216 return NULL;
2217
2218 if (*mode == 't')
2219 tm = _O_TEXT;
2220 else if (*mode != 'b') {
2221 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
2222 return NULL;
2223 } else
2224 tm = _O_BINARY;
2225
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002226 if (bufsize != -1) {
2227 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2228 return NULL;
2229 }
2230
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002231 f = _PyPopen(cmdstring, tm, POPEN_3);
2232
2233 return f;
2234}
2235
2236/*
2237 * Variation on win32pipe.popen
2238 *
2239 * The result of this function is 2 pipes - the processes stdin,
2240 * and stdout+stderr combined as a single pipe.
2241 */
2242
2243static PyObject *
2244win32_popen4(PyObject *self, PyObject *args)
2245{
2246 PyObject *f;
2247 int tm = 0;
2248
2249 char *cmdstring;
2250 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002251 int bufsize = -1;
2252 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002253 return NULL;
2254
2255 if (*mode == 't')
2256 tm = _O_TEXT;
2257 else if (*mode != 'b') {
2258 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
2259 return NULL;
2260 } else
2261 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002262
2263 if (bufsize != -1) {
2264 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2265 return NULL;
2266 }
2267
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002268 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002269
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002270 return f;
2271}
2272
2273static int
Mark Hammondb37a3732000-08-14 04:47:33 +00002274_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002275 HANDLE hStdin,
2276 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00002277 HANDLE hStderr,
2278 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002279{
2280 PROCESS_INFORMATION piProcInfo;
2281 STARTUPINFO siStartInfo;
2282 char *s1,*s2, *s3 = " /c ";
2283 const char *szConsoleSpawn = "w9xpopen.exe \"";
2284 int i;
2285 int x;
2286
2287 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
2288 s1 = (char *)_alloca(i);
2289 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
2290 return x;
2291 if (GetVersion() < 0x80000000) {
2292 /*
2293 * NT/2000
2294 */
2295 x = i + strlen(s3) + strlen(cmdstring) + 1;
2296 s2 = (char *)_alloca(x);
2297 ZeroMemory(s2, x);
2298 sprintf(s2, "%s%s%s", s1, s3, cmdstring);
2299 }
2300 else {
2301 /*
2302 * Oh gag, we're on Win9x. Use the workaround listed in
2303 * KB: Q150956
2304 */
2305 char modulepath[256];
2306 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
2307 for (i = x = 0; modulepath[i]; i++)
2308 if (modulepath[i] == '\\')
2309 x = i+1;
2310 modulepath[x] = '\0';
2311 x = i + strlen(s3) + strlen(cmdstring) + 1 +
2312 strlen(modulepath) +
2313 strlen(szConsoleSpawn) + 1;
2314 s2 = (char *)_alloca(x);
2315 ZeroMemory(s2, x);
2316 sprintf(
2317 s2,
2318 "%s%s%s%s%s\"",
2319 modulepath,
2320 szConsoleSpawn,
2321 s1,
2322 s3,
2323 cmdstring);
2324 }
2325 }
2326
2327 /* Could be an else here to try cmd.exe / command.com in the path
2328 Now we'll just error out.. */
2329 else
2330 return -1;
2331
2332 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
2333 siStartInfo.cb = sizeof(STARTUPINFO);
2334 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
2335 siStartInfo.hStdInput = hStdin;
2336 siStartInfo.hStdOutput = hStdout;
2337 siStartInfo.hStdError = hStderr;
2338 siStartInfo.wShowWindow = SW_HIDE;
2339
2340 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002341 s2,
2342 NULL,
2343 NULL,
2344 TRUE,
2345 CREATE_NEW_CONSOLE,
2346 NULL,
2347 NULL,
2348 &siStartInfo,
2349 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002350 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002351 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002352
Mark Hammondb37a3732000-08-14 04:47:33 +00002353 /* Return process handle */
2354 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002355 return TRUE;
2356 }
2357 return FALSE;
2358}
2359
2360/* The following code is based off of KB: Q190351 */
2361
2362static PyObject *
2363_PyPopen(char *cmdstring, int mode, int n)
2364{
2365 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
2366 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00002367 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002368
2369 SECURITY_ATTRIBUTES saAttr;
2370 BOOL fSuccess;
2371 int fd1, fd2, fd3;
2372 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00002373 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002374 PyObject *f;
2375
2376 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
2377 saAttr.bInheritHandle = TRUE;
2378 saAttr.lpSecurityDescriptor = NULL;
2379
2380 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
2381 return win32_error("CreatePipe", NULL);
2382
2383 /* Create new output read handle and the input write handle. Set
2384 * the inheritance properties to FALSE. Otherwise, the child inherits
2385 * the these handles; resulting in non-closeable handles to the pipes
2386 * being created. */
2387 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002388 GetCurrentProcess(), &hChildStdinWrDup, 0,
2389 FALSE,
2390 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002391 if (!fSuccess)
2392 return win32_error("DuplicateHandle", NULL);
2393
2394 /* Close the inheritable version of ChildStdin
2395 that we're using. */
2396 CloseHandle(hChildStdinWr);
2397
2398 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
2399 return win32_error("CreatePipe", NULL);
2400
2401 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002402 GetCurrentProcess(), &hChildStdoutRdDup, 0,
2403 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002404 if (!fSuccess)
2405 return win32_error("DuplicateHandle", NULL);
2406
2407 /* Close the inheritable version of ChildStdout
2408 that we're using. */
2409 CloseHandle(hChildStdoutRd);
2410
2411 if (n != POPEN_4) {
2412 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
2413 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002414 fSuccess = DuplicateHandle(GetCurrentProcess(),
2415 hChildStderrRd,
2416 GetCurrentProcess(),
2417 &hChildStderrRdDup, 0,
2418 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002419 if (!fSuccess)
2420 return win32_error("DuplicateHandle", NULL);
2421 /* Close the inheritable version of ChildStdErr that we're using. */
2422 CloseHandle(hChildStderrRd);
2423 }
2424
2425 switch (n) {
2426 case POPEN_1:
2427 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
2428 case _O_WRONLY | _O_TEXT:
2429 /* Case for writing to child Stdin in text mode. */
2430 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2431 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002432 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002433 PyFile_SetBufSize(f, 0);
2434 /* We don't care about these pipes anymore, so close them. */
2435 CloseHandle(hChildStdoutRdDup);
2436 CloseHandle(hChildStderrRdDup);
2437 break;
2438
2439 case _O_RDONLY | _O_TEXT:
2440 /* Case for reading from child Stdout in text mode. */
2441 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2442 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002443 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002444 PyFile_SetBufSize(f, 0);
2445 /* We don't care about these pipes anymore, so close them. */
2446 CloseHandle(hChildStdinWrDup);
2447 CloseHandle(hChildStderrRdDup);
2448 break;
2449
2450 case _O_RDONLY | _O_BINARY:
2451 /* Case for readinig from child Stdout in binary mode. */
2452 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2453 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002454 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002455 PyFile_SetBufSize(f, 0);
2456 /* We don't care about these pipes anymore, so close them. */
2457 CloseHandle(hChildStdinWrDup);
2458 CloseHandle(hChildStderrRdDup);
2459 break;
2460
2461 case _O_WRONLY | _O_BINARY:
2462 /* Case for writing to child Stdin in binary mode. */
2463 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2464 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002465 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002466 PyFile_SetBufSize(f, 0);
2467 /* We don't care about these pipes anymore, so close them. */
2468 CloseHandle(hChildStdoutRdDup);
2469 CloseHandle(hChildStderrRdDup);
2470 break;
2471 }
Mark Hammondb37a3732000-08-14 04:47:33 +00002472 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002473 break;
2474
2475 case POPEN_2:
2476 case POPEN_4:
2477 {
2478 char *m1, *m2;
2479 PyObject *p1, *p2;
2480
2481 if (mode && _O_TEXT) {
2482 m1 = "r";
2483 m2 = "w";
2484 } else {
2485 m1 = "rb";
2486 m2 = "wb";
2487 }
2488
2489 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2490 f1 = _fdopen(fd1, m2);
2491 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2492 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002493 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002494 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00002495 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002496 PyFile_SetBufSize(p2, 0);
2497
2498 if (n != 4)
2499 CloseHandle(hChildStderrRdDup);
2500
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002501 f = Py_BuildValue("OO",p1,p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00002502 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002503 break;
2504 }
2505
2506 case POPEN_3:
2507 {
2508 char *m1, *m2;
2509 PyObject *p1, *p2, *p3;
2510
2511 if (mode && _O_TEXT) {
2512 m1 = "r";
2513 m2 = "w";
2514 } else {
2515 m1 = "rb";
2516 m2 = "wb";
2517 }
2518
2519 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2520 f1 = _fdopen(fd1, m2);
2521 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2522 f2 = _fdopen(fd2, m1);
2523 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
2524 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002525 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00002526 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
2527 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002528 PyFile_SetBufSize(p1, 0);
2529 PyFile_SetBufSize(p2, 0);
2530 PyFile_SetBufSize(p3, 0);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002531 f = Py_BuildValue("OOO",p1,p2,p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00002532 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002533 break;
2534 }
2535 }
2536
2537 if (n == POPEN_4) {
2538 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002539 hChildStdinRd,
2540 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00002541 hChildStdoutWr,
2542 &hProcess))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002543 return win32_error("CreateProcess", NULL);
2544 }
2545 else {
2546 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002547 hChildStdinRd,
2548 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00002549 hChildStderrWr,
2550 &hProcess))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002551 return win32_error("CreateProcess", NULL);
2552 }
2553
Mark Hammondb37a3732000-08-14 04:47:33 +00002554 /*
2555 * Insert the files we've created into the process dictionary
2556 * all referencing the list with the process handle and the
2557 * initial number of files (see description below in _PyPclose).
2558 * Since if _PyPclose later tried to wait on a process when all
2559 * handles weren't closed, it could create a deadlock with the
2560 * child, we spend some energy here to try to ensure that we
2561 * either insert all file handles into the dictionary or none
2562 * at all. It's a little clumsy with the various popen modes
2563 * and variable number of files involved.
2564 */
2565 if (!_PyPopenProcs) {
2566 _PyPopenProcs = PyDict_New();
2567 }
2568
2569 if (_PyPopenProcs) {
2570 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
2571 int ins_rc[3];
2572
2573 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
2574 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
2575
2576 procObj = PyList_New(2);
2577 hProcessObj = PyLong_FromVoidPtr(hProcess);
2578 intObj = PyInt_FromLong(file_count);
2579
2580 if (procObj && hProcessObj && intObj) {
2581 PyList_SetItem(procObj,0,hProcessObj);
2582 PyList_SetItem(procObj,1,intObj);
2583
2584 fileObj[0] = PyLong_FromVoidPtr(f1);
2585 if (fileObj[0]) {
2586 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
2587 fileObj[0],
2588 procObj);
2589 }
2590 if (file_count >= 2) {
2591 fileObj[1] = PyLong_FromVoidPtr(f2);
2592 if (fileObj[1]) {
2593 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
2594 fileObj[1],
2595 procObj);
2596 }
2597 }
2598 if (file_count >= 3) {
2599 fileObj[2] = PyLong_FromVoidPtr(f3);
2600 if (fileObj[2]) {
2601 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
2602 fileObj[2],
2603 procObj);
2604 }
2605 }
2606
2607 if (ins_rc[0] < 0 || !fileObj[0] ||
2608 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
2609 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
2610 /* Something failed - remove any dictionary
2611 * entries that did make it.
2612 */
2613 if (!ins_rc[0] && fileObj[0]) {
2614 PyDict_DelItem(_PyPopenProcs,
2615 fileObj[0]);
2616 }
2617 if (!ins_rc[1] && fileObj[1]) {
2618 PyDict_DelItem(_PyPopenProcs,
2619 fileObj[1]);
2620 }
2621 if (!ins_rc[2] && fileObj[2]) {
2622 PyDict_DelItem(_PyPopenProcs,
2623 fileObj[2]);
2624 }
2625 }
2626 }
2627
2628 /*
2629 * Clean up our localized references for the dictionary keys
2630 * and value since PyDict_SetItem will Py_INCREF any copies
2631 * that got placed in the dictionary.
2632 */
2633 Py_XDECREF(procObj);
2634 Py_XDECREF(fileObj[0]);
2635 Py_XDECREF(fileObj[1]);
2636 Py_XDECREF(fileObj[2]);
2637 }
2638
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002639 /* Child is launched. Close the parents copy of those pipe
2640 * handles that only the child should have open. You need to
2641 * make sure that no handles to the write end of the output pipe
2642 * are maintained in this process or else the pipe will not close
2643 * when the child process exits and the ReadFile will hang. */
2644
2645 if (!CloseHandle(hChildStdinRd))
2646 return win32_error("CloseHandle", NULL);
2647
2648 if (!CloseHandle(hChildStdoutWr))
2649 return win32_error("CloseHandle", NULL);
2650
2651 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
2652 return win32_error("CloseHandle", NULL);
2653
2654 return f;
2655}
Fredrik Lundh56055a42000-07-23 19:47:12 +00002656
2657/*
2658 * Wrapper for fclose() to use for popen* files, so we can retrieve the
2659 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00002660 *
2661 * This function uses the _PyPopenProcs dictionary in order to map the
2662 * input file pointer to information about the process that was
2663 * originally created by the popen* call that created the file pointer.
2664 * The dictionary uses the file pointer as a key (with one entry
2665 * inserted for each file returned by the original popen* call) and a
2666 * single list object as the value for all files from a single call.
2667 * The list object contains the Win32 process handle at [0], and a file
2668 * count at [1], which is initialized to the total number of file
2669 * handles using that list.
2670 *
2671 * This function closes whichever handle it is passed, and decrements
2672 * the file count in the dictionary for the process handle pointed to
2673 * by this file. On the last close (when the file count reaches zero),
2674 * this function will wait for the child process and then return its
2675 * exit code as the result of the close() operation. This permits the
2676 * files to be closed in any order - it is always the close() of the
2677 * final handle that will return the exit code.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002678 */
Tim Peters736aa322000-09-01 06:51:24 +00002679
2680 /* RED_FLAG 31-Aug-2000 Tim
2681 * This is always called (today!) between a pair of
2682 * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS
2683 * macros. So the thread running this has no valid thread state, as
2684 * far as Python is concerned. However, this calls some Python API
2685 * functions that cannot be called safely without a valid thread
2686 * state, in particular PyDict_GetItem.
2687 * As a temporary hack (although it may last for years ...), we
2688 * *rely* on not having a valid thread state in this function, in
2689 * order to create our own "from scratch".
2690 * This will deadlock if _PyPclose is ever called by a thread
2691 * holding the global lock.
2692 */
2693
Fredrik Lundh56055a42000-07-23 19:47:12 +00002694static int _PyPclose(FILE *file)
2695{
Fredrik Lundh20318932000-07-26 17:29:12 +00002696 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002697 DWORD exit_code;
2698 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00002699 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
2700 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00002701#ifdef WITH_THREAD
2702 PyInterpreterState* pInterpreterState;
2703 PyThreadState* pThreadState;
2704#endif
2705
Fredrik Lundh20318932000-07-26 17:29:12 +00002706 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00002707 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00002708 */
2709 result = fclose(file);
2710
Tim Peters736aa322000-09-01 06:51:24 +00002711#ifdef WITH_THREAD
2712 /* Bootstrap a valid thread state into existence. */
2713 pInterpreterState = PyInterpreterState_New();
2714 if (!pInterpreterState) {
2715 /* Well, we're hosed now! We don't have a thread
2716 * state, so can't call a nice error routine, or raise
2717 * an exception. Just die.
2718 */
2719 Py_FatalError("unable to allocate interpreter state "
Tim Peters9acdd3a2000-09-01 19:26:36 +00002720 "when closing popen object.");
Tim Peters736aa322000-09-01 06:51:24 +00002721 return -1; /* unreachable */
2722 }
2723 pThreadState = PyThreadState_New(pInterpreterState);
2724 if (!pThreadState) {
2725 Py_FatalError("unable to allocate thread state "
Tim Peters9acdd3a2000-09-01 19:26:36 +00002726 "when closing popen object.");
Tim Peters736aa322000-09-01 06:51:24 +00002727 return -1; /* unreachable */
2728 }
2729 /* Grab the global lock. Note that this will deadlock if the
2730 * current thread already has the lock! (see RED_FLAG comments
2731 * before this function)
2732 */
2733 PyEval_RestoreThread(pThreadState);
2734#endif
2735
Fredrik Lundh56055a42000-07-23 19:47:12 +00002736 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00002737 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
2738 (procObj = PyDict_GetItem(_PyPopenProcs,
2739 fileObj)) != NULL &&
2740 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
2741 (intObj = PyList_GetItem(procObj,1)) != NULL) {
2742
2743 hProcess = PyLong_AsVoidPtr(hProcessObj);
2744 file_count = PyInt_AsLong(intObj);
2745
2746 if (file_count > 1) {
2747 /* Still other files referencing process */
2748 file_count--;
2749 PyList_SetItem(procObj,1,
2750 PyInt_FromLong(file_count));
2751 } else {
2752 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00002753 if (result != EOF &&
2754 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
2755 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00002756 /* Possible truncation here in 16-bit environments, but
2757 * real exit codes are just the lower byte in any event.
2758 */
2759 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002760 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00002761 /* Indicate failure - this will cause the file object
2762 * to raise an I/O error and translate the last Win32
2763 * error code from errno. We do have a problem with
2764 * last errors that overlap the normal errno table,
2765 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002766 */
Fredrik Lundh20318932000-07-26 17:29:12 +00002767 if (result != EOF) {
2768 /* If the error wasn't from the fclose(), then
2769 * set errno for the file object error handling.
2770 */
2771 errno = GetLastError();
2772 }
2773 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002774 }
2775
2776 /* Free up the native handle at this point */
2777 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00002778 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00002779
Mark Hammondb37a3732000-08-14 04:47:33 +00002780 /* Remove this file pointer from dictionary */
2781 PyDict_DelItem(_PyPopenProcs, fileObj);
2782
2783 if (PyDict_Size(_PyPopenProcs) == 0) {
2784 Py_DECREF(_PyPopenProcs);
2785 _PyPopenProcs = NULL;
2786 }
2787
2788 } /* if object retrieval ok */
2789
2790 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002791 } /* if _PyPopenProcs */
2792
Tim Peters736aa322000-09-01 06:51:24 +00002793#ifdef WITH_THREAD
2794 /* Tear down the thread & interpreter states.
2795 * Note that interpreter state clear & delete functions automatically
Tim Peters9acdd3a2000-09-01 19:26:36 +00002796 * call the thread clear & delete functions, and indeed insist on
2797 * doing that themselves. The lock must be held during the clear, but
2798 * need not be held during the delete.
Tim Peters736aa322000-09-01 06:51:24 +00002799 */
2800 PyInterpreterState_Clear(pInterpreterState);
2801 PyEval_ReleaseThread(pThreadState);
2802 PyInterpreterState_Delete(pInterpreterState);
2803#endif
2804
Fredrik Lundh56055a42000-07-23 19:47:12 +00002805 return result;
2806}
Tim Peters9acdd3a2000-09-01 19:26:36 +00002807
2808#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00002809static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002810posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00002811{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002812 char *name;
2813 char *mode = "r";
2814 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00002815 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002816 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002817 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00002818 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002819 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002820 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002821 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00002822 if (fp == NULL)
2823 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002824 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002825 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002826 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002827 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00002828}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002829#endif
2830
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002831#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00002832
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002833
Guido van Rossumb6775db1994-08-01 11:34:53 +00002834#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002835static char posix_setuid__doc__[] =
2836"setuid(uid) -> None\n\
2837Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00002838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002839posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002840{
2841 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002842 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002843 return NULL;
2844 if (setuid(uid) < 0)
2845 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002846 Py_INCREF(Py_None);
2847 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002848}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002849#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002850
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002851
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00002852#ifdef HAVE_SETEUID
2853static char posix_seteuid__doc__[] =
2854"seteuid(uid) -> None\n\
2855Set the current process's effective user id.";
2856static PyObject *
2857posix_seteuid (PyObject *self, PyObject *args)
2858{
2859 int euid;
2860 if (!PyArg_ParseTuple(args, "i", &euid)) {
2861 return NULL;
2862 } else if (seteuid(euid) < 0) {
2863 return posix_error();
2864 } else {
2865 Py_INCREF(Py_None);
2866 return Py_None;
2867 }
2868}
2869#endif /* HAVE_SETEUID */
2870
2871#ifdef HAVE_SETEGID
2872static char posix_setegid__doc__[] =
2873"setegid(gid) -> None\n\
2874Set the current process's effective group id.";
2875static PyObject *
2876posix_setegid (PyObject *self, PyObject *args)
2877{
2878 int egid;
2879 if (!PyArg_ParseTuple(args, "i", &egid)) {
2880 return NULL;
2881 } else if (setegid(egid) < 0) {
2882 return posix_error();
2883 } else {
2884 Py_INCREF(Py_None);
2885 return Py_None;
2886 }
2887}
2888#endif /* HAVE_SETEGID */
2889
2890#ifdef HAVE_SETREUID
2891static char posix_setreuid__doc__[] =
2892"seteuid(ruid, euid) -> None\n\
2893Set the current process's real and effective user ids.";
2894static PyObject *
2895posix_setreuid (PyObject *self, PyObject *args)
2896{
2897 int ruid, euid;
2898 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
2899 return NULL;
2900 } else if (setreuid(ruid, euid) < 0) {
2901 return posix_error();
2902 } else {
2903 Py_INCREF(Py_None);
2904 return Py_None;
2905 }
2906}
2907#endif /* HAVE_SETREUID */
2908
2909#ifdef HAVE_SETREGID
2910static char posix_setregid__doc__[] =
2911"setegid(rgid, egid) -> None\n\
2912Set the current process's real and effective group ids.";
2913static PyObject *
2914posix_setregid (PyObject *self, PyObject *args)
2915{
2916 int rgid, egid;
2917 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
2918 return NULL;
2919 } else if (setregid(rgid, egid) < 0) {
2920 return posix_error();
2921 } else {
2922 Py_INCREF(Py_None);
2923 return Py_None;
2924 }
2925}
2926#endif /* HAVE_SETREGID */
2927
Guido van Rossumb6775db1994-08-01 11:34:53 +00002928#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002929static char posix_setgid__doc__[] =
2930"setgid(gid) -> None\n\
2931Set the current process's group id.";
2932
Barry Warsaw53699e91996-12-10 23:23:01 +00002933static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002934posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002935{
2936 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002937 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002938 return NULL;
2939 if (setgid(gid) < 0)
2940 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002941 Py_INCREF(Py_None);
2942 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002943}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002944#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002945
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002946
Guido van Rossumb6775db1994-08-01 11:34:53 +00002947#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948static char posix_waitpid__doc__[] =
2949"waitpid(pid, options) -> (pid, status)\n\
2950Wait for completion of a give child process.";
2951
Barry Warsaw53699e91996-12-10 23:23:01 +00002952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002953posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002954{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002955 int pid, options;
2956#ifdef UNION_WAIT
2957 union wait status;
2958#define status_i (status.w_status)
2959#else
2960 int status;
2961#define status_i status
2962#endif
2963 status_i = 0;
2964
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002965 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002966 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002967 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002968#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002969 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002970#else
2971 pid = waitpid(pid, &status, options);
2972#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002973 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002974 if (pid == -1)
2975 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002976 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002977 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002978}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002979#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002981
Guido van Rossumad0ee831995-03-01 10:34:45 +00002982#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002983static char posix_wait__doc__[] =
2984"wait() -> (pid, status)\n\
2985Wait for completion of a child process.";
2986
Barry Warsaw53699e91996-12-10 23:23:01 +00002987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002988posix_wait(PyObject *self, PyObject *args)
Guido van Rossum21803b81992-08-09 12:55:27 +00002989{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002990 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002991#ifdef UNION_WAIT
2992 union wait status;
2993#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002994#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002995 int status;
2996#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002997#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002998 if (!PyArg_ParseTuple(args, ":wait"))
2999 return NULL;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003000 status_i = 0;
3001 Py_BEGIN_ALLOW_THREADS
3002 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00003003 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00003004 if (pid == -1)
3005 return posix_error();
3006 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003007 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003008#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00003009}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003010#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003012
3013static char posix_lstat__doc__[] =
3014"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
3015Like stat(path), but do not follow symbolic links.";
3016
Barry Warsaw53699e91996-12-10 23:23:01 +00003017static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003018posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003019{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003020#ifdef HAVE_LSTAT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003021 return posix_do_stat(self, args, "s:lstat", lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003022#else /* !HAVE_LSTAT */
Fred Drake699f3522000-06-29 21:12:41 +00003023 return posix_do_stat(self, args, "s:lstat", STAT);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003024#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003025}
3026
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003027
Guido van Rossumb6775db1994-08-01 11:34:53 +00003028#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003029static char posix_readlink__doc__[] =
3030"readlink(path) -> path\n\
3031Return a string representing the path to which the symbolic link points.";
3032
Barry Warsaw53699e91996-12-10 23:23:01 +00003033static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003034posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003035{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003036 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003037 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003038 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003039 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003040 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003041 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00003042 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00003043 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003044 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00003045 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00003046 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003047}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003048#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003050
Guido van Rossumb6775db1994-08-01 11:34:53 +00003051#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003052static char posix_symlink__doc__[] =
3053"symlink(src, dst) -> None\n\
3054Create a symbolic link.";
3055
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003056static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003057posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003058{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003059 return posix_2str(args, "ss:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003060}
3061#endif /* HAVE_SYMLINK */
3062
3063
3064#ifdef HAVE_TIMES
3065#ifndef HZ
3066#define HZ 60 /* Universal constant :-) */
3067#endif /* HZ */
3068
Guido van Rossumd48f2521997-12-05 22:19:34 +00003069#if defined(PYCC_VACPP) && defined(PYOS_OS2)
3070static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00003071system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003072{
3073 ULONG value = 0;
3074
3075 Py_BEGIN_ALLOW_THREADS
3076 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
3077 Py_END_ALLOW_THREADS
3078
3079 return value;
3080}
3081
3082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003083posix_times(PyObject *self, PyObject *args)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003084{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003085 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossumd48f2521997-12-05 22:19:34 +00003086 return NULL;
3087
3088 /* Currently Only Uptime is Provided -- Others Later */
3089 return Py_BuildValue("ddddd",
3090 (double)0 /* t.tms_utime / HZ */,
3091 (double)0 /* t.tms_stime / HZ */,
3092 (double)0 /* t.tms_cutime / HZ */,
3093 (double)0 /* t.tms_cstime / HZ */,
3094 (double)system_uptime() / 1000);
3095}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003096#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003097static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003098posix_times(PyObject *self, PyObject *args)
Guido van Rossum22db57e1992-04-05 14:25:30 +00003099{
3100 struct tms t;
3101 clock_t c;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003102 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum22db57e1992-04-05 14:25:30 +00003103 return NULL;
3104 errno = 0;
3105 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00003106 if (c == (clock_t) -1)
3107 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003108 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00003109 (double)t.tms_utime / HZ,
3110 (double)t.tms_stime / HZ,
3111 (double)t.tms_cutime / HZ,
3112 (double)t.tms_cstime / HZ,
3113 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00003114}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003115#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003116#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003117
3118
Guido van Rossum87755a21996-09-07 00:59:43 +00003119#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003120#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00003121static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003122posix_times(PyObject *self, PyObject *args)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003123{
3124 FILETIME create, exit, kernel, user;
3125 HANDLE hProc;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003126 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003127 return NULL;
3128 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003129 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
3130 /* The fields of a FILETIME structure are the hi and lo part
3131 of a 64-bit value expressed in 100 nanosecond units.
3132 1e7 is one second in such units; 1e-7 the inverse.
3133 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
3134 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003135 return Py_BuildValue(
3136 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003137 (double)(kernel.dwHighDateTime*429.4967296 +
3138 kernel.dwLowDateTime*1e-7),
3139 (double)(user.dwHighDateTime*429.4967296 +
3140 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00003141 (double)0,
3142 (double)0,
3143 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003144}
Guido van Rossum8d665e61996-06-26 18:22:49 +00003145#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003146
3147#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00003148static char posix_times__doc__[] =
3149"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
3150Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003151#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003152
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003153
Guido van Rossumb6775db1994-08-01 11:34:53 +00003154#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003155static char posix_setsid__doc__[] =
3156"setsid() -> None\n\
3157Call the system call setsid().";
3158
Barry Warsaw53699e91996-12-10 23:23:01 +00003159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003160posix_setsid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003161{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003162 if (!PyArg_ParseTuple(args, ":setsid"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003163 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003164 if (setsid() < 0)
3165 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003166 Py_INCREF(Py_None);
3167 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003168}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003169#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003170
Guido van Rossumb6775db1994-08-01 11:34:53 +00003171#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003172static char posix_setpgid__doc__[] =
3173"setpgid(pid, pgrp) -> None\n\
3174Call the system call setpgid().";
3175
Barry Warsaw53699e91996-12-10 23:23:01 +00003176static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003177posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003178{
3179 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003180 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003181 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003182 if (setpgid(pid, pgrp) < 0)
3183 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003184 Py_INCREF(Py_None);
3185 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003186}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003187#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003188
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003189
Guido van Rossumb6775db1994-08-01 11:34:53 +00003190#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003191static char posix_tcgetpgrp__doc__[] =
3192"tcgetpgrp(fd) -> pgid\n\
3193Return the process group associated with the terminal given by a fd.";
3194
Barry Warsaw53699e91996-12-10 23:23:01 +00003195static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003196posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00003197{
3198 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003199 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003200 return NULL;
3201 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003202 if (pgid < 0)
3203 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003204 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00003205}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003206#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00003207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003208
Guido van Rossumb6775db1994-08-01 11:34:53 +00003209#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210static char posix_tcsetpgrp__doc__[] =
3211"tcsetpgrp(fd, pgid) -> None\n\
3212Set the process group associated with the terminal given by a fd.";
3213
Barry Warsaw53699e91996-12-10 23:23:01 +00003214static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003215posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00003216{
3217 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003218 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003219 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003220 if (tcsetpgrp(fd, pgid) < 0)
3221 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00003222 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00003223 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00003224}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003225#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00003226
Guido van Rossum687dd131993-05-17 08:34:16 +00003227/* Functions acting on file descriptors */
3228
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003229static char posix_open__doc__[] =
3230"open(filename, flag [, mode=0777]) -> fd\n\
3231Open a file (for low level IO).";
3232
Barry Warsaw53699e91996-12-10 23:23:01 +00003233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003234posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003235{
3236 char *file;
3237 int flag;
3238 int mode = 0777;
3239 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003240 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
3241 return NULL;
3242
Barry Warsaw53699e91996-12-10 23:23:01 +00003243 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003244 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003245 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003246 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00003247 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00003248 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003249}
3250
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003251
3252static char posix_close__doc__[] =
3253"close(fd) -> None\n\
3254Close a file descriptor (for low level IO).";
3255
Barry Warsaw53699e91996-12-10 23:23:01 +00003256static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003257posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003258{
3259 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003260 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003261 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003262 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003263 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003264 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003265 if (res < 0)
3266 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003267 Py_INCREF(Py_None);
3268 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003269}
3270
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003271
3272static char posix_dup__doc__[] =
3273"dup(fd) -> fd2\n\
3274Return a duplicate of a file descriptor.";
3275
Barry Warsaw53699e91996-12-10 23:23:01 +00003276static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003277posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003278{
3279 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003280 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003281 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003282 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003283 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003284 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003285 if (fd < 0)
3286 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003287 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003288}
3289
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003290
3291static char posix_dup2__doc__[] =
3292"dup2(fd, fd2) -> None\n\
3293Duplicate file descriptor.";
3294
Barry Warsaw53699e91996-12-10 23:23:01 +00003295static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003296posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003297{
3298 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003299 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00003300 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003301 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003302 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00003303 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003304 if (res < 0)
3305 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003306 Py_INCREF(Py_None);
3307 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003308}
3309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003310
3311static char posix_lseek__doc__[] =
3312"lseek(fd, pos, how) -> newpos\n\
3313Set the current position of a file descriptor.";
3314
Barry Warsaw53699e91996-12-10 23:23:01 +00003315static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003316posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003317{
3318 int fd, how;
Fred Drake699f3522000-06-29 21:12:41 +00003319#ifdef MS_WIN64
3320 LONG_LONG pos, res;
3321#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00003322 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00003323#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003324 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003325 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00003326 return NULL;
3327#ifdef SEEK_SET
3328 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
3329 switch (how) {
3330 case 0: how = SEEK_SET; break;
3331 case 1: how = SEEK_CUR; break;
3332 case 2: how = SEEK_END; break;
3333 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003334#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003335
3336#if !defined(HAVE_LARGEFILE_SUPPORT)
3337 pos = PyInt_AsLong(posobj);
3338#else
3339 pos = PyLong_Check(posobj) ?
3340 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
3341#endif
3342 if (PyErr_Occurred())
3343 return NULL;
3344
Barry Warsaw53699e91996-12-10 23:23:01 +00003345 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003346#ifdef MS_WIN64
3347 res = _lseeki64(fd, pos, how);
3348#else
Guido van Rossum687dd131993-05-17 08:34:16 +00003349 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00003350#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003351 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003352 if (res < 0)
3353 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00003354
3355#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00003356 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00003357#else
3358 return PyLong_FromLongLong(res);
3359#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003360}
3361
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003362
3363static char posix_read__doc__[] =
3364"read(fd, buffersize) -> string\n\
3365Read a file descriptor.";
3366
Barry Warsaw53699e91996-12-10 23:23:01 +00003367static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003368posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003369{
Guido van Rossum8bac5461996-06-11 18:38:48 +00003370 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00003371 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003372 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003373 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003374 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003375 if (buffer == NULL)
3376 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003377 Py_BEGIN_ALLOW_THREADS
3378 n = read(fd, PyString_AsString(buffer), size);
3379 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00003380 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003381 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00003382 return posix_error();
3383 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00003384 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00003385 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00003386 return buffer;
3387}
3388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003389
3390static char posix_write__doc__[] =
3391"write(fd, string) -> byteswritten\n\
3392Write a string to a file descriptor.";
3393
Barry Warsaw53699e91996-12-10 23:23:01 +00003394static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003395posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003396{
3397 int fd, size;
3398 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003399 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003400 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003401 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003402 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00003403 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003404 if (size < 0)
3405 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003406 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003407}
3408
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003409
3410static char posix_fstat__doc__[]=
3411"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
3412Like stat(), but for an open file descriptor.";
3413
Barry Warsaw53699e91996-12-10 23:23:01 +00003414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003415posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003416{
3417 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00003418 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00003419 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003420 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003421 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003422 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003423 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00003424 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003425 if (res != 0)
3426 return posix_error();
Fred Drake699f3522000-06-29 21:12:41 +00003427
3428 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00003429}
3430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003431
3432static char posix_fdopen__doc__[] =
3433"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
3434Return an open file object connected to a file descriptor.";
3435
Barry Warsaw53699e91996-12-10 23:23:01 +00003436static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003437posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003438{
Guido van Rossum687dd131993-05-17 08:34:16 +00003439 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003440 char *mode = "r";
3441 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00003442 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00003443 PyObject *f;
3444 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00003445 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003446
Barry Warsaw53699e91996-12-10 23:23:01 +00003447 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003448 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003449 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003450 if (fp == NULL)
3451 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003452 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003453 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00003454 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003455 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00003456}
3457
Skip Montanaro1517d842000-07-19 14:34:14 +00003458static char posix_isatty__doc__[] =
3459"isatty(fd) -> Boolean\n\
3460Return true if the file descriptor 'fd' is an open file descriptor\n\
3461connected to a terminal.";
3462
3463static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00003464posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00003465{
3466 int fd;
3467 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
3468 return NULL;
3469 return Py_BuildValue("i", isatty(fd));
3470}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003471
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003472#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003473static char posix_pipe__doc__[] =
3474"pipe() -> (read_end, write_end)\n\
3475Create a pipe.";
3476
Barry Warsaw53699e91996-12-10 23:23:01 +00003477static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003478posix_pipe(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003479{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003480#if defined(PYOS_OS2)
3481 HFILE read, write;
3482 APIRET rc;
3483
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003484 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003485 return NULL;
3486
3487 Py_BEGIN_ALLOW_THREADS
3488 rc = DosCreatePipe( &read, &write, 4096);
3489 Py_END_ALLOW_THREADS
3490 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003491 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003492
3493 return Py_BuildValue("(ii)", read, write);
3494#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00003495#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00003496 int fds[2];
3497 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003498 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum687dd131993-05-17 08:34:16 +00003499 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003500 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003501 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00003502 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003503 if (res != 0)
3504 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003505 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003506#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00003507 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003508 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00003509 BOOL ok;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003510 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum794d8131994-08-23 13:48:48 +00003511 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003512 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003513 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00003514 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00003515 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003516 return win32_error("CreatePipe", NULL);
Fred Drake699f3522000-06-29 21:12:41 +00003517 read_fd = _open_osfhandle((intptr_t)read, 0);
3518 write_fd = _open_osfhandle((intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003519 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003520#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003521#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003522}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003523#endif /* HAVE_PIPE */
3524
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003525
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003526#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003527static char posix_mkfifo__doc__[] =
3528"mkfifo(file, [, mode=0666]) -> None\n\
3529Create a FIFO (a POSIX named pipe).";
3530
Barry Warsaw53699e91996-12-10 23:23:01 +00003531static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003532posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003533{
3534 char *file;
3535 int mode = 0666;
3536 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003537 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003538 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003539 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003540 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003541 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003542 if (res < 0)
3543 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003544 Py_INCREF(Py_None);
3545 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003546}
3547#endif
3548
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003549
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003550#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003551static char posix_ftruncate__doc__[] =
3552"ftruncate(fd, length) -> None\n\
3553Truncate a file to a specified length.";
3554
Barry Warsaw53699e91996-12-10 23:23:01 +00003555static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003556posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003557{
3558 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003559 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003560 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003561 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003562
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003563 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003564 return NULL;
3565
3566#if !defined(HAVE_LARGEFILE_SUPPORT)
3567 length = PyInt_AsLong(lenobj);
3568#else
3569 length = PyLong_Check(lenobj) ?
3570 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
3571#endif
3572 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003573 return NULL;
3574
Barry Warsaw53699e91996-12-10 23:23:01 +00003575 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003576 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00003577 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003578 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003579 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003580 return NULL;
3581 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003582 Py_INCREF(Py_None);
3583 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003584}
3585#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003586
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003587#ifdef NeXT
3588#define HAVE_PUTENV
3589/* Steve Spicklemire got this putenv from NeXTAnswers */
3590static int
3591putenv(char *newval)
3592{
3593 extern char **environ;
3594
3595 static int firstTime = 1;
3596 char **ep;
3597 char *cp;
3598 int esiz;
3599 char *np;
3600
3601 if (!(np = strchr(newval, '=')))
3602 return 1;
3603 *np = '\0';
3604
3605 /* look it up */
3606 for (ep=environ ; *ep ; ep++)
3607 {
3608 /* this should always be true... */
3609 if (cp = strchr(*ep, '='))
3610 {
3611 *cp = '\0';
3612 if (!strcmp(*ep, newval))
3613 {
3614 /* got it! */
3615 *cp = '=';
3616 break;
3617 }
3618 *cp = '=';
3619 }
3620 else
3621 {
3622 *np = '=';
3623 return 1;
3624 }
3625 }
3626
3627 *np = '=';
3628 if (*ep)
3629 {
3630 /* the string was already there:
3631 just replace it with the new one */
3632 *ep = newval;
3633 return 0;
3634 }
3635
3636 /* expand environ by one */
3637 for (esiz=2, ep=environ ; *ep ; ep++)
3638 esiz++;
3639 if (firstTime)
3640 {
3641 char **epp;
3642 char **newenv;
3643 if (!(newenv = malloc(esiz * sizeof(char *))))
3644 return 1;
3645
3646 for (ep=environ, epp=newenv ; *ep ;)
3647 *epp++ = *ep++;
3648 *epp++ = newval;
3649 *epp = (char *) 0;
3650 environ = newenv;
3651 }
3652 else
3653 {
3654 if (!(environ = realloc(environ, esiz * sizeof(char *))))
3655 return 1;
3656 environ[esiz - 2] = newval;
3657 environ[esiz - 1] = (char *) 0;
3658 firstTime = 0;
3659 }
3660
3661 return 0;
3662}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00003663#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003664
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003665
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003666#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003667static char posix_putenv__doc__[] =
3668"putenv(key, value) -> None\n\
3669Change or add an environment variable.";
3670
Fred Drake762e2061999-08-26 17:23:54 +00003671/* Save putenv() parameters as values here, so we can collect them when they
3672 * get re-set with another call for the same key. */
3673static PyObject *posix_putenv_garbage;
3674
Barry Warsaw53699e91996-12-10 23:23:01 +00003675static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003676posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003677{
3678 char *s1, *s2;
3679 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00003680 PyObject *newstr;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003681
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003682 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003683 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003684
3685#if defined(PYOS_OS2)
3686 if (stricmp(s1, "BEGINLIBPATH") == 0) {
3687 APIRET rc;
3688
3689 if (strlen(s2) == 0) /* If New Value is an Empty String */
3690 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3691
3692 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
3693 if (rc != NO_ERROR)
3694 return os2_error(rc);
3695
3696 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
3697 APIRET rc;
3698
3699 if (strlen(s2) == 0) /* If New Value is an Empty String */
3700 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3701
3702 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
3703 if (rc != NO_ERROR)
3704 return os2_error(rc);
3705 } else {
3706#endif
3707
Fred Drake762e2061999-08-26 17:23:54 +00003708 /* XXX This can leak memory -- not easy to fix :-( */
3709 newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2);
3710 if (newstr == NULL)
3711 return PyErr_NoMemory();
3712 new = PyString_AS_STRING(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003713 (void) sprintf(new, "%s=%s", s1, s2);
3714 if (putenv(new)) {
3715 posix_error();
3716 return NULL;
3717 }
Fred Drake762e2061999-08-26 17:23:54 +00003718 /* Install the first arg and newstr in posix_putenv_garbage;
3719 * this will cause previous value to be collected. This has to
3720 * happen after the real putenv() call because the old value
3721 * was still accessible until then. */
3722 if (PyDict_SetItem(posix_putenv_garbage,
3723 PyTuple_GET_ITEM(args, 0), newstr)) {
3724 /* really not much we can do; just leak */
3725 PyErr_Clear();
3726 }
3727 else {
3728 Py_DECREF(newstr);
3729 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003730
3731#if defined(PYOS_OS2)
3732 }
3733#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003734 Py_INCREF(Py_None);
3735 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003736}
Guido van Rossumb6a47161997-09-15 22:54:34 +00003737#endif /* putenv */
3738
3739#ifdef HAVE_STRERROR
3740static char posix_strerror__doc__[] =
3741"strerror(code) -> string\n\
3742Translate an error code to a message string.";
3743
3744PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003745posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00003746{
3747 int code;
3748 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003749 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00003750 return NULL;
3751 message = strerror(code);
3752 if (message == NULL) {
3753 PyErr_SetString(PyExc_ValueError,
3754 "strerror code out of range");
3755 return NULL;
3756 }
3757 return PyString_FromString(message);
3758}
3759#endif /* strerror */
3760
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003761
Guido van Rossumc9641791998-08-04 15:26:23 +00003762#ifdef HAVE_SYS_WAIT_H
3763
3764#ifdef WIFSTOPPED
3765static char posix_WIFSTOPPED__doc__[] =
3766"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003767Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003768
3769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003770posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003771{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003772#ifdef UNION_WAIT
3773 union wait status;
3774#define status_i (status.w_status)
3775#else
3776 int status;
3777#define status_i status
3778#endif
3779 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003780
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003781 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003782 {
3783 return NULL;
3784 }
3785
3786 return Py_BuildValue("i", WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003787#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003788}
3789#endif /* WIFSTOPPED */
3790
3791#ifdef WIFSIGNALED
3792static char posix_WIFSIGNALED__doc__[] =
3793"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00003794Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003795
3796static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003797posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003798{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003799#ifdef UNION_WAIT
3800 union wait status;
3801#define status_i (status.w_status)
3802#else
3803 int status;
3804#define status_i status
3805#endif
3806 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003807
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003808 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003809 {
3810 return NULL;
3811 }
3812
3813 return Py_BuildValue("i", WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003814#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003815}
3816#endif /* WIFSIGNALED */
3817
3818#ifdef WIFEXITED
3819static char posix_WIFEXITED__doc__[] =
3820"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003821Return true if the process returning 'status' exited using the exit()\n\
3822system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003823
3824static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003825posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003826{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003827#ifdef UNION_WAIT
3828 union wait status;
3829#define status_i (status.w_status)
3830#else
3831 int status;
3832#define status_i status
3833#endif
3834 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003835
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003836 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003837 {
3838 return NULL;
3839 }
3840
3841 return Py_BuildValue("i", WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003842#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003843}
3844#endif /* WIFEXITED */
3845
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003846#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00003847static char posix_WEXITSTATUS__doc__[] =
3848"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003849Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003850
3851static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003852posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003853{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003854#ifdef UNION_WAIT
3855 union wait status;
3856#define status_i (status.w_status)
3857#else
3858 int status;
3859#define status_i status
3860#endif
3861 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003862
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003863 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003864 {
3865 return NULL;
3866 }
3867
3868 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003869#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003870}
3871#endif /* WEXITSTATUS */
3872
3873#ifdef WTERMSIG
3874static char posix_WTERMSIG__doc__[] =
3875"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003876Return the signal that terminated the process that provided the 'status'\n\
3877value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003878
3879static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003880posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003881{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003882#ifdef UNION_WAIT
3883 union wait status;
3884#define status_i (status.w_status)
3885#else
3886 int status;
3887#define status_i status
3888#endif
3889 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003890
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003891 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003892 {
3893 return NULL;
3894 }
3895
3896 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003897#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003898}
3899#endif /* WTERMSIG */
3900
3901#ifdef WSTOPSIG
3902static char posix_WSTOPSIG__doc__[] =
3903"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003904Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003905
3906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003907posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003908{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003909#ifdef UNION_WAIT
3910 union wait status;
3911#define status_i (status.w_status)
3912#else
3913 int status;
3914#define status_i status
3915#endif
3916 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003917
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003918 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003919 {
3920 return NULL;
3921 }
3922
3923 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003924#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003925}
3926#endif /* WSTOPSIG */
3927
3928#endif /* HAVE_SYS_WAIT_H */
3929
3930
Guido van Rossum94f6f721999-01-06 18:42:14 +00003931#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00003932#ifdef _SCO_DS
3933/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
3934 needed definitions in sys/statvfs.h */
3935#define _SVID3
3936#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003937#include <sys/statvfs.h>
3938
3939static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003940"fstatvfs(fd) -> \n\
3941 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003942Perform an fstatvfs system call on the given fd.";
3943
3944static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003945posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00003946{
3947 int fd, res;
3948 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003949 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003950 return NULL;
3951 Py_BEGIN_ALLOW_THREADS
3952 res = fstatvfs(fd, &st);
3953 Py_END_ALLOW_THREADS
3954 if (res != 0)
3955 return posix_error();
3956#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003957 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003958 (long) st.f_bsize,
3959 (long) st.f_frsize,
3960 (long) st.f_blocks,
3961 (long) st.f_bfree,
3962 (long) st.f_bavail,
3963 (long) st.f_files,
3964 (long) st.f_ffree,
3965 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003966 (long) st.f_flag,
3967 (long) st.f_namemax);
3968#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003969 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003970 (long) st.f_bsize,
3971 (long) st.f_frsize,
3972 (LONG_LONG) st.f_blocks,
3973 (LONG_LONG) st.f_bfree,
3974 (LONG_LONG) st.f_bavail,
3975 (LONG_LONG) st.f_files,
3976 (LONG_LONG) st.f_ffree,
3977 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003978 (long) st.f_flag,
3979 (long) st.f_namemax);
3980#endif
3981}
3982#endif /* HAVE_FSTATVFS */
3983
3984
3985#if defined(HAVE_STATVFS)
3986#include <sys/statvfs.h>
3987
3988static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003989"statvfs(path) -> \n\
3990 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003991Perform a statvfs system call on the given path.";
3992
3993static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003994posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00003995{
3996 char *path;
3997 int res;
3998 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003999 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004000 return NULL;
4001 Py_BEGIN_ALLOW_THREADS
4002 res = statvfs(path, &st);
4003 Py_END_ALLOW_THREADS
4004 if (res != 0)
4005 return posix_error_with_filename(path);
4006#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00004007 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00004008 (long) st.f_bsize,
4009 (long) st.f_frsize,
4010 (long) st.f_blocks,
4011 (long) st.f_bfree,
4012 (long) st.f_bavail,
4013 (long) st.f_files,
4014 (long) st.f_ffree,
4015 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00004016 (long) st.f_flag,
4017 (long) st.f_namemax);
4018#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00004019 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00004020 (long) st.f_bsize,
4021 (long) st.f_frsize,
4022 (LONG_LONG) st.f_blocks,
4023 (LONG_LONG) st.f_bfree,
4024 (LONG_LONG) st.f_bavail,
4025 (LONG_LONG) st.f_files,
4026 (LONG_LONG) st.f_ffree,
4027 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00004028 (long) st.f_flag,
4029 (long) st.f_namemax);
4030#endif
4031}
4032#endif /* HAVE_STATVFS */
4033
4034
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004035#ifdef HAVE_TEMPNAM
4036static char posix_tempnam__doc__[] = "\
4037tempnam([dir[, prefix]]) -> string\n\
4038Return a unique name for a temporary file.\n\
4039The directory and a short may be specified as strings; they may be omitted\n\
4040or None if not needed.";
4041
4042static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004043posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004044{
4045 PyObject *result = NULL;
4046 char *dir = NULL;
4047 char *pfx = NULL;
4048 char *name;
4049
4050 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
4051 return NULL;
4052 name = tempnam(dir, pfx);
4053 if (name == NULL)
4054 return PyErr_NoMemory();
4055 result = PyString_FromString(name);
4056 free(name);
4057 return result;
4058}
Guido van Rossumd371ff11999-01-25 16:12:23 +00004059#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004060
4061
4062#ifdef HAVE_TMPFILE
4063static char posix_tmpfile__doc__[] = "\
4064tmpfile() -> file object\n\
4065Create a temporary file with no directory entries.";
4066
4067static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004068posix_tmpfile(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004069{
4070 FILE *fp;
4071
4072 if (!PyArg_ParseTuple(args, ":tmpfile"))
4073 return NULL;
4074 fp = tmpfile();
4075 if (fp == NULL)
4076 return posix_error();
4077 return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose);
4078}
4079#endif
4080
4081
4082#ifdef HAVE_TMPNAM
4083static char posix_tmpnam__doc__[] = "\
4084tmpnam() -> string\n\
4085Return a unique name for a temporary file.";
4086
4087static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004088posix_tmpnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004089{
4090 char buffer[L_tmpnam];
4091 char *name;
4092
4093 if (!PyArg_ParseTuple(args, ":tmpnam"))
4094 return NULL;
Greg Wardb48bc172000-03-01 21:51:56 +00004095#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004096 name = tmpnam_r(buffer);
4097#else
4098 name = tmpnam(buffer);
4099#endif
4100 if (name == NULL) {
4101 PyErr_SetObject(PyExc_OSError,
4102 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00004103#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004104 "unexpected NULL from tmpnam_r"
4105#else
4106 "unexpected NULL from tmpnam"
4107#endif
4108 ));
4109 return NULL;
4110 }
4111 return PyString_FromString(buffer);
4112}
4113#endif
4114
4115
Fred Drakec9680921999-12-13 16:37:25 +00004116/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
4117 * It maps strings representing configuration variable names to
4118 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00004119 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00004120 * rarely-used constants. There are three separate tables that use
4121 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00004122 *
4123 * This code is always included, even if none of the interfaces that
4124 * need it are included. The #if hackery needed to avoid it would be
4125 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00004126 */
4127struct constdef {
4128 char *name;
4129 long value;
4130};
4131
Fred Drake12c6e2d1999-12-14 21:25:03 +00004132static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004133conv_confname(PyObject *arg, int *valuep, struct constdef *table,
4134 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004135{
4136 if (PyInt_Check(arg)) {
4137 *valuep = PyInt_AS_LONG(arg);
4138 return 1;
4139 }
4140 if (PyString_Check(arg)) {
4141 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00004142 size_t lo = 0;
4143 size_t mid;
4144 size_t hi = tablesize;
4145 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004146 char *confname = PyString_AS_STRING(arg);
4147 while (lo < hi) {
4148 mid = (lo + hi) / 2;
4149 cmp = strcmp(confname, table[mid].name);
4150 if (cmp < 0)
4151 hi = mid;
4152 else if (cmp > 0)
4153 lo = mid + 1;
4154 else {
4155 *valuep = table[mid].value;
4156 return 1;
4157 }
4158 }
4159 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
4160 }
4161 else
4162 PyErr_SetString(PyExc_TypeError,
4163 "configuration names must be strings or integers");
4164 return 0;
4165}
4166
4167
4168#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
4169static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004170#ifdef _PC_ABI_AIO_XFER_MAX
4171 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
4172#endif
4173#ifdef _PC_ABI_ASYNC_IO
4174 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
4175#endif
Fred Drakec9680921999-12-13 16:37:25 +00004176#ifdef _PC_ASYNC_IO
4177 {"PC_ASYNC_IO", _PC_ASYNC_IO},
4178#endif
4179#ifdef _PC_CHOWN_RESTRICTED
4180 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
4181#endif
4182#ifdef _PC_FILESIZEBITS
4183 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
4184#endif
4185#ifdef _PC_LAST
4186 {"PC_LAST", _PC_LAST},
4187#endif
4188#ifdef _PC_LINK_MAX
4189 {"PC_LINK_MAX", _PC_LINK_MAX},
4190#endif
4191#ifdef _PC_MAX_CANON
4192 {"PC_MAX_CANON", _PC_MAX_CANON},
4193#endif
4194#ifdef _PC_MAX_INPUT
4195 {"PC_MAX_INPUT", _PC_MAX_INPUT},
4196#endif
4197#ifdef _PC_NAME_MAX
4198 {"PC_NAME_MAX", _PC_NAME_MAX},
4199#endif
4200#ifdef _PC_NO_TRUNC
4201 {"PC_NO_TRUNC", _PC_NO_TRUNC},
4202#endif
4203#ifdef _PC_PATH_MAX
4204 {"PC_PATH_MAX", _PC_PATH_MAX},
4205#endif
4206#ifdef _PC_PIPE_BUF
4207 {"PC_PIPE_BUF", _PC_PIPE_BUF},
4208#endif
4209#ifdef _PC_PRIO_IO
4210 {"PC_PRIO_IO", _PC_PRIO_IO},
4211#endif
4212#ifdef _PC_SOCK_MAXBUF
4213 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
4214#endif
4215#ifdef _PC_SYNC_IO
4216 {"PC_SYNC_IO", _PC_SYNC_IO},
4217#endif
4218#ifdef _PC_VDISABLE
4219 {"PC_VDISABLE", _PC_VDISABLE},
4220#endif
4221};
4222
Fred Drakec9680921999-12-13 16:37:25 +00004223static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004224conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004225{
4226 return conv_confname(arg, valuep, posix_constants_pathconf,
4227 sizeof(posix_constants_pathconf)
4228 / sizeof(struct constdef));
4229}
4230#endif
4231
4232#ifdef HAVE_FPATHCONF
4233static char posix_fpathconf__doc__[] = "\
4234fpathconf(fd, name) -> integer\n\
4235Return the configuration limit name for the file descriptor fd.\n\
4236If there is no limit, return -1.";
4237
4238static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004239posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004240{
4241 PyObject *result = NULL;
4242 int name, fd;
4243
Fred Drake12c6e2d1999-12-14 21:25:03 +00004244 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
4245 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00004246 long limit;
4247
4248 errno = 0;
4249 limit = fpathconf(fd, name);
4250 if (limit == -1 && errno != 0)
4251 posix_error();
4252 else
4253 result = PyInt_FromLong(limit);
4254 }
4255 return result;
4256}
4257#endif
4258
4259
4260#ifdef HAVE_PATHCONF
4261static char posix_pathconf__doc__[] = "\
4262pathconf(path, name) -> integer\n\
4263Return the configuration limit name for the file or directory path.\n\
4264If there is no limit, return -1.";
4265
4266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004267posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004268{
4269 PyObject *result = NULL;
4270 int name;
4271 char *path;
4272
4273 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
4274 conv_path_confname, &name)) {
4275 long limit;
4276
4277 errno = 0;
4278 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004279 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00004280 if (errno == EINVAL)
4281 /* could be a path or name problem */
4282 posix_error();
4283 else
4284 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004285 }
Fred Drakec9680921999-12-13 16:37:25 +00004286 else
4287 result = PyInt_FromLong(limit);
4288 }
4289 return result;
4290}
4291#endif
4292
4293#ifdef HAVE_CONFSTR
4294static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004295#ifdef _CS_ARCHITECTURE
4296 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
4297#endif
4298#ifdef _CS_HOSTNAME
4299 {"CS_HOSTNAME", _CS_HOSTNAME},
4300#endif
4301#ifdef _CS_HW_PROVIDER
4302 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
4303#endif
4304#ifdef _CS_HW_SERIAL
4305 {"CS_HW_SERIAL", _CS_HW_SERIAL},
4306#endif
4307#ifdef _CS_INITTAB_NAME
4308 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
4309#endif
Fred Drakec9680921999-12-13 16:37:25 +00004310#ifdef _CS_LFS64_CFLAGS
4311 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
4312#endif
4313#ifdef _CS_LFS64_LDFLAGS
4314 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
4315#endif
4316#ifdef _CS_LFS64_LIBS
4317 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
4318#endif
4319#ifdef _CS_LFS64_LINTFLAGS
4320 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
4321#endif
4322#ifdef _CS_LFS_CFLAGS
4323 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
4324#endif
4325#ifdef _CS_LFS_LDFLAGS
4326 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
4327#endif
4328#ifdef _CS_LFS_LIBS
4329 {"CS_LFS_LIBS", _CS_LFS_LIBS},
4330#endif
4331#ifdef _CS_LFS_LINTFLAGS
4332 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
4333#endif
Fred Draked86ed291999-12-15 15:34:33 +00004334#ifdef _CS_MACHINE
4335 {"CS_MACHINE", _CS_MACHINE},
4336#endif
Fred Drakec9680921999-12-13 16:37:25 +00004337#ifdef _CS_PATH
4338 {"CS_PATH", _CS_PATH},
4339#endif
Fred Draked86ed291999-12-15 15:34:33 +00004340#ifdef _CS_RELEASE
4341 {"CS_RELEASE", _CS_RELEASE},
4342#endif
4343#ifdef _CS_SRPC_DOMAIN
4344 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
4345#endif
4346#ifdef _CS_SYSNAME
4347 {"CS_SYSNAME", _CS_SYSNAME},
4348#endif
4349#ifdef _CS_VERSION
4350 {"CS_VERSION", _CS_VERSION},
4351#endif
Fred Drakec9680921999-12-13 16:37:25 +00004352#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
4353 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
4354#endif
4355#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
4356 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
4357#endif
4358#ifdef _CS_XBS5_ILP32_OFF32_LIBS
4359 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
4360#endif
4361#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
4362 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
4363#endif
4364#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
4365 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
4366#endif
4367#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
4368 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
4369#endif
4370#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
4371 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
4372#endif
4373#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
4374 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
4375#endif
4376#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
4377 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
4378#endif
4379#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
4380 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
4381#endif
4382#ifdef _CS_XBS5_LP64_OFF64_LIBS
4383 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
4384#endif
4385#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
4386 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
4387#endif
4388#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
4389 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
4390#endif
4391#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
4392 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
4393#endif
4394#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
4395 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
4396#endif
4397#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
4398 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
4399#endif
Fred Draked86ed291999-12-15 15:34:33 +00004400#ifdef _MIPS_CS_AVAIL_PROCESSORS
4401 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
4402#endif
4403#ifdef _MIPS_CS_BASE
4404 {"MIPS_CS_BASE", _MIPS_CS_BASE},
4405#endif
4406#ifdef _MIPS_CS_HOSTID
4407 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
4408#endif
4409#ifdef _MIPS_CS_HW_NAME
4410 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
4411#endif
4412#ifdef _MIPS_CS_NUM_PROCESSORS
4413 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
4414#endif
4415#ifdef _MIPS_CS_OSREL_MAJ
4416 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
4417#endif
4418#ifdef _MIPS_CS_OSREL_MIN
4419 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
4420#endif
4421#ifdef _MIPS_CS_OSREL_PATCH
4422 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
4423#endif
4424#ifdef _MIPS_CS_OS_NAME
4425 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
4426#endif
4427#ifdef _MIPS_CS_OS_PROVIDER
4428 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
4429#endif
4430#ifdef _MIPS_CS_PROCESSORS
4431 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
4432#endif
4433#ifdef _MIPS_CS_SERIAL
4434 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
4435#endif
4436#ifdef _MIPS_CS_VENDOR
4437 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
4438#endif
Fred Drakec9680921999-12-13 16:37:25 +00004439};
4440
4441static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004442conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004443{
4444 return conv_confname(arg, valuep, posix_constants_confstr,
4445 sizeof(posix_constants_confstr)
4446 / sizeof(struct constdef));
4447}
4448
4449static char posix_confstr__doc__[] = "\
4450confstr(name) -> string\n\
4451Return a string-valued system configuration variable.";
4452
4453static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004454posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004455{
4456 PyObject *result = NULL;
4457 int name;
4458 char buffer[64];
4459
4460 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
4461 int len = confstr(name, buffer, sizeof(buffer));
4462
Fred Drakec9680921999-12-13 16:37:25 +00004463 errno = 0;
4464 if (len == 0) {
4465 if (errno != 0)
4466 posix_error();
4467 else
4468 result = PyString_FromString("");
4469 }
4470 else {
4471 if (len >= sizeof(buffer)) {
4472 result = PyString_FromStringAndSize(NULL, len);
4473 if (result != NULL)
4474 confstr(name, PyString_AS_STRING(result), len+1);
4475 }
4476 else
4477 result = PyString_FromString(buffer);
4478 }
4479 }
4480 return result;
4481}
4482#endif
4483
4484
4485#ifdef HAVE_SYSCONF
4486static struct constdef posix_constants_sysconf[] = {
4487#ifdef _SC_2_CHAR_TERM
4488 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
4489#endif
4490#ifdef _SC_2_C_BIND
4491 {"SC_2_C_BIND", _SC_2_C_BIND},
4492#endif
4493#ifdef _SC_2_C_DEV
4494 {"SC_2_C_DEV", _SC_2_C_DEV},
4495#endif
4496#ifdef _SC_2_C_VERSION
4497 {"SC_2_C_VERSION", _SC_2_C_VERSION},
4498#endif
4499#ifdef _SC_2_FORT_DEV
4500 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
4501#endif
4502#ifdef _SC_2_FORT_RUN
4503 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
4504#endif
4505#ifdef _SC_2_LOCALEDEF
4506 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
4507#endif
4508#ifdef _SC_2_SW_DEV
4509 {"SC_2_SW_DEV", _SC_2_SW_DEV},
4510#endif
4511#ifdef _SC_2_UPE
4512 {"SC_2_UPE", _SC_2_UPE},
4513#endif
4514#ifdef _SC_2_VERSION
4515 {"SC_2_VERSION", _SC_2_VERSION},
4516#endif
Fred Draked86ed291999-12-15 15:34:33 +00004517#ifdef _SC_ABI_ASYNCHRONOUS_IO
4518 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
4519#endif
4520#ifdef _SC_ACL
4521 {"SC_ACL", _SC_ACL},
4522#endif
Fred Drakec9680921999-12-13 16:37:25 +00004523#ifdef _SC_AIO_LISTIO_MAX
4524 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
4525#endif
Fred Drakec9680921999-12-13 16:37:25 +00004526#ifdef _SC_AIO_MAX
4527 {"SC_AIO_MAX", _SC_AIO_MAX},
4528#endif
4529#ifdef _SC_AIO_PRIO_DELTA_MAX
4530 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
4531#endif
4532#ifdef _SC_ARG_MAX
4533 {"SC_ARG_MAX", _SC_ARG_MAX},
4534#endif
4535#ifdef _SC_ASYNCHRONOUS_IO
4536 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
4537#endif
4538#ifdef _SC_ATEXIT_MAX
4539 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
4540#endif
Fred Draked86ed291999-12-15 15:34:33 +00004541#ifdef _SC_AUDIT
4542 {"SC_AUDIT", _SC_AUDIT},
4543#endif
Fred Drakec9680921999-12-13 16:37:25 +00004544#ifdef _SC_AVPHYS_PAGES
4545 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
4546#endif
4547#ifdef _SC_BC_BASE_MAX
4548 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
4549#endif
4550#ifdef _SC_BC_DIM_MAX
4551 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
4552#endif
4553#ifdef _SC_BC_SCALE_MAX
4554 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
4555#endif
4556#ifdef _SC_BC_STRING_MAX
4557 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
4558#endif
Fred Draked86ed291999-12-15 15:34:33 +00004559#ifdef _SC_CAP
4560 {"SC_CAP", _SC_CAP},
4561#endif
Fred Drakec9680921999-12-13 16:37:25 +00004562#ifdef _SC_CHARCLASS_NAME_MAX
4563 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
4564#endif
4565#ifdef _SC_CHAR_BIT
4566 {"SC_CHAR_BIT", _SC_CHAR_BIT},
4567#endif
4568#ifdef _SC_CHAR_MAX
4569 {"SC_CHAR_MAX", _SC_CHAR_MAX},
4570#endif
4571#ifdef _SC_CHAR_MIN
4572 {"SC_CHAR_MIN", _SC_CHAR_MIN},
4573#endif
4574#ifdef _SC_CHILD_MAX
4575 {"SC_CHILD_MAX", _SC_CHILD_MAX},
4576#endif
4577#ifdef _SC_CLK_TCK
4578 {"SC_CLK_TCK", _SC_CLK_TCK},
4579#endif
4580#ifdef _SC_COHER_BLKSZ
4581 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
4582#endif
4583#ifdef _SC_COLL_WEIGHTS_MAX
4584 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
4585#endif
4586#ifdef _SC_DCACHE_ASSOC
4587 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
4588#endif
4589#ifdef _SC_DCACHE_BLKSZ
4590 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
4591#endif
4592#ifdef _SC_DCACHE_LINESZ
4593 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
4594#endif
4595#ifdef _SC_DCACHE_SZ
4596 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
4597#endif
4598#ifdef _SC_DCACHE_TBLKSZ
4599 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
4600#endif
4601#ifdef _SC_DELAYTIMER_MAX
4602 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
4603#endif
4604#ifdef _SC_EQUIV_CLASS_MAX
4605 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
4606#endif
4607#ifdef _SC_EXPR_NEST_MAX
4608 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
4609#endif
4610#ifdef _SC_FSYNC
4611 {"SC_FSYNC", _SC_FSYNC},
4612#endif
4613#ifdef _SC_GETGR_R_SIZE_MAX
4614 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
4615#endif
4616#ifdef _SC_GETPW_R_SIZE_MAX
4617 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
4618#endif
4619#ifdef _SC_ICACHE_ASSOC
4620 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
4621#endif
4622#ifdef _SC_ICACHE_BLKSZ
4623 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
4624#endif
4625#ifdef _SC_ICACHE_LINESZ
4626 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
4627#endif
4628#ifdef _SC_ICACHE_SZ
4629 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
4630#endif
Fred Draked86ed291999-12-15 15:34:33 +00004631#ifdef _SC_INF
4632 {"SC_INF", _SC_INF},
4633#endif
Fred Drakec9680921999-12-13 16:37:25 +00004634#ifdef _SC_INT_MAX
4635 {"SC_INT_MAX", _SC_INT_MAX},
4636#endif
4637#ifdef _SC_INT_MIN
4638 {"SC_INT_MIN", _SC_INT_MIN},
4639#endif
4640#ifdef _SC_IOV_MAX
4641 {"SC_IOV_MAX", _SC_IOV_MAX},
4642#endif
Fred Draked86ed291999-12-15 15:34:33 +00004643#ifdef _SC_IP_SECOPTS
4644 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
4645#endif
Fred Drakec9680921999-12-13 16:37:25 +00004646#ifdef _SC_JOB_CONTROL
4647 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
4648#endif
Fred Draked86ed291999-12-15 15:34:33 +00004649#ifdef _SC_KERN_POINTERS
4650 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
4651#endif
4652#ifdef _SC_KERN_SIM
4653 {"SC_KERN_SIM", _SC_KERN_SIM},
4654#endif
Fred Drakec9680921999-12-13 16:37:25 +00004655#ifdef _SC_LINE_MAX
4656 {"SC_LINE_MAX", _SC_LINE_MAX},
4657#endif
4658#ifdef _SC_LOGIN_NAME_MAX
4659 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
4660#endif
4661#ifdef _SC_LOGNAME_MAX
4662 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
4663#endif
4664#ifdef _SC_LONG_BIT
4665 {"SC_LONG_BIT", _SC_LONG_BIT},
4666#endif
Fred Draked86ed291999-12-15 15:34:33 +00004667#ifdef _SC_MAC
4668 {"SC_MAC", _SC_MAC},
4669#endif
Fred Drakec9680921999-12-13 16:37:25 +00004670#ifdef _SC_MAPPED_FILES
4671 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
4672#endif
4673#ifdef _SC_MAXPID
4674 {"SC_MAXPID", _SC_MAXPID},
4675#endif
4676#ifdef _SC_MB_LEN_MAX
4677 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
4678#endif
4679#ifdef _SC_MEMLOCK
4680 {"SC_MEMLOCK", _SC_MEMLOCK},
4681#endif
4682#ifdef _SC_MEMLOCK_RANGE
4683 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
4684#endif
4685#ifdef _SC_MEMORY_PROTECTION
4686 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
4687#endif
4688#ifdef _SC_MESSAGE_PASSING
4689 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
4690#endif
Fred Draked86ed291999-12-15 15:34:33 +00004691#ifdef _SC_MMAP_FIXED_ALIGNMENT
4692 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
4693#endif
Fred Drakec9680921999-12-13 16:37:25 +00004694#ifdef _SC_MQ_OPEN_MAX
4695 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
4696#endif
4697#ifdef _SC_MQ_PRIO_MAX
4698 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
4699#endif
Fred Draked86ed291999-12-15 15:34:33 +00004700#ifdef _SC_NACLS_MAX
4701 {"SC_NACLS_MAX", _SC_NACLS_MAX},
4702#endif
Fred Drakec9680921999-12-13 16:37:25 +00004703#ifdef _SC_NGROUPS_MAX
4704 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
4705#endif
4706#ifdef _SC_NL_ARGMAX
4707 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
4708#endif
4709#ifdef _SC_NL_LANGMAX
4710 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
4711#endif
4712#ifdef _SC_NL_MSGMAX
4713 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
4714#endif
4715#ifdef _SC_NL_NMAX
4716 {"SC_NL_NMAX", _SC_NL_NMAX},
4717#endif
4718#ifdef _SC_NL_SETMAX
4719 {"SC_NL_SETMAX", _SC_NL_SETMAX},
4720#endif
4721#ifdef _SC_NL_TEXTMAX
4722 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
4723#endif
4724#ifdef _SC_NPROCESSORS_CONF
4725 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
4726#endif
4727#ifdef _SC_NPROCESSORS_ONLN
4728 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
4729#endif
Fred Draked86ed291999-12-15 15:34:33 +00004730#ifdef _SC_NPROC_CONF
4731 {"SC_NPROC_CONF", _SC_NPROC_CONF},
4732#endif
4733#ifdef _SC_NPROC_ONLN
4734 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
4735#endif
Fred Drakec9680921999-12-13 16:37:25 +00004736#ifdef _SC_NZERO
4737 {"SC_NZERO", _SC_NZERO},
4738#endif
4739#ifdef _SC_OPEN_MAX
4740 {"SC_OPEN_MAX", _SC_OPEN_MAX},
4741#endif
4742#ifdef _SC_PAGESIZE
4743 {"SC_PAGESIZE", _SC_PAGESIZE},
4744#endif
4745#ifdef _SC_PAGE_SIZE
4746 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
4747#endif
4748#ifdef _SC_PASS_MAX
4749 {"SC_PASS_MAX", _SC_PASS_MAX},
4750#endif
4751#ifdef _SC_PHYS_PAGES
4752 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
4753#endif
4754#ifdef _SC_PII
4755 {"SC_PII", _SC_PII},
4756#endif
4757#ifdef _SC_PII_INTERNET
4758 {"SC_PII_INTERNET", _SC_PII_INTERNET},
4759#endif
4760#ifdef _SC_PII_INTERNET_DGRAM
4761 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
4762#endif
4763#ifdef _SC_PII_INTERNET_STREAM
4764 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
4765#endif
4766#ifdef _SC_PII_OSI
4767 {"SC_PII_OSI", _SC_PII_OSI},
4768#endif
4769#ifdef _SC_PII_OSI_CLTS
4770 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
4771#endif
4772#ifdef _SC_PII_OSI_COTS
4773 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
4774#endif
4775#ifdef _SC_PII_OSI_M
4776 {"SC_PII_OSI_M", _SC_PII_OSI_M},
4777#endif
4778#ifdef _SC_PII_SOCKET
4779 {"SC_PII_SOCKET", _SC_PII_SOCKET},
4780#endif
4781#ifdef _SC_PII_XTI
4782 {"SC_PII_XTI", _SC_PII_XTI},
4783#endif
4784#ifdef _SC_POLL
4785 {"SC_POLL", _SC_POLL},
4786#endif
4787#ifdef _SC_PRIORITIZED_IO
4788 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
4789#endif
4790#ifdef _SC_PRIORITY_SCHEDULING
4791 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
4792#endif
4793#ifdef _SC_REALTIME_SIGNALS
4794 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
4795#endif
4796#ifdef _SC_RE_DUP_MAX
4797 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
4798#endif
4799#ifdef _SC_RTSIG_MAX
4800 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
4801#endif
4802#ifdef _SC_SAVED_IDS
4803 {"SC_SAVED_IDS", _SC_SAVED_IDS},
4804#endif
4805#ifdef _SC_SCHAR_MAX
4806 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
4807#endif
4808#ifdef _SC_SCHAR_MIN
4809 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
4810#endif
4811#ifdef _SC_SELECT
4812 {"SC_SELECT", _SC_SELECT},
4813#endif
4814#ifdef _SC_SEMAPHORES
4815 {"SC_SEMAPHORES", _SC_SEMAPHORES},
4816#endif
4817#ifdef _SC_SEM_NSEMS_MAX
4818 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
4819#endif
4820#ifdef _SC_SEM_VALUE_MAX
4821 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
4822#endif
4823#ifdef _SC_SHARED_MEMORY_OBJECTS
4824 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
4825#endif
4826#ifdef _SC_SHRT_MAX
4827 {"SC_SHRT_MAX", _SC_SHRT_MAX},
4828#endif
4829#ifdef _SC_SHRT_MIN
4830 {"SC_SHRT_MIN", _SC_SHRT_MIN},
4831#endif
4832#ifdef _SC_SIGQUEUE_MAX
4833 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
4834#endif
4835#ifdef _SC_SIGRT_MAX
4836 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
4837#endif
4838#ifdef _SC_SIGRT_MIN
4839 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
4840#endif
Fred Draked86ed291999-12-15 15:34:33 +00004841#ifdef _SC_SOFTPOWER
4842 {"SC_SOFTPOWER", _SC_SOFTPOWER},
4843#endif
Fred Drakec9680921999-12-13 16:37:25 +00004844#ifdef _SC_SPLIT_CACHE
4845 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
4846#endif
4847#ifdef _SC_SSIZE_MAX
4848 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
4849#endif
4850#ifdef _SC_STACK_PROT
4851 {"SC_STACK_PROT", _SC_STACK_PROT},
4852#endif
4853#ifdef _SC_STREAM_MAX
4854 {"SC_STREAM_MAX", _SC_STREAM_MAX},
4855#endif
4856#ifdef _SC_SYNCHRONIZED_IO
4857 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
4858#endif
4859#ifdef _SC_THREADS
4860 {"SC_THREADS", _SC_THREADS},
4861#endif
4862#ifdef _SC_THREAD_ATTR_STACKADDR
4863 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
4864#endif
4865#ifdef _SC_THREAD_ATTR_STACKSIZE
4866 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
4867#endif
4868#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
4869 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
4870#endif
4871#ifdef _SC_THREAD_KEYS_MAX
4872 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
4873#endif
4874#ifdef _SC_THREAD_PRIORITY_SCHEDULING
4875 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
4876#endif
4877#ifdef _SC_THREAD_PRIO_INHERIT
4878 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
4879#endif
4880#ifdef _SC_THREAD_PRIO_PROTECT
4881 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
4882#endif
4883#ifdef _SC_THREAD_PROCESS_SHARED
4884 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
4885#endif
4886#ifdef _SC_THREAD_SAFE_FUNCTIONS
4887 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
4888#endif
4889#ifdef _SC_THREAD_STACK_MIN
4890 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
4891#endif
4892#ifdef _SC_THREAD_THREADS_MAX
4893 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
4894#endif
4895#ifdef _SC_TIMERS
4896 {"SC_TIMERS", _SC_TIMERS},
4897#endif
4898#ifdef _SC_TIMER_MAX
4899 {"SC_TIMER_MAX", _SC_TIMER_MAX},
4900#endif
4901#ifdef _SC_TTY_NAME_MAX
4902 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
4903#endif
4904#ifdef _SC_TZNAME_MAX
4905 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
4906#endif
4907#ifdef _SC_T_IOV_MAX
4908 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
4909#endif
4910#ifdef _SC_UCHAR_MAX
4911 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
4912#endif
4913#ifdef _SC_UINT_MAX
4914 {"SC_UINT_MAX", _SC_UINT_MAX},
4915#endif
4916#ifdef _SC_UIO_MAXIOV
4917 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
4918#endif
4919#ifdef _SC_ULONG_MAX
4920 {"SC_ULONG_MAX", _SC_ULONG_MAX},
4921#endif
4922#ifdef _SC_USHRT_MAX
4923 {"SC_USHRT_MAX", _SC_USHRT_MAX},
4924#endif
4925#ifdef _SC_VERSION
4926 {"SC_VERSION", _SC_VERSION},
4927#endif
4928#ifdef _SC_WORD_BIT
4929 {"SC_WORD_BIT", _SC_WORD_BIT},
4930#endif
4931#ifdef _SC_XBS5_ILP32_OFF32
4932 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
4933#endif
4934#ifdef _SC_XBS5_ILP32_OFFBIG
4935 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
4936#endif
4937#ifdef _SC_XBS5_LP64_OFF64
4938 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
4939#endif
4940#ifdef _SC_XBS5_LPBIG_OFFBIG
4941 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
4942#endif
4943#ifdef _SC_XOPEN_CRYPT
4944 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
4945#endif
4946#ifdef _SC_XOPEN_ENH_I18N
4947 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
4948#endif
4949#ifdef _SC_XOPEN_LEGACY
4950 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
4951#endif
4952#ifdef _SC_XOPEN_REALTIME
4953 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
4954#endif
4955#ifdef _SC_XOPEN_REALTIME_THREADS
4956 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
4957#endif
4958#ifdef _SC_XOPEN_SHM
4959 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
4960#endif
4961#ifdef _SC_XOPEN_UNIX
4962 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
4963#endif
4964#ifdef _SC_XOPEN_VERSION
4965 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
4966#endif
4967#ifdef _SC_XOPEN_XCU_VERSION
4968 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
4969#endif
4970#ifdef _SC_XOPEN_XPG2
4971 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
4972#endif
4973#ifdef _SC_XOPEN_XPG3
4974 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
4975#endif
4976#ifdef _SC_XOPEN_XPG4
4977 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
4978#endif
4979};
4980
4981static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004982conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004983{
4984 return conv_confname(arg, valuep, posix_constants_sysconf,
4985 sizeof(posix_constants_sysconf)
4986 / sizeof(struct constdef));
4987}
4988
4989static char posix_sysconf__doc__[] = "\
4990sysconf(name) -> integer\n\
4991Return an integer-valued system configuration variable.";
4992
4993static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004994posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004995{
4996 PyObject *result = NULL;
4997 int name;
4998
4999 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
5000 int value;
5001
5002 errno = 0;
5003 value = sysconf(name);
5004 if (value == -1 && errno != 0)
5005 posix_error();
5006 else
5007 result = PyInt_FromLong(value);
5008 }
5009 return result;
5010}
5011#endif
5012
5013
Fred Drakebec628d1999-12-15 18:31:10 +00005014/* This code is used to ensure that the tables of configuration value names
5015 * are in sorted order as required by conv_confname(), and also to build the
5016 * the exported dictionaries that are used to publish information about the
5017 * names available on the host platform.
5018 *
5019 * Sorting the table at runtime ensures that the table is properly ordered
5020 * when used, even for platforms we're not able to test on. It also makes
5021 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00005022 */
Fred Drakebec628d1999-12-15 18:31:10 +00005023
5024static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005025cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00005026{
5027 const struct constdef *c1 =
5028 (const struct constdef *) v1;
5029 const struct constdef *c2 =
5030 (const struct constdef *) v2;
5031
5032 return strcmp(c1->name, c2->name);
5033}
5034
5035static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005036setup_confname_table(struct constdef *table, size_t tablesize,
5037 char *tablename, PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00005038{
Fred Drakebec628d1999-12-15 18:31:10 +00005039 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00005040 size_t i;
5041 int status;
Fred Drakebec628d1999-12-15 18:31:10 +00005042
5043 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
5044 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00005045 if (d == NULL)
5046 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005047
Barry Warsaw3155db32000-04-13 15:20:40 +00005048 for (i=0; i < tablesize; ++i) {
5049 PyObject *o = PyInt_FromLong(table[i].value);
5050 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
5051 Py_XDECREF(o);
5052 Py_DECREF(d);
5053 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005054 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005055 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00005056 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005057 status = PyDict_SetItemString(moddict, tablename, d);
5058 Py_DECREF(d);
5059 return status;
Fred Draked86ed291999-12-15 15:34:33 +00005060}
5061
Fred Drakebec628d1999-12-15 18:31:10 +00005062/* Return -1 on failure, 0 on success. */
5063static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005064setup_confname_tables(PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00005065{
5066#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00005067 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00005068 sizeof(posix_constants_pathconf)
5069 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005070 "pathconf_names", moddict))
5071 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005072#endif
5073#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00005074 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00005075 sizeof(posix_constants_confstr)
5076 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005077 "confstr_names", moddict))
5078 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005079#endif
5080#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00005081 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00005082 sizeof(posix_constants_sysconf)
5083 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005084 "sysconf_names", moddict))
5085 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005086#endif
Fred Drakebec628d1999-12-15 18:31:10 +00005087 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00005088}
Fred Draked86ed291999-12-15 15:34:33 +00005089
5090
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005091static char posix_abort__doc__[] = "\
5092abort() -> does not return!\n\
5093Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
5094in the hardest way possible on the hosting operating system.";
5095
5096static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005097posix_abort(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005098{
5099 if (!PyArg_ParseTuple(args, ":abort"))
5100 return NULL;
5101 abort();
5102 /*NOTREACHED*/
5103 Py_FatalError("abort() called from Python code didn't abort!");
5104 return NULL;
5105}
Fred Drakebec628d1999-12-15 18:31:10 +00005106
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005107
5108static PyMethodDef posix_methods[] = {
5109 {"access", posix_access, METH_VARARGS, posix_access__doc__},
5110#ifdef HAVE_TTYNAME
5111 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
5112#endif
5113 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
5114 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005115#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005116 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005117#endif /* HAVE_CHOWN */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005118#ifdef HAVE_CTERMID
5119 {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__},
5120#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00005121#ifdef HAVE_GETCWD
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005122 {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00005123#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005124#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005125 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005126#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005127 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
5128 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
5129 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005130#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005131 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005132#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005133#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005134 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005135#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005136 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
5137 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
5138 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005139#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005140 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005141#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005142#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005143 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005144#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005145 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005146#ifdef HAVE_UNAME
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005147 {"uname", posix_uname, METH_VARARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005148#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005149 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
5150 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
5151 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005152#ifdef HAVE_TIMES
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005153 {"times", posix_times, METH_VARARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005154#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005155 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005156#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005157 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
5158 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005159#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00005160#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005161 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
5162 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00005163#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005164#ifdef HAVE_FORK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005165 {"fork", posix_fork, METH_VARARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005166#endif /* HAVE_FORK */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005167#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005168 {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__},
Thomas Wouters70c21a12000-07-14 14:28:33 +00005169#endif /* HAVE_OPENPTY || HAVE__GETPTY */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005170#ifdef HAVE_FORKPTY
5171 {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
5172#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005173#ifdef HAVE_GETEGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005174 {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005175#endif /* HAVE_GETEGID */
5176#ifdef HAVE_GETEUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005177 {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005178#endif /* HAVE_GETEUID */
5179#ifdef HAVE_GETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005180 {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005181#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00005182#ifdef HAVE_GETGROUPS
5183 {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__},
5184#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005185 {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005186#ifdef HAVE_GETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005187 {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005188#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005189#ifdef HAVE_GETPPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005190 {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005191#endif /* HAVE_GETPPID */
5192#ifdef HAVE_GETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005193 {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005194#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005195#ifdef HAVE_GETLOGIN
5196 {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__},
5197#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00005198#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005199 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005200#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00005201#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005202 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00005203#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005204#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005205 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005206#ifdef MS_WIN32
5207 {"popen2", win32_popen2, METH_VARARGS},
5208 {"popen3", win32_popen3, METH_VARARGS},
5209 {"popen4", win32_popen4, METH_VARARGS},
5210#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005211#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005212#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005213 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005214#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005215#ifdef HAVE_SETEUID
5216 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
5217#endif /* HAVE_SETEUID */
5218#ifdef HAVE_SETEGID
5219 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
5220#endif /* HAVE_SETEGID */
5221#ifdef HAVE_SETREUID
5222 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
5223#endif /* HAVE_SETREUID */
5224#ifdef HAVE_SETREGID
5225 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
5226#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005227#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005228 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005229#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005230#ifdef HAVE_SETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005231 {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005232#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005233#ifdef HAVE_WAIT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005234 {"wait", posix_wait, METH_VARARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005235#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005236#ifdef HAVE_WAITPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005237 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005238#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005239#ifdef HAVE_SETSID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005240 {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005241#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005242#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005243 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005244#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005245#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005246 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005247#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005248#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005249 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005250#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005251 {"open", posix_open, METH_VARARGS, posix_open__doc__},
5252 {"close", posix_close, METH_VARARGS, posix_close__doc__},
5253 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
5254 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
5255 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
5256 {"read", posix_read, METH_VARARGS, posix_read__doc__},
5257 {"write", posix_write, METH_VARARGS, posix_write__doc__},
5258 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
5259 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00005260 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005261#ifdef HAVE_PIPE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005262 {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005263#endif
5264#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005265 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005266#endif
5267#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005268 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005269#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005270#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005271 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005272#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00005273#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005274 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00005275#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00005276#ifdef HAVE_FSYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005277 {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005278#endif
5279#ifdef HAVE_FDATASYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005280 {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005281#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00005282#ifdef HAVE_SYS_WAIT_H
5283#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005284 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005285#endif /* WIFSTOPPED */
5286#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005287 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005288#endif /* WIFSIGNALED */
5289#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005290 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005291#endif /* WIFEXITED */
5292#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005293 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005294#endif /* WEXITSTATUS */
5295#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005296 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005297#endif /* WTERMSIG */
5298#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005299 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005300#endif /* WSTOPSIG */
5301#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005302#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005303 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005304#endif
5305#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005306 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005307#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005308#ifdef HAVE_TMPNAM
5309 {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__},
5310#endif
5311#ifdef HAVE_TEMPNAM
5312 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
5313#endif
5314#ifdef HAVE_TMPNAM
5315 {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__},
5316#endif
Fred Drakec9680921999-12-13 16:37:25 +00005317#ifdef HAVE_CONFSTR
5318 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
5319#endif
5320#ifdef HAVE_SYSCONF
5321 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
5322#endif
5323#ifdef HAVE_FPATHCONF
5324 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
5325#endif
5326#ifdef HAVE_PATHCONF
5327 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
5328#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005329 {"abort", posix_abort, METH_VARARGS, posix_abort__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005330 {NULL, NULL} /* Sentinel */
5331};
5332
5333
Barry Warsaw4a342091996-12-19 23:50:02 +00005334static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005335ins(PyObject *d, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00005336{
5337 PyObject* v = PyInt_FromLong(value);
5338 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
5339 return -1; /* triggers fatal error */
5340
5341 Py_DECREF(v);
5342 return 0;
5343}
5344
Guido van Rossumd48f2521997-12-05 22:19:34 +00005345#if defined(PYOS_OS2)
5346/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
5347static int insertvalues(PyObject *d)
5348{
5349 APIRET rc;
5350 ULONG values[QSV_MAX+1];
5351 PyObject *v;
5352 char *ver, tmp[10];
5353
5354 Py_BEGIN_ALLOW_THREADS
5355 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
5356 Py_END_ALLOW_THREADS
5357
5358 if (rc != NO_ERROR) {
5359 os2_error(rc);
5360 return -1;
5361 }
5362
5363 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
5364 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
5365 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
5366 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
5367 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
5368 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
5369 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
5370
5371 switch (values[QSV_VERSION_MINOR]) {
5372 case 0: ver = "2.00"; break;
5373 case 10: ver = "2.10"; break;
5374 case 11: ver = "2.11"; break;
5375 case 30: ver = "3.00"; break;
5376 case 40: ver = "4.00"; break;
5377 case 50: ver = "5.00"; break;
5378 default:
5379 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
5380 values[QSV_VERSION_MINOR]);
5381 ver = &tmp[0];
5382 }
5383
5384 /* Add Indicator of the Version of the Operating System */
5385 v = PyString_FromString(ver);
5386 if (!v || PyDict_SetItemString(d, "version", v) < 0)
5387 return -1;
5388 Py_DECREF(v);
5389
5390 /* Add Indicator of Which Drive was Used to Boot the System */
5391 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
5392 tmp[1] = ':';
5393 tmp[2] = '\0';
5394
5395 v = PyString_FromString(tmp);
5396 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
5397 return -1;
5398 Py_DECREF(v);
5399
5400 return 0;
5401}
5402#endif
5403
Barry Warsaw4a342091996-12-19 23:50:02 +00005404static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005405all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00005406{
Guido van Rossum94f6f721999-01-06 18:42:14 +00005407#ifdef F_OK
5408 if (ins(d, "F_OK", (long)F_OK)) return -1;
5409#endif
5410#ifdef R_OK
5411 if (ins(d, "R_OK", (long)R_OK)) return -1;
5412#endif
5413#ifdef W_OK
5414 if (ins(d, "W_OK", (long)W_OK)) return -1;
5415#endif
5416#ifdef X_OK
5417 if (ins(d, "X_OK", (long)X_OK)) return -1;
5418#endif
Fred Drakec9680921999-12-13 16:37:25 +00005419#ifdef NGROUPS_MAX
5420 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
5421#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005422#ifdef TMP_MAX
5423 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
5424#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005425#ifdef WNOHANG
5426 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
5427#endif
5428#ifdef O_RDONLY
5429 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
5430#endif
5431#ifdef O_WRONLY
5432 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
5433#endif
5434#ifdef O_RDWR
5435 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
5436#endif
5437#ifdef O_NDELAY
5438 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
5439#endif
5440#ifdef O_NONBLOCK
5441 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
5442#endif
5443#ifdef O_APPEND
5444 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
5445#endif
5446#ifdef O_DSYNC
5447 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
5448#endif
5449#ifdef O_RSYNC
5450 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
5451#endif
5452#ifdef O_SYNC
5453 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
5454#endif
5455#ifdef O_NOCTTY
5456 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
5457#endif
5458#ifdef O_CREAT
5459 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
5460#endif
5461#ifdef O_EXCL
5462 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
5463#endif
5464#ifdef O_TRUNC
5465 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
5466#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00005467#ifdef O_BINARY
5468 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
5469#endif
5470#ifdef O_TEXT
5471 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
5472#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005473
Guido van Rossum246bc171999-02-01 23:54:31 +00005474#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00005475 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
5476 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
5477 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
5478 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
5479 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00005480#endif
5481
Guido van Rossumd48f2521997-12-05 22:19:34 +00005482#if defined(PYOS_OS2)
5483 if (insertvalues(d)) return -1;
5484#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005485 return 0;
5486}
5487
5488
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005489#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005490#define INITFUNC initnt
5491#define MODNAME "nt"
5492#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005493#if defined(PYOS_OS2)
5494#define INITFUNC initos2
5495#define MODNAME "os2"
5496#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005497#define INITFUNC initposix
5498#define MODNAME "posix"
5499#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005500#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005501
Guido van Rossum3886bb61998-12-04 18:50:17 +00005502DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005503INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00005504{
Barry Warsaw53699e91996-12-10 23:23:01 +00005505 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005506
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005507 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005508 posix_methods,
5509 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005510 (PyObject *)NULL,
5511 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00005512 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005513
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005514 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005515 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00005516 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005517 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00005518 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00005519
Barry Warsaw4a342091996-12-19 23:50:02 +00005520 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00005521 return;
5522
Fred Drakebec628d1999-12-15 18:31:10 +00005523 if (setup_confname_tables(d))
5524 return;
5525
Barry Warsawca74da41999-02-09 19:31:45 +00005526 PyDict_SetItemString(d, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00005527
Guido van Rossumb3d39562000-01-31 18:41:26 +00005528#ifdef HAVE_PUTENV
Fred Drake762e2061999-08-26 17:23:54 +00005529 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00005530#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005531}