blob: 99e5864b2c7dafc117816de04be6ce813cdc8f0a [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
Guido van Rossumb00adfb2000-09-25 13:22:00 +0000111#if defined(sun) && !defined(__SVR4)
112/* SunOS 4.1.4 doesn't have prototypes for these: */
113extern int rename(const char *, const char *);
114extern int pclose(FILE *);
115extern int fclose(FILE *);
116#endif
117
Guido van Rossum36bc6801995-06-14 22:54:23 +0000118#ifdef NeXT
119/* NeXT's <unistd.h> and <utime.h> aren't worth much */
120#undef HAVE_UNISTD_H
121#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000122#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000123/* #undef HAVE_GETCWD */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000124#define UNION_WAIT /* This should really be checked for by autoconf */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000125#endif
126
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000127#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000128#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000129extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000130#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000131#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000132extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000134extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000136#endif
137#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000138extern int chdir(char *);
139extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000140#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000141extern int chdir(const char *);
142extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000143#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000144extern int chmod(const char *, mode_t);
145extern int chown(const char *, uid_t, gid_t);
146extern char *getcwd(char *, int);
147extern char *strerror(int);
148extern int link(const char *, const char *);
149extern int rename(const char *, const char *);
150extern int stat(const char *, struct stat *);
151extern int unlink(const char *);
152extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000154extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000155#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000157extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000158#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163#ifdef HAVE_UTIME_H
164#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000165#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000167#ifdef HAVE_SYS_UTIME_H
168#include <sys/utime.h>
169#define HAVE_UTIME_H /* pretend we do for the rest of this file */
170#endif /* HAVE_SYS_UTIME_H */
171
Guido van Rossumb6775db1994-08-01 11:34:53 +0000172#ifdef HAVE_SYS_TIMES_H
173#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000174#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175
176#ifdef HAVE_SYS_PARAM_H
177#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000178#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179
180#ifdef HAVE_SYS_UTSNAME_H
181#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000182#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183
184#ifndef MAXPATHLEN
185#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000186#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000188#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000190#define NAMLEN(dirent) strlen((dirent)->d_name)
191#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000192#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#include <direct.h>
194#define NAMLEN(dirent) strlen((dirent)->d_name)
195#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000197#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000198#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000199#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000200#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000201#endif
202#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000204#endif
205#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000207#endif
208#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211#include <direct.h>
212#include <io.h>
213#include <process.h>
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000214#define WINDOWS_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000216#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000218#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000219#else /* 16-bit Windows */
220#include <dos.h>
221#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000222#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000223#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
Guido van Rossumd48f2521997-12-05 22:19:34 +0000225#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000229#ifdef UNION_WAIT
230/* Emulate some macros on systems that have a union instead of macros */
231
232#ifndef WIFEXITED
233#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
234#endif
235
236#ifndef WEXITSTATUS
237#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
238#endif
239
240#ifndef WTERMSIG
241#define WTERMSIG(u_wait) ((u_wait).w_termsig)
242#endif
243
244#endif /* UNION_WAIT */
245
Greg Wardb48bc172000-03-01 21:51:56 +0000246/* Don't use the "_r" form if we don't need it (also, won't have a
247 prototype for it, at least on Solaris -- maybe others as well?). */
248#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
249#define USE_CTERMID_R
250#endif
251
252#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
253#define USE_TMPNAM_R
254#endif
255
Fred Drake699f3522000-06-29 21:12:41 +0000256/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000257#undef STAT
Fred Drake699f3522000-06-29 21:12:41 +0000258#ifdef MS_WIN64
259# define STAT _stati64
260# define FSTAT _fstati64
261# define STRUCT_STAT struct _stati64
262#else
263# define STAT stat
264# define FSTAT fstat
265# define STRUCT_STAT struct stat
266#endif
267
268
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269/* Return a dictionary corresponding to the POSIX environment table */
270
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000271#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000273#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274
Barry Warsaw53699e91996-12-10 23:23:01 +0000275static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000276convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277{
Barry Warsaw53699e91996-12-10 23:23:01 +0000278 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000280 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 if (d == NULL)
282 return NULL;
283 if (environ == NULL)
284 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000285 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000287 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000288 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 char *p = strchr(*e, '=');
290 if (p == NULL)
291 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000292 k = PyString_FromStringAndSize(*e, (int)(p-*e));
293 if (k == NULL) {
294 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000296 }
297 v = PyString_FromString(p+1);
298 if (v == NULL) {
299 PyErr_Clear();
300 Py_DECREF(k);
301 continue;
302 }
303 if (PyDict_GetItem(d, k) == NULL) {
304 if (PyDict_SetItem(d, k, v) != 0)
305 PyErr_Clear();
306 }
307 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000308 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000310#if defined(PYOS_OS2)
311 {
312 APIRET rc;
313 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
314
315 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000316 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000317 PyObject *v = PyString_FromString(buffer);
318 PyDict_SetItemString(d, "BEGINLIBPATH", v);
319 Py_DECREF(v);
320 }
321 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
322 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
323 PyObject *v = PyString_FromString(buffer);
324 PyDict_SetItemString(d, "ENDLIBPATH", v);
325 Py_DECREF(v);
326 }
327 }
328#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 return d;
330}
331
332
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333/* Set a POSIX-specific error from errno, and return NULL */
334
Barry Warsawd58d7641998-07-23 16:14:40 +0000335static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000336posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000337{
Barry Warsawca74da41999-02-09 19:31:45 +0000338 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339}
Barry Warsawd58d7641998-07-23 16:14:40 +0000340static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000341posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000342{
Barry Warsawca74da41999-02-09 19:31:45 +0000343 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000344}
345
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000346#ifdef MS_WIN32
347static PyObject *
348win32_error(char* function, char* filename)
349{
Mark Hammond33a6da92000-08-15 00:46:38 +0000350 /* XXX We should pass the function name along in the future.
351 (_winreg.c also wants to pass the function name.)
352 This would however require an additional param to the
353 Windows error object, which is non-trivial.
354 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000355 errno = GetLastError();
356 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000357 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000358 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000359 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000360}
361#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362
Guido van Rossumd48f2521997-12-05 22:19:34 +0000363#if defined(PYOS_OS2)
364/**********************************************************************
365 * Helper Function to Trim and Format OS/2 Messages
366 **********************************************************************/
367 static void
368os2_formatmsg(char *msgbuf, int msglen, char *reason)
369{
370 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
371
372 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
373 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
374
375 while (lastc > msgbuf && isspace(*lastc))
376 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
377 }
378
379 /* Add Optional Reason Text */
380 if (reason) {
381 strcat(msgbuf, " : ");
382 strcat(msgbuf, reason);
383 }
384}
385
386/**********************************************************************
387 * Decode an OS/2 Operating System Error Code
388 *
389 * A convenience function to lookup an OS/2 error code and return a
390 * text message we can use to raise a Python exception.
391 *
392 * Notes:
393 * The messages for errors returned from the OS/2 kernel reside in
394 * the file OSO001.MSG in the \OS2 directory hierarchy.
395 *
396 **********************************************************************/
397 static char *
398os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
399{
400 APIRET rc;
401 ULONG msglen;
402
403 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
404 Py_BEGIN_ALLOW_THREADS
405 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
406 errorcode, "oso001.msg", &msglen);
407 Py_END_ALLOW_THREADS
408
409 if (rc == NO_ERROR)
410 os2_formatmsg(msgbuf, msglen, reason);
411 else
412 sprintf(msgbuf, "unknown OS error #%d", errorcode);
413
414 return msgbuf;
415}
416
417/* Set an OS/2-specific error and return NULL. OS/2 kernel
418 errors are not in a global variable e.g. 'errno' nor are
419 they congruent with posix error numbers. */
420
421static PyObject * os2_error(int code)
422{
423 char text[1024];
424 PyObject *v;
425
426 os2_strerror(text, sizeof(text), code, "");
427
428 v = Py_BuildValue("(is)", code, text);
429 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000430 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000431 Py_DECREF(v);
432 }
433 return NULL; /* Signal to Python that an Exception is Pending */
434}
435
436#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437
438/* POSIX generic methods */
439
Barry Warsaw53699e91996-12-10 23:23:01 +0000440static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000441posix_int(PyObject *args, char *format, int (*func)(int))
Guido van Rossum21142a01999-01-08 21:05:37 +0000442{
443 int fd;
444 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000445 if (!PyArg_ParseTuple(args, format, &fd))
Guido van Rossum21142a01999-01-08 21:05:37 +0000446 return NULL;
447 Py_BEGIN_ALLOW_THREADS
448 res = (*func)(fd);
449 Py_END_ALLOW_THREADS
450 if (res < 0)
451 return posix_error();
452 Py_INCREF(Py_None);
453 return Py_None;
454}
455
456
457static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000458posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000460 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000461 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000462 if (!PyArg_ParseTuple(args, format, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000464 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000465 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000466 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000467 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000468 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000469 Py_INCREF(Py_None);
470 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471}
472
Barry Warsaw53699e91996-12-10 23:23:01 +0000473static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000474posix_2str(PyObject *args, char *format,
475 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000477 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000478 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000479 if (!PyArg_ParseTuple(args, format, &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000481 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000482 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000483 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000484 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000485 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000487 Py_INCREF(Py_None);
488 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489}
490
Fred Drake699f3522000-06-29 21:12:41 +0000491/* pack a system stat C structure into the Python stat tuple
492 (used by posix_stat() and posix_fstat()) */
493static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000494_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000495{
496 PyObject *v = PyTuple_New(10);
497 if (v == NULL)
498 return NULL;
499
500 PyTuple_SetItem(v, 0, PyInt_FromLong((long)st.st_mode));
501#ifdef HAVE_LARGEFILE_SUPPORT
502 PyTuple_SetItem(v, 1, PyLong_FromLongLong((LONG_LONG)st.st_ino));
503#else
504 PyTuple_SetItem(v, 1, PyInt_FromLong((long)st.st_ino));
505#endif
506#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
507 PyTuple_SetItem(v, 2, PyLong_FromLongLong((LONG_LONG)st.st_dev));
508#else
509 PyTuple_SetItem(v, 2, PyInt_FromLong((long)st.st_dev));
510#endif
511 PyTuple_SetItem(v, 3, PyInt_FromLong((long)st.st_nlink));
512 PyTuple_SetItem(v, 4, PyInt_FromLong((long)st.st_uid));
513 PyTuple_SetItem(v, 5, PyInt_FromLong((long)st.st_gid));
514#ifdef HAVE_LARGEFILE_SUPPORT
515 PyTuple_SetItem(v, 6, PyLong_FromLongLong((LONG_LONG)st.st_size));
516#else
517 PyTuple_SetItem(v, 6, PyInt_FromLong(st.st_size));
518#endif
519#if SIZEOF_TIME_T > SIZEOF_LONG
520 PyTuple_SetItem(v, 7, PyLong_FromLongLong((LONG_LONG)st.st_atime));
521 PyTuple_SetItem(v, 8, PyLong_FromLongLong((LONG_LONG)st.st_mtime));
522 PyTuple_SetItem(v, 9, PyLong_FromLongLong((LONG_LONG)st.st_ctime));
523#else
524 PyTuple_SetItem(v, 7, PyInt_FromLong((long)st.st_atime));
525 PyTuple_SetItem(v, 8, PyInt_FromLong((long)st.st_mtime));
526 PyTuple_SetItem(v, 9, PyInt_FromLong((long)st.st_ctime));
527#endif
528
529 if (PyErr_Occurred()) {
530 Py_DECREF(v);
531 return NULL;
532 }
533
534 return v;
535}
536
537
Barry Warsaw53699e91996-12-10 23:23:01 +0000538static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000539posix_do_stat(PyObject *self, PyObject *args, char *format,
540 int (*statfunc)(const char *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541{
Fred Drake699f3522000-06-29 21:12:41 +0000542 STRUCT_STAT st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000543 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000544 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000545
546#ifdef MS_WIN32
547 int pathlen;
548 char pathcopy[MAX_PATH];
549#endif /* MS_WIN32 */
550
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000551 if (!PyArg_ParseTuple(args, format, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552 return NULL;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000553
554#ifdef MS_WIN32
555 pathlen = strlen(path);
556 /* the library call can blow up if the file name is too long! */
557 if (pathlen > MAX_PATH) {
558 errno = ENAMETOOLONG;
559 return posix_error();
560 }
561
562 if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) {
Guido van Rossum19dde102000-05-03 02:44:55 +0000563 /* exception for specific or current drive root */
564 if (!((pathlen == 1) ||
565 ((pathlen == 3) &&
Guido van Rossumace88ae2000-04-21 18:54:45 +0000566 (path[1] == ':') &&
Guido van Rossum19dde102000-05-03 02:44:55 +0000567 (path[2] == '\\' || path[2] == '/'))))
Guido van Rossumace88ae2000-04-21 18:54:45 +0000568 {
569 strncpy(pathcopy, path, pathlen);
570 pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */
571 path = pathcopy;
572 }
573 }
574#endif /* MS_WIN32 */
575
Barry Warsaw53699e91996-12-10 23:23:01 +0000576 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000577 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000578 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000579 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000580 return posix_error_with_filename(path);
Fred Drake699f3522000-06-29 21:12:41 +0000581
582 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583}
584
585
586/* POSIX methods */
587
Guido van Rossum94f6f721999-01-06 18:42:14 +0000588static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000589"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000590Test for access to a file.";
591
592static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000593posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +0000594{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000595 char *path;
596 int mode;
597 int res;
598
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000599 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +0000600 return NULL;
601 Py_BEGIN_ALLOW_THREADS
602 res = access(path, mode);
603 Py_END_ALLOW_THREADS
604 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000605}
606
Guido van Rossumd371ff11999-01-25 16:12:23 +0000607#ifndef F_OK
608#define F_OK 0
609#endif
610#ifndef R_OK
611#define R_OK 4
612#endif
613#ifndef W_OK
614#define W_OK 2
615#endif
616#ifndef X_OK
617#define X_OK 1
618#endif
619
620#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000621static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000622"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000623Return the name of the terminal device connected to 'fd'.";
624
625static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000626posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +0000627{
Guido van Rossum94f6f721999-01-06 18:42:14 +0000628 int id;
629 char *ret;
630
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000631 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +0000632 return NULL;
633
Guido van Rossum94f6f721999-01-06 18:42:14 +0000634 ret = ttyname(id);
635 if (ret == NULL)
636 return(posix_error());
637 return(PyString_FromString(ret));
638}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000639#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000640
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000641#ifdef HAVE_CTERMID
642static char posix_ctermid__doc__[] =
643"ctermid() -> String\n\
644Return the name of the controlling terminal for this process.";
645
646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000647posix_ctermid(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000648{
649 char *ret;
650 char buffer[L_ctermid];
651
652 if (!PyArg_ParseTuple(args, ":ctermid"))
653 return NULL;
654
Greg Wardb48bc172000-03-01 21:51:56 +0000655#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000656 ret = ctermid_r(buffer);
657#else
658 ret = ctermid(buffer);
659#endif
660 if (ret == NULL)
661 return(posix_error());
662 return(PyString_FromString(buffer));
663}
664#endif
665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000666static char posix_chdir__doc__[] =
667"chdir(path) -> None\n\
668Change the current working directory to the specified path.";
669
Barry Warsaw53699e91996-12-10 23:23:01 +0000670static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000671posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000673 return posix_1str(args, "s:chdir", chdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674}
675
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000676
677static char posix_chmod__doc__[] =
678"chmod(path, mode) -> None\n\
679Change the access permissions of a file.";
680
Barry Warsaw53699e91996-12-10 23:23:01 +0000681static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000682posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000683{
Guido van Rossumffd15f52000-03-31 00:47:28 +0000684 char *path;
685 int i;
686 int res;
Guido van Rossum49679b42000-03-31 00:48:21 +0000687 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +0000688 return NULL;
689 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +0000690 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +0000691 Py_END_ALLOW_THREADS
692 if (res < 0)
693 return posix_error_with_filename(path);
694 Py_INCREF(Py_None);
695 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000696}
697
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000698
Guido van Rossum21142a01999-01-08 21:05:37 +0000699#ifdef HAVE_FSYNC
700static char posix_fsync__doc__[] =
701"fsync(fildes) -> None\n\
702force write of file with filedescriptor to disk.";
703
704static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000705posix_fsync(PyObject *self, PyObject *args)
Guido van Rossum21142a01999-01-08 21:05:37 +0000706{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000707 return posix_int(args, "i:fsync", fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000708}
709#endif /* HAVE_FSYNC */
710
711#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +0000712
Guido van Rossum7f58e2e2000-09-22 17:26:14 +0000713#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +0000714extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
715#endif
716
Guido van Rossum21142a01999-01-08 21:05:37 +0000717static char posix_fdatasync__doc__[] =
718"fdatasync(fildes) -> None\n\
719force write of file with filedescriptor to disk.\n\
720 does not force update of metadata.";
721
722static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000723posix_fdatasync(PyObject *self, PyObject *args)
Guido van Rossum21142a01999-01-08 21:05:37 +0000724{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000725 return posix_int(args, "i:fdatasync", fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000726}
727#endif /* HAVE_FDATASYNC */
728
729
Fredrik Lundh10723342000-07-10 16:38:09 +0000730#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000731static char posix_chown__doc__[] =
732"chown(path, uid, gid) -> None\n\
733Change the owner and group id of path to the numeric uid and gid.";
734
Barry Warsaw53699e91996-12-10 23:23:01 +0000735static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000736posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000737{
Fredrik Lundh44328e62000-07-10 15:59:30 +0000738 char *path;
739 int uid, gid;
740 int res;
741 if (!PyArg_ParseTuple(args, "sii:chown", &path, &uid, &gid))
742 return NULL;
743 Py_BEGIN_ALLOW_THREADS
744 res = chown(path, (uid_t) uid, (gid_t) gid);
745 Py_END_ALLOW_THREADS
746 if (res < 0)
747 return posix_error_with_filename(path);
748 Py_INCREF(Py_None);
749 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000750}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000751#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000753
Guido van Rossum36bc6801995-06-14 22:54:23 +0000754#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000755static char posix_getcwd__doc__[] =
756"getcwd() -> path\n\
757Return a string representing the current working directory.";
758
Barry Warsaw53699e91996-12-10 23:23:01 +0000759static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000760posix_getcwd(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761{
762 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000763 char *res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000764 if (!PyArg_ParseTuple(args, ":getcwd"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000766 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000767 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000768 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000769 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000771 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000773#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000775
Guido van Rossumb6775db1994-08-01 11:34:53 +0000776#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000777static char posix_link__doc__[] =
778"link(src, dst) -> None\n\
779Create a hard link to a file.";
780
Barry Warsaw53699e91996-12-10 23:23:01 +0000781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000782posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000783{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000784 return posix_2str(args, "ss:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000786#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000788
789static char posix_listdir__doc__[] =
790"listdir(path) -> list_of_strings\n\
791Return a list containing the names of the entries in the directory.\n\
792\n\
793 path: path of directory to list\n\
794\n\
795The list is in arbitrary order. It does not include the special\n\
796entries '.' and '..' even if they are present in the directory.";
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000799posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000800{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000801 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000802 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000803#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000804
Guido van Rossumb6775db1994-08-01 11:34:53 +0000805 char *name;
806 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000807 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808 HANDLE hFindFile;
809 WIN32_FIND_DATA FileData;
810 char namebuf[MAX_PATH+5];
Tim Peters0bb44a42000-09-15 07:44:49 +0000811 char ch;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000813 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000814 return NULL;
815 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000816 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000817 return NULL;
818 }
819 strcpy(namebuf, name);
Tim Peters0bb44a42000-09-15 07:44:49 +0000820 ch = namebuf[len-1];
821 if (ch != '/' && ch != '\\' && ch != ':')
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822 namebuf[len++] = '/';
823 strcpy(namebuf + len, "*.*");
824
Barry Warsaw53699e91996-12-10 23:23:01 +0000825 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000826 return NULL;
827
828 hFindFile = FindFirstFile(namebuf, &FileData);
829 if (hFindFile == INVALID_HANDLE_VALUE) {
830 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000831 if (errno == ERROR_FILE_NOT_FOUND)
832 return PyList_New(0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000833 return win32_error("FindFirstFile", name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000834 }
835 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000836 if (FileData.cFileName[0] == '.' &&
837 (FileData.cFileName[1] == '\0' ||
838 FileData.cFileName[1] == '.' &&
839 FileData.cFileName[2] == '\0'))
840 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000841 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000842 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000843 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844 d = NULL;
845 break;
846 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000847 if (PyList_Append(d, v) != 0) {
848 Py_DECREF(v);
849 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000850 d = NULL;
851 break;
852 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000853 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000854 } while (FindNextFile(hFindFile, &FileData) == TRUE);
855
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000856 if (FindClose(hFindFile) == FALSE)
857 return win32_error("FindClose", name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000858
859 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000860
Tim Peters0bb44a42000-09-15 07:44:49 +0000861#elif defined(_MSC_VER) /* 16-bit Windows */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000862
863#ifndef MAX_PATH
864#define MAX_PATH 250
865#endif
866 char *name, *pt;
867 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000868 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000869 char namebuf[MAX_PATH+5];
870 struct _find_t ep;
871
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000872 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000873 return NULL;
874 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000875 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000876 return NULL;
877 }
878 strcpy(namebuf, name);
879 for (pt = namebuf; *pt; pt++)
880 if (*pt == '/')
881 *pt = '\\';
882 if (namebuf[len-1] != '\\')
883 namebuf[len++] = '\\';
884 strcpy(namebuf + len, "*.*");
885
Barry Warsaw53699e91996-12-10 23:23:01 +0000886 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000887 return NULL;
888
889 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000890 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
891 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000892 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000893 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000894 }
895 do {
896 if (ep.name[0] == '.' &&
897 (ep.name[1] == '\0' ||
898 ep.name[1] == '.' &&
899 ep.name[2] == '\0'))
900 continue;
901 strcpy(namebuf, ep.name);
902 for (pt = namebuf; *pt; pt++)
903 if (isupper(*pt))
904 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000905 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000906 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000907 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000908 d = NULL;
909 break;
910 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000911 if (PyList_Append(d, v) != 0) {
912 Py_DECREF(v);
913 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000914 d = NULL;
915 break;
916 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000917 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000918 } while (_dos_findnext(&ep) == 0);
919
920 return d;
921
Tim Peters0bb44a42000-09-15 07:44:49 +0000922#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000923
924#ifndef MAX_PATH
925#define MAX_PATH CCHMAXPATH
926#endif
927 char *name, *pt;
928 int len;
929 PyObject *d, *v;
930 char namebuf[MAX_PATH+5];
931 HDIR hdir = 1;
932 ULONG srchcnt = 1;
933 FILEFINDBUF3 ep;
934 APIRET rc;
935
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000936 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000937 return NULL;
938 if (len >= MAX_PATH) {
939 PyErr_SetString(PyExc_ValueError, "path too long");
940 return NULL;
941 }
942 strcpy(namebuf, name);
943 for (pt = namebuf; *pt; pt++)
944 if (*pt == '/')
945 *pt = '\\';
946 if (namebuf[len-1] != '\\')
947 namebuf[len++] = '\\';
948 strcpy(namebuf + len, "*.*");
949
950 if ((d = PyList_New(0)) == NULL)
951 return NULL;
952
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000953 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
954 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000955 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000956 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
957 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
958 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000959
960 if (rc != NO_ERROR) {
961 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000962 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000963 }
964
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000965 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000966 do {
967 if (ep.achName[0] == '.'
968 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000969 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000970
971 strcpy(namebuf, ep.achName);
972
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000973 /* Leave Case of Name Alone -- In Native Form */
974 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000975
976 v = PyString_FromString(namebuf);
977 if (v == NULL) {
978 Py_DECREF(d);
979 d = NULL;
980 break;
981 }
982 if (PyList_Append(d, v) != 0) {
983 Py_DECREF(v);
984 Py_DECREF(d);
985 d = NULL;
986 break;
987 }
988 Py_DECREF(v);
989 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
990 }
991
992 return d;
993#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000994
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000995 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000996 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000997 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000998 struct dirent *ep;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000999 if (!PyArg_ParseTuple(args, "s:listdir", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001001 if ((dirp = opendir(name)) == NULL) {
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001002 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001003 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001005 closedir(dirp);
1006 return NULL;
1007 }
1008 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001009 if (ep->d_name[0] == '.' &&
1010 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001011 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001012 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001013 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001014 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001015 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001016 d = NULL;
1017 break;
1018 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001019 if (PyList_Append(d, v) != 0) {
1020 Py_DECREF(v);
1021 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001022 d = NULL;
1023 break;
1024 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001025 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001026 }
1027 closedir(dirp);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001028
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001029 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001030
Tim Peters0bb44a42000-09-15 07:44:49 +00001031#endif /* which OS */
1032} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001033
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001034static char posix_mkdir__doc__[] =
1035"mkdir(path [, mode=0777]) -> None\n\
1036Create a directory.";
1037
Barry Warsaw53699e91996-12-10 23:23:01 +00001038static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001039posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001040{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001041 int res;
1042 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001043 int mode = 0777;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001044 if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001045 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001046 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001047#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001048 res = mkdir(path);
1049#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001050 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001051#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001052 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001053 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001054 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001055 Py_INCREF(Py_None);
1056 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001057}
1058
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001059
Guido van Rossumb6775db1994-08-01 11:34:53 +00001060#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001061static char posix_nice__doc__[] =
1062"nice(inc) -> new_priority\n\
1063Decrease the priority of process and return new priority.";
1064
Barry Warsaw53699e91996-12-10 23:23:01 +00001065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001066posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001067{
1068 int increment, value;
1069
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001070 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001071 return NULL;
1072 value = nice(increment);
1073 if (value == -1)
1074 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001075 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001076}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001077#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001079
1080static char posix_rename__doc__[] =
1081"rename(old, new) -> None\n\
1082Rename a file or directory.";
1083
Barry Warsaw53699e91996-12-10 23:23:01 +00001084static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001085posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001086{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001087 return posix_2str(args, "ss:rename", rename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001088}
1089
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001090
1091static char posix_rmdir__doc__[] =
1092"rmdir(path) -> None\n\
1093Remove a directory.";
1094
Barry Warsaw53699e91996-12-10 23:23:01 +00001095static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001096posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001097{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001098 return posix_1str(args, "s:rmdir", rmdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001099}
1100
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001101
1102static char posix_stat__doc__[] =
1103"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1104Perform a stat system call on the given path.";
1105
Barry Warsaw53699e91996-12-10 23:23:01 +00001106static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001107posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001108{
Fred Drake699f3522000-06-29 21:12:41 +00001109 return posix_do_stat(self, args, "s:stat", STAT);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110}
1111
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001112
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001113#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001114static char posix_system__doc__[] =
1115"system(command) -> exit_status\n\
1116Execute the command (a string) in a subshell.";
1117
Barry Warsaw53699e91996-12-10 23:23:01 +00001118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001119posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001120{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001121 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001122 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001123 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001124 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001125 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001126 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001127 Py_END_ALLOW_THREADS
1128 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001129}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001130#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001131
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001132
1133static char posix_umask__doc__[] =
1134"umask(new_mask) -> old_mask\n\
1135Set the current numeric umask and return the previous umask.";
1136
Barry Warsaw53699e91996-12-10 23:23:01 +00001137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001138posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001139{
1140 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001141 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001142 return NULL;
1143 i = umask(i);
1144 if (i < 0)
1145 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001146 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001147}
1148
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001149
1150static char posix_unlink__doc__[] =
1151"unlink(path) -> None\n\
1152Remove a file (same as remove(path)).";
1153
1154static char posix_remove__doc__[] =
1155"remove(path) -> None\n\
1156Remove a file (same as unlink(path)).";
1157
Barry Warsaw53699e91996-12-10 23:23:01 +00001158static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001159posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001161 return posix_1str(args, "s:remove", unlink);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001162}
1163
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001164
Guido van Rossumb6775db1994-08-01 11:34:53 +00001165#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001166static char posix_uname__doc__[] =
1167"uname() -> (sysname, nodename, release, version, machine)\n\
1168Return a tuple identifying the current operating system.";
1169
Barry Warsaw53699e91996-12-10 23:23:01 +00001170static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001171posix_uname(PyObject *self, PyObject *args)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001172{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001173 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001174 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001175 if (!PyArg_ParseTuple(args, ":uname"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001176 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001177 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001178 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001179 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001180 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001181 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001182 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001183 u.sysname,
1184 u.nodename,
1185 u.release,
1186 u.version,
1187 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001188}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001189#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001190
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001191
1192static char posix_utime__doc__[] =
1193"utime(path, (atime, utime)) -> None\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001194utime(path, None) -> None\n\
1195Set the access and modified time of the file to the given values. If the\n\
1196second form is used, set the access and modified times to the current time.";
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001197
Barry Warsaw53699e91996-12-10 23:23:01 +00001198static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001199posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001200{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001201 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001202 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001203 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001204 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001205
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001206/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001207#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001208 struct utimbuf buf;
1209#define ATIME buf.actime
1210#define MTIME buf.modtime
1211#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001212#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001213 time_t buf[2];
1214#define ATIME buf[0]
1215#define MTIME buf[1]
1216#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001217#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001218
Barry Warsaw3cef8562000-05-01 16:17:24 +00001219 if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001220 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001221 if (arg == Py_None) {
1222 /* optional time values not given */
1223 Py_BEGIN_ALLOW_THREADS
1224 res = utime(path, NULL);
1225 Py_END_ALLOW_THREADS
1226 }
1227 else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) {
1228 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001229 "utime() arg 2 must be a tuple (atime, mtime)");
Barry Warsaw3cef8562000-05-01 16:17:24 +00001230 return NULL;
1231 }
1232 else {
1233 ATIME = atime;
1234 MTIME = mtime;
1235 Py_BEGIN_ALLOW_THREADS
1236 res = utime(path, UTIME_ARG);
1237 Py_END_ALLOW_THREADS
1238 }
Guido van Rossumff4949e1992-08-05 19:58:53 +00001239 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001240 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001241 Py_INCREF(Py_None);
1242 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001243#undef UTIME_ARG
1244#undef ATIME
1245#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001246}
1247
Guido van Rossum85e3b011991-06-03 12:42:10 +00001248
Guido van Rossum3b066191991-06-04 19:40:25 +00001249/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001250
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001251static char posix__exit__doc__[] =
1252"_exit(status)\n\
1253Exit to the system with specified status, without normal exit processing.";
1254
Barry Warsaw53699e91996-12-10 23:23:01 +00001255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001256posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001257{
1258 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001259 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001260 return NULL;
1261 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001262 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001263}
1264
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001265
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001266#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001267static char posix_execv__doc__[] =
1268"execv(path, args)\n\
1269Execute an executable path with arguments, replacing current process.\n\
1270\n\
1271 path: path of executable file\n\
1272 args: tuple or list of strings";
1273
Barry Warsaw53699e91996-12-10 23:23:01 +00001274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001275posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001276{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001277 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001278 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001279 char **argvlist;
1280 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001281 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001282
Guido van Rossum89b33251993-10-22 14:26:06 +00001283 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001284 argv is a list or tuple of strings. */
1285
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001286 if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001287 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001288 if (PyList_Check(argv)) {
1289 argc = PyList_Size(argv);
1290 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001291 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001292 else if (PyTuple_Check(argv)) {
1293 argc = PyTuple_Size(argv);
1294 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001295 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001296 else {
Fred Drake661ea262000-10-24 19:57:45 +00001297 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
Guido van Rossum50422b42000-04-26 20:34:28 +00001298 return NULL;
1299 }
1300
1301 if (argc == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001302 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001303 return NULL;
1304 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001305
Barry Warsaw53699e91996-12-10 23:23:01 +00001306 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001307 if (argvlist == NULL)
1308 return NULL;
1309 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001310 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1311 PyMem_DEL(argvlist);
Guido van Rossum50422b42000-04-26 20:34:28 +00001312 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001313 "execv() arg 2 must contain only strings");
Guido van Rossum50422b42000-04-26 20:34:28 +00001314 return NULL;
1315
Guido van Rossum85e3b011991-06-03 12:42:10 +00001316 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001317 }
1318 argvlist[argc] = NULL;
1319
Guido van Rossumb6775db1994-08-01 11:34:53 +00001320#ifdef BAD_EXEC_PROTOTYPES
1321 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001322#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001323 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001324#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001325
Guido van Rossum85e3b011991-06-03 12:42:10 +00001326 /* If we get here it's definitely an error */
1327
Barry Warsaw53699e91996-12-10 23:23:01 +00001328 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001329 return posix_error();
1330}
1331
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001332
1333static char posix_execve__doc__[] =
1334"execve(path, args, env)\n\
1335Execute a path with arguments and environment, replacing current process.\n\
1336\n\
1337 path: path of executable file\n\
1338 args: tuple or list of arguments\n\
Thomas Wouters7e474022000-07-16 12:04:32 +00001339 env: dictionary of strings mapping to strings";
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001340
Barry Warsaw53699e91996-12-10 23:23:01 +00001341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001342posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001343{
1344 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001345 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001346 char **argvlist;
1347 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001348 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001349 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001350 PyObject *(*getitem)(PyObject *, int);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001351
1352 /* execve has three arguments: (path, argv, env), where
1353 argv is a list or tuple of strings and env is a dictionary
1354 like posix.environ. */
1355
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001356 if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001357 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001358 if (PyList_Check(argv)) {
1359 argc = PyList_Size(argv);
1360 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001361 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001362 else if (PyTuple_Check(argv)) {
1363 argc = PyTuple_Size(argv);
1364 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001365 }
1366 else {
Fred Drake661ea262000-10-24 19:57:45 +00001367 PyErr_SetString(PyExc_TypeError, "execve() arg 2 must be a tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001368 return NULL;
1369 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001370 if (!PyMapping_Check(env)) {
Fred Drake661ea262000-10-24 19:57:45 +00001371 PyErr_SetString(PyExc_TypeError, "execve() arg 3 must be a mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001372 return NULL;
1373 }
1374
Guido van Rossum50422b42000-04-26 20:34:28 +00001375 if (argc == 0) {
1376 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001377 "execve() arg 2 must not be empty");
Guido van Rossum50422b42000-04-26 20:34:28 +00001378 return NULL;
1379 }
1380
Barry Warsaw53699e91996-12-10 23:23:01 +00001381 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001382 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001383 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001384 return NULL;
1385 }
1386 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001387 if (!PyArg_Parse((*getitem)(argv, i),
Fred Drake661ea262000-10-24 19:57:45 +00001388 "s;execve() arg 2 must contain only strings",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001389 &argvlist[i]))
1390 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001391 goto fail_1;
1392 }
1393 }
1394 argvlist[argc] = NULL;
1395
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001396 i = PyMapping_Size(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001397 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001398 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001399 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001400 goto fail_1;
1401 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001402 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001403 keys = PyMapping_Keys(env);
1404 vals = PyMapping_Values(env);
1405 if (!keys || !vals)
1406 goto fail_2;
1407
1408 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001409 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001410
1411 key = PyList_GetItem(keys, pos);
1412 val = PyList_GetItem(vals, pos);
1413 if (!key || !val)
1414 goto fail_2;
1415
Fred Drake661ea262000-10-24 19:57:45 +00001416 if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) ||
1417 !PyArg_Parse(val, "s;execve() arg 3 contains a non-string value", &v))
Barry Warsaw43d68b81996-12-19 22:10:44 +00001418 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001419 goto fail_2;
1420 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001421
1422#if defined(PYOS_OS2)
1423 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1424 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1425#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001426 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001427 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001428 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001429 goto fail_2;
1430 }
1431 sprintf(p, "%s=%s", k, v);
1432 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001433#if defined(PYOS_OS2)
1434 }
1435#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001436 }
1437 envlist[envc] = 0;
1438
Guido van Rossumb6775db1994-08-01 11:34:53 +00001439
1440#ifdef BAD_EXEC_PROTOTYPES
1441 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001442#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001443 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001444#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001445
1446 /* If we get here it's definitely an error */
1447
1448 (void) posix_error();
1449
1450 fail_2:
1451 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001452 PyMem_DEL(envlist[envc]);
1453 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001454 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001455 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001456 Py_XDECREF(vals);
1457 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001458 return NULL;
1459}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001460#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001461
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001462
Guido van Rossuma1065681999-01-25 23:20:23 +00001463#ifdef HAVE_SPAWNV
1464static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001465"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001466Execute an executable path with arguments, replacing current process.\n\
1467\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001468 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001469 path: path of executable file\n\
1470 args: tuple or list of strings";
1471
1472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001473posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00001474{
1475 char *path;
1476 PyObject *argv;
1477 char **argvlist;
1478 int mode, i, argc;
Fred Drake699f3522000-06-29 21:12:41 +00001479 intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001480 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00001481
1482 /* spawnv has three arguments: (mode, path, argv), where
1483 argv is a list or tuple of strings. */
1484
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001485 if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00001486 return NULL;
1487 if (PyList_Check(argv)) {
1488 argc = PyList_Size(argv);
1489 getitem = PyList_GetItem;
1490 }
1491 else if (PyTuple_Check(argv)) {
1492 argc = PyTuple_Size(argv);
1493 getitem = PyTuple_GetItem;
1494 }
1495 else {
Fred Drake661ea262000-10-24 19:57:45 +00001496 PyErr_SetString(PyExc_TypeError, "spawmv() arg 2 must be a tuple or list");
Guido van Rossuma1065681999-01-25 23:20:23 +00001497 return NULL;
1498 }
1499
1500 argvlist = PyMem_NEW(char *, argc+1);
1501 if (argvlist == NULL)
1502 return NULL;
1503 for (i = 0; i < argc; i++) {
1504 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1505 PyMem_DEL(argvlist);
Fred Drake137507e2000-06-01 02:02:46 +00001506 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001507 "spawnv() arg 2 must contain only strings");
Fred Drake137507e2000-06-01 02:02:46 +00001508 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00001509 }
1510 }
1511 argvlist[argc] = NULL;
1512
Guido van Rossum246bc171999-02-01 23:54:31 +00001513 if (mode == _OLD_P_OVERLAY)
1514 mode = _P_OVERLAY;
Fred Drake699f3522000-06-29 21:12:41 +00001515 spawnval = _spawnv(mode, path, argvlist);
Guido van Rossuma1065681999-01-25 23:20:23 +00001516
1517 PyMem_DEL(argvlist);
1518
Fred Drake699f3522000-06-29 21:12:41 +00001519 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00001520 return posix_error();
1521 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001522#if SIZEOF_LONG == SIZEOF_VOID_P
1523 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001524#else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001525 return Py_BuildValue("L", (LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001526#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00001527}
1528
1529
1530static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001531"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001532Execute a path with arguments and environment, replacing current process.\n\
1533\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001534 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001535 path: path of executable file\n\
1536 args: tuple or list of arguments\n\
Thomas Wouters7e474022000-07-16 12:04:32 +00001537 env: dictionary of strings mapping to strings";
Guido van Rossuma1065681999-01-25 23:20:23 +00001538
1539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001540posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00001541{
1542 char *path;
1543 PyObject *argv, *env;
1544 char **argvlist;
1545 char **envlist;
1546 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1547 int mode, i, pos, argc, envc;
Fred Drake699f3522000-06-29 21:12:41 +00001548 intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001549 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00001550
1551 /* spawnve has four arguments: (mode, path, argv, env), where
1552 argv is a list or tuple of strings and env is a dictionary
1553 like posix.environ. */
1554
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001555 if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00001556 return NULL;
1557 if (PyList_Check(argv)) {
1558 argc = PyList_Size(argv);
1559 getitem = PyList_GetItem;
1560 }
1561 else if (PyTuple_Check(argv)) {
1562 argc = PyTuple_Size(argv);
1563 getitem = PyTuple_GetItem;
1564 }
1565 else {
Fred Drake661ea262000-10-24 19:57:45 +00001566 PyErr_SetString(PyExc_TypeError, "spawnve() arg 2 must be a tuple or list");
Guido van Rossuma1065681999-01-25 23:20:23 +00001567 return NULL;
1568 }
1569 if (!PyMapping_Check(env)) {
Fred Drake661ea262000-10-24 19:57:45 +00001570 PyErr_SetString(PyExc_TypeError, "spawnve() arg 3 must be a mapping object");
Guido van Rossuma1065681999-01-25 23:20:23 +00001571 return NULL;
1572 }
1573
1574 argvlist = PyMem_NEW(char *, argc+1);
1575 if (argvlist == NULL) {
1576 PyErr_NoMemory();
1577 return NULL;
1578 }
1579 for (i = 0; i < argc; i++) {
1580 if (!PyArg_Parse((*getitem)(argv, i),
Fred Drake661ea262000-10-24 19:57:45 +00001581 "s;spawnve() arg 2 must contain only strings",
Guido van Rossuma1065681999-01-25 23:20:23 +00001582 &argvlist[i]))
1583 {
1584 goto fail_1;
1585 }
1586 }
1587 argvlist[argc] = NULL;
1588
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001589 i = PyMapping_Size(env);
Guido van Rossuma1065681999-01-25 23:20:23 +00001590 envlist = PyMem_NEW(char *, i + 1);
1591 if (envlist == NULL) {
1592 PyErr_NoMemory();
1593 goto fail_1;
1594 }
1595 envc = 0;
1596 keys = PyMapping_Keys(env);
1597 vals = PyMapping_Values(env);
1598 if (!keys || !vals)
1599 goto fail_2;
1600
1601 for (pos = 0; pos < i; pos++) {
1602 char *p, *k, *v;
1603
1604 key = PyList_GetItem(keys, pos);
1605 val = PyList_GetItem(vals, pos);
1606 if (!key || !val)
1607 goto fail_2;
1608
Fred Drake661ea262000-10-24 19:57:45 +00001609 if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) ||
1610 !PyArg_Parse(val, "s;spawnve() arg 3 contains a non-string value", &v))
Guido van Rossuma1065681999-01-25 23:20:23 +00001611 {
1612 goto fail_2;
1613 }
1614 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1615 if (p == NULL) {
1616 PyErr_NoMemory();
1617 goto fail_2;
1618 }
1619 sprintf(p, "%s=%s", k, v);
1620 envlist[envc++] = p;
1621 }
1622 envlist[envc] = 0;
1623
Guido van Rossum246bc171999-02-01 23:54:31 +00001624 if (mode == _OLD_P_OVERLAY)
1625 mode = _P_OVERLAY;
Fred Drake699f3522000-06-29 21:12:41 +00001626 spawnval = _spawnve(mode, path, argvlist, envlist);
1627 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00001628 (void) posix_error();
1629 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001630#if SIZEOF_LONG == SIZEOF_VOID_P
1631 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001632#else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001633 res = Py_BuildValue("L", (LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001634#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00001635
1636 fail_2:
1637 while (--envc >= 0)
1638 PyMem_DEL(envlist[envc]);
1639 PyMem_DEL(envlist);
1640 fail_1:
1641 PyMem_DEL(argvlist);
1642 Py_XDECREF(vals);
1643 Py_XDECREF(keys);
1644 return res;
1645}
1646#endif /* HAVE_SPAWNV */
1647
1648
Guido van Rossumad0ee831995-03-01 10:34:45 +00001649#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001650static char posix_fork__doc__[] =
1651"fork() -> pid\n\
1652Fork a child process.\n\
1653\n\
1654Return 0 to child process and PID of child to parent process.";
1655
Barry Warsaw53699e91996-12-10 23:23:01 +00001656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001657posix_fork(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001658{
1659 int pid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001660 if (!PyArg_ParseTuple(args, ":fork"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001661 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001662 pid = fork();
1663 if (pid == -1)
1664 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00001665 if (pid == 0)
1666 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001667 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001668}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001669#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001670
Fred Drake8cef4cf2000-06-28 16:40:38 +00001671#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
1672#ifdef HAVE_PTY_H
1673#include <pty.h>
1674#else
1675#ifdef HAVE_LIBUTIL_H
1676#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00001677#endif /* HAVE_LIBUTIL_H */
1678#endif /* HAVE_PTY_H */
Thomas Wouters70c21a12000-07-14 14:28:33 +00001679#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00001680
Thomas Wouters70c21a12000-07-14 14:28:33 +00001681#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001682static char posix_openpty__doc__[] =
1683"openpty() -> (master_fd, slave_fd)\n\
1684Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
1685
1686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001687posix_openpty(PyObject *self, PyObject *args)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001688{
1689 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001690#ifndef HAVE_OPENPTY
1691 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001692#endif
1693
Fred Drake8cef4cf2000-06-28 16:40:38 +00001694 if (!PyArg_ParseTuple(args, ":openpty"))
1695 return NULL;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001696
1697#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00001698 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
1699 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00001700#else
1701 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
1702 if (slave_name == NULL)
1703 return posix_error();
1704
1705 slave_fd = open(slave_name, O_RDWR);
1706 if (slave_fd < 0)
1707 return posix_error();
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00001708#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00001709
Fred Drake8cef4cf2000-06-28 16:40:38 +00001710 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00001711
Fred Drake8cef4cf2000-06-28 16:40:38 +00001712}
Thomas Wouters70c21a12000-07-14 14:28:33 +00001713#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00001714
1715#ifdef HAVE_FORKPTY
1716static char posix_forkpty__doc__[] =
1717"forkpty() -> (pid, master_fd)\n\
1718Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
1719Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
1720To both, return fd of newly opened pseudo-terminal.\n";
1721
1722static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001723posix_forkpty(PyObject *self, PyObject *args)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001724{
1725 int master_fd, pid;
1726
1727 if (!PyArg_ParseTuple(args, ":forkpty"))
1728 return NULL;
1729 pid = forkpty(&master_fd, NULL, NULL, NULL);
1730 if (pid == -1)
1731 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00001732 if (pid == 0)
1733 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00001734 return Py_BuildValue("(ii)", pid, master_fd);
1735}
1736#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001737
Guido van Rossumad0ee831995-03-01 10:34:45 +00001738#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001739static char posix_getegid__doc__[] =
1740"getegid() -> egid\n\
1741Return the current process's effective group id.";
1742
Barry Warsaw53699e91996-12-10 23:23:01 +00001743static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001744posix_getegid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001745{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001746 if (!PyArg_ParseTuple(args, ":getegid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001747 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001748 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001749}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001750#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001752
Guido van Rossumad0ee831995-03-01 10:34:45 +00001753#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001754static char posix_geteuid__doc__[] =
1755"geteuid() -> euid\n\
1756Return the current process's effective user id.";
1757
Barry Warsaw53699e91996-12-10 23:23:01 +00001758static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001759posix_geteuid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001760{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001761 if (!PyArg_ParseTuple(args, ":geteuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001762 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001763 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001764}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001765#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001766
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001767
Guido van Rossumad0ee831995-03-01 10:34:45 +00001768#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001769static char posix_getgid__doc__[] =
1770"getgid() -> gid\n\
1771Return the current process's group id.";
1772
Barry Warsaw53699e91996-12-10 23:23:01 +00001773static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001774posix_getgid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001775{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001776 if (!PyArg_ParseTuple(args, ":getgid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001777 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001778 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001779}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001780#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001781
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001782
1783static char posix_getpid__doc__[] =
1784"getpid() -> pid\n\
1785Return the current process id";
1786
Barry Warsaw53699e91996-12-10 23:23:01 +00001787static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001788posix_getpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001789{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001790 if (!PyArg_ParseTuple(args, ":getpid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001791 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001792 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001793}
1794
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001795
Fred Drakec9680921999-12-13 16:37:25 +00001796#ifdef HAVE_GETGROUPS
1797static char posix_getgroups__doc__[] = "\
1798getgroups() -> list of group IDs\n\
1799Return list of supplemental group IDs for the process.";
1800
1801static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001802posix_getgroups(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00001803{
1804 PyObject *result = NULL;
1805
1806 if (PyArg_ParseTuple(args, ":getgroups")) {
1807#ifdef NGROUPS_MAX
1808#define MAX_GROUPS NGROUPS_MAX
1809#else
1810 /* defined to be 16 on Solaris7, so this should be a small number */
1811#define MAX_GROUPS 64
1812#endif
1813 gid_t grouplist[MAX_GROUPS];
1814 int n;
1815
1816 n = getgroups(MAX_GROUPS, grouplist);
1817 if (n < 0)
1818 posix_error();
1819 else {
1820 result = PyList_New(n);
1821 if (result != NULL) {
1822 PyObject *o;
1823 int i;
1824 for (i = 0; i < n; ++i) {
1825 o = PyInt_FromLong((long)grouplist[i]);
1826 if (o == NULL) {
1827 Py_DECREF(result);
1828 result = NULL;
1829 break;
1830 }
1831 PyList_SET_ITEM(result, i, o);
1832 }
1833 }
1834 }
1835 }
1836 return result;
1837}
1838#endif
1839
Guido van Rossumb6775db1994-08-01 11:34:53 +00001840#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001841static char posix_getpgrp__doc__[] =
1842"getpgrp() -> pgrp\n\
1843Return the current process group id.";
1844
Barry Warsaw53699e91996-12-10 23:23:01 +00001845static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001846posix_getpgrp(PyObject *self, PyObject *args)
Guido van Rossum04814471991-06-04 20:23:49 +00001847{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001848 if (!PyArg_ParseTuple(args, ":getpgrp"))
Guido van Rossum04814471991-06-04 20:23:49 +00001849 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001850#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001851 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001852#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001853 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001854#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001855}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001856#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001857
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001858
Guido van Rossumb6775db1994-08-01 11:34:53 +00001859#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001860static char posix_setpgrp__doc__[] =
1861"setpgrp() -> None\n\
1862Make this process a session leader.";
1863
Barry Warsaw53699e91996-12-10 23:23:01 +00001864static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001865posix_setpgrp(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00001866{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001867 if (!PyArg_ParseTuple(args, ":setpgrp"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001868 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001869#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001870 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001871#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001872 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001873#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001874 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001875 Py_INCREF(Py_None);
1876 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001877}
1878
Guido van Rossumb6775db1994-08-01 11:34:53 +00001879#endif /* HAVE_SETPGRP */
1880
Guido van Rossumad0ee831995-03-01 10:34:45 +00001881#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001882static char posix_getppid__doc__[] =
1883"getppid() -> ppid\n\
1884Return the parent's process id.";
1885
Barry Warsaw53699e91996-12-10 23:23:01 +00001886static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001887posix_getppid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001888{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001889 if (!PyArg_ParseTuple(args, ":getppid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001890 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001891 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001892}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001893#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001894
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001895
Fred Drake12c6e2d1999-12-14 21:25:03 +00001896#ifdef HAVE_GETLOGIN
1897static char posix_getlogin__doc__[] = "\
1898getlogin() -> string\n\
1899Return the actual login name.";
1900
1901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001902posix_getlogin(PyObject *self, PyObject *args)
Fred Drake12c6e2d1999-12-14 21:25:03 +00001903{
1904 PyObject *result = NULL;
1905
1906 if (PyArg_ParseTuple(args, ":getlogin")) {
Fred Drakea30680b2000-12-06 21:24:28 +00001907 char *name;
1908 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00001909
Fred Drakea30680b2000-12-06 21:24:28 +00001910 errno = 0;
1911 name = getlogin();
1912 if (name == NULL) {
1913 if (errno)
1914 posix_error();
1915 else
1916 PyErr_SetString(PyExc_OSError,
Fred Drakee63544f2000-12-06 21:45:33 +00001917 "unable to determine login name");
Fred Drakea30680b2000-12-06 21:24:28 +00001918 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00001919 else
1920 result = PyString_FromString(name);
Fred Drakea30680b2000-12-06 21:24:28 +00001921 errno = old_errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00001922 }
1923 return result;
1924}
1925#endif
1926
Guido van Rossumad0ee831995-03-01 10:34:45 +00001927#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001928static char posix_getuid__doc__[] =
1929"getuid() -> uid\n\
1930Return the current process's user id.";
1931
Barry Warsaw53699e91996-12-10 23:23:01 +00001932static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001933posix_getuid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001934{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001935 if (!PyArg_ParseTuple(args, ":getuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001936 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001937 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001938}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001939#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001940
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001941
Guido van Rossumad0ee831995-03-01 10:34:45 +00001942#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001943static char posix_kill__doc__[] =
1944"kill(pid, sig) -> None\n\
1945Kill a process with a signal.";
1946
Barry Warsaw53699e91996-12-10 23:23:01 +00001947static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001948posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001949{
1950 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001951 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001952 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001953#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001954 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1955 APIRET rc;
1956 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001957 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001958
1959 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1960 APIRET rc;
1961 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001962 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001963
1964 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001965 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001966#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001967 if (kill(pid, sig) == -1)
1968 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001969#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001970 Py_INCREF(Py_None);
1971 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001972}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001973#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001974
Guido van Rossumc0125471996-06-28 18:55:32 +00001975#ifdef HAVE_PLOCK
1976
1977#ifdef HAVE_SYS_LOCK_H
1978#include <sys/lock.h>
1979#endif
1980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001981static char posix_plock__doc__[] =
1982"plock(op) -> None\n\
1983Lock program segments into memory.";
1984
Barry Warsaw53699e91996-12-10 23:23:01 +00001985static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001986posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00001987{
1988 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001989 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001990 return NULL;
1991 if (plock(op) == -1)
1992 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001993 Py_INCREF(Py_None);
1994 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001995}
1996#endif
1997
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001998
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001999#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002000static char posix_popen__doc__[] =
2001"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
2002Open a pipe to/from a command returning a file object.";
2003
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002004#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002005static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002006async_system(const char *command)
2007{
2008 char *p, errormsg[256], args[1024];
2009 RESULTCODES rcodes;
2010 APIRET rc;
2011 char *shell = getenv("COMSPEC");
2012 if (!shell)
2013 shell = "cmd";
2014
2015 strcpy(args, shell);
2016 p = &args[ strlen(args)+1 ];
2017 strcpy(p, "/c ");
2018 strcat(p, command);
2019 p += strlen(p) + 1;
2020 *p = '\0';
2021
2022 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002023 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002024 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002025 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002026 &rcodes, shell);
2027 return rc;
2028}
2029
Guido van Rossumd48f2521997-12-05 22:19:34 +00002030static FILE *
2031popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002032{
2033 HFILE rhan, whan;
2034 FILE *retfd = NULL;
2035 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2036
Guido van Rossumd48f2521997-12-05 22:19:34 +00002037 if (rc != NO_ERROR) {
2038 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002039 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002040 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002041
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002042 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2043 int oldfd = dup(1); /* Save STDOUT 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(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002047
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002048 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
2049 DosClose(whan); /* Close Now-Unused Pipe Write 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(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002053 }
2054
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002055 dup2(oldfd, 1); /* Reconnect STDOUT 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 STDOUT Handle */
2059 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002060
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002061 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
2062 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002063
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002064 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2065 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002066
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002067 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
2068 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002069
2070 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002071 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002072 }
2073
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002074 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
2075 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002076
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002077 close(oldfd); /* And Close Saved STDIN Handle */
2078 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002079
Guido van Rossumd48f2521997-12-05 22:19:34 +00002080 } else {
2081 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002082 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002083 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002084}
2085
2086static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002087posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002088{
2089 char *name;
2090 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00002091 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002092 FILE *fp;
2093 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002094 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002095 return NULL;
2096 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00002097 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002098 Py_END_ALLOW_THREADS
2099 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002100 return os2_error(err);
2101
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002102 f = PyFile_FromFile(fp, name, mode, fclose);
2103 if (f != NULL)
2104 PyFile_SetBufSize(f, bufsize);
2105 return f;
2106}
2107
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002108#elif defined(MS_WIN32)
2109
2110/*
2111 * Portable 'popen' replacement for Win32.
2112 *
2113 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
2114 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00002115 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002116 */
2117
2118#include <malloc.h>
2119#include <io.h>
2120#include <fcntl.h>
2121
2122/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
2123#define POPEN_1 1
2124#define POPEN_2 2
2125#define POPEN_3 3
2126#define POPEN_4 4
2127
2128static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002129static int _PyPclose(FILE *file);
2130
2131/*
2132 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00002133 * for use when retrieving the process exit code. See _PyPclose() below
2134 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002135 */
2136static PyObject *_PyPopenProcs = NULL;
2137
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002138
2139/* popen that works from a GUI.
2140 *
2141 * The result of this function is a pipe (file) connected to the
2142 * processes stdin or stdout, depending on the requested mode.
2143 */
2144
2145static PyObject *
2146posix_popen(PyObject *self, PyObject *args)
2147{
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002148 PyObject *f, *s;
2149 int tm = 0;
2150
2151 char *cmdstring;
2152 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002153 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002154 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002155 return NULL;
2156
2157 s = PyTuple_New(0);
2158
2159 if (*mode == 'r')
2160 tm = _O_RDONLY;
2161 else if (*mode != 'w') {
Fred Drake661ea262000-10-24 19:57:45 +00002162 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002163 return NULL;
2164 } else
2165 tm = _O_WRONLY;
2166
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002167 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00002168 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002169 return NULL;
2170 }
2171
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002172 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002173 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002174 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002175 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002176 else
2177 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
2178
2179 return f;
2180}
2181
2182/* Variation on win32pipe.popen
2183 *
2184 * The result of this function is a pipe (file) connected to the
2185 * process's stdin, and a pipe connected to the process's stdout.
2186 */
2187
2188static PyObject *
2189win32_popen2(PyObject *self, PyObject *args)
2190{
2191 PyObject *f;
2192 int tm=0;
2193
2194 char *cmdstring;
2195 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002196 int bufsize = -1;
2197 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002198 return NULL;
2199
2200 if (*mode == 't')
2201 tm = _O_TEXT;
2202 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00002203 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002204 return NULL;
2205 } else
2206 tm = _O_BINARY;
2207
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002208 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00002209 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002210 return NULL;
2211 }
2212
2213 f = _PyPopen(cmdstring, tm, POPEN_2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002214
2215 return f;
2216}
2217
2218/*
2219 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002220 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002221 * The result of this function is 3 pipes - the process's stdin,
2222 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002223 */
2224
2225static PyObject *
2226win32_popen3(PyObject *self, PyObject *args)
2227{
2228 PyObject *f;
2229 int tm = 0;
2230
2231 char *cmdstring;
2232 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002233 int bufsize = -1;
2234 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002235 return NULL;
2236
2237 if (*mode == 't')
2238 tm = _O_TEXT;
2239 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00002240 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002241 return NULL;
2242 } else
2243 tm = _O_BINARY;
2244
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002245 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00002246 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002247 return NULL;
2248 }
2249
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002250 f = _PyPopen(cmdstring, tm, POPEN_3);
2251
2252 return f;
2253}
2254
2255/*
2256 * Variation on win32pipe.popen
2257 *
2258 * The result of this function is 2 pipes - the processes stdin,
2259 * and stdout+stderr combined as a single pipe.
2260 */
2261
2262static PyObject *
2263win32_popen4(PyObject *self, PyObject *args)
2264{
2265 PyObject *f;
2266 int tm = 0;
2267
2268 char *cmdstring;
2269 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002270 int bufsize = -1;
2271 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002272 return NULL;
2273
2274 if (*mode == 't')
2275 tm = _O_TEXT;
2276 else if (*mode != 'b') {
Fred Drake661ea262000-10-24 19:57:45 +00002277 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002278 return NULL;
2279 } else
2280 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002281
2282 if (bufsize != -1) {
Fred Drake661ea262000-10-24 19:57:45 +00002283 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002284 return NULL;
2285 }
2286
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002287 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002288
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002289 return f;
2290}
2291
2292static int
Mark Hammondb37a3732000-08-14 04:47:33 +00002293_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002294 HANDLE hStdin,
2295 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00002296 HANDLE hStderr,
2297 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002298{
2299 PROCESS_INFORMATION piProcInfo;
2300 STARTUPINFO siStartInfo;
2301 char *s1,*s2, *s3 = " /c ";
2302 const char *szConsoleSpawn = "w9xpopen.exe \"";
2303 int i;
2304 int x;
2305
2306 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
2307 s1 = (char *)_alloca(i);
2308 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
2309 return x;
2310 if (GetVersion() < 0x80000000) {
2311 /*
2312 * NT/2000
2313 */
2314 x = i + strlen(s3) + strlen(cmdstring) + 1;
2315 s2 = (char *)_alloca(x);
2316 ZeroMemory(s2, x);
2317 sprintf(s2, "%s%s%s", s1, s3, cmdstring);
2318 }
2319 else {
2320 /*
2321 * Oh gag, we're on Win9x. Use the workaround listed in
2322 * KB: Q150956
2323 */
2324 char modulepath[256];
2325 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
2326 for (i = x = 0; modulepath[i]; i++)
2327 if (modulepath[i] == '\\')
2328 x = i+1;
2329 modulepath[x] = '\0';
2330 x = i + strlen(s3) + strlen(cmdstring) + 1 +
2331 strlen(modulepath) +
2332 strlen(szConsoleSpawn) + 1;
2333 s2 = (char *)_alloca(x);
2334 ZeroMemory(s2, x);
2335 sprintf(
2336 s2,
2337 "%s%s%s%s%s\"",
2338 modulepath,
2339 szConsoleSpawn,
2340 s1,
2341 s3,
2342 cmdstring);
2343 }
2344 }
2345
2346 /* Could be an else here to try cmd.exe / command.com in the path
2347 Now we'll just error out.. */
2348 else
2349 return -1;
2350
2351 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
2352 siStartInfo.cb = sizeof(STARTUPINFO);
2353 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
2354 siStartInfo.hStdInput = hStdin;
2355 siStartInfo.hStdOutput = hStdout;
2356 siStartInfo.hStdError = hStderr;
2357 siStartInfo.wShowWindow = SW_HIDE;
2358
2359 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002360 s2,
2361 NULL,
2362 NULL,
2363 TRUE,
2364 CREATE_NEW_CONSOLE,
2365 NULL,
2366 NULL,
2367 &siStartInfo,
2368 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002369 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002370 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002371
Mark Hammondb37a3732000-08-14 04:47:33 +00002372 /* Return process handle */
2373 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002374 return TRUE;
2375 }
2376 return FALSE;
2377}
2378
2379/* The following code is based off of KB: Q190351 */
2380
2381static PyObject *
2382_PyPopen(char *cmdstring, int mode, int n)
2383{
2384 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
2385 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00002386 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002387
2388 SECURITY_ATTRIBUTES saAttr;
2389 BOOL fSuccess;
2390 int fd1, fd2, fd3;
2391 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00002392 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002393 PyObject *f;
2394
2395 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
2396 saAttr.bInheritHandle = TRUE;
2397 saAttr.lpSecurityDescriptor = NULL;
2398
2399 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
2400 return win32_error("CreatePipe", NULL);
2401
2402 /* Create new output read handle and the input write handle. Set
2403 * the inheritance properties to FALSE. Otherwise, the child inherits
2404 * the these handles; resulting in non-closeable handles to the pipes
2405 * being created. */
2406 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002407 GetCurrentProcess(), &hChildStdinWrDup, 0,
2408 FALSE,
2409 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002410 if (!fSuccess)
2411 return win32_error("DuplicateHandle", NULL);
2412
2413 /* Close the inheritable version of ChildStdin
2414 that we're using. */
2415 CloseHandle(hChildStdinWr);
2416
2417 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
2418 return win32_error("CreatePipe", NULL);
2419
2420 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002421 GetCurrentProcess(), &hChildStdoutRdDup, 0,
2422 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002423 if (!fSuccess)
2424 return win32_error("DuplicateHandle", NULL);
2425
2426 /* Close the inheritable version of ChildStdout
2427 that we're using. */
2428 CloseHandle(hChildStdoutRd);
2429
2430 if (n != POPEN_4) {
2431 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
2432 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002433 fSuccess = DuplicateHandle(GetCurrentProcess(),
2434 hChildStderrRd,
2435 GetCurrentProcess(),
2436 &hChildStderrRdDup, 0,
2437 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002438 if (!fSuccess)
2439 return win32_error("DuplicateHandle", NULL);
2440 /* Close the inheritable version of ChildStdErr that we're using. */
2441 CloseHandle(hChildStderrRd);
2442 }
2443
2444 switch (n) {
2445 case POPEN_1:
2446 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
2447 case _O_WRONLY | _O_TEXT:
2448 /* Case for writing to child Stdin in text mode. */
2449 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2450 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002451 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002452 PyFile_SetBufSize(f, 0);
2453 /* We don't care about these pipes anymore, so close them. */
2454 CloseHandle(hChildStdoutRdDup);
2455 CloseHandle(hChildStderrRdDup);
2456 break;
2457
2458 case _O_RDONLY | _O_TEXT:
2459 /* Case for reading from child Stdout in text mode. */
2460 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2461 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002462 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002463 PyFile_SetBufSize(f, 0);
2464 /* We don't care about these pipes anymore, so close them. */
2465 CloseHandle(hChildStdinWrDup);
2466 CloseHandle(hChildStderrRdDup);
2467 break;
2468
2469 case _O_RDONLY | _O_BINARY:
2470 /* Case for readinig from child Stdout in binary mode. */
2471 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2472 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002473 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002474 PyFile_SetBufSize(f, 0);
2475 /* We don't care about these pipes anymore, so close them. */
2476 CloseHandle(hChildStdinWrDup);
2477 CloseHandle(hChildStderrRdDup);
2478 break;
2479
2480 case _O_WRONLY | _O_BINARY:
2481 /* Case for writing to child Stdin in binary mode. */
2482 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2483 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002484 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002485 PyFile_SetBufSize(f, 0);
2486 /* We don't care about these pipes anymore, so close them. */
2487 CloseHandle(hChildStdoutRdDup);
2488 CloseHandle(hChildStderrRdDup);
2489 break;
2490 }
Mark Hammondb37a3732000-08-14 04:47:33 +00002491 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002492 break;
2493
2494 case POPEN_2:
2495 case POPEN_4:
2496 {
2497 char *m1, *m2;
2498 PyObject *p1, *p2;
2499
2500 if (mode && _O_TEXT) {
2501 m1 = "r";
2502 m2 = "w";
2503 } else {
2504 m1 = "rb";
2505 m2 = "wb";
2506 }
2507
2508 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2509 f1 = _fdopen(fd1, m2);
2510 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2511 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002512 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002513 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00002514 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002515 PyFile_SetBufSize(p2, 0);
2516
2517 if (n != 4)
2518 CloseHandle(hChildStderrRdDup);
2519
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002520 f = Py_BuildValue("OO",p1,p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00002521 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002522 break;
2523 }
2524
2525 case POPEN_3:
2526 {
2527 char *m1, *m2;
2528 PyObject *p1, *p2, *p3;
2529
2530 if (mode && _O_TEXT) {
2531 m1 = "r";
2532 m2 = "w";
2533 } else {
2534 m1 = "rb";
2535 m2 = "wb";
2536 }
2537
2538 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2539 f1 = _fdopen(fd1, m2);
2540 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2541 f2 = _fdopen(fd2, m1);
2542 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
2543 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002544 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00002545 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
2546 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002547 PyFile_SetBufSize(p1, 0);
2548 PyFile_SetBufSize(p2, 0);
2549 PyFile_SetBufSize(p3, 0);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002550 f = Py_BuildValue("OOO",p1,p2,p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00002551 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002552 break;
2553 }
2554 }
2555
2556 if (n == POPEN_4) {
2557 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002558 hChildStdinRd,
2559 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00002560 hChildStdoutWr,
2561 &hProcess))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002562 return win32_error("CreateProcess", NULL);
2563 }
2564 else {
2565 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002566 hChildStdinRd,
2567 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00002568 hChildStderrWr,
2569 &hProcess))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002570 return win32_error("CreateProcess", NULL);
2571 }
2572
Mark Hammondb37a3732000-08-14 04:47:33 +00002573 /*
2574 * Insert the files we've created into the process dictionary
2575 * all referencing the list with the process handle and the
2576 * initial number of files (see description below in _PyPclose).
2577 * Since if _PyPclose later tried to wait on a process when all
2578 * handles weren't closed, it could create a deadlock with the
2579 * child, we spend some energy here to try to ensure that we
2580 * either insert all file handles into the dictionary or none
2581 * at all. It's a little clumsy with the various popen modes
2582 * and variable number of files involved.
2583 */
2584 if (!_PyPopenProcs) {
2585 _PyPopenProcs = PyDict_New();
2586 }
2587
2588 if (_PyPopenProcs) {
2589 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
2590 int ins_rc[3];
2591
2592 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
2593 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
2594
2595 procObj = PyList_New(2);
2596 hProcessObj = PyLong_FromVoidPtr(hProcess);
2597 intObj = PyInt_FromLong(file_count);
2598
2599 if (procObj && hProcessObj && intObj) {
2600 PyList_SetItem(procObj,0,hProcessObj);
2601 PyList_SetItem(procObj,1,intObj);
2602
2603 fileObj[0] = PyLong_FromVoidPtr(f1);
2604 if (fileObj[0]) {
2605 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
2606 fileObj[0],
2607 procObj);
2608 }
2609 if (file_count >= 2) {
2610 fileObj[1] = PyLong_FromVoidPtr(f2);
2611 if (fileObj[1]) {
2612 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
2613 fileObj[1],
2614 procObj);
2615 }
2616 }
2617 if (file_count >= 3) {
2618 fileObj[2] = PyLong_FromVoidPtr(f3);
2619 if (fileObj[2]) {
2620 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
2621 fileObj[2],
2622 procObj);
2623 }
2624 }
2625
2626 if (ins_rc[0] < 0 || !fileObj[0] ||
2627 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
2628 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
2629 /* Something failed - remove any dictionary
2630 * entries that did make it.
2631 */
2632 if (!ins_rc[0] && fileObj[0]) {
2633 PyDict_DelItem(_PyPopenProcs,
2634 fileObj[0]);
2635 }
2636 if (!ins_rc[1] && fileObj[1]) {
2637 PyDict_DelItem(_PyPopenProcs,
2638 fileObj[1]);
2639 }
2640 if (!ins_rc[2] && fileObj[2]) {
2641 PyDict_DelItem(_PyPopenProcs,
2642 fileObj[2]);
2643 }
2644 }
2645 }
2646
2647 /*
2648 * Clean up our localized references for the dictionary keys
2649 * and value since PyDict_SetItem will Py_INCREF any copies
2650 * that got placed in the dictionary.
2651 */
2652 Py_XDECREF(procObj);
2653 Py_XDECREF(fileObj[0]);
2654 Py_XDECREF(fileObj[1]);
2655 Py_XDECREF(fileObj[2]);
2656 }
2657
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002658 /* Child is launched. Close the parents copy of those pipe
2659 * handles that only the child should have open. You need to
2660 * make sure that no handles to the write end of the output pipe
2661 * are maintained in this process or else the pipe will not close
2662 * when the child process exits and the ReadFile will hang. */
2663
2664 if (!CloseHandle(hChildStdinRd))
2665 return win32_error("CloseHandle", NULL);
2666
2667 if (!CloseHandle(hChildStdoutWr))
2668 return win32_error("CloseHandle", NULL);
2669
2670 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
2671 return win32_error("CloseHandle", NULL);
2672
2673 return f;
2674}
Fredrik Lundh56055a42000-07-23 19:47:12 +00002675
2676/*
2677 * Wrapper for fclose() to use for popen* files, so we can retrieve the
2678 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00002679 *
2680 * This function uses the _PyPopenProcs dictionary in order to map the
2681 * input file pointer to information about the process that was
2682 * originally created by the popen* call that created the file pointer.
2683 * The dictionary uses the file pointer as a key (with one entry
2684 * inserted for each file returned by the original popen* call) and a
2685 * single list object as the value for all files from a single call.
2686 * The list object contains the Win32 process handle at [0], and a file
2687 * count at [1], which is initialized to the total number of file
2688 * handles using that list.
2689 *
2690 * This function closes whichever handle it is passed, and decrements
2691 * the file count in the dictionary for the process handle pointed to
2692 * by this file. On the last close (when the file count reaches zero),
2693 * this function will wait for the child process and then return its
2694 * exit code as the result of the close() operation. This permits the
2695 * files to be closed in any order - it is always the close() of the
2696 * final handle that will return the exit code.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002697 */
Tim Peters736aa322000-09-01 06:51:24 +00002698
2699 /* RED_FLAG 31-Aug-2000 Tim
2700 * This is always called (today!) between a pair of
2701 * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS
2702 * macros. So the thread running this has no valid thread state, as
2703 * far as Python is concerned. However, this calls some Python API
2704 * functions that cannot be called safely without a valid thread
2705 * state, in particular PyDict_GetItem.
2706 * As a temporary hack (although it may last for years ...), we
2707 * *rely* on not having a valid thread state in this function, in
2708 * order to create our own "from scratch".
2709 * This will deadlock if _PyPclose is ever called by a thread
2710 * holding the global lock.
2711 */
2712
Fredrik Lundh56055a42000-07-23 19:47:12 +00002713static int _PyPclose(FILE *file)
2714{
Fredrik Lundh20318932000-07-26 17:29:12 +00002715 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002716 DWORD exit_code;
2717 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00002718 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
2719 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00002720#ifdef WITH_THREAD
2721 PyInterpreterState* pInterpreterState;
2722 PyThreadState* pThreadState;
2723#endif
2724
Fredrik Lundh20318932000-07-26 17:29:12 +00002725 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00002726 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00002727 */
2728 result = fclose(file);
2729
Tim Peters736aa322000-09-01 06:51:24 +00002730#ifdef WITH_THREAD
2731 /* Bootstrap a valid thread state into existence. */
2732 pInterpreterState = PyInterpreterState_New();
2733 if (!pInterpreterState) {
2734 /* Well, we're hosed now! We don't have a thread
2735 * state, so can't call a nice error routine, or raise
2736 * an exception. Just die.
2737 */
2738 Py_FatalError("unable to allocate interpreter state "
Fred Drake661ea262000-10-24 19:57:45 +00002739 "when closing popen object");
Tim Peters736aa322000-09-01 06:51:24 +00002740 return -1; /* unreachable */
2741 }
2742 pThreadState = PyThreadState_New(pInterpreterState);
2743 if (!pThreadState) {
2744 Py_FatalError("unable to allocate thread state "
Fred Drake661ea262000-10-24 19:57:45 +00002745 "when closing popen object");
Tim Peters736aa322000-09-01 06:51:24 +00002746 return -1; /* unreachable */
2747 }
2748 /* Grab the global lock. Note that this will deadlock if the
2749 * current thread already has the lock! (see RED_FLAG comments
2750 * before this function)
2751 */
2752 PyEval_RestoreThread(pThreadState);
2753#endif
2754
Fredrik Lundh56055a42000-07-23 19:47:12 +00002755 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00002756 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
2757 (procObj = PyDict_GetItem(_PyPopenProcs,
2758 fileObj)) != NULL &&
2759 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
2760 (intObj = PyList_GetItem(procObj,1)) != NULL) {
2761
2762 hProcess = PyLong_AsVoidPtr(hProcessObj);
2763 file_count = PyInt_AsLong(intObj);
2764
2765 if (file_count > 1) {
2766 /* Still other files referencing process */
2767 file_count--;
2768 PyList_SetItem(procObj,1,
2769 PyInt_FromLong(file_count));
2770 } else {
2771 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00002772 if (result != EOF &&
2773 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
2774 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00002775 /* Possible truncation here in 16-bit environments, but
2776 * real exit codes are just the lower byte in any event.
2777 */
2778 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002779 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00002780 /* Indicate failure - this will cause the file object
2781 * to raise an I/O error and translate the last Win32
2782 * error code from errno. We do have a problem with
2783 * last errors that overlap the normal errno table,
2784 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002785 */
Fredrik Lundh20318932000-07-26 17:29:12 +00002786 if (result != EOF) {
2787 /* If the error wasn't from the fclose(), then
2788 * set errno for the file object error handling.
2789 */
2790 errno = GetLastError();
2791 }
2792 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002793 }
2794
2795 /* Free up the native handle at this point */
2796 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00002797 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00002798
Mark Hammondb37a3732000-08-14 04:47:33 +00002799 /* Remove this file pointer from dictionary */
2800 PyDict_DelItem(_PyPopenProcs, fileObj);
2801
2802 if (PyDict_Size(_PyPopenProcs) == 0) {
2803 Py_DECREF(_PyPopenProcs);
2804 _PyPopenProcs = NULL;
2805 }
2806
2807 } /* if object retrieval ok */
2808
2809 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002810 } /* if _PyPopenProcs */
2811
Tim Peters736aa322000-09-01 06:51:24 +00002812#ifdef WITH_THREAD
2813 /* Tear down the thread & interpreter states.
2814 * Note that interpreter state clear & delete functions automatically
Tim Peters9acdd3a2000-09-01 19:26:36 +00002815 * call the thread clear & delete functions, and indeed insist on
2816 * doing that themselves. The lock must be held during the clear, but
2817 * need not be held during the delete.
Tim Peters736aa322000-09-01 06:51:24 +00002818 */
2819 PyInterpreterState_Clear(pInterpreterState);
2820 PyEval_ReleaseThread(pThreadState);
2821 PyInterpreterState_Delete(pInterpreterState);
2822#endif
2823
Fredrik Lundh56055a42000-07-23 19:47:12 +00002824 return result;
2825}
Tim Peters9acdd3a2000-09-01 19:26:36 +00002826
2827#else /* which OS? */
Barry Warsaw53699e91996-12-10 23:23:01 +00002828static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002829posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00002830{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002831 char *name;
2832 char *mode = "r";
2833 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00002834 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002835 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002836 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00002837 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002838 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002839 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002840 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00002841 if (fp == NULL)
2842 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002843 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002844 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002845 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002846 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00002847}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002848#endif
2849
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002850#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00002851
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002852
Guido van Rossumb6775db1994-08-01 11:34:53 +00002853#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002854static char posix_setuid__doc__[] =
2855"setuid(uid) -> None\n\
2856Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00002857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002858posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002859{
2860 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002861 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002862 return NULL;
2863 if (setuid(uid) < 0)
2864 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002865 Py_INCREF(Py_None);
2866 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002867}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002868#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002870
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00002871#ifdef HAVE_SETEUID
2872static char posix_seteuid__doc__[] =
2873"seteuid(uid) -> None\n\
2874Set the current process's effective user id.";
2875static PyObject *
2876posix_seteuid (PyObject *self, PyObject *args)
2877{
2878 int euid;
2879 if (!PyArg_ParseTuple(args, "i", &euid)) {
2880 return NULL;
2881 } else if (seteuid(euid) < 0) {
2882 return posix_error();
2883 } else {
2884 Py_INCREF(Py_None);
2885 return Py_None;
2886 }
2887}
2888#endif /* HAVE_SETEUID */
2889
2890#ifdef HAVE_SETEGID
2891static char posix_setegid__doc__[] =
2892"setegid(gid) -> None\n\
2893Set the current process's effective group id.";
2894static PyObject *
2895posix_setegid (PyObject *self, PyObject *args)
2896{
2897 int egid;
2898 if (!PyArg_ParseTuple(args, "i", &egid)) {
2899 return NULL;
2900 } else if (setegid(egid) < 0) {
2901 return posix_error();
2902 } else {
2903 Py_INCREF(Py_None);
2904 return Py_None;
2905 }
2906}
2907#endif /* HAVE_SETEGID */
2908
2909#ifdef HAVE_SETREUID
2910static char posix_setreuid__doc__[] =
2911"seteuid(ruid, euid) -> None\n\
2912Set the current process's real and effective user ids.";
2913static PyObject *
2914posix_setreuid (PyObject *self, PyObject *args)
2915{
2916 int ruid, euid;
2917 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
2918 return NULL;
2919 } else if (setreuid(ruid, euid) < 0) {
2920 return posix_error();
2921 } else {
2922 Py_INCREF(Py_None);
2923 return Py_None;
2924 }
2925}
2926#endif /* HAVE_SETREUID */
2927
2928#ifdef HAVE_SETREGID
2929static char posix_setregid__doc__[] =
2930"setegid(rgid, egid) -> None\n\
2931Set the current process's real and effective group ids.";
2932static PyObject *
2933posix_setregid (PyObject *self, PyObject *args)
2934{
2935 int rgid, egid;
2936 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
2937 return NULL;
2938 } else if (setregid(rgid, egid) < 0) {
2939 return posix_error();
2940 } else {
2941 Py_INCREF(Py_None);
2942 return Py_None;
2943 }
2944}
2945#endif /* HAVE_SETREGID */
2946
Guido van Rossumb6775db1994-08-01 11:34:53 +00002947#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948static char posix_setgid__doc__[] =
2949"setgid(gid) -> None\n\
2950Set the current process's group id.";
2951
Barry Warsaw53699e91996-12-10 23:23:01 +00002952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002953posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002954{
2955 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002956 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002957 return NULL;
2958 if (setgid(gid) < 0)
2959 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002960 Py_INCREF(Py_None);
2961 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002962}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002963#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002964
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002965
Guido van Rossumb6775db1994-08-01 11:34:53 +00002966#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002967static char posix_waitpid__doc__[] =
2968"waitpid(pid, options) -> (pid, status)\n\
Guido van Rossumf377d572000-12-12 00:37:58 +00002969Wait for completion of a given child process.";
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002970
Barry Warsaw53699e91996-12-10 23:23:01 +00002971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002972posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002973{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002974 int pid, options;
2975#ifdef UNION_WAIT
2976 union wait status;
2977#define status_i (status.w_status)
2978#else
2979 int status;
2980#define status_i status
2981#endif
2982 status_i = 0;
2983
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002984 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002985 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002986 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002987#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002988 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002989#else
2990 pid = waitpid(pid, &status, options);
2991#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002992 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002993 if (pid == -1)
2994 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002995 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002996 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002997}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002998#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002999
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003000
Guido van Rossumad0ee831995-03-01 10:34:45 +00003001#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003002static char posix_wait__doc__[] =
3003"wait() -> (pid, status)\n\
3004Wait for completion of a child process.";
3005
Barry Warsaw53699e91996-12-10 23:23:01 +00003006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003007posix_wait(PyObject *self, PyObject *args)
Guido van Rossum21803b81992-08-09 12:55:27 +00003008{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003009 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003010#ifdef UNION_WAIT
3011 union wait status;
3012#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003013#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003014 int status;
3015#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003016#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003017 if (!PyArg_ParseTuple(args, ":wait"))
3018 return NULL;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003019 status_i = 0;
3020 Py_BEGIN_ALLOW_THREADS
3021 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00003022 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00003023 if (pid == -1)
3024 return posix_error();
3025 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003026 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003027#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00003028}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003029#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003031
3032static char posix_lstat__doc__[] =
3033"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
3034Like stat(path), but do not follow symbolic links.";
3035
Barry Warsaw53699e91996-12-10 23:23:01 +00003036static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003037posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003038{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003039#ifdef HAVE_LSTAT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003040 return posix_do_stat(self, args, "s:lstat", lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003041#else /* !HAVE_LSTAT */
Fred Drake699f3522000-06-29 21:12:41 +00003042 return posix_do_stat(self, args, "s:lstat", STAT);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003043#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003044}
3045
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003046
Guido van Rossumb6775db1994-08-01 11:34:53 +00003047#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003048static char posix_readlink__doc__[] =
3049"readlink(path) -> path\n\
3050Return a string representing the path to which the symbolic link points.";
3051
Barry Warsaw53699e91996-12-10 23:23:01 +00003052static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003053posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003054{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003055 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003056 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003057 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003058 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003059 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003060 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00003061 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00003062 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003063 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00003064 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00003065 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003066}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003067#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003069
Guido van Rossumb6775db1994-08-01 11:34:53 +00003070#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003071static char posix_symlink__doc__[] =
3072"symlink(src, dst) -> None\n\
3073Create a symbolic link.";
3074
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003075static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003076posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003077{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003078 return posix_2str(args, "ss:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003079}
3080#endif /* HAVE_SYMLINK */
3081
3082
3083#ifdef HAVE_TIMES
3084#ifndef HZ
3085#define HZ 60 /* Universal constant :-) */
3086#endif /* HZ */
3087
Guido van Rossumd48f2521997-12-05 22:19:34 +00003088#if defined(PYCC_VACPP) && defined(PYOS_OS2)
3089static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00003090system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003091{
3092 ULONG value = 0;
3093
3094 Py_BEGIN_ALLOW_THREADS
3095 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
3096 Py_END_ALLOW_THREADS
3097
3098 return value;
3099}
3100
3101static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003102posix_times(PyObject *self, PyObject *args)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003103{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003104 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossumd48f2521997-12-05 22:19:34 +00003105 return NULL;
3106
3107 /* Currently Only Uptime is Provided -- Others Later */
3108 return Py_BuildValue("ddddd",
3109 (double)0 /* t.tms_utime / HZ */,
3110 (double)0 /* t.tms_stime / HZ */,
3111 (double)0 /* t.tms_cutime / HZ */,
3112 (double)0 /* t.tms_cstime / HZ */,
3113 (double)system_uptime() / 1000);
3114}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003115#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003116static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003117posix_times(PyObject *self, PyObject *args)
Guido van Rossum22db57e1992-04-05 14:25:30 +00003118{
3119 struct tms t;
3120 clock_t c;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003121 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum22db57e1992-04-05 14:25:30 +00003122 return NULL;
3123 errno = 0;
3124 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00003125 if (c == (clock_t) -1)
3126 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003127 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00003128 (double)t.tms_utime / HZ,
3129 (double)t.tms_stime / HZ,
3130 (double)t.tms_cutime / HZ,
3131 (double)t.tms_cstime / HZ,
3132 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00003133}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003134#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003135#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003136
3137
Guido van Rossum87755a21996-09-07 00:59:43 +00003138#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003139#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00003140static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003141posix_times(PyObject *self, PyObject *args)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003142{
3143 FILETIME create, exit, kernel, user;
3144 HANDLE hProc;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003145 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003146 return NULL;
3147 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003148 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
3149 /* The fields of a FILETIME structure are the hi and lo part
3150 of a 64-bit value expressed in 100 nanosecond units.
3151 1e7 is one second in such units; 1e-7 the inverse.
3152 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
3153 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003154 return Py_BuildValue(
3155 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003156 (double)(kernel.dwHighDateTime*429.4967296 +
3157 kernel.dwLowDateTime*1e-7),
3158 (double)(user.dwHighDateTime*429.4967296 +
3159 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00003160 (double)0,
3161 (double)0,
3162 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003163}
Guido van Rossum8d665e61996-06-26 18:22:49 +00003164#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003165
3166#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00003167static char posix_times__doc__[] =
3168"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
3169Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003170#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003172
Guido van Rossumb6775db1994-08-01 11:34:53 +00003173#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003174static char posix_setsid__doc__[] =
3175"setsid() -> None\n\
3176Call the system call setsid().";
3177
Barry Warsaw53699e91996-12-10 23:23:01 +00003178static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003179posix_setsid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003180{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003181 if (!PyArg_ParseTuple(args, ":setsid"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003182 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003183 if (setsid() < 0)
3184 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003185 Py_INCREF(Py_None);
3186 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003187}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003188#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003189
Guido van Rossumb6775db1994-08-01 11:34:53 +00003190#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003191static char posix_setpgid__doc__[] =
3192"setpgid(pid, pgrp) -> None\n\
3193Call the system call setpgid().";
3194
Barry Warsaw53699e91996-12-10 23:23:01 +00003195static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003196posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003197{
3198 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003199 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003200 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003201 if (setpgid(pid, pgrp) < 0)
3202 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003203 Py_INCREF(Py_None);
3204 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003205}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003206#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003208
Guido van Rossumb6775db1994-08-01 11:34:53 +00003209#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210static char posix_tcgetpgrp__doc__[] =
3211"tcgetpgrp(fd) -> pgid\n\
3212Return 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_tcgetpgrp(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, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003219 return NULL;
3220 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003221 if (pgid < 0)
3222 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003223 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00003224}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003225#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00003226
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003227
Guido van Rossumb6775db1994-08-01 11:34:53 +00003228#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003229static char posix_tcsetpgrp__doc__[] =
3230"tcsetpgrp(fd, pgid) -> None\n\
3231Set the process group associated with the terminal given by a fd.";
3232
Barry Warsaw53699e91996-12-10 23:23:01 +00003233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003234posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00003235{
3236 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003237 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003238 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003239 if (tcsetpgrp(fd, pgid) < 0)
3240 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00003241 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00003242 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00003243}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003244#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00003245
Guido van Rossum687dd131993-05-17 08:34:16 +00003246/* Functions acting on file descriptors */
3247
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003248static char posix_open__doc__[] =
3249"open(filename, flag [, mode=0777]) -> fd\n\
3250Open a file (for low level IO).";
3251
Barry Warsaw53699e91996-12-10 23:23:01 +00003252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003253posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003254{
3255 char *file;
3256 int flag;
3257 int mode = 0777;
3258 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003259 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
3260 return NULL;
3261
Barry Warsaw53699e91996-12-10 23:23:01 +00003262 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003263 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003264 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003265 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00003266 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00003267 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003268}
3269
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003270
3271static char posix_close__doc__[] =
3272"close(fd) -> None\n\
3273Close a file descriptor (for low level IO).";
3274
Barry Warsaw53699e91996-12-10 23:23:01 +00003275static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003276posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003277{
3278 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003279 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003280 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003281 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003282 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003283 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003284 if (res < 0)
3285 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003286 Py_INCREF(Py_None);
3287 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003288}
3289
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003290
3291static char posix_dup__doc__[] =
3292"dup(fd) -> fd2\n\
3293Return a duplicate of a file descriptor.";
3294
Barry Warsaw53699e91996-12-10 23:23:01 +00003295static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003296posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003297{
3298 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003299 if (!PyArg_ParseTuple(args, "i:dup", &fd))
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 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003303 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003304 if (fd < 0)
3305 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003306 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003307}
3308
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003309
3310static char posix_dup2__doc__[] =
3311"dup2(fd, fd2) -> None\n\
3312Duplicate file descriptor.";
3313
Barry Warsaw53699e91996-12-10 23:23:01 +00003314static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003315posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003316{
3317 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003318 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00003319 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003320 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003321 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00003322 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003323 if (res < 0)
3324 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003325 Py_INCREF(Py_None);
3326 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003327}
3328
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003329
3330static char posix_lseek__doc__[] =
3331"lseek(fd, pos, how) -> newpos\n\
3332Set the current position of a file descriptor.";
3333
Barry Warsaw53699e91996-12-10 23:23:01 +00003334static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003335posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003336{
3337 int fd, how;
Fred Drake699f3522000-06-29 21:12:41 +00003338#ifdef MS_WIN64
3339 LONG_LONG pos, res;
3340#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00003341 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00003342#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003343 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003344 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00003345 return NULL;
3346#ifdef SEEK_SET
3347 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
3348 switch (how) {
3349 case 0: how = SEEK_SET; break;
3350 case 1: how = SEEK_CUR; break;
3351 case 2: how = SEEK_END; break;
3352 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003353#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003354
3355#if !defined(HAVE_LARGEFILE_SUPPORT)
3356 pos = PyInt_AsLong(posobj);
3357#else
3358 pos = PyLong_Check(posobj) ?
3359 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
3360#endif
3361 if (PyErr_Occurred())
3362 return NULL;
3363
Barry Warsaw53699e91996-12-10 23:23:01 +00003364 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003365#ifdef MS_WIN64
3366 res = _lseeki64(fd, pos, how);
3367#else
Guido van Rossum687dd131993-05-17 08:34:16 +00003368 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00003369#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003370 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003371 if (res < 0)
3372 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00003373
3374#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00003375 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00003376#else
3377 return PyLong_FromLongLong(res);
3378#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003379}
3380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003381
3382static char posix_read__doc__[] =
3383"read(fd, buffersize) -> string\n\
3384Read a file descriptor.";
3385
Barry Warsaw53699e91996-12-10 23:23:01 +00003386static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003387posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003388{
Guido van Rossum8bac5461996-06-11 18:38:48 +00003389 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00003390 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003391 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003392 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003393 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003394 if (buffer == NULL)
3395 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003396 Py_BEGIN_ALLOW_THREADS
3397 n = read(fd, PyString_AsString(buffer), size);
3398 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00003399 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003400 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00003401 return posix_error();
3402 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00003403 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00003404 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00003405 return buffer;
3406}
3407
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003408
3409static char posix_write__doc__[] =
3410"write(fd, string) -> byteswritten\n\
3411Write a string to a file descriptor.";
3412
Barry Warsaw53699e91996-12-10 23:23:01 +00003413static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003414posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003415{
3416 int fd, size;
3417 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003418 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003419 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003420 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003421 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00003422 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003423 if (size < 0)
3424 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003425 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003426}
3427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003428
3429static char posix_fstat__doc__[]=
3430"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
3431Like stat(), but for an open file descriptor.";
3432
Barry Warsaw53699e91996-12-10 23:23:01 +00003433static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003434posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003435{
3436 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00003437 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00003438 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003439 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003440 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003441 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003442 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00003443 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003444 if (res != 0)
3445 return posix_error();
Fred Drake699f3522000-06-29 21:12:41 +00003446
3447 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00003448}
3449
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003450
3451static char posix_fdopen__doc__[] =
3452"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
3453Return an open file object connected to a file descriptor.";
3454
Barry Warsaw53699e91996-12-10 23:23:01 +00003455static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003456posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003457{
Guido van Rossum687dd131993-05-17 08:34:16 +00003458 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003459 char *mode = "r";
3460 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00003461 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00003462 PyObject *f;
3463 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00003464 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003465
Barry Warsaw53699e91996-12-10 23:23:01 +00003466 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003467 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003468 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003469 if (fp == NULL)
3470 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003471 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003472 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00003473 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003474 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00003475}
3476
Skip Montanaro1517d842000-07-19 14:34:14 +00003477static char posix_isatty__doc__[] =
3478"isatty(fd) -> Boolean\n\
3479Return true if the file descriptor 'fd' is an open file descriptor\n\
Thomas Wouters12e15952000-10-03 16:54:24 +00003480connected to the slave end of a terminal.";
Skip Montanaro1517d842000-07-19 14:34:14 +00003481
3482static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00003483posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00003484{
3485 int fd;
3486 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
3487 return NULL;
3488 return Py_BuildValue("i", isatty(fd));
3489}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003490
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003491#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003492static char posix_pipe__doc__[] =
3493"pipe() -> (read_end, write_end)\n\
3494Create a pipe.";
3495
Barry Warsaw53699e91996-12-10 23:23:01 +00003496static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003497posix_pipe(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003498{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003499#if defined(PYOS_OS2)
3500 HFILE read, write;
3501 APIRET rc;
3502
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003503 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003504 return NULL;
3505
3506 Py_BEGIN_ALLOW_THREADS
3507 rc = DosCreatePipe( &read, &write, 4096);
3508 Py_END_ALLOW_THREADS
3509 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003510 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003511
3512 return Py_BuildValue("(ii)", read, write);
3513#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00003514#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00003515 int fds[2];
3516 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003517 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum687dd131993-05-17 08:34:16 +00003518 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003519 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003520 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00003521 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003522 if (res != 0)
3523 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003524 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003525#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00003526 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003527 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00003528 BOOL ok;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003529 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum794d8131994-08-23 13:48:48 +00003530 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003531 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003532 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00003533 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00003534 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003535 return win32_error("CreatePipe", NULL);
Fred Drake699f3522000-06-29 21:12:41 +00003536 read_fd = _open_osfhandle((intptr_t)read, 0);
3537 write_fd = _open_osfhandle((intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003538 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003539#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003540#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003541}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003542#endif /* HAVE_PIPE */
3543
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003544
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003545#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003546static char posix_mkfifo__doc__[] =
3547"mkfifo(file, [, mode=0666]) -> None\n\
3548Create a FIFO (a POSIX named pipe).";
3549
Barry Warsaw53699e91996-12-10 23:23:01 +00003550static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003551posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003552{
3553 char *file;
3554 int mode = 0666;
3555 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003556 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003557 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003558 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003559 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003560 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003561 if (res < 0)
3562 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003563 Py_INCREF(Py_None);
3564 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003565}
3566#endif
3567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003568
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003569#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003570static char posix_ftruncate__doc__[] =
3571"ftruncate(fd, length) -> None\n\
3572Truncate a file to a specified length.";
3573
Barry Warsaw53699e91996-12-10 23:23:01 +00003574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003575posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003576{
3577 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003578 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003579 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003580 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003581
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003582 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003583 return NULL;
3584
3585#if !defined(HAVE_LARGEFILE_SUPPORT)
3586 length = PyInt_AsLong(lenobj);
3587#else
3588 length = PyLong_Check(lenobj) ?
3589 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
3590#endif
3591 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003592 return NULL;
3593
Barry Warsaw53699e91996-12-10 23:23:01 +00003594 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003595 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00003596 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003597 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003598 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003599 return NULL;
3600 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003601 Py_INCREF(Py_None);
3602 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003603}
3604#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003605
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003606#ifdef NeXT
3607#define HAVE_PUTENV
3608/* Steve Spicklemire got this putenv from NeXTAnswers */
3609static int
3610putenv(char *newval)
3611{
3612 extern char **environ;
3613
3614 static int firstTime = 1;
3615 char **ep;
3616 char *cp;
3617 int esiz;
3618 char *np;
3619
3620 if (!(np = strchr(newval, '=')))
3621 return 1;
3622 *np = '\0';
3623
3624 /* look it up */
3625 for (ep=environ ; *ep ; ep++)
3626 {
3627 /* this should always be true... */
3628 if (cp = strchr(*ep, '='))
3629 {
3630 *cp = '\0';
3631 if (!strcmp(*ep, newval))
3632 {
3633 /* got it! */
3634 *cp = '=';
3635 break;
3636 }
3637 *cp = '=';
3638 }
3639 else
3640 {
3641 *np = '=';
3642 return 1;
3643 }
3644 }
3645
3646 *np = '=';
3647 if (*ep)
3648 {
3649 /* the string was already there:
3650 just replace it with the new one */
3651 *ep = newval;
3652 return 0;
3653 }
3654
3655 /* expand environ by one */
3656 for (esiz=2, ep=environ ; *ep ; ep++)
3657 esiz++;
3658 if (firstTime)
3659 {
3660 char **epp;
3661 char **newenv;
3662 if (!(newenv = malloc(esiz * sizeof(char *))))
3663 return 1;
3664
3665 for (ep=environ, epp=newenv ; *ep ;)
3666 *epp++ = *ep++;
3667 *epp++ = newval;
3668 *epp = (char *) 0;
3669 environ = newenv;
3670 }
3671 else
3672 {
3673 if (!(environ = realloc(environ, esiz * sizeof(char *))))
3674 return 1;
3675 environ[esiz - 2] = newval;
3676 environ[esiz - 1] = (char *) 0;
3677 firstTime = 0;
3678 }
3679
3680 return 0;
3681}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00003682#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003684
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003685#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003686static char posix_putenv__doc__[] =
3687"putenv(key, value) -> None\n\
3688Change or add an environment variable.";
3689
Fred Drake762e2061999-08-26 17:23:54 +00003690/* Save putenv() parameters as values here, so we can collect them when they
3691 * get re-set with another call for the same key. */
3692static PyObject *posix_putenv_garbage;
3693
Barry Warsaw53699e91996-12-10 23:23:01 +00003694static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003695posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003696{
3697 char *s1, *s2;
3698 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00003699 PyObject *newstr;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003700
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003701 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003702 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003703
3704#if defined(PYOS_OS2)
3705 if (stricmp(s1, "BEGINLIBPATH") == 0) {
3706 APIRET rc;
3707
3708 if (strlen(s2) == 0) /* If New Value is an Empty String */
3709 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3710
3711 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
3712 if (rc != NO_ERROR)
3713 return os2_error(rc);
3714
3715 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
3716 APIRET rc;
3717
3718 if (strlen(s2) == 0) /* If New Value is an Empty String */
3719 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3720
3721 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
3722 if (rc != NO_ERROR)
3723 return os2_error(rc);
3724 } else {
3725#endif
3726
Fred Drake762e2061999-08-26 17:23:54 +00003727 /* XXX This can leak memory -- not easy to fix :-( */
3728 newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2);
3729 if (newstr == NULL)
3730 return PyErr_NoMemory();
3731 new = PyString_AS_STRING(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003732 (void) sprintf(new, "%s=%s", s1, s2);
3733 if (putenv(new)) {
3734 posix_error();
3735 return NULL;
3736 }
Fred Drake762e2061999-08-26 17:23:54 +00003737 /* Install the first arg and newstr in posix_putenv_garbage;
3738 * this will cause previous value to be collected. This has to
3739 * happen after the real putenv() call because the old value
3740 * was still accessible until then. */
3741 if (PyDict_SetItem(posix_putenv_garbage,
3742 PyTuple_GET_ITEM(args, 0), newstr)) {
3743 /* really not much we can do; just leak */
3744 PyErr_Clear();
3745 }
3746 else {
3747 Py_DECREF(newstr);
3748 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003749
3750#if defined(PYOS_OS2)
3751 }
3752#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003753 Py_INCREF(Py_None);
3754 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003755}
Guido van Rossumb6a47161997-09-15 22:54:34 +00003756#endif /* putenv */
3757
3758#ifdef HAVE_STRERROR
3759static char posix_strerror__doc__[] =
3760"strerror(code) -> string\n\
3761Translate an error code to a message string.";
3762
3763PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003764posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00003765{
3766 int code;
3767 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003768 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00003769 return NULL;
3770 message = strerror(code);
3771 if (message == NULL) {
3772 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00003773 "strerror() argument out of range");
Guido van Rossumb6a47161997-09-15 22:54:34 +00003774 return NULL;
3775 }
3776 return PyString_FromString(message);
3777}
3778#endif /* strerror */
3779
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003780
Guido van Rossumc9641791998-08-04 15:26:23 +00003781#ifdef HAVE_SYS_WAIT_H
3782
3783#ifdef WIFSTOPPED
3784static char posix_WIFSTOPPED__doc__[] =
3785"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003786Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003787
3788static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003789posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003790{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003791#ifdef UNION_WAIT
3792 union wait status;
3793#define status_i (status.w_status)
3794#else
3795 int status;
3796#define status_i status
3797#endif
3798 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003799
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003800 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003801 {
3802 return NULL;
3803 }
3804
3805 return Py_BuildValue("i", WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003806#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003807}
3808#endif /* WIFSTOPPED */
3809
3810#ifdef WIFSIGNALED
3811static char posix_WIFSIGNALED__doc__[] =
3812"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00003813Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003814
3815static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003816posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003817{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003818#ifdef UNION_WAIT
3819 union wait status;
3820#define status_i (status.w_status)
3821#else
3822 int status;
3823#define status_i status
3824#endif
3825 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003826
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003827 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003828 {
3829 return NULL;
3830 }
3831
3832 return Py_BuildValue("i", WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003833#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003834}
3835#endif /* WIFSIGNALED */
3836
3837#ifdef WIFEXITED
3838static char posix_WIFEXITED__doc__[] =
3839"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003840Return true if the process returning 'status' exited using the exit()\n\
3841system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003842
3843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003844posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003845{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003846#ifdef UNION_WAIT
3847 union wait status;
3848#define status_i (status.w_status)
3849#else
3850 int status;
3851#define status_i status
3852#endif
3853 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003854
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003855 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003856 {
3857 return NULL;
3858 }
3859
3860 return Py_BuildValue("i", WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003861#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003862}
3863#endif /* WIFEXITED */
3864
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003865#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00003866static char posix_WEXITSTATUS__doc__[] =
3867"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003868Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003869
3870static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003871posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003872{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003873#ifdef UNION_WAIT
3874 union wait status;
3875#define status_i (status.w_status)
3876#else
3877 int status;
3878#define status_i status
3879#endif
3880 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003881
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003882 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003883 {
3884 return NULL;
3885 }
3886
3887 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003888#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003889}
3890#endif /* WEXITSTATUS */
3891
3892#ifdef WTERMSIG
3893static char posix_WTERMSIG__doc__[] =
3894"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003895Return the signal that terminated the process that provided the 'status'\n\
3896value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003897
3898static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003899posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003900{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003901#ifdef UNION_WAIT
3902 union wait status;
3903#define status_i (status.w_status)
3904#else
3905 int status;
3906#define status_i status
3907#endif
3908 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003909
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003910 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003911 {
3912 return NULL;
3913 }
3914
3915 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003916#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003917}
3918#endif /* WTERMSIG */
3919
3920#ifdef WSTOPSIG
3921static char posix_WSTOPSIG__doc__[] =
3922"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003923Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003924
3925static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003926posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003927{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003928#ifdef UNION_WAIT
3929 union wait status;
3930#define status_i (status.w_status)
3931#else
3932 int status;
3933#define status_i status
3934#endif
3935 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003936
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003937 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003938 {
3939 return NULL;
3940 }
3941
3942 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003943#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003944}
3945#endif /* WSTOPSIG */
3946
3947#endif /* HAVE_SYS_WAIT_H */
3948
3949
Guido van Rossum94f6f721999-01-06 18:42:14 +00003950#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00003951#ifdef _SCO_DS
3952/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
3953 needed definitions in sys/statvfs.h */
3954#define _SVID3
3955#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003956#include <sys/statvfs.h>
3957
3958static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003959"fstatvfs(fd) -> \n\
3960 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003961Perform an fstatvfs system call on the given fd.";
3962
3963static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003964posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00003965{
3966 int fd, res;
3967 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003968 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003969 return NULL;
3970 Py_BEGIN_ALLOW_THREADS
3971 res = fstatvfs(fd, &st);
3972 Py_END_ALLOW_THREADS
3973 if (res != 0)
3974 return posix_error();
3975#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003976 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003977 (long) st.f_bsize,
3978 (long) st.f_frsize,
3979 (long) st.f_blocks,
3980 (long) st.f_bfree,
3981 (long) st.f_bavail,
3982 (long) st.f_files,
3983 (long) st.f_ffree,
3984 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003985 (long) st.f_flag,
3986 (long) st.f_namemax);
3987#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003988 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003989 (long) st.f_bsize,
3990 (long) st.f_frsize,
3991 (LONG_LONG) st.f_blocks,
3992 (LONG_LONG) st.f_bfree,
3993 (LONG_LONG) st.f_bavail,
3994 (LONG_LONG) st.f_files,
3995 (LONG_LONG) st.f_ffree,
3996 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003997 (long) st.f_flag,
3998 (long) st.f_namemax);
3999#endif
4000}
4001#endif /* HAVE_FSTATVFS */
4002
4003
4004#if defined(HAVE_STATVFS)
4005#include <sys/statvfs.h>
4006
4007static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00004008"statvfs(path) -> \n\
4009 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00004010Perform a statvfs system call on the given path.";
4011
4012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004013posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00004014{
4015 char *path;
4016 int res;
4017 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004018 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004019 return NULL;
4020 Py_BEGIN_ALLOW_THREADS
4021 res = statvfs(path, &st);
4022 Py_END_ALLOW_THREADS
4023 if (res != 0)
4024 return posix_error_with_filename(path);
4025#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00004026 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00004027 (long) st.f_bsize,
4028 (long) st.f_frsize,
4029 (long) st.f_blocks,
4030 (long) st.f_bfree,
4031 (long) st.f_bavail,
4032 (long) st.f_files,
4033 (long) st.f_ffree,
4034 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00004035 (long) st.f_flag,
4036 (long) st.f_namemax);
4037#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00004038 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00004039 (long) st.f_bsize,
4040 (long) st.f_frsize,
4041 (LONG_LONG) st.f_blocks,
4042 (LONG_LONG) st.f_bfree,
4043 (LONG_LONG) st.f_bavail,
4044 (LONG_LONG) st.f_files,
4045 (LONG_LONG) st.f_ffree,
4046 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00004047 (long) st.f_flag,
4048 (long) st.f_namemax);
4049#endif
4050}
4051#endif /* HAVE_STATVFS */
4052
4053
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004054#ifdef HAVE_TEMPNAM
4055static char posix_tempnam__doc__[] = "\
4056tempnam([dir[, prefix]]) -> string\n\
4057Return a unique name for a temporary file.\n\
4058The directory and a short may be specified as strings; they may be omitted\n\
4059or None if not needed.";
4060
4061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004062posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004063{
4064 PyObject *result = NULL;
4065 char *dir = NULL;
4066 char *pfx = NULL;
4067 char *name;
4068
4069 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
4070 return NULL;
4071 name = tempnam(dir, pfx);
4072 if (name == NULL)
4073 return PyErr_NoMemory();
4074 result = PyString_FromString(name);
4075 free(name);
4076 return result;
4077}
Guido van Rossumd371ff11999-01-25 16:12:23 +00004078#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004079
4080
4081#ifdef HAVE_TMPFILE
4082static char posix_tmpfile__doc__[] = "\
4083tmpfile() -> file object\n\
4084Create a temporary file with no directory entries.";
4085
4086static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004087posix_tmpfile(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004088{
4089 FILE *fp;
4090
4091 if (!PyArg_ParseTuple(args, ":tmpfile"))
4092 return NULL;
4093 fp = tmpfile();
4094 if (fp == NULL)
4095 return posix_error();
4096 return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose);
4097}
4098#endif
4099
4100
4101#ifdef HAVE_TMPNAM
4102static char posix_tmpnam__doc__[] = "\
4103tmpnam() -> string\n\
4104Return a unique name for a temporary file.";
4105
4106static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004107posix_tmpnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004108{
4109 char buffer[L_tmpnam];
4110 char *name;
4111
4112 if (!PyArg_ParseTuple(args, ":tmpnam"))
4113 return NULL;
Greg Wardb48bc172000-03-01 21:51:56 +00004114#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004115 name = tmpnam_r(buffer);
4116#else
4117 name = tmpnam(buffer);
4118#endif
4119 if (name == NULL) {
4120 PyErr_SetObject(PyExc_OSError,
4121 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00004122#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004123 "unexpected NULL from tmpnam_r"
4124#else
4125 "unexpected NULL from tmpnam"
4126#endif
4127 ));
4128 return NULL;
4129 }
4130 return PyString_FromString(buffer);
4131}
4132#endif
4133
4134
Fred Drakec9680921999-12-13 16:37:25 +00004135/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
4136 * It maps strings representing configuration variable names to
4137 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00004138 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00004139 * rarely-used constants. There are three separate tables that use
4140 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00004141 *
4142 * This code is always included, even if none of the interfaces that
4143 * need it are included. The #if hackery needed to avoid it would be
4144 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00004145 */
4146struct constdef {
4147 char *name;
4148 long value;
4149};
4150
Fred Drake12c6e2d1999-12-14 21:25:03 +00004151static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004152conv_confname(PyObject *arg, int *valuep, struct constdef *table,
4153 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004154{
4155 if (PyInt_Check(arg)) {
4156 *valuep = PyInt_AS_LONG(arg);
4157 return 1;
4158 }
4159 if (PyString_Check(arg)) {
4160 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00004161 size_t lo = 0;
4162 size_t mid;
4163 size_t hi = tablesize;
4164 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004165 char *confname = PyString_AS_STRING(arg);
4166 while (lo < hi) {
4167 mid = (lo + hi) / 2;
4168 cmp = strcmp(confname, table[mid].name);
4169 if (cmp < 0)
4170 hi = mid;
4171 else if (cmp > 0)
4172 lo = mid + 1;
4173 else {
4174 *valuep = table[mid].value;
4175 return 1;
4176 }
4177 }
4178 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
4179 }
4180 else
4181 PyErr_SetString(PyExc_TypeError,
4182 "configuration names must be strings or integers");
4183 return 0;
4184}
4185
4186
4187#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
4188static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004189#ifdef _PC_ABI_AIO_XFER_MAX
4190 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
4191#endif
4192#ifdef _PC_ABI_ASYNC_IO
4193 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
4194#endif
Fred Drakec9680921999-12-13 16:37:25 +00004195#ifdef _PC_ASYNC_IO
4196 {"PC_ASYNC_IO", _PC_ASYNC_IO},
4197#endif
4198#ifdef _PC_CHOWN_RESTRICTED
4199 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
4200#endif
4201#ifdef _PC_FILESIZEBITS
4202 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
4203#endif
4204#ifdef _PC_LAST
4205 {"PC_LAST", _PC_LAST},
4206#endif
4207#ifdef _PC_LINK_MAX
4208 {"PC_LINK_MAX", _PC_LINK_MAX},
4209#endif
4210#ifdef _PC_MAX_CANON
4211 {"PC_MAX_CANON", _PC_MAX_CANON},
4212#endif
4213#ifdef _PC_MAX_INPUT
4214 {"PC_MAX_INPUT", _PC_MAX_INPUT},
4215#endif
4216#ifdef _PC_NAME_MAX
4217 {"PC_NAME_MAX", _PC_NAME_MAX},
4218#endif
4219#ifdef _PC_NO_TRUNC
4220 {"PC_NO_TRUNC", _PC_NO_TRUNC},
4221#endif
4222#ifdef _PC_PATH_MAX
4223 {"PC_PATH_MAX", _PC_PATH_MAX},
4224#endif
4225#ifdef _PC_PIPE_BUF
4226 {"PC_PIPE_BUF", _PC_PIPE_BUF},
4227#endif
4228#ifdef _PC_PRIO_IO
4229 {"PC_PRIO_IO", _PC_PRIO_IO},
4230#endif
4231#ifdef _PC_SOCK_MAXBUF
4232 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
4233#endif
4234#ifdef _PC_SYNC_IO
4235 {"PC_SYNC_IO", _PC_SYNC_IO},
4236#endif
4237#ifdef _PC_VDISABLE
4238 {"PC_VDISABLE", _PC_VDISABLE},
4239#endif
4240};
4241
Fred Drakec9680921999-12-13 16:37:25 +00004242static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004243conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004244{
4245 return conv_confname(arg, valuep, posix_constants_pathconf,
4246 sizeof(posix_constants_pathconf)
4247 / sizeof(struct constdef));
4248}
4249#endif
4250
4251#ifdef HAVE_FPATHCONF
4252static char posix_fpathconf__doc__[] = "\
4253fpathconf(fd, name) -> integer\n\
4254Return the configuration limit name for the file descriptor fd.\n\
4255If there is no limit, return -1.";
4256
4257static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004258posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004259{
4260 PyObject *result = NULL;
4261 int name, fd;
4262
Fred Drake12c6e2d1999-12-14 21:25:03 +00004263 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
4264 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00004265 long limit;
4266
4267 errno = 0;
4268 limit = fpathconf(fd, name);
4269 if (limit == -1 && errno != 0)
4270 posix_error();
4271 else
4272 result = PyInt_FromLong(limit);
4273 }
4274 return result;
4275}
4276#endif
4277
4278
4279#ifdef HAVE_PATHCONF
4280static char posix_pathconf__doc__[] = "\
4281pathconf(path, name) -> integer\n\
4282Return the configuration limit name for the file or directory path.\n\
4283If there is no limit, return -1.";
4284
4285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004286posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004287{
4288 PyObject *result = NULL;
4289 int name;
4290 char *path;
4291
4292 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
4293 conv_path_confname, &name)) {
4294 long limit;
4295
4296 errno = 0;
4297 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004298 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00004299 if (errno == EINVAL)
4300 /* could be a path or name problem */
4301 posix_error();
4302 else
4303 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004304 }
Fred Drakec9680921999-12-13 16:37:25 +00004305 else
4306 result = PyInt_FromLong(limit);
4307 }
4308 return result;
4309}
4310#endif
4311
4312#ifdef HAVE_CONFSTR
4313static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004314#ifdef _CS_ARCHITECTURE
4315 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
4316#endif
4317#ifdef _CS_HOSTNAME
4318 {"CS_HOSTNAME", _CS_HOSTNAME},
4319#endif
4320#ifdef _CS_HW_PROVIDER
4321 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
4322#endif
4323#ifdef _CS_HW_SERIAL
4324 {"CS_HW_SERIAL", _CS_HW_SERIAL},
4325#endif
4326#ifdef _CS_INITTAB_NAME
4327 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
4328#endif
Fred Drakec9680921999-12-13 16:37:25 +00004329#ifdef _CS_LFS64_CFLAGS
4330 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
4331#endif
4332#ifdef _CS_LFS64_LDFLAGS
4333 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
4334#endif
4335#ifdef _CS_LFS64_LIBS
4336 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
4337#endif
4338#ifdef _CS_LFS64_LINTFLAGS
4339 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
4340#endif
4341#ifdef _CS_LFS_CFLAGS
4342 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
4343#endif
4344#ifdef _CS_LFS_LDFLAGS
4345 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
4346#endif
4347#ifdef _CS_LFS_LIBS
4348 {"CS_LFS_LIBS", _CS_LFS_LIBS},
4349#endif
4350#ifdef _CS_LFS_LINTFLAGS
4351 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
4352#endif
Fred Draked86ed291999-12-15 15:34:33 +00004353#ifdef _CS_MACHINE
4354 {"CS_MACHINE", _CS_MACHINE},
4355#endif
Fred Drakec9680921999-12-13 16:37:25 +00004356#ifdef _CS_PATH
4357 {"CS_PATH", _CS_PATH},
4358#endif
Fred Draked86ed291999-12-15 15:34:33 +00004359#ifdef _CS_RELEASE
4360 {"CS_RELEASE", _CS_RELEASE},
4361#endif
4362#ifdef _CS_SRPC_DOMAIN
4363 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
4364#endif
4365#ifdef _CS_SYSNAME
4366 {"CS_SYSNAME", _CS_SYSNAME},
4367#endif
4368#ifdef _CS_VERSION
4369 {"CS_VERSION", _CS_VERSION},
4370#endif
Fred Drakec9680921999-12-13 16:37:25 +00004371#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
4372 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
4373#endif
4374#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
4375 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
4376#endif
4377#ifdef _CS_XBS5_ILP32_OFF32_LIBS
4378 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
4379#endif
4380#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
4381 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
4382#endif
4383#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
4384 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
4385#endif
4386#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
4387 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
4388#endif
4389#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
4390 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
4391#endif
4392#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
4393 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
4394#endif
4395#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
4396 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
4397#endif
4398#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
4399 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
4400#endif
4401#ifdef _CS_XBS5_LP64_OFF64_LIBS
4402 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
4403#endif
4404#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
4405 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
4406#endif
4407#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
4408 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
4409#endif
4410#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
4411 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
4412#endif
4413#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
4414 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
4415#endif
4416#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
4417 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
4418#endif
Fred Draked86ed291999-12-15 15:34:33 +00004419#ifdef _MIPS_CS_AVAIL_PROCESSORS
4420 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
4421#endif
4422#ifdef _MIPS_CS_BASE
4423 {"MIPS_CS_BASE", _MIPS_CS_BASE},
4424#endif
4425#ifdef _MIPS_CS_HOSTID
4426 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
4427#endif
4428#ifdef _MIPS_CS_HW_NAME
4429 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
4430#endif
4431#ifdef _MIPS_CS_NUM_PROCESSORS
4432 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
4433#endif
4434#ifdef _MIPS_CS_OSREL_MAJ
4435 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
4436#endif
4437#ifdef _MIPS_CS_OSREL_MIN
4438 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
4439#endif
4440#ifdef _MIPS_CS_OSREL_PATCH
4441 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
4442#endif
4443#ifdef _MIPS_CS_OS_NAME
4444 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
4445#endif
4446#ifdef _MIPS_CS_OS_PROVIDER
4447 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
4448#endif
4449#ifdef _MIPS_CS_PROCESSORS
4450 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
4451#endif
4452#ifdef _MIPS_CS_SERIAL
4453 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
4454#endif
4455#ifdef _MIPS_CS_VENDOR
4456 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
4457#endif
Fred Drakec9680921999-12-13 16:37:25 +00004458};
4459
4460static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004461conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004462{
4463 return conv_confname(arg, valuep, posix_constants_confstr,
4464 sizeof(posix_constants_confstr)
4465 / sizeof(struct constdef));
4466}
4467
4468static char posix_confstr__doc__[] = "\
4469confstr(name) -> string\n\
4470Return a string-valued system configuration variable.";
4471
4472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004473posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004474{
4475 PyObject *result = NULL;
4476 int name;
4477 char buffer[64];
4478
4479 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
4480 int len = confstr(name, buffer, sizeof(buffer));
4481
Fred Drakec9680921999-12-13 16:37:25 +00004482 errno = 0;
4483 if (len == 0) {
4484 if (errno != 0)
4485 posix_error();
4486 else
4487 result = PyString_FromString("");
4488 }
4489 else {
4490 if (len >= sizeof(buffer)) {
4491 result = PyString_FromStringAndSize(NULL, len);
4492 if (result != NULL)
4493 confstr(name, PyString_AS_STRING(result), len+1);
4494 }
4495 else
4496 result = PyString_FromString(buffer);
4497 }
4498 }
4499 return result;
4500}
4501#endif
4502
4503
4504#ifdef HAVE_SYSCONF
4505static struct constdef posix_constants_sysconf[] = {
4506#ifdef _SC_2_CHAR_TERM
4507 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
4508#endif
4509#ifdef _SC_2_C_BIND
4510 {"SC_2_C_BIND", _SC_2_C_BIND},
4511#endif
4512#ifdef _SC_2_C_DEV
4513 {"SC_2_C_DEV", _SC_2_C_DEV},
4514#endif
4515#ifdef _SC_2_C_VERSION
4516 {"SC_2_C_VERSION", _SC_2_C_VERSION},
4517#endif
4518#ifdef _SC_2_FORT_DEV
4519 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
4520#endif
4521#ifdef _SC_2_FORT_RUN
4522 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
4523#endif
4524#ifdef _SC_2_LOCALEDEF
4525 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
4526#endif
4527#ifdef _SC_2_SW_DEV
4528 {"SC_2_SW_DEV", _SC_2_SW_DEV},
4529#endif
4530#ifdef _SC_2_UPE
4531 {"SC_2_UPE", _SC_2_UPE},
4532#endif
4533#ifdef _SC_2_VERSION
4534 {"SC_2_VERSION", _SC_2_VERSION},
4535#endif
Fred Draked86ed291999-12-15 15:34:33 +00004536#ifdef _SC_ABI_ASYNCHRONOUS_IO
4537 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
4538#endif
4539#ifdef _SC_ACL
4540 {"SC_ACL", _SC_ACL},
4541#endif
Fred Drakec9680921999-12-13 16:37:25 +00004542#ifdef _SC_AIO_LISTIO_MAX
4543 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
4544#endif
Fred Drakec9680921999-12-13 16:37:25 +00004545#ifdef _SC_AIO_MAX
4546 {"SC_AIO_MAX", _SC_AIO_MAX},
4547#endif
4548#ifdef _SC_AIO_PRIO_DELTA_MAX
4549 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
4550#endif
4551#ifdef _SC_ARG_MAX
4552 {"SC_ARG_MAX", _SC_ARG_MAX},
4553#endif
4554#ifdef _SC_ASYNCHRONOUS_IO
4555 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
4556#endif
4557#ifdef _SC_ATEXIT_MAX
4558 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
4559#endif
Fred Draked86ed291999-12-15 15:34:33 +00004560#ifdef _SC_AUDIT
4561 {"SC_AUDIT", _SC_AUDIT},
4562#endif
Fred Drakec9680921999-12-13 16:37:25 +00004563#ifdef _SC_AVPHYS_PAGES
4564 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
4565#endif
4566#ifdef _SC_BC_BASE_MAX
4567 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
4568#endif
4569#ifdef _SC_BC_DIM_MAX
4570 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
4571#endif
4572#ifdef _SC_BC_SCALE_MAX
4573 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
4574#endif
4575#ifdef _SC_BC_STRING_MAX
4576 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
4577#endif
Fred Draked86ed291999-12-15 15:34:33 +00004578#ifdef _SC_CAP
4579 {"SC_CAP", _SC_CAP},
4580#endif
Fred Drakec9680921999-12-13 16:37:25 +00004581#ifdef _SC_CHARCLASS_NAME_MAX
4582 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
4583#endif
4584#ifdef _SC_CHAR_BIT
4585 {"SC_CHAR_BIT", _SC_CHAR_BIT},
4586#endif
4587#ifdef _SC_CHAR_MAX
4588 {"SC_CHAR_MAX", _SC_CHAR_MAX},
4589#endif
4590#ifdef _SC_CHAR_MIN
4591 {"SC_CHAR_MIN", _SC_CHAR_MIN},
4592#endif
4593#ifdef _SC_CHILD_MAX
4594 {"SC_CHILD_MAX", _SC_CHILD_MAX},
4595#endif
4596#ifdef _SC_CLK_TCK
4597 {"SC_CLK_TCK", _SC_CLK_TCK},
4598#endif
4599#ifdef _SC_COHER_BLKSZ
4600 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
4601#endif
4602#ifdef _SC_COLL_WEIGHTS_MAX
4603 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
4604#endif
4605#ifdef _SC_DCACHE_ASSOC
4606 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
4607#endif
4608#ifdef _SC_DCACHE_BLKSZ
4609 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
4610#endif
4611#ifdef _SC_DCACHE_LINESZ
4612 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
4613#endif
4614#ifdef _SC_DCACHE_SZ
4615 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
4616#endif
4617#ifdef _SC_DCACHE_TBLKSZ
4618 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
4619#endif
4620#ifdef _SC_DELAYTIMER_MAX
4621 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
4622#endif
4623#ifdef _SC_EQUIV_CLASS_MAX
4624 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
4625#endif
4626#ifdef _SC_EXPR_NEST_MAX
4627 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
4628#endif
4629#ifdef _SC_FSYNC
4630 {"SC_FSYNC", _SC_FSYNC},
4631#endif
4632#ifdef _SC_GETGR_R_SIZE_MAX
4633 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
4634#endif
4635#ifdef _SC_GETPW_R_SIZE_MAX
4636 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
4637#endif
4638#ifdef _SC_ICACHE_ASSOC
4639 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
4640#endif
4641#ifdef _SC_ICACHE_BLKSZ
4642 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
4643#endif
4644#ifdef _SC_ICACHE_LINESZ
4645 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
4646#endif
4647#ifdef _SC_ICACHE_SZ
4648 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
4649#endif
Fred Draked86ed291999-12-15 15:34:33 +00004650#ifdef _SC_INF
4651 {"SC_INF", _SC_INF},
4652#endif
Fred Drakec9680921999-12-13 16:37:25 +00004653#ifdef _SC_INT_MAX
4654 {"SC_INT_MAX", _SC_INT_MAX},
4655#endif
4656#ifdef _SC_INT_MIN
4657 {"SC_INT_MIN", _SC_INT_MIN},
4658#endif
4659#ifdef _SC_IOV_MAX
4660 {"SC_IOV_MAX", _SC_IOV_MAX},
4661#endif
Fred Draked86ed291999-12-15 15:34:33 +00004662#ifdef _SC_IP_SECOPTS
4663 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
4664#endif
Fred Drakec9680921999-12-13 16:37:25 +00004665#ifdef _SC_JOB_CONTROL
4666 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
4667#endif
Fred Draked86ed291999-12-15 15:34:33 +00004668#ifdef _SC_KERN_POINTERS
4669 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
4670#endif
4671#ifdef _SC_KERN_SIM
4672 {"SC_KERN_SIM", _SC_KERN_SIM},
4673#endif
Fred Drakec9680921999-12-13 16:37:25 +00004674#ifdef _SC_LINE_MAX
4675 {"SC_LINE_MAX", _SC_LINE_MAX},
4676#endif
4677#ifdef _SC_LOGIN_NAME_MAX
4678 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
4679#endif
4680#ifdef _SC_LOGNAME_MAX
4681 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
4682#endif
4683#ifdef _SC_LONG_BIT
4684 {"SC_LONG_BIT", _SC_LONG_BIT},
4685#endif
Fred Draked86ed291999-12-15 15:34:33 +00004686#ifdef _SC_MAC
4687 {"SC_MAC", _SC_MAC},
4688#endif
Fred Drakec9680921999-12-13 16:37:25 +00004689#ifdef _SC_MAPPED_FILES
4690 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
4691#endif
4692#ifdef _SC_MAXPID
4693 {"SC_MAXPID", _SC_MAXPID},
4694#endif
4695#ifdef _SC_MB_LEN_MAX
4696 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
4697#endif
4698#ifdef _SC_MEMLOCK
4699 {"SC_MEMLOCK", _SC_MEMLOCK},
4700#endif
4701#ifdef _SC_MEMLOCK_RANGE
4702 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
4703#endif
4704#ifdef _SC_MEMORY_PROTECTION
4705 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
4706#endif
4707#ifdef _SC_MESSAGE_PASSING
4708 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
4709#endif
Fred Draked86ed291999-12-15 15:34:33 +00004710#ifdef _SC_MMAP_FIXED_ALIGNMENT
4711 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
4712#endif
Fred Drakec9680921999-12-13 16:37:25 +00004713#ifdef _SC_MQ_OPEN_MAX
4714 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
4715#endif
4716#ifdef _SC_MQ_PRIO_MAX
4717 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
4718#endif
Fred Draked86ed291999-12-15 15:34:33 +00004719#ifdef _SC_NACLS_MAX
4720 {"SC_NACLS_MAX", _SC_NACLS_MAX},
4721#endif
Fred Drakec9680921999-12-13 16:37:25 +00004722#ifdef _SC_NGROUPS_MAX
4723 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
4724#endif
4725#ifdef _SC_NL_ARGMAX
4726 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
4727#endif
4728#ifdef _SC_NL_LANGMAX
4729 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
4730#endif
4731#ifdef _SC_NL_MSGMAX
4732 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
4733#endif
4734#ifdef _SC_NL_NMAX
4735 {"SC_NL_NMAX", _SC_NL_NMAX},
4736#endif
4737#ifdef _SC_NL_SETMAX
4738 {"SC_NL_SETMAX", _SC_NL_SETMAX},
4739#endif
4740#ifdef _SC_NL_TEXTMAX
4741 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
4742#endif
4743#ifdef _SC_NPROCESSORS_CONF
4744 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
4745#endif
4746#ifdef _SC_NPROCESSORS_ONLN
4747 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
4748#endif
Fred Draked86ed291999-12-15 15:34:33 +00004749#ifdef _SC_NPROC_CONF
4750 {"SC_NPROC_CONF", _SC_NPROC_CONF},
4751#endif
4752#ifdef _SC_NPROC_ONLN
4753 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
4754#endif
Fred Drakec9680921999-12-13 16:37:25 +00004755#ifdef _SC_NZERO
4756 {"SC_NZERO", _SC_NZERO},
4757#endif
4758#ifdef _SC_OPEN_MAX
4759 {"SC_OPEN_MAX", _SC_OPEN_MAX},
4760#endif
4761#ifdef _SC_PAGESIZE
4762 {"SC_PAGESIZE", _SC_PAGESIZE},
4763#endif
4764#ifdef _SC_PAGE_SIZE
4765 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
4766#endif
4767#ifdef _SC_PASS_MAX
4768 {"SC_PASS_MAX", _SC_PASS_MAX},
4769#endif
4770#ifdef _SC_PHYS_PAGES
4771 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
4772#endif
4773#ifdef _SC_PII
4774 {"SC_PII", _SC_PII},
4775#endif
4776#ifdef _SC_PII_INTERNET
4777 {"SC_PII_INTERNET", _SC_PII_INTERNET},
4778#endif
4779#ifdef _SC_PII_INTERNET_DGRAM
4780 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
4781#endif
4782#ifdef _SC_PII_INTERNET_STREAM
4783 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
4784#endif
4785#ifdef _SC_PII_OSI
4786 {"SC_PII_OSI", _SC_PII_OSI},
4787#endif
4788#ifdef _SC_PII_OSI_CLTS
4789 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
4790#endif
4791#ifdef _SC_PII_OSI_COTS
4792 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
4793#endif
4794#ifdef _SC_PII_OSI_M
4795 {"SC_PII_OSI_M", _SC_PII_OSI_M},
4796#endif
4797#ifdef _SC_PII_SOCKET
4798 {"SC_PII_SOCKET", _SC_PII_SOCKET},
4799#endif
4800#ifdef _SC_PII_XTI
4801 {"SC_PII_XTI", _SC_PII_XTI},
4802#endif
4803#ifdef _SC_POLL
4804 {"SC_POLL", _SC_POLL},
4805#endif
4806#ifdef _SC_PRIORITIZED_IO
4807 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
4808#endif
4809#ifdef _SC_PRIORITY_SCHEDULING
4810 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
4811#endif
4812#ifdef _SC_REALTIME_SIGNALS
4813 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
4814#endif
4815#ifdef _SC_RE_DUP_MAX
4816 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
4817#endif
4818#ifdef _SC_RTSIG_MAX
4819 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
4820#endif
4821#ifdef _SC_SAVED_IDS
4822 {"SC_SAVED_IDS", _SC_SAVED_IDS},
4823#endif
4824#ifdef _SC_SCHAR_MAX
4825 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
4826#endif
4827#ifdef _SC_SCHAR_MIN
4828 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
4829#endif
4830#ifdef _SC_SELECT
4831 {"SC_SELECT", _SC_SELECT},
4832#endif
4833#ifdef _SC_SEMAPHORES
4834 {"SC_SEMAPHORES", _SC_SEMAPHORES},
4835#endif
4836#ifdef _SC_SEM_NSEMS_MAX
4837 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
4838#endif
4839#ifdef _SC_SEM_VALUE_MAX
4840 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
4841#endif
4842#ifdef _SC_SHARED_MEMORY_OBJECTS
4843 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
4844#endif
4845#ifdef _SC_SHRT_MAX
4846 {"SC_SHRT_MAX", _SC_SHRT_MAX},
4847#endif
4848#ifdef _SC_SHRT_MIN
4849 {"SC_SHRT_MIN", _SC_SHRT_MIN},
4850#endif
4851#ifdef _SC_SIGQUEUE_MAX
4852 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
4853#endif
4854#ifdef _SC_SIGRT_MAX
4855 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
4856#endif
4857#ifdef _SC_SIGRT_MIN
4858 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
4859#endif
Fred Draked86ed291999-12-15 15:34:33 +00004860#ifdef _SC_SOFTPOWER
4861 {"SC_SOFTPOWER", _SC_SOFTPOWER},
4862#endif
Fred Drakec9680921999-12-13 16:37:25 +00004863#ifdef _SC_SPLIT_CACHE
4864 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
4865#endif
4866#ifdef _SC_SSIZE_MAX
4867 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
4868#endif
4869#ifdef _SC_STACK_PROT
4870 {"SC_STACK_PROT", _SC_STACK_PROT},
4871#endif
4872#ifdef _SC_STREAM_MAX
4873 {"SC_STREAM_MAX", _SC_STREAM_MAX},
4874#endif
4875#ifdef _SC_SYNCHRONIZED_IO
4876 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
4877#endif
4878#ifdef _SC_THREADS
4879 {"SC_THREADS", _SC_THREADS},
4880#endif
4881#ifdef _SC_THREAD_ATTR_STACKADDR
4882 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
4883#endif
4884#ifdef _SC_THREAD_ATTR_STACKSIZE
4885 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
4886#endif
4887#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
4888 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
4889#endif
4890#ifdef _SC_THREAD_KEYS_MAX
4891 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
4892#endif
4893#ifdef _SC_THREAD_PRIORITY_SCHEDULING
4894 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
4895#endif
4896#ifdef _SC_THREAD_PRIO_INHERIT
4897 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
4898#endif
4899#ifdef _SC_THREAD_PRIO_PROTECT
4900 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
4901#endif
4902#ifdef _SC_THREAD_PROCESS_SHARED
4903 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
4904#endif
4905#ifdef _SC_THREAD_SAFE_FUNCTIONS
4906 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
4907#endif
4908#ifdef _SC_THREAD_STACK_MIN
4909 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
4910#endif
4911#ifdef _SC_THREAD_THREADS_MAX
4912 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
4913#endif
4914#ifdef _SC_TIMERS
4915 {"SC_TIMERS", _SC_TIMERS},
4916#endif
4917#ifdef _SC_TIMER_MAX
4918 {"SC_TIMER_MAX", _SC_TIMER_MAX},
4919#endif
4920#ifdef _SC_TTY_NAME_MAX
4921 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
4922#endif
4923#ifdef _SC_TZNAME_MAX
4924 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
4925#endif
4926#ifdef _SC_T_IOV_MAX
4927 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
4928#endif
4929#ifdef _SC_UCHAR_MAX
4930 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
4931#endif
4932#ifdef _SC_UINT_MAX
4933 {"SC_UINT_MAX", _SC_UINT_MAX},
4934#endif
4935#ifdef _SC_UIO_MAXIOV
4936 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
4937#endif
4938#ifdef _SC_ULONG_MAX
4939 {"SC_ULONG_MAX", _SC_ULONG_MAX},
4940#endif
4941#ifdef _SC_USHRT_MAX
4942 {"SC_USHRT_MAX", _SC_USHRT_MAX},
4943#endif
4944#ifdef _SC_VERSION
4945 {"SC_VERSION", _SC_VERSION},
4946#endif
4947#ifdef _SC_WORD_BIT
4948 {"SC_WORD_BIT", _SC_WORD_BIT},
4949#endif
4950#ifdef _SC_XBS5_ILP32_OFF32
4951 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
4952#endif
4953#ifdef _SC_XBS5_ILP32_OFFBIG
4954 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
4955#endif
4956#ifdef _SC_XBS5_LP64_OFF64
4957 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
4958#endif
4959#ifdef _SC_XBS5_LPBIG_OFFBIG
4960 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
4961#endif
4962#ifdef _SC_XOPEN_CRYPT
4963 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
4964#endif
4965#ifdef _SC_XOPEN_ENH_I18N
4966 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
4967#endif
4968#ifdef _SC_XOPEN_LEGACY
4969 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
4970#endif
4971#ifdef _SC_XOPEN_REALTIME
4972 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
4973#endif
4974#ifdef _SC_XOPEN_REALTIME_THREADS
4975 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
4976#endif
4977#ifdef _SC_XOPEN_SHM
4978 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
4979#endif
4980#ifdef _SC_XOPEN_UNIX
4981 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
4982#endif
4983#ifdef _SC_XOPEN_VERSION
4984 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
4985#endif
4986#ifdef _SC_XOPEN_XCU_VERSION
4987 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
4988#endif
4989#ifdef _SC_XOPEN_XPG2
4990 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
4991#endif
4992#ifdef _SC_XOPEN_XPG3
4993 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
4994#endif
4995#ifdef _SC_XOPEN_XPG4
4996 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
4997#endif
4998};
4999
5000static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005001conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005002{
5003 return conv_confname(arg, valuep, posix_constants_sysconf,
5004 sizeof(posix_constants_sysconf)
5005 / sizeof(struct constdef));
5006}
5007
5008static char posix_sysconf__doc__[] = "\
5009sysconf(name) -> integer\n\
5010Return an integer-valued system configuration variable.";
5011
5012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005013posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005014{
5015 PyObject *result = NULL;
5016 int name;
5017
5018 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
5019 int value;
5020
5021 errno = 0;
5022 value = sysconf(name);
5023 if (value == -1 && errno != 0)
5024 posix_error();
5025 else
5026 result = PyInt_FromLong(value);
5027 }
5028 return result;
5029}
5030#endif
5031
5032
Fred Drakebec628d1999-12-15 18:31:10 +00005033/* This code is used to ensure that the tables of configuration value names
5034 * are in sorted order as required by conv_confname(), and also to build the
5035 * the exported dictionaries that are used to publish information about the
5036 * names available on the host platform.
5037 *
5038 * Sorting the table at runtime ensures that the table is properly ordered
5039 * when used, even for platforms we're not able to test on. It also makes
5040 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00005041 */
Fred Drakebec628d1999-12-15 18:31:10 +00005042
5043static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005044cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00005045{
5046 const struct constdef *c1 =
5047 (const struct constdef *) v1;
5048 const struct constdef *c2 =
5049 (const struct constdef *) v2;
5050
5051 return strcmp(c1->name, c2->name);
5052}
5053
5054static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005055setup_confname_table(struct constdef *table, size_t tablesize,
5056 char *tablename, PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00005057{
Fred Drakebec628d1999-12-15 18:31:10 +00005058 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00005059 size_t i;
5060 int status;
Fred Drakebec628d1999-12-15 18:31:10 +00005061
5062 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
5063 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00005064 if (d == NULL)
5065 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005066
Barry Warsaw3155db32000-04-13 15:20:40 +00005067 for (i=0; i < tablesize; ++i) {
5068 PyObject *o = PyInt_FromLong(table[i].value);
5069 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
5070 Py_XDECREF(o);
5071 Py_DECREF(d);
5072 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005073 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005074 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00005075 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005076 status = PyDict_SetItemString(moddict, tablename, d);
5077 Py_DECREF(d);
5078 return status;
Fred Draked86ed291999-12-15 15:34:33 +00005079}
5080
Fred Drakebec628d1999-12-15 18:31:10 +00005081/* Return -1 on failure, 0 on success. */
5082static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005083setup_confname_tables(PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00005084{
5085#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00005086 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00005087 sizeof(posix_constants_pathconf)
5088 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005089 "pathconf_names", moddict))
5090 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005091#endif
5092#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00005093 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00005094 sizeof(posix_constants_confstr)
5095 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005096 "confstr_names", moddict))
5097 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005098#endif
5099#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00005100 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00005101 sizeof(posix_constants_sysconf)
5102 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005103 "sysconf_names", moddict))
5104 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005105#endif
Fred Drakebec628d1999-12-15 18:31:10 +00005106 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00005107}
Fred Draked86ed291999-12-15 15:34:33 +00005108
5109
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005110static char posix_abort__doc__[] = "\
5111abort() -> does not return!\n\
5112Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
5113in the hardest way possible on the hosting operating system.";
5114
5115static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005116posix_abort(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005117{
5118 if (!PyArg_ParseTuple(args, ":abort"))
5119 return NULL;
5120 abort();
5121 /*NOTREACHED*/
5122 Py_FatalError("abort() called from Python code didn't abort!");
5123 return NULL;
5124}
Fred Drakebec628d1999-12-15 18:31:10 +00005125
Tim Petersf58a7aa2000-09-22 10:05:54 +00005126#ifdef MS_WIN32
5127static char win32_startfile__doc__[] = "\
5128startfile(filepath) - Start a file with its associated application.\n\
5129\n\
5130This acts like double-clicking the file in Explorer, or giving the file\n\
5131name as an argument to the DOS \"start\" command: the file is opened\n\
5132with whatever application (if any) its extension is associated.\n\
5133\n\
5134startfile returns as soon as the associated application is launched.\n\
5135There is no option to wait for the application to close, and no way\n\
5136to retrieve the application's exit status.\n\
5137\n\
5138The filepath is relative to the current directory. If you want to use\n\
5139an absolute path, make sure the first character is not a slash (\"/\");\n\
5140the underlying Win32 ShellExecute function doesn't work if it is.";
5141
5142static PyObject *
5143win32_startfile(PyObject *self, PyObject *args)
5144{
5145 char *filepath;
5146 HINSTANCE rc;
5147 if (!PyArg_ParseTuple(args, "s:startfile", &filepath))
5148 return NULL;
5149 Py_BEGIN_ALLOW_THREADS
5150 rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL);
5151 Py_END_ALLOW_THREADS
5152 if (rc <= (HINSTANCE)32)
5153 return win32_error("startfile", filepath);
5154 Py_INCREF(Py_None);
5155 return Py_None;
5156}
5157#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005158
5159static PyMethodDef posix_methods[] = {
5160 {"access", posix_access, METH_VARARGS, posix_access__doc__},
5161#ifdef HAVE_TTYNAME
5162 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
5163#endif
5164 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
5165 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005166#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005167 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005168#endif /* HAVE_CHOWN */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005169#ifdef HAVE_CTERMID
5170 {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__},
5171#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00005172#ifdef HAVE_GETCWD
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005173 {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00005174#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005175#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005176 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005177#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005178 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
5179 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
5180 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005181#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005182 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005183#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005184#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005185 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005186#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005187 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
5188 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
5189 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005190#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005191 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005192#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005193#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005194 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005195#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005196 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005197#ifdef HAVE_UNAME
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005198 {"uname", posix_uname, METH_VARARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005199#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005200 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
5201 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
5202 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005203#ifdef HAVE_TIMES
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005204 {"times", posix_times, METH_VARARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005205#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005206 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005207#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005208 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
5209 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005210#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00005211#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005212 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
5213 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00005214#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005215#ifdef HAVE_FORK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005216 {"fork", posix_fork, METH_VARARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005217#endif /* HAVE_FORK */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005218#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005219 {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__},
Thomas Wouters70c21a12000-07-14 14:28:33 +00005220#endif /* HAVE_OPENPTY || HAVE__GETPTY */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005221#ifdef HAVE_FORKPTY
5222 {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
5223#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005224#ifdef HAVE_GETEGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005225 {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005226#endif /* HAVE_GETEGID */
5227#ifdef HAVE_GETEUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005228 {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005229#endif /* HAVE_GETEUID */
5230#ifdef HAVE_GETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005231 {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005232#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00005233#ifdef HAVE_GETGROUPS
5234 {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__},
5235#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005236 {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005237#ifdef HAVE_GETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005238 {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005239#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005240#ifdef HAVE_GETPPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005241 {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005242#endif /* HAVE_GETPPID */
5243#ifdef HAVE_GETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005244 {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005245#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005246#ifdef HAVE_GETLOGIN
5247 {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__},
5248#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00005249#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005250 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005251#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00005252#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005253 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00005254#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005255#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005256 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005257#ifdef MS_WIN32
5258 {"popen2", win32_popen2, METH_VARARGS},
5259 {"popen3", win32_popen3, METH_VARARGS},
5260 {"popen4", win32_popen4, METH_VARARGS},
Tim Petersf58a7aa2000-09-22 10:05:54 +00005261 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005262#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005263#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005264#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005265 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005266#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005267#ifdef HAVE_SETEUID
5268 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
5269#endif /* HAVE_SETEUID */
5270#ifdef HAVE_SETEGID
5271 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
5272#endif /* HAVE_SETEGID */
5273#ifdef HAVE_SETREUID
5274 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
5275#endif /* HAVE_SETREUID */
5276#ifdef HAVE_SETREGID
5277 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
5278#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005279#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005280 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005281#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005282#ifdef HAVE_SETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005283 {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005284#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005285#ifdef HAVE_WAIT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005286 {"wait", posix_wait, METH_VARARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005287#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005288#ifdef HAVE_WAITPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005289 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005290#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005291#ifdef HAVE_SETSID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005292 {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005293#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005294#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005295 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005296#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005297#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005298 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005299#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005300#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005301 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005302#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005303 {"open", posix_open, METH_VARARGS, posix_open__doc__},
5304 {"close", posix_close, METH_VARARGS, posix_close__doc__},
5305 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
5306 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
5307 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
5308 {"read", posix_read, METH_VARARGS, posix_read__doc__},
5309 {"write", posix_write, METH_VARARGS, posix_write__doc__},
5310 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
5311 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00005312 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005313#ifdef HAVE_PIPE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005314 {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005315#endif
5316#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005317 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005318#endif
5319#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005320 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005321#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005322#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005323 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005324#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00005325#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005326 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00005327#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00005328#ifdef HAVE_FSYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005329 {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005330#endif
5331#ifdef HAVE_FDATASYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005332 {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005333#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00005334#ifdef HAVE_SYS_WAIT_H
5335#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005336 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005337#endif /* WIFSTOPPED */
5338#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005339 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005340#endif /* WIFSIGNALED */
5341#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005342 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005343#endif /* WIFEXITED */
5344#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005345 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005346#endif /* WEXITSTATUS */
5347#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005348 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005349#endif /* WTERMSIG */
5350#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005351 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005352#endif /* WSTOPSIG */
5353#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005354#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005355 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005356#endif
5357#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005358 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005359#endif
Guido van Rossume2ad6332001-01-08 17:51:55 +00005360#ifdef HAVE_TMPFILE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005361 {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__},
5362#endif
5363#ifdef HAVE_TEMPNAM
5364 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
5365#endif
5366#ifdef HAVE_TMPNAM
5367 {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__},
5368#endif
Fred Drakec9680921999-12-13 16:37:25 +00005369#ifdef HAVE_CONFSTR
5370 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
5371#endif
5372#ifdef HAVE_SYSCONF
5373 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
5374#endif
5375#ifdef HAVE_FPATHCONF
5376 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
5377#endif
5378#ifdef HAVE_PATHCONF
5379 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
5380#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005381 {"abort", posix_abort, METH_VARARGS, posix_abort__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005382 {NULL, NULL} /* Sentinel */
5383};
5384
5385
Barry Warsaw4a342091996-12-19 23:50:02 +00005386static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005387ins(PyObject *d, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00005388{
5389 PyObject* v = PyInt_FromLong(value);
5390 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
5391 return -1; /* triggers fatal error */
5392
5393 Py_DECREF(v);
5394 return 0;
5395}
5396
Guido van Rossumd48f2521997-12-05 22:19:34 +00005397#if defined(PYOS_OS2)
5398/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
5399static int insertvalues(PyObject *d)
5400{
5401 APIRET rc;
5402 ULONG values[QSV_MAX+1];
5403 PyObject *v;
5404 char *ver, tmp[10];
5405
5406 Py_BEGIN_ALLOW_THREADS
5407 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
5408 Py_END_ALLOW_THREADS
5409
5410 if (rc != NO_ERROR) {
5411 os2_error(rc);
5412 return -1;
5413 }
5414
5415 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
5416 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
5417 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
5418 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
5419 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
5420 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
5421 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
5422
5423 switch (values[QSV_VERSION_MINOR]) {
5424 case 0: ver = "2.00"; break;
5425 case 10: ver = "2.10"; break;
5426 case 11: ver = "2.11"; break;
5427 case 30: ver = "3.00"; break;
5428 case 40: ver = "4.00"; break;
5429 case 50: ver = "5.00"; break;
5430 default:
5431 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
5432 values[QSV_VERSION_MINOR]);
5433 ver = &tmp[0];
5434 }
5435
5436 /* Add Indicator of the Version of the Operating System */
5437 v = PyString_FromString(ver);
5438 if (!v || PyDict_SetItemString(d, "version", v) < 0)
5439 return -1;
5440 Py_DECREF(v);
5441
5442 /* Add Indicator of Which Drive was Used to Boot the System */
5443 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
5444 tmp[1] = ':';
5445 tmp[2] = '\0';
5446
5447 v = PyString_FromString(tmp);
5448 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
5449 return -1;
5450 Py_DECREF(v);
5451
5452 return 0;
5453}
5454#endif
5455
Barry Warsaw4a342091996-12-19 23:50:02 +00005456static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005457all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00005458{
Guido van Rossum94f6f721999-01-06 18:42:14 +00005459#ifdef F_OK
5460 if (ins(d, "F_OK", (long)F_OK)) return -1;
5461#endif
5462#ifdef R_OK
5463 if (ins(d, "R_OK", (long)R_OK)) return -1;
5464#endif
5465#ifdef W_OK
5466 if (ins(d, "W_OK", (long)W_OK)) return -1;
5467#endif
5468#ifdef X_OK
5469 if (ins(d, "X_OK", (long)X_OK)) return -1;
5470#endif
Fred Drakec9680921999-12-13 16:37:25 +00005471#ifdef NGROUPS_MAX
5472 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
5473#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005474#ifdef TMP_MAX
5475 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
5476#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005477#ifdef WNOHANG
5478 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
5479#endif
5480#ifdef O_RDONLY
5481 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
5482#endif
5483#ifdef O_WRONLY
5484 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
5485#endif
5486#ifdef O_RDWR
5487 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
5488#endif
5489#ifdef O_NDELAY
5490 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
5491#endif
5492#ifdef O_NONBLOCK
5493 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
5494#endif
5495#ifdef O_APPEND
5496 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
5497#endif
5498#ifdef O_DSYNC
5499 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
5500#endif
5501#ifdef O_RSYNC
5502 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
5503#endif
5504#ifdef O_SYNC
5505 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
5506#endif
5507#ifdef O_NOCTTY
5508 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
5509#endif
5510#ifdef O_CREAT
5511 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
5512#endif
5513#ifdef O_EXCL
5514 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
5515#endif
5516#ifdef O_TRUNC
5517 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
5518#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00005519#ifdef O_BINARY
5520 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
5521#endif
5522#ifdef O_TEXT
5523 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
5524#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005525
Guido van Rossum246bc171999-02-01 23:54:31 +00005526#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00005527 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
5528 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
5529 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
5530 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
5531 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00005532#endif
5533
Guido van Rossumd48f2521997-12-05 22:19:34 +00005534#if defined(PYOS_OS2)
5535 if (insertvalues(d)) return -1;
5536#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005537 return 0;
5538}
5539
5540
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005541#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005542#define INITFUNC initnt
5543#define MODNAME "nt"
5544#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005545#if defined(PYOS_OS2)
5546#define INITFUNC initos2
5547#define MODNAME "os2"
5548#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005549#define INITFUNC initposix
5550#define MODNAME "posix"
5551#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005552#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005553
Guido van Rossum3886bb61998-12-04 18:50:17 +00005554DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005555INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00005556{
Barry Warsaw53699e91996-12-10 23:23:01 +00005557 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005558
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005559 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005560 posix_methods,
5561 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005562 (PyObject *)NULL,
5563 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00005564 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005565
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005566 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005567 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00005568 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005569 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00005570 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00005571
Barry Warsaw4a342091996-12-19 23:50:02 +00005572 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00005573 return;
5574
Fred Drakebec628d1999-12-15 18:31:10 +00005575 if (setup_confname_tables(d))
5576 return;
5577
Barry Warsawca74da41999-02-09 19:31:45 +00005578 PyDict_SetItemString(d, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00005579
Guido van Rossumb3d39562000-01-31 18:41:26 +00005580#ifdef HAVE_PUTENV
Neil Schemenauer19030a02001-01-16 04:27:47 +00005581 if (posix_putenv_garbage == NULL)
5582 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00005583#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005584}