blob: d7755bf95ee802600196c69b4ce128c037a568c9 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* POSIX module implementation */
12
Guido van Rossuma4916fa1996-05-23 22:58:55 +000013/* This file is also used for Windows NT and MS-Win. In that case the module
Guido van Rossumad0ee831995-03-01 10:34:45 +000014 actually calls itself 'nt', not 'posix', and a few functions are
15 either unimplemented or implemented differently. The source
Guido van Rossum8d665e61996-06-26 18:22:49 +000016 assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +000017 of the compiler used. Different compilers define their own feature
Guido van Rossuma4916fa1996-05-23 22:58:55 +000018 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000019
Guido van Rossuma4916fa1996-05-23 22:58:55 +000020/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000021
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000022static char posix__doc__ [] =
23"This module provides access to operating system functionality that is\n\
24standardized by the C Standard and the POSIX standard (a thinly\n\
25disguised Unix interface). Refer to the library manual and\n\
26corresponding Unix manual entries for more information on calls.";
27
Barry Warsaw53699e91996-12-10 23:23:01 +000028#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000030#if defined(PYOS_OS2)
31#define INCL_DOS
32#define INCL_DOSERRORS
33#define INCL_DOSPROCESS
34#define INCL_NOPMAPI
35#include <os2.h>
36#endif
37
Guido van Rossumb6775db1994-08-01 11:34:53 +000038#include <sys/types.h>
39#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000040#ifdef HAVE_SYS_WAIT_H
41#include <sys/wait.h> /* For WNOHANG */
42#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000043
Guido van Rossuma376cc51996-12-05 23:43:35 +000044#ifdef HAVE_SIGNAL_H
45#include <signal.h>
46#endif
47
Guido van Rossumb6775db1994-08-01 11:34:53 +000048#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000050#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000051
Guido van Rossuma4916fa1996-05-23 22:58:55 +000052/* Various compilers have only certain posix functions */
Guido van Rossum6d8841c1997-08-14 19:57:39 +000053/* XXX Gosh I wish these were all moved into config.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000054#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000055#include <process.h>
56#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000057#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000058#define HAVE_GETCWD 1
59#define HAVE_OPENDIR 1
60#define HAVE_SYSTEM 1
61#if defined(__OS2__)
62#define HAVE_EXECV 1
63#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000064#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000065#include <process.h>
66#else
67#ifdef __BORLANDC__ /* Borland compiler */
68#define HAVE_EXECV 1
69#define HAVE_GETCWD 1
70#define HAVE_GETEGID 1
71#define HAVE_GETEUID 1
72#define HAVE_GETGID 1
73#define HAVE_GETPPID 1
74#define HAVE_GETUID 1
75#define HAVE_KILL 1
76#define HAVE_OPENDIR 1
77#define HAVE_PIPE 1
78#define HAVE_POPEN 1
79#define HAVE_SYSTEM 1
80#define HAVE_WAIT 1
81#else
82#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +000083#define HAVE_GETCWD 1
84#ifdef MS_WIN32
Guido van Rossuma1065681999-01-25 23:20:23 +000085#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +000086#define HAVE_EXECV 1
87#define HAVE_PIPE 1
88#define HAVE_POPEN 1
89#define HAVE_SYSTEM 1
90#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +000091#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000092#else /* all other compilers */
93/* Unix functions that the configure script doesn't check for */
94#define HAVE_EXECV 1
95#define HAVE_FORK 1
96#define HAVE_GETCWD 1
97#define HAVE_GETEGID 1
98#define HAVE_GETEUID 1
99#define HAVE_GETGID 1
100#define HAVE_GETPPID 1
101#define HAVE_GETUID 1
102#define HAVE_KILL 1
103#define HAVE_OPENDIR 1
104#define HAVE_PIPE 1
105#define HAVE_POPEN 1
106#define HAVE_SYSTEM 1
107#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000108#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#endif /* _MSC_VER */
110#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000111#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000112#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000113
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000115
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000117#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000118#endif
119
120#ifdef NeXT
121/* NeXT's <unistd.h> and <utime.h> aren't worth much */
122#undef HAVE_UNISTD_H
123#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000124#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000125/* #undef HAVE_GETCWD */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000126#define UNION_WAIT /* This should really be checked for by autoconf */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000127#endif
128
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000129#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000130#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000131extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000132#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000133#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000134extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000136extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000138#endif
139#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000140extern int chdir(char *);
141extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000142#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000143extern int chdir(const char *);
144extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000145#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000146extern int chmod(const char *, mode_t);
147extern int chown(const char *, uid_t, gid_t);
148extern char *getcwd(char *, int);
149extern char *strerror(int);
150extern int link(const char *, const char *);
151extern int rename(const char *, const char *);
152extern int stat(const char *, struct stat *);
153extern int unlink(const char *);
154extern int pclose(FILE *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000156extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000157#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000159extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000160#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000162
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000163#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165#ifdef HAVE_UTIME_H
166#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000167#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000169#ifdef HAVE_SYS_UTIME_H
170#include <sys/utime.h>
171#define HAVE_UTIME_H /* pretend we do for the rest of this file */
172#endif /* HAVE_SYS_UTIME_H */
173
Guido van Rossumb6775db1994-08-01 11:34:53 +0000174#ifdef HAVE_SYS_TIMES_H
175#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000176#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177
178#ifdef HAVE_SYS_PARAM_H
179#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000180#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181
182#ifdef HAVE_SYS_UTSNAME_H
183#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000184#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185
186#ifndef MAXPATHLEN
187#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000188#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000190#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000192#define NAMLEN(dirent) strlen((dirent)->d_name)
193#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000194#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000195#include <direct.h>
196#define NAMLEN(dirent) strlen((dirent)->d_name)
197#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000199#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000200#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000201#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000203#endif
204#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000206#endif
207#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000209#endif
210#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000212#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#include <direct.h>
214#include <io.h>
215#include <process.h>
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000216#define WINDOWS_LEAN_AND_MEAN
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000218#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000219#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000220#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#else /* 16-bit Windows */
222#include <dos.h>
223#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000224#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossumd48f2521997-12-05 22:19:34 +0000227#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000231#ifdef UNION_WAIT
232/* Emulate some macros on systems that have a union instead of macros */
233
234#ifndef WIFEXITED
235#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
236#endif
237
238#ifndef WEXITSTATUS
239#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
240#endif
241
242#ifndef WTERMSIG
243#define WTERMSIG(u_wait) ((u_wait).w_termsig)
244#endif
245
246#endif /* UNION_WAIT */
247
Greg Wardb48bc172000-03-01 21:51:56 +0000248/* Don't use the "_r" form if we don't need it (also, won't have a
249 prototype for it, at least on Solaris -- maybe others as well?). */
250#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
251#define USE_CTERMID_R
252#endif
253
254#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
255#define USE_TMPNAM_R
256#endif
257
Fred Drake699f3522000-06-29 21:12:41 +0000258/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000259#undef STAT
Fred Drake699f3522000-06-29 21:12:41 +0000260#ifdef MS_WIN64
261# define STAT _stati64
262# define FSTAT _fstati64
263# define STRUCT_STAT struct _stati64
264#else
265# define STAT stat
266# define FSTAT fstat
267# define STRUCT_STAT struct stat
268#endif
269
270
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271/* Return a dictionary corresponding to the POSIX environment table */
272
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000273#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000275#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276
Barry Warsaw53699e91996-12-10 23:23:01 +0000277static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000278convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279{
Barry Warsaw53699e91996-12-10 23:23:01 +0000280 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000282 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283 if (d == NULL)
284 return NULL;
285 if (environ == NULL)
286 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000287 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000289 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000290 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 char *p = strchr(*e, '=');
292 if (p == NULL)
293 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000294 k = PyString_FromStringAndSize(*e, (int)(p-*e));
295 if (k == NULL) {
296 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000298 }
299 v = PyString_FromString(p+1);
300 if (v == NULL) {
301 PyErr_Clear();
302 Py_DECREF(k);
303 continue;
304 }
305 if (PyDict_GetItem(d, k) == NULL) {
306 if (PyDict_SetItem(d, k, v) != 0)
307 PyErr_Clear();
308 }
309 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000310 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000312#if defined(PYOS_OS2)
313 {
314 APIRET rc;
315 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
316
317 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000318 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
Guido van Rossumd48f2521997-12-05 22:19:34 +0000319 PyObject *v = PyString_FromString(buffer);
320 PyDict_SetItemString(d, "BEGINLIBPATH", v);
321 Py_DECREF(v);
322 }
323 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
324 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
325 PyObject *v = PyString_FromString(buffer);
326 PyDict_SetItemString(d, "ENDLIBPATH", v);
327 Py_DECREF(v);
328 }
329 }
330#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 return d;
332}
333
334
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335/* Set a POSIX-specific error from errno, and return NULL */
336
Barry Warsawd58d7641998-07-23 16:14:40 +0000337static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000338posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000339{
Barry Warsawca74da41999-02-09 19:31:45 +0000340 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341}
Barry Warsawd58d7641998-07-23 16:14:40 +0000342static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000343posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000344{
Barry Warsawca74da41999-02-09 19:31:45 +0000345 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000346}
347
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000348#ifdef MS_WIN32
349static PyObject *
350win32_error(char* function, char* filename)
351{
Mark Hammond33a6da92000-08-15 00:46:38 +0000352 /* XXX We should pass the function name along in the future.
353 (_winreg.c also wants to pass the function name.)
354 This would however require an additional param to the
355 Windows error object, which is non-trivial.
356 */
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000357 errno = GetLastError();
358 if (filename)
Mark Hammond33a6da92000-08-15 00:46:38 +0000359 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000360 else
Mark Hammond33a6da92000-08-15 00:46:38 +0000361 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000362}
363#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364
Guido van Rossumd48f2521997-12-05 22:19:34 +0000365#if defined(PYOS_OS2)
366/**********************************************************************
367 * Helper Function to Trim and Format OS/2 Messages
368 **********************************************************************/
369 static void
370os2_formatmsg(char *msgbuf, int msglen, char *reason)
371{
372 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
373
374 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
375 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
376
377 while (lastc > msgbuf && isspace(*lastc))
378 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
379 }
380
381 /* Add Optional Reason Text */
382 if (reason) {
383 strcat(msgbuf, " : ");
384 strcat(msgbuf, reason);
385 }
386}
387
388/**********************************************************************
389 * Decode an OS/2 Operating System Error Code
390 *
391 * A convenience function to lookup an OS/2 error code and return a
392 * text message we can use to raise a Python exception.
393 *
394 * Notes:
395 * The messages for errors returned from the OS/2 kernel reside in
396 * the file OSO001.MSG in the \OS2 directory hierarchy.
397 *
398 **********************************************************************/
399 static char *
400os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
401{
402 APIRET rc;
403 ULONG msglen;
404
405 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
406 Py_BEGIN_ALLOW_THREADS
407 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
408 errorcode, "oso001.msg", &msglen);
409 Py_END_ALLOW_THREADS
410
411 if (rc == NO_ERROR)
412 os2_formatmsg(msgbuf, msglen, reason);
413 else
414 sprintf(msgbuf, "unknown OS error #%d", errorcode);
415
416 return msgbuf;
417}
418
419/* Set an OS/2-specific error and return NULL. OS/2 kernel
420 errors are not in a global variable e.g. 'errno' nor are
421 they congruent with posix error numbers. */
422
423static PyObject * os2_error(int code)
424{
425 char text[1024];
426 PyObject *v;
427
428 os2_strerror(text, sizeof(text), code, "");
429
430 v = Py_BuildValue("(is)", code, text);
431 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000432 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000433 Py_DECREF(v);
434 }
435 return NULL; /* Signal to Python that an Exception is Pending */
436}
437
438#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439
440/* POSIX generic methods */
441
Barry Warsaw53699e91996-12-10 23:23:01 +0000442static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000443posix_int(PyObject *args, char *format, int (*func)(int))
Guido van Rossum21142a01999-01-08 21:05:37 +0000444{
445 int fd;
446 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000447 if (!PyArg_ParseTuple(args, format, &fd))
Guido van Rossum21142a01999-01-08 21:05:37 +0000448 return NULL;
449 Py_BEGIN_ALLOW_THREADS
450 res = (*func)(fd);
451 Py_END_ALLOW_THREADS
452 if (res < 0)
453 return posix_error();
454 Py_INCREF(Py_None);
455 return Py_None;
456}
457
458
459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000460posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000462 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000463 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000464 if (!PyArg_ParseTuple(args, format, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000466 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000467 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000468 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000469 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000470 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000471 Py_INCREF(Py_None);
472 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473}
474
Barry Warsaw53699e91996-12-10 23:23:01 +0000475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000476posix_2str(PyObject *args, char *format,
477 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000479 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000480 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000481 if (!PyArg_ParseTuple(args, format, &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000483 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000484 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000485 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000486 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000487 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_INCREF(Py_None);
490 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491}
492
Fred Drake699f3522000-06-29 21:12:41 +0000493/* pack a system stat C structure into the Python stat tuple
494 (used by posix_stat() and posix_fstat()) */
495static PyObject*
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000496_pystat_fromstructstat(STRUCT_STAT st)
Fred Drake699f3522000-06-29 21:12:41 +0000497{
498 PyObject *v = PyTuple_New(10);
499 if (v == NULL)
500 return NULL;
501
502 PyTuple_SetItem(v, 0, PyInt_FromLong((long)st.st_mode));
503#ifdef HAVE_LARGEFILE_SUPPORT
504 PyTuple_SetItem(v, 1, PyLong_FromLongLong((LONG_LONG)st.st_ino));
505#else
506 PyTuple_SetItem(v, 1, PyInt_FromLong((long)st.st_ino));
507#endif
508#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
509 PyTuple_SetItem(v, 2, PyLong_FromLongLong((LONG_LONG)st.st_dev));
510#else
511 PyTuple_SetItem(v, 2, PyInt_FromLong((long)st.st_dev));
512#endif
513 PyTuple_SetItem(v, 3, PyInt_FromLong((long)st.st_nlink));
514 PyTuple_SetItem(v, 4, PyInt_FromLong((long)st.st_uid));
515 PyTuple_SetItem(v, 5, PyInt_FromLong((long)st.st_gid));
516#ifdef HAVE_LARGEFILE_SUPPORT
517 PyTuple_SetItem(v, 6, PyLong_FromLongLong((LONG_LONG)st.st_size));
518#else
519 PyTuple_SetItem(v, 6, PyInt_FromLong(st.st_size));
520#endif
521#if SIZEOF_TIME_T > SIZEOF_LONG
522 PyTuple_SetItem(v, 7, PyLong_FromLongLong((LONG_LONG)st.st_atime));
523 PyTuple_SetItem(v, 8, PyLong_FromLongLong((LONG_LONG)st.st_mtime));
524 PyTuple_SetItem(v, 9, PyLong_FromLongLong((LONG_LONG)st.st_ctime));
525#else
526 PyTuple_SetItem(v, 7, PyInt_FromLong((long)st.st_atime));
527 PyTuple_SetItem(v, 8, PyInt_FromLong((long)st.st_mtime));
528 PyTuple_SetItem(v, 9, PyInt_FromLong((long)st.st_ctime));
529#endif
530
531 if (PyErr_Occurred()) {
532 Py_DECREF(v);
533 return NULL;
534 }
535
536 return v;
537}
538
539
Barry Warsaw53699e91996-12-10 23:23:01 +0000540static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000541posix_do_stat(PyObject *self, PyObject *args, char *format,
542 int (*statfunc)(const char *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543{
Fred Drake699f3522000-06-29 21:12:41 +0000544 STRUCT_STAT st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000545 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000546 int res;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000547
548#ifdef MS_WIN32
549 int pathlen;
550 char pathcopy[MAX_PATH];
551#endif /* MS_WIN32 */
552
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000553 if (!PyArg_ParseTuple(args, format, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554 return NULL;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000555
556#ifdef MS_WIN32
557 pathlen = strlen(path);
558 /* the library call can blow up if the file name is too long! */
559 if (pathlen > MAX_PATH) {
560 errno = ENAMETOOLONG;
561 return posix_error();
562 }
563
564 if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) {
Guido van Rossum19dde102000-05-03 02:44:55 +0000565 /* exception for specific or current drive root */
566 if (!((pathlen == 1) ||
567 ((pathlen == 3) &&
Guido van Rossumace88ae2000-04-21 18:54:45 +0000568 (path[1] == ':') &&
Guido van Rossum19dde102000-05-03 02:44:55 +0000569 (path[2] == '\\' || path[2] == '/'))))
Guido van Rossumace88ae2000-04-21 18:54:45 +0000570 {
571 strncpy(pathcopy, path, pathlen);
572 pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */
573 path = pathcopy;
574 }
575 }
576#endif /* MS_WIN32 */
577
Barry Warsaw53699e91996-12-10 23:23:01 +0000578 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000579 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000580 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000581 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000582 return posix_error_with_filename(path);
Fred Drake699f3522000-06-29 21:12:41 +0000583
584 return _pystat_fromstructstat(st);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585}
586
587
588/* POSIX methods */
589
Guido van Rossum94f6f721999-01-06 18:42:14 +0000590static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000591"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000592Test for access to a file.";
593
594static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000595posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +0000596{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000597 char *path;
598 int mode;
599 int res;
600
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000601 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +0000602 return NULL;
603 Py_BEGIN_ALLOW_THREADS
604 res = access(path, mode);
605 Py_END_ALLOW_THREADS
606 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000607}
608
Guido van Rossumd371ff11999-01-25 16:12:23 +0000609#ifndef F_OK
610#define F_OK 0
611#endif
612#ifndef R_OK
613#define R_OK 4
614#endif
615#ifndef W_OK
616#define W_OK 2
617#endif
618#ifndef X_OK
619#define X_OK 1
620#endif
621
622#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000623static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000624"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000625Return the name of the terminal device connected to 'fd'.";
626
627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000628posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +0000629{
Guido van Rossum94f6f721999-01-06 18:42:14 +0000630 int id;
631 char *ret;
632
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000633 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +0000634 return NULL;
635
Guido van Rossum94f6f721999-01-06 18:42:14 +0000636 ret = ttyname(id);
637 if (ret == NULL)
638 return(posix_error());
639 return(PyString_FromString(ret));
640}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000641#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000642
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000643#ifdef HAVE_CTERMID
644static char posix_ctermid__doc__[] =
645"ctermid() -> String\n\
646Return the name of the controlling terminal for this process.";
647
648static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000649posix_ctermid(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000650{
651 char *ret;
652 char buffer[L_ctermid];
653
654 if (!PyArg_ParseTuple(args, ":ctermid"))
655 return NULL;
656
Greg Wardb48bc172000-03-01 21:51:56 +0000657#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000658 ret = ctermid_r(buffer);
659#else
660 ret = ctermid(buffer);
661#endif
662 if (ret == NULL)
663 return(posix_error());
664 return(PyString_FromString(buffer));
665}
666#endif
667
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000668static char posix_chdir__doc__[] =
669"chdir(path) -> None\n\
670Change the current working directory to the specified path.";
671
Barry Warsaw53699e91996-12-10 23:23:01 +0000672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000673posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000675 return posix_1str(args, "s:chdir", chdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676}
677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000678
679static char posix_chmod__doc__[] =
680"chmod(path, mode) -> None\n\
681Change the access permissions of a file.";
682
Barry Warsaw53699e91996-12-10 23:23:01 +0000683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000684posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685{
Guido van Rossumffd15f52000-03-31 00:47:28 +0000686 char *path;
687 int i;
688 int res;
Guido van Rossum49679b42000-03-31 00:48:21 +0000689 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +0000690 return NULL;
691 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +0000692 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +0000693 Py_END_ALLOW_THREADS
694 if (res < 0)
695 return posix_error_with_filename(path);
696 Py_INCREF(Py_None);
697 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000698}
699
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000700
Guido van Rossum21142a01999-01-08 21:05:37 +0000701#ifdef HAVE_FSYNC
702static char posix_fsync__doc__[] =
703"fsync(fildes) -> None\n\
704force write of file with filedescriptor to disk.";
705
706static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000707posix_fsync(PyObject *self, PyObject *args)
Guido van Rossum21142a01999-01-08 21:05:37 +0000708{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000709 return posix_int(args, "i:fsync", fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000710}
711#endif /* HAVE_FSYNC */
712
713#ifdef HAVE_FDATASYNC
714static char posix_fdatasync__doc__[] =
715"fdatasync(fildes) -> None\n\
716force write of file with filedescriptor to disk.\n\
717 does not force update of metadata.";
718
719static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000720posix_fdatasync(PyObject *self, PyObject *args)
Guido van Rossum21142a01999-01-08 21:05:37 +0000721{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000722 return posix_int(args, "i:fdatasync", fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000723}
724#endif /* HAVE_FDATASYNC */
725
726
Fredrik Lundh10723342000-07-10 16:38:09 +0000727#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000728static char posix_chown__doc__[] =
729"chown(path, uid, gid) -> None\n\
730Change the owner and group id of path to the numeric uid and gid.";
731
Barry Warsaw53699e91996-12-10 23:23:01 +0000732static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000733posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000734{
Fredrik Lundh44328e62000-07-10 15:59:30 +0000735 char *path;
736 int uid, gid;
737 int res;
738 if (!PyArg_ParseTuple(args, "sii:chown", &path, &uid, &gid))
739 return NULL;
740 Py_BEGIN_ALLOW_THREADS
741 res = chown(path, (uid_t) uid, (gid_t) gid);
742 Py_END_ALLOW_THREADS
743 if (res < 0)
744 return posix_error_with_filename(path);
745 Py_INCREF(Py_None);
746 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000747}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000748#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000749
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000750
Guido van Rossum36bc6801995-06-14 22:54:23 +0000751#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000752static char posix_getcwd__doc__[] =
753"getcwd() -> path\n\
754Return a string representing the current working directory.";
755
Barry Warsaw53699e91996-12-10 23:23:01 +0000756static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000757posix_getcwd(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758{
759 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000760 char *res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000761 if (!PyArg_ParseTuple(args, ":getcwd"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000763 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000764 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000765 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000766 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000768 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000770#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000772
Guido van Rossumb6775db1994-08-01 11:34:53 +0000773#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000774static char posix_link__doc__[] =
775"link(src, dst) -> None\n\
776Create a hard link to a file.";
777
Barry Warsaw53699e91996-12-10 23:23:01 +0000778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000779posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000781 return posix_2str(args, "ss:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000783#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000785
786static char posix_listdir__doc__[] =
787"listdir(path) -> list_of_strings\n\
788Return a list containing the names of the entries in the directory.\n\
789\n\
790 path: path of directory to list\n\
791\n\
792The list is in arbitrary order. It does not include the special\n\
793entries '.' and '..' even if they are present in the directory.";
794
Barry Warsaw53699e91996-12-10 23:23:01 +0000795static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000796posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000798 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000799 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000800#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000801
Guido van Rossumb6775db1994-08-01 11:34:53 +0000802 char *name;
803 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000804 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000805 HANDLE hFindFile;
806 WIN32_FIND_DATA FileData;
807 char namebuf[MAX_PATH+5];
808
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000809 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000810 return NULL;
811 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000812 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000813 return NULL;
814 }
815 strcpy(namebuf, name);
816 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
817 namebuf[len++] = '/';
818 strcpy(namebuf + len, "*.*");
819
Barry Warsaw53699e91996-12-10 23:23:01 +0000820 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000821 return NULL;
822
823 hFindFile = FindFirstFile(namebuf, &FileData);
824 if (hFindFile == INVALID_HANDLE_VALUE) {
825 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000826 if (errno == ERROR_FILE_NOT_FOUND)
827 return PyList_New(0);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000828 return win32_error("FindFirstFile", name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829 }
830 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000831 if (FileData.cFileName[0] == '.' &&
832 (FileData.cFileName[1] == '\0' ||
833 FileData.cFileName[1] == '.' &&
834 FileData.cFileName[2] == '\0'))
835 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000836 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000837 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000838 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000839 d = NULL;
840 break;
841 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000842 if (PyList_Append(d, v) != 0) {
843 Py_DECREF(v);
844 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000845 d = NULL;
846 break;
847 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000848 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000849 } while (FindNextFile(hFindFile, &FileData) == TRUE);
850
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000851 if (FindClose(hFindFile) == FALSE)
852 return win32_error("FindClose", name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000853
854 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000855
Guido van Rossum8d665e61996-06-26 18:22:49 +0000856#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000857#ifdef _MSC_VER /* 16-bit Windows */
858
859#ifndef MAX_PATH
860#define MAX_PATH 250
861#endif
862 char *name, *pt;
863 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000864 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000865 char namebuf[MAX_PATH+5];
866 struct _find_t ep;
867
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000868 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000869 return NULL;
870 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000871 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000872 return NULL;
873 }
874 strcpy(namebuf, name);
875 for (pt = namebuf; *pt; pt++)
876 if (*pt == '/')
877 *pt = '\\';
878 if (namebuf[len-1] != '\\')
879 namebuf[len++] = '\\';
880 strcpy(namebuf + len, "*.*");
881
Barry Warsaw53699e91996-12-10 23:23:01 +0000882 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000883 return NULL;
884
885 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000886 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
887 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000888 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000889 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000890 }
891 do {
892 if (ep.name[0] == '.' &&
893 (ep.name[1] == '\0' ||
894 ep.name[1] == '.' &&
895 ep.name[2] == '\0'))
896 continue;
897 strcpy(namebuf, ep.name);
898 for (pt = namebuf; *pt; pt++)
899 if (isupper(*pt))
900 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000901 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000902 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000903 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000904 d = NULL;
905 break;
906 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000907 if (PyList_Append(d, v) != 0) {
908 Py_DECREF(v);
909 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000910 d = NULL;
911 break;
912 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000913 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000914 } while (_dos_findnext(&ep) == 0);
915
916 return d;
917
918#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000919#if defined(PYOS_OS2)
920
921#ifndef MAX_PATH
922#define MAX_PATH CCHMAXPATH
923#endif
924 char *name, *pt;
925 int len;
926 PyObject *d, *v;
927 char namebuf[MAX_PATH+5];
928 HDIR hdir = 1;
929 ULONG srchcnt = 1;
930 FILEFINDBUF3 ep;
931 APIRET rc;
932
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000933 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000934 return NULL;
935 if (len >= MAX_PATH) {
936 PyErr_SetString(PyExc_ValueError, "path too long");
937 return NULL;
938 }
939 strcpy(namebuf, name);
940 for (pt = namebuf; *pt; pt++)
941 if (*pt == '/')
942 *pt = '\\';
943 if (namebuf[len-1] != '\\')
944 namebuf[len++] = '\\';
945 strcpy(namebuf + len, "*.*");
946
947 if ((d = PyList_New(0)) == NULL)
948 return NULL;
949
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000950 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
951 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000952 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000953 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
954 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
955 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000956
957 if (rc != NO_ERROR) {
958 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000959 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000960 }
961
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000962 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000963 do {
964 if (ep.achName[0] == '.'
965 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000966 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000967
968 strcpy(namebuf, ep.achName);
969
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000970 /* Leave Case of Name Alone -- In Native Form */
971 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000972
973 v = PyString_FromString(namebuf);
974 if (v == NULL) {
975 Py_DECREF(d);
976 d = NULL;
977 break;
978 }
979 if (PyList_Append(d, v) != 0) {
980 Py_DECREF(v);
981 Py_DECREF(d);
982 d = NULL;
983 break;
984 }
985 Py_DECREF(v);
986 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
987 }
988
989 return d;
990#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000991
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000992 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000993 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000994 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000995 struct dirent *ep;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000996 if (!PyArg_ParseTuple(args, "s:listdir", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000997 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000998 if ((dirp = opendir(name)) == NULL) {
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000999 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001000 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001001 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001002 closedir(dirp);
1003 return NULL;
1004 }
1005 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001006 if (ep->d_name[0] == '.' &&
1007 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001008 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001009 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001010 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001011 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001012 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001013 d = NULL;
1014 break;
1015 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001016 if (PyList_Append(d, v) != 0) {
1017 Py_DECREF(v);
1018 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001019 d = NULL;
1020 break;
1021 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001022 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001023 }
1024 closedir(dirp);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001025
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001026 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001027
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001028#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001029#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +00001030#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001031}
1032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001033static char posix_mkdir__doc__[] =
1034"mkdir(path [, mode=0777]) -> None\n\
1035Create a directory.";
1036
Barry Warsaw53699e91996-12-10 23:23:01 +00001037static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001038posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001039{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001040 int res;
1041 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001042 int mode = 0777;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001043 if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001044 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001045 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001046#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001047 res = mkdir(path);
1048#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001049 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001050#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001051 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001052 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001053 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001054 Py_INCREF(Py_None);
1055 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001056}
1057
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001058
Guido van Rossumb6775db1994-08-01 11:34:53 +00001059#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001060static char posix_nice__doc__[] =
1061"nice(inc) -> new_priority\n\
1062Decrease the priority of process and return new priority.";
1063
Barry Warsaw53699e91996-12-10 23:23:01 +00001064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001065posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00001066{
1067 int increment, value;
1068
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001069 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001070 return NULL;
1071 value = nice(increment);
1072 if (value == -1)
1073 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001074 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001075}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001076#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001077
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001078
1079static char posix_rename__doc__[] =
1080"rename(old, new) -> None\n\
1081Rename a file or directory.";
1082
Barry Warsaw53699e91996-12-10 23:23:01 +00001083static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001084posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001085{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001086 return posix_2str(args, "ss:rename", rename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001087}
1088
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001089
1090static char posix_rmdir__doc__[] =
1091"rmdir(path) -> None\n\
1092Remove a directory.";
1093
Barry Warsaw53699e91996-12-10 23:23:01 +00001094static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001095posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001097 return posix_1str(args, "s:rmdir", rmdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001098}
1099
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001100
1101static char posix_stat__doc__[] =
1102"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1103Perform a stat system call on the given path.";
1104
Barry Warsaw53699e91996-12-10 23:23:01 +00001105static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001106posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001107{
Fred Drake699f3522000-06-29 21:12:41 +00001108 return posix_do_stat(self, args, "s:stat", STAT);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001109}
1110
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001111
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001112#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001113static char posix_system__doc__[] =
1114"system(command) -> exit_status\n\
1115Execute the command (a string) in a subshell.";
1116
Barry Warsaw53699e91996-12-10 23:23:01 +00001117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001118posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001119{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001120 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001121 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001122 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001123 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001124 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001125 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001126 Py_END_ALLOW_THREADS
1127 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001129#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001131
1132static char posix_umask__doc__[] =
1133"umask(new_mask) -> old_mask\n\
1134Set the current numeric umask and return the previous umask.";
1135
Barry Warsaw53699e91996-12-10 23:23:01 +00001136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001137posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001138{
1139 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001140 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001141 return NULL;
1142 i = umask(i);
1143 if (i < 0)
1144 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001145 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001146}
1147
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001148
1149static char posix_unlink__doc__[] =
1150"unlink(path) -> None\n\
1151Remove a file (same as remove(path)).";
1152
1153static char posix_remove__doc__[] =
1154"remove(path) -> None\n\
1155Remove a file (same as unlink(path)).";
1156
Barry Warsaw53699e91996-12-10 23:23:01 +00001157static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001158posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001159{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001160 return posix_1str(args, "s:remove", unlink);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161}
1162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001163
Guido van Rossumb6775db1994-08-01 11:34:53 +00001164#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001165static char posix_uname__doc__[] =
1166"uname() -> (sysname, nodename, release, version, machine)\n\
1167Return a tuple identifying the current operating system.";
1168
Barry Warsaw53699e91996-12-10 23:23:01 +00001169static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001170posix_uname(PyObject *self, PyObject *args)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001171{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001172 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001173 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001174 if (!PyArg_ParseTuple(args, ":uname"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001175 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001176 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001177 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001178 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001179 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001180 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001181 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001182 u.sysname,
1183 u.nodename,
1184 u.release,
1185 u.version,
1186 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001187}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001188#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001189
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001190
1191static char posix_utime__doc__[] =
1192"utime(path, (atime, utime)) -> None\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00001193utime(path, None) -> None\n\
1194Set the access and modified time of the file to the given values. If the\n\
1195second form is used, set the access and modified times to the current time.";
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001196
Barry Warsaw53699e91996-12-10 23:23:01 +00001197static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001198posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001199{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001200 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001201 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001202 int res;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001203 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001204
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001205/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001206#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001207 struct utimbuf buf;
1208#define ATIME buf.actime
1209#define MTIME buf.modtime
1210#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001211#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001212 time_t buf[2];
1213#define ATIME buf[0]
1214#define MTIME buf[1]
1215#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001216#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001217
Barry Warsaw3cef8562000-05-01 16:17:24 +00001218 if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001219 return NULL;
Barry Warsaw3cef8562000-05-01 16:17:24 +00001220 if (arg == Py_None) {
1221 /* optional time values not given */
1222 Py_BEGIN_ALLOW_THREADS
1223 res = utime(path, NULL);
1224 Py_END_ALLOW_THREADS
1225 }
1226 else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) {
1227 PyErr_SetString(PyExc_TypeError,
1228 "Second argument must be a 2-tuple of numbers.");
1229 return NULL;
1230 }
1231 else {
1232 ATIME = atime;
1233 MTIME = mtime;
1234 Py_BEGIN_ALLOW_THREADS
1235 res = utime(path, UTIME_ARG);
1236 Py_END_ALLOW_THREADS
1237 }
Guido van Rossumff4949e1992-08-05 19:58:53 +00001238 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001239 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001240 Py_INCREF(Py_None);
1241 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001242#undef UTIME_ARG
1243#undef ATIME
1244#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245}
1246
Guido van Rossum85e3b011991-06-03 12:42:10 +00001247
Guido van Rossum3b066191991-06-04 19:40:25 +00001248/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001250static char posix__exit__doc__[] =
1251"_exit(status)\n\
1252Exit to the system with specified status, without normal exit processing.";
1253
Barry Warsaw53699e91996-12-10 23:23:01 +00001254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001255posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001256{
1257 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001258 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001259 return NULL;
1260 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001261 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001262}
1263
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001264
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001265#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001266static char posix_execv__doc__[] =
1267"execv(path, args)\n\
1268Execute an executable path with arguments, replacing current process.\n\
1269\n\
1270 path: path of executable file\n\
1271 args: tuple or list of strings";
1272
Barry Warsaw53699e91996-12-10 23:23:01 +00001273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001274posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001275{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001276 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001277 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001278 char **argvlist;
1279 int i, argc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001280 PyObject *(*getitem)(PyObject *, int);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001281
Guido van Rossum89b33251993-10-22 14:26:06 +00001282 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001283 argv is a list or tuple of strings. */
1284
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001285 if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001286 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001287 if (PyList_Check(argv)) {
1288 argc = PyList_Size(argv);
1289 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001290 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001291 else if (PyTuple_Check(argv)) {
1292 argc = PyTuple_Size(argv);
1293 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001294 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001295 else {
Guido van Rossum50422b42000-04-26 20:34:28 +00001296 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1297 return NULL;
1298 }
1299
1300 if (argc == 0) {
1301 PyErr_SetString(PyExc_ValueError, "empty argument list");
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001302 return NULL;
1303 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001304
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001306 if (argvlist == NULL)
1307 return NULL;
1308 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001309 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1310 PyMem_DEL(argvlist);
Guido van Rossum50422b42000-04-26 20:34:28 +00001311 PyErr_SetString(PyExc_TypeError,
1312 "all arguments must be strings");
1313 return NULL;
1314
Guido van Rossum85e3b011991-06-03 12:42:10 +00001315 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001316 }
1317 argvlist[argc] = NULL;
1318
Guido van Rossumb6775db1994-08-01 11:34:53 +00001319#ifdef BAD_EXEC_PROTOTYPES
1320 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001321#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001322 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001323#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001324
Guido van Rossum85e3b011991-06-03 12:42:10 +00001325 /* If we get here it's definitely an error */
1326
Barry Warsaw53699e91996-12-10 23:23:01 +00001327 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001328 return posix_error();
1329}
1330
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001331
1332static char posix_execve__doc__[] =
1333"execve(path, args, env)\n\
1334Execute a path with arguments and environment, replacing current process.\n\
1335\n\
1336 path: path of executable file\n\
1337 args: tuple or list of arguments\n\
Thomas Wouters7e474022000-07-16 12:04:32 +00001338 env: dictionary of strings mapping to strings";
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001339
Barry Warsaw53699e91996-12-10 23:23:01 +00001340static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001341posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001342{
1343 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001344 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001345 char **argvlist;
1346 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001347 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001348 int i, pos, argc, envc;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001349 PyObject *(*getitem)(PyObject *, int);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001350
1351 /* execve has three arguments: (path, argv, env), where
1352 argv is a list or tuple of strings and env is a dictionary
1353 like posix.environ. */
1354
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001355 if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001356 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001357 if (PyList_Check(argv)) {
1358 argc = PyList_Size(argv);
1359 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001360 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001361 else if (PyTuple_Check(argv)) {
1362 argc = PyTuple_Size(argv);
1363 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001364 }
1365 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001366 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001367 return NULL;
1368 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001369 if (!PyMapping_Check(env)) {
1370 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001371 return NULL;
1372 }
1373
Guido van Rossum50422b42000-04-26 20:34:28 +00001374 if (argc == 0) {
1375 PyErr_SetString(PyExc_ValueError,
1376 "empty argument list");
1377 return NULL;
1378 }
1379
Barry Warsaw53699e91996-12-10 23:23:01 +00001380 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001381 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001382 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001383 return NULL;
1384 }
1385 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001386 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001387 "s;argv must be list of strings",
1388 &argvlist[i]))
1389 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001390 goto fail_1;
1391 }
1392 }
1393 argvlist[argc] = NULL;
1394
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001395 i = PyMapping_Size(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001396 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001397 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001398 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001399 goto fail_1;
1400 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001401 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001402 keys = PyMapping_Keys(env);
1403 vals = PyMapping_Values(env);
1404 if (!keys || !vals)
1405 goto fail_2;
1406
1407 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001408 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001409
1410 key = PyList_GetItem(keys, pos);
1411 val = PyList_GetItem(vals, pos);
1412 if (!key || !val)
1413 goto fail_2;
1414
Barry Warsaw53699e91996-12-10 23:23:01 +00001415 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001416 !PyArg_Parse(val, "s;non-string value in env", &v))
1417 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001418 goto fail_2;
1419 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001420
1421#if defined(PYOS_OS2)
1422 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1423 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1424#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001425 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001426 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001427 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001428 goto fail_2;
1429 }
1430 sprintf(p, "%s=%s", k, v);
1431 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001432#if defined(PYOS_OS2)
1433 }
1434#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001435 }
1436 envlist[envc] = 0;
1437
Guido van Rossumb6775db1994-08-01 11:34:53 +00001438
1439#ifdef BAD_EXEC_PROTOTYPES
1440 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001441#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001442 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001443#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001444
1445 /* If we get here it's definitely an error */
1446
1447 (void) posix_error();
1448
1449 fail_2:
1450 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001451 PyMem_DEL(envlist[envc]);
1452 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001453 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001454 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001455 Py_XDECREF(vals);
1456 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001457 return NULL;
1458}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001459#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001460
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001461
Guido van Rossuma1065681999-01-25 23:20:23 +00001462#ifdef HAVE_SPAWNV
1463static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001464"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001465Execute an executable path with arguments, replacing current process.\n\
1466\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001467 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001468 path: path of executable file\n\
1469 args: tuple or list of strings";
1470
1471static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001472posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00001473{
1474 char *path;
1475 PyObject *argv;
1476 char **argvlist;
1477 int mode, i, argc;
Fred Drake699f3522000-06-29 21:12:41 +00001478 intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001479 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00001480
1481 /* spawnv has three arguments: (mode, path, argv), where
1482 argv is a list or tuple of strings. */
1483
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001484 if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00001485 return NULL;
1486 if (PyList_Check(argv)) {
1487 argc = PyList_Size(argv);
1488 getitem = PyList_GetItem;
1489 }
1490 else if (PyTuple_Check(argv)) {
1491 argc = PyTuple_Size(argv);
1492 getitem = PyTuple_GetItem;
1493 }
1494 else {
Fred Drake137507e2000-06-01 02:02:46 +00001495 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossuma1065681999-01-25 23:20:23 +00001496 return NULL;
1497 }
1498
1499 argvlist = PyMem_NEW(char *, argc+1);
1500 if (argvlist == NULL)
1501 return NULL;
1502 for (i = 0; i < argc; i++) {
1503 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1504 PyMem_DEL(argvlist);
Fred Drake137507e2000-06-01 02:02:46 +00001505 PyErr_SetString(PyExc_TypeError,
1506 "all arguments must be strings");
1507 return NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00001508 }
1509 }
1510 argvlist[argc] = NULL;
1511
Guido van Rossum246bc171999-02-01 23:54:31 +00001512 if (mode == _OLD_P_OVERLAY)
1513 mode = _P_OVERLAY;
Fred Drake699f3522000-06-29 21:12:41 +00001514 spawnval = _spawnv(mode, path, argvlist);
Guido van Rossuma1065681999-01-25 23:20:23 +00001515
1516 PyMem_DEL(argvlist);
1517
Fred Drake699f3522000-06-29 21:12:41 +00001518 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00001519 return posix_error();
1520 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001521#if SIZEOF_LONG == SIZEOF_VOID_P
1522 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001523#else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001524 return Py_BuildValue("L", (LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001525#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00001526}
1527
1528
1529static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001530"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001531Execute a path with arguments and environment, replacing current process.\n\
1532\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001533 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001534 path: path of executable file\n\
1535 args: tuple or list of arguments\n\
Thomas Wouters7e474022000-07-16 12:04:32 +00001536 env: dictionary of strings mapping to strings";
Guido van Rossuma1065681999-01-25 23:20:23 +00001537
1538static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001539posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00001540{
1541 char *path;
1542 PyObject *argv, *env;
1543 char **argvlist;
1544 char **envlist;
1545 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1546 int mode, i, pos, argc, envc;
Fred Drake699f3522000-06-29 21:12:41 +00001547 intptr_t spawnval;
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001548 PyObject *(*getitem)(PyObject *, int);
Guido van Rossuma1065681999-01-25 23:20:23 +00001549
1550 /* spawnve has four arguments: (mode, path, argv, env), where
1551 argv is a list or tuple of strings and env is a dictionary
1552 like posix.environ. */
1553
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001554 if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00001555 return NULL;
1556 if (PyList_Check(argv)) {
1557 argc = PyList_Size(argv);
1558 getitem = PyList_GetItem;
1559 }
1560 else if (PyTuple_Check(argv)) {
1561 argc = PyTuple_Size(argv);
1562 getitem = PyTuple_GetItem;
1563 }
1564 else {
1565 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1566 return NULL;
1567 }
1568 if (!PyMapping_Check(env)) {
1569 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1570 return NULL;
1571 }
1572
1573 argvlist = PyMem_NEW(char *, argc+1);
1574 if (argvlist == NULL) {
1575 PyErr_NoMemory();
1576 return NULL;
1577 }
1578 for (i = 0; i < argc; i++) {
1579 if (!PyArg_Parse((*getitem)(argv, i),
1580 "s;argv must be list of strings",
1581 &argvlist[i]))
1582 {
1583 goto fail_1;
1584 }
1585 }
1586 argvlist[argc] = NULL;
1587
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001588 i = PyMapping_Size(env);
Guido van Rossuma1065681999-01-25 23:20:23 +00001589 envlist = PyMem_NEW(char *, i + 1);
1590 if (envlist == NULL) {
1591 PyErr_NoMemory();
1592 goto fail_1;
1593 }
1594 envc = 0;
1595 keys = PyMapping_Keys(env);
1596 vals = PyMapping_Values(env);
1597 if (!keys || !vals)
1598 goto fail_2;
1599
1600 for (pos = 0; pos < i; pos++) {
1601 char *p, *k, *v;
1602
1603 key = PyList_GetItem(keys, pos);
1604 val = PyList_GetItem(vals, pos);
1605 if (!key || !val)
1606 goto fail_2;
1607
1608 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1609 !PyArg_Parse(val, "s;non-string value in env", &v))
1610 {
1611 goto fail_2;
1612 }
1613 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1614 if (p == NULL) {
1615 PyErr_NoMemory();
1616 goto fail_2;
1617 }
1618 sprintf(p, "%s=%s", k, v);
1619 envlist[envc++] = p;
1620 }
1621 envlist[envc] = 0;
1622
Guido van Rossum246bc171999-02-01 23:54:31 +00001623 if (mode == _OLD_P_OVERLAY)
1624 mode = _P_OVERLAY;
Fred Drake699f3522000-06-29 21:12:41 +00001625 spawnval = _spawnve(mode, path, argvlist, envlist);
1626 if (spawnval == -1)
Guido van Rossuma1065681999-01-25 23:20:23 +00001627 (void) posix_error();
1628 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001629#if SIZEOF_LONG == SIZEOF_VOID_P
1630 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001631#else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00001632 res = Py_BuildValue("L", (LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00001633#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00001634
1635 fail_2:
1636 while (--envc >= 0)
1637 PyMem_DEL(envlist[envc]);
1638 PyMem_DEL(envlist);
1639 fail_1:
1640 PyMem_DEL(argvlist);
1641 Py_XDECREF(vals);
1642 Py_XDECREF(keys);
1643 return res;
1644}
1645#endif /* HAVE_SPAWNV */
1646
1647
Guido van Rossumad0ee831995-03-01 10:34:45 +00001648#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001649static char posix_fork__doc__[] =
1650"fork() -> pid\n\
1651Fork a child process.\n\
1652\n\
1653Return 0 to child process and PID of child to parent process.";
1654
Barry Warsaw53699e91996-12-10 23:23:01 +00001655static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001656posix_fork(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001657{
1658 int pid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001659 if (!PyArg_ParseTuple(args, ":fork"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001660 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001661 pid = fork();
1662 if (pid == -1)
1663 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00001664 if (pid == 0)
1665 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001666 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001667}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001668#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001669
Fred Drake8cef4cf2000-06-28 16:40:38 +00001670#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
1671#ifdef HAVE_PTY_H
1672#include <pty.h>
1673#else
1674#ifdef HAVE_LIBUTIL_H
1675#include <libutil.h>
Fred Drake8cef4cf2000-06-28 16:40:38 +00001676#endif /* HAVE_LIBUTIL_H */
1677#endif /* HAVE_PTY_H */
Thomas Wouters70c21a12000-07-14 14:28:33 +00001678#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00001679
Thomas Wouters70c21a12000-07-14 14:28:33 +00001680#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001681static char posix_openpty__doc__[] =
1682"openpty() -> (master_fd, slave_fd)\n\
1683Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
1684
1685static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001686posix_openpty(PyObject *self, PyObject *args)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001687{
1688 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001689#ifndef HAVE_OPENPTY
1690 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001691#endif
1692
Fred Drake8cef4cf2000-06-28 16:40:38 +00001693 if (!PyArg_ParseTuple(args, ":openpty"))
1694 return NULL;
Thomas Wouters70c21a12000-07-14 14:28:33 +00001695
1696#ifdef HAVE_OPENPTY
Fred Drake8cef4cf2000-06-28 16:40:38 +00001697 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
1698 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00001699#else
1700 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
1701 if (slave_name == NULL)
1702 return posix_error();
1703
1704 slave_fd = open(slave_name, O_RDWR);
1705 if (slave_fd < 0)
1706 return posix_error();
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00001707#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00001708
Fred Drake8cef4cf2000-06-28 16:40:38 +00001709 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00001710
Fred Drake8cef4cf2000-06-28 16:40:38 +00001711}
Thomas Wouters70c21a12000-07-14 14:28:33 +00001712#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00001713
1714#ifdef HAVE_FORKPTY
1715static char posix_forkpty__doc__[] =
1716"forkpty() -> (pid, master_fd)\n\
1717Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
1718Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
1719To both, return fd of newly opened pseudo-terminal.\n";
1720
1721static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001722posix_forkpty(PyObject *self, PyObject *args)
Fred Drake8cef4cf2000-06-28 16:40:38 +00001723{
1724 int master_fd, pid;
1725
1726 if (!PyArg_ParseTuple(args, ":forkpty"))
1727 return NULL;
1728 pid = forkpty(&master_fd, NULL, NULL, NULL);
1729 if (pid == -1)
1730 return posix_error();
Fred Drake49b0c3b2000-07-06 19:42:19 +00001731 if (pid == 0)
1732 PyOS_AfterFork();
Fred Drake8cef4cf2000-06-28 16:40:38 +00001733 return Py_BuildValue("(ii)", pid, master_fd);
1734}
1735#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001736
Guido van Rossumad0ee831995-03-01 10:34:45 +00001737#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001738static char posix_getegid__doc__[] =
1739"getegid() -> egid\n\
1740Return the current process's effective group id.";
1741
Barry Warsaw53699e91996-12-10 23:23:01 +00001742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001743posix_getegid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001744{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001745 if (!PyArg_ParseTuple(args, ":getegid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001746 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001747 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001748}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001749#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001750
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001751
Guido van Rossumad0ee831995-03-01 10:34:45 +00001752#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001753static char posix_geteuid__doc__[] =
1754"geteuid() -> euid\n\
1755Return the current process's effective user id.";
1756
Barry Warsaw53699e91996-12-10 23:23:01 +00001757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001758posix_geteuid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001759{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001760 if (!PyArg_ParseTuple(args, ":geteuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001761 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001762 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001763}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001764#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001765
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001766
Guido van Rossumad0ee831995-03-01 10:34:45 +00001767#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001768static char posix_getgid__doc__[] =
1769"getgid() -> gid\n\
1770Return the current process's group id.";
1771
Barry Warsaw53699e91996-12-10 23:23:01 +00001772static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001773posix_getgid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001774{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001775 if (!PyArg_ParseTuple(args, ":getgid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001776 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001777 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001778}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001779#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001780
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001781
1782static char posix_getpid__doc__[] =
1783"getpid() -> pid\n\
1784Return the current process id";
1785
Barry Warsaw53699e91996-12-10 23:23:01 +00001786static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001787posix_getpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001788{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001789 if (!PyArg_ParseTuple(args, ":getpid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001790 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001791 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001792}
1793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001794
Fred Drakec9680921999-12-13 16:37:25 +00001795#ifdef HAVE_GETGROUPS
1796static char posix_getgroups__doc__[] = "\
1797getgroups() -> list of group IDs\n\
1798Return list of supplemental group IDs for the process.";
1799
1800static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001801posix_getgroups(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00001802{
1803 PyObject *result = NULL;
1804
1805 if (PyArg_ParseTuple(args, ":getgroups")) {
1806#ifdef NGROUPS_MAX
1807#define MAX_GROUPS NGROUPS_MAX
1808#else
1809 /* defined to be 16 on Solaris7, so this should be a small number */
1810#define MAX_GROUPS 64
1811#endif
1812 gid_t grouplist[MAX_GROUPS];
1813 int n;
1814
1815 n = getgroups(MAX_GROUPS, grouplist);
1816 if (n < 0)
1817 posix_error();
1818 else {
1819 result = PyList_New(n);
1820 if (result != NULL) {
1821 PyObject *o;
1822 int i;
1823 for (i = 0; i < n; ++i) {
1824 o = PyInt_FromLong((long)grouplist[i]);
1825 if (o == NULL) {
1826 Py_DECREF(result);
1827 result = NULL;
1828 break;
1829 }
1830 PyList_SET_ITEM(result, i, o);
1831 }
1832 }
1833 }
1834 }
1835 return result;
1836}
1837#endif
1838
Guido van Rossumb6775db1994-08-01 11:34:53 +00001839#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001840static char posix_getpgrp__doc__[] =
1841"getpgrp() -> pgrp\n\
1842Return the current process group id.";
1843
Barry Warsaw53699e91996-12-10 23:23:01 +00001844static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001845posix_getpgrp(PyObject *self, PyObject *args)
Guido van Rossum04814471991-06-04 20:23:49 +00001846{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001847 if (!PyArg_ParseTuple(args, ":getpgrp"))
Guido van Rossum04814471991-06-04 20:23:49 +00001848 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001849#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001850 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001851#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001852 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001853#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001854}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001855#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001856
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001857
Guido van Rossumb6775db1994-08-01 11:34:53 +00001858#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001859static char posix_setpgrp__doc__[] =
1860"setpgrp() -> None\n\
1861Make this process a session leader.";
1862
Barry Warsaw53699e91996-12-10 23:23:01 +00001863static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001864posix_setpgrp(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00001865{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001866 if (!PyArg_ParseTuple(args, ":setpgrp"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001867 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001868#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001869 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001870#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001871 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001872#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001873 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001874 Py_INCREF(Py_None);
1875 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001876}
1877
Guido van Rossumb6775db1994-08-01 11:34:53 +00001878#endif /* HAVE_SETPGRP */
1879
Guido van Rossumad0ee831995-03-01 10:34:45 +00001880#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001881static char posix_getppid__doc__[] =
1882"getppid() -> ppid\n\
1883Return the parent's process id.";
1884
Barry Warsaw53699e91996-12-10 23:23:01 +00001885static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001886posix_getppid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001887{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001888 if (!PyArg_ParseTuple(args, ":getppid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001889 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001890 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001891}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001892#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001893
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001894
Fred Drake12c6e2d1999-12-14 21:25:03 +00001895#ifdef HAVE_GETLOGIN
1896static char posix_getlogin__doc__[] = "\
1897getlogin() -> string\n\
1898Return the actual login name.";
1899
1900static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001901posix_getlogin(PyObject *self, PyObject *args)
Fred Drake12c6e2d1999-12-14 21:25:03 +00001902{
1903 PyObject *result = NULL;
1904
1905 if (PyArg_ParseTuple(args, ":getlogin")) {
1906 char *name = getlogin();
1907
1908 if (name == NULL)
1909 posix_error();
1910 else
1911 result = PyString_FromString(name);
1912 }
1913 return result;
1914}
1915#endif
1916
Guido van Rossumad0ee831995-03-01 10:34:45 +00001917#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001918static char posix_getuid__doc__[] =
1919"getuid() -> uid\n\
1920Return the current process's user id.";
1921
Barry Warsaw53699e91996-12-10 23:23:01 +00001922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001923posix_getuid(PyObject *self, PyObject *args)
Guido van Rossum46003ff1992-05-15 11:05:24 +00001924{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001925 if (!PyArg_ParseTuple(args, ":getuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001926 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001927 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001928}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001929#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001930
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001931
Guido van Rossumad0ee831995-03-01 10:34:45 +00001932#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001933static char posix_kill__doc__[] =
1934"kill(pid, sig) -> None\n\
1935Kill a process with a signal.";
1936
Barry Warsaw53699e91996-12-10 23:23:01 +00001937static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001938posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00001939{
1940 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001941 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001942 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001943#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001944 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1945 APIRET rc;
1946 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001947 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001948
1949 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1950 APIRET rc;
1951 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001952 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001953
1954 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001955 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001956#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001957 if (kill(pid, sig) == -1)
1958 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001959#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001960 Py_INCREF(Py_None);
1961 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001962}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001963#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001964
Guido van Rossumc0125471996-06-28 18:55:32 +00001965#ifdef HAVE_PLOCK
1966
1967#ifdef HAVE_SYS_LOCK_H
1968#include <sys/lock.h>
1969#endif
1970
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001971static char posix_plock__doc__[] =
1972"plock(op) -> None\n\
1973Lock program segments into memory.";
1974
Barry Warsaw53699e91996-12-10 23:23:01 +00001975static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001976posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00001977{
1978 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001979 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001980 return NULL;
1981 if (plock(op) == -1)
1982 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001983 Py_INCREF(Py_None);
1984 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001985}
1986#endif
1987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001988
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001989#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001990static char posix_popen__doc__[] =
1991"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1992Open a pipe to/from a command returning a file object.";
1993
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001994#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001995static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001996async_system(const char *command)
1997{
1998 char *p, errormsg[256], args[1024];
1999 RESULTCODES rcodes;
2000 APIRET rc;
2001 char *shell = getenv("COMSPEC");
2002 if (!shell)
2003 shell = "cmd";
2004
2005 strcpy(args, shell);
2006 p = &args[ strlen(args)+1 ];
2007 strcpy(p, "/c ");
2008 strcat(p, command);
2009 p += strlen(p) + 1;
2010 *p = '\0';
2011
2012 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002013 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002014 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002015 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002016 &rcodes, shell);
2017 return rc;
2018}
2019
Guido van Rossumd48f2521997-12-05 22:19:34 +00002020static FILE *
2021popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002022{
2023 HFILE rhan, whan;
2024 FILE *retfd = NULL;
2025 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2026
Guido van Rossumd48f2521997-12-05 22:19:34 +00002027 if (rc != NO_ERROR) {
2028 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002029 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002030 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002031
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002032 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2033 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002034
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002035 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2036 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002037
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002038 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
2039 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002040
2041 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002042 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002043 }
2044
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002045 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
2046 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002047
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002048 close(oldfd); /* And Close Saved STDOUT Handle */
2049 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002050
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002051 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
2052 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002053
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002054 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2055 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002056
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002057 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
2058 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002059
2060 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002061 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002062 }
2063
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002064 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
2065 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002066
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002067 close(oldfd); /* And Close Saved STDIN Handle */
2068 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002069
Guido van Rossumd48f2521997-12-05 22:19:34 +00002070 } else {
2071 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002072 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002073 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002074}
2075
2076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002077posix_popen(PyObject *self, PyObject *args)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002078{
2079 char *name;
2080 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00002081 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002082 FILE *fp;
2083 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002084 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002085 return NULL;
2086 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00002087 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002088 Py_END_ALLOW_THREADS
2089 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002090 return os2_error(err);
2091
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002092 f = PyFile_FromFile(fp, name, mode, fclose);
2093 if (f != NULL)
2094 PyFile_SetBufSize(f, bufsize);
2095 return f;
2096}
2097
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002098#elif defined(MS_WIN32)
2099
2100/*
2101 * Portable 'popen' replacement for Win32.
2102 *
2103 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
2104 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
Mark Hammondb37a3732000-08-14 04:47:33 +00002105 * Return code handling by David Bolen <db3l@fitlinxx.com>.
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002106 */
2107
2108#include <malloc.h>
2109#include <io.h>
2110#include <fcntl.h>
2111
2112/* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
2113#define POPEN_1 1
2114#define POPEN_2 2
2115#define POPEN_3 3
2116#define POPEN_4 4
2117
2118static PyObject *_PyPopen(char *, int, int);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002119static int _PyPclose(FILE *file);
2120
2121/*
2122 * Internal dictionary mapping popen* file pointers to process handles,
Mark Hammondb37a3732000-08-14 04:47:33 +00002123 * for use when retrieving the process exit code. See _PyPclose() below
2124 * for more information on this dictionary's use.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002125 */
2126static PyObject *_PyPopenProcs = NULL;
2127
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002128
2129/* popen that works from a GUI.
2130 *
2131 * The result of this function is a pipe (file) connected to the
2132 * processes stdin or stdout, depending on the requested mode.
2133 */
2134
2135static PyObject *
2136posix_popen(PyObject *self, PyObject *args)
2137{
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002138 PyObject *f, *s;
2139 int tm = 0;
2140
2141 char *cmdstring;
2142 char *mode = "r";
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002143 int bufsize = -1;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002144 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002145 return NULL;
2146
2147 s = PyTuple_New(0);
2148
2149 if (*mode == 'r')
2150 tm = _O_RDONLY;
2151 else if (*mode != 'w') {
2152 PyErr_SetString(PyExc_ValueError, "mode must be 'r' or 'w'");
2153 return NULL;
2154 } else
2155 tm = _O_WRONLY;
2156
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002157 if (bufsize != -1) {
2158 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2159 return NULL;
2160 }
2161
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002162 if (*(mode+1) == 't')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002163 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002164 else if (*(mode+1) == 'b')
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002165 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002166 else
2167 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
2168
2169 return f;
2170}
2171
2172/* Variation on win32pipe.popen
2173 *
2174 * The result of this function is a pipe (file) connected to the
2175 * process's stdin, and a pipe connected to the process's stdout.
2176 */
2177
2178static PyObject *
2179win32_popen2(PyObject *self, PyObject *args)
2180{
2181 PyObject *f;
2182 int tm=0;
2183
2184 char *cmdstring;
2185 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002186 int bufsize = -1;
2187 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002188 return NULL;
2189
2190 if (*mode == 't')
2191 tm = _O_TEXT;
2192 else if (*mode != 'b') {
2193 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
2194 return NULL;
2195 } else
2196 tm = _O_BINARY;
2197
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002198 if (bufsize != -1) {
2199 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2200 return NULL;
2201 }
2202
2203 f = _PyPopen(cmdstring, tm, POPEN_2);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002204
2205 return f;
2206}
2207
2208/*
2209 * Variation on <om win32pipe.popen>
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002210 *
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002211 * The result of this function is 3 pipes - the process's stdin,
2212 * stdout and stderr
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002213 */
2214
2215static PyObject *
2216win32_popen3(PyObject *self, PyObject *args)
2217{
2218 PyObject *f;
2219 int tm = 0;
2220
2221 char *cmdstring;
2222 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002223 int bufsize = -1;
2224 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002225 return NULL;
2226
2227 if (*mode == 't')
2228 tm = _O_TEXT;
2229 else if (*mode != 'b') {
2230 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
2231 return NULL;
2232 } else
2233 tm = _O_BINARY;
2234
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002235 if (bufsize != -1) {
2236 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2237 return NULL;
2238 }
2239
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002240 f = _PyPopen(cmdstring, tm, POPEN_3);
2241
2242 return f;
2243}
2244
2245/*
2246 * Variation on win32pipe.popen
2247 *
2248 * The result of this function is 2 pipes - the processes stdin,
2249 * and stdout+stderr combined as a single pipe.
2250 */
2251
2252static PyObject *
2253win32_popen4(PyObject *self, PyObject *args)
2254{
2255 PyObject *f;
2256 int tm = 0;
2257
2258 char *cmdstring;
2259 char *mode = "t";
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002260 int bufsize = -1;
2261 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002262 return NULL;
2263
2264 if (*mode == 't')
2265 tm = _O_TEXT;
2266 else if (*mode != 'b') {
2267 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
2268 return NULL;
2269 } else
2270 tm = _O_BINARY;
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002271
2272 if (bufsize != -1) {
2273 PyErr_SetString(PyExc_ValueError, "bufsize must be -1");
2274 return NULL;
2275 }
2276
Fredrik Lundhbb7eeff2000-07-09 17:59:32 +00002277 f = _PyPopen(cmdstring, tm, POPEN_4);
Fredrik Lundh766ccdc2000-07-09 17:41:01 +00002278
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002279 return f;
2280}
2281
2282static int
Mark Hammondb37a3732000-08-14 04:47:33 +00002283_PyPopenCreateProcess(char *cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002284 HANDLE hStdin,
2285 HANDLE hStdout,
Mark Hammondb37a3732000-08-14 04:47:33 +00002286 HANDLE hStderr,
2287 HANDLE *hProcess)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002288{
2289 PROCESS_INFORMATION piProcInfo;
2290 STARTUPINFO siStartInfo;
2291 char *s1,*s2, *s3 = " /c ";
2292 const char *szConsoleSpawn = "w9xpopen.exe \"";
2293 int i;
2294 int x;
2295
2296 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
2297 s1 = (char *)_alloca(i);
2298 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
2299 return x;
2300 if (GetVersion() < 0x80000000) {
2301 /*
2302 * NT/2000
2303 */
2304 x = i + strlen(s3) + strlen(cmdstring) + 1;
2305 s2 = (char *)_alloca(x);
2306 ZeroMemory(s2, x);
2307 sprintf(s2, "%s%s%s", s1, s3, cmdstring);
2308 }
2309 else {
2310 /*
2311 * Oh gag, we're on Win9x. Use the workaround listed in
2312 * KB: Q150956
2313 */
2314 char modulepath[256];
2315 GetModuleFileName(NULL, modulepath, sizeof(modulepath));
2316 for (i = x = 0; modulepath[i]; i++)
2317 if (modulepath[i] == '\\')
2318 x = i+1;
2319 modulepath[x] = '\0';
2320 x = i + strlen(s3) + strlen(cmdstring) + 1 +
2321 strlen(modulepath) +
2322 strlen(szConsoleSpawn) + 1;
2323 s2 = (char *)_alloca(x);
2324 ZeroMemory(s2, x);
2325 sprintf(
2326 s2,
2327 "%s%s%s%s%s\"",
2328 modulepath,
2329 szConsoleSpawn,
2330 s1,
2331 s3,
2332 cmdstring);
2333 }
2334 }
2335
2336 /* Could be an else here to try cmd.exe / command.com in the path
2337 Now we'll just error out.. */
2338 else
2339 return -1;
2340
2341 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
2342 siStartInfo.cb = sizeof(STARTUPINFO);
2343 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
2344 siStartInfo.hStdInput = hStdin;
2345 siStartInfo.hStdOutput = hStdout;
2346 siStartInfo.hStdError = hStderr;
2347 siStartInfo.wShowWindow = SW_HIDE;
2348
2349 if (CreateProcess(NULL,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002350 s2,
2351 NULL,
2352 NULL,
2353 TRUE,
2354 CREATE_NEW_CONSOLE,
2355 NULL,
2356 NULL,
2357 &siStartInfo,
2358 &piProcInfo) ) {
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002359 /* Close the handles now so anyone waiting is woken. */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002360 CloseHandle(piProcInfo.hThread);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002361
Mark Hammondb37a3732000-08-14 04:47:33 +00002362 /* Return process handle */
2363 *hProcess = piProcInfo.hProcess;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002364 return TRUE;
2365 }
2366 return FALSE;
2367}
2368
2369/* The following code is based off of KB: Q190351 */
2370
2371static PyObject *
2372_PyPopen(char *cmdstring, int mode, int n)
2373{
2374 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
2375 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
Mark Hammondb37a3732000-08-14 04:47:33 +00002376 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002377
2378 SECURITY_ATTRIBUTES saAttr;
2379 BOOL fSuccess;
2380 int fd1, fd2, fd3;
2381 FILE *f1, *f2, *f3;
Mark Hammondb37a3732000-08-14 04:47:33 +00002382 long file_count;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002383 PyObject *f;
2384
2385 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
2386 saAttr.bInheritHandle = TRUE;
2387 saAttr.lpSecurityDescriptor = NULL;
2388
2389 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
2390 return win32_error("CreatePipe", NULL);
2391
2392 /* Create new output read handle and the input write handle. Set
2393 * the inheritance properties to FALSE. Otherwise, the child inherits
2394 * the these handles; resulting in non-closeable handles to the pipes
2395 * being created. */
2396 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002397 GetCurrentProcess(), &hChildStdinWrDup, 0,
2398 FALSE,
2399 DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002400 if (!fSuccess)
2401 return win32_error("DuplicateHandle", NULL);
2402
2403 /* Close the inheritable version of ChildStdin
2404 that we're using. */
2405 CloseHandle(hChildStdinWr);
2406
2407 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
2408 return win32_error("CreatePipe", NULL);
2409
2410 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002411 GetCurrentProcess(), &hChildStdoutRdDup, 0,
2412 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002413 if (!fSuccess)
2414 return win32_error("DuplicateHandle", NULL);
2415
2416 /* Close the inheritable version of ChildStdout
2417 that we're using. */
2418 CloseHandle(hChildStdoutRd);
2419
2420 if (n != POPEN_4) {
2421 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
2422 return win32_error("CreatePipe", NULL);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002423 fSuccess = DuplicateHandle(GetCurrentProcess(),
2424 hChildStderrRd,
2425 GetCurrentProcess(),
2426 &hChildStderrRdDup, 0,
2427 FALSE, DUPLICATE_SAME_ACCESS);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002428 if (!fSuccess)
2429 return win32_error("DuplicateHandle", NULL);
2430 /* Close the inheritable version of ChildStdErr that we're using. */
2431 CloseHandle(hChildStderrRd);
2432 }
2433
2434 switch (n) {
2435 case POPEN_1:
2436 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
2437 case _O_WRONLY | _O_TEXT:
2438 /* Case for writing to child Stdin in text mode. */
2439 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2440 f1 = _fdopen(fd1, "w");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002441 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002442 PyFile_SetBufSize(f, 0);
2443 /* We don't care about these pipes anymore, so close them. */
2444 CloseHandle(hChildStdoutRdDup);
2445 CloseHandle(hChildStderrRdDup);
2446 break;
2447
2448 case _O_RDONLY | _O_TEXT:
2449 /* Case for reading from child Stdout in text mode. */
2450 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2451 f1 = _fdopen(fd1, "r");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002452 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002453 PyFile_SetBufSize(f, 0);
2454 /* We don't care about these pipes anymore, so close them. */
2455 CloseHandle(hChildStdinWrDup);
2456 CloseHandle(hChildStderrRdDup);
2457 break;
2458
2459 case _O_RDONLY | _O_BINARY:
2460 /* Case for readinig from child Stdout in binary mode. */
2461 fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2462 f1 = _fdopen(fd1, "rb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002463 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002464 PyFile_SetBufSize(f, 0);
2465 /* We don't care about these pipes anymore, so close them. */
2466 CloseHandle(hChildStdinWrDup);
2467 CloseHandle(hChildStderrRdDup);
2468 break;
2469
2470 case _O_WRONLY | _O_BINARY:
2471 /* Case for writing to child Stdin in binary mode. */
2472 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2473 f1 = _fdopen(fd1, "wb");
Fredrik Lundh56055a42000-07-23 19:47:12 +00002474 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002475 PyFile_SetBufSize(f, 0);
2476 /* We don't care about these pipes anymore, so close them. */
2477 CloseHandle(hChildStdoutRdDup);
2478 CloseHandle(hChildStderrRdDup);
2479 break;
2480 }
Mark Hammondb37a3732000-08-14 04:47:33 +00002481 file_count = 1;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002482 break;
2483
2484 case POPEN_2:
2485 case POPEN_4:
2486 {
2487 char *m1, *m2;
2488 PyObject *p1, *p2;
2489
2490 if (mode && _O_TEXT) {
2491 m1 = "r";
2492 m2 = "w";
2493 } else {
2494 m1 = "rb";
2495 m2 = "wb";
2496 }
2497
2498 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2499 f1 = _fdopen(fd1, m2);
2500 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2501 f2 = _fdopen(fd2, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002502 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002503 PyFile_SetBufSize(p1, 0);
Mark Hammondb37a3732000-08-14 04:47:33 +00002504 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002505 PyFile_SetBufSize(p2, 0);
2506
2507 if (n != 4)
2508 CloseHandle(hChildStderrRdDup);
2509
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002510 f = Py_BuildValue("OO",p1,p2);
Mark Hammondb37a3732000-08-14 04:47:33 +00002511 file_count = 2;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002512 break;
2513 }
2514
2515 case POPEN_3:
2516 {
2517 char *m1, *m2;
2518 PyObject *p1, *p2, *p3;
2519
2520 if (mode && _O_TEXT) {
2521 m1 = "r";
2522 m2 = "w";
2523 } else {
2524 m1 = "rb";
2525 m2 = "wb";
2526 }
2527
2528 fd1 = _open_osfhandle((long)hChildStdinWrDup, mode);
2529 f1 = _fdopen(fd1, m2);
2530 fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode);
2531 f2 = _fdopen(fd2, m1);
2532 fd3 = _open_osfhandle((long)hChildStderrRdDup, mode);
2533 f3 = _fdopen(fd3, m1);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002534 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
Mark Hammondb37a3732000-08-14 04:47:33 +00002535 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
2536 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002537 PyFile_SetBufSize(p1, 0);
2538 PyFile_SetBufSize(p2, 0);
2539 PyFile_SetBufSize(p3, 0);
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002540 f = Py_BuildValue("OOO",p1,p2,p3);
Mark Hammondb37a3732000-08-14 04:47:33 +00002541 file_count = 3;
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002542 break;
2543 }
2544 }
2545
2546 if (n == POPEN_4) {
2547 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002548 hChildStdinRd,
2549 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00002550 hChildStdoutWr,
2551 &hProcess))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002552 return win32_error("CreateProcess", NULL);
2553 }
2554 else {
2555 if (!_PyPopenCreateProcess(cmdstring,
Fredrik Lundh9ac81f62000-07-09 23:35:24 +00002556 hChildStdinRd,
2557 hChildStdoutWr,
Mark Hammondb37a3732000-08-14 04:47:33 +00002558 hChildStderrWr,
2559 &hProcess))
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002560 return win32_error("CreateProcess", NULL);
2561 }
2562
Mark Hammondb37a3732000-08-14 04:47:33 +00002563 /*
2564 * Insert the files we've created into the process dictionary
2565 * all referencing the list with the process handle and the
2566 * initial number of files (see description below in _PyPclose).
2567 * Since if _PyPclose later tried to wait on a process when all
2568 * handles weren't closed, it could create a deadlock with the
2569 * child, we spend some energy here to try to ensure that we
2570 * either insert all file handles into the dictionary or none
2571 * at all. It's a little clumsy with the various popen modes
2572 * and variable number of files involved.
2573 */
2574 if (!_PyPopenProcs) {
2575 _PyPopenProcs = PyDict_New();
2576 }
2577
2578 if (_PyPopenProcs) {
2579 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
2580 int ins_rc[3];
2581
2582 fileObj[0] = fileObj[1] = fileObj[2] = NULL;
2583 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
2584
2585 procObj = PyList_New(2);
2586 hProcessObj = PyLong_FromVoidPtr(hProcess);
2587 intObj = PyInt_FromLong(file_count);
2588
2589 if (procObj && hProcessObj && intObj) {
2590 PyList_SetItem(procObj,0,hProcessObj);
2591 PyList_SetItem(procObj,1,intObj);
2592
2593 fileObj[0] = PyLong_FromVoidPtr(f1);
2594 if (fileObj[0]) {
2595 ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
2596 fileObj[0],
2597 procObj);
2598 }
2599 if (file_count >= 2) {
2600 fileObj[1] = PyLong_FromVoidPtr(f2);
2601 if (fileObj[1]) {
2602 ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
2603 fileObj[1],
2604 procObj);
2605 }
2606 }
2607 if (file_count >= 3) {
2608 fileObj[2] = PyLong_FromVoidPtr(f3);
2609 if (fileObj[2]) {
2610 ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
2611 fileObj[2],
2612 procObj);
2613 }
2614 }
2615
2616 if (ins_rc[0] < 0 || !fileObj[0] ||
2617 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
2618 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
2619 /* Something failed - remove any dictionary
2620 * entries that did make it.
2621 */
2622 if (!ins_rc[0] && fileObj[0]) {
2623 PyDict_DelItem(_PyPopenProcs,
2624 fileObj[0]);
2625 }
2626 if (!ins_rc[1] && fileObj[1]) {
2627 PyDict_DelItem(_PyPopenProcs,
2628 fileObj[1]);
2629 }
2630 if (!ins_rc[2] && fileObj[2]) {
2631 PyDict_DelItem(_PyPopenProcs,
2632 fileObj[2]);
2633 }
2634 }
2635 }
2636
2637 /*
2638 * Clean up our localized references for the dictionary keys
2639 * and value since PyDict_SetItem will Py_INCREF any copies
2640 * that got placed in the dictionary.
2641 */
2642 Py_XDECREF(procObj);
2643 Py_XDECREF(fileObj[0]);
2644 Py_XDECREF(fileObj[1]);
2645 Py_XDECREF(fileObj[2]);
2646 }
2647
Fredrik Lundhffb9c772000-07-09 14:49:51 +00002648 /* Child is launched. Close the parents copy of those pipe
2649 * handles that only the child should have open. You need to
2650 * make sure that no handles to the write end of the output pipe
2651 * are maintained in this process or else the pipe will not close
2652 * when the child process exits and the ReadFile will hang. */
2653
2654 if (!CloseHandle(hChildStdinRd))
2655 return win32_error("CloseHandle", NULL);
2656
2657 if (!CloseHandle(hChildStdoutWr))
2658 return win32_error("CloseHandle", NULL);
2659
2660 if ((n != 4) && (!CloseHandle(hChildStderrWr)))
2661 return win32_error("CloseHandle", NULL);
2662
2663 return f;
2664}
Fredrik Lundh56055a42000-07-23 19:47:12 +00002665
2666/*
2667 * Wrapper for fclose() to use for popen* files, so we can retrieve the
2668 * exit code for the child process and return as a result of the close.
Mark Hammondb37a3732000-08-14 04:47:33 +00002669 *
2670 * This function uses the _PyPopenProcs dictionary in order to map the
2671 * input file pointer to information about the process that was
2672 * originally created by the popen* call that created the file pointer.
2673 * The dictionary uses the file pointer as a key (with one entry
2674 * inserted for each file returned by the original popen* call) and a
2675 * single list object as the value for all files from a single call.
2676 * The list object contains the Win32 process handle at [0], and a file
2677 * count at [1], which is initialized to the total number of file
2678 * handles using that list.
2679 *
2680 * This function closes whichever handle it is passed, and decrements
2681 * the file count in the dictionary for the process handle pointed to
2682 * by this file. On the last close (when the file count reaches zero),
2683 * this function will wait for the child process and then return its
2684 * exit code as the result of the close() operation. This permits the
2685 * files to be closed in any order - it is always the close() of the
2686 * final handle that will return the exit code.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002687 */
2688static int _PyPclose(FILE *file)
2689{
Fredrik Lundh20318932000-07-26 17:29:12 +00002690 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002691 DWORD exit_code;
2692 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00002693 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
2694 long file_count;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002695
Fredrik Lundh20318932000-07-26 17:29:12 +00002696 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00002697 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00002698 */
2699 result = fclose(file);
2700
Fredrik Lundh56055a42000-07-23 19:47:12 +00002701 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00002702 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
2703 (procObj = PyDict_GetItem(_PyPopenProcs,
2704 fileObj)) != NULL &&
2705 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
2706 (intObj = PyList_GetItem(procObj,1)) != NULL) {
2707
2708 hProcess = PyLong_AsVoidPtr(hProcessObj);
2709 file_count = PyInt_AsLong(intObj);
2710
2711 if (file_count > 1) {
2712 /* Still other files referencing process */
2713 file_count--;
2714 PyList_SetItem(procObj,1,
2715 PyInt_FromLong(file_count));
2716 } else {
2717 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00002718 if (result != EOF &&
2719 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
2720 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00002721 /* Possible truncation here in 16-bit environments, but
2722 * real exit codes are just the lower byte in any event.
2723 */
2724 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002725 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00002726 /* Indicate failure - this will cause the file object
2727 * to raise an I/O error and translate the last Win32
2728 * error code from errno. We do have a problem with
2729 * last errors that overlap the normal errno table,
2730 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002731 */
Fredrik Lundh20318932000-07-26 17:29:12 +00002732 if (result != EOF) {
2733 /* If the error wasn't from the fclose(), then
2734 * set errno for the file object error handling.
2735 */
2736 errno = GetLastError();
2737 }
2738 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002739 }
2740
2741 /* Free up the native handle at this point */
2742 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00002743 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00002744
Mark Hammondb37a3732000-08-14 04:47:33 +00002745 /* Remove this file pointer from dictionary */
2746 PyDict_DelItem(_PyPopenProcs, fileObj);
2747
2748 if (PyDict_Size(_PyPopenProcs) == 0) {
2749 Py_DECREF(_PyPopenProcs);
2750 _PyPopenProcs = NULL;
2751 }
2752
2753 } /* if object retrieval ok */
2754
2755 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002756 } /* if _PyPopenProcs */
2757
Fredrik Lundh56055a42000-07-23 19:47:12 +00002758 return result;
2759}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002760#else
Barry Warsaw53699e91996-12-10 23:23:01 +00002761static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002762posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00002763{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002764 char *name;
2765 char *mode = "r";
2766 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00002767 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002768 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002769 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00002770 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002771 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002772 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002773 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00002774 if (fp == NULL)
2775 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002776 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002777 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002778 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002779 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00002780}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002781#endif
2782
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002783#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00002784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002785
Guido van Rossumb6775db1994-08-01 11:34:53 +00002786#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002787static char posix_setuid__doc__[] =
2788"setuid(uid) -> None\n\
2789Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00002790static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002791posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002792{
2793 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002794 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002795 return NULL;
2796 if (setuid(uid) < 0)
2797 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002798 Py_INCREF(Py_None);
2799 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002800}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002801#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002803
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00002804#ifdef HAVE_SETEUID
2805static char posix_seteuid__doc__[] =
2806"seteuid(uid) -> None\n\
2807Set the current process's effective user id.";
2808static PyObject *
2809posix_seteuid (PyObject *self, PyObject *args)
2810{
2811 int euid;
2812 if (!PyArg_ParseTuple(args, "i", &euid)) {
2813 return NULL;
2814 } else if (seteuid(euid) < 0) {
2815 return posix_error();
2816 } else {
2817 Py_INCREF(Py_None);
2818 return Py_None;
2819 }
2820}
2821#endif /* HAVE_SETEUID */
2822
2823#ifdef HAVE_SETEGID
2824static char posix_setegid__doc__[] =
2825"setegid(gid) -> None\n\
2826Set the current process's effective group id.";
2827static PyObject *
2828posix_setegid (PyObject *self, PyObject *args)
2829{
2830 int egid;
2831 if (!PyArg_ParseTuple(args, "i", &egid)) {
2832 return NULL;
2833 } else if (setegid(egid) < 0) {
2834 return posix_error();
2835 } else {
2836 Py_INCREF(Py_None);
2837 return Py_None;
2838 }
2839}
2840#endif /* HAVE_SETEGID */
2841
2842#ifdef HAVE_SETREUID
2843static char posix_setreuid__doc__[] =
2844"seteuid(ruid, euid) -> None\n\
2845Set the current process's real and effective user ids.";
2846static PyObject *
2847posix_setreuid (PyObject *self, PyObject *args)
2848{
2849 int ruid, euid;
2850 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
2851 return NULL;
2852 } else if (setreuid(ruid, euid) < 0) {
2853 return posix_error();
2854 } else {
2855 Py_INCREF(Py_None);
2856 return Py_None;
2857 }
2858}
2859#endif /* HAVE_SETREUID */
2860
2861#ifdef HAVE_SETREGID
2862static char posix_setregid__doc__[] =
2863"setegid(rgid, egid) -> None\n\
2864Set the current process's real and effective group ids.";
2865static PyObject *
2866posix_setregid (PyObject *self, PyObject *args)
2867{
2868 int rgid, egid;
2869 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
2870 return NULL;
2871 } else if (setregid(rgid, egid) < 0) {
2872 return posix_error();
2873 } else {
2874 Py_INCREF(Py_None);
2875 return Py_None;
2876 }
2877}
2878#endif /* HAVE_SETREGID */
2879
Guido van Rossumb6775db1994-08-01 11:34:53 +00002880#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002881static char posix_setgid__doc__[] =
2882"setgid(gid) -> None\n\
2883Set the current process's group id.";
2884
Barry Warsaw53699e91996-12-10 23:23:01 +00002885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002886posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002887{
2888 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002889 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002890 return NULL;
2891 if (setgid(gid) < 0)
2892 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002893 Py_INCREF(Py_None);
2894 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002895}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002896#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002898
Guido van Rossumb6775db1994-08-01 11:34:53 +00002899#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002900static char posix_waitpid__doc__[] =
2901"waitpid(pid, options) -> (pid, status)\n\
2902Wait for completion of a give child process.";
2903
Barry Warsaw53699e91996-12-10 23:23:01 +00002904static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002905posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002906{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002907 int pid, options;
2908#ifdef UNION_WAIT
2909 union wait status;
2910#define status_i (status.w_status)
2911#else
2912 int status;
2913#define status_i status
2914#endif
2915 status_i = 0;
2916
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002917 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002918 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002919 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002920#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002921 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002922#else
2923 pid = waitpid(pid, &status, options);
2924#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002925 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002926 if (pid == -1)
2927 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002928 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002929 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002930}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002931#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002933
Guido van Rossumad0ee831995-03-01 10:34:45 +00002934#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002935static char posix_wait__doc__[] =
2936"wait() -> (pid, status)\n\
2937Wait for completion of a child process.";
2938
Barry Warsaw53699e91996-12-10 23:23:01 +00002939static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002940posix_wait(PyObject *self, PyObject *args)
Guido van Rossum21803b81992-08-09 12:55:27 +00002941{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002942 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002943#ifdef UNION_WAIT
2944 union wait status;
2945#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002946#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002947 int status;
2948#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002949#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002950 if (!PyArg_ParseTuple(args, ":wait"))
2951 return NULL;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002952 status_i = 0;
2953 Py_BEGIN_ALLOW_THREADS
2954 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002955 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002956 if (pid == -1)
2957 return posix_error();
2958 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002959 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002960#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00002961}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002962#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002964
2965static char posix_lstat__doc__[] =
2966"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2967Like stat(path), but do not follow symbolic links.";
2968
Barry Warsaw53699e91996-12-10 23:23:01 +00002969static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002970posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002971{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002972#ifdef HAVE_LSTAT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002973 return posix_do_stat(self, args, "s:lstat", lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002974#else /* !HAVE_LSTAT */
Fred Drake699f3522000-06-29 21:12:41 +00002975 return posix_do_stat(self, args, "s:lstat", STAT);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002976#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002977}
2978
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002979
Guido van Rossumb6775db1994-08-01 11:34:53 +00002980#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002981static char posix_readlink__doc__[] =
2982"readlink(path) -> path\n\
2983Return a string representing the path to which the symbolic link points.";
2984
Barry Warsaw53699e91996-12-10 23:23:01 +00002985static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002986posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002987{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002988 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002989 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002990 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002991 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002992 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002993 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002994 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002995 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002996 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002997 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002998 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002999}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003000#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003002
Guido van Rossumb6775db1994-08-01 11:34:53 +00003003#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003004static char posix_symlink__doc__[] =
3005"symlink(src, dst) -> None\n\
3006Create a symbolic link.";
3007
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003008static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003009posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003010{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003011 return posix_2str(args, "ss:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003012}
3013#endif /* HAVE_SYMLINK */
3014
3015
3016#ifdef HAVE_TIMES
3017#ifndef HZ
3018#define HZ 60 /* Universal constant :-) */
3019#endif /* HZ */
3020
Guido van Rossumd48f2521997-12-05 22:19:34 +00003021#if defined(PYCC_VACPP) && defined(PYOS_OS2)
3022static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00003023system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003024{
3025 ULONG value = 0;
3026
3027 Py_BEGIN_ALLOW_THREADS
3028 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
3029 Py_END_ALLOW_THREADS
3030
3031 return value;
3032}
3033
3034static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003035posix_times(PyObject *self, PyObject *args)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003036{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003037 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossumd48f2521997-12-05 22:19:34 +00003038 return NULL;
3039
3040 /* Currently Only Uptime is Provided -- Others Later */
3041 return Py_BuildValue("ddddd",
3042 (double)0 /* t.tms_utime / HZ */,
3043 (double)0 /* t.tms_stime / HZ */,
3044 (double)0 /* t.tms_cutime / HZ */,
3045 (double)0 /* t.tms_cstime / HZ */,
3046 (double)system_uptime() / 1000);
3047}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003048#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003049static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003050posix_times(PyObject *self, PyObject *args)
Guido van Rossum22db57e1992-04-05 14:25:30 +00003051{
3052 struct tms t;
3053 clock_t c;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003054 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum22db57e1992-04-05 14:25:30 +00003055 return NULL;
3056 errno = 0;
3057 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00003058 if (c == (clock_t) -1)
3059 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003060 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00003061 (double)t.tms_utime / HZ,
3062 (double)t.tms_stime / HZ,
3063 (double)t.tms_cutime / HZ,
3064 (double)t.tms_cstime / HZ,
3065 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00003066}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003067#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003068#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003069
3070
Guido van Rossum87755a21996-09-07 00:59:43 +00003071#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003072#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00003073static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003074posix_times(PyObject *self, PyObject *args)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003075{
3076 FILETIME create, exit, kernel, user;
3077 HANDLE hProc;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003078 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003079 return NULL;
3080 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003081 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
3082 /* The fields of a FILETIME structure are the hi and lo part
3083 of a 64-bit value expressed in 100 nanosecond units.
3084 1e7 is one second in such units; 1e-7 the inverse.
3085 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
3086 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003087 return Py_BuildValue(
3088 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003089 (double)(kernel.dwHighDateTime*429.4967296 +
3090 kernel.dwLowDateTime*1e-7),
3091 (double)(user.dwHighDateTime*429.4967296 +
3092 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00003093 (double)0,
3094 (double)0,
3095 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003096}
Guido van Rossum8d665e61996-06-26 18:22:49 +00003097#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003098
3099#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00003100static char posix_times__doc__[] =
3101"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
3102Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003103#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003104
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003105
Guido van Rossumb6775db1994-08-01 11:34:53 +00003106#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003107static char posix_setsid__doc__[] =
3108"setsid() -> None\n\
3109Call the system call setsid().";
3110
Barry Warsaw53699e91996-12-10 23:23:01 +00003111static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003112posix_setsid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003113{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003114 if (!PyArg_ParseTuple(args, ":setsid"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003115 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003116 if (setsid() < 0)
3117 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003118 Py_INCREF(Py_None);
3119 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003120}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003121#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003122
Guido van Rossumb6775db1994-08-01 11:34:53 +00003123#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003124static char posix_setpgid__doc__[] =
3125"setpgid(pid, pgrp) -> None\n\
3126Call the system call setpgid().";
3127
Barry Warsaw53699e91996-12-10 23:23:01 +00003128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003129posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003130{
3131 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003132 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003133 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003134 if (setpgid(pid, pgrp) < 0)
3135 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003136 Py_INCREF(Py_None);
3137 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003138}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003139#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003140
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003141
Guido van Rossumb6775db1994-08-01 11:34:53 +00003142#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003143static char posix_tcgetpgrp__doc__[] =
3144"tcgetpgrp(fd) -> pgid\n\
3145Return the process group associated with the terminal given by a fd.";
3146
Barry Warsaw53699e91996-12-10 23:23:01 +00003147static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003148posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00003149{
3150 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003151 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003152 return NULL;
3153 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003154 if (pgid < 0)
3155 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003156 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00003157}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003158#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00003159
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003160
Guido van Rossumb6775db1994-08-01 11:34:53 +00003161#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003162static char posix_tcsetpgrp__doc__[] =
3163"tcsetpgrp(fd, pgid) -> None\n\
3164Set the process group associated with the terminal given by a fd.";
3165
Barry Warsaw53699e91996-12-10 23:23:01 +00003166static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003167posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00003168{
3169 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003170 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003171 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003172 if (tcsetpgrp(fd, pgid) < 0)
3173 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00003174 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00003175 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00003176}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003177#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00003178
Guido van Rossum687dd131993-05-17 08:34:16 +00003179/* Functions acting on file descriptors */
3180
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003181static char posix_open__doc__[] =
3182"open(filename, flag [, mode=0777]) -> fd\n\
3183Open a file (for low level IO).";
3184
Barry Warsaw53699e91996-12-10 23:23:01 +00003185static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003186posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003187{
3188 char *file;
3189 int flag;
3190 int mode = 0777;
3191 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003192 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
3193 return NULL;
3194
Barry Warsaw53699e91996-12-10 23:23:01 +00003195 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003196 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003197 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003198 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00003199 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00003200 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003201}
3202
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003203
3204static char posix_close__doc__[] =
3205"close(fd) -> None\n\
3206Close a file descriptor (for low level IO).";
3207
Barry Warsaw53699e91996-12-10 23:23:01 +00003208static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003209posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003210{
3211 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003212 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003213 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003214 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003215 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003216 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003217 if (res < 0)
3218 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003219 Py_INCREF(Py_None);
3220 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003221}
3222
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003223
3224static char posix_dup__doc__[] =
3225"dup(fd) -> fd2\n\
3226Return a duplicate of a file descriptor.";
3227
Barry Warsaw53699e91996-12-10 23:23:01 +00003228static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003229posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003230{
3231 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003232 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003233 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003234 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003235 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003236 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003237 if (fd < 0)
3238 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003239 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003240}
3241
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003242
3243static char posix_dup2__doc__[] =
3244"dup2(fd, fd2) -> None\n\
3245Duplicate file descriptor.";
3246
Barry Warsaw53699e91996-12-10 23:23:01 +00003247static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003248posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003249{
3250 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003251 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00003252 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003253 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003254 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00003255 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003256 if (res < 0)
3257 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003258 Py_INCREF(Py_None);
3259 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003260}
3261
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003262
3263static char posix_lseek__doc__[] =
3264"lseek(fd, pos, how) -> newpos\n\
3265Set the current position of a file descriptor.";
3266
Barry Warsaw53699e91996-12-10 23:23:01 +00003267static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003268posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003269{
3270 int fd, how;
Fred Drake699f3522000-06-29 21:12:41 +00003271#ifdef MS_WIN64
3272 LONG_LONG pos, res;
3273#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00003274 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00003275#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003276 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003277 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00003278 return NULL;
3279#ifdef SEEK_SET
3280 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
3281 switch (how) {
3282 case 0: how = SEEK_SET; break;
3283 case 1: how = SEEK_CUR; break;
3284 case 2: how = SEEK_END; break;
3285 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003286#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003287
3288#if !defined(HAVE_LARGEFILE_SUPPORT)
3289 pos = PyInt_AsLong(posobj);
3290#else
3291 pos = PyLong_Check(posobj) ?
3292 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
3293#endif
3294 if (PyErr_Occurred())
3295 return NULL;
3296
Barry Warsaw53699e91996-12-10 23:23:01 +00003297 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003298#ifdef MS_WIN64
3299 res = _lseeki64(fd, pos, how);
3300#else
Guido van Rossum687dd131993-05-17 08:34:16 +00003301 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00003302#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003303 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003304 if (res < 0)
3305 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00003306
3307#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00003308 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00003309#else
3310 return PyLong_FromLongLong(res);
3311#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003312}
3313
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003314
3315static char posix_read__doc__[] =
3316"read(fd, buffersize) -> string\n\
3317Read a file descriptor.";
3318
Barry Warsaw53699e91996-12-10 23:23:01 +00003319static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003320posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003321{
Guido van Rossum8bac5461996-06-11 18:38:48 +00003322 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00003323 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003324 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003325 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003326 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003327 if (buffer == NULL)
3328 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003329 Py_BEGIN_ALLOW_THREADS
3330 n = read(fd, PyString_AsString(buffer), size);
3331 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00003332 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003333 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00003334 return posix_error();
3335 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00003336 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00003337 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00003338 return buffer;
3339}
3340
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003341
3342static char posix_write__doc__[] =
3343"write(fd, string) -> byteswritten\n\
3344Write a string to a file descriptor.";
3345
Barry Warsaw53699e91996-12-10 23:23:01 +00003346static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003347posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003348{
3349 int fd, size;
3350 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003351 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003352 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003353 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003354 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00003355 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003356 if (size < 0)
3357 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003358 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003359}
3360
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003361
3362static char posix_fstat__doc__[]=
3363"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
3364Like stat(), but for an open file descriptor.";
3365
Barry Warsaw53699e91996-12-10 23:23:01 +00003366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003367posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003368{
3369 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00003370 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00003371 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003372 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003373 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003374 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003375 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00003376 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003377 if (res != 0)
3378 return posix_error();
Fred Drake699f3522000-06-29 21:12:41 +00003379
3380 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00003381}
3382
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003383
3384static char posix_fdopen__doc__[] =
3385"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
3386Return an open file object connected to a file descriptor.";
3387
Barry Warsaw53699e91996-12-10 23:23:01 +00003388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003389posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003390{
Guido van Rossum687dd131993-05-17 08:34:16 +00003391 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003392 char *mode = "r";
3393 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00003394 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00003395 PyObject *f;
3396 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00003397 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003398
Barry Warsaw53699e91996-12-10 23:23:01 +00003399 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003400 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003401 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003402 if (fp == NULL)
3403 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003404 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003405 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00003406 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003407 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00003408}
3409
Skip Montanaro1517d842000-07-19 14:34:14 +00003410static char posix_isatty__doc__[] =
3411"isatty(fd) -> Boolean\n\
3412Return true if the file descriptor 'fd' is an open file descriptor\n\
3413connected to a terminal.";
3414
3415static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00003416posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00003417{
3418 int fd;
3419 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
3420 return NULL;
3421 return Py_BuildValue("i", isatty(fd));
3422}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003423
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003424#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003425static char posix_pipe__doc__[] =
3426"pipe() -> (read_end, write_end)\n\
3427Create a pipe.";
3428
Barry Warsaw53699e91996-12-10 23:23:01 +00003429static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003430posix_pipe(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003431{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003432#if defined(PYOS_OS2)
3433 HFILE read, write;
3434 APIRET rc;
3435
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003436 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003437 return NULL;
3438
3439 Py_BEGIN_ALLOW_THREADS
3440 rc = DosCreatePipe( &read, &write, 4096);
3441 Py_END_ALLOW_THREADS
3442 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003443 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003444
3445 return Py_BuildValue("(ii)", read, write);
3446#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00003447#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00003448 int fds[2];
3449 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003450 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum687dd131993-05-17 08:34:16 +00003451 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003452 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003453 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00003454 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003455 if (res != 0)
3456 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003457 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003458#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00003459 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003460 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00003461 BOOL ok;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003462 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum794d8131994-08-23 13:48:48 +00003463 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003464 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003465 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00003466 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00003467 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003468 return win32_error("CreatePipe", NULL);
Fred Drake699f3522000-06-29 21:12:41 +00003469 read_fd = _open_osfhandle((intptr_t)read, 0);
3470 write_fd = _open_osfhandle((intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003471 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003472#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003473#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003474}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003475#endif /* HAVE_PIPE */
3476
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003477
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003478#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003479static char posix_mkfifo__doc__[] =
3480"mkfifo(file, [, mode=0666]) -> None\n\
3481Create a FIFO (a POSIX named pipe).";
3482
Barry Warsaw53699e91996-12-10 23:23:01 +00003483static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003484posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003485{
3486 char *file;
3487 int mode = 0666;
3488 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003489 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003490 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003491 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003492 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003493 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003494 if (res < 0)
3495 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003496 Py_INCREF(Py_None);
3497 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003498}
3499#endif
3500
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003501
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003502#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003503static char posix_ftruncate__doc__[] =
3504"ftruncate(fd, length) -> None\n\
3505Truncate a file to a specified length.";
3506
Barry Warsaw53699e91996-12-10 23:23:01 +00003507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003508posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003509{
3510 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003511 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003512 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003513 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003514
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003515 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003516 return NULL;
3517
3518#if !defined(HAVE_LARGEFILE_SUPPORT)
3519 length = PyInt_AsLong(lenobj);
3520#else
3521 length = PyLong_Check(lenobj) ?
3522 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
3523#endif
3524 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003525 return NULL;
3526
Barry Warsaw53699e91996-12-10 23:23:01 +00003527 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003528 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00003529 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003530 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003531 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003532 return NULL;
3533 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003534 Py_INCREF(Py_None);
3535 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003536}
3537#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003538
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003539#ifdef NeXT
3540#define HAVE_PUTENV
3541/* Steve Spicklemire got this putenv from NeXTAnswers */
3542static int
3543putenv(char *newval)
3544{
3545 extern char **environ;
3546
3547 static int firstTime = 1;
3548 char **ep;
3549 char *cp;
3550 int esiz;
3551 char *np;
3552
3553 if (!(np = strchr(newval, '=')))
3554 return 1;
3555 *np = '\0';
3556
3557 /* look it up */
3558 for (ep=environ ; *ep ; ep++)
3559 {
3560 /* this should always be true... */
3561 if (cp = strchr(*ep, '='))
3562 {
3563 *cp = '\0';
3564 if (!strcmp(*ep, newval))
3565 {
3566 /* got it! */
3567 *cp = '=';
3568 break;
3569 }
3570 *cp = '=';
3571 }
3572 else
3573 {
3574 *np = '=';
3575 return 1;
3576 }
3577 }
3578
3579 *np = '=';
3580 if (*ep)
3581 {
3582 /* the string was already there:
3583 just replace it with the new one */
3584 *ep = newval;
3585 return 0;
3586 }
3587
3588 /* expand environ by one */
3589 for (esiz=2, ep=environ ; *ep ; ep++)
3590 esiz++;
3591 if (firstTime)
3592 {
3593 char **epp;
3594 char **newenv;
3595 if (!(newenv = malloc(esiz * sizeof(char *))))
3596 return 1;
3597
3598 for (ep=environ, epp=newenv ; *ep ;)
3599 *epp++ = *ep++;
3600 *epp++ = newval;
3601 *epp = (char *) 0;
3602 environ = newenv;
3603 }
3604 else
3605 {
3606 if (!(environ = realloc(environ, esiz * sizeof(char *))))
3607 return 1;
3608 environ[esiz - 2] = newval;
3609 environ[esiz - 1] = (char *) 0;
3610 firstTime = 0;
3611 }
3612
3613 return 0;
3614}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00003615#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003616
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003617
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003618#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003619static char posix_putenv__doc__[] =
3620"putenv(key, value) -> None\n\
3621Change or add an environment variable.";
3622
Guido van Rossumbcc20741998-08-04 22:53:56 +00003623#ifdef __BEOS__
3624/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
3625int putenv( const char *str );
3626#endif
3627
Fred Drake762e2061999-08-26 17:23:54 +00003628/* Save putenv() parameters as values here, so we can collect them when they
3629 * get re-set with another call for the same key. */
3630static PyObject *posix_putenv_garbage;
3631
Barry Warsaw53699e91996-12-10 23:23:01 +00003632static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003633posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003634{
3635 char *s1, *s2;
3636 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00003637 PyObject *newstr;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003638
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003639 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003640 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003641
3642#if defined(PYOS_OS2)
3643 if (stricmp(s1, "BEGINLIBPATH") == 0) {
3644 APIRET rc;
3645
3646 if (strlen(s2) == 0) /* If New Value is an Empty String */
3647 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3648
3649 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
3650 if (rc != NO_ERROR)
3651 return os2_error(rc);
3652
3653 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
3654 APIRET rc;
3655
3656 if (strlen(s2) == 0) /* If New Value is an Empty String */
3657 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3658
3659 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
3660 if (rc != NO_ERROR)
3661 return os2_error(rc);
3662 } else {
3663#endif
3664
Fred Drake762e2061999-08-26 17:23:54 +00003665 /* XXX This can leak memory -- not easy to fix :-( */
3666 newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2);
3667 if (newstr == NULL)
3668 return PyErr_NoMemory();
3669 new = PyString_AS_STRING(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003670 (void) sprintf(new, "%s=%s", s1, s2);
3671 if (putenv(new)) {
3672 posix_error();
3673 return NULL;
3674 }
Fred Drake762e2061999-08-26 17:23:54 +00003675 /* Install the first arg and newstr in posix_putenv_garbage;
3676 * this will cause previous value to be collected. This has to
3677 * happen after the real putenv() call because the old value
3678 * was still accessible until then. */
3679 if (PyDict_SetItem(posix_putenv_garbage,
3680 PyTuple_GET_ITEM(args, 0), newstr)) {
3681 /* really not much we can do; just leak */
3682 PyErr_Clear();
3683 }
3684 else {
3685 Py_DECREF(newstr);
3686 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003687
3688#if defined(PYOS_OS2)
3689 }
3690#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003691 Py_INCREF(Py_None);
3692 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003693}
Guido van Rossumb6a47161997-09-15 22:54:34 +00003694#endif /* putenv */
3695
3696#ifdef HAVE_STRERROR
3697static char posix_strerror__doc__[] =
3698"strerror(code) -> string\n\
3699Translate an error code to a message string.";
3700
3701PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003702posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00003703{
3704 int code;
3705 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003706 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00003707 return NULL;
3708 message = strerror(code);
3709 if (message == NULL) {
3710 PyErr_SetString(PyExc_ValueError,
3711 "strerror code out of range");
3712 return NULL;
3713 }
3714 return PyString_FromString(message);
3715}
3716#endif /* strerror */
3717
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003718
Guido van Rossumc9641791998-08-04 15:26:23 +00003719#ifdef HAVE_SYS_WAIT_H
3720
3721#ifdef WIFSTOPPED
3722static char posix_WIFSTOPPED__doc__[] =
3723"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003724Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003725
3726static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003727posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003728{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003729#ifdef UNION_WAIT
3730 union wait status;
3731#define status_i (status.w_status)
3732#else
3733 int status;
3734#define status_i status
3735#endif
3736 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003737
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003738 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003739 {
3740 return NULL;
3741 }
3742
3743 return Py_BuildValue("i", WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003744#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003745}
3746#endif /* WIFSTOPPED */
3747
3748#ifdef WIFSIGNALED
3749static char posix_WIFSIGNALED__doc__[] =
3750"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00003751Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003752
3753static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003754posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003755{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003756#ifdef UNION_WAIT
3757 union wait status;
3758#define status_i (status.w_status)
3759#else
3760 int status;
3761#define status_i status
3762#endif
3763 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003764
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003765 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003766 {
3767 return NULL;
3768 }
3769
3770 return Py_BuildValue("i", WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003771#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003772}
3773#endif /* WIFSIGNALED */
3774
3775#ifdef WIFEXITED
3776static char posix_WIFEXITED__doc__[] =
3777"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003778Return true if the process returning 'status' exited using the exit()\n\
3779system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003780
3781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003782posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003783{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003784#ifdef UNION_WAIT
3785 union wait status;
3786#define status_i (status.w_status)
3787#else
3788 int status;
3789#define status_i status
3790#endif
3791 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003792
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003793 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003794 {
3795 return NULL;
3796 }
3797
3798 return Py_BuildValue("i", WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003799#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003800}
3801#endif /* WIFEXITED */
3802
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003803#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00003804static char posix_WEXITSTATUS__doc__[] =
3805"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003806Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003807
3808static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003809posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003810{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003811#ifdef UNION_WAIT
3812 union wait status;
3813#define status_i (status.w_status)
3814#else
3815 int status;
3816#define status_i status
3817#endif
3818 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003819
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003820 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003821 {
3822 return NULL;
3823 }
3824
3825 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003826#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003827}
3828#endif /* WEXITSTATUS */
3829
3830#ifdef WTERMSIG
3831static char posix_WTERMSIG__doc__[] =
3832"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003833Return the signal that terminated the process that provided the 'status'\n\
3834value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003835
3836static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003837posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003838{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003839#ifdef UNION_WAIT
3840 union wait status;
3841#define status_i (status.w_status)
3842#else
3843 int status;
3844#define status_i status
3845#endif
3846 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003847
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003848 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003849 {
3850 return NULL;
3851 }
3852
3853 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003854#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003855}
3856#endif /* WTERMSIG */
3857
3858#ifdef WSTOPSIG
3859static char posix_WSTOPSIG__doc__[] =
3860"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003861Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003862
3863static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003864posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003865{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003866#ifdef UNION_WAIT
3867 union wait status;
3868#define status_i (status.w_status)
3869#else
3870 int status;
3871#define status_i status
3872#endif
3873 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003874
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003875 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003876 {
3877 return NULL;
3878 }
3879
3880 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003881#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003882}
3883#endif /* WSTOPSIG */
3884
3885#endif /* HAVE_SYS_WAIT_H */
3886
3887
Guido van Rossum94f6f721999-01-06 18:42:14 +00003888#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00003889#ifdef _SCO_DS
3890/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
3891 needed definitions in sys/statvfs.h */
3892#define _SVID3
3893#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003894#include <sys/statvfs.h>
3895
3896static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003897"fstatvfs(fd) -> \n\
3898 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003899Perform an fstatvfs system call on the given fd.";
3900
3901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003902posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00003903{
3904 int fd, res;
3905 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003906 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003907 return NULL;
3908 Py_BEGIN_ALLOW_THREADS
3909 res = fstatvfs(fd, &st);
3910 Py_END_ALLOW_THREADS
3911 if (res != 0)
3912 return posix_error();
3913#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003914 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003915 (long) st.f_bsize,
3916 (long) st.f_frsize,
3917 (long) st.f_blocks,
3918 (long) st.f_bfree,
3919 (long) st.f_bavail,
3920 (long) st.f_files,
3921 (long) st.f_ffree,
3922 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003923 (long) st.f_flag,
3924 (long) st.f_namemax);
3925#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003926 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003927 (long) st.f_bsize,
3928 (long) st.f_frsize,
3929 (LONG_LONG) st.f_blocks,
3930 (LONG_LONG) st.f_bfree,
3931 (LONG_LONG) st.f_bavail,
3932 (LONG_LONG) st.f_files,
3933 (LONG_LONG) st.f_ffree,
3934 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003935 (long) st.f_flag,
3936 (long) st.f_namemax);
3937#endif
3938}
3939#endif /* HAVE_FSTATVFS */
3940
3941
3942#if defined(HAVE_STATVFS)
3943#include <sys/statvfs.h>
3944
3945static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003946"statvfs(path) -> \n\
3947 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003948Perform a statvfs system call on the given path.";
3949
3950static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003951posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00003952{
3953 char *path;
3954 int res;
3955 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003956 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003957 return NULL;
3958 Py_BEGIN_ALLOW_THREADS
3959 res = statvfs(path, &st);
3960 Py_END_ALLOW_THREADS
3961 if (res != 0)
3962 return posix_error_with_filename(path);
3963#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003964 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003965 (long) st.f_bsize,
3966 (long) st.f_frsize,
3967 (long) st.f_blocks,
3968 (long) st.f_bfree,
3969 (long) st.f_bavail,
3970 (long) st.f_files,
3971 (long) st.f_ffree,
3972 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003973 (long) st.f_flag,
3974 (long) st.f_namemax);
3975#else /* 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_LONG) st.f_blocks,
3980 (LONG_LONG) st.f_bfree,
3981 (LONG_LONG) st.f_bavail,
3982 (LONG_LONG) st.f_files,
3983 (LONG_LONG) st.f_ffree,
3984 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003985 (long) st.f_flag,
3986 (long) st.f_namemax);
3987#endif
3988}
3989#endif /* HAVE_STATVFS */
3990
3991
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003992#ifdef HAVE_TEMPNAM
3993static char posix_tempnam__doc__[] = "\
3994tempnam([dir[, prefix]]) -> string\n\
3995Return a unique name for a temporary file.\n\
3996The directory and a short may be specified as strings; they may be omitted\n\
3997or None if not needed.";
3998
3999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004000posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004001{
4002 PyObject *result = NULL;
4003 char *dir = NULL;
4004 char *pfx = NULL;
4005 char *name;
4006
4007 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
4008 return NULL;
4009 name = tempnam(dir, pfx);
4010 if (name == NULL)
4011 return PyErr_NoMemory();
4012 result = PyString_FromString(name);
4013 free(name);
4014 return result;
4015}
Guido van Rossumd371ff11999-01-25 16:12:23 +00004016#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004017
4018
4019#ifdef HAVE_TMPFILE
4020static char posix_tmpfile__doc__[] = "\
4021tmpfile() -> file object\n\
4022Create a temporary file with no directory entries.";
4023
4024static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004025posix_tmpfile(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004026{
4027 FILE *fp;
4028
4029 if (!PyArg_ParseTuple(args, ":tmpfile"))
4030 return NULL;
4031 fp = tmpfile();
4032 if (fp == NULL)
4033 return posix_error();
4034 return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose);
4035}
4036#endif
4037
4038
4039#ifdef HAVE_TMPNAM
4040static char posix_tmpnam__doc__[] = "\
4041tmpnam() -> string\n\
4042Return a unique name for a temporary file.";
4043
4044static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004045posix_tmpnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004046{
4047 char buffer[L_tmpnam];
4048 char *name;
4049
4050 if (!PyArg_ParseTuple(args, ":tmpnam"))
4051 return NULL;
Greg Wardb48bc172000-03-01 21:51:56 +00004052#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004053 name = tmpnam_r(buffer);
4054#else
4055 name = tmpnam(buffer);
4056#endif
4057 if (name == NULL) {
4058 PyErr_SetObject(PyExc_OSError,
4059 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00004060#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004061 "unexpected NULL from tmpnam_r"
4062#else
4063 "unexpected NULL from tmpnam"
4064#endif
4065 ));
4066 return NULL;
4067 }
4068 return PyString_FromString(buffer);
4069}
4070#endif
4071
4072
Fred Drakec9680921999-12-13 16:37:25 +00004073/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
4074 * It maps strings representing configuration variable names to
4075 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00004076 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00004077 * rarely-used constants. There are three separate tables that use
4078 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00004079 *
4080 * This code is always included, even if none of the interfaces that
4081 * need it are included. The #if hackery needed to avoid it would be
4082 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00004083 */
4084struct constdef {
4085 char *name;
4086 long value;
4087};
4088
Fred Drake12c6e2d1999-12-14 21:25:03 +00004089static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004090conv_confname(PyObject *arg, int *valuep, struct constdef *table,
4091 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004092{
4093 if (PyInt_Check(arg)) {
4094 *valuep = PyInt_AS_LONG(arg);
4095 return 1;
4096 }
4097 if (PyString_Check(arg)) {
4098 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00004099 size_t lo = 0;
4100 size_t mid;
4101 size_t hi = tablesize;
4102 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004103 char *confname = PyString_AS_STRING(arg);
4104 while (lo < hi) {
4105 mid = (lo + hi) / 2;
4106 cmp = strcmp(confname, table[mid].name);
4107 if (cmp < 0)
4108 hi = mid;
4109 else if (cmp > 0)
4110 lo = mid + 1;
4111 else {
4112 *valuep = table[mid].value;
4113 return 1;
4114 }
4115 }
4116 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
4117 }
4118 else
4119 PyErr_SetString(PyExc_TypeError,
4120 "configuration names must be strings or integers");
4121 return 0;
4122}
4123
4124
4125#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
4126static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004127#ifdef _PC_ABI_AIO_XFER_MAX
4128 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
4129#endif
4130#ifdef _PC_ABI_ASYNC_IO
4131 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
4132#endif
Fred Drakec9680921999-12-13 16:37:25 +00004133#ifdef _PC_ASYNC_IO
4134 {"PC_ASYNC_IO", _PC_ASYNC_IO},
4135#endif
4136#ifdef _PC_CHOWN_RESTRICTED
4137 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
4138#endif
4139#ifdef _PC_FILESIZEBITS
4140 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
4141#endif
4142#ifdef _PC_LAST
4143 {"PC_LAST", _PC_LAST},
4144#endif
4145#ifdef _PC_LINK_MAX
4146 {"PC_LINK_MAX", _PC_LINK_MAX},
4147#endif
4148#ifdef _PC_MAX_CANON
4149 {"PC_MAX_CANON", _PC_MAX_CANON},
4150#endif
4151#ifdef _PC_MAX_INPUT
4152 {"PC_MAX_INPUT", _PC_MAX_INPUT},
4153#endif
4154#ifdef _PC_NAME_MAX
4155 {"PC_NAME_MAX", _PC_NAME_MAX},
4156#endif
4157#ifdef _PC_NO_TRUNC
4158 {"PC_NO_TRUNC", _PC_NO_TRUNC},
4159#endif
4160#ifdef _PC_PATH_MAX
4161 {"PC_PATH_MAX", _PC_PATH_MAX},
4162#endif
4163#ifdef _PC_PIPE_BUF
4164 {"PC_PIPE_BUF", _PC_PIPE_BUF},
4165#endif
4166#ifdef _PC_PRIO_IO
4167 {"PC_PRIO_IO", _PC_PRIO_IO},
4168#endif
4169#ifdef _PC_SOCK_MAXBUF
4170 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
4171#endif
4172#ifdef _PC_SYNC_IO
4173 {"PC_SYNC_IO", _PC_SYNC_IO},
4174#endif
4175#ifdef _PC_VDISABLE
4176 {"PC_VDISABLE", _PC_VDISABLE},
4177#endif
4178};
4179
Fred Drakec9680921999-12-13 16:37:25 +00004180static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004181conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004182{
4183 return conv_confname(arg, valuep, posix_constants_pathconf,
4184 sizeof(posix_constants_pathconf)
4185 / sizeof(struct constdef));
4186}
4187#endif
4188
4189#ifdef HAVE_FPATHCONF
4190static char posix_fpathconf__doc__[] = "\
4191fpathconf(fd, name) -> integer\n\
4192Return the configuration limit name for the file descriptor fd.\n\
4193If there is no limit, return -1.";
4194
4195static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004196posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004197{
4198 PyObject *result = NULL;
4199 int name, fd;
4200
Fred Drake12c6e2d1999-12-14 21:25:03 +00004201 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
4202 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00004203 long limit;
4204
4205 errno = 0;
4206 limit = fpathconf(fd, name);
4207 if (limit == -1 && errno != 0)
4208 posix_error();
4209 else
4210 result = PyInt_FromLong(limit);
4211 }
4212 return result;
4213}
4214#endif
4215
4216
4217#ifdef HAVE_PATHCONF
4218static char posix_pathconf__doc__[] = "\
4219pathconf(path, name) -> integer\n\
4220Return the configuration limit name for the file or directory path.\n\
4221If there is no limit, return -1.";
4222
4223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004224posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004225{
4226 PyObject *result = NULL;
4227 int name;
4228 char *path;
4229
4230 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
4231 conv_path_confname, &name)) {
4232 long limit;
4233
4234 errno = 0;
4235 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004236 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00004237 if (errno == EINVAL)
4238 /* could be a path or name problem */
4239 posix_error();
4240 else
4241 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004242 }
Fred Drakec9680921999-12-13 16:37:25 +00004243 else
4244 result = PyInt_FromLong(limit);
4245 }
4246 return result;
4247}
4248#endif
4249
4250#ifdef HAVE_CONFSTR
4251static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004252#ifdef _CS_ARCHITECTURE
4253 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
4254#endif
4255#ifdef _CS_HOSTNAME
4256 {"CS_HOSTNAME", _CS_HOSTNAME},
4257#endif
4258#ifdef _CS_HW_PROVIDER
4259 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
4260#endif
4261#ifdef _CS_HW_SERIAL
4262 {"CS_HW_SERIAL", _CS_HW_SERIAL},
4263#endif
4264#ifdef _CS_INITTAB_NAME
4265 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
4266#endif
Fred Drakec9680921999-12-13 16:37:25 +00004267#ifdef _CS_LFS64_CFLAGS
4268 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
4269#endif
4270#ifdef _CS_LFS64_LDFLAGS
4271 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
4272#endif
4273#ifdef _CS_LFS64_LIBS
4274 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
4275#endif
4276#ifdef _CS_LFS64_LINTFLAGS
4277 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
4278#endif
4279#ifdef _CS_LFS_CFLAGS
4280 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
4281#endif
4282#ifdef _CS_LFS_LDFLAGS
4283 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
4284#endif
4285#ifdef _CS_LFS_LIBS
4286 {"CS_LFS_LIBS", _CS_LFS_LIBS},
4287#endif
4288#ifdef _CS_LFS_LINTFLAGS
4289 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
4290#endif
Fred Draked86ed291999-12-15 15:34:33 +00004291#ifdef _CS_MACHINE
4292 {"CS_MACHINE", _CS_MACHINE},
4293#endif
Fred Drakec9680921999-12-13 16:37:25 +00004294#ifdef _CS_PATH
4295 {"CS_PATH", _CS_PATH},
4296#endif
Fred Draked86ed291999-12-15 15:34:33 +00004297#ifdef _CS_RELEASE
4298 {"CS_RELEASE", _CS_RELEASE},
4299#endif
4300#ifdef _CS_SRPC_DOMAIN
4301 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
4302#endif
4303#ifdef _CS_SYSNAME
4304 {"CS_SYSNAME", _CS_SYSNAME},
4305#endif
4306#ifdef _CS_VERSION
4307 {"CS_VERSION", _CS_VERSION},
4308#endif
Fred Drakec9680921999-12-13 16:37:25 +00004309#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
4310 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
4311#endif
4312#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
4313 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
4314#endif
4315#ifdef _CS_XBS5_ILP32_OFF32_LIBS
4316 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
4317#endif
4318#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
4319 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
4320#endif
4321#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
4322 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
4323#endif
4324#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
4325 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
4326#endif
4327#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
4328 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
4329#endif
4330#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
4331 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
4332#endif
4333#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
4334 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
4335#endif
4336#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
4337 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
4338#endif
4339#ifdef _CS_XBS5_LP64_OFF64_LIBS
4340 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
4341#endif
4342#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
4343 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
4344#endif
4345#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
4346 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
4347#endif
4348#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
4349 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
4350#endif
4351#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
4352 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
4353#endif
4354#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
4355 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
4356#endif
Fred Draked86ed291999-12-15 15:34:33 +00004357#ifdef _MIPS_CS_AVAIL_PROCESSORS
4358 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
4359#endif
4360#ifdef _MIPS_CS_BASE
4361 {"MIPS_CS_BASE", _MIPS_CS_BASE},
4362#endif
4363#ifdef _MIPS_CS_HOSTID
4364 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
4365#endif
4366#ifdef _MIPS_CS_HW_NAME
4367 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
4368#endif
4369#ifdef _MIPS_CS_NUM_PROCESSORS
4370 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
4371#endif
4372#ifdef _MIPS_CS_OSREL_MAJ
4373 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
4374#endif
4375#ifdef _MIPS_CS_OSREL_MIN
4376 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
4377#endif
4378#ifdef _MIPS_CS_OSREL_PATCH
4379 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
4380#endif
4381#ifdef _MIPS_CS_OS_NAME
4382 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
4383#endif
4384#ifdef _MIPS_CS_OS_PROVIDER
4385 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
4386#endif
4387#ifdef _MIPS_CS_PROCESSORS
4388 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
4389#endif
4390#ifdef _MIPS_CS_SERIAL
4391 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
4392#endif
4393#ifdef _MIPS_CS_VENDOR
4394 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
4395#endif
Fred Drakec9680921999-12-13 16:37:25 +00004396};
4397
4398static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004399conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004400{
4401 return conv_confname(arg, valuep, posix_constants_confstr,
4402 sizeof(posix_constants_confstr)
4403 / sizeof(struct constdef));
4404}
4405
4406static char posix_confstr__doc__[] = "\
4407confstr(name) -> string\n\
4408Return a string-valued system configuration variable.";
4409
4410static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004411posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004412{
4413 PyObject *result = NULL;
4414 int name;
4415 char buffer[64];
4416
4417 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
4418 int len = confstr(name, buffer, sizeof(buffer));
4419
Fred Drakec9680921999-12-13 16:37:25 +00004420 errno = 0;
4421 if (len == 0) {
4422 if (errno != 0)
4423 posix_error();
4424 else
4425 result = PyString_FromString("");
4426 }
4427 else {
4428 if (len >= sizeof(buffer)) {
4429 result = PyString_FromStringAndSize(NULL, len);
4430 if (result != NULL)
4431 confstr(name, PyString_AS_STRING(result), len+1);
4432 }
4433 else
4434 result = PyString_FromString(buffer);
4435 }
4436 }
4437 return result;
4438}
4439#endif
4440
4441
4442#ifdef HAVE_SYSCONF
4443static struct constdef posix_constants_sysconf[] = {
4444#ifdef _SC_2_CHAR_TERM
4445 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
4446#endif
4447#ifdef _SC_2_C_BIND
4448 {"SC_2_C_BIND", _SC_2_C_BIND},
4449#endif
4450#ifdef _SC_2_C_DEV
4451 {"SC_2_C_DEV", _SC_2_C_DEV},
4452#endif
4453#ifdef _SC_2_C_VERSION
4454 {"SC_2_C_VERSION", _SC_2_C_VERSION},
4455#endif
4456#ifdef _SC_2_FORT_DEV
4457 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
4458#endif
4459#ifdef _SC_2_FORT_RUN
4460 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
4461#endif
4462#ifdef _SC_2_LOCALEDEF
4463 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
4464#endif
4465#ifdef _SC_2_SW_DEV
4466 {"SC_2_SW_DEV", _SC_2_SW_DEV},
4467#endif
4468#ifdef _SC_2_UPE
4469 {"SC_2_UPE", _SC_2_UPE},
4470#endif
4471#ifdef _SC_2_VERSION
4472 {"SC_2_VERSION", _SC_2_VERSION},
4473#endif
Fred Draked86ed291999-12-15 15:34:33 +00004474#ifdef _SC_ABI_ASYNCHRONOUS_IO
4475 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
4476#endif
4477#ifdef _SC_ACL
4478 {"SC_ACL", _SC_ACL},
4479#endif
Fred Drakec9680921999-12-13 16:37:25 +00004480#ifdef _SC_AIO_LISTIO_MAX
4481 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
4482#endif
Fred Drakec9680921999-12-13 16:37:25 +00004483#ifdef _SC_AIO_MAX
4484 {"SC_AIO_MAX", _SC_AIO_MAX},
4485#endif
4486#ifdef _SC_AIO_PRIO_DELTA_MAX
4487 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
4488#endif
4489#ifdef _SC_ARG_MAX
4490 {"SC_ARG_MAX", _SC_ARG_MAX},
4491#endif
4492#ifdef _SC_ASYNCHRONOUS_IO
4493 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
4494#endif
4495#ifdef _SC_ATEXIT_MAX
4496 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
4497#endif
Fred Draked86ed291999-12-15 15:34:33 +00004498#ifdef _SC_AUDIT
4499 {"SC_AUDIT", _SC_AUDIT},
4500#endif
Fred Drakec9680921999-12-13 16:37:25 +00004501#ifdef _SC_AVPHYS_PAGES
4502 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
4503#endif
4504#ifdef _SC_BC_BASE_MAX
4505 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
4506#endif
4507#ifdef _SC_BC_DIM_MAX
4508 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
4509#endif
4510#ifdef _SC_BC_SCALE_MAX
4511 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
4512#endif
4513#ifdef _SC_BC_STRING_MAX
4514 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
4515#endif
Fred Draked86ed291999-12-15 15:34:33 +00004516#ifdef _SC_CAP
4517 {"SC_CAP", _SC_CAP},
4518#endif
Fred Drakec9680921999-12-13 16:37:25 +00004519#ifdef _SC_CHARCLASS_NAME_MAX
4520 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
4521#endif
4522#ifdef _SC_CHAR_BIT
4523 {"SC_CHAR_BIT", _SC_CHAR_BIT},
4524#endif
4525#ifdef _SC_CHAR_MAX
4526 {"SC_CHAR_MAX", _SC_CHAR_MAX},
4527#endif
4528#ifdef _SC_CHAR_MIN
4529 {"SC_CHAR_MIN", _SC_CHAR_MIN},
4530#endif
4531#ifdef _SC_CHILD_MAX
4532 {"SC_CHILD_MAX", _SC_CHILD_MAX},
4533#endif
4534#ifdef _SC_CLK_TCK
4535 {"SC_CLK_TCK", _SC_CLK_TCK},
4536#endif
4537#ifdef _SC_COHER_BLKSZ
4538 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
4539#endif
4540#ifdef _SC_COLL_WEIGHTS_MAX
4541 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
4542#endif
4543#ifdef _SC_DCACHE_ASSOC
4544 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
4545#endif
4546#ifdef _SC_DCACHE_BLKSZ
4547 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
4548#endif
4549#ifdef _SC_DCACHE_LINESZ
4550 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
4551#endif
4552#ifdef _SC_DCACHE_SZ
4553 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
4554#endif
4555#ifdef _SC_DCACHE_TBLKSZ
4556 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
4557#endif
4558#ifdef _SC_DELAYTIMER_MAX
4559 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
4560#endif
4561#ifdef _SC_EQUIV_CLASS_MAX
4562 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
4563#endif
4564#ifdef _SC_EXPR_NEST_MAX
4565 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
4566#endif
4567#ifdef _SC_FSYNC
4568 {"SC_FSYNC", _SC_FSYNC},
4569#endif
4570#ifdef _SC_GETGR_R_SIZE_MAX
4571 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
4572#endif
4573#ifdef _SC_GETPW_R_SIZE_MAX
4574 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
4575#endif
4576#ifdef _SC_ICACHE_ASSOC
4577 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
4578#endif
4579#ifdef _SC_ICACHE_BLKSZ
4580 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
4581#endif
4582#ifdef _SC_ICACHE_LINESZ
4583 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
4584#endif
4585#ifdef _SC_ICACHE_SZ
4586 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
4587#endif
Fred Draked86ed291999-12-15 15:34:33 +00004588#ifdef _SC_INF
4589 {"SC_INF", _SC_INF},
4590#endif
Fred Drakec9680921999-12-13 16:37:25 +00004591#ifdef _SC_INT_MAX
4592 {"SC_INT_MAX", _SC_INT_MAX},
4593#endif
4594#ifdef _SC_INT_MIN
4595 {"SC_INT_MIN", _SC_INT_MIN},
4596#endif
4597#ifdef _SC_IOV_MAX
4598 {"SC_IOV_MAX", _SC_IOV_MAX},
4599#endif
Fred Draked86ed291999-12-15 15:34:33 +00004600#ifdef _SC_IP_SECOPTS
4601 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
4602#endif
Fred Drakec9680921999-12-13 16:37:25 +00004603#ifdef _SC_JOB_CONTROL
4604 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
4605#endif
Fred Draked86ed291999-12-15 15:34:33 +00004606#ifdef _SC_KERN_POINTERS
4607 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
4608#endif
4609#ifdef _SC_KERN_SIM
4610 {"SC_KERN_SIM", _SC_KERN_SIM},
4611#endif
Fred Drakec9680921999-12-13 16:37:25 +00004612#ifdef _SC_LINE_MAX
4613 {"SC_LINE_MAX", _SC_LINE_MAX},
4614#endif
4615#ifdef _SC_LOGIN_NAME_MAX
4616 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
4617#endif
4618#ifdef _SC_LOGNAME_MAX
4619 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
4620#endif
4621#ifdef _SC_LONG_BIT
4622 {"SC_LONG_BIT", _SC_LONG_BIT},
4623#endif
Fred Draked86ed291999-12-15 15:34:33 +00004624#ifdef _SC_MAC
4625 {"SC_MAC", _SC_MAC},
4626#endif
Fred Drakec9680921999-12-13 16:37:25 +00004627#ifdef _SC_MAPPED_FILES
4628 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
4629#endif
4630#ifdef _SC_MAXPID
4631 {"SC_MAXPID", _SC_MAXPID},
4632#endif
4633#ifdef _SC_MB_LEN_MAX
4634 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
4635#endif
4636#ifdef _SC_MEMLOCK
4637 {"SC_MEMLOCK", _SC_MEMLOCK},
4638#endif
4639#ifdef _SC_MEMLOCK_RANGE
4640 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
4641#endif
4642#ifdef _SC_MEMORY_PROTECTION
4643 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
4644#endif
4645#ifdef _SC_MESSAGE_PASSING
4646 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
4647#endif
Fred Draked86ed291999-12-15 15:34:33 +00004648#ifdef _SC_MMAP_FIXED_ALIGNMENT
4649 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
4650#endif
Fred Drakec9680921999-12-13 16:37:25 +00004651#ifdef _SC_MQ_OPEN_MAX
4652 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
4653#endif
4654#ifdef _SC_MQ_PRIO_MAX
4655 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
4656#endif
Fred Draked86ed291999-12-15 15:34:33 +00004657#ifdef _SC_NACLS_MAX
4658 {"SC_NACLS_MAX", _SC_NACLS_MAX},
4659#endif
Fred Drakec9680921999-12-13 16:37:25 +00004660#ifdef _SC_NGROUPS_MAX
4661 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
4662#endif
4663#ifdef _SC_NL_ARGMAX
4664 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
4665#endif
4666#ifdef _SC_NL_LANGMAX
4667 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
4668#endif
4669#ifdef _SC_NL_MSGMAX
4670 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
4671#endif
4672#ifdef _SC_NL_NMAX
4673 {"SC_NL_NMAX", _SC_NL_NMAX},
4674#endif
4675#ifdef _SC_NL_SETMAX
4676 {"SC_NL_SETMAX", _SC_NL_SETMAX},
4677#endif
4678#ifdef _SC_NL_TEXTMAX
4679 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
4680#endif
4681#ifdef _SC_NPROCESSORS_CONF
4682 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
4683#endif
4684#ifdef _SC_NPROCESSORS_ONLN
4685 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
4686#endif
Fred Draked86ed291999-12-15 15:34:33 +00004687#ifdef _SC_NPROC_CONF
4688 {"SC_NPROC_CONF", _SC_NPROC_CONF},
4689#endif
4690#ifdef _SC_NPROC_ONLN
4691 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
4692#endif
Fred Drakec9680921999-12-13 16:37:25 +00004693#ifdef _SC_NZERO
4694 {"SC_NZERO", _SC_NZERO},
4695#endif
4696#ifdef _SC_OPEN_MAX
4697 {"SC_OPEN_MAX", _SC_OPEN_MAX},
4698#endif
4699#ifdef _SC_PAGESIZE
4700 {"SC_PAGESIZE", _SC_PAGESIZE},
4701#endif
4702#ifdef _SC_PAGE_SIZE
4703 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
4704#endif
4705#ifdef _SC_PASS_MAX
4706 {"SC_PASS_MAX", _SC_PASS_MAX},
4707#endif
4708#ifdef _SC_PHYS_PAGES
4709 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
4710#endif
4711#ifdef _SC_PII
4712 {"SC_PII", _SC_PII},
4713#endif
4714#ifdef _SC_PII_INTERNET
4715 {"SC_PII_INTERNET", _SC_PII_INTERNET},
4716#endif
4717#ifdef _SC_PII_INTERNET_DGRAM
4718 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
4719#endif
4720#ifdef _SC_PII_INTERNET_STREAM
4721 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
4722#endif
4723#ifdef _SC_PII_OSI
4724 {"SC_PII_OSI", _SC_PII_OSI},
4725#endif
4726#ifdef _SC_PII_OSI_CLTS
4727 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
4728#endif
4729#ifdef _SC_PII_OSI_COTS
4730 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
4731#endif
4732#ifdef _SC_PII_OSI_M
4733 {"SC_PII_OSI_M", _SC_PII_OSI_M},
4734#endif
4735#ifdef _SC_PII_SOCKET
4736 {"SC_PII_SOCKET", _SC_PII_SOCKET},
4737#endif
4738#ifdef _SC_PII_XTI
4739 {"SC_PII_XTI", _SC_PII_XTI},
4740#endif
4741#ifdef _SC_POLL
4742 {"SC_POLL", _SC_POLL},
4743#endif
4744#ifdef _SC_PRIORITIZED_IO
4745 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
4746#endif
4747#ifdef _SC_PRIORITY_SCHEDULING
4748 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
4749#endif
4750#ifdef _SC_REALTIME_SIGNALS
4751 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
4752#endif
4753#ifdef _SC_RE_DUP_MAX
4754 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
4755#endif
4756#ifdef _SC_RTSIG_MAX
4757 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
4758#endif
4759#ifdef _SC_SAVED_IDS
4760 {"SC_SAVED_IDS", _SC_SAVED_IDS},
4761#endif
4762#ifdef _SC_SCHAR_MAX
4763 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
4764#endif
4765#ifdef _SC_SCHAR_MIN
4766 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
4767#endif
4768#ifdef _SC_SELECT
4769 {"SC_SELECT", _SC_SELECT},
4770#endif
4771#ifdef _SC_SEMAPHORES
4772 {"SC_SEMAPHORES", _SC_SEMAPHORES},
4773#endif
4774#ifdef _SC_SEM_NSEMS_MAX
4775 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
4776#endif
4777#ifdef _SC_SEM_VALUE_MAX
4778 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
4779#endif
4780#ifdef _SC_SHARED_MEMORY_OBJECTS
4781 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
4782#endif
4783#ifdef _SC_SHRT_MAX
4784 {"SC_SHRT_MAX", _SC_SHRT_MAX},
4785#endif
4786#ifdef _SC_SHRT_MIN
4787 {"SC_SHRT_MIN", _SC_SHRT_MIN},
4788#endif
4789#ifdef _SC_SIGQUEUE_MAX
4790 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
4791#endif
4792#ifdef _SC_SIGRT_MAX
4793 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
4794#endif
4795#ifdef _SC_SIGRT_MIN
4796 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
4797#endif
Fred Draked86ed291999-12-15 15:34:33 +00004798#ifdef _SC_SOFTPOWER
4799 {"SC_SOFTPOWER", _SC_SOFTPOWER},
4800#endif
Fred Drakec9680921999-12-13 16:37:25 +00004801#ifdef _SC_SPLIT_CACHE
4802 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
4803#endif
4804#ifdef _SC_SSIZE_MAX
4805 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
4806#endif
4807#ifdef _SC_STACK_PROT
4808 {"SC_STACK_PROT", _SC_STACK_PROT},
4809#endif
4810#ifdef _SC_STREAM_MAX
4811 {"SC_STREAM_MAX", _SC_STREAM_MAX},
4812#endif
4813#ifdef _SC_SYNCHRONIZED_IO
4814 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
4815#endif
4816#ifdef _SC_THREADS
4817 {"SC_THREADS", _SC_THREADS},
4818#endif
4819#ifdef _SC_THREAD_ATTR_STACKADDR
4820 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
4821#endif
4822#ifdef _SC_THREAD_ATTR_STACKSIZE
4823 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
4824#endif
4825#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
4826 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
4827#endif
4828#ifdef _SC_THREAD_KEYS_MAX
4829 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
4830#endif
4831#ifdef _SC_THREAD_PRIORITY_SCHEDULING
4832 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
4833#endif
4834#ifdef _SC_THREAD_PRIO_INHERIT
4835 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
4836#endif
4837#ifdef _SC_THREAD_PRIO_PROTECT
4838 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
4839#endif
4840#ifdef _SC_THREAD_PROCESS_SHARED
4841 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
4842#endif
4843#ifdef _SC_THREAD_SAFE_FUNCTIONS
4844 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
4845#endif
4846#ifdef _SC_THREAD_STACK_MIN
4847 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
4848#endif
4849#ifdef _SC_THREAD_THREADS_MAX
4850 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
4851#endif
4852#ifdef _SC_TIMERS
4853 {"SC_TIMERS", _SC_TIMERS},
4854#endif
4855#ifdef _SC_TIMER_MAX
4856 {"SC_TIMER_MAX", _SC_TIMER_MAX},
4857#endif
4858#ifdef _SC_TTY_NAME_MAX
4859 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
4860#endif
4861#ifdef _SC_TZNAME_MAX
4862 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
4863#endif
4864#ifdef _SC_T_IOV_MAX
4865 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
4866#endif
4867#ifdef _SC_UCHAR_MAX
4868 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
4869#endif
4870#ifdef _SC_UINT_MAX
4871 {"SC_UINT_MAX", _SC_UINT_MAX},
4872#endif
4873#ifdef _SC_UIO_MAXIOV
4874 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
4875#endif
4876#ifdef _SC_ULONG_MAX
4877 {"SC_ULONG_MAX", _SC_ULONG_MAX},
4878#endif
4879#ifdef _SC_USHRT_MAX
4880 {"SC_USHRT_MAX", _SC_USHRT_MAX},
4881#endif
4882#ifdef _SC_VERSION
4883 {"SC_VERSION", _SC_VERSION},
4884#endif
4885#ifdef _SC_WORD_BIT
4886 {"SC_WORD_BIT", _SC_WORD_BIT},
4887#endif
4888#ifdef _SC_XBS5_ILP32_OFF32
4889 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
4890#endif
4891#ifdef _SC_XBS5_ILP32_OFFBIG
4892 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
4893#endif
4894#ifdef _SC_XBS5_LP64_OFF64
4895 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
4896#endif
4897#ifdef _SC_XBS5_LPBIG_OFFBIG
4898 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
4899#endif
4900#ifdef _SC_XOPEN_CRYPT
4901 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
4902#endif
4903#ifdef _SC_XOPEN_ENH_I18N
4904 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
4905#endif
4906#ifdef _SC_XOPEN_LEGACY
4907 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
4908#endif
4909#ifdef _SC_XOPEN_REALTIME
4910 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
4911#endif
4912#ifdef _SC_XOPEN_REALTIME_THREADS
4913 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
4914#endif
4915#ifdef _SC_XOPEN_SHM
4916 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
4917#endif
4918#ifdef _SC_XOPEN_UNIX
4919 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
4920#endif
4921#ifdef _SC_XOPEN_VERSION
4922 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
4923#endif
4924#ifdef _SC_XOPEN_XCU_VERSION
4925 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
4926#endif
4927#ifdef _SC_XOPEN_XPG2
4928 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
4929#endif
4930#ifdef _SC_XOPEN_XPG3
4931 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
4932#endif
4933#ifdef _SC_XOPEN_XPG4
4934 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
4935#endif
4936};
4937
4938static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004939conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004940{
4941 return conv_confname(arg, valuep, posix_constants_sysconf,
4942 sizeof(posix_constants_sysconf)
4943 / sizeof(struct constdef));
4944}
4945
4946static char posix_sysconf__doc__[] = "\
4947sysconf(name) -> integer\n\
4948Return an integer-valued system configuration variable.";
4949
4950static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004951posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004952{
4953 PyObject *result = NULL;
4954 int name;
4955
4956 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
4957 int value;
4958
4959 errno = 0;
4960 value = sysconf(name);
4961 if (value == -1 && errno != 0)
4962 posix_error();
4963 else
4964 result = PyInt_FromLong(value);
4965 }
4966 return result;
4967}
4968#endif
4969
4970
Fred Drakebec628d1999-12-15 18:31:10 +00004971/* This code is used to ensure that the tables of configuration value names
4972 * are in sorted order as required by conv_confname(), and also to build the
4973 * the exported dictionaries that are used to publish information about the
4974 * names available on the host platform.
4975 *
4976 * Sorting the table at runtime ensures that the table is properly ordered
4977 * when used, even for platforms we're not able to test on. It also makes
4978 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00004979 */
Fred Drakebec628d1999-12-15 18:31:10 +00004980
4981static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004982cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00004983{
4984 const struct constdef *c1 =
4985 (const struct constdef *) v1;
4986 const struct constdef *c2 =
4987 (const struct constdef *) v2;
4988
4989 return strcmp(c1->name, c2->name);
4990}
4991
4992static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004993setup_confname_table(struct constdef *table, size_t tablesize,
4994 char *tablename, PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00004995{
Fred Drakebec628d1999-12-15 18:31:10 +00004996 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00004997 size_t i;
4998 int status;
Fred Drakebec628d1999-12-15 18:31:10 +00004999
5000 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
5001 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00005002 if (d == NULL)
5003 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005004
Barry Warsaw3155db32000-04-13 15:20:40 +00005005 for (i=0; i < tablesize; ++i) {
5006 PyObject *o = PyInt_FromLong(table[i].value);
5007 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
5008 Py_XDECREF(o);
5009 Py_DECREF(d);
5010 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005011 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005012 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00005013 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005014 status = PyDict_SetItemString(moddict, tablename, d);
5015 Py_DECREF(d);
5016 return status;
Fred Draked86ed291999-12-15 15:34:33 +00005017}
5018
Fred Drakebec628d1999-12-15 18:31:10 +00005019/* Return -1 on failure, 0 on success. */
5020static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005021setup_confname_tables(PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00005022{
5023#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00005024 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00005025 sizeof(posix_constants_pathconf)
5026 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005027 "pathconf_names", moddict))
5028 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005029#endif
5030#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00005031 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00005032 sizeof(posix_constants_confstr)
5033 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005034 "confstr_names", moddict))
5035 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005036#endif
5037#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00005038 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00005039 sizeof(posix_constants_sysconf)
5040 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005041 "sysconf_names", moddict))
5042 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005043#endif
Fred Drakebec628d1999-12-15 18:31:10 +00005044 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00005045}
Fred Draked86ed291999-12-15 15:34:33 +00005046
5047
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005048static char posix_abort__doc__[] = "\
5049abort() -> does not return!\n\
5050Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
5051in the hardest way possible on the hosting operating system.";
5052
5053static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005054posix_abort(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005055{
5056 if (!PyArg_ParseTuple(args, ":abort"))
5057 return NULL;
5058 abort();
5059 /*NOTREACHED*/
5060 Py_FatalError("abort() called from Python code didn't abort!");
5061 return NULL;
5062}
Fred Drakebec628d1999-12-15 18:31:10 +00005063
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005064
5065static PyMethodDef posix_methods[] = {
5066 {"access", posix_access, METH_VARARGS, posix_access__doc__},
5067#ifdef HAVE_TTYNAME
5068 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
5069#endif
5070 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
5071 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005072#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005073 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005074#endif /* HAVE_CHOWN */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005075#ifdef HAVE_CTERMID
5076 {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__},
5077#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00005078#ifdef HAVE_GETCWD
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005079 {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00005080#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005081#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005082 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005083#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005084 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
5085 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
5086 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005087#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005088 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005089#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005090#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005091 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005092#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005093 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
5094 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
5095 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005096#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005097 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005098#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005099#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005100 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005101#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005102 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005103#ifdef HAVE_UNAME
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005104 {"uname", posix_uname, METH_VARARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005105#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005106 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
5107 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
5108 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005109#ifdef HAVE_TIMES
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005110 {"times", posix_times, METH_VARARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005111#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005112 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005113#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005114 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
5115 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005116#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00005117#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005118 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
5119 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00005120#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005121#ifdef HAVE_FORK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005122 {"fork", posix_fork, METH_VARARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005123#endif /* HAVE_FORK */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005124#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005125 {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__},
Thomas Wouters70c21a12000-07-14 14:28:33 +00005126#endif /* HAVE_OPENPTY || HAVE__GETPTY */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005127#ifdef HAVE_FORKPTY
5128 {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
5129#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005130#ifdef HAVE_GETEGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005131 {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005132#endif /* HAVE_GETEGID */
5133#ifdef HAVE_GETEUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005134 {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005135#endif /* HAVE_GETEUID */
5136#ifdef HAVE_GETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005137 {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005138#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00005139#ifdef HAVE_GETGROUPS
5140 {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__},
5141#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005142 {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005143#ifdef HAVE_GETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005144 {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005145#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005146#ifdef HAVE_GETPPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005147 {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005148#endif /* HAVE_GETPPID */
5149#ifdef HAVE_GETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005150 {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005151#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005152#ifdef HAVE_GETLOGIN
5153 {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__},
5154#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00005155#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005156 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005157#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00005158#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005159 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00005160#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005161#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005162 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005163#ifdef MS_WIN32
5164 {"popen2", win32_popen2, METH_VARARGS},
5165 {"popen3", win32_popen3, METH_VARARGS},
5166 {"popen4", win32_popen4, METH_VARARGS},
5167#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005168#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005169#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005170 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005171#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005172#ifdef HAVE_SETEUID
5173 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
5174#endif /* HAVE_SETEUID */
5175#ifdef HAVE_SETEGID
5176 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
5177#endif /* HAVE_SETEGID */
5178#ifdef HAVE_SETREUID
5179 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
5180#endif /* HAVE_SETREUID */
5181#ifdef HAVE_SETREGID
5182 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
5183#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005184#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005185 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005186#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005187#ifdef HAVE_SETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005188 {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005189#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005190#ifdef HAVE_WAIT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005191 {"wait", posix_wait, METH_VARARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005192#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005193#ifdef HAVE_WAITPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005194 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005195#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005196#ifdef HAVE_SETSID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005197 {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005198#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005199#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005200 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005201#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005202#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005203 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005204#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005205#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005206 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005207#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005208 {"open", posix_open, METH_VARARGS, posix_open__doc__},
5209 {"close", posix_close, METH_VARARGS, posix_close__doc__},
5210 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
5211 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
5212 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
5213 {"read", posix_read, METH_VARARGS, posix_read__doc__},
5214 {"write", posix_write, METH_VARARGS, posix_write__doc__},
5215 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
5216 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00005217 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005218#ifdef HAVE_PIPE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005219 {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005220#endif
5221#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005222 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005223#endif
5224#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005225 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005226#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005227#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005228 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005229#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00005230#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005231 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00005232#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00005233#ifdef HAVE_FSYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005234 {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005235#endif
5236#ifdef HAVE_FDATASYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005237 {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005238#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00005239#ifdef HAVE_SYS_WAIT_H
5240#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005241 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005242#endif /* WIFSTOPPED */
5243#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005244 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005245#endif /* WIFSIGNALED */
5246#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005247 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005248#endif /* WIFEXITED */
5249#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005250 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005251#endif /* WEXITSTATUS */
5252#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005253 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005254#endif /* WTERMSIG */
5255#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005256 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005257#endif /* WSTOPSIG */
5258#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005259#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005260 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005261#endif
5262#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005263 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005264#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005265#ifdef HAVE_TMPNAM
5266 {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__},
5267#endif
5268#ifdef HAVE_TEMPNAM
5269 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
5270#endif
5271#ifdef HAVE_TMPNAM
5272 {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__},
5273#endif
Fred Drakec9680921999-12-13 16:37:25 +00005274#ifdef HAVE_CONFSTR
5275 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
5276#endif
5277#ifdef HAVE_SYSCONF
5278 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
5279#endif
5280#ifdef HAVE_FPATHCONF
5281 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
5282#endif
5283#ifdef HAVE_PATHCONF
5284 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
5285#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005286 {"abort", posix_abort, METH_VARARGS, posix_abort__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005287 {NULL, NULL} /* Sentinel */
5288};
5289
5290
Barry Warsaw4a342091996-12-19 23:50:02 +00005291static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005292ins(PyObject *d, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00005293{
5294 PyObject* v = PyInt_FromLong(value);
5295 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
5296 return -1; /* triggers fatal error */
5297
5298 Py_DECREF(v);
5299 return 0;
5300}
5301
Guido van Rossumd48f2521997-12-05 22:19:34 +00005302#if defined(PYOS_OS2)
5303/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
5304static int insertvalues(PyObject *d)
5305{
5306 APIRET rc;
5307 ULONG values[QSV_MAX+1];
5308 PyObject *v;
5309 char *ver, tmp[10];
5310
5311 Py_BEGIN_ALLOW_THREADS
5312 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
5313 Py_END_ALLOW_THREADS
5314
5315 if (rc != NO_ERROR) {
5316 os2_error(rc);
5317 return -1;
5318 }
5319
5320 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
5321 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
5322 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
5323 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
5324 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
5325 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
5326 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
5327
5328 switch (values[QSV_VERSION_MINOR]) {
5329 case 0: ver = "2.00"; break;
5330 case 10: ver = "2.10"; break;
5331 case 11: ver = "2.11"; break;
5332 case 30: ver = "3.00"; break;
5333 case 40: ver = "4.00"; break;
5334 case 50: ver = "5.00"; break;
5335 default:
5336 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
5337 values[QSV_VERSION_MINOR]);
5338 ver = &tmp[0];
5339 }
5340
5341 /* Add Indicator of the Version of the Operating System */
5342 v = PyString_FromString(ver);
5343 if (!v || PyDict_SetItemString(d, "version", v) < 0)
5344 return -1;
5345 Py_DECREF(v);
5346
5347 /* Add Indicator of Which Drive was Used to Boot the System */
5348 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
5349 tmp[1] = ':';
5350 tmp[2] = '\0';
5351
5352 v = PyString_FromString(tmp);
5353 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
5354 return -1;
5355 Py_DECREF(v);
5356
5357 return 0;
5358}
5359#endif
5360
Barry Warsaw4a342091996-12-19 23:50:02 +00005361static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005362all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00005363{
Guido van Rossum94f6f721999-01-06 18:42:14 +00005364#ifdef F_OK
5365 if (ins(d, "F_OK", (long)F_OK)) return -1;
5366#endif
5367#ifdef R_OK
5368 if (ins(d, "R_OK", (long)R_OK)) return -1;
5369#endif
5370#ifdef W_OK
5371 if (ins(d, "W_OK", (long)W_OK)) return -1;
5372#endif
5373#ifdef X_OK
5374 if (ins(d, "X_OK", (long)X_OK)) return -1;
5375#endif
Fred Drakec9680921999-12-13 16:37:25 +00005376#ifdef NGROUPS_MAX
5377 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
5378#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005379#ifdef TMP_MAX
5380 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
5381#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005382#ifdef WNOHANG
5383 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
5384#endif
5385#ifdef O_RDONLY
5386 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
5387#endif
5388#ifdef O_WRONLY
5389 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
5390#endif
5391#ifdef O_RDWR
5392 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
5393#endif
5394#ifdef O_NDELAY
5395 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
5396#endif
5397#ifdef O_NONBLOCK
5398 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
5399#endif
5400#ifdef O_APPEND
5401 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
5402#endif
5403#ifdef O_DSYNC
5404 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
5405#endif
5406#ifdef O_RSYNC
5407 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
5408#endif
5409#ifdef O_SYNC
5410 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
5411#endif
5412#ifdef O_NOCTTY
5413 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
5414#endif
5415#ifdef O_CREAT
5416 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
5417#endif
5418#ifdef O_EXCL
5419 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
5420#endif
5421#ifdef O_TRUNC
5422 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
5423#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00005424#ifdef O_BINARY
5425 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
5426#endif
5427#ifdef O_TEXT
5428 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
5429#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005430
Guido van Rossum246bc171999-02-01 23:54:31 +00005431#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00005432 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
5433 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
5434 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
5435 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
5436 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00005437#endif
5438
Guido van Rossumd48f2521997-12-05 22:19:34 +00005439#if defined(PYOS_OS2)
5440 if (insertvalues(d)) return -1;
5441#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005442 return 0;
5443}
5444
5445
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005446#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005447#define INITFUNC initnt
5448#define MODNAME "nt"
5449#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005450#if defined(PYOS_OS2)
5451#define INITFUNC initos2
5452#define MODNAME "os2"
5453#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005454#define INITFUNC initposix
5455#define MODNAME "posix"
5456#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005457#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005458
Guido van Rossum3886bb61998-12-04 18:50:17 +00005459DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005460INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00005461{
Barry Warsaw53699e91996-12-10 23:23:01 +00005462 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005463
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005464 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005465 posix_methods,
5466 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005467 (PyObject *)NULL,
5468 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00005469 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005470
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005471 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005472 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00005473 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005474 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00005475 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00005476
Barry Warsaw4a342091996-12-19 23:50:02 +00005477 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00005478 return;
5479
Fred Drakebec628d1999-12-15 18:31:10 +00005480 if (setup_confname_tables(d))
5481 return;
5482
Barry Warsawca74da41999-02-09 19:31:45 +00005483 PyDict_SetItemString(d, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00005484
Guido van Rossumb3d39562000-01-31 18:41:26 +00005485#ifdef HAVE_PUTENV
Fred Drake762e2061999-08-26 17:23:54 +00005486 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00005487#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005488}