blob: 42bde9758ad19f6174cf0e5963ca929afc1ab896 [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 */
Tim Peters736aa322000-09-01 06:51:24 +00002688
2689 /* RED_FLAG 31-Aug-2000 Tim
2690 * This is always called (today!) between a pair of
2691 * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS
2692 * macros. So the thread running this has no valid thread state, as
2693 * far as Python is concerned. However, this calls some Python API
2694 * functions that cannot be called safely without a valid thread
2695 * state, in particular PyDict_GetItem.
2696 * As a temporary hack (although it may last for years ...), we
2697 * *rely* on not having a valid thread state in this function, in
2698 * order to create our own "from scratch".
2699 * This will deadlock if _PyPclose is ever called by a thread
2700 * holding the global lock.
2701 */
2702
Fredrik Lundh56055a42000-07-23 19:47:12 +00002703static int _PyPclose(FILE *file)
2704{
Fredrik Lundh20318932000-07-26 17:29:12 +00002705 int result;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002706 DWORD exit_code;
2707 HANDLE hProcess;
Mark Hammondb37a3732000-08-14 04:47:33 +00002708 PyObject *procObj, *hProcessObj, *intObj, *fileObj;
2709 long file_count;
Tim Peters736aa322000-09-01 06:51:24 +00002710#ifdef WITH_THREAD
2711 PyInterpreterState* pInterpreterState;
2712 PyThreadState* pThreadState;
2713#endif
2714
Fredrik Lundh20318932000-07-26 17:29:12 +00002715 /* Close the file handle first, to ensure it can't block the
Mark Hammondb37a3732000-08-14 04:47:33 +00002716 * child from exiting if it's the last handle.
Fredrik Lundh20318932000-07-26 17:29:12 +00002717 */
2718 result = fclose(file);
2719
Tim Peters736aa322000-09-01 06:51:24 +00002720#ifdef WITH_THREAD
2721 /* Bootstrap a valid thread state into existence. */
2722 pInterpreterState = PyInterpreterState_New();
2723 if (!pInterpreterState) {
2724 /* Well, we're hosed now! We don't have a thread
2725 * state, so can't call a nice error routine, or raise
2726 * an exception. Just die.
2727 */
2728 Py_FatalError("unable to allocate interpreter state "
2729 " when closing popen object.");
2730 return -1; /* unreachable */
2731 }
2732 pThreadState = PyThreadState_New(pInterpreterState);
2733 if (!pThreadState) {
2734 Py_FatalError("unable to allocate thread state "
2735 " when closing popen object.");
2736 return -1; /* unreachable */
2737 }
2738 /* Grab the global lock. Note that this will deadlock if the
2739 * current thread already has the lock! (see RED_FLAG comments
2740 * before this function)
2741 */
2742 PyEval_RestoreThread(pThreadState);
2743#endif
2744
Fredrik Lundh56055a42000-07-23 19:47:12 +00002745 if (_PyPopenProcs) {
Mark Hammondb37a3732000-08-14 04:47:33 +00002746 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
2747 (procObj = PyDict_GetItem(_PyPopenProcs,
2748 fileObj)) != NULL &&
2749 (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
2750 (intObj = PyList_GetItem(procObj,1)) != NULL) {
2751
2752 hProcess = PyLong_AsVoidPtr(hProcessObj);
2753 file_count = PyInt_AsLong(intObj);
2754
2755 if (file_count > 1) {
2756 /* Still other files referencing process */
2757 file_count--;
2758 PyList_SetItem(procObj,1,
2759 PyInt_FromLong(file_count));
2760 } else {
2761 /* Last file for this process */
Fredrik Lundh20318932000-07-26 17:29:12 +00002762 if (result != EOF &&
2763 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
2764 GetExitCodeProcess(hProcess, &exit_code)) {
Fredrik Lundh56055a42000-07-23 19:47:12 +00002765 /* Possible truncation here in 16-bit environments, but
2766 * real exit codes are just the lower byte in any event.
2767 */
2768 result = exit_code;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002769 } else {
Fredrik Lundh20318932000-07-26 17:29:12 +00002770 /* Indicate failure - this will cause the file object
2771 * to raise an I/O error and translate the last Win32
2772 * error code from errno. We do have a problem with
2773 * last errors that overlap the normal errno table,
2774 * but that's a consistent problem with the file object.
Fredrik Lundh56055a42000-07-23 19:47:12 +00002775 */
Fredrik Lundh20318932000-07-26 17:29:12 +00002776 if (result != EOF) {
2777 /* If the error wasn't from the fclose(), then
2778 * set errno for the file object error handling.
2779 */
2780 errno = GetLastError();
2781 }
2782 result = -1;
Fredrik Lundh56055a42000-07-23 19:47:12 +00002783 }
2784
2785 /* Free up the native handle at this point */
2786 CloseHandle(hProcess);
Mark Hammondb37a3732000-08-14 04:47:33 +00002787 }
Fredrik Lundh56055a42000-07-23 19:47:12 +00002788
Mark Hammondb37a3732000-08-14 04:47:33 +00002789 /* Remove this file pointer from dictionary */
2790 PyDict_DelItem(_PyPopenProcs, fileObj);
2791
2792 if (PyDict_Size(_PyPopenProcs) == 0) {
2793 Py_DECREF(_PyPopenProcs);
2794 _PyPopenProcs = NULL;
2795 }
2796
2797 } /* if object retrieval ok */
2798
2799 Py_XDECREF(fileObj);
Fredrik Lundh56055a42000-07-23 19:47:12 +00002800 } /* if _PyPopenProcs */
2801
Tim Peters736aa322000-09-01 06:51:24 +00002802#ifdef WITH_THREAD
2803 /* Tear down the thread & interpreter states.
2804 * Note that interpreter state clear & delete functions automatically
2805 * call the thread & clear functions, and * indeed insist on doing
2806 * that themselves. The lock must be held during the clear, but need
2807 * not be held during the delete.
2808 */
2809 PyInterpreterState_Clear(pInterpreterState);
2810 PyEval_ReleaseThread(pThreadState);
2811 PyInterpreterState_Delete(pInterpreterState);
2812#endif
2813
Fredrik Lundh56055a42000-07-23 19:47:12 +00002814 return result;
2815}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002816#else
Barry Warsaw53699e91996-12-10 23:23:01 +00002817static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002818posix_popen(PyObject *self, PyObject *args)
Guido van Rossum3b066191991-06-04 19:40:25 +00002819{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002820 char *name;
2821 char *mode = "r";
2822 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00002823 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002824 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002825 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00002826 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002827 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002828 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002829 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00002830 if (fp == NULL)
2831 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002832 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002833 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002834 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002835 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00002836}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002837#endif
2838
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002839#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00002840
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002841
Guido van Rossumb6775db1994-08-01 11:34:53 +00002842#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002843static char posix_setuid__doc__[] =
2844"setuid(uid) -> None\n\
2845Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00002846static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002847posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002848{
2849 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002850 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002851 return NULL;
2852 if (setuid(uid) < 0)
2853 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002854 Py_INCREF(Py_None);
2855 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002856}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002857#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002858
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002859
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00002860#ifdef HAVE_SETEUID
2861static char posix_seteuid__doc__[] =
2862"seteuid(uid) -> None\n\
2863Set the current process's effective user id.";
2864static PyObject *
2865posix_seteuid (PyObject *self, PyObject *args)
2866{
2867 int euid;
2868 if (!PyArg_ParseTuple(args, "i", &euid)) {
2869 return NULL;
2870 } else if (seteuid(euid) < 0) {
2871 return posix_error();
2872 } else {
2873 Py_INCREF(Py_None);
2874 return Py_None;
2875 }
2876}
2877#endif /* HAVE_SETEUID */
2878
2879#ifdef HAVE_SETEGID
2880static char posix_setegid__doc__[] =
2881"setegid(gid) -> None\n\
2882Set the current process's effective group id.";
2883static PyObject *
2884posix_setegid (PyObject *self, PyObject *args)
2885{
2886 int egid;
2887 if (!PyArg_ParseTuple(args, "i", &egid)) {
2888 return NULL;
2889 } else if (setegid(egid) < 0) {
2890 return posix_error();
2891 } else {
2892 Py_INCREF(Py_None);
2893 return Py_None;
2894 }
2895}
2896#endif /* HAVE_SETEGID */
2897
2898#ifdef HAVE_SETREUID
2899static char posix_setreuid__doc__[] =
2900"seteuid(ruid, euid) -> None\n\
2901Set the current process's real and effective user ids.";
2902static PyObject *
2903posix_setreuid (PyObject *self, PyObject *args)
2904{
2905 int ruid, euid;
2906 if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) {
2907 return NULL;
2908 } else if (setreuid(ruid, euid) < 0) {
2909 return posix_error();
2910 } else {
2911 Py_INCREF(Py_None);
2912 return Py_None;
2913 }
2914}
2915#endif /* HAVE_SETREUID */
2916
2917#ifdef HAVE_SETREGID
2918static char posix_setregid__doc__[] =
2919"setegid(rgid, egid) -> None\n\
2920Set the current process's real and effective group ids.";
2921static PyObject *
2922posix_setregid (PyObject *self, PyObject *args)
2923{
2924 int rgid, egid;
2925 if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) {
2926 return NULL;
2927 } else if (setregid(rgid, egid) < 0) {
2928 return posix_error();
2929 } else {
2930 Py_INCREF(Py_None);
2931 return Py_None;
2932 }
2933}
2934#endif /* HAVE_SETREGID */
2935
Guido van Rossumb6775db1994-08-01 11:34:53 +00002936#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002937static char posix_setgid__doc__[] =
2938"setgid(gid) -> None\n\
2939Set the current process's group id.";
2940
Barry Warsaw53699e91996-12-10 23:23:01 +00002941static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002942posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002943{
2944 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002945 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002946 return NULL;
2947 if (setgid(gid) < 0)
2948 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002949 Py_INCREF(Py_None);
2950 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002951}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002952#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002953
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002954
Guido van Rossumb6775db1994-08-01 11:34:53 +00002955#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002956static char posix_waitpid__doc__[] =
2957"waitpid(pid, options) -> (pid, status)\n\
2958Wait for completion of a give child process.";
2959
Barry Warsaw53699e91996-12-10 23:23:01 +00002960static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002961posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002962{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002963 int pid, options;
2964#ifdef UNION_WAIT
2965 union wait status;
2966#define status_i (status.w_status)
2967#else
2968 int status;
2969#define status_i status
2970#endif
2971 status_i = 0;
2972
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002973 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002974 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002975 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002976#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002977 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002978#else
2979 pid = waitpid(pid, &status, options);
2980#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002981 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002982 if (pid == -1)
2983 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002984 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002985 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002986}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002987#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002988
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002989
Guido van Rossumad0ee831995-03-01 10:34:45 +00002990#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002991static char posix_wait__doc__[] =
2992"wait() -> (pid, status)\n\
2993Wait for completion of a child process.";
2994
Barry Warsaw53699e91996-12-10 23:23:01 +00002995static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002996posix_wait(PyObject *self, PyObject *args)
Guido van Rossum21803b81992-08-09 12:55:27 +00002997{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002998 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002999#ifdef UNION_WAIT
3000 union wait status;
3001#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003002#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003003 int status;
3004#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003005#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003006 if (!PyArg_ParseTuple(args, ":wait"))
3007 return NULL;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003008 status_i = 0;
3009 Py_BEGIN_ALLOW_THREADS
3010 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00003011 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00003012 if (pid == -1)
3013 return posix_error();
3014 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003015 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003016#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00003017}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003018#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003019
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003020
3021static char posix_lstat__doc__[] =
3022"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
3023Like stat(path), but do not follow symbolic links.";
3024
Barry Warsaw53699e91996-12-10 23:23:01 +00003025static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003026posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003027{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003028#ifdef HAVE_LSTAT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003029 return posix_do_stat(self, args, "s:lstat", lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003030#else /* !HAVE_LSTAT */
Fred Drake699f3522000-06-29 21:12:41 +00003031 return posix_do_stat(self, args, "s:lstat", STAT);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003032#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003033}
3034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003035
Guido van Rossumb6775db1994-08-01 11:34:53 +00003036#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003037static char posix_readlink__doc__[] =
3038"readlink(path) -> path\n\
3039Return a string representing the path to which the symbolic link points.";
3040
Barry Warsaw53699e91996-12-10 23:23:01 +00003041static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003042posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003043{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003044 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00003045 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003046 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003047 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003048 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003049 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00003050 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00003051 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003052 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00003053 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00003054 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003055}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003056#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003057
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003058
Guido van Rossumb6775db1994-08-01 11:34:53 +00003059#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003060static char posix_symlink__doc__[] =
3061"symlink(src, dst) -> None\n\
3062Create a symbolic link.";
3063
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003065posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003066{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003067 return posix_2str(args, "ss:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003068}
3069#endif /* HAVE_SYMLINK */
3070
3071
3072#ifdef HAVE_TIMES
3073#ifndef HZ
3074#define HZ 60 /* Universal constant :-) */
3075#endif /* HZ */
3076
Guido van Rossumd48f2521997-12-05 22:19:34 +00003077#if defined(PYCC_VACPP) && defined(PYOS_OS2)
3078static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00003079system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003080{
3081 ULONG value = 0;
3082
3083 Py_BEGIN_ALLOW_THREADS
3084 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
3085 Py_END_ALLOW_THREADS
3086
3087 return value;
3088}
3089
3090static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003091posix_times(PyObject *self, PyObject *args)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003092{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003093 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossumd48f2521997-12-05 22:19:34 +00003094 return NULL;
3095
3096 /* Currently Only Uptime is Provided -- Others Later */
3097 return Py_BuildValue("ddddd",
3098 (double)0 /* t.tms_utime / HZ */,
3099 (double)0 /* t.tms_stime / HZ */,
3100 (double)0 /* t.tms_cutime / HZ */,
3101 (double)0 /* t.tms_cstime / HZ */,
3102 (double)system_uptime() / 1000);
3103}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003104#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003105static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003106posix_times(PyObject *self, PyObject *args)
Guido van Rossum22db57e1992-04-05 14:25:30 +00003107{
3108 struct tms t;
3109 clock_t c;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003110 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum22db57e1992-04-05 14:25:30 +00003111 return NULL;
3112 errno = 0;
3113 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00003114 if (c == (clock_t) -1)
3115 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003116 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00003117 (double)t.tms_utime / HZ,
3118 (double)t.tms_stime / HZ,
3119 (double)t.tms_cutime / HZ,
3120 (double)t.tms_cstime / HZ,
3121 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00003122}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003123#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003124#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003125
3126
Guido van Rossum87755a21996-09-07 00:59:43 +00003127#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003128#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00003129static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003130posix_times(PyObject *self, PyObject *args)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003131{
3132 FILETIME create, exit, kernel, user;
3133 HANDLE hProc;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003134 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003135 return NULL;
3136 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003137 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
3138 /* The fields of a FILETIME structure are the hi and lo part
3139 of a 64-bit value expressed in 100 nanosecond units.
3140 1e7 is one second in such units; 1e-7 the inverse.
3141 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
3142 */
Barry Warsaw53699e91996-12-10 23:23:01 +00003143 return Py_BuildValue(
3144 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00003145 (double)(kernel.dwHighDateTime*429.4967296 +
3146 kernel.dwLowDateTime*1e-7),
3147 (double)(user.dwHighDateTime*429.4967296 +
3148 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00003149 (double)0,
3150 (double)0,
3151 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00003152}
Guido van Rossum8d665e61996-06-26 18:22:49 +00003153#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003154
3155#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00003156static char posix_times__doc__[] =
3157"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
3158Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00003159#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003161
Guido van Rossumb6775db1994-08-01 11:34:53 +00003162#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003163static char posix_setsid__doc__[] =
3164"setsid() -> None\n\
3165Call the system call setsid().";
3166
Barry Warsaw53699e91996-12-10 23:23:01 +00003167static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003168posix_setsid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003169{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003170 if (!PyArg_ParseTuple(args, ":setsid"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003171 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003172 if (setsid() < 0)
3173 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003174 Py_INCREF(Py_None);
3175 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003176}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003177#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003178
Guido van Rossumb6775db1994-08-01 11:34:53 +00003179#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003180static char posix_setpgid__doc__[] =
3181"setpgid(pid, pgrp) -> None\n\
3182Call the system call setpgid().";
3183
Barry Warsaw53699e91996-12-10 23:23:01 +00003184static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003185posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003186{
3187 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003188 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00003189 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003190 if (setpgid(pid, pgrp) < 0)
3191 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003192 Py_INCREF(Py_None);
3193 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003194}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003195#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00003196
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003197
Guido van Rossumb6775db1994-08-01 11:34:53 +00003198#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003199static char posix_tcgetpgrp__doc__[] =
3200"tcgetpgrp(fd) -> pgid\n\
3201Return the process group associated with the terminal given by a fd.";
3202
Barry Warsaw53699e91996-12-10 23:23:01 +00003203static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003204posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00003205{
3206 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003207 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003208 return NULL;
3209 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003210 if (pgid < 0)
3211 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003212 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00003213}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003214#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00003215
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003216
Guido van Rossumb6775db1994-08-01 11:34:53 +00003217#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003218static char posix_tcsetpgrp__doc__[] =
3219"tcsetpgrp(fd, pgid) -> None\n\
3220Set the process group associated with the terminal given by a fd.";
3221
Barry Warsaw53699e91996-12-10 23:23:01 +00003222static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003223posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00003224{
3225 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003226 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00003227 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00003228 if (tcsetpgrp(fd, pgid) < 0)
3229 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00003230 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00003231 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00003232}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003233#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00003234
Guido van Rossum687dd131993-05-17 08:34:16 +00003235/* Functions acting on file descriptors */
3236
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003237static char posix_open__doc__[] =
3238"open(filename, flag [, mode=0777]) -> fd\n\
3239Open a file (for low level IO).";
3240
Barry Warsaw53699e91996-12-10 23:23:01 +00003241static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003242posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003243{
3244 char *file;
3245 int flag;
3246 int mode = 0777;
3247 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003248 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
3249 return NULL;
3250
Barry Warsaw53699e91996-12-10 23:23:01 +00003251 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003252 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003253 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003254 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00003255 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00003256 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003257}
3258
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003259
3260static char posix_close__doc__[] =
3261"close(fd) -> None\n\
3262Close a file descriptor (for low level IO).";
3263
Barry Warsaw53699e91996-12-10 23:23:01 +00003264static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003265posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003266{
3267 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003268 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003269 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003270 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003271 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003272 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003273 if (res < 0)
3274 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003275 Py_INCREF(Py_None);
3276 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003277}
3278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003279
3280static char posix_dup__doc__[] =
3281"dup(fd) -> fd2\n\
3282Return a duplicate of a file descriptor.";
3283
Barry Warsaw53699e91996-12-10 23:23:01 +00003284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003285posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003286{
3287 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003288 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003289 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003290 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003291 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00003292 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003293 if (fd < 0)
3294 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003295 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00003296}
3297
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003298
3299static char posix_dup2__doc__[] =
3300"dup2(fd, fd2) -> None\n\
3301Duplicate file descriptor.";
3302
Barry Warsaw53699e91996-12-10 23:23:01 +00003303static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003304posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003305{
3306 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003307 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00003308 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003309 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003310 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00003311 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003312 if (res < 0)
3313 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003314 Py_INCREF(Py_None);
3315 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00003316}
3317
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003318
3319static char posix_lseek__doc__[] =
3320"lseek(fd, pos, how) -> newpos\n\
3321Set the current position of a file descriptor.";
3322
Barry Warsaw53699e91996-12-10 23:23:01 +00003323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003324posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003325{
3326 int fd, how;
Fred Drake699f3522000-06-29 21:12:41 +00003327#ifdef MS_WIN64
3328 LONG_LONG pos, res;
3329#else
Guido van Rossum94f6f721999-01-06 18:42:14 +00003330 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00003331#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003332 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003333 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00003334 return NULL;
3335#ifdef SEEK_SET
3336 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
3337 switch (how) {
3338 case 0: how = SEEK_SET; break;
3339 case 1: how = SEEK_CUR; break;
3340 case 2: how = SEEK_END; break;
3341 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003342#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003343
3344#if !defined(HAVE_LARGEFILE_SUPPORT)
3345 pos = PyInt_AsLong(posobj);
3346#else
3347 pos = PyLong_Check(posobj) ?
3348 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
3349#endif
3350 if (PyErr_Occurred())
3351 return NULL;
3352
Barry Warsaw53699e91996-12-10 23:23:01 +00003353 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003354#ifdef MS_WIN64
3355 res = _lseeki64(fd, pos, how);
3356#else
Guido van Rossum687dd131993-05-17 08:34:16 +00003357 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00003358#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003359 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003360 if (res < 0)
3361 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00003362
3363#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00003364 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00003365#else
3366 return PyLong_FromLongLong(res);
3367#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003368}
3369
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003370
3371static char posix_read__doc__[] =
3372"read(fd, buffersize) -> string\n\
3373Read a file descriptor.";
3374
Barry Warsaw53699e91996-12-10 23:23:01 +00003375static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003376posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003377{
Guido van Rossum8bac5461996-06-11 18:38:48 +00003378 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00003379 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003380 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003381 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003382 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003383 if (buffer == NULL)
3384 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003385 Py_BEGIN_ALLOW_THREADS
3386 n = read(fd, PyString_AsString(buffer), size);
3387 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00003388 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003389 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00003390 return posix_error();
3391 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00003392 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00003393 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00003394 return buffer;
3395}
3396
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003397
3398static char posix_write__doc__[] =
3399"write(fd, string) -> byteswritten\n\
3400Write a string to a file descriptor.";
3401
Barry Warsaw53699e91996-12-10 23:23:01 +00003402static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003403posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003404{
3405 int fd, size;
3406 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003407 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00003408 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003409 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003410 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00003411 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003412 if (size < 0)
3413 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003414 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00003415}
3416
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003417
3418static char posix_fstat__doc__[]=
3419"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
3420Like stat(), but for an open file descriptor.";
3421
Barry Warsaw53699e91996-12-10 23:23:01 +00003422static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003423posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003424{
3425 int fd;
Fred Drake699f3522000-06-29 21:12:41 +00003426 STRUCT_STAT st;
Guido van Rossum687dd131993-05-17 08:34:16 +00003427 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003428 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00003429 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003430 Py_BEGIN_ALLOW_THREADS
Fred Drake699f3522000-06-29 21:12:41 +00003431 res = FSTAT(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00003432 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003433 if (res != 0)
3434 return posix_error();
Fred Drake699f3522000-06-29 21:12:41 +00003435
3436 return _pystat_fromstructstat(st);
Guido van Rossum687dd131993-05-17 08:34:16 +00003437}
3438
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003439
3440static char posix_fdopen__doc__[] =
3441"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
3442Return an open file object connected to a file descriptor.";
3443
Barry Warsaw53699e91996-12-10 23:23:01 +00003444static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003445posix_fdopen(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003446{
Guido van Rossum687dd131993-05-17 08:34:16 +00003447 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003448 char *mode = "r";
3449 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00003450 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00003451 PyObject *f;
3452 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00003453 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00003454
Barry Warsaw53699e91996-12-10 23:23:01 +00003455 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003456 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003457 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003458 if (fp == NULL)
3459 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003460 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003461 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00003462 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00003463 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00003464}
3465
Skip Montanaro1517d842000-07-19 14:34:14 +00003466static char posix_isatty__doc__[] =
3467"isatty(fd) -> Boolean\n\
3468Return true if the file descriptor 'fd' is an open file descriptor\n\
3469connected to a terminal.";
3470
3471static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00003472posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00003473{
3474 int fd;
3475 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
3476 return NULL;
3477 return Py_BuildValue("i", isatty(fd));
3478}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003479
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003480#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003481static char posix_pipe__doc__[] =
3482"pipe() -> (read_end, write_end)\n\
3483Create a pipe.";
3484
Barry Warsaw53699e91996-12-10 23:23:01 +00003485static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003486posix_pipe(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00003487{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003488#if defined(PYOS_OS2)
3489 HFILE read, write;
3490 APIRET rc;
3491
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003492 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003493 return NULL;
3494
3495 Py_BEGIN_ALLOW_THREADS
3496 rc = DosCreatePipe( &read, &write, 4096);
3497 Py_END_ALLOW_THREADS
3498 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00003499 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003500
3501 return Py_BuildValue("(ii)", read, write);
3502#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00003503#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00003504 int fds[2];
3505 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003506 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum687dd131993-05-17 08:34:16 +00003507 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003508 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003509 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00003510 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00003511 if (res != 0)
3512 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003513 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003514#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00003515 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003516 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00003517 BOOL ok;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003518 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum794d8131994-08-23 13:48:48 +00003519 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003520 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003521 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00003522 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00003523 if (!ok)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00003524 return win32_error("CreatePipe", NULL);
Fred Drake699f3522000-06-29 21:12:41 +00003525 read_fd = _open_osfhandle((intptr_t)read, 0);
3526 write_fd = _open_osfhandle((intptr_t)write, 1);
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00003527 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00003528#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003529#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00003530}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003531#endif /* HAVE_PIPE */
3532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003533
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003534#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003535static char posix_mkfifo__doc__[] =
3536"mkfifo(file, [, mode=0666]) -> None\n\
3537Create a FIFO (a POSIX named pipe).";
3538
Barry Warsaw53699e91996-12-10 23:23:01 +00003539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003540posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003541{
3542 char *file;
3543 int mode = 0666;
3544 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003545 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003546 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00003547 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003548 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00003549 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003550 if (res < 0)
3551 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00003552 Py_INCREF(Py_None);
3553 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003554}
3555#endif
3556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003557
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003558#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003559static char posix_ftruncate__doc__[] =
3560"ftruncate(fd, length) -> None\n\
3561Truncate a file to a specified length.";
3562
Barry Warsaw53699e91996-12-10 23:23:01 +00003563static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003564posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003565{
3566 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003567 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003568 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003569 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003570
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003571 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003572 return NULL;
3573
3574#if !defined(HAVE_LARGEFILE_SUPPORT)
3575 length = PyInt_AsLong(lenobj);
3576#else
3577 length = PyLong_Check(lenobj) ?
3578 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
3579#endif
3580 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003581 return NULL;
3582
Barry Warsaw53699e91996-12-10 23:23:01 +00003583 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003584 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00003585 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003586 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00003587 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003588 return NULL;
3589 }
Barry Warsaw53699e91996-12-10 23:23:01 +00003590 Py_INCREF(Py_None);
3591 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003592}
3593#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00003594
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003595#ifdef NeXT
3596#define HAVE_PUTENV
3597/* Steve Spicklemire got this putenv from NeXTAnswers */
3598static int
3599putenv(char *newval)
3600{
3601 extern char **environ;
3602
3603 static int firstTime = 1;
3604 char **ep;
3605 char *cp;
3606 int esiz;
3607 char *np;
3608
3609 if (!(np = strchr(newval, '=')))
3610 return 1;
3611 *np = '\0';
3612
3613 /* look it up */
3614 for (ep=environ ; *ep ; ep++)
3615 {
3616 /* this should always be true... */
3617 if (cp = strchr(*ep, '='))
3618 {
3619 *cp = '\0';
3620 if (!strcmp(*ep, newval))
3621 {
3622 /* got it! */
3623 *cp = '=';
3624 break;
3625 }
3626 *cp = '=';
3627 }
3628 else
3629 {
3630 *np = '=';
3631 return 1;
3632 }
3633 }
3634
3635 *np = '=';
3636 if (*ep)
3637 {
3638 /* the string was already there:
3639 just replace it with the new one */
3640 *ep = newval;
3641 return 0;
3642 }
3643
3644 /* expand environ by one */
3645 for (esiz=2, ep=environ ; *ep ; ep++)
3646 esiz++;
3647 if (firstTime)
3648 {
3649 char **epp;
3650 char **newenv;
3651 if (!(newenv = malloc(esiz * sizeof(char *))))
3652 return 1;
3653
3654 for (ep=environ, epp=newenv ; *ep ;)
3655 *epp++ = *ep++;
3656 *epp++ = newval;
3657 *epp = (char *) 0;
3658 environ = newenv;
3659 }
3660 else
3661 {
3662 if (!(environ = realloc(environ, esiz * sizeof(char *))))
3663 return 1;
3664 environ[esiz - 2] = newval;
3665 environ[esiz - 1] = (char *) 0;
3666 firstTime = 0;
3667 }
3668
3669 return 0;
3670}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00003671#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00003672
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003673
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003674#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003675static char posix_putenv__doc__[] =
3676"putenv(key, value) -> None\n\
3677Change or add an environment variable.";
3678
Fred Drake762e2061999-08-26 17:23:54 +00003679/* Save putenv() parameters as values here, so we can collect them when they
3680 * get re-set with another call for the same key. */
3681static PyObject *posix_putenv_garbage;
3682
Barry Warsaw53699e91996-12-10 23:23:01 +00003683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003684posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003685{
3686 char *s1, *s2;
3687 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00003688 PyObject *newstr;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003689
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003690 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003691 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00003692
3693#if defined(PYOS_OS2)
3694 if (stricmp(s1, "BEGINLIBPATH") == 0) {
3695 APIRET rc;
3696
3697 if (strlen(s2) == 0) /* If New Value is an Empty String */
3698 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3699
3700 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
3701 if (rc != NO_ERROR)
3702 return os2_error(rc);
3703
3704 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
3705 APIRET rc;
3706
3707 if (strlen(s2) == 0) /* If New Value is an Empty String */
3708 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
3709
3710 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
3711 if (rc != NO_ERROR)
3712 return os2_error(rc);
3713 } else {
3714#endif
3715
Fred Drake762e2061999-08-26 17:23:54 +00003716 /* XXX This can leak memory -- not easy to fix :-( */
3717 newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2);
3718 if (newstr == NULL)
3719 return PyErr_NoMemory();
3720 new = PyString_AS_STRING(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003721 (void) sprintf(new, "%s=%s", s1, s2);
3722 if (putenv(new)) {
3723 posix_error();
3724 return NULL;
3725 }
Fred Drake762e2061999-08-26 17:23:54 +00003726 /* Install the first arg and newstr in posix_putenv_garbage;
3727 * this will cause previous value to be collected. This has to
3728 * happen after the real putenv() call because the old value
3729 * was still accessible until then. */
3730 if (PyDict_SetItem(posix_putenv_garbage,
3731 PyTuple_GET_ITEM(args, 0), newstr)) {
3732 /* really not much we can do; just leak */
3733 PyErr_Clear();
3734 }
3735 else {
3736 Py_DECREF(newstr);
3737 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003738
3739#if defined(PYOS_OS2)
3740 }
3741#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003742 Py_INCREF(Py_None);
3743 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003744}
Guido van Rossumb6a47161997-09-15 22:54:34 +00003745#endif /* putenv */
3746
3747#ifdef HAVE_STRERROR
3748static char posix_strerror__doc__[] =
3749"strerror(code) -> string\n\
3750Translate an error code to a message string.";
3751
3752PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003753posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00003754{
3755 int code;
3756 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003757 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00003758 return NULL;
3759 message = strerror(code);
3760 if (message == NULL) {
3761 PyErr_SetString(PyExc_ValueError,
3762 "strerror code out of range");
3763 return NULL;
3764 }
3765 return PyString_FromString(message);
3766}
3767#endif /* strerror */
3768
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003769
Guido van Rossumc9641791998-08-04 15:26:23 +00003770#ifdef HAVE_SYS_WAIT_H
3771
3772#ifdef WIFSTOPPED
3773static char posix_WIFSTOPPED__doc__[] =
3774"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003775Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003776
3777static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003778posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003779{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003780#ifdef UNION_WAIT
3781 union wait status;
3782#define status_i (status.w_status)
3783#else
3784 int status;
3785#define status_i status
3786#endif
3787 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003788
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003789 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003790 {
3791 return NULL;
3792 }
3793
3794 return Py_BuildValue("i", WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003795#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003796}
3797#endif /* WIFSTOPPED */
3798
3799#ifdef WIFSIGNALED
3800static char posix_WIFSIGNALED__doc__[] =
3801"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00003802Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003803
3804static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003805posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003806{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003807#ifdef UNION_WAIT
3808 union wait status;
3809#define status_i (status.w_status)
3810#else
3811 int status;
3812#define status_i status
3813#endif
3814 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003815
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003816 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003817 {
3818 return NULL;
3819 }
3820
3821 return Py_BuildValue("i", WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003822#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003823}
3824#endif /* WIFSIGNALED */
3825
3826#ifdef WIFEXITED
3827static char posix_WIFEXITED__doc__[] =
3828"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003829Return true if the process returning 'status' exited using the exit()\n\
3830system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003831
3832static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003833posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003834{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003835#ifdef UNION_WAIT
3836 union wait status;
3837#define status_i (status.w_status)
3838#else
3839 int status;
3840#define status_i status
3841#endif
3842 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003843
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003844 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003845 {
3846 return NULL;
3847 }
3848
3849 return Py_BuildValue("i", WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003850#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003851}
3852#endif /* WIFEXITED */
3853
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003854#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00003855static char posix_WEXITSTATUS__doc__[] =
3856"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003857Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003858
3859static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003860posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003861{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003862#ifdef UNION_WAIT
3863 union wait status;
3864#define status_i (status.w_status)
3865#else
3866 int status;
3867#define status_i status
3868#endif
3869 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003870
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003871 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003872 {
3873 return NULL;
3874 }
3875
3876 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003877#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003878}
3879#endif /* WEXITSTATUS */
3880
3881#ifdef WTERMSIG
3882static char posix_WTERMSIG__doc__[] =
3883"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003884Return the signal that terminated the process that provided the 'status'\n\
3885value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003886
3887static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003888posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003889{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003890#ifdef UNION_WAIT
3891 union wait status;
3892#define status_i (status.w_status)
3893#else
3894 int status;
3895#define status_i status
3896#endif
3897 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003898
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003899 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003900 {
3901 return NULL;
3902 }
3903
3904 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003905#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003906}
3907#endif /* WTERMSIG */
3908
3909#ifdef WSTOPSIG
3910static char posix_WSTOPSIG__doc__[] =
3911"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003912Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003913
3914static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003915posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00003916{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003917#ifdef UNION_WAIT
3918 union wait status;
3919#define status_i (status.w_status)
3920#else
3921 int status;
3922#define status_i status
3923#endif
3924 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003925
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003926 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003927 {
3928 return NULL;
3929 }
3930
3931 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003932#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003933}
3934#endif /* WSTOPSIG */
3935
3936#endif /* HAVE_SYS_WAIT_H */
3937
3938
Guido van Rossum94f6f721999-01-06 18:42:14 +00003939#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00003940#ifdef _SCO_DS
3941/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
3942 needed definitions in sys/statvfs.h */
3943#define _SVID3
3944#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003945#include <sys/statvfs.h>
3946
3947static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003948"fstatvfs(fd) -> \n\
3949 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003950Perform an fstatvfs system call on the given fd.";
3951
3952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003953posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00003954{
3955 int fd, res;
3956 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003957 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003958 return NULL;
3959 Py_BEGIN_ALLOW_THREADS
3960 res = fstatvfs(fd, &st);
3961 Py_END_ALLOW_THREADS
3962 if (res != 0)
3963 return posix_error();
3964#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003965 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003966 (long) st.f_bsize,
3967 (long) st.f_frsize,
3968 (long) st.f_blocks,
3969 (long) st.f_bfree,
3970 (long) st.f_bavail,
3971 (long) st.f_files,
3972 (long) st.f_ffree,
3973 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003974 (long) st.f_flag,
3975 (long) st.f_namemax);
3976#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003977 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003978 (long) st.f_bsize,
3979 (long) st.f_frsize,
3980 (LONG_LONG) st.f_blocks,
3981 (LONG_LONG) st.f_bfree,
3982 (LONG_LONG) st.f_bavail,
3983 (LONG_LONG) st.f_files,
3984 (LONG_LONG) st.f_ffree,
3985 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003986 (long) st.f_flag,
3987 (long) st.f_namemax);
3988#endif
3989}
3990#endif /* HAVE_FSTATVFS */
3991
3992
3993#if defined(HAVE_STATVFS)
3994#include <sys/statvfs.h>
3995
3996static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003997"statvfs(path) -> \n\
3998 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003999Perform a statvfs system call on the given path.";
4000
4001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004002posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00004003{
4004 char *path;
4005 int res;
4006 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004007 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00004008 return NULL;
4009 Py_BEGIN_ALLOW_THREADS
4010 res = statvfs(path, &st);
4011 Py_END_ALLOW_THREADS
4012 if (res != 0)
4013 return posix_error_with_filename(path);
4014#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00004015 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00004016 (long) st.f_bsize,
4017 (long) st.f_frsize,
4018 (long) st.f_blocks,
4019 (long) st.f_bfree,
4020 (long) st.f_bavail,
4021 (long) st.f_files,
4022 (long) st.f_ffree,
4023 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00004024 (long) st.f_flag,
4025 (long) st.f_namemax);
4026#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00004027 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00004028 (long) st.f_bsize,
4029 (long) st.f_frsize,
4030 (LONG_LONG) st.f_blocks,
4031 (LONG_LONG) st.f_bfree,
4032 (LONG_LONG) st.f_bavail,
4033 (LONG_LONG) st.f_files,
4034 (LONG_LONG) st.f_ffree,
4035 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00004036 (long) st.f_flag,
4037 (long) st.f_namemax);
4038#endif
4039}
4040#endif /* HAVE_STATVFS */
4041
4042
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004043#ifdef HAVE_TEMPNAM
4044static char posix_tempnam__doc__[] = "\
4045tempnam([dir[, prefix]]) -> string\n\
4046Return a unique name for a temporary file.\n\
4047The directory and a short may be specified as strings; they may be omitted\n\
4048or None if not needed.";
4049
4050static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004051posix_tempnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004052{
4053 PyObject *result = NULL;
4054 char *dir = NULL;
4055 char *pfx = NULL;
4056 char *name;
4057
4058 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
4059 return NULL;
4060 name = tempnam(dir, pfx);
4061 if (name == NULL)
4062 return PyErr_NoMemory();
4063 result = PyString_FromString(name);
4064 free(name);
4065 return result;
4066}
Guido van Rossumd371ff11999-01-25 16:12:23 +00004067#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004068
4069
4070#ifdef HAVE_TMPFILE
4071static char posix_tmpfile__doc__[] = "\
4072tmpfile() -> file object\n\
4073Create a temporary file with no directory entries.";
4074
4075static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004076posix_tmpfile(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004077{
4078 FILE *fp;
4079
4080 if (!PyArg_ParseTuple(args, ":tmpfile"))
4081 return NULL;
4082 fp = tmpfile();
4083 if (fp == NULL)
4084 return posix_error();
4085 return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose);
4086}
4087#endif
4088
4089
4090#ifdef HAVE_TMPNAM
4091static char posix_tmpnam__doc__[] = "\
4092tmpnam() -> string\n\
4093Return a unique name for a temporary file.";
4094
4095static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004096posix_tmpnam(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004097{
4098 char buffer[L_tmpnam];
4099 char *name;
4100
4101 if (!PyArg_ParseTuple(args, ":tmpnam"))
4102 return NULL;
Greg Wardb48bc172000-03-01 21:51:56 +00004103#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004104 name = tmpnam_r(buffer);
4105#else
4106 name = tmpnam(buffer);
4107#endif
4108 if (name == NULL) {
4109 PyErr_SetObject(PyExc_OSError,
4110 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00004111#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004112 "unexpected NULL from tmpnam_r"
4113#else
4114 "unexpected NULL from tmpnam"
4115#endif
4116 ));
4117 return NULL;
4118 }
4119 return PyString_FromString(buffer);
4120}
4121#endif
4122
4123
Fred Drakec9680921999-12-13 16:37:25 +00004124/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
4125 * It maps strings representing configuration variable names to
4126 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00004127 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00004128 * rarely-used constants. There are three separate tables that use
4129 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00004130 *
4131 * This code is always included, even if none of the interfaces that
4132 * need it are included. The #if hackery needed to avoid it would be
4133 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00004134 */
4135struct constdef {
4136 char *name;
4137 long value;
4138};
4139
Fred Drake12c6e2d1999-12-14 21:25:03 +00004140static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004141conv_confname(PyObject *arg, int *valuep, struct constdef *table,
4142 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004143{
4144 if (PyInt_Check(arg)) {
4145 *valuep = PyInt_AS_LONG(arg);
4146 return 1;
4147 }
4148 if (PyString_Check(arg)) {
4149 /* look up the value in the table using a binary search */
Fred Drake699f3522000-06-29 21:12:41 +00004150 size_t lo = 0;
4151 size_t mid;
4152 size_t hi = tablesize;
4153 int cmp;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004154 char *confname = PyString_AS_STRING(arg);
4155 while (lo < hi) {
4156 mid = (lo + hi) / 2;
4157 cmp = strcmp(confname, table[mid].name);
4158 if (cmp < 0)
4159 hi = mid;
4160 else if (cmp > 0)
4161 lo = mid + 1;
4162 else {
4163 *valuep = table[mid].value;
4164 return 1;
4165 }
4166 }
4167 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
4168 }
4169 else
4170 PyErr_SetString(PyExc_TypeError,
4171 "configuration names must be strings or integers");
4172 return 0;
4173}
4174
4175
4176#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
4177static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004178#ifdef _PC_ABI_AIO_XFER_MAX
4179 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
4180#endif
4181#ifdef _PC_ABI_ASYNC_IO
4182 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
4183#endif
Fred Drakec9680921999-12-13 16:37:25 +00004184#ifdef _PC_ASYNC_IO
4185 {"PC_ASYNC_IO", _PC_ASYNC_IO},
4186#endif
4187#ifdef _PC_CHOWN_RESTRICTED
4188 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
4189#endif
4190#ifdef _PC_FILESIZEBITS
4191 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
4192#endif
4193#ifdef _PC_LAST
4194 {"PC_LAST", _PC_LAST},
4195#endif
4196#ifdef _PC_LINK_MAX
4197 {"PC_LINK_MAX", _PC_LINK_MAX},
4198#endif
4199#ifdef _PC_MAX_CANON
4200 {"PC_MAX_CANON", _PC_MAX_CANON},
4201#endif
4202#ifdef _PC_MAX_INPUT
4203 {"PC_MAX_INPUT", _PC_MAX_INPUT},
4204#endif
4205#ifdef _PC_NAME_MAX
4206 {"PC_NAME_MAX", _PC_NAME_MAX},
4207#endif
4208#ifdef _PC_NO_TRUNC
4209 {"PC_NO_TRUNC", _PC_NO_TRUNC},
4210#endif
4211#ifdef _PC_PATH_MAX
4212 {"PC_PATH_MAX", _PC_PATH_MAX},
4213#endif
4214#ifdef _PC_PIPE_BUF
4215 {"PC_PIPE_BUF", _PC_PIPE_BUF},
4216#endif
4217#ifdef _PC_PRIO_IO
4218 {"PC_PRIO_IO", _PC_PRIO_IO},
4219#endif
4220#ifdef _PC_SOCK_MAXBUF
4221 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
4222#endif
4223#ifdef _PC_SYNC_IO
4224 {"PC_SYNC_IO", _PC_SYNC_IO},
4225#endif
4226#ifdef _PC_VDISABLE
4227 {"PC_VDISABLE", _PC_VDISABLE},
4228#endif
4229};
4230
Fred Drakec9680921999-12-13 16:37:25 +00004231static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004232conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004233{
4234 return conv_confname(arg, valuep, posix_constants_pathconf,
4235 sizeof(posix_constants_pathconf)
4236 / sizeof(struct constdef));
4237}
4238#endif
4239
4240#ifdef HAVE_FPATHCONF
4241static char posix_fpathconf__doc__[] = "\
4242fpathconf(fd, name) -> integer\n\
4243Return the configuration limit name for the file descriptor fd.\n\
4244If there is no limit, return -1.";
4245
4246static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004247posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004248{
4249 PyObject *result = NULL;
4250 int name, fd;
4251
Fred Drake12c6e2d1999-12-14 21:25:03 +00004252 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
4253 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00004254 long limit;
4255
4256 errno = 0;
4257 limit = fpathconf(fd, name);
4258 if (limit == -1 && errno != 0)
4259 posix_error();
4260 else
4261 result = PyInt_FromLong(limit);
4262 }
4263 return result;
4264}
4265#endif
4266
4267
4268#ifdef HAVE_PATHCONF
4269static char posix_pathconf__doc__[] = "\
4270pathconf(path, name) -> integer\n\
4271Return the configuration limit name for the file or directory path.\n\
4272If there is no limit, return -1.";
4273
4274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004275posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004276{
4277 PyObject *result = NULL;
4278 int name;
4279 char *path;
4280
4281 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
4282 conv_path_confname, &name)) {
4283 long limit;
4284
4285 errno = 0;
4286 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004287 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00004288 if (errno == EINVAL)
4289 /* could be a path or name problem */
4290 posix_error();
4291 else
4292 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00004293 }
Fred Drakec9680921999-12-13 16:37:25 +00004294 else
4295 result = PyInt_FromLong(limit);
4296 }
4297 return result;
4298}
4299#endif
4300
4301#ifdef HAVE_CONFSTR
4302static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00004303#ifdef _CS_ARCHITECTURE
4304 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
4305#endif
4306#ifdef _CS_HOSTNAME
4307 {"CS_HOSTNAME", _CS_HOSTNAME},
4308#endif
4309#ifdef _CS_HW_PROVIDER
4310 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
4311#endif
4312#ifdef _CS_HW_SERIAL
4313 {"CS_HW_SERIAL", _CS_HW_SERIAL},
4314#endif
4315#ifdef _CS_INITTAB_NAME
4316 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
4317#endif
Fred Drakec9680921999-12-13 16:37:25 +00004318#ifdef _CS_LFS64_CFLAGS
4319 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
4320#endif
4321#ifdef _CS_LFS64_LDFLAGS
4322 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
4323#endif
4324#ifdef _CS_LFS64_LIBS
4325 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
4326#endif
4327#ifdef _CS_LFS64_LINTFLAGS
4328 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
4329#endif
4330#ifdef _CS_LFS_CFLAGS
4331 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
4332#endif
4333#ifdef _CS_LFS_LDFLAGS
4334 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
4335#endif
4336#ifdef _CS_LFS_LIBS
4337 {"CS_LFS_LIBS", _CS_LFS_LIBS},
4338#endif
4339#ifdef _CS_LFS_LINTFLAGS
4340 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
4341#endif
Fred Draked86ed291999-12-15 15:34:33 +00004342#ifdef _CS_MACHINE
4343 {"CS_MACHINE", _CS_MACHINE},
4344#endif
Fred Drakec9680921999-12-13 16:37:25 +00004345#ifdef _CS_PATH
4346 {"CS_PATH", _CS_PATH},
4347#endif
Fred Draked86ed291999-12-15 15:34:33 +00004348#ifdef _CS_RELEASE
4349 {"CS_RELEASE", _CS_RELEASE},
4350#endif
4351#ifdef _CS_SRPC_DOMAIN
4352 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
4353#endif
4354#ifdef _CS_SYSNAME
4355 {"CS_SYSNAME", _CS_SYSNAME},
4356#endif
4357#ifdef _CS_VERSION
4358 {"CS_VERSION", _CS_VERSION},
4359#endif
Fred Drakec9680921999-12-13 16:37:25 +00004360#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
4361 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
4362#endif
4363#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
4364 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
4365#endif
4366#ifdef _CS_XBS5_ILP32_OFF32_LIBS
4367 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
4368#endif
4369#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
4370 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
4371#endif
4372#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
4373 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
4374#endif
4375#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
4376 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
4377#endif
4378#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
4379 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
4380#endif
4381#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
4382 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
4383#endif
4384#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
4385 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
4386#endif
4387#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
4388 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
4389#endif
4390#ifdef _CS_XBS5_LP64_OFF64_LIBS
4391 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
4392#endif
4393#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
4394 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
4395#endif
4396#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
4397 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
4398#endif
4399#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
4400 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
4401#endif
4402#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
4403 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
4404#endif
4405#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
4406 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
4407#endif
Fred Draked86ed291999-12-15 15:34:33 +00004408#ifdef _MIPS_CS_AVAIL_PROCESSORS
4409 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
4410#endif
4411#ifdef _MIPS_CS_BASE
4412 {"MIPS_CS_BASE", _MIPS_CS_BASE},
4413#endif
4414#ifdef _MIPS_CS_HOSTID
4415 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
4416#endif
4417#ifdef _MIPS_CS_HW_NAME
4418 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
4419#endif
4420#ifdef _MIPS_CS_NUM_PROCESSORS
4421 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
4422#endif
4423#ifdef _MIPS_CS_OSREL_MAJ
4424 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
4425#endif
4426#ifdef _MIPS_CS_OSREL_MIN
4427 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
4428#endif
4429#ifdef _MIPS_CS_OSREL_PATCH
4430 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
4431#endif
4432#ifdef _MIPS_CS_OS_NAME
4433 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
4434#endif
4435#ifdef _MIPS_CS_OS_PROVIDER
4436 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
4437#endif
4438#ifdef _MIPS_CS_PROCESSORS
4439 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
4440#endif
4441#ifdef _MIPS_CS_SERIAL
4442 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
4443#endif
4444#ifdef _MIPS_CS_VENDOR
4445 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
4446#endif
Fred Drakec9680921999-12-13 16:37:25 +00004447};
4448
4449static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004450conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004451{
4452 return conv_confname(arg, valuep, posix_constants_confstr,
4453 sizeof(posix_constants_confstr)
4454 / sizeof(struct constdef));
4455}
4456
4457static char posix_confstr__doc__[] = "\
4458confstr(name) -> string\n\
4459Return a string-valued system configuration variable.";
4460
4461static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004462posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00004463{
4464 PyObject *result = NULL;
4465 int name;
4466 char buffer[64];
4467
4468 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
4469 int len = confstr(name, buffer, sizeof(buffer));
4470
Fred Drakec9680921999-12-13 16:37:25 +00004471 errno = 0;
4472 if (len == 0) {
4473 if (errno != 0)
4474 posix_error();
4475 else
4476 result = PyString_FromString("");
4477 }
4478 else {
4479 if (len >= sizeof(buffer)) {
4480 result = PyString_FromStringAndSize(NULL, len);
4481 if (result != NULL)
4482 confstr(name, PyString_AS_STRING(result), len+1);
4483 }
4484 else
4485 result = PyString_FromString(buffer);
4486 }
4487 }
4488 return result;
4489}
4490#endif
4491
4492
4493#ifdef HAVE_SYSCONF
4494static struct constdef posix_constants_sysconf[] = {
4495#ifdef _SC_2_CHAR_TERM
4496 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
4497#endif
4498#ifdef _SC_2_C_BIND
4499 {"SC_2_C_BIND", _SC_2_C_BIND},
4500#endif
4501#ifdef _SC_2_C_DEV
4502 {"SC_2_C_DEV", _SC_2_C_DEV},
4503#endif
4504#ifdef _SC_2_C_VERSION
4505 {"SC_2_C_VERSION", _SC_2_C_VERSION},
4506#endif
4507#ifdef _SC_2_FORT_DEV
4508 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
4509#endif
4510#ifdef _SC_2_FORT_RUN
4511 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
4512#endif
4513#ifdef _SC_2_LOCALEDEF
4514 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
4515#endif
4516#ifdef _SC_2_SW_DEV
4517 {"SC_2_SW_DEV", _SC_2_SW_DEV},
4518#endif
4519#ifdef _SC_2_UPE
4520 {"SC_2_UPE", _SC_2_UPE},
4521#endif
4522#ifdef _SC_2_VERSION
4523 {"SC_2_VERSION", _SC_2_VERSION},
4524#endif
Fred Draked86ed291999-12-15 15:34:33 +00004525#ifdef _SC_ABI_ASYNCHRONOUS_IO
4526 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
4527#endif
4528#ifdef _SC_ACL
4529 {"SC_ACL", _SC_ACL},
4530#endif
Fred Drakec9680921999-12-13 16:37:25 +00004531#ifdef _SC_AIO_LISTIO_MAX
4532 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
4533#endif
Fred Drakec9680921999-12-13 16:37:25 +00004534#ifdef _SC_AIO_MAX
4535 {"SC_AIO_MAX", _SC_AIO_MAX},
4536#endif
4537#ifdef _SC_AIO_PRIO_DELTA_MAX
4538 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
4539#endif
4540#ifdef _SC_ARG_MAX
4541 {"SC_ARG_MAX", _SC_ARG_MAX},
4542#endif
4543#ifdef _SC_ASYNCHRONOUS_IO
4544 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
4545#endif
4546#ifdef _SC_ATEXIT_MAX
4547 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
4548#endif
Fred Draked86ed291999-12-15 15:34:33 +00004549#ifdef _SC_AUDIT
4550 {"SC_AUDIT", _SC_AUDIT},
4551#endif
Fred Drakec9680921999-12-13 16:37:25 +00004552#ifdef _SC_AVPHYS_PAGES
4553 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
4554#endif
4555#ifdef _SC_BC_BASE_MAX
4556 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
4557#endif
4558#ifdef _SC_BC_DIM_MAX
4559 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
4560#endif
4561#ifdef _SC_BC_SCALE_MAX
4562 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
4563#endif
4564#ifdef _SC_BC_STRING_MAX
4565 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
4566#endif
Fred Draked86ed291999-12-15 15:34:33 +00004567#ifdef _SC_CAP
4568 {"SC_CAP", _SC_CAP},
4569#endif
Fred Drakec9680921999-12-13 16:37:25 +00004570#ifdef _SC_CHARCLASS_NAME_MAX
4571 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
4572#endif
4573#ifdef _SC_CHAR_BIT
4574 {"SC_CHAR_BIT", _SC_CHAR_BIT},
4575#endif
4576#ifdef _SC_CHAR_MAX
4577 {"SC_CHAR_MAX", _SC_CHAR_MAX},
4578#endif
4579#ifdef _SC_CHAR_MIN
4580 {"SC_CHAR_MIN", _SC_CHAR_MIN},
4581#endif
4582#ifdef _SC_CHILD_MAX
4583 {"SC_CHILD_MAX", _SC_CHILD_MAX},
4584#endif
4585#ifdef _SC_CLK_TCK
4586 {"SC_CLK_TCK", _SC_CLK_TCK},
4587#endif
4588#ifdef _SC_COHER_BLKSZ
4589 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
4590#endif
4591#ifdef _SC_COLL_WEIGHTS_MAX
4592 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
4593#endif
4594#ifdef _SC_DCACHE_ASSOC
4595 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
4596#endif
4597#ifdef _SC_DCACHE_BLKSZ
4598 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
4599#endif
4600#ifdef _SC_DCACHE_LINESZ
4601 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
4602#endif
4603#ifdef _SC_DCACHE_SZ
4604 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
4605#endif
4606#ifdef _SC_DCACHE_TBLKSZ
4607 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
4608#endif
4609#ifdef _SC_DELAYTIMER_MAX
4610 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
4611#endif
4612#ifdef _SC_EQUIV_CLASS_MAX
4613 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
4614#endif
4615#ifdef _SC_EXPR_NEST_MAX
4616 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
4617#endif
4618#ifdef _SC_FSYNC
4619 {"SC_FSYNC", _SC_FSYNC},
4620#endif
4621#ifdef _SC_GETGR_R_SIZE_MAX
4622 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
4623#endif
4624#ifdef _SC_GETPW_R_SIZE_MAX
4625 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
4626#endif
4627#ifdef _SC_ICACHE_ASSOC
4628 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
4629#endif
4630#ifdef _SC_ICACHE_BLKSZ
4631 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
4632#endif
4633#ifdef _SC_ICACHE_LINESZ
4634 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
4635#endif
4636#ifdef _SC_ICACHE_SZ
4637 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
4638#endif
Fred Draked86ed291999-12-15 15:34:33 +00004639#ifdef _SC_INF
4640 {"SC_INF", _SC_INF},
4641#endif
Fred Drakec9680921999-12-13 16:37:25 +00004642#ifdef _SC_INT_MAX
4643 {"SC_INT_MAX", _SC_INT_MAX},
4644#endif
4645#ifdef _SC_INT_MIN
4646 {"SC_INT_MIN", _SC_INT_MIN},
4647#endif
4648#ifdef _SC_IOV_MAX
4649 {"SC_IOV_MAX", _SC_IOV_MAX},
4650#endif
Fred Draked86ed291999-12-15 15:34:33 +00004651#ifdef _SC_IP_SECOPTS
4652 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
4653#endif
Fred Drakec9680921999-12-13 16:37:25 +00004654#ifdef _SC_JOB_CONTROL
4655 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
4656#endif
Fred Draked86ed291999-12-15 15:34:33 +00004657#ifdef _SC_KERN_POINTERS
4658 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
4659#endif
4660#ifdef _SC_KERN_SIM
4661 {"SC_KERN_SIM", _SC_KERN_SIM},
4662#endif
Fred Drakec9680921999-12-13 16:37:25 +00004663#ifdef _SC_LINE_MAX
4664 {"SC_LINE_MAX", _SC_LINE_MAX},
4665#endif
4666#ifdef _SC_LOGIN_NAME_MAX
4667 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
4668#endif
4669#ifdef _SC_LOGNAME_MAX
4670 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
4671#endif
4672#ifdef _SC_LONG_BIT
4673 {"SC_LONG_BIT", _SC_LONG_BIT},
4674#endif
Fred Draked86ed291999-12-15 15:34:33 +00004675#ifdef _SC_MAC
4676 {"SC_MAC", _SC_MAC},
4677#endif
Fred Drakec9680921999-12-13 16:37:25 +00004678#ifdef _SC_MAPPED_FILES
4679 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
4680#endif
4681#ifdef _SC_MAXPID
4682 {"SC_MAXPID", _SC_MAXPID},
4683#endif
4684#ifdef _SC_MB_LEN_MAX
4685 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
4686#endif
4687#ifdef _SC_MEMLOCK
4688 {"SC_MEMLOCK", _SC_MEMLOCK},
4689#endif
4690#ifdef _SC_MEMLOCK_RANGE
4691 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
4692#endif
4693#ifdef _SC_MEMORY_PROTECTION
4694 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
4695#endif
4696#ifdef _SC_MESSAGE_PASSING
4697 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
4698#endif
Fred Draked86ed291999-12-15 15:34:33 +00004699#ifdef _SC_MMAP_FIXED_ALIGNMENT
4700 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
4701#endif
Fred Drakec9680921999-12-13 16:37:25 +00004702#ifdef _SC_MQ_OPEN_MAX
4703 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
4704#endif
4705#ifdef _SC_MQ_PRIO_MAX
4706 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
4707#endif
Fred Draked86ed291999-12-15 15:34:33 +00004708#ifdef _SC_NACLS_MAX
4709 {"SC_NACLS_MAX", _SC_NACLS_MAX},
4710#endif
Fred Drakec9680921999-12-13 16:37:25 +00004711#ifdef _SC_NGROUPS_MAX
4712 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
4713#endif
4714#ifdef _SC_NL_ARGMAX
4715 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
4716#endif
4717#ifdef _SC_NL_LANGMAX
4718 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
4719#endif
4720#ifdef _SC_NL_MSGMAX
4721 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
4722#endif
4723#ifdef _SC_NL_NMAX
4724 {"SC_NL_NMAX", _SC_NL_NMAX},
4725#endif
4726#ifdef _SC_NL_SETMAX
4727 {"SC_NL_SETMAX", _SC_NL_SETMAX},
4728#endif
4729#ifdef _SC_NL_TEXTMAX
4730 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
4731#endif
4732#ifdef _SC_NPROCESSORS_CONF
4733 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
4734#endif
4735#ifdef _SC_NPROCESSORS_ONLN
4736 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
4737#endif
Fred Draked86ed291999-12-15 15:34:33 +00004738#ifdef _SC_NPROC_CONF
4739 {"SC_NPROC_CONF", _SC_NPROC_CONF},
4740#endif
4741#ifdef _SC_NPROC_ONLN
4742 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
4743#endif
Fred Drakec9680921999-12-13 16:37:25 +00004744#ifdef _SC_NZERO
4745 {"SC_NZERO", _SC_NZERO},
4746#endif
4747#ifdef _SC_OPEN_MAX
4748 {"SC_OPEN_MAX", _SC_OPEN_MAX},
4749#endif
4750#ifdef _SC_PAGESIZE
4751 {"SC_PAGESIZE", _SC_PAGESIZE},
4752#endif
4753#ifdef _SC_PAGE_SIZE
4754 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
4755#endif
4756#ifdef _SC_PASS_MAX
4757 {"SC_PASS_MAX", _SC_PASS_MAX},
4758#endif
4759#ifdef _SC_PHYS_PAGES
4760 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
4761#endif
4762#ifdef _SC_PII
4763 {"SC_PII", _SC_PII},
4764#endif
4765#ifdef _SC_PII_INTERNET
4766 {"SC_PII_INTERNET", _SC_PII_INTERNET},
4767#endif
4768#ifdef _SC_PII_INTERNET_DGRAM
4769 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
4770#endif
4771#ifdef _SC_PII_INTERNET_STREAM
4772 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
4773#endif
4774#ifdef _SC_PII_OSI
4775 {"SC_PII_OSI", _SC_PII_OSI},
4776#endif
4777#ifdef _SC_PII_OSI_CLTS
4778 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
4779#endif
4780#ifdef _SC_PII_OSI_COTS
4781 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
4782#endif
4783#ifdef _SC_PII_OSI_M
4784 {"SC_PII_OSI_M", _SC_PII_OSI_M},
4785#endif
4786#ifdef _SC_PII_SOCKET
4787 {"SC_PII_SOCKET", _SC_PII_SOCKET},
4788#endif
4789#ifdef _SC_PII_XTI
4790 {"SC_PII_XTI", _SC_PII_XTI},
4791#endif
4792#ifdef _SC_POLL
4793 {"SC_POLL", _SC_POLL},
4794#endif
4795#ifdef _SC_PRIORITIZED_IO
4796 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
4797#endif
4798#ifdef _SC_PRIORITY_SCHEDULING
4799 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
4800#endif
4801#ifdef _SC_REALTIME_SIGNALS
4802 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
4803#endif
4804#ifdef _SC_RE_DUP_MAX
4805 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
4806#endif
4807#ifdef _SC_RTSIG_MAX
4808 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
4809#endif
4810#ifdef _SC_SAVED_IDS
4811 {"SC_SAVED_IDS", _SC_SAVED_IDS},
4812#endif
4813#ifdef _SC_SCHAR_MAX
4814 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
4815#endif
4816#ifdef _SC_SCHAR_MIN
4817 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
4818#endif
4819#ifdef _SC_SELECT
4820 {"SC_SELECT", _SC_SELECT},
4821#endif
4822#ifdef _SC_SEMAPHORES
4823 {"SC_SEMAPHORES", _SC_SEMAPHORES},
4824#endif
4825#ifdef _SC_SEM_NSEMS_MAX
4826 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
4827#endif
4828#ifdef _SC_SEM_VALUE_MAX
4829 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
4830#endif
4831#ifdef _SC_SHARED_MEMORY_OBJECTS
4832 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
4833#endif
4834#ifdef _SC_SHRT_MAX
4835 {"SC_SHRT_MAX", _SC_SHRT_MAX},
4836#endif
4837#ifdef _SC_SHRT_MIN
4838 {"SC_SHRT_MIN", _SC_SHRT_MIN},
4839#endif
4840#ifdef _SC_SIGQUEUE_MAX
4841 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
4842#endif
4843#ifdef _SC_SIGRT_MAX
4844 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
4845#endif
4846#ifdef _SC_SIGRT_MIN
4847 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
4848#endif
Fred Draked86ed291999-12-15 15:34:33 +00004849#ifdef _SC_SOFTPOWER
4850 {"SC_SOFTPOWER", _SC_SOFTPOWER},
4851#endif
Fred Drakec9680921999-12-13 16:37:25 +00004852#ifdef _SC_SPLIT_CACHE
4853 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
4854#endif
4855#ifdef _SC_SSIZE_MAX
4856 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
4857#endif
4858#ifdef _SC_STACK_PROT
4859 {"SC_STACK_PROT", _SC_STACK_PROT},
4860#endif
4861#ifdef _SC_STREAM_MAX
4862 {"SC_STREAM_MAX", _SC_STREAM_MAX},
4863#endif
4864#ifdef _SC_SYNCHRONIZED_IO
4865 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
4866#endif
4867#ifdef _SC_THREADS
4868 {"SC_THREADS", _SC_THREADS},
4869#endif
4870#ifdef _SC_THREAD_ATTR_STACKADDR
4871 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
4872#endif
4873#ifdef _SC_THREAD_ATTR_STACKSIZE
4874 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
4875#endif
4876#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
4877 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
4878#endif
4879#ifdef _SC_THREAD_KEYS_MAX
4880 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
4881#endif
4882#ifdef _SC_THREAD_PRIORITY_SCHEDULING
4883 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
4884#endif
4885#ifdef _SC_THREAD_PRIO_INHERIT
4886 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
4887#endif
4888#ifdef _SC_THREAD_PRIO_PROTECT
4889 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
4890#endif
4891#ifdef _SC_THREAD_PROCESS_SHARED
4892 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
4893#endif
4894#ifdef _SC_THREAD_SAFE_FUNCTIONS
4895 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
4896#endif
4897#ifdef _SC_THREAD_STACK_MIN
4898 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
4899#endif
4900#ifdef _SC_THREAD_THREADS_MAX
4901 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
4902#endif
4903#ifdef _SC_TIMERS
4904 {"SC_TIMERS", _SC_TIMERS},
4905#endif
4906#ifdef _SC_TIMER_MAX
4907 {"SC_TIMER_MAX", _SC_TIMER_MAX},
4908#endif
4909#ifdef _SC_TTY_NAME_MAX
4910 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
4911#endif
4912#ifdef _SC_TZNAME_MAX
4913 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
4914#endif
4915#ifdef _SC_T_IOV_MAX
4916 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
4917#endif
4918#ifdef _SC_UCHAR_MAX
4919 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
4920#endif
4921#ifdef _SC_UINT_MAX
4922 {"SC_UINT_MAX", _SC_UINT_MAX},
4923#endif
4924#ifdef _SC_UIO_MAXIOV
4925 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
4926#endif
4927#ifdef _SC_ULONG_MAX
4928 {"SC_ULONG_MAX", _SC_ULONG_MAX},
4929#endif
4930#ifdef _SC_USHRT_MAX
4931 {"SC_USHRT_MAX", _SC_USHRT_MAX},
4932#endif
4933#ifdef _SC_VERSION
4934 {"SC_VERSION", _SC_VERSION},
4935#endif
4936#ifdef _SC_WORD_BIT
4937 {"SC_WORD_BIT", _SC_WORD_BIT},
4938#endif
4939#ifdef _SC_XBS5_ILP32_OFF32
4940 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
4941#endif
4942#ifdef _SC_XBS5_ILP32_OFFBIG
4943 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
4944#endif
4945#ifdef _SC_XBS5_LP64_OFF64
4946 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
4947#endif
4948#ifdef _SC_XBS5_LPBIG_OFFBIG
4949 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
4950#endif
4951#ifdef _SC_XOPEN_CRYPT
4952 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
4953#endif
4954#ifdef _SC_XOPEN_ENH_I18N
4955 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
4956#endif
4957#ifdef _SC_XOPEN_LEGACY
4958 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
4959#endif
4960#ifdef _SC_XOPEN_REALTIME
4961 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
4962#endif
4963#ifdef _SC_XOPEN_REALTIME_THREADS
4964 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
4965#endif
4966#ifdef _SC_XOPEN_SHM
4967 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
4968#endif
4969#ifdef _SC_XOPEN_UNIX
4970 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
4971#endif
4972#ifdef _SC_XOPEN_VERSION
4973 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
4974#endif
4975#ifdef _SC_XOPEN_XCU_VERSION
4976 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
4977#endif
4978#ifdef _SC_XOPEN_XPG2
4979 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
4980#endif
4981#ifdef _SC_XOPEN_XPG3
4982 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
4983#endif
4984#ifdef _SC_XOPEN_XPG4
4985 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
4986#endif
4987};
4988
4989static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004990conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00004991{
4992 return conv_confname(arg, valuep, posix_constants_sysconf,
4993 sizeof(posix_constants_sysconf)
4994 / sizeof(struct constdef));
4995}
4996
4997static char posix_sysconf__doc__[] = "\
4998sysconf(name) -> integer\n\
4999Return an integer-valued system configuration variable.";
5000
5001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005002posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005003{
5004 PyObject *result = NULL;
5005 int name;
5006
5007 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
5008 int value;
5009
5010 errno = 0;
5011 value = sysconf(name);
5012 if (value == -1 && errno != 0)
5013 posix_error();
5014 else
5015 result = PyInt_FromLong(value);
5016 }
5017 return result;
5018}
5019#endif
5020
5021
Fred Drakebec628d1999-12-15 18:31:10 +00005022/* This code is used to ensure that the tables of configuration value names
5023 * are in sorted order as required by conv_confname(), and also to build the
5024 * the exported dictionaries that are used to publish information about the
5025 * names available on the host platform.
5026 *
5027 * Sorting the table at runtime ensures that the table is properly ordered
5028 * when used, even for platforms we're not able to test on. It also makes
5029 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00005030 */
Fred Drakebec628d1999-12-15 18:31:10 +00005031
5032static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005033cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00005034{
5035 const struct constdef *c1 =
5036 (const struct constdef *) v1;
5037 const struct constdef *c2 =
5038 (const struct constdef *) v2;
5039
5040 return strcmp(c1->name, c2->name);
5041}
5042
5043static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005044setup_confname_table(struct constdef *table, size_t tablesize,
5045 char *tablename, PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00005046{
Fred Drakebec628d1999-12-15 18:31:10 +00005047 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00005048 size_t i;
5049 int status;
Fred Drakebec628d1999-12-15 18:31:10 +00005050
5051 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
5052 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00005053 if (d == NULL)
5054 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005055
Barry Warsaw3155db32000-04-13 15:20:40 +00005056 for (i=0; i < tablesize; ++i) {
5057 PyObject *o = PyInt_FromLong(table[i].value);
5058 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
5059 Py_XDECREF(o);
5060 Py_DECREF(d);
5061 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005062 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005063 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00005064 }
Barry Warsaw3155db32000-04-13 15:20:40 +00005065 status = PyDict_SetItemString(moddict, tablename, d);
5066 Py_DECREF(d);
5067 return status;
Fred Draked86ed291999-12-15 15:34:33 +00005068}
5069
Fred Drakebec628d1999-12-15 18:31:10 +00005070/* Return -1 on failure, 0 on success. */
5071static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005072setup_confname_tables(PyObject *moddict)
Fred Draked86ed291999-12-15 15:34:33 +00005073{
5074#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00005075 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00005076 sizeof(posix_constants_pathconf)
5077 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005078 "pathconf_names", moddict))
5079 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005080#endif
5081#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00005082 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00005083 sizeof(posix_constants_confstr)
5084 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005085 "confstr_names", moddict))
5086 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005087#endif
5088#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00005089 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00005090 sizeof(posix_constants_sysconf)
5091 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00005092 "sysconf_names", moddict))
5093 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00005094#endif
Fred Drakebec628d1999-12-15 18:31:10 +00005095 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00005096}
Fred Draked86ed291999-12-15 15:34:33 +00005097
5098
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005099static char posix_abort__doc__[] = "\
5100abort() -> does not return!\n\
5101Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
5102in the hardest way possible on the hosting operating system.";
5103
5104static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005105posix_abort(PyObject *self, PyObject *args)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005106{
5107 if (!PyArg_ParseTuple(args, ":abort"))
5108 return NULL;
5109 abort();
5110 /*NOTREACHED*/
5111 Py_FatalError("abort() called from Python code didn't abort!");
5112 return NULL;
5113}
Fred Drakebec628d1999-12-15 18:31:10 +00005114
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005115
5116static PyMethodDef posix_methods[] = {
5117 {"access", posix_access, METH_VARARGS, posix_access__doc__},
5118#ifdef HAVE_TTYNAME
5119 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
5120#endif
5121 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
5122 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005123#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005124 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005125#endif /* HAVE_CHOWN */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005126#ifdef HAVE_CTERMID
5127 {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__},
5128#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00005129#ifdef HAVE_GETCWD
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005130 {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00005131#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005132#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005133 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005134#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005135 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
5136 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
5137 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005138#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005139 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005140#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005141#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005142 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005143#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005144 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
5145 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
5146 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005147#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005148 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005149#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005150#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005151 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005152#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005153 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005154#ifdef HAVE_UNAME
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005155 {"uname", posix_uname, METH_VARARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005156#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005157 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
5158 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
5159 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005160#ifdef HAVE_TIMES
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005161 {"times", posix_times, METH_VARARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005162#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005163 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005164#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005165 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
5166 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005167#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00005168#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005169 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
5170 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00005171#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005172#ifdef HAVE_FORK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005173 {"fork", posix_fork, METH_VARARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005174#endif /* HAVE_FORK */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005175#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005176 {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__},
Thomas Wouters70c21a12000-07-14 14:28:33 +00005177#endif /* HAVE_OPENPTY || HAVE__GETPTY */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005178#ifdef HAVE_FORKPTY
5179 {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
5180#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005181#ifdef HAVE_GETEGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005182 {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005183#endif /* HAVE_GETEGID */
5184#ifdef HAVE_GETEUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005185 {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005186#endif /* HAVE_GETEUID */
5187#ifdef HAVE_GETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005188 {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005189#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00005190#ifdef HAVE_GETGROUPS
5191 {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__},
5192#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005193 {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00005194#ifdef HAVE_GETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005195 {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005196#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005197#ifdef HAVE_GETPPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005198 {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005199#endif /* HAVE_GETPPID */
5200#ifdef HAVE_GETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005201 {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005202#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005203#ifdef HAVE_GETLOGIN
5204 {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__},
5205#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00005206#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005207 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005208#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00005209#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005210 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00005211#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005212#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005213 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Fredrik Lundhffb9c772000-07-09 14:49:51 +00005214#ifdef MS_WIN32
5215 {"popen2", win32_popen2, METH_VARARGS},
5216 {"popen3", win32_popen3, METH_VARARGS},
5217 {"popen4", win32_popen4, METH_VARARGS},
5218#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005219#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005220#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005221 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005222#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005223#ifdef HAVE_SETEUID
5224 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
5225#endif /* HAVE_SETEUID */
5226#ifdef HAVE_SETEGID
5227 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
5228#endif /* HAVE_SETEGID */
5229#ifdef HAVE_SETREUID
5230 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
5231#endif /* HAVE_SETREUID */
5232#ifdef HAVE_SETREGID
5233 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
5234#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005235#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005236 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005237#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005238#ifdef HAVE_SETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005239 {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005240#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00005241#ifdef HAVE_WAIT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005242 {"wait", posix_wait, METH_VARARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00005243#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005244#ifdef HAVE_WAITPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005245 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005246#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005247#ifdef HAVE_SETSID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005248 {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005249#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005250#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005251 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005252#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005253#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005254 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005255#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005256#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005257 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005258#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005259 {"open", posix_open, METH_VARARGS, posix_open__doc__},
5260 {"close", posix_close, METH_VARARGS, posix_close__doc__},
5261 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
5262 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
5263 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
5264 {"read", posix_read, METH_VARARGS, posix_read__doc__},
5265 {"write", posix_write, METH_VARARGS, posix_write__doc__},
5266 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
5267 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Skip Montanaro1517d842000-07-19 14:34:14 +00005268 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005269#ifdef HAVE_PIPE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005270 {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005271#endif
5272#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005273 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005274#endif
5275#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005276 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005277#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005278#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005279 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005280#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00005281#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005282 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00005283#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00005284#ifdef HAVE_FSYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005285 {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005286#endif
5287#ifdef HAVE_FDATASYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005288 {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00005289#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00005290#ifdef HAVE_SYS_WAIT_H
5291#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005292 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005293#endif /* WIFSTOPPED */
5294#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005295 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005296#endif /* WIFSIGNALED */
5297#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005298 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005299#endif /* WIFEXITED */
5300#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005301 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005302#endif /* WEXITSTATUS */
5303#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005304 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005305#endif /* WTERMSIG */
5306#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005307 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00005308#endif /* WSTOPSIG */
5309#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005310#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005311 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005312#endif
5313#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005314 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00005315#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005316#ifdef HAVE_TMPNAM
5317 {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__},
5318#endif
5319#ifdef HAVE_TEMPNAM
5320 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
5321#endif
5322#ifdef HAVE_TMPNAM
5323 {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__},
5324#endif
Fred Drakec9680921999-12-13 16:37:25 +00005325#ifdef HAVE_CONFSTR
5326 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
5327#endif
5328#ifdef HAVE_SYSCONF
5329 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
5330#endif
5331#ifdef HAVE_FPATHCONF
5332 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
5333#endif
5334#ifdef HAVE_PATHCONF
5335 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
5336#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005337 {"abort", posix_abort, METH_VARARGS, posix_abort__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005338 {NULL, NULL} /* Sentinel */
5339};
5340
5341
Barry Warsaw4a342091996-12-19 23:50:02 +00005342static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005343ins(PyObject *d, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00005344{
5345 PyObject* v = PyInt_FromLong(value);
5346 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
5347 return -1; /* triggers fatal error */
5348
5349 Py_DECREF(v);
5350 return 0;
5351}
5352
Guido van Rossumd48f2521997-12-05 22:19:34 +00005353#if defined(PYOS_OS2)
5354/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
5355static int insertvalues(PyObject *d)
5356{
5357 APIRET rc;
5358 ULONG values[QSV_MAX+1];
5359 PyObject *v;
5360 char *ver, tmp[10];
5361
5362 Py_BEGIN_ALLOW_THREADS
5363 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
5364 Py_END_ALLOW_THREADS
5365
5366 if (rc != NO_ERROR) {
5367 os2_error(rc);
5368 return -1;
5369 }
5370
5371 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
5372 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
5373 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
5374 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
5375 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
5376 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
5377 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
5378
5379 switch (values[QSV_VERSION_MINOR]) {
5380 case 0: ver = "2.00"; break;
5381 case 10: ver = "2.10"; break;
5382 case 11: ver = "2.11"; break;
5383 case 30: ver = "3.00"; break;
5384 case 40: ver = "4.00"; break;
5385 case 50: ver = "5.00"; break;
5386 default:
5387 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
5388 values[QSV_VERSION_MINOR]);
5389 ver = &tmp[0];
5390 }
5391
5392 /* Add Indicator of the Version of the Operating System */
5393 v = PyString_FromString(ver);
5394 if (!v || PyDict_SetItemString(d, "version", v) < 0)
5395 return -1;
5396 Py_DECREF(v);
5397
5398 /* Add Indicator of Which Drive was Used to Boot the System */
5399 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
5400 tmp[1] = ':';
5401 tmp[2] = '\0';
5402
5403 v = PyString_FromString(tmp);
5404 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
5405 return -1;
5406 Py_DECREF(v);
5407
5408 return 0;
5409}
5410#endif
5411
Barry Warsaw4a342091996-12-19 23:50:02 +00005412static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005413all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00005414{
Guido van Rossum94f6f721999-01-06 18:42:14 +00005415#ifdef F_OK
5416 if (ins(d, "F_OK", (long)F_OK)) return -1;
5417#endif
5418#ifdef R_OK
5419 if (ins(d, "R_OK", (long)R_OK)) return -1;
5420#endif
5421#ifdef W_OK
5422 if (ins(d, "W_OK", (long)W_OK)) return -1;
5423#endif
5424#ifdef X_OK
5425 if (ins(d, "X_OK", (long)X_OK)) return -1;
5426#endif
Fred Drakec9680921999-12-13 16:37:25 +00005427#ifdef NGROUPS_MAX
5428 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
5429#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00005430#ifdef TMP_MAX
5431 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
5432#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005433#ifdef WNOHANG
5434 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
5435#endif
5436#ifdef O_RDONLY
5437 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
5438#endif
5439#ifdef O_WRONLY
5440 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
5441#endif
5442#ifdef O_RDWR
5443 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
5444#endif
5445#ifdef O_NDELAY
5446 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
5447#endif
5448#ifdef O_NONBLOCK
5449 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
5450#endif
5451#ifdef O_APPEND
5452 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
5453#endif
5454#ifdef O_DSYNC
5455 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
5456#endif
5457#ifdef O_RSYNC
5458 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
5459#endif
5460#ifdef O_SYNC
5461 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
5462#endif
5463#ifdef O_NOCTTY
5464 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
5465#endif
5466#ifdef O_CREAT
5467 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
5468#endif
5469#ifdef O_EXCL
5470 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
5471#endif
5472#ifdef O_TRUNC
5473 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
5474#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00005475#ifdef O_BINARY
5476 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
5477#endif
5478#ifdef O_TEXT
5479 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
5480#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005481
Guido van Rossum246bc171999-02-01 23:54:31 +00005482#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00005483 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
5484 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
5485 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
5486 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
5487 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00005488#endif
5489
Guido van Rossumd48f2521997-12-05 22:19:34 +00005490#if defined(PYOS_OS2)
5491 if (insertvalues(d)) return -1;
5492#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00005493 return 0;
5494}
5495
5496
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005497#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005498#define INITFUNC initnt
5499#define MODNAME "nt"
5500#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005501#if defined(PYOS_OS2)
5502#define INITFUNC initos2
5503#define MODNAME "os2"
5504#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005505#define INITFUNC initposix
5506#define MODNAME "posix"
5507#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005508#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005509
Guido van Rossum3886bb61998-12-04 18:50:17 +00005510DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005511INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00005512{
Barry Warsaw53699e91996-12-10 23:23:01 +00005513 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00005514
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005515 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005516 posix_methods,
5517 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005518 (PyObject *)NULL,
5519 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00005520 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005521
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005522 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005523 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00005524 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00005525 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00005526 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00005527
Barry Warsaw4a342091996-12-19 23:50:02 +00005528 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00005529 return;
5530
Fred Drakebec628d1999-12-15 18:31:10 +00005531 if (setup_confname_tables(d))
5532 return;
5533
Barry Warsawca74da41999-02-09 19:31:45 +00005534 PyDict_SetItemString(d, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00005535
Guido van Rossumb3d39562000-01-31 18:41:26 +00005536#ifdef HAVE_PUTENV
Fred Drake762e2061999-08-26 17:23:54 +00005537 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00005538#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005539}