blob: 8458d583a8c374e3a59161af5d184425406276be [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* POSIX module implementation */
33
Guido van Rossuma4916fa1996-05-23 22:58:55 +000034/* This file is also used for Windows NT and MS-Win. In that case the module
Guido van Rossumad0ee831995-03-01 10:34:45 +000035 actually calls itself 'nt', not 'posix', and a few functions are
36 either unimplemented or implemented differently. The source
Guido van Rossum8d665e61996-06-26 18:22:49 +000037 assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +000038 of the compiler used. Different compilers define their own feature
Guido van Rossuma4916fa1996-05-23 22:58:55 +000039 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000040
Guido van Rossuma4916fa1996-05-23 22:58:55 +000041/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000042
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000043static char posix__doc__ [] =
44"This module provides access to operating system functionality that is\n\
45standardized by the C Standard and the POSIX standard (a thinly\n\
46disguised Unix interface). Refer to the library manual and\n\
47corresponding Unix manual entries for more information on calls.";
48
Barry Warsaw53699e91996-12-10 23:23:01 +000049#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000051#if defined(PYOS_OS2)
52#define INCL_DOS
53#define INCL_DOSERRORS
54#define INCL_DOSPROCESS
55#define INCL_NOPMAPI
56#include <os2.h>
57#endif
58
Guido van Rossumb6775db1994-08-01 11:34:53 +000059#include <sys/types.h>
60#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000061#ifdef HAVE_SYS_WAIT_H
62#include <sys/wait.h> /* For WNOHANG */
63#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000064
Guido van Rossuma376cc51996-12-05 23:43:35 +000065#ifdef HAVE_SIGNAL_H
66#include <signal.h>
67#endif
68
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include "mytime.h" /* For clock_t on some systems */
70
71#ifdef HAVE_FCNTL_H
72#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000073#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Guido van Rossuma4916fa1996-05-23 22:58:55 +000075/* Various compilers have only certain posix functions */
Guido van Rossum6d8841c1997-08-14 19:57:39 +000076/* XXX Gosh I wish these were all moved into config.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000077#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000078#include <process.h>
79#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000080#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000081#define HAVE_GETCWD 1
82#define HAVE_OPENDIR 1
83#define HAVE_SYSTEM 1
84#if defined(__OS2__)
85#define HAVE_EXECV 1
86#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000087#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000088#include <process.h>
89#else
90#ifdef __BORLANDC__ /* Borland compiler */
91#define HAVE_EXECV 1
92#define HAVE_GETCWD 1
93#define HAVE_GETEGID 1
94#define HAVE_GETEUID 1
95#define HAVE_GETGID 1
96#define HAVE_GETPPID 1
97#define HAVE_GETUID 1
98#define HAVE_KILL 1
99#define HAVE_OPENDIR 1
100#define HAVE_PIPE 1
101#define HAVE_POPEN 1
102#define HAVE_SYSTEM 1
103#define HAVE_WAIT 1
104#else
105#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000106#define HAVE_GETCWD 1
107#ifdef MS_WIN32
Guido van Rossuma1065681999-01-25 23:20:23 +0000108#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#define HAVE_EXECV 1
110#define HAVE_PIPE 1
111#define HAVE_POPEN 1
112#define HAVE_SYSTEM 1
113#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000114#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#else /* all other compilers */
116/* Unix functions that the configure script doesn't check for */
117#define HAVE_EXECV 1
118#define HAVE_FORK 1
119#define HAVE_GETCWD 1
120#define HAVE_GETEGID 1
121#define HAVE_GETEUID 1
122#define HAVE_GETGID 1
123#define HAVE_GETPPID 1
124#define HAVE_GETUID 1
125#define HAVE_KILL 1
126#define HAVE_OPENDIR 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
129#define HAVE_SYSTEM 1
130#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000131#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#endif /* _MSC_VER */
133#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000134#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000135#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000138
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000140#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000141#endif
142
143#ifdef NeXT
144/* NeXT's <unistd.h> and <utime.h> aren't worth much */
145#undef HAVE_UNISTD_H
146#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000147#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000148/* #undef HAVE_GETCWD */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000149#define UNION_WAIT /* This should really be checked for by autoconf */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000150#endif
151
152#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000153/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
154extern int rename();
155extern int pclose();
156extern int lstat();
157extern int symlink();
Guido van Rossum8c67e4e1999-04-07 15:49:41 +0000158extern int fsync();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000160#if defined(PYCC_VACPP)
161extern int mkdir Py_PROTO((char *));
162#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000163#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000164extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000166extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000167#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#endif
169#if defined(__IBMC__) || defined(__IBMCPP__)
170extern int chdir Py_PROTO((char *));
171extern int rmdir Py_PROTO((char *));
172#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000173extern int chdir Py_PROTO((const char *));
174extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000176extern int chmod Py_PROTO((const char *, mode_t));
177extern int chown Py_PROTO((const char *, uid_t, gid_t));
178extern char *getcwd Py_PROTO((char *, int));
179extern char *strerror Py_PROTO((int));
180extern int link Py_PROTO((const char *, const char *));
181extern int rename Py_PROTO((const char *, const char *));
182extern int stat Py_PROTO((const char *, struct stat *));
183extern int unlink Py_PROTO((const char *));
184extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000186extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000187#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000189extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000190#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000192
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#ifdef HAVE_UTIME_H
196#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000197#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000199#ifdef HAVE_SYS_UTIME_H
200#include <sys/utime.h>
201#define HAVE_UTIME_H /* pretend we do for the rest of this file */
202#endif /* HAVE_SYS_UTIME_H */
203
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_SYS_TIMES_H
205#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
208#ifdef HAVE_SYS_PARAM_H
209#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
212#ifdef HAVE_SYS_UTSNAME_H
213#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
216#ifndef MAXPATHLEN
217#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000218#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000220#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000222#define NAMLEN(dirent) strlen((dirent)->d_name)
223#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000224#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#include <direct.h>
226#define NAMLEN(dirent) strlen((dirent)->d_name)
227#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000230#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000231#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#endif
234#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#endif
237#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#endif
240#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000242#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#include <direct.h>
244#include <io.h>
245#include <process.h>
246#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000247#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000249#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000250#else /* 16-bit Windows */
251#include <dos.h>
252#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000253#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000254#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossumd48f2521997-12-05 22:19:34 +0000256#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000260#ifdef UNION_WAIT
261/* Emulate some macros on systems that have a union instead of macros */
262
263#ifndef WIFEXITED
264#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
265#endif
266
267#ifndef WEXITSTATUS
268#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
269#endif
270
271#ifndef WTERMSIG
272#define WTERMSIG(u_wait) ((u_wait).w_termsig)
273#endif
274
275#endif /* UNION_WAIT */
276
Greg Wardb48bc172000-03-01 21:51:56 +0000277/* Don't use the "_r" form if we don't need it (also, won't have a
278 prototype for it, at least on Solaris -- maybe others as well?). */
279#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
280#define USE_CTERMID_R
281#endif
282
283#if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
284#define USE_TMPNAM_R
285#endif
286
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287/* Return a dictionary corresponding to the POSIX environment table */
288
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000289#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000291#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292
Barry Warsaw53699e91996-12-10 23:23:01 +0000293static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294convertenviron()
295{
Barry Warsaw53699e91996-12-10 23:23:01 +0000296 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000298 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 if (d == NULL)
300 return NULL;
301 if (environ == NULL)
302 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000303 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000305 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000306 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307 char *p = strchr(*e, '=');
308 if (p == NULL)
309 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000310 k = PyString_FromStringAndSize(*e, (int)(p-*e));
311 if (k == NULL) {
312 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000314 }
315 v = PyString_FromString(p+1);
316 if (v == NULL) {
317 PyErr_Clear();
318 Py_DECREF(k);
319 continue;
320 }
321 if (PyDict_GetItem(d, k) == NULL) {
322 if (PyDict_SetItem(d, k, v) != 0)
323 PyErr_Clear();
324 }
325 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000326 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000328#if defined(PYOS_OS2)
329 {
330 APIRET rc;
331 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
332
333 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
334 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
335 PyObject *v = PyString_FromString(buffer);
336 PyDict_SetItemString(d, "BEGINLIBPATH", v);
337 Py_DECREF(v);
338 }
339 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
340 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
341 PyObject *v = PyString_FromString(buffer);
342 PyDict_SetItemString(d, "ENDLIBPATH", v);
343 Py_DECREF(v);
344 }
345 }
346#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 return d;
348}
349
350
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351/* Set a POSIX-specific error from errno, and return NULL */
352
Barry Warsawd58d7641998-07-23 16:14:40 +0000353static PyObject *
354posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000355{
Barry Warsawca74da41999-02-09 19:31:45 +0000356 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357}
Barry Warsawd58d7641998-07-23 16:14:40 +0000358static PyObject *
359posix_error_with_filename(name)
360 char* name;
361{
Barry Warsawca74da41999-02-09 19:31:45 +0000362 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000363}
364
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365
Guido van Rossumd48f2521997-12-05 22:19:34 +0000366#if defined(PYOS_OS2)
367/**********************************************************************
368 * Helper Function to Trim and Format OS/2 Messages
369 **********************************************************************/
370 static void
371os2_formatmsg(char *msgbuf, int msglen, char *reason)
372{
373 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
374
375 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
376 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
377
378 while (lastc > msgbuf && isspace(*lastc))
379 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
380 }
381
382 /* Add Optional Reason Text */
383 if (reason) {
384 strcat(msgbuf, " : ");
385 strcat(msgbuf, reason);
386 }
387}
388
389/**********************************************************************
390 * Decode an OS/2 Operating System Error Code
391 *
392 * A convenience function to lookup an OS/2 error code and return a
393 * text message we can use to raise a Python exception.
394 *
395 * Notes:
396 * The messages for errors returned from the OS/2 kernel reside in
397 * the file OSO001.MSG in the \OS2 directory hierarchy.
398 *
399 **********************************************************************/
400 static char *
401os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
402{
403 APIRET rc;
404 ULONG msglen;
405
406 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
407 Py_BEGIN_ALLOW_THREADS
408 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
409 errorcode, "oso001.msg", &msglen);
410 Py_END_ALLOW_THREADS
411
412 if (rc == NO_ERROR)
413 os2_formatmsg(msgbuf, msglen, reason);
414 else
415 sprintf(msgbuf, "unknown OS error #%d", errorcode);
416
417 return msgbuf;
418}
419
420/* Set an OS/2-specific error and return NULL. OS/2 kernel
421 errors are not in a global variable e.g. 'errno' nor are
422 they congruent with posix error numbers. */
423
424static PyObject * os2_error(int code)
425{
426 char text[1024];
427 PyObject *v;
428
429 os2_strerror(text, sizeof(text), code, "");
430
431 v = Py_BuildValue("(is)", code, text);
432 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000433 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000434 Py_DECREF(v);
435 }
436 return NULL; /* Signal to Python that an Exception is Pending */
437}
438
439#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440
441/* POSIX generic methods */
442
Barry Warsaw53699e91996-12-10 23:23:01 +0000443static PyObject *
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000444posix_int(args, format, func)
Guido van Rossum21142a01999-01-08 21:05:37 +0000445 PyObject *args;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000446 char *format;
Guido van Rossum21142a01999-01-08 21:05:37 +0000447 int (*func) Py_FPROTO((int));
448{
449 int fd;
450 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000451 if (!PyArg_ParseTuple(args, format, &fd))
Guido van Rossum21142a01999-01-08 21:05:37 +0000452 return NULL;
453 Py_BEGIN_ALLOW_THREADS
454 res = (*func)(fd);
455 Py_END_ALLOW_THREADS
456 if (res < 0)
457 return posix_error();
458 Py_INCREF(Py_None);
459 return Py_None;
460}
461
462
463static PyObject *
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000464posix_1str(args, format, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000465 PyObject *args;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000466 char *format;
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000469 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000470 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000471 if (!PyArg_ParseTuple(args, format, &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000473 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000474 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000475 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000476 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000477 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000478 Py_INCREF(Py_None);
479 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480}
481
Barry Warsaw53699e91996-12-10 23:23:01 +0000482static PyObject *
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000483posix_2str(args, format, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000484 PyObject *args;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000485 char *format;
Barry Warsaw53699e91996-12-10 23:23:01 +0000486 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000488 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000489 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000490 if (!PyArg_ParseTuple(args, format, &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000492 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000493 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000495 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000496 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000498 Py_INCREF(Py_None);
499 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500}
501
Barry Warsaw53699e91996-12-10 23:23:01 +0000502static PyObject *
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000503posix_strint(args, format, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000504 PyObject *args;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000505 char *format;
Barry Warsaw53699e91996-12-10 23:23:01 +0000506 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000508 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000510 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000511 if (!PyArg_ParseTuple(args, format, &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000513 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000514 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000515 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000516 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000517 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000518 Py_INCREF(Py_None);
519 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520}
521
Barry Warsaw53699e91996-12-10 23:23:01 +0000522static PyObject *
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000523posix_strintint(args, format, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000524 PyObject *args;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000525 char *format;
Barry Warsaw53699e91996-12-10 23:23:01 +0000526 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000527{
528 char *path;
529 int i,i2;
530 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000531 if (!PyArg_ParseTuple(args, format, &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000532 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000533 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000534 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000535 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000536 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000537 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000538 Py_INCREF(Py_None);
539 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000540}
541
Barry Warsaw53699e91996-12-10 23:23:01 +0000542static PyObject *
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000543posix_do_stat(self, args, format, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000544 PyObject *self;
545 PyObject *args;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000546 char *format;
Barry Warsaw53699e91996-12-10 23:23:01 +0000547 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548{
549 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000550 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000551 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000552 if (!PyArg_ParseTuple(args, format, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000554 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000555 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000556 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000557 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000558 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000559#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000560 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000561 (long)st.st_mode,
562 (long)st.st_ino,
563 (long)st.st_dev,
564 (long)st.st_nlink,
565 (long)st.st_uid,
566 (long)st.st_gid,
567 (long)st.st_size,
568 (long)st.st_atime,
569 (long)st.st_mtime,
570 (long)st.st_ctime);
571#else
572 return Py_BuildValue("(lLllllLlll)",
573 (long)st.st_mode,
574 (LONG_LONG)st.st_ino,
575 (long)st.st_dev,
576 (long)st.st_nlink,
577 (long)st.st_uid,
578 (long)st.st_gid,
579 (LONG_LONG)st.st_size,
580 (long)st.st_atime,
581 (long)st.st_mtime,
582 (long)st.st_ctime);
583#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584}
585
586
587/* POSIX methods */
588
Guido van Rossum94f6f721999-01-06 18:42:14 +0000589static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000590"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000591Test for access to a file.";
592
593static PyObject *
594posix_access(self, args)
595 PyObject *self;
596 PyObject *args;
597{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000598 char *path;
599 int mode;
600 int res;
601
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000602 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +0000603 return NULL;
604 Py_BEGIN_ALLOW_THREADS
605 res = access(path, mode);
606 Py_END_ALLOW_THREADS
607 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000608}
609
Guido van Rossumd371ff11999-01-25 16:12:23 +0000610#ifndef F_OK
611#define F_OK 0
612#endif
613#ifndef R_OK
614#define R_OK 4
615#endif
616#ifndef W_OK
617#define W_OK 2
618#endif
619#ifndef X_OK
620#define X_OK 1
621#endif
622
623#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000624static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000625"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000626Return the name of the terminal device connected to 'fd'.";
627
628static PyObject *
629posix_ttyname(self, args)
630 PyObject *self;
631 PyObject *args;
632{
Guido van Rossum94f6f721999-01-06 18:42:14 +0000633 int id;
634 char *ret;
635
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000636 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +0000637 return NULL;
638
Guido van Rossum94f6f721999-01-06 18:42:14 +0000639 ret = ttyname(id);
640 if (ret == NULL)
641 return(posix_error());
642 return(PyString_FromString(ret));
643}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000644#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000645
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000646#ifdef HAVE_CTERMID
647static char posix_ctermid__doc__[] =
648"ctermid() -> String\n\
649Return the name of the controlling terminal for this process.";
650
651static PyObject *
652posix_ctermid(self, args)
653 PyObject *self;
654 PyObject *args;
655{
656 char *ret;
657 char buffer[L_ctermid];
658
659 if (!PyArg_ParseTuple(args, ":ctermid"))
660 return NULL;
661
Greg Wardb48bc172000-03-01 21:51:56 +0000662#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000663 ret = ctermid_r(buffer);
664#else
665 ret = ctermid(buffer);
666#endif
667 if (ret == NULL)
668 return(posix_error());
669 return(PyString_FromString(buffer));
670}
671#endif
672
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000673static char posix_chdir__doc__[] =
674"chdir(path) -> None\n\
675Change the current working directory to the specified path.";
676
Barry Warsaw53699e91996-12-10 23:23:01 +0000677static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000679 PyObject *self;
680 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000682 return posix_1str(args, "s:chdir", chdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000683}
684
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000685
686static char posix_chmod__doc__[] =
687"chmod(path, mode) -> None\n\
688Change the access permissions of a file.";
689
Barry Warsaw53699e91996-12-10 23:23:01 +0000690static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000692 PyObject *self;
693 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694{
Guido van Rossumffd15f52000-03-31 00:47:28 +0000695 char *path;
696 int i;
697 int res;
Guido van Rossum49679b42000-03-31 00:48:21 +0000698 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +0000699 return NULL;
700 Py_BEGIN_ALLOW_THREADS
Guido van Rossum49679b42000-03-31 00:48:21 +0000701 res = chmod(path, (mode_t)i);
Guido van Rossumffd15f52000-03-31 00:47:28 +0000702 Py_END_ALLOW_THREADS
703 if (res < 0)
704 return posix_error_with_filename(path);
705 Py_INCREF(Py_None);
706 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707}
708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000709
Guido van Rossum21142a01999-01-08 21:05:37 +0000710#ifdef HAVE_FSYNC
711static char posix_fsync__doc__[] =
712"fsync(fildes) -> None\n\
713force write of file with filedescriptor to disk.";
714
715static PyObject *
716posix_fsync(self, args)
717 PyObject *self;
718 PyObject *args;
719{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000720 return posix_int(args, "i:fsync", fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000721}
722#endif /* HAVE_FSYNC */
723
724#ifdef HAVE_FDATASYNC
725static char posix_fdatasync__doc__[] =
726"fdatasync(fildes) -> None\n\
727force write of file with filedescriptor to disk.\n\
728 does not force update of metadata.";
729
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000730extern int fdatasync(int); /* Prototype just in case */
731
Guido van Rossum21142a01999-01-08 21:05:37 +0000732static PyObject *
733posix_fdatasync(self, args)
734 PyObject *self;
735 PyObject *args;
736{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000737 return posix_int(args, "i:fdatasync", fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000738}
739#endif /* HAVE_FDATASYNC */
740
741
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000743static char posix_chown__doc__[] =
744"chown(path, uid, gid) -> None\n\
745Change the owner and group id of path to the numeric uid and gid.";
746
Barry Warsaw53699e91996-12-10 23:23:01 +0000747static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000749 PyObject *self;
750 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000752 return posix_strintint(args, "sii:chown", chown);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000753}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000754#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000755
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000756
Guido van Rossum36bc6801995-06-14 22:54:23 +0000757#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000758static char posix_getcwd__doc__[] =
759"getcwd() -> path\n\
760Return a string representing the current working directory.";
761
Barry Warsaw53699e91996-12-10 23:23:01 +0000762static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000763posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000764 PyObject *self;
765 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000766{
767 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000768 char *res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000769 if (!PyArg_ParseTuple(args, ":getcwd"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000771 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000772 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000773 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000774 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000776 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000778#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000780
Guido van Rossumb6775db1994-08-01 11:34:53 +0000781#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000782static char posix_link__doc__[] =
783"link(src, dst) -> None\n\
784Create a hard link to a file.";
785
Barry Warsaw53699e91996-12-10 23:23:01 +0000786static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000788 PyObject *self;
789 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000791 return posix_2str(args, "ss:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000793#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000795
796static char posix_listdir__doc__[] =
797"listdir(path) -> list_of_strings\n\
798Return a list containing the names of the entries in the directory.\n\
799\n\
800 path: path of directory to list\n\
801\n\
802The list is in arbitrary order. It does not include the special\n\
803entries '.' and '..' even if they are present in the directory.";
804
Barry Warsaw53699e91996-12-10 23:23:01 +0000805static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000806posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000807 PyObject *self;
808 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000809{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000810 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000811 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000812#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000813
Guido van Rossumb6775db1994-08-01 11:34:53 +0000814 char *name;
815 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000816 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000817 HANDLE hFindFile;
818 WIN32_FIND_DATA FileData;
819 char namebuf[MAX_PATH+5];
820
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000821 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822 return NULL;
823 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000824 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000825 return NULL;
826 }
827 strcpy(namebuf, name);
828 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
829 namebuf[len++] = '/';
830 strcpy(namebuf + len, "*.*");
831
Barry Warsaw53699e91996-12-10 23:23:01 +0000832 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000833 return NULL;
834
835 hFindFile = FindFirstFile(namebuf, &FileData);
836 if (hFindFile == INVALID_HANDLE_VALUE) {
837 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000838 if (errno == ERROR_FILE_NOT_FOUND)
839 return PyList_New(0);
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000840 return posix_error_with_filename(name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000841 }
842 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000843 if (FileData.cFileName[0] == '.' &&
844 (FileData.cFileName[1] == '\0' ||
845 FileData.cFileName[1] == '.' &&
846 FileData.cFileName[2] == '\0'))
847 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000848 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000849 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000850 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000851 d = NULL;
852 break;
853 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000854 if (PyList_Append(d, v) != 0) {
855 Py_DECREF(v);
856 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000857 d = NULL;
858 break;
859 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000860 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000861 } while (FindNextFile(hFindFile, &FileData) == TRUE);
862
863 if (FindClose(hFindFile) == FALSE) {
864 errno = GetLastError();
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000865 return posix_error_with_filename(&name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000866 }
867
868 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000869
Guido van Rossum8d665e61996-06-26 18:22:49 +0000870#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000871#ifdef _MSC_VER /* 16-bit Windows */
872
873#ifndef MAX_PATH
874#define MAX_PATH 250
875#endif
876 char *name, *pt;
877 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000878 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000879 char namebuf[MAX_PATH+5];
880 struct _find_t ep;
881
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000882 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000883 return NULL;
884 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000885 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000886 return NULL;
887 }
888 strcpy(namebuf, name);
889 for (pt = namebuf; *pt; pt++)
890 if (*pt == '/')
891 *pt = '\\';
892 if (namebuf[len-1] != '\\')
893 namebuf[len++] = '\\';
894 strcpy(namebuf + len, "*.*");
895
Barry Warsaw53699e91996-12-10 23:23:01 +0000896 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000897 return NULL;
898
899 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000900 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
901 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000902 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000903 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000904 }
905 do {
906 if (ep.name[0] == '.' &&
907 (ep.name[1] == '\0' ||
908 ep.name[1] == '.' &&
909 ep.name[2] == '\0'))
910 continue;
911 strcpy(namebuf, ep.name);
912 for (pt = namebuf; *pt; pt++)
913 if (isupper(*pt))
914 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000915 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000916 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000917 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000918 d = NULL;
919 break;
920 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000921 if (PyList_Append(d, v) != 0) {
922 Py_DECREF(v);
923 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000924 d = NULL;
925 break;
926 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000927 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000928 } while (_dos_findnext(&ep) == 0);
929
930 return d;
931
932#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000933#if defined(PYOS_OS2)
934
935#ifndef MAX_PATH
936#define MAX_PATH CCHMAXPATH
937#endif
938 char *name, *pt;
939 int len;
940 PyObject *d, *v;
941 char namebuf[MAX_PATH+5];
942 HDIR hdir = 1;
943 ULONG srchcnt = 1;
944 FILEFINDBUF3 ep;
945 APIRET rc;
946
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000947 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000948 return NULL;
949 if (len >= MAX_PATH) {
950 PyErr_SetString(PyExc_ValueError, "path too long");
951 return NULL;
952 }
953 strcpy(namebuf, name);
954 for (pt = namebuf; *pt; pt++)
955 if (*pt == '/')
956 *pt = '\\';
957 if (namebuf[len-1] != '\\')
958 namebuf[len++] = '\\';
959 strcpy(namebuf + len, "*.*");
960
961 if ((d = PyList_New(0)) == NULL)
962 return NULL;
963
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000964 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
965 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000966 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000967 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
968 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
969 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000970
971 if (rc != NO_ERROR) {
972 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000973 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000974 }
975
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000976 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000977 do {
978 if (ep.achName[0] == '.'
979 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000980 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000981
982 strcpy(namebuf, ep.achName);
983
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000984 /* Leave Case of Name Alone -- In Native Form */
985 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000986
987 v = PyString_FromString(namebuf);
988 if (v == NULL) {
989 Py_DECREF(d);
990 d = NULL;
991 break;
992 }
993 if (PyList_Append(d, v) != 0) {
994 Py_DECREF(v);
995 Py_DECREF(d);
996 d = NULL;
997 break;
998 }
999 Py_DECREF(v);
1000 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1001 }
1002
1003 return d;
1004#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001005
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001006 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00001007 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001008 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001009 struct dirent *ep;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001010 if (!PyArg_ParseTuple(args, "s:listdir", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001011 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001012 if ((dirp = opendir(name)) == NULL) {
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001013 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001014 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001015 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001016 closedir(dirp);
1017 return NULL;
1018 }
1019 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001020 if (ep->d_name[0] == '.' &&
1021 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001022 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001023 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001024 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001025 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001026 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001027 d = NULL;
1028 break;
1029 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001030 if (PyList_Append(d, v) != 0) {
1031 Py_DECREF(v);
1032 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001033 d = NULL;
1034 break;
1035 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001036 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001037 }
1038 closedir(dirp);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001039
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001040 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001041
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001042#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001043#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +00001044#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001045}
1046
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001047static char posix_mkdir__doc__[] =
1048"mkdir(path [, mode=0777]) -> None\n\
1049Create a directory.";
1050
Barry Warsaw53699e91996-12-10 23:23:01 +00001051static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001052posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001053 PyObject *self;
1054 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001055{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001056 int res;
1057 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001058 int mode = 0777;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001059 if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001060 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001061 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001062#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001063 res = mkdir(path);
1064#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001065 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001066#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001067 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001068 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001069 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 Py_INCREF(Py_None);
1071 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001072}
1073
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001074
Guido van Rossumb6775db1994-08-01 11:34:53 +00001075#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001076static char posix_nice__doc__[] =
1077"nice(inc) -> new_priority\n\
1078Decrease the priority of process and return new priority.";
1079
Barry Warsaw53699e91996-12-10 23:23:01 +00001080static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001081posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001082 PyObject *self;
1083 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001084{
1085 int increment, value;
1086
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001087 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001088 return NULL;
1089 value = nice(increment);
1090 if (value == -1)
1091 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001092 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001093}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001094#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001096
1097static char posix_rename__doc__[] =
1098"rename(old, new) -> None\n\
1099Rename a file or directory.";
1100
Barry Warsaw53699e91996-12-10 23:23:01 +00001101static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001102posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001103 PyObject *self;
1104 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001106 return posix_2str(args, "ss:rename", rename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001107}
1108
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001109
1110static char posix_rmdir__doc__[] =
1111"rmdir(path) -> None\n\
1112Remove a directory.";
1113
Barry Warsaw53699e91996-12-10 23:23:01 +00001114static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001115posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001116 PyObject *self;
1117 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001118{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001119 return posix_1str(args, "s:rmdir", rmdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001120}
1121
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001122
1123static char posix_stat__doc__[] =
1124"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1125Perform a stat system call on the given path.";
1126
Barry Warsaw53699e91996-12-10 23:23:01 +00001127static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001129 PyObject *self;
1130 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001131{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001132 return posix_do_stat(self, args, "s:stat", stat);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001133}
1134
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001135
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001136#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001137static char posix_system__doc__[] =
1138"system(command) -> exit_status\n\
1139Execute the command (a string) in a subshell.";
1140
Barry Warsaw53699e91996-12-10 23:23:01 +00001141static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001142posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001143 PyObject *self;
1144 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001145{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001146 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001147 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001148 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001149 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001150 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001151 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001152 Py_END_ALLOW_THREADS
1153 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001154}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001155#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001156
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001157
1158static char posix_umask__doc__[] =
1159"umask(new_mask) -> old_mask\n\
1160Set the current numeric umask and return the previous umask.";
1161
Barry Warsaw53699e91996-12-10 23:23:01 +00001162static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001163posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001164 PyObject *self;
1165 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001166{
1167 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001168 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001169 return NULL;
1170 i = umask(i);
1171 if (i < 0)
1172 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001173 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001174}
1175
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001176
1177static char posix_unlink__doc__[] =
1178"unlink(path) -> None\n\
1179Remove a file (same as remove(path)).";
1180
1181static char posix_remove__doc__[] =
1182"remove(path) -> None\n\
1183Remove a file (same as unlink(path)).";
1184
Barry Warsaw53699e91996-12-10 23:23:01 +00001185static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001186posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001187 PyObject *self;
1188 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001190 return posix_1str(args, "s:remove", unlink);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191}
1192
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001193
Guido van Rossumb6775db1994-08-01 11:34:53 +00001194#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001195static char posix_uname__doc__[] =
1196"uname() -> (sysname, nodename, release, version, machine)\n\
1197Return a tuple identifying the current operating system.";
1198
Barry Warsaw53699e91996-12-10 23:23:01 +00001199static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001200posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001201 PyObject *self;
1202 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001203{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001204 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001205 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001206 if (!PyArg_ParseTuple(args, ":uname"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001207 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001208 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001209 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001210 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001211 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001212 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001213 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001214 u.sysname,
1215 u.nodename,
1216 u.release,
1217 u.version,
1218 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001219}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001220#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001222
1223static char posix_utime__doc__[] =
1224"utime(path, (atime, utime)) -> None\n\
1225Set the access and modified time of the file to the given values.";
1226
Barry Warsaw53699e91996-12-10 23:23:01 +00001227static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001228posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001229 PyObject *self;
1230 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001231{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001232 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001233 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001234 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001235
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001236/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001237#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001238 struct utimbuf buf;
1239#define ATIME buf.actime
1240#define MTIME buf.modtime
1241#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001242#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001243 time_t buf[2];
1244#define ATIME buf[0]
1245#define MTIME buf[1]
1246#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001247#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001248
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001249 if (!PyArg_ParseTuple(args, "s(ll):utime", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001250 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001251 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001252 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001253 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001254 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001255 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001256 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001257 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001258 Py_INCREF(Py_None);
1259 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001260#undef UTIME_ARG
1261#undef ATIME
1262#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263}
1264
Guido van Rossum85e3b011991-06-03 12:42:10 +00001265
Guido van Rossum3b066191991-06-04 19:40:25 +00001266/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001268static char posix__exit__doc__[] =
1269"_exit(status)\n\
1270Exit to the system with specified status, without normal exit processing.";
1271
Barry Warsaw53699e91996-12-10 23:23:01 +00001272static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001273posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001274 PyObject *self;
1275 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001276{
1277 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001278 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001279 return NULL;
1280 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001281 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001282}
1283
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001284
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001285#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001286static char posix_execv__doc__[] =
1287"execv(path, args)\n\
1288Execute an executable path with arguments, replacing current process.\n\
1289\n\
1290 path: path of executable file\n\
1291 args: tuple or list of strings";
1292
Barry Warsaw53699e91996-12-10 23:23:01 +00001293static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001294posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001295 PyObject *self;
1296 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001297{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001298 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001299 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001300 char **argvlist;
1301 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001302 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001303
Guido van Rossum89b33251993-10-22 14:26:06 +00001304 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001305 argv is a list or tuple of strings. */
1306
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001307 if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001308 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001309 if (PyList_Check(argv)) {
1310 argc = PyList_Size(argv);
1311 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001312 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 else if (PyTuple_Check(argv)) {
1314 argc = PyTuple_Size(argv);
1315 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001316 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001317 else {
1318 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001319 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001320 return NULL;
1321 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001322
Barry Warsaw53699e91996-12-10 23:23:01 +00001323 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001324 if (argvlist == NULL)
1325 return NULL;
1326 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001327 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1328 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001329 goto badarg;
1330 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001331 }
1332 argvlist[argc] = NULL;
1333
Guido van Rossumb6775db1994-08-01 11:34:53 +00001334#ifdef BAD_EXEC_PROTOTYPES
1335 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001336#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001337 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001338#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001339
Guido van Rossum85e3b011991-06-03 12:42:10 +00001340 /* If we get here it's definitely an error */
1341
Barry Warsaw53699e91996-12-10 23:23:01 +00001342 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001343 return posix_error();
1344}
1345
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001346
1347static char posix_execve__doc__[] =
1348"execve(path, args, env)\n\
1349Execute a path with arguments and environment, replacing current process.\n\
1350\n\
1351 path: path of executable file\n\
1352 args: tuple or list of arguments\n\
1353 env: dictonary of strings mapping to strings";
1354
Barry Warsaw53699e91996-12-10 23:23:01 +00001355static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001356posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001357 PyObject *self;
1358 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001359{
1360 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001361 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001362 char **argvlist;
1363 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001364 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001365 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001366 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001367
1368 /* execve has three arguments: (path, argv, env), where
1369 argv is a list or tuple of strings and env is a dictionary
1370 like posix.environ. */
1371
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001372 if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001373 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001374 if (PyList_Check(argv)) {
1375 argc = PyList_Size(argv);
1376 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001377 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001378 else if (PyTuple_Check(argv)) {
1379 argc = PyTuple_Size(argv);
1380 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001381 }
1382 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001383 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001384 return NULL;
1385 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001386 if (!PyMapping_Check(env)) {
1387 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001388 return NULL;
1389 }
1390
Barry Warsaw53699e91996-12-10 23:23:01 +00001391 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001392 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001393 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001394 return NULL;
1395 }
1396 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001397 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001398 "s;argv must be list of strings",
1399 &argvlist[i]))
1400 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001401 goto fail_1;
1402 }
1403 }
1404 argvlist[argc] = NULL;
1405
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001406 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001407 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001408 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001409 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001410 goto fail_1;
1411 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001412 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001413 keys = PyMapping_Keys(env);
1414 vals = PyMapping_Values(env);
1415 if (!keys || !vals)
1416 goto fail_2;
1417
1418 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001419 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001420
1421 key = PyList_GetItem(keys, pos);
1422 val = PyList_GetItem(vals, pos);
1423 if (!key || !val)
1424 goto fail_2;
1425
Barry Warsaw53699e91996-12-10 23:23:01 +00001426 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001427 !PyArg_Parse(val, "s;non-string value in env", &v))
1428 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001429 goto fail_2;
1430 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001431
1432#if defined(PYOS_OS2)
1433 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1434 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1435#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001436 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001437 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001438 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001439 goto fail_2;
1440 }
1441 sprintf(p, "%s=%s", k, v);
1442 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001443#if defined(PYOS_OS2)
1444 }
1445#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001446 }
1447 envlist[envc] = 0;
1448
Guido van Rossumb6775db1994-08-01 11:34:53 +00001449
1450#ifdef BAD_EXEC_PROTOTYPES
1451 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001452#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001453 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001454#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001455
1456 /* If we get here it's definitely an error */
1457
1458 (void) posix_error();
1459
1460 fail_2:
1461 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001462 PyMem_DEL(envlist[envc]);
1463 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001464 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001465 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001466 Py_XDECREF(vals);
1467 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001468 return NULL;
1469}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001470#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001471
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001472
Guido van Rossuma1065681999-01-25 23:20:23 +00001473#ifdef HAVE_SPAWNV
1474static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001475"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001476Execute an executable path with arguments, replacing current process.\n\
1477\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001478 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001479 path: path of executable file\n\
1480 args: tuple or list of strings";
1481
1482static PyObject *
1483posix_spawnv(self, args)
1484 PyObject *self;
1485 PyObject *args;
1486{
1487 char *path;
1488 PyObject *argv;
1489 char **argvlist;
1490 int mode, i, argc;
1491 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1492
1493 /* spawnv has three arguments: (mode, path, argv), where
1494 argv is a list or tuple of strings. */
1495
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001496 if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00001497 return NULL;
1498 if (PyList_Check(argv)) {
1499 argc = PyList_Size(argv);
1500 getitem = PyList_GetItem;
1501 }
1502 else if (PyTuple_Check(argv)) {
1503 argc = PyTuple_Size(argv);
1504 getitem = PyTuple_GetItem;
1505 }
1506 else {
1507 badarg:
1508 PyErr_BadArgument();
1509 return NULL;
1510 }
1511
1512 argvlist = PyMem_NEW(char *, argc+1);
1513 if (argvlist == NULL)
1514 return NULL;
1515 for (i = 0; i < argc; i++) {
1516 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1517 PyMem_DEL(argvlist);
1518 goto badarg;
1519 }
1520 }
1521 argvlist[argc] = NULL;
1522
Guido van Rossum246bc171999-02-01 23:54:31 +00001523 if (mode == _OLD_P_OVERLAY)
1524 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001525 i = _spawnv(mode, path, argvlist);
1526
1527 PyMem_DEL(argvlist);
1528
1529 if (i == -1)
1530 return posix_error();
1531 else
1532 return Py_BuildValue("i", i);
1533}
1534
1535
1536static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001537"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001538Execute a path with arguments and environment, replacing current process.\n\
1539\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001540 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001541 path: path of executable file\n\
1542 args: tuple or list of arguments\n\
1543 env: dictonary of strings mapping to strings";
1544
1545static PyObject *
1546posix_spawnve(self, args)
1547 PyObject *self;
1548 PyObject *args;
1549{
1550 char *path;
1551 PyObject *argv, *env;
1552 char **argvlist;
1553 char **envlist;
1554 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1555 int mode, i, pos, argc, envc;
1556 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1557
1558 /* spawnve has four arguments: (mode, path, argv, env), where
1559 argv is a list or tuple of strings and env is a dictionary
1560 like posix.environ. */
1561
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001562 if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00001563 return NULL;
1564 if (PyList_Check(argv)) {
1565 argc = PyList_Size(argv);
1566 getitem = PyList_GetItem;
1567 }
1568 else if (PyTuple_Check(argv)) {
1569 argc = PyTuple_Size(argv);
1570 getitem = PyTuple_GetItem;
1571 }
1572 else {
1573 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1574 return NULL;
1575 }
1576 if (!PyMapping_Check(env)) {
1577 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1578 return NULL;
1579 }
1580
1581 argvlist = PyMem_NEW(char *, argc+1);
1582 if (argvlist == NULL) {
1583 PyErr_NoMemory();
1584 return NULL;
1585 }
1586 for (i = 0; i < argc; i++) {
1587 if (!PyArg_Parse((*getitem)(argv, i),
1588 "s;argv must be list of strings",
1589 &argvlist[i]))
1590 {
1591 goto fail_1;
1592 }
1593 }
1594 argvlist[argc] = NULL;
1595
1596 i = PyMapping_Length(env);
1597 envlist = PyMem_NEW(char *, i + 1);
1598 if (envlist == NULL) {
1599 PyErr_NoMemory();
1600 goto fail_1;
1601 }
1602 envc = 0;
1603 keys = PyMapping_Keys(env);
1604 vals = PyMapping_Values(env);
1605 if (!keys || !vals)
1606 goto fail_2;
1607
1608 for (pos = 0; pos < i; pos++) {
1609 char *p, *k, *v;
1610
1611 key = PyList_GetItem(keys, pos);
1612 val = PyList_GetItem(vals, pos);
1613 if (!key || !val)
1614 goto fail_2;
1615
1616 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1617 !PyArg_Parse(val, "s;non-string value in env", &v))
1618 {
1619 goto fail_2;
1620 }
1621 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1622 if (p == NULL) {
1623 PyErr_NoMemory();
1624 goto fail_2;
1625 }
1626 sprintf(p, "%s=%s", k, v);
1627 envlist[envc++] = p;
1628 }
1629 envlist[envc] = 0;
1630
Guido van Rossum246bc171999-02-01 23:54:31 +00001631 if (mode == _OLD_P_OVERLAY)
1632 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001633 i = _spawnve(mode, path, argvlist, envlist);
1634 if (i == -1)
1635 (void) posix_error();
1636 else
1637 res = Py_BuildValue("i", i);
1638
1639 fail_2:
1640 while (--envc >= 0)
1641 PyMem_DEL(envlist[envc]);
1642 PyMem_DEL(envlist);
1643 fail_1:
1644 PyMem_DEL(argvlist);
1645 Py_XDECREF(vals);
1646 Py_XDECREF(keys);
1647 return res;
1648}
1649#endif /* HAVE_SPAWNV */
1650
1651
Guido van Rossumad0ee831995-03-01 10:34:45 +00001652#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001653static char posix_fork__doc__[] =
1654"fork() -> pid\n\
1655Fork a child process.\n\
1656\n\
1657Return 0 to child process and PID of child to parent process.";
1658
Barry Warsaw53699e91996-12-10 23:23:01 +00001659static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001660posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001661 PyObject *self;
1662 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001663{
1664 int pid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001665 if (!PyArg_ParseTuple(args, ":fork"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001666 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001667 pid = fork();
1668 if (pid == -1)
1669 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001670 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001671 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001672}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001673#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001675
Guido van Rossumad0ee831995-03-01 10:34:45 +00001676#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001677static char posix_getegid__doc__[] =
1678"getegid() -> egid\n\
1679Return the current process's effective group id.";
1680
Barry Warsaw53699e91996-12-10 23:23:01 +00001681static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001682posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001683 PyObject *self;
1684 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001685{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001686 if (!PyArg_ParseTuple(args, ":getegid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001687 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001688 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001689}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001690#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001692
Guido van Rossumad0ee831995-03-01 10:34:45 +00001693#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001694static char posix_geteuid__doc__[] =
1695"geteuid() -> euid\n\
1696Return the current process's effective user id.";
1697
Barry Warsaw53699e91996-12-10 23:23:01 +00001698static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001699posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001700 PyObject *self;
1701 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001702{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001703 if (!PyArg_ParseTuple(args, ":geteuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001704 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001705 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001706}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001707#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001709
Guido van Rossumad0ee831995-03-01 10:34:45 +00001710#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001711static char posix_getgid__doc__[] =
1712"getgid() -> gid\n\
1713Return the current process's group id.";
1714
Barry Warsaw53699e91996-12-10 23:23:01 +00001715static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001716posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001717 PyObject *self;
1718 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001719{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001720 if (!PyArg_ParseTuple(args, ":getgid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001721 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001722 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001723}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001724#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001725
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001726
1727static char posix_getpid__doc__[] =
1728"getpid() -> pid\n\
1729Return the current process id";
1730
Barry Warsaw53699e91996-12-10 23:23:01 +00001731static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001732posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001733 PyObject *self;
1734 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001735{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001736 if (!PyArg_ParseTuple(args, ":getpid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001737 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001739}
1740
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001741
Fred Drakec9680921999-12-13 16:37:25 +00001742#ifdef HAVE_GETGROUPS
1743static char posix_getgroups__doc__[] = "\
1744getgroups() -> list of group IDs\n\
1745Return list of supplemental group IDs for the process.";
1746
1747static PyObject *
1748posix_getgroups(self, args)
1749 PyObject *self;
1750 PyObject *args;
1751{
1752 PyObject *result = NULL;
1753
1754 if (PyArg_ParseTuple(args, ":getgroups")) {
1755#ifdef NGROUPS_MAX
1756#define MAX_GROUPS NGROUPS_MAX
1757#else
1758 /* defined to be 16 on Solaris7, so this should be a small number */
1759#define MAX_GROUPS 64
1760#endif
1761 gid_t grouplist[MAX_GROUPS];
1762 int n;
1763
1764 n = getgroups(MAX_GROUPS, grouplist);
1765 if (n < 0)
1766 posix_error();
1767 else {
1768 result = PyList_New(n);
1769 if (result != NULL) {
1770 PyObject *o;
1771 int i;
1772 for (i = 0; i < n; ++i) {
1773 o = PyInt_FromLong((long)grouplist[i]);
1774 if (o == NULL) {
1775 Py_DECREF(result);
1776 result = NULL;
1777 break;
1778 }
1779 PyList_SET_ITEM(result, i, o);
1780 }
1781 }
1782 }
1783 }
1784 return result;
1785}
1786#endif
1787
Guido van Rossumb6775db1994-08-01 11:34:53 +00001788#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001789static char posix_getpgrp__doc__[] =
1790"getpgrp() -> pgrp\n\
1791Return the current process group id.";
1792
Barry Warsaw53699e91996-12-10 23:23:01 +00001793static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001794posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001795 PyObject *self;
1796 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001797{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001798 if (!PyArg_ParseTuple(args, ":getpgrp"))
Guido van Rossum04814471991-06-04 20:23:49 +00001799 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001800#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001801 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001802#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001803 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001804#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001805}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001806#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001807
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001808
Guido van Rossumb6775db1994-08-01 11:34:53 +00001809#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001810static char posix_setpgrp__doc__[] =
1811"setpgrp() -> None\n\
1812Make this process a session leader.";
1813
Barry Warsaw53699e91996-12-10 23:23:01 +00001814static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001815posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001816 PyObject *self;
1817 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001818{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001819 if (!PyArg_ParseTuple(args, ":setpgrp"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001820 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001821#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001822 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001823#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001824 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001825#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001826 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001827 Py_INCREF(Py_None);
1828 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001829}
1830
Guido van Rossumb6775db1994-08-01 11:34:53 +00001831#endif /* HAVE_SETPGRP */
1832
Guido van Rossumad0ee831995-03-01 10:34:45 +00001833#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001834static char posix_getppid__doc__[] =
1835"getppid() -> ppid\n\
1836Return the parent's process id.";
1837
Barry Warsaw53699e91996-12-10 23:23:01 +00001838static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001839posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001840 PyObject *self;
1841 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001842{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001843 if (!PyArg_ParseTuple(args, ":getppid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001844 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001845 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001846}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001847#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001848
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001849
Fred Drake12c6e2d1999-12-14 21:25:03 +00001850#ifdef HAVE_GETLOGIN
1851static char posix_getlogin__doc__[] = "\
1852getlogin() -> string\n\
1853Return the actual login name.";
1854
1855static PyObject *
1856posix_getlogin(self, args)
1857 PyObject *self;
1858 PyObject *args;
1859{
1860 PyObject *result = NULL;
1861
1862 if (PyArg_ParseTuple(args, ":getlogin")) {
1863 char *name = getlogin();
1864
1865 if (name == NULL)
1866 posix_error();
1867 else
1868 result = PyString_FromString(name);
1869 }
1870 return result;
1871}
1872#endif
1873
Guido van Rossumad0ee831995-03-01 10:34:45 +00001874#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001875static char posix_getuid__doc__[] =
1876"getuid() -> uid\n\
1877Return the current process's user id.";
1878
Barry Warsaw53699e91996-12-10 23:23:01 +00001879static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001880posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001881 PyObject *self;
1882 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001883{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001884 if (!PyArg_ParseTuple(args, ":getuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001885 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001886 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001887}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001888#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001890
Guido van Rossumad0ee831995-03-01 10:34:45 +00001891#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001892static char posix_kill__doc__[] =
1893"kill(pid, sig) -> None\n\
1894Kill a process with a signal.";
1895
Barry Warsaw53699e91996-12-10 23:23:01 +00001896static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001897posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001898 PyObject *self;
1899 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001900{
1901 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001902 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001903 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001904#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001905 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1906 APIRET rc;
1907 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001908 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001909
1910 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1911 APIRET rc;
1912 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001913 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001914
1915 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001916 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001917#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001918 if (kill(pid, sig) == -1)
1919 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001920#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001921 Py_INCREF(Py_None);
1922 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001923}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001924#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001925
Guido van Rossumc0125471996-06-28 18:55:32 +00001926#ifdef HAVE_PLOCK
1927
1928#ifdef HAVE_SYS_LOCK_H
1929#include <sys/lock.h>
1930#endif
1931
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001932static char posix_plock__doc__[] =
1933"plock(op) -> None\n\
1934Lock program segments into memory.";
1935
Barry Warsaw53699e91996-12-10 23:23:01 +00001936static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001937posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001938 PyObject *self;
1939 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001940{
1941 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001942 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001943 return NULL;
1944 if (plock(op) == -1)
1945 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001946 Py_INCREF(Py_None);
1947 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001948}
1949#endif
1950
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001951
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001952#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001953static char posix_popen__doc__[] =
1954"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1955Open a pipe to/from a command returning a file object.";
1956
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001957#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001958static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001959async_system(const char *command)
1960{
1961 char *p, errormsg[256], args[1024];
1962 RESULTCODES rcodes;
1963 APIRET rc;
1964 char *shell = getenv("COMSPEC");
1965 if (!shell)
1966 shell = "cmd";
1967
1968 strcpy(args, shell);
1969 p = &args[ strlen(args)+1 ];
1970 strcpy(p, "/c ");
1971 strcat(p, command);
1972 p += strlen(p) + 1;
1973 *p = '\0';
1974
1975 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001976 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001977 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001978 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001979 &rcodes, shell);
1980 return rc;
1981}
1982
Guido van Rossumd48f2521997-12-05 22:19:34 +00001983static FILE *
1984popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001985{
1986 HFILE rhan, whan;
1987 FILE *retfd = NULL;
1988 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1989
Guido van Rossumd48f2521997-12-05 22:19:34 +00001990 if (rc != NO_ERROR) {
1991 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001992 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001993 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001994
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001995 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1996 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001997
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001998 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1999 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002000
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002001 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
2002 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002003
2004 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002005 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002006 }
2007
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002008 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
2009 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002010
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002011 close(oldfd); /* And Close Saved STDOUT Handle */
2012 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002013
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002014 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
2015 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002016
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002017 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2018 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002019
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002020 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
2021 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002022
2023 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002024 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002025 }
2026
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002027 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
2028 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002029
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002030 close(oldfd); /* And Close Saved STDIN Handle */
2031 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002032
Guido van Rossumd48f2521997-12-05 22:19:34 +00002033 } else {
2034 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002035 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002036 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002037}
2038
2039static PyObject *
2040posix_popen(self, args)
2041 PyObject *self;
2042 PyObject *args;
2043{
2044 char *name;
2045 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00002046 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002047 FILE *fp;
2048 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002049 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002050 return NULL;
2051 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00002052 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002053 Py_END_ALLOW_THREADS
2054 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002055 return os2_error(err);
2056
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002057 f = PyFile_FromFile(fp, name, mode, fclose);
2058 if (f != NULL)
2059 PyFile_SetBufSize(f, bufsize);
2060 return f;
2061}
2062
2063#else
Barry Warsaw53699e91996-12-10 23:23:01 +00002064static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00002065posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002066 PyObject *self;
2067 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00002068{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002069 char *name;
2070 char *mode = "r";
2071 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00002072 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002073 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002074 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00002075 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002076 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002077 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002078 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00002079 if (fp == NULL)
2080 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002081 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002082 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002083 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002084 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00002085}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002086#endif
2087
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002088#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00002089
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002090
Guido van Rossumb6775db1994-08-01 11:34:53 +00002091#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002092static char posix_setuid__doc__[] =
2093"setuid(uid) -> None\n\
2094Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00002095static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002096posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002097 PyObject *self;
2098 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002099{
2100 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002101 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002102 return NULL;
2103 if (setuid(uid) < 0)
2104 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002105 Py_INCREF(Py_None);
2106 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002107}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002108#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002109
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002110
Guido van Rossumb6775db1994-08-01 11:34:53 +00002111#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002112static char posix_setgid__doc__[] =
2113"setgid(gid) -> None\n\
2114Set the current process's group id.";
2115
Barry Warsaw53699e91996-12-10 23:23:01 +00002116static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002117posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002118 PyObject *self;
2119 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002120{
2121 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002122 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002123 return NULL;
2124 if (setgid(gid) < 0)
2125 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002126 Py_INCREF(Py_None);
2127 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002128}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002129#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002130
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002131
Guido van Rossumb6775db1994-08-01 11:34:53 +00002132#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002133static char posix_waitpid__doc__[] =
2134"waitpid(pid, options) -> (pid, status)\n\
2135Wait for completion of a give child process.";
2136
Barry Warsaw53699e91996-12-10 23:23:01 +00002137static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002138posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002139 PyObject *self;
2140 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002141{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002142 int pid, options;
2143#ifdef UNION_WAIT
2144 union wait status;
2145#define status_i (status.w_status)
2146#else
2147 int status;
2148#define status_i status
2149#endif
2150 status_i = 0;
2151
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002152 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002153 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002154 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002155#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002156 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002157#else
2158 pid = waitpid(pid, &status, options);
2159#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002160 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002161 if (pid == -1)
2162 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002163 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002164 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002165}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002166#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002167
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002168
Guido van Rossumad0ee831995-03-01 10:34:45 +00002169#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002170static char posix_wait__doc__[] =
2171"wait() -> (pid, status)\n\
2172Wait for completion of a child process.";
2173
Barry Warsaw53699e91996-12-10 23:23:01 +00002174static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002175posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002176 PyObject *self;
2177 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002178{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002179 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002180#ifdef UNION_WAIT
2181 union wait status;
2182#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002183#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002184 int status;
2185#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002186#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002187 if (!PyArg_ParseTuple(args, ":wait"))
2188 return NULL;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002189 status_i = 0;
2190 Py_BEGIN_ALLOW_THREADS
2191 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002192 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002193 if (pid == -1)
2194 return posix_error();
2195 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002196 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002197#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00002198}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002199#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002200
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002201
2202static char posix_lstat__doc__[] =
2203"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2204Like stat(path), but do not follow symbolic links.";
2205
Barry Warsaw53699e91996-12-10 23:23:01 +00002206static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002207posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002208 PyObject *self;
2209 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002211#ifdef HAVE_LSTAT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002212 return posix_do_stat(self, args, "s:lstat", lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002213#else /* !HAVE_LSTAT */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002214 return posix_do_stat(self, args, "s:lstat", stat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002215#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002216}
2217
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002218
Guido van Rossumb6775db1994-08-01 11:34:53 +00002219#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002220static char posix_readlink__doc__[] =
2221"readlink(path) -> path\n\
2222Return a string representing the path to which the symbolic link points.";
2223
Barry Warsaw53699e91996-12-10 23:23:01 +00002224static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002225posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002226 PyObject *self;
2227 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002228{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002229 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002230 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002231 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002232 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002233 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002234 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002235 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002236 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002237 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002238 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002239 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002240}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002241#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002242
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002243
Guido van Rossumb6775db1994-08-01 11:34:53 +00002244#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002245static char posix_symlink__doc__[] =
2246"symlink(src, dst) -> None\n\
2247Create a symbolic link.";
2248
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002249static PyObject *
2250posix_symlink(self, args)
2251 PyObject *self;
2252 PyObject *args;
2253{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002254 return posix_2str(args, "ss:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002255}
2256#endif /* HAVE_SYMLINK */
2257
2258
2259#ifdef HAVE_TIMES
2260#ifndef HZ
2261#define HZ 60 /* Universal constant :-) */
2262#endif /* HZ */
2263
Guido van Rossumd48f2521997-12-05 22:19:34 +00002264#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2265static long
2266system_uptime()
2267{
2268 ULONG value = 0;
2269
2270 Py_BEGIN_ALLOW_THREADS
2271 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2272 Py_END_ALLOW_THREADS
2273
2274 return value;
2275}
2276
2277static PyObject *
2278posix_times(self, args)
2279 PyObject *self;
2280 PyObject *args;
2281{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002282 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossumd48f2521997-12-05 22:19:34 +00002283 return NULL;
2284
2285 /* Currently Only Uptime is Provided -- Others Later */
2286 return Py_BuildValue("ddddd",
2287 (double)0 /* t.tms_utime / HZ */,
2288 (double)0 /* t.tms_stime / HZ */,
2289 (double)0 /* t.tms_cutime / HZ */,
2290 (double)0 /* t.tms_cstime / HZ */,
2291 (double)system_uptime() / 1000);
2292}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002293#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002294static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002295posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002296 PyObject *self;
2297 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002298{
2299 struct tms t;
2300 clock_t c;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002301 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002302 return NULL;
2303 errno = 0;
2304 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002305 if (c == (clock_t) -1)
2306 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002307 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002308 (double)t.tms_utime / HZ,
2309 (double)t.tms_stime / HZ,
2310 (double)t.tms_cutime / HZ,
2311 (double)t.tms_cstime / HZ,
2312 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002313}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002314#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002315#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002316
2317
Guido van Rossum87755a21996-09-07 00:59:43 +00002318#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002319#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002320static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002321posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002322 PyObject *self;
2323 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002324{
2325 FILETIME create, exit, kernel, user;
2326 HANDLE hProc;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002327 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002328 return NULL;
2329 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002330 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
2331 /* The fields of a FILETIME structure are the hi and lo part
2332 of a 64-bit value expressed in 100 nanosecond units.
2333 1e7 is one second in such units; 1e-7 the inverse.
2334 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
2335 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002336 return Py_BuildValue(
2337 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002338 (double)(kernel.dwHighDateTime*429.4967296 +
2339 kernel.dwLowDateTime*1e-7),
2340 (double)(user.dwHighDateTime*429.4967296 +
2341 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00002342 (double)0,
2343 (double)0,
2344 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002345}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002346#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002347
2348#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002349static char posix_times__doc__[] =
2350"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2351Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002352#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002353
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002354
Guido van Rossumb6775db1994-08-01 11:34:53 +00002355#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002356static char posix_setsid__doc__[] =
2357"setsid() -> None\n\
2358Call the system call setsid().";
2359
Barry Warsaw53699e91996-12-10 23:23:01 +00002360static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002361posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002362 PyObject *self;
2363 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002364{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002365 if (!PyArg_ParseTuple(args, ":setsid"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002366 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002367 if (setsid() < 0)
2368 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002369 Py_INCREF(Py_None);
2370 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002371}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002372#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002373
Guido van Rossumb6775db1994-08-01 11:34:53 +00002374#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002375static char posix_setpgid__doc__[] =
2376"setpgid(pid, pgrp) -> None\n\
2377Call the system call setpgid().";
2378
Barry Warsaw53699e91996-12-10 23:23:01 +00002379static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002380posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002381 PyObject *self;
2382 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002383{
2384 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002385 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002386 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002387 if (setpgid(pid, pgrp) < 0)
2388 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002389 Py_INCREF(Py_None);
2390 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002391}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002392#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002394
Guido van Rossumb6775db1994-08-01 11:34:53 +00002395#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002396static char posix_tcgetpgrp__doc__[] =
2397"tcgetpgrp(fd) -> pgid\n\
2398Return the process group associated with the terminal given by a fd.";
2399
Barry Warsaw53699e91996-12-10 23:23:01 +00002400static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002401posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002402 PyObject *self;
2403 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002404{
2405 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002406 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002407 return NULL;
2408 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002409 if (pgid < 0)
2410 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002411 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002412}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002413#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002414
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002415
Guido van Rossumb6775db1994-08-01 11:34:53 +00002416#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002417static char posix_tcsetpgrp__doc__[] =
2418"tcsetpgrp(fd, pgid) -> None\n\
2419Set the process group associated with the terminal given by a fd.";
2420
Barry Warsaw53699e91996-12-10 23:23:01 +00002421static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002422posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002423 PyObject *self;
2424 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002425{
2426 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002427 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002428 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002429 if (tcsetpgrp(fd, pgid) < 0)
2430 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002431 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002432 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002433}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002434#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002435
Guido van Rossum687dd131993-05-17 08:34:16 +00002436/* Functions acting on file descriptors */
2437
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002438static char posix_open__doc__[] =
2439"open(filename, flag [, mode=0777]) -> fd\n\
2440Open a file (for low level IO).";
2441
Barry Warsaw53699e91996-12-10 23:23:01 +00002442static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002443posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002444 PyObject *self;
2445 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002446{
2447 char *file;
2448 int flag;
2449 int mode = 0777;
2450 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002451 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2452 return NULL;
2453
Barry Warsaw53699e91996-12-10 23:23:01 +00002454 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002455 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002456 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002457 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002458 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002459 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002460}
2461
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002462
2463static char posix_close__doc__[] =
2464"close(fd) -> None\n\
2465Close a file descriptor (for low level IO).";
2466
Barry Warsaw53699e91996-12-10 23:23:01 +00002467static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002468posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002469 PyObject *self;
2470 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002471{
2472 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002473 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002474 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002475 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002476 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002477 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002478 if (res < 0)
2479 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002480 Py_INCREF(Py_None);
2481 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002482}
2483
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002484
2485static char posix_dup__doc__[] =
2486"dup(fd) -> fd2\n\
2487Return a duplicate of a file descriptor.";
2488
Barry Warsaw53699e91996-12-10 23:23:01 +00002489static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002490posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002491 PyObject *self;
2492 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002493{
2494 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002495 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002496 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002497 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002498 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002499 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002500 if (fd < 0)
2501 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002502 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002503}
2504
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002505
2506static char posix_dup2__doc__[] =
2507"dup2(fd, fd2) -> None\n\
2508Duplicate file descriptor.";
2509
Barry Warsaw53699e91996-12-10 23:23:01 +00002510static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002511posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002512 PyObject *self;
2513 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002514{
2515 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002516 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002517 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002518 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002519 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002520 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002521 if (res < 0)
2522 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002523 Py_INCREF(Py_None);
2524 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002525}
2526
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002527
2528static char posix_lseek__doc__[] =
2529"lseek(fd, pos, how) -> newpos\n\
2530Set the current position of a file descriptor.";
2531
Barry Warsaw53699e91996-12-10 23:23:01 +00002532static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002533posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002534 PyObject *self;
2535 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002536{
2537 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002538 off_t pos, res;
2539 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002540 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002541 return NULL;
2542#ifdef SEEK_SET
2543 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2544 switch (how) {
2545 case 0: how = SEEK_SET; break;
2546 case 1: how = SEEK_CUR; break;
2547 case 2: how = SEEK_END; break;
2548 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002549#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002550
2551#if !defined(HAVE_LARGEFILE_SUPPORT)
2552 pos = PyInt_AsLong(posobj);
2553#else
2554 pos = PyLong_Check(posobj) ?
2555 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2556#endif
2557 if (PyErr_Occurred())
2558 return NULL;
2559
Barry Warsaw53699e91996-12-10 23:23:01 +00002560 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002561 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002562 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002563 if (res < 0)
2564 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002565
2566#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002567 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002568#else
2569 return PyLong_FromLongLong(res);
2570#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002571}
2572
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002573
2574static char posix_read__doc__[] =
2575"read(fd, buffersize) -> string\n\
2576Read a file descriptor.";
2577
Barry Warsaw53699e91996-12-10 23:23:01 +00002578static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002579posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002580 PyObject *self;
2581 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002582{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002583 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002584 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002585 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002586 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002587 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002588 if (buffer == NULL)
2589 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002590 Py_BEGIN_ALLOW_THREADS
2591 n = read(fd, PyString_AsString(buffer), size);
2592 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002593 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002594 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002595 return posix_error();
2596 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002597 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002598 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002599 return buffer;
2600}
2601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002602
2603static char posix_write__doc__[] =
2604"write(fd, string) -> byteswritten\n\
2605Write a string to a file descriptor.";
2606
Barry Warsaw53699e91996-12-10 23:23:01 +00002607static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002608posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002609 PyObject *self;
2610 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002611{
2612 int fd, size;
2613 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002614 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002615 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002616 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002617 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002618 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002619 if (size < 0)
2620 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002621 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002622}
2623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002624
2625static char posix_fstat__doc__[]=
2626"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2627Like stat(), but for an open file descriptor.";
2628
Barry Warsaw53699e91996-12-10 23:23:01 +00002629static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002630posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002631 PyObject *self;
2632 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002633{
2634 int fd;
2635 struct stat st;
2636 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002637 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002638 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002639 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002640 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002641 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002642 if (res != 0)
2643 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002644#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002645 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002646 (long)st.st_mode,
2647 (long)st.st_ino,
2648 (long)st.st_dev,
2649 (long)st.st_nlink,
2650 (long)st.st_uid,
2651 (long)st.st_gid,
2652 (long)st.st_size,
2653 (long)st.st_atime,
2654 (long)st.st_mtime,
2655 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002656#else
2657 return Py_BuildValue("(lLllllLlll)",
2658 (long)st.st_mode,
2659 (LONG_LONG)st.st_ino,
2660 (long)st.st_dev,
2661 (long)st.st_nlink,
2662 (long)st.st_uid,
2663 (long)st.st_gid,
2664 (LONG_LONG)st.st_size,
2665 (long)st.st_atime,
2666 (long)st.st_mtime,
2667 (long)st.st_ctime);
2668#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002669}
2670
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002671
2672static char posix_fdopen__doc__[] =
2673"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2674Return an open file object connected to a file descriptor.";
2675
Barry Warsaw53699e91996-12-10 23:23:01 +00002676static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002677posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002678 PyObject *self;
2679 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002680{
Barry Warsaw53699e91996-12-10 23:23:01 +00002681 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002682 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002683 char *mode = "r";
2684 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002685 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002686 PyObject *f;
2687 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002688 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002689
Barry Warsaw53699e91996-12-10 23:23:01 +00002690 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002691 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002692 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002693 if (fp == NULL)
2694 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002695 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002696 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002697 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002698 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002699}
2700
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002701
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002702#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002703static char posix_pipe__doc__[] =
2704"pipe() -> (read_end, write_end)\n\
2705Create a pipe.";
2706
Barry Warsaw53699e91996-12-10 23:23:01 +00002707static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002708posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002709 PyObject *self;
2710 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002711{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002712#if defined(PYOS_OS2)
2713 HFILE read, write;
2714 APIRET rc;
2715
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002716 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002717 return NULL;
2718
2719 Py_BEGIN_ALLOW_THREADS
2720 rc = DosCreatePipe( &read, &write, 4096);
2721 Py_END_ALLOW_THREADS
2722 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002723 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002724
2725 return Py_BuildValue("(ii)", read, write);
2726#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002727#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002728 int fds[2];
2729 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002730 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum687dd131993-05-17 08:34:16 +00002731 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002732 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002733 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002734 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002735 if (res != 0)
2736 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002737 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002738#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002739 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002740 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002741 BOOL ok;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002742 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum794d8131994-08-23 13:48:48 +00002743 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002744 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002745 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002746 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002747 if (!ok)
2748 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002749 read_fd = _open_osfhandle((long)read, 0);
2750 write_fd = _open_osfhandle((long)write, 1);
2751 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002752#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002753#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002754}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002755#endif /* HAVE_PIPE */
2756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002757
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002758#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002759static char posix_mkfifo__doc__[] =
2760"mkfifo(file, [, mode=0666]) -> None\n\
2761Create a FIFO (a POSIX named pipe).";
2762
Barry Warsaw53699e91996-12-10 23:23:01 +00002763static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002764posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002765 PyObject *self;
2766 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002767{
2768 char *file;
2769 int mode = 0666;
2770 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002771 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002772 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002773 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002774 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002775 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002776 if (res < 0)
2777 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002778 Py_INCREF(Py_None);
2779 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002780}
2781#endif
2782
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002783
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002784#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002785static char posix_ftruncate__doc__[] =
2786"ftruncate(fd, length) -> None\n\
2787Truncate a file to a specified length.";
2788
Barry Warsaw53699e91996-12-10 23:23:01 +00002789static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002790posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002791 PyObject *self; /* Not used */
2792 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002793{
2794 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002795 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002796 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002797 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002798
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002799 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00002800 return NULL;
2801
2802#if !defined(HAVE_LARGEFILE_SUPPORT)
2803 length = PyInt_AsLong(lenobj);
2804#else
2805 length = PyLong_Check(lenobj) ?
2806 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2807#endif
2808 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002809 return NULL;
2810
Barry Warsaw53699e91996-12-10 23:23:01 +00002811 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002812 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002813 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002814 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002815 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002816 return NULL;
2817 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002818 Py_INCREF(Py_None);
2819 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002820}
2821#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002822
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002823#ifdef NeXT
2824#define HAVE_PUTENV
2825/* Steve Spicklemire got this putenv from NeXTAnswers */
2826static int
2827putenv(char *newval)
2828{
2829 extern char **environ;
2830
2831 static int firstTime = 1;
2832 char **ep;
2833 char *cp;
2834 int esiz;
2835 char *np;
2836
2837 if (!(np = strchr(newval, '=')))
2838 return 1;
2839 *np = '\0';
2840
2841 /* look it up */
2842 for (ep=environ ; *ep ; ep++)
2843 {
2844 /* this should always be true... */
2845 if (cp = strchr(*ep, '='))
2846 {
2847 *cp = '\0';
2848 if (!strcmp(*ep, newval))
2849 {
2850 /* got it! */
2851 *cp = '=';
2852 break;
2853 }
2854 *cp = '=';
2855 }
2856 else
2857 {
2858 *np = '=';
2859 return 1;
2860 }
2861 }
2862
2863 *np = '=';
2864 if (*ep)
2865 {
2866 /* the string was already there:
2867 just replace it with the new one */
2868 *ep = newval;
2869 return 0;
2870 }
2871
2872 /* expand environ by one */
2873 for (esiz=2, ep=environ ; *ep ; ep++)
2874 esiz++;
2875 if (firstTime)
2876 {
2877 char **epp;
2878 char **newenv;
2879 if (!(newenv = malloc(esiz * sizeof(char *))))
2880 return 1;
2881
2882 for (ep=environ, epp=newenv ; *ep ;)
2883 *epp++ = *ep++;
2884 *epp++ = newval;
2885 *epp = (char *) 0;
2886 environ = newenv;
2887 }
2888 else
2889 {
2890 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2891 return 1;
2892 environ[esiz - 2] = newval;
2893 environ[esiz - 1] = (char *) 0;
2894 firstTime = 0;
2895 }
2896
2897 return 0;
2898}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002899#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002900
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002901
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002902#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002903static char posix_putenv__doc__[] =
2904"putenv(key, value) -> None\n\
2905Change or add an environment variable.";
2906
Guido van Rossumbcc20741998-08-04 22:53:56 +00002907#ifdef __BEOS__
2908/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2909int putenv( const char *str );
2910#endif
2911
Fred Drake762e2061999-08-26 17:23:54 +00002912/* Save putenv() parameters as values here, so we can collect them when they
2913 * get re-set with another call for the same key. */
2914static PyObject *posix_putenv_garbage;
2915
Barry Warsaw53699e91996-12-10 23:23:01 +00002916static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002917posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002918 PyObject *self;
2919 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002920{
2921 char *s1, *s2;
2922 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00002923 PyObject *newstr;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002924
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002925 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002926 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002927
2928#if defined(PYOS_OS2)
2929 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2930 APIRET rc;
2931
2932 if (strlen(s2) == 0) /* If New Value is an Empty String */
2933 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2934
2935 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2936 if (rc != NO_ERROR)
2937 return os2_error(rc);
2938
2939 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2940 APIRET rc;
2941
2942 if (strlen(s2) == 0) /* If New Value is an Empty String */
2943 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2944
2945 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2946 if (rc != NO_ERROR)
2947 return os2_error(rc);
2948 } else {
2949#endif
2950
Fred Drake762e2061999-08-26 17:23:54 +00002951 /* XXX This can leak memory -- not easy to fix :-( */
2952 newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2);
2953 if (newstr == NULL)
2954 return PyErr_NoMemory();
2955 new = PyString_AS_STRING(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002956 (void) sprintf(new, "%s=%s", s1, s2);
2957 if (putenv(new)) {
2958 posix_error();
2959 return NULL;
2960 }
Fred Drake762e2061999-08-26 17:23:54 +00002961 /* Install the first arg and newstr in posix_putenv_garbage;
2962 * this will cause previous value to be collected. This has to
2963 * happen after the real putenv() call because the old value
2964 * was still accessible until then. */
2965 if (PyDict_SetItem(posix_putenv_garbage,
2966 PyTuple_GET_ITEM(args, 0), newstr)) {
2967 /* really not much we can do; just leak */
2968 PyErr_Clear();
2969 }
2970 else {
2971 Py_DECREF(newstr);
2972 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002973
2974#if defined(PYOS_OS2)
2975 }
2976#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002977 Py_INCREF(Py_None);
2978 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002979}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002980#endif /* putenv */
2981
2982#ifdef HAVE_STRERROR
2983static char posix_strerror__doc__[] =
2984"strerror(code) -> string\n\
2985Translate an error code to a message string.";
2986
2987PyObject *
2988posix_strerror(self, args)
2989 PyObject *self;
2990 PyObject *args;
2991{
2992 int code;
2993 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002994 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00002995 return NULL;
2996 message = strerror(code);
2997 if (message == NULL) {
2998 PyErr_SetString(PyExc_ValueError,
2999 "strerror code out of range");
3000 return NULL;
3001 }
3002 return PyString_FromString(message);
3003}
3004#endif /* strerror */
3005
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003006
Guido van Rossumc9641791998-08-04 15:26:23 +00003007#ifdef HAVE_SYS_WAIT_H
3008
3009#ifdef WIFSTOPPED
3010static char posix_WIFSTOPPED__doc__[] =
3011"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003012Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003013
3014static PyObject *
3015posix_WIFSTOPPED(self, args)
3016 PyObject *self;
3017 PyObject *args;
3018{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003019#ifdef UNION_WAIT
3020 union wait status;
3021#define status_i (status.w_status)
3022#else
3023 int status;
3024#define status_i status
3025#endif
3026 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003027
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003028 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003029 {
3030 return NULL;
3031 }
3032
3033 return Py_BuildValue("i", WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003034#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003035}
3036#endif /* WIFSTOPPED */
3037
3038#ifdef WIFSIGNALED
3039static char posix_WIFSIGNALED__doc__[] =
3040"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00003041Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003042
3043static PyObject *
3044posix_WIFSIGNALED(self, args)
3045 PyObject *self;
3046 PyObject *args;
3047{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003048#ifdef UNION_WAIT
3049 union wait status;
3050#define status_i (status.w_status)
3051#else
3052 int status;
3053#define status_i status
3054#endif
3055 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003056
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003057 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003058 {
3059 return NULL;
3060 }
3061
3062 return Py_BuildValue("i", WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003063#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003064}
3065#endif /* WIFSIGNALED */
3066
3067#ifdef WIFEXITED
3068static char posix_WIFEXITED__doc__[] =
3069"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003070Return true if the process returning 'status' exited using the exit()\n\
3071system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003072
3073static PyObject *
3074posix_WIFEXITED(self, args)
3075 PyObject *self;
3076 PyObject *args;
3077{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003078#ifdef UNION_WAIT
3079 union wait status;
3080#define status_i (status.w_status)
3081#else
3082 int status;
3083#define status_i status
3084#endif
3085 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003086
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003087 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003088 {
3089 return NULL;
3090 }
3091
3092 return Py_BuildValue("i", WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003093#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003094}
3095#endif /* WIFEXITED */
3096
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003097#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00003098static char posix_WEXITSTATUS__doc__[] =
3099"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003100Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003101
3102static PyObject *
3103posix_WEXITSTATUS(self, args)
3104 PyObject *self;
3105 PyObject *args;
3106{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003107#ifdef UNION_WAIT
3108 union wait status;
3109#define status_i (status.w_status)
3110#else
3111 int status;
3112#define status_i status
3113#endif
3114 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003115
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003116 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003117 {
3118 return NULL;
3119 }
3120
3121 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003122#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003123}
3124#endif /* WEXITSTATUS */
3125
3126#ifdef WTERMSIG
3127static char posix_WTERMSIG__doc__[] =
3128"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003129Return the signal that terminated the process that provided the 'status'\n\
3130value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003131
3132static PyObject *
3133posix_WTERMSIG(self, args)
3134 PyObject *self;
3135 PyObject *args;
3136{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003137#ifdef UNION_WAIT
3138 union wait status;
3139#define status_i (status.w_status)
3140#else
3141 int status;
3142#define status_i status
3143#endif
3144 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003145
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003146 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003147 {
3148 return NULL;
3149 }
3150
3151 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003152#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003153}
3154#endif /* WTERMSIG */
3155
3156#ifdef WSTOPSIG
3157static char posix_WSTOPSIG__doc__[] =
3158"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003159Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003160
3161static PyObject *
3162posix_WSTOPSIG(self, args)
3163 PyObject *self;
3164 PyObject *args;
3165{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003166#ifdef UNION_WAIT
3167 union wait status;
3168#define status_i (status.w_status)
3169#else
3170 int status;
3171#define status_i status
3172#endif
3173 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003174
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003175 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003176 {
3177 return NULL;
3178 }
3179
3180 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003181#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003182}
3183#endif /* WSTOPSIG */
3184
3185#endif /* HAVE_SYS_WAIT_H */
3186
3187
Guido van Rossum94f6f721999-01-06 18:42:14 +00003188#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00003189#ifdef _SCO_DS
3190/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
3191 needed definitions in sys/statvfs.h */
3192#define _SVID3
3193#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003194#include <sys/statvfs.h>
3195
3196static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003197"fstatvfs(fd) -> \n\
3198 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003199Perform an fstatvfs system call on the given fd.";
3200
3201static PyObject *
3202posix_fstatvfs(self, args)
3203 PyObject *self;
3204 PyObject *args;
3205{
3206 int fd, res;
3207 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003208 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003209 return NULL;
3210 Py_BEGIN_ALLOW_THREADS
3211 res = fstatvfs(fd, &st);
3212 Py_END_ALLOW_THREADS
3213 if (res != 0)
3214 return posix_error();
3215#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003216 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003217 (long) st.f_bsize,
3218 (long) st.f_frsize,
3219 (long) st.f_blocks,
3220 (long) st.f_bfree,
3221 (long) st.f_bavail,
3222 (long) st.f_files,
3223 (long) st.f_ffree,
3224 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003225 (long) st.f_flag,
3226 (long) st.f_namemax);
3227#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003228 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003229 (long) st.f_bsize,
3230 (long) st.f_frsize,
3231 (LONG_LONG) st.f_blocks,
3232 (LONG_LONG) st.f_bfree,
3233 (LONG_LONG) st.f_bavail,
3234 (LONG_LONG) st.f_files,
3235 (LONG_LONG) st.f_ffree,
3236 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003237 (long) st.f_flag,
3238 (long) st.f_namemax);
3239#endif
3240}
3241#endif /* HAVE_FSTATVFS */
3242
3243
3244#if defined(HAVE_STATVFS)
3245#include <sys/statvfs.h>
3246
3247static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003248"statvfs(path) -> \n\
3249 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003250Perform a statvfs system call on the given path.";
3251
3252static PyObject *
3253posix_statvfs(self, args)
3254 PyObject *self;
3255 PyObject *args;
3256{
3257 char *path;
3258 int res;
3259 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003260 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003261 return NULL;
3262 Py_BEGIN_ALLOW_THREADS
3263 res = statvfs(path, &st);
3264 Py_END_ALLOW_THREADS
3265 if (res != 0)
3266 return posix_error_with_filename(path);
3267#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003268 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003269 (long) st.f_bsize,
3270 (long) st.f_frsize,
3271 (long) st.f_blocks,
3272 (long) st.f_bfree,
3273 (long) st.f_bavail,
3274 (long) st.f_files,
3275 (long) st.f_ffree,
3276 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003277 (long) st.f_flag,
3278 (long) st.f_namemax);
3279#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003280 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003281 (long) st.f_bsize,
3282 (long) st.f_frsize,
3283 (LONG_LONG) st.f_blocks,
3284 (LONG_LONG) st.f_bfree,
3285 (LONG_LONG) st.f_bavail,
3286 (LONG_LONG) st.f_files,
3287 (LONG_LONG) st.f_ffree,
3288 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003289 (long) st.f_flag,
3290 (long) st.f_namemax);
3291#endif
3292}
3293#endif /* HAVE_STATVFS */
3294
3295
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003296#ifdef HAVE_TEMPNAM
3297static char posix_tempnam__doc__[] = "\
3298tempnam([dir[, prefix]]) -> string\n\
3299Return a unique name for a temporary file.\n\
3300The directory and a short may be specified as strings; they may be omitted\n\
3301or None if not needed.";
3302
3303static PyObject *
3304posix_tempnam(self, args)
3305 PyObject *self;
3306 PyObject *args;
3307{
3308 PyObject *result = NULL;
3309 char *dir = NULL;
3310 char *pfx = NULL;
3311 char *name;
3312
3313 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
3314 return NULL;
3315 name = tempnam(dir, pfx);
3316 if (name == NULL)
3317 return PyErr_NoMemory();
3318 result = PyString_FromString(name);
3319 free(name);
3320 return result;
3321}
Guido van Rossumd371ff11999-01-25 16:12:23 +00003322#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003323
3324
3325#ifdef HAVE_TMPFILE
3326static char posix_tmpfile__doc__[] = "\
3327tmpfile() -> file object\n\
3328Create a temporary file with no directory entries.";
3329
3330static PyObject *
3331posix_tmpfile(self, args)
3332 PyObject *self;
3333 PyObject *args;
3334{
3335 FILE *fp;
3336
3337 if (!PyArg_ParseTuple(args, ":tmpfile"))
3338 return NULL;
3339 fp = tmpfile();
3340 if (fp == NULL)
3341 return posix_error();
3342 return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose);
3343}
3344#endif
3345
3346
3347#ifdef HAVE_TMPNAM
3348static char posix_tmpnam__doc__[] = "\
3349tmpnam() -> string\n\
3350Return a unique name for a temporary file.";
3351
3352static PyObject *
3353posix_tmpnam(self, args)
3354 PyObject *self;
3355 PyObject *args;
3356{
3357 char buffer[L_tmpnam];
3358 char *name;
3359
3360 if (!PyArg_ParseTuple(args, ":tmpnam"))
3361 return NULL;
Greg Wardb48bc172000-03-01 21:51:56 +00003362#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003363 name = tmpnam_r(buffer);
3364#else
3365 name = tmpnam(buffer);
3366#endif
3367 if (name == NULL) {
3368 PyErr_SetObject(PyExc_OSError,
3369 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00003370#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003371 "unexpected NULL from tmpnam_r"
3372#else
3373 "unexpected NULL from tmpnam"
3374#endif
3375 ));
3376 return NULL;
3377 }
3378 return PyString_FromString(buffer);
3379}
3380#endif
3381
3382
Fred Drakec9680921999-12-13 16:37:25 +00003383/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
3384 * It maps strings representing configuration variable names to
3385 * integer values, allowing those functions to be called with the
3386 * magic names instead of poluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00003387 * rarely-used constants. There are three separate tables that use
3388 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00003389 *
3390 * This code is always included, even if none of the interfaces that
3391 * need it are included. The #if hackery needed to avoid it would be
3392 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00003393 */
3394struct constdef {
3395 char *name;
3396 long value;
3397};
3398
Fred Drake12c6e2d1999-12-14 21:25:03 +00003399static int
3400conv_confname(arg, valuep, table, tablesize)
3401 PyObject *arg;
3402 int *valuep;
3403 struct constdef *table;
3404 size_t tablesize;
3405{
3406 if (PyInt_Check(arg)) {
3407 *valuep = PyInt_AS_LONG(arg);
3408 return 1;
3409 }
3410 if (PyString_Check(arg)) {
3411 /* look up the value in the table using a binary search */
3412 int lo = 0;
3413 int hi = tablesize;
3414 int cmp, mid;
3415 char *confname = PyString_AS_STRING(arg);
3416 while (lo < hi) {
3417 mid = (lo + hi) / 2;
3418 cmp = strcmp(confname, table[mid].name);
3419 if (cmp < 0)
3420 hi = mid;
3421 else if (cmp > 0)
3422 lo = mid + 1;
3423 else {
3424 *valuep = table[mid].value;
3425 return 1;
3426 }
3427 }
3428 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
3429 }
3430 else
3431 PyErr_SetString(PyExc_TypeError,
3432 "configuration names must be strings or integers");
3433 return 0;
3434}
3435
3436
3437#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
3438static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00003439#ifdef _PC_ABI_AIO_XFER_MAX
3440 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
3441#endif
3442#ifdef _PC_ABI_ASYNC_IO
3443 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
3444#endif
Fred Drakec9680921999-12-13 16:37:25 +00003445#ifdef _PC_ASYNC_IO
3446 {"PC_ASYNC_IO", _PC_ASYNC_IO},
3447#endif
3448#ifdef _PC_CHOWN_RESTRICTED
3449 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
3450#endif
3451#ifdef _PC_FILESIZEBITS
3452 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
3453#endif
3454#ifdef _PC_LAST
3455 {"PC_LAST", _PC_LAST},
3456#endif
3457#ifdef _PC_LINK_MAX
3458 {"PC_LINK_MAX", _PC_LINK_MAX},
3459#endif
3460#ifdef _PC_MAX_CANON
3461 {"PC_MAX_CANON", _PC_MAX_CANON},
3462#endif
3463#ifdef _PC_MAX_INPUT
3464 {"PC_MAX_INPUT", _PC_MAX_INPUT},
3465#endif
3466#ifdef _PC_NAME_MAX
3467 {"PC_NAME_MAX", _PC_NAME_MAX},
3468#endif
3469#ifdef _PC_NO_TRUNC
3470 {"PC_NO_TRUNC", _PC_NO_TRUNC},
3471#endif
3472#ifdef _PC_PATH_MAX
3473 {"PC_PATH_MAX", _PC_PATH_MAX},
3474#endif
3475#ifdef _PC_PIPE_BUF
3476 {"PC_PIPE_BUF", _PC_PIPE_BUF},
3477#endif
3478#ifdef _PC_PRIO_IO
3479 {"PC_PRIO_IO", _PC_PRIO_IO},
3480#endif
3481#ifdef _PC_SOCK_MAXBUF
3482 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
3483#endif
3484#ifdef _PC_SYNC_IO
3485 {"PC_SYNC_IO", _PC_SYNC_IO},
3486#endif
3487#ifdef _PC_VDISABLE
3488 {"PC_VDISABLE", _PC_VDISABLE},
3489#endif
3490};
3491
Fred Drakec9680921999-12-13 16:37:25 +00003492static int
3493conv_path_confname(arg, valuep)
3494 PyObject *arg;
3495 int *valuep;
3496{
3497 return conv_confname(arg, valuep, posix_constants_pathconf,
3498 sizeof(posix_constants_pathconf)
3499 / sizeof(struct constdef));
3500}
3501#endif
3502
3503#ifdef HAVE_FPATHCONF
3504static char posix_fpathconf__doc__[] = "\
3505fpathconf(fd, name) -> integer\n\
3506Return the configuration limit name for the file descriptor fd.\n\
3507If there is no limit, return -1.";
3508
3509static PyObject *
3510posix_fpathconf(self, args)
3511 PyObject *self;
3512 PyObject *args;
3513{
3514 PyObject *result = NULL;
3515 int name, fd;
3516
Fred Drake12c6e2d1999-12-14 21:25:03 +00003517 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
3518 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00003519 long limit;
3520
3521 errno = 0;
3522 limit = fpathconf(fd, name);
3523 if (limit == -1 && errno != 0)
3524 posix_error();
3525 else
3526 result = PyInt_FromLong(limit);
3527 }
3528 return result;
3529}
3530#endif
3531
3532
3533#ifdef HAVE_PATHCONF
3534static char posix_pathconf__doc__[] = "\
3535pathconf(path, name) -> integer\n\
3536Return the configuration limit name for the file or directory path.\n\
3537If there is no limit, return -1.";
3538
3539static PyObject *
3540posix_pathconf(self, args)
3541 PyObject *self;
3542 PyObject *args;
3543{
3544 PyObject *result = NULL;
3545 int name;
3546 char *path;
3547
3548 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
3549 conv_path_confname, &name)) {
3550 long limit;
3551
3552 errno = 0;
3553 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00003554 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00003555 if (errno == EINVAL)
3556 /* could be a path or name problem */
3557 posix_error();
3558 else
3559 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00003560 }
Fred Drakec9680921999-12-13 16:37:25 +00003561 else
3562 result = PyInt_FromLong(limit);
3563 }
3564 return result;
3565}
3566#endif
3567
3568#ifdef HAVE_CONFSTR
3569static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00003570#ifdef _CS_ARCHITECTURE
3571 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
3572#endif
3573#ifdef _CS_HOSTNAME
3574 {"CS_HOSTNAME", _CS_HOSTNAME},
3575#endif
3576#ifdef _CS_HW_PROVIDER
3577 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
3578#endif
3579#ifdef _CS_HW_SERIAL
3580 {"CS_HW_SERIAL", _CS_HW_SERIAL},
3581#endif
3582#ifdef _CS_INITTAB_NAME
3583 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
3584#endif
Fred Drakec9680921999-12-13 16:37:25 +00003585#ifdef _CS_LFS64_CFLAGS
3586 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
3587#endif
3588#ifdef _CS_LFS64_LDFLAGS
3589 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
3590#endif
3591#ifdef _CS_LFS64_LIBS
3592 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
3593#endif
3594#ifdef _CS_LFS64_LINTFLAGS
3595 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
3596#endif
3597#ifdef _CS_LFS_CFLAGS
3598 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
3599#endif
3600#ifdef _CS_LFS_LDFLAGS
3601 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
3602#endif
3603#ifdef _CS_LFS_LIBS
3604 {"CS_LFS_LIBS", _CS_LFS_LIBS},
3605#endif
3606#ifdef _CS_LFS_LINTFLAGS
3607 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
3608#endif
Fred Draked86ed291999-12-15 15:34:33 +00003609#ifdef _CS_MACHINE
3610 {"CS_MACHINE", _CS_MACHINE},
3611#endif
Fred Drakec9680921999-12-13 16:37:25 +00003612#ifdef _CS_PATH
3613 {"CS_PATH", _CS_PATH},
3614#endif
Fred Draked86ed291999-12-15 15:34:33 +00003615#ifdef _CS_RELEASE
3616 {"CS_RELEASE", _CS_RELEASE},
3617#endif
3618#ifdef _CS_SRPC_DOMAIN
3619 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
3620#endif
3621#ifdef _CS_SYSNAME
3622 {"CS_SYSNAME", _CS_SYSNAME},
3623#endif
3624#ifdef _CS_VERSION
3625 {"CS_VERSION", _CS_VERSION},
3626#endif
Fred Drakec9680921999-12-13 16:37:25 +00003627#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
3628 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
3629#endif
3630#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
3631 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
3632#endif
3633#ifdef _CS_XBS5_ILP32_OFF32_LIBS
3634 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
3635#endif
3636#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
3637 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
3638#endif
3639#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
3640 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
3641#endif
3642#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
3643 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
3644#endif
3645#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
3646 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
3647#endif
3648#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
3649 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
3650#endif
3651#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
3652 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
3653#endif
3654#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
3655 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
3656#endif
3657#ifdef _CS_XBS5_LP64_OFF64_LIBS
3658 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
3659#endif
3660#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
3661 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
3662#endif
3663#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
3664 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
3665#endif
3666#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
3667 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
3668#endif
3669#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
3670 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
3671#endif
3672#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
3673 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
3674#endif
Fred Draked86ed291999-12-15 15:34:33 +00003675#ifdef _MIPS_CS_AVAIL_PROCESSORS
3676 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
3677#endif
3678#ifdef _MIPS_CS_BASE
3679 {"MIPS_CS_BASE", _MIPS_CS_BASE},
3680#endif
3681#ifdef _MIPS_CS_HOSTID
3682 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
3683#endif
3684#ifdef _MIPS_CS_HW_NAME
3685 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
3686#endif
3687#ifdef _MIPS_CS_NUM_PROCESSORS
3688 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
3689#endif
3690#ifdef _MIPS_CS_OSREL_MAJ
3691 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
3692#endif
3693#ifdef _MIPS_CS_OSREL_MIN
3694 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
3695#endif
3696#ifdef _MIPS_CS_OSREL_PATCH
3697 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
3698#endif
3699#ifdef _MIPS_CS_OS_NAME
3700 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
3701#endif
3702#ifdef _MIPS_CS_OS_PROVIDER
3703 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
3704#endif
3705#ifdef _MIPS_CS_PROCESSORS
3706 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
3707#endif
3708#ifdef _MIPS_CS_SERIAL
3709 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
3710#endif
3711#ifdef _MIPS_CS_VENDOR
3712 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
3713#endif
Fred Drakec9680921999-12-13 16:37:25 +00003714};
3715
3716static int
3717conv_confstr_confname(arg, valuep)
3718 PyObject *arg;
3719 int *valuep;
3720{
3721 return conv_confname(arg, valuep, posix_constants_confstr,
3722 sizeof(posix_constants_confstr)
3723 / sizeof(struct constdef));
3724}
3725
3726static char posix_confstr__doc__[] = "\
3727confstr(name) -> string\n\
3728Return a string-valued system configuration variable.";
3729
3730static PyObject *
3731posix_confstr(self, args)
3732 PyObject *self;
3733 PyObject *args;
3734{
3735 PyObject *result = NULL;
3736 int name;
3737 char buffer[64];
3738
3739 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
3740 int len = confstr(name, buffer, sizeof(buffer));
3741
Fred Drakec9680921999-12-13 16:37:25 +00003742 errno = 0;
3743 if (len == 0) {
3744 if (errno != 0)
3745 posix_error();
3746 else
3747 result = PyString_FromString("");
3748 }
3749 else {
3750 if (len >= sizeof(buffer)) {
3751 result = PyString_FromStringAndSize(NULL, len);
3752 if (result != NULL)
3753 confstr(name, PyString_AS_STRING(result), len+1);
3754 }
3755 else
3756 result = PyString_FromString(buffer);
3757 }
3758 }
3759 return result;
3760}
3761#endif
3762
3763
3764#ifdef HAVE_SYSCONF
3765static struct constdef posix_constants_sysconf[] = {
3766#ifdef _SC_2_CHAR_TERM
3767 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
3768#endif
3769#ifdef _SC_2_C_BIND
3770 {"SC_2_C_BIND", _SC_2_C_BIND},
3771#endif
3772#ifdef _SC_2_C_DEV
3773 {"SC_2_C_DEV", _SC_2_C_DEV},
3774#endif
3775#ifdef _SC_2_C_VERSION
3776 {"SC_2_C_VERSION", _SC_2_C_VERSION},
3777#endif
3778#ifdef _SC_2_FORT_DEV
3779 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
3780#endif
3781#ifdef _SC_2_FORT_RUN
3782 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
3783#endif
3784#ifdef _SC_2_LOCALEDEF
3785 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
3786#endif
3787#ifdef _SC_2_SW_DEV
3788 {"SC_2_SW_DEV", _SC_2_SW_DEV},
3789#endif
3790#ifdef _SC_2_UPE
3791 {"SC_2_UPE", _SC_2_UPE},
3792#endif
3793#ifdef _SC_2_VERSION
3794 {"SC_2_VERSION", _SC_2_VERSION},
3795#endif
Fred Draked86ed291999-12-15 15:34:33 +00003796#ifdef _SC_ABI_ASYNCHRONOUS_IO
3797 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
3798#endif
3799#ifdef _SC_ACL
3800 {"SC_ACL", _SC_ACL},
3801#endif
Fred Drakec9680921999-12-13 16:37:25 +00003802#ifdef _SC_AIO_LISTIO_MAX
3803 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
3804#endif
Fred Drakec9680921999-12-13 16:37:25 +00003805#ifdef _SC_AIO_MAX
3806 {"SC_AIO_MAX", _SC_AIO_MAX},
3807#endif
3808#ifdef _SC_AIO_PRIO_DELTA_MAX
3809 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
3810#endif
3811#ifdef _SC_ARG_MAX
3812 {"SC_ARG_MAX", _SC_ARG_MAX},
3813#endif
3814#ifdef _SC_ASYNCHRONOUS_IO
3815 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
3816#endif
3817#ifdef _SC_ATEXIT_MAX
3818 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
3819#endif
Fred Draked86ed291999-12-15 15:34:33 +00003820#ifdef _SC_AUDIT
3821 {"SC_AUDIT", _SC_AUDIT},
3822#endif
Fred Drakec9680921999-12-13 16:37:25 +00003823#ifdef _SC_AVPHYS_PAGES
3824 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
3825#endif
3826#ifdef _SC_BC_BASE_MAX
3827 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
3828#endif
3829#ifdef _SC_BC_DIM_MAX
3830 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
3831#endif
3832#ifdef _SC_BC_SCALE_MAX
3833 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
3834#endif
3835#ifdef _SC_BC_STRING_MAX
3836 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
3837#endif
Fred Draked86ed291999-12-15 15:34:33 +00003838#ifdef _SC_CAP
3839 {"SC_CAP", _SC_CAP},
3840#endif
Fred Drakec9680921999-12-13 16:37:25 +00003841#ifdef _SC_CHARCLASS_NAME_MAX
3842 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
3843#endif
3844#ifdef _SC_CHAR_BIT
3845 {"SC_CHAR_BIT", _SC_CHAR_BIT},
3846#endif
3847#ifdef _SC_CHAR_MAX
3848 {"SC_CHAR_MAX", _SC_CHAR_MAX},
3849#endif
3850#ifdef _SC_CHAR_MIN
3851 {"SC_CHAR_MIN", _SC_CHAR_MIN},
3852#endif
3853#ifdef _SC_CHILD_MAX
3854 {"SC_CHILD_MAX", _SC_CHILD_MAX},
3855#endif
3856#ifdef _SC_CLK_TCK
3857 {"SC_CLK_TCK", _SC_CLK_TCK},
3858#endif
3859#ifdef _SC_COHER_BLKSZ
3860 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
3861#endif
3862#ifdef _SC_COLL_WEIGHTS_MAX
3863 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
3864#endif
3865#ifdef _SC_DCACHE_ASSOC
3866 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
3867#endif
3868#ifdef _SC_DCACHE_BLKSZ
3869 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
3870#endif
3871#ifdef _SC_DCACHE_LINESZ
3872 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
3873#endif
3874#ifdef _SC_DCACHE_SZ
3875 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
3876#endif
3877#ifdef _SC_DCACHE_TBLKSZ
3878 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
3879#endif
3880#ifdef _SC_DELAYTIMER_MAX
3881 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
3882#endif
3883#ifdef _SC_EQUIV_CLASS_MAX
3884 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
3885#endif
3886#ifdef _SC_EXPR_NEST_MAX
3887 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
3888#endif
3889#ifdef _SC_FSYNC
3890 {"SC_FSYNC", _SC_FSYNC},
3891#endif
3892#ifdef _SC_GETGR_R_SIZE_MAX
3893 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
3894#endif
3895#ifdef _SC_GETPW_R_SIZE_MAX
3896 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
3897#endif
3898#ifdef _SC_ICACHE_ASSOC
3899 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
3900#endif
3901#ifdef _SC_ICACHE_BLKSZ
3902 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
3903#endif
3904#ifdef _SC_ICACHE_LINESZ
3905 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
3906#endif
3907#ifdef _SC_ICACHE_SZ
3908 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
3909#endif
Fred Draked86ed291999-12-15 15:34:33 +00003910#ifdef _SC_INF
3911 {"SC_INF", _SC_INF},
3912#endif
Fred Drakec9680921999-12-13 16:37:25 +00003913#ifdef _SC_INT_MAX
3914 {"SC_INT_MAX", _SC_INT_MAX},
3915#endif
3916#ifdef _SC_INT_MIN
3917 {"SC_INT_MIN", _SC_INT_MIN},
3918#endif
3919#ifdef _SC_IOV_MAX
3920 {"SC_IOV_MAX", _SC_IOV_MAX},
3921#endif
Fred Draked86ed291999-12-15 15:34:33 +00003922#ifdef _SC_IP_SECOPTS
3923 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
3924#endif
Fred Drakec9680921999-12-13 16:37:25 +00003925#ifdef _SC_JOB_CONTROL
3926 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
3927#endif
Fred Draked86ed291999-12-15 15:34:33 +00003928#ifdef _SC_KERN_POINTERS
3929 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
3930#endif
3931#ifdef _SC_KERN_SIM
3932 {"SC_KERN_SIM", _SC_KERN_SIM},
3933#endif
Fred Drakec9680921999-12-13 16:37:25 +00003934#ifdef _SC_LINE_MAX
3935 {"SC_LINE_MAX", _SC_LINE_MAX},
3936#endif
3937#ifdef _SC_LOGIN_NAME_MAX
3938 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
3939#endif
3940#ifdef _SC_LOGNAME_MAX
3941 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
3942#endif
3943#ifdef _SC_LONG_BIT
3944 {"SC_LONG_BIT", _SC_LONG_BIT},
3945#endif
Fred Draked86ed291999-12-15 15:34:33 +00003946#ifdef _SC_MAC
3947 {"SC_MAC", _SC_MAC},
3948#endif
Fred Drakec9680921999-12-13 16:37:25 +00003949#ifdef _SC_MAPPED_FILES
3950 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
3951#endif
3952#ifdef _SC_MAXPID
3953 {"SC_MAXPID", _SC_MAXPID},
3954#endif
3955#ifdef _SC_MB_LEN_MAX
3956 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
3957#endif
3958#ifdef _SC_MEMLOCK
3959 {"SC_MEMLOCK", _SC_MEMLOCK},
3960#endif
3961#ifdef _SC_MEMLOCK_RANGE
3962 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
3963#endif
3964#ifdef _SC_MEMORY_PROTECTION
3965 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
3966#endif
3967#ifdef _SC_MESSAGE_PASSING
3968 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
3969#endif
Fred Draked86ed291999-12-15 15:34:33 +00003970#ifdef _SC_MMAP_FIXED_ALIGNMENT
3971 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
3972#endif
Fred Drakec9680921999-12-13 16:37:25 +00003973#ifdef _SC_MQ_OPEN_MAX
3974 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
3975#endif
3976#ifdef _SC_MQ_PRIO_MAX
3977 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
3978#endif
Fred Draked86ed291999-12-15 15:34:33 +00003979#ifdef _SC_NACLS_MAX
3980 {"SC_NACLS_MAX", _SC_NACLS_MAX},
3981#endif
Fred Drakec9680921999-12-13 16:37:25 +00003982#ifdef _SC_NGROUPS_MAX
3983 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
3984#endif
3985#ifdef _SC_NL_ARGMAX
3986 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
3987#endif
3988#ifdef _SC_NL_LANGMAX
3989 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
3990#endif
3991#ifdef _SC_NL_MSGMAX
3992 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
3993#endif
3994#ifdef _SC_NL_NMAX
3995 {"SC_NL_NMAX", _SC_NL_NMAX},
3996#endif
3997#ifdef _SC_NL_SETMAX
3998 {"SC_NL_SETMAX", _SC_NL_SETMAX},
3999#endif
4000#ifdef _SC_NL_TEXTMAX
4001 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
4002#endif
4003#ifdef _SC_NPROCESSORS_CONF
4004 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
4005#endif
4006#ifdef _SC_NPROCESSORS_ONLN
4007 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
4008#endif
Fred Draked86ed291999-12-15 15:34:33 +00004009#ifdef _SC_NPROC_CONF
4010 {"SC_NPROC_CONF", _SC_NPROC_CONF},
4011#endif
4012#ifdef _SC_NPROC_ONLN
4013 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
4014#endif
Fred Drakec9680921999-12-13 16:37:25 +00004015#ifdef _SC_NZERO
4016 {"SC_NZERO", _SC_NZERO},
4017#endif
4018#ifdef _SC_OPEN_MAX
4019 {"SC_OPEN_MAX", _SC_OPEN_MAX},
4020#endif
4021#ifdef _SC_PAGESIZE
4022 {"SC_PAGESIZE", _SC_PAGESIZE},
4023#endif
4024#ifdef _SC_PAGE_SIZE
4025 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
4026#endif
4027#ifdef _SC_PASS_MAX
4028 {"SC_PASS_MAX", _SC_PASS_MAX},
4029#endif
4030#ifdef _SC_PHYS_PAGES
4031 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
4032#endif
4033#ifdef _SC_PII
4034 {"SC_PII", _SC_PII},
4035#endif
4036#ifdef _SC_PII_INTERNET
4037 {"SC_PII_INTERNET", _SC_PII_INTERNET},
4038#endif
4039#ifdef _SC_PII_INTERNET_DGRAM
4040 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
4041#endif
4042#ifdef _SC_PII_INTERNET_STREAM
4043 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
4044#endif
4045#ifdef _SC_PII_OSI
4046 {"SC_PII_OSI", _SC_PII_OSI},
4047#endif
4048#ifdef _SC_PII_OSI_CLTS
4049 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
4050#endif
4051#ifdef _SC_PII_OSI_COTS
4052 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
4053#endif
4054#ifdef _SC_PII_OSI_M
4055 {"SC_PII_OSI_M", _SC_PII_OSI_M},
4056#endif
4057#ifdef _SC_PII_SOCKET
4058 {"SC_PII_SOCKET", _SC_PII_SOCKET},
4059#endif
4060#ifdef _SC_PII_XTI
4061 {"SC_PII_XTI", _SC_PII_XTI},
4062#endif
4063#ifdef _SC_POLL
4064 {"SC_POLL", _SC_POLL},
4065#endif
4066#ifdef _SC_PRIORITIZED_IO
4067 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
4068#endif
4069#ifdef _SC_PRIORITY_SCHEDULING
4070 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
4071#endif
4072#ifdef _SC_REALTIME_SIGNALS
4073 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
4074#endif
4075#ifdef _SC_RE_DUP_MAX
4076 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
4077#endif
4078#ifdef _SC_RTSIG_MAX
4079 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
4080#endif
4081#ifdef _SC_SAVED_IDS
4082 {"SC_SAVED_IDS", _SC_SAVED_IDS},
4083#endif
4084#ifdef _SC_SCHAR_MAX
4085 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
4086#endif
4087#ifdef _SC_SCHAR_MIN
4088 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
4089#endif
4090#ifdef _SC_SELECT
4091 {"SC_SELECT", _SC_SELECT},
4092#endif
4093#ifdef _SC_SEMAPHORES
4094 {"SC_SEMAPHORES", _SC_SEMAPHORES},
4095#endif
4096#ifdef _SC_SEM_NSEMS_MAX
4097 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
4098#endif
4099#ifdef _SC_SEM_VALUE_MAX
4100 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
4101#endif
4102#ifdef _SC_SHARED_MEMORY_OBJECTS
4103 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
4104#endif
4105#ifdef _SC_SHRT_MAX
4106 {"SC_SHRT_MAX", _SC_SHRT_MAX},
4107#endif
4108#ifdef _SC_SHRT_MIN
4109 {"SC_SHRT_MIN", _SC_SHRT_MIN},
4110#endif
4111#ifdef _SC_SIGQUEUE_MAX
4112 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
4113#endif
4114#ifdef _SC_SIGRT_MAX
4115 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
4116#endif
4117#ifdef _SC_SIGRT_MIN
4118 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
4119#endif
Fred Draked86ed291999-12-15 15:34:33 +00004120#ifdef _SC_SOFTPOWER
4121 {"SC_SOFTPOWER", _SC_SOFTPOWER},
4122#endif
Fred Drakec9680921999-12-13 16:37:25 +00004123#ifdef _SC_SPLIT_CACHE
4124 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
4125#endif
4126#ifdef _SC_SSIZE_MAX
4127 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
4128#endif
4129#ifdef _SC_STACK_PROT
4130 {"SC_STACK_PROT", _SC_STACK_PROT},
4131#endif
4132#ifdef _SC_STREAM_MAX
4133 {"SC_STREAM_MAX", _SC_STREAM_MAX},
4134#endif
4135#ifdef _SC_SYNCHRONIZED_IO
4136 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
4137#endif
4138#ifdef _SC_THREADS
4139 {"SC_THREADS", _SC_THREADS},
4140#endif
4141#ifdef _SC_THREAD_ATTR_STACKADDR
4142 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
4143#endif
4144#ifdef _SC_THREAD_ATTR_STACKSIZE
4145 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
4146#endif
4147#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
4148 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
4149#endif
4150#ifdef _SC_THREAD_KEYS_MAX
4151 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
4152#endif
4153#ifdef _SC_THREAD_PRIORITY_SCHEDULING
4154 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
4155#endif
4156#ifdef _SC_THREAD_PRIO_INHERIT
4157 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
4158#endif
4159#ifdef _SC_THREAD_PRIO_PROTECT
4160 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
4161#endif
4162#ifdef _SC_THREAD_PROCESS_SHARED
4163 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
4164#endif
4165#ifdef _SC_THREAD_SAFE_FUNCTIONS
4166 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
4167#endif
4168#ifdef _SC_THREAD_STACK_MIN
4169 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
4170#endif
4171#ifdef _SC_THREAD_THREADS_MAX
4172 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
4173#endif
4174#ifdef _SC_TIMERS
4175 {"SC_TIMERS", _SC_TIMERS},
4176#endif
4177#ifdef _SC_TIMER_MAX
4178 {"SC_TIMER_MAX", _SC_TIMER_MAX},
4179#endif
4180#ifdef _SC_TTY_NAME_MAX
4181 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
4182#endif
4183#ifdef _SC_TZNAME_MAX
4184 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
4185#endif
4186#ifdef _SC_T_IOV_MAX
4187 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
4188#endif
4189#ifdef _SC_UCHAR_MAX
4190 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
4191#endif
4192#ifdef _SC_UINT_MAX
4193 {"SC_UINT_MAX", _SC_UINT_MAX},
4194#endif
4195#ifdef _SC_UIO_MAXIOV
4196 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
4197#endif
4198#ifdef _SC_ULONG_MAX
4199 {"SC_ULONG_MAX", _SC_ULONG_MAX},
4200#endif
4201#ifdef _SC_USHRT_MAX
4202 {"SC_USHRT_MAX", _SC_USHRT_MAX},
4203#endif
4204#ifdef _SC_VERSION
4205 {"SC_VERSION", _SC_VERSION},
4206#endif
4207#ifdef _SC_WORD_BIT
4208 {"SC_WORD_BIT", _SC_WORD_BIT},
4209#endif
4210#ifdef _SC_XBS5_ILP32_OFF32
4211 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
4212#endif
4213#ifdef _SC_XBS5_ILP32_OFFBIG
4214 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
4215#endif
4216#ifdef _SC_XBS5_LP64_OFF64
4217 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
4218#endif
4219#ifdef _SC_XBS5_LPBIG_OFFBIG
4220 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
4221#endif
4222#ifdef _SC_XOPEN_CRYPT
4223 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
4224#endif
4225#ifdef _SC_XOPEN_ENH_I18N
4226 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
4227#endif
4228#ifdef _SC_XOPEN_LEGACY
4229 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
4230#endif
4231#ifdef _SC_XOPEN_REALTIME
4232 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
4233#endif
4234#ifdef _SC_XOPEN_REALTIME_THREADS
4235 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
4236#endif
4237#ifdef _SC_XOPEN_SHM
4238 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
4239#endif
4240#ifdef _SC_XOPEN_UNIX
4241 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
4242#endif
4243#ifdef _SC_XOPEN_VERSION
4244 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
4245#endif
4246#ifdef _SC_XOPEN_XCU_VERSION
4247 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
4248#endif
4249#ifdef _SC_XOPEN_XPG2
4250 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
4251#endif
4252#ifdef _SC_XOPEN_XPG3
4253 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
4254#endif
4255#ifdef _SC_XOPEN_XPG4
4256 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
4257#endif
4258};
4259
4260static int
4261conv_sysconf_confname(arg, valuep)
4262 PyObject *arg;
4263 int *valuep;
4264{
4265 return conv_confname(arg, valuep, posix_constants_sysconf,
4266 sizeof(posix_constants_sysconf)
4267 / sizeof(struct constdef));
4268}
4269
4270static char posix_sysconf__doc__[] = "\
4271sysconf(name) -> integer\n\
4272Return an integer-valued system configuration variable.";
4273
4274static PyObject *
4275posix_sysconf(self, args)
4276 PyObject *self;
4277 PyObject *args;
4278{
4279 PyObject *result = NULL;
4280 int name;
4281
4282 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
4283 int value;
4284
4285 errno = 0;
4286 value = sysconf(name);
4287 if (value == -1 && errno != 0)
4288 posix_error();
4289 else
4290 result = PyInt_FromLong(value);
4291 }
4292 return result;
4293}
4294#endif
4295
4296
Fred Drakebec628d1999-12-15 18:31:10 +00004297/* This code is used to ensure that the tables of configuration value names
4298 * are in sorted order as required by conv_confname(), and also to build the
4299 * the exported dictionaries that are used to publish information about the
4300 * names available on the host platform.
4301 *
4302 * Sorting the table at runtime ensures that the table is properly ordered
4303 * when used, even for platforms we're not able to test on. It also makes
4304 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00004305 */
Fred Drakebec628d1999-12-15 18:31:10 +00004306
4307static int
4308cmp_constdefs(v1, v2)
4309 const void *v1;
4310 const void *v2;
4311{
4312 const struct constdef *c1 =
4313 (const struct constdef *) v1;
4314 const struct constdef *c2 =
4315 (const struct constdef *) v2;
4316
4317 return strcmp(c1->name, c2->name);
4318}
4319
4320static int
4321setup_confname_table(table, tablesize, tablename, moddict)
Fred Draked86ed291999-12-15 15:34:33 +00004322 struct constdef *table;
4323 size_t tablesize;
Fred Drakebec628d1999-12-15 18:31:10 +00004324 char * tablename;
4325 PyObject *moddict;
Fred Draked86ed291999-12-15 15:34:33 +00004326{
Fred Drakebec628d1999-12-15 18:31:10 +00004327 PyObject *d = NULL;
4328
4329 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
4330 d = PyDict_New();
4331 if (d != NULL) {
4332 PyObject *o;
Fred Drakee1a79b91999-12-30 18:05:43 +00004333 size_t i = 0;
Fred Draked86ed291999-12-15 15:34:33 +00004334
Fred Drakebec628d1999-12-15 18:31:10 +00004335 for (; i < tablesize; ++i) {
4336 o = PyInt_FromLong(table[i].value);
4337 if (o == NULL
4338 || PyDict_SetItemString(d, table[i].name, o) == -1) {
4339 Py_DECREF(d);
4340 d = NULL;
4341 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004342 }
4343 }
Fred Drakebec628d1999-12-15 18:31:10 +00004344 if (PyDict_SetItemString(moddict, tablename, d) == -1)
4345 return -1;
4346 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00004347 }
Fred Drakebec628d1999-12-15 18:31:10 +00004348 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004349}
4350
Fred Drakebec628d1999-12-15 18:31:10 +00004351/* Return -1 on failure, 0 on success. */
4352static int
4353setup_confname_tables(moddict)
4354 PyObject *moddict;
Fred Draked86ed291999-12-15 15:34:33 +00004355{
4356#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00004357 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00004358 sizeof(posix_constants_pathconf)
4359 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00004360 "pathconf_names", moddict))
4361 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004362#endif
4363#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00004364 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00004365 sizeof(posix_constants_confstr)
4366 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00004367 "confstr_names", moddict))
4368 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004369#endif
4370#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00004371 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00004372 sizeof(posix_constants_sysconf)
4373 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00004374 "sysconf_names", moddict))
4375 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004376#endif
Fred Drakebec628d1999-12-15 18:31:10 +00004377 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00004378}
Fred Draked86ed291999-12-15 15:34:33 +00004379
4380
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004381static char posix_abort__doc__[] = "\
4382abort() -> does not return!\n\
4383Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
4384in the hardest way possible on the hosting operating system.";
4385
4386static PyObject *
4387posix_abort(self, args)
4388 PyObject *self;
4389 PyObject *args;
4390{
4391 if (!PyArg_ParseTuple(args, ":abort"))
4392 return NULL;
4393 abort();
4394 /*NOTREACHED*/
4395 Py_FatalError("abort() called from Python code didn't abort!");
4396 return NULL;
4397}
Fred Drakebec628d1999-12-15 18:31:10 +00004398
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004399
4400static PyMethodDef posix_methods[] = {
4401 {"access", posix_access, METH_VARARGS, posix_access__doc__},
4402#ifdef HAVE_TTYNAME
4403 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
4404#endif
4405 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
4406 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004407#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004408 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004409#endif /* HAVE_CHOWN */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004410#ifdef HAVE_CTERMID
4411 {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__},
4412#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00004413#ifdef HAVE_GETCWD
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004414 {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00004415#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004416#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004417 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004418#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004419 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
4420 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
4421 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004422#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004423 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004424#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004425#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004426 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004427#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004428 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
4429 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
4430 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004431#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004432 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004433#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004434#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004435 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004436#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004437 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004438#ifdef HAVE_UNAME
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004439 {"uname", posix_uname, METH_VARARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004440#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004441 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
4442 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
4443 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004444#ifdef HAVE_TIMES
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004445 {"times", posix_times, METH_VARARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004446#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004447 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004448#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004449 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
4450 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004451#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00004452#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004453 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
4454 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00004455#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00004456#ifdef HAVE_FORK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004457 {"fork", posix_fork, METH_VARARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004458#endif /* HAVE_FORK */
4459#ifdef HAVE_GETEGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004460 {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004461#endif /* HAVE_GETEGID */
4462#ifdef HAVE_GETEUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004463 {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004464#endif /* HAVE_GETEUID */
4465#ifdef HAVE_GETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004466 {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004467#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00004468#ifdef HAVE_GETGROUPS
4469 {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__},
4470#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004471 {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004472#ifdef HAVE_GETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004473 {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004474#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00004475#ifdef HAVE_GETPPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004476 {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004477#endif /* HAVE_GETPPID */
4478#ifdef HAVE_GETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004479 {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004480#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004481#ifdef HAVE_GETLOGIN
4482 {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__},
4483#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00004484#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004485 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004486#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00004487#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004488 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00004489#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004490#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004491 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004492#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004493#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004494 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004495#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004496#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004497 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004498#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004499#ifdef HAVE_SETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004500 {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004501#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00004502#ifdef HAVE_WAIT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004503 {"wait", posix_wait, METH_VARARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004504#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004505#ifdef HAVE_WAITPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004506 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004507#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004508#ifdef HAVE_SETSID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004509 {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004510#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004511#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004512 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004513#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004514#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004515 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004516#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004517#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004518 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004519#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004520 {"open", posix_open, METH_VARARGS, posix_open__doc__},
4521 {"close", posix_close, METH_VARARGS, posix_close__doc__},
4522 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
4523 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
4524 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
4525 {"read", posix_read, METH_VARARGS, posix_read__doc__},
4526 {"write", posix_write, METH_VARARGS, posix_write__doc__},
4527 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
4528 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004529#ifdef HAVE_PIPE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004530 {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004531#endif
4532#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004533 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004534#endif
4535#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004536 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004537#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004538#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004539 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004540#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00004541#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004542 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00004543#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00004544#ifdef HAVE_FSYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004545 {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00004546#endif
4547#ifdef HAVE_FDATASYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004548 {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00004549#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00004550#ifdef HAVE_SYS_WAIT_H
4551#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004552 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004553#endif /* WIFSTOPPED */
4554#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004555 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004556#endif /* WIFSIGNALED */
4557#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004558 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004559#endif /* WIFEXITED */
4560#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004561 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004562#endif /* WEXITSTATUS */
4563#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004564 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004565#endif /* WTERMSIG */
4566#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004567 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004568#endif /* WSTOPSIG */
4569#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004570#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004571 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00004572#endif
4573#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004574 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00004575#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004576#ifdef HAVE_TMPNAM
4577 {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__},
4578#endif
4579#ifdef HAVE_TEMPNAM
4580 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
4581#endif
4582#ifdef HAVE_TMPNAM
4583 {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__},
4584#endif
Fred Drakec9680921999-12-13 16:37:25 +00004585#ifdef HAVE_CONFSTR
4586 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
4587#endif
4588#ifdef HAVE_SYSCONF
4589 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
4590#endif
4591#ifdef HAVE_FPATHCONF
4592 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
4593#endif
4594#ifdef HAVE_PATHCONF
4595 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
4596#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004597 {"abort", posix_abort, METH_VARARGS, posix_abort__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004598 {NULL, NULL} /* Sentinel */
4599};
4600
4601
Barry Warsaw4a342091996-12-19 23:50:02 +00004602static int
4603ins(d, symbol, value)
4604 PyObject* d;
4605 char* symbol;
4606 long value;
4607{
4608 PyObject* v = PyInt_FromLong(value);
4609 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
4610 return -1; /* triggers fatal error */
4611
4612 Py_DECREF(v);
4613 return 0;
4614}
4615
Guido van Rossumd48f2521997-12-05 22:19:34 +00004616#if defined(PYOS_OS2)
4617/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
4618static int insertvalues(PyObject *d)
4619{
4620 APIRET rc;
4621 ULONG values[QSV_MAX+1];
4622 PyObject *v;
4623 char *ver, tmp[10];
4624
4625 Py_BEGIN_ALLOW_THREADS
4626 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
4627 Py_END_ALLOW_THREADS
4628
4629 if (rc != NO_ERROR) {
4630 os2_error(rc);
4631 return -1;
4632 }
4633
4634 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
4635 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
4636 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
4637 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
4638 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
4639 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
4640 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
4641
4642 switch (values[QSV_VERSION_MINOR]) {
4643 case 0: ver = "2.00"; break;
4644 case 10: ver = "2.10"; break;
4645 case 11: ver = "2.11"; break;
4646 case 30: ver = "3.00"; break;
4647 case 40: ver = "4.00"; break;
4648 case 50: ver = "5.00"; break;
4649 default:
4650 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
4651 values[QSV_VERSION_MINOR]);
4652 ver = &tmp[0];
4653 }
4654
4655 /* Add Indicator of the Version of the Operating System */
4656 v = PyString_FromString(ver);
4657 if (!v || PyDict_SetItemString(d, "version", v) < 0)
4658 return -1;
4659 Py_DECREF(v);
4660
4661 /* Add Indicator of Which Drive was Used to Boot the System */
4662 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
4663 tmp[1] = ':';
4664 tmp[2] = '\0';
4665
4666 v = PyString_FromString(tmp);
4667 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
4668 return -1;
4669 Py_DECREF(v);
4670
4671 return 0;
4672}
4673#endif
4674
Barry Warsaw4a342091996-12-19 23:50:02 +00004675static int
4676all_ins(d)
4677 PyObject* d;
4678{
Guido van Rossum94f6f721999-01-06 18:42:14 +00004679#ifdef F_OK
4680 if (ins(d, "F_OK", (long)F_OK)) return -1;
4681#endif
4682#ifdef R_OK
4683 if (ins(d, "R_OK", (long)R_OK)) return -1;
4684#endif
4685#ifdef W_OK
4686 if (ins(d, "W_OK", (long)W_OK)) return -1;
4687#endif
4688#ifdef X_OK
4689 if (ins(d, "X_OK", (long)X_OK)) return -1;
4690#endif
Fred Drakec9680921999-12-13 16:37:25 +00004691#ifdef NGROUPS_MAX
4692 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
4693#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004694#ifdef TMP_MAX
4695 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
4696#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00004697#ifdef WNOHANG
4698 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
4699#endif
4700#ifdef O_RDONLY
4701 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
4702#endif
4703#ifdef O_WRONLY
4704 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
4705#endif
4706#ifdef O_RDWR
4707 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
4708#endif
4709#ifdef O_NDELAY
4710 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
4711#endif
4712#ifdef O_NONBLOCK
4713 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
4714#endif
4715#ifdef O_APPEND
4716 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
4717#endif
4718#ifdef O_DSYNC
4719 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
4720#endif
4721#ifdef O_RSYNC
4722 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
4723#endif
4724#ifdef O_SYNC
4725 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
4726#endif
4727#ifdef O_NOCTTY
4728 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
4729#endif
4730#ifdef O_CREAT
4731 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
4732#endif
4733#ifdef O_EXCL
4734 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
4735#endif
4736#ifdef O_TRUNC
4737 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
4738#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00004739#ifdef O_BINARY
4740 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
4741#endif
4742#ifdef O_TEXT
4743 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
4744#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00004745
Guido van Rossum246bc171999-02-01 23:54:31 +00004746#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00004747 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
4748 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
4749 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
4750 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
4751 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00004752#endif
4753
Guido van Rossumd48f2521997-12-05 22:19:34 +00004754#if defined(PYOS_OS2)
4755 if (insertvalues(d)) return -1;
4756#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00004757 return 0;
4758}
4759
4760
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004761#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004762#define INITFUNC initnt
4763#define MODNAME "nt"
4764#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004765#if defined(PYOS_OS2)
4766#define INITFUNC initos2
4767#define MODNAME "os2"
4768#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004769#define INITFUNC initposix
4770#define MODNAME "posix"
4771#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004772#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004773
Guido van Rossum3886bb61998-12-04 18:50:17 +00004774DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004775INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00004776{
Barry Warsaw53699e91996-12-10 23:23:01 +00004777 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004778
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004779 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004780 posix_methods,
4781 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004782 (PyObject *)NULL,
4783 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00004784 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004785
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004786 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004787 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00004788 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004789 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00004790 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00004791
Barry Warsaw4a342091996-12-19 23:50:02 +00004792 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00004793 return;
4794
Fred Drakebec628d1999-12-15 18:31:10 +00004795 if (setup_confname_tables(d))
4796 return;
4797
Barry Warsawca74da41999-02-09 19:31:45 +00004798 PyDict_SetItemString(d, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00004799
Guido van Rossumb3d39562000-01-31 18:41:26 +00004800#ifdef HAVE_PUTENV
Fred Drake762e2061999-08-26 17:23:54 +00004801 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00004802#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004803}