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