blob: e2691065bab5f354b77e5dcb74187ae06d827a8b [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;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000552
553#ifdef MS_WIN32
554 int pathlen;
555 char pathcopy[MAX_PATH];
556#endif /* MS_WIN32 */
557
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000558 if (!PyArg_ParseTuple(args, format, &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559 return NULL;
Guido van Rossumace88ae2000-04-21 18:54:45 +0000560
561#ifdef MS_WIN32
562 pathlen = strlen(path);
563 /* the library call can blow up if the file name is too long! */
564 if (pathlen > MAX_PATH) {
565 errno = ENAMETOOLONG;
566 return posix_error();
567 }
568
569 if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) {
570 /* exception for drive root */
571 if (!((pathlen == 3) &&
572 (path[1] == ':') &&
573 (path[2] == '\\' || path[2] == '/')))
574 {
575 strncpy(pathcopy, path, pathlen);
576 pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */
577 path = pathcopy;
578 }
579 }
580#endif /* MS_WIN32 */
581
Barry Warsaw53699e91996-12-10 23:23:01 +0000582 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000583 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000584 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000585 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000586 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000587#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000588 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000589 (long)st.st_mode,
590 (long)st.st_ino,
591 (long)st.st_dev,
592 (long)st.st_nlink,
593 (long)st.st_uid,
594 (long)st.st_gid,
595 (long)st.st_size,
596 (long)st.st_atime,
597 (long)st.st_mtime,
598 (long)st.st_ctime);
599#else
600 return Py_BuildValue("(lLllllLlll)",
601 (long)st.st_mode,
602 (LONG_LONG)st.st_ino,
603 (long)st.st_dev,
604 (long)st.st_nlink,
605 (long)st.st_uid,
606 (long)st.st_gid,
607 (LONG_LONG)st.st_size,
608 (long)st.st_atime,
609 (long)st.st_mtime,
610 (long)st.st_ctime);
611#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612}
613
614
615/* POSIX methods */
616
Guido van Rossum94f6f721999-01-06 18:42:14 +0000617static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000618"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000619Test for access to a file.";
620
621static PyObject *
622posix_access(self, args)
623 PyObject *self;
624 PyObject *args;
625{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000626 char *path;
627 int mode;
628 int res;
629
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000630 if (!PyArg_ParseTuple(args, "si:access", &path, &mode))
Guido van Rossum015f22a1999-01-06 22:52:38 +0000631 return NULL;
632 Py_BEGIN_ALLOW_THREADS
633 res = access(path, mode);
634 Py_END_ALLOW_THREADS
635 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000636}
637
Guido van Rossumd371ff11999-01-25 16:12:23 +0000638#ifndef F_OK
639#define F_OK 0
640#endif
641#ifndef R_OK
642#define R_OK 4
643#endif
644#ifndef W_OK
645#define W_OK 2
646#endif
647#ifndef X_OK
648#define X_OK 1
649#endif
650
651#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000652static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000653"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000654Return the name of the terminal device connected to 'fd'.";
655
656static PyObject *
657posix_ttyname(self, args)
658 PyObject *self;
659 PyObject *args;
660{
Guido van Rossum94f6f721999-01-06 18:42:14 +0000661 int id;
662 char *ret;
663
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000664 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
Guido van Rossum94f6f721999-01-06 18:42:14 +0000665 return NULL;
666
Guido van Rossum94f6f721999-01-06 18:42:14 +0000667 ret = ttyname(id);
668 if (ret == NULL)
669 return(posix_error());
670 return(PyString_FromString(ret));
671}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000672#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000673
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000674#ifdef HAVE_CTERMID
675static char posix_ctermid__doc__[] =
676"ctermid() -> String\n\
677Return the name of the controlling terminal for this process.";
678
679static PyObject *
680posix_ctermid(self, args)
681 PyObject *self;
682 PyObject *args;
683{
684 char *ret;
685 char buffer[L_ctermid];
686
687 if (!PyArg_ParseTuple(args, ":ctermid"))
688 return NULL;
689
Greg Wardb48bc172000-03-01 21:51:56 +0000690#ifdef USE_CTERMID_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000691 ret = ctermid_r(buffer);
692#else
693 ret = ctermid(buffer);
694#endif
695 if (ret == NULL)
696 return(posix_error());
697 return(PyString_FromString(buffer));
698}
699#endif
700
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000701static char posix_chdir__doc__[] =
702"chdir(path) -> None\n\
703Change the current working directory to the specified path.";
704
Barry Warsaw53699e91996-12-10 23:23:01 +0000705static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000706posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000707 PyObject *self;
708 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000710 return posix_1str(args, "s:chdir", chdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711}
712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000713
714static char posix_chmod__doc__[] =
715"chmod(path, mode) -> None\n\
716Change the access permissions of a file.";
717
Barry Warsaw53699e91996-12-10 23:23:01 +0000718static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000720 PyObject *self;
721 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722{
Guido van Rossumffd15f52000-03-31 00:47:28 +0000723 char *path;
724 int i;
725 int res;
Guido van Rossum49679b42000-03-31 00:48:21 +0000726 if (!PyArg_ParseTuple(args, "si", &path, &i))
Guido van Rossumffd15f52000-03-31 00:47:28 +0000727 return NULL;
728 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef40e772000-03-31 01:26:23 +0000729 res = chmod(path, i);
Guido van Rossumffd15f52000-03-31 00:47:28 +0000730 Py_END_ALLOW_THREADS
731 if (res < 0)
732 return posix_error_with_filename(path);
733 Py_INCREF(Py_None);
734 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000735}
736
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000737
Guido van Rossum21142a01999-01-08 21:05:37 +0000738#ifdef HAVE_FSYNC
739static char posix_fsync__doc__[] =
740"fsync(fildes) -> None\n\
741force write of file with filedescriptor to disk.";
742
743static PyObject *
744posix_fsync(self, args)
745 PyObject *self;
746 PyObject *args;
747{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000748 return posix_int(args, "i:fsync", fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000749}
750#endif /* HAVE_FSYNC */
751
752#ifdef HAVE_FDATASYNC
753static char posix_fdatasync__doc__[] =
754"fdatasync(fildes) -> None\n\
755force write of file with filedescriptor to disk.\n\
756 does not force update of metadata.";
757
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000758extern int fdatasync(int); /* Prototype just in case */
759
Guido van Rossum21142a01999-01-08 21:05:37 +0000760static PyObject *
761posix_fdatasync(self, args)
762 PyObject *self;
763 PyObject *args;
764{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000765 return posix_int(args, "i:fdatasync", fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +0000766}
767#endif /* HAVE_FDATASYNC */
768
769
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000771static char posix_chown__doc__[] =
772"chown(path, uid, gid) -> None\n\
773Change the owner and group id of path to the numeric uid and gid.";
774
Barry Warsaw53699e91996-12-10 23:23:01 +0000775static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000776posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000777 PyObject *self;
778 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000779{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000780 return posix_strintint(args, "sii:chown", chown);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000781}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000782#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000783
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000784
Guido van Rossum36bc6801995-06-14 22:54:23 +0000785#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000786static char posix_getcwd__doc__[] =
787"getcwd() -> path\n\
788Return a string representing the current working directory.";
789
Barry Warsaw53699e91996-12-10 23:23:01 +0000790static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000792 PyObject *self;
793 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794{
795 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000796 char *res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000797 if (!PyArg_ParseTuple(args, ":getcwd"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000799 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000800 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000801 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000802 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000803 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000804 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000805}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000806#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000807
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000808
Guido van Rossumb6775db1994-08-01 11:34:53 +0000809#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000810static char posix_link__doc__[] =
811"link(src, dst) -> None\n\
812Create a hard link to a file.";
813
Barry Warsaw53699e91996-12-10 23:23:01 +0000814static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000816 PyObject *self;
817 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818{
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000819 return posix_2str(args, "ss:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000821#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000823
824static char posix_listdir__doc__[] =
825"listdir(path) -> list_of_strings\n\
826Return a list containing the names of the entries in the directory.\n\
827\n\
828 path: path of directory to list\n\
829\n\
830The list is in arbitrary order. It does not include the special\n\
831entries '.' and '..' even if they are present in the directory.";
832
Barry Warsaw53699e91996-12-10 23:23:01 +0000833static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000834posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000835 PyObject *self;
836 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000837{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000838 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000839 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000840#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000841
Guido van Rossumb6775db1994-08-01 11:34:53 +0000842 char *name;
843 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000844 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000845 HANDLE hFindFile;
846 WIN32_FIND_DATA FileData;
847 char namebuf[MAX_PATH+5];
848
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000849 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000850 return NULL;
851 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000852 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000853 return NULL;
854 }
855 strcpy(namebuf, name);
856 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
857 namebuf[len++] = '/';
858 strcpy(namebuf + len, "*.*");
859
Barry Warsaw53699e91996-12-10 23:23:01 +0000860 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000861 return NULL;
862
863 hFindFile = FindFirstFile(namebuf, &FileData);
864 if (hFindFile == INVALID_HANDLE_VALUE) {
865 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000866 if (errno == ERROR_FILE_NOT_FOUND)
867 return PyList_New(0);
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000868 return posix_error_with_filename(name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000869 }
870 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000871 if (FileData.cFileName[0] == '.' &&
872 (FileData.cFileName[1] == '\0' ||
873 FileData.cFileName[1] == '.' &&
874 FileData.cFileName[2] == '\0'))
875 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000876 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000877 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000878 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000879 d = NULL;
880 break;
881 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000882 if (PyList_Append(d, v) != 0) {
883 Py_DECREF(v);
884 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000885 d = NULL;
886 break;
887 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000888 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000889 } while (FindNextFile(hFindFile, &FileData) == TRUE);
890
891 if (FindClose(hFindFile) == FALSE) {
892 errno = GetLastError();
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000893 return posix_error_with_filename(&name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000894 }
895
896 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000897
Guido van Rossum8d665e61996-06-26 18:22:49 +0000898#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000899#ifdef _MSC_VER /* 16-bit Windows */
900
901#ifndef MAX_PATH
902#define MAX_PATH 250
903#endif
904 char *name, *pt;
905 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000906 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000907 char namebuf[MAX_PATH+5];
908 struct _find_t ep;
909
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000910 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000911 return NULL;
912 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000913 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000914 return NULL;
915 }
916 strcpy(namebuf, name);
917 for (pt = namebuf; *pt; pt++)
918 if (*pt == '/')
919 *pt = '\\';
920 if (namebuf[len-1] != '\\')
921 namebuf[len++] = '\\';
922 strcpy(namebuf + len, "*.*");
923
Barry Warsaw53699e91996-12-10 23:23:01 +0000924 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000925 return NULL;
926
927 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000928 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
929 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000930 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000931 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000932 }
933 do {
934 if (ep.name[0] == '.' &&
935 (ep.name[1] == '\0' ||
936 ep.name[1] == '.' &&
937 ep.name[2] == '\0'))
938 continue;
939 strcpy(namebuf, ep.name);
940 for (pt = namebuf; *pt; pt++)
941 if (isupper(*pt))
942 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000943 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000944 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000945 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000946 d = NULL;
947 break;
948 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000949 if (PyList_Append(d, v) != 0) {
950 Py_DECREF(v);
951 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000952 d = NULL;
953 break;
954 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000955 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000956 } while (_dos_findnext(&ep) == 0);
957
958 return d;
959
960#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000961#if defined(PYOS_OS2)
962
963#ifndef MAX_PATH
964#define MAX_PATH CCHMAXPATH
965#endif
966 char *name, *pt;
967 int len;
968 PyObject *d, *v;
969 char namebuf[MAX_PATH+5];
970 HDIR hdir = 1;
971 ULONG srchcnt = 1;
972 FILEFINDBUF3 ep;
973 APIRET rc;
974
Fred Drake5ab8eaf1999-12-09 21:13:07 +0000975 if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000976 return NULL;
977 if (len >= MAX_PATH) {
978 PyErr_SetString(PyExc_ValueError, "path too long");
979 return NULL;
980 }
981 strcpy(namebuf, name);
982 for (pt = namebuf; *pt; pt++)
983 if (*pt == '/')
984 *pt = '\\';
985 if (namebuf[len-1] != '\\')
986 namebuf[len++] = '\\';
987 strcpy(namebuf + len, "*.*");
988
989 if ((d = PyList_New(0)) == NULL)
990 return NULL;
991
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000992 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
993 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000994 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000995 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
996 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
997 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000998
999 if (rc != NO_ERROR) {
1000 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001001 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001002 }
1003
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001004 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001005 do {
1006 if (ep.achName[0] == '.'
1007 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001008 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001009
1010 strcpy(namebuf, ep.achName);
1011
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001012 /* Leave Case of Name Alone -- In Native Form */
1013 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001014
1015 v = PyString_FromString(namebuf);
1016 if (v == NULL) {
1017 Py_DECREF(d);
1018 d = NULL;
1019 break;
1020 }
1021 if (PyList_Append(d, v) != 0) {
1022 Py_DECREF(v);
1023 Py_DECREF(d);
1024 d = NULL;
1025 break;
1026 }
1027 Py_DECREF(v);
1028 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
1029 }
1030
1031 return d;
1032#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001033
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001034 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +00001035 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001036 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001037 struct dirent *ep;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001038 if (!PyArg_ParseTuple(args, "s:listdir", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001039 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001040 if ((dirp = opendir(name)) == NULL) {
Barry Warsawf63b8cc1999-05-27 23:13:21 +00001041 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +00001042 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001043 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001044 closedir(dirp);
1045 return NULL;
1046 }
1047 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001048 if (ep->d_name[0] == '.' &&
1049 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +00001050 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +00001051 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +00001052 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001053 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001054 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001055 d = NULL;
1056 break;
1057 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001058 if (PyList_Append(d, v) != 0) {
1059 Py_DECREF(v);
1060 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001061 d = NULL;
1062 break;
1063 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001064 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001065 }
1066 closedir(dirp);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001067
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001068 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001069
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001070#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001071#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +00001072#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001073}
1074
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001075static char posix_mkdir__doc__[] =
1076"mkdir(path [, mode=0777]) -> None\n\
1077Create a directory.";
1078
Barry Warsaw53699e91996-12-10 23:23:01 +00001079static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001080posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001081 PyObject *self;
1082 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001083{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001084 int res;
1085 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001086 int mode = 0777;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001087 if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001088 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001089 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001090#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001091 res = mkdir(path);
1092#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001093 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001094#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001095 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001096 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001097 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001098 Py_INCREF(Py_None);
1099 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001100}
1101
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001102
Guido van Rossumb6775db1994-08-01 11:34:53 +00001103#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001104static char posix_nice__doc__[] =
1105"nice(inc) -> new_priority\n\
1106Decrease the priority of process and return new priority.";
1107
Barry Warsaw53699e91996-12-10 23:23:01 +00001108static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001109posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001110 PyObject *self;
1111 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001112{
1113 int increment, value;
1114
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001115 if (!PyArg_ParseTuple(args, "i:nice", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001116 return NULL;
1117 value = nice(increment);
1118 if (value == -1)
1119 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001120 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001121}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001122#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001123
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001124
1125static char posix_rename__doc__[] =
1126"rename(old, new) -> None\n\
1127Rename a file or directory.";
1128
Barry Warsaw53699e91996-12-10 23:23:01 +00001129static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001130posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001131 PyObject *self;
1132 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001133{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001134 return posix_2str(args, "ss:rename", rename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135}
1136
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001137
1138static char posix_rmdir__doc__[] =
1139"rmdir(path) -> None\n\
1140Remove a directory.";
1141
Barry Warsaw53699e91996-12-10 23:23:01 +00001142static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001143posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001144 PyObject *self;
1145 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001146{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001147 return posix_1str(args, "s:rmdir", rmdir);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001148}
1149
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001150
1151static char posix_stat__doc__[] =
1152"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1153Perform a stat system call on the given path.";
1154
Barry Warsaw53699e91996-12-10 23:23:01 +00001155static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001156posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001157 PyObject *self;
1158 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001159{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001160 return posix_do_stat(self, args, "s:stat", stat);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161}
1162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001163
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001164#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001165static char posix_system__doc__[] =
1166"system(command) -> exit_status\n\
1167Execute the command (a string) in a subshell.";
1168
Barry Warsaw53699e91996-12-10 23:23:01 +00001169static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001170posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001171 PyObject *self;
1172 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001173{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001174 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001175 long sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001176 if (!PyArg_ParseTuple(args, "s:system", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001178 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001179 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001180 Py_END_ALLOW_THREADS
1181 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001183#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001184
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001185
1186static char posix_umask__doc__[] =
1187"umask(new_mask) -> old_mask\n\
1188Set the current numeric umask and return the previous umask.";
1189
Barry Warsaw53699e91996-12-10 23:23:01 +00001190static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001192 PyObject *self;
1193 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001194{
1195 int i;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001196 if (!PyArg_ParseTuple(args, "i:umask", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001197 return NULL;
1198 i = umask(i);
1199 if (i < 0)
1200 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001201 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001202}
1203
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001204
1205static char posix_unlink__doc__[] =
1206"unlink(path) -> None\n\
1207Remove a file (same as remove(path)).";
1208
1209static char posix_remove__doc__[] =
1210"remove(path) -> None\n\
1211Remove a file (same as unlink(path)).";
1212
Barry Warsaw53699e91996-12-10 23:23:01 +00001213static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001214posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001215 PyObject *self;
1216 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001217{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001218 return posix_1str(args, "s:remove", unlink);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001219}
1220
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001221
Guido van Rossumb6775db1994-08-01 11:34:53 +00001222#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001223static char posix_uname__doc__[] =
1224"uname() -> (sysname, nodename, release, version, machine)\n\
1225Return a tuple identifying the current operating system.";
1226
Barry Warsaw53699e91996-12-10 23:23:01 +00001227static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001228posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001229 PyObject *self;
1230 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001231{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001232 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001233 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001234 if (!PyArg_ParseTuple(args, ":uname"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001235 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001236 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001237 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001238 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001239 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001240 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001241 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001242 u.sysname,
1243 u.nodename,
1244 u.release,
1245 u.version,
1246 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001247}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001248#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001250
1251static char posix_utime__doc__[] =
1252"utime(path, (atime, utime)) -> None\n\
1253Set the access and modified time of the file to the given values.";
1254
Barry Warsaw53699e91996-12-10 23:23:01 +00001255static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001256posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001257 PyObject *self;
1258 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001260 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001261 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001262 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001263
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001264/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001265#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001266 struct utimbuf buf;
1267#define ATIME buf.actime
1268#define MTIME buf.modtime
1269#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001270#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001271 time_t buf[2];
1272#define ATIME buf[0]
1273#define MTIME buf[1]
1274#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001275#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001276
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001277 if (!PyArg_ParseTuple(args, "s(ll):utime", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001278 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001279 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001280 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001281 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001282 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001283 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001284 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001285 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001286 Py_INCREF(Py_None);
1287 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001288#undef UTIME_ARG
1289#undef ATIME
1290#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001291}
1292
Guido van Rossum85e3b011991-06-03 12:42:10 +00001293
Guido van Rossum3b066191991-06-04 19:40:25 +00001294/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001295
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001296static char posix__exit__doc__[] =
1297"_exit(status)\n\
1298Exit to the system with specified status, without normal exit processing.";
1299
Barry Warsaw53699e91996-12-10 23:23:01 +00001300static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001301posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001302 PyObject *self;
1303 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001304{
1305 int sts;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001306 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001307 return NULL;
1308 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001309 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001310}
1311
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001312
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001313#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001314static char posix_execv__doc__[] =
1315"execv(path, args)\n\
1316Execute an executable path with arguments, replacing current process.\n\
1317\n\
1318 path: path of executable file\n\
1319 args: tuple or list of strings";
1320
Barry Warsaw53699e91996-12-10 23:23:01 +00001321static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001322posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001323 PyObject *self;
1324 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001325{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001326 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001327 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001328 char **argvlist;
1329 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001330 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001331
Guido van Rossum89b33251993-10-22 14:26:06 +00001332 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001333 argv is a list or tuple of strings. */
1334
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001335 if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001336 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001337 if (PyList_Check(argv)) {
1338 argc = PyList_Size(argv);
1339 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001340 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001341 else if (PyTuple_Check(argv)) {
1342 argc = PyTuple_Size(argv);
1343 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001344 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001345 else {
1346 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001347 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001348 return NULL;
1349 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001350
Barry Warsaw53699e91996-12-10 23:23:01 +00001351 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001352 if (argvlist == NULL)
1353 return NULL;
1354 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001355 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1356 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001357 goto badarg;
1358 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001359 }
1360 argvlist[argc] = NULL;
1361
Guido van Rossumb6775db1994-08-01 11:34:53 +00001362#ifdef BAD_EXEC_PROTOTYPES
1363 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001364#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001365 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001366#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001367
Guido van Rossum85e3b011991-06-03 12:42:10 +00001368 /* If we get here it's definitely an error */
1369
Barry Warsaw53699e91996-12-10 23:23:01 +00001370 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001371 return posix_error();
1372}
1373
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001374
1375static char posix_execve__doc__[] =
1376"execve(path, args, env)\n\
1377Execute a path with arguments and environment, replacing current process.\n\
1378\n\
1379 path: path of executable file\n\
1380 args: tuple or list of arguments\n\
1381 env: dictonary of strings mapping to strings";
1382
Barry Warsaw53699e91996-12-10 23:23:01 +00001383static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001384posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001385 PyObject *self;
1386 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001387{
1388 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001389 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001390 char **argvlist;
1391 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001392 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001393 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001394 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001395
1396 /* execve has three arguments: (path, argv, env), where
1397 argv is a list or tuple of strings and env is a dictionary
1398 like posix.environ. */
1399
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001400 if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001401 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001402 if (PyList_Check(argv)) {
1403 argc = PyList_Size(argv);
1404 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001405 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001406 else if (PyTuple_Check(argv)) {
1407 argc = PyTuple_Size(argv);
1408 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001409 }
1410 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001411 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001412 return NULL;
1413 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001414 if (!PyMapping_Check(env)) {
1415 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001416 return NULL;
1417 }
1418
Barry Warsaw53699e91996-12-10 23:23:01 +00001419 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001420 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001421 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001422 return NULL;
1423 }
1424 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001425 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001426 "s;argv must be list of strings",
1427 &argvlist[i]))
1428 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001429 goto fail_1;
1430 }
1431 }
1432 argvlist[argc] = NULL;
1433
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001434 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001435 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001436 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001437 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001438 goto fail_1;
1439 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001440 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001441 keys = PyMapping_Keys(env);
1442 vals = PyMapping_Values(env);
1443 if (!keys || !vals)
1444 goto fail_2;
1445
1446 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001447 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001448
1449 key = PyList_GetItem(keys, pos);
1450 val = PyList_GetItem(vals, pos);
1451 if (!key || !val)
1452 goto fail_2;
1453
Barry Warsaw53699e91996-12-10 23:23:01 +00001454 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001455 !PyArg_Parse(val, "s;non-string value in env", &v))
1456 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001457 goto fail_2;
1458 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001459
1460#if defined(PYOS_OS2)
1461 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1462 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1463#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001464 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001465 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001466 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001467 goto fail_2;
1468 }
1469 sprintf(p, "%s=%s", k, v);
1470 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001471#if defined(PYOS_OS2)
1472 }
1473#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001474 }
1475 envlist[envc] = 0;
1476
Guido van Rossumb6775db1994-08-01 11:34:53 +00001477
1478#ifdef BAD_EXEC_PROTOTYPES
1479 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001480#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001481 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001482#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001483
1484 /* If we get here it's definitely an error */
1485
1486 (void) posix_error();
1487
1488 fail_2:
1489 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001490 PyMem_DEL(envlist[envc]);
1491 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001492 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001493 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001494 Py_XDECREF(vals);
1495 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001496 return NULL;
1497}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001498#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001499
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001500
Guido van Rossuma1065681999-01-25 23:20:23 +00001501#ifdef HAVE_SPAWNV
1502static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001503"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001504Execute an executable path with arguments, replacing current process.\n\
1505\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001506 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001507 path: path of executable file\n\
1508 args: tuple or list of strings";
1509
1510static PyObject *
1511posix_spawnv(self, args)
1512 PyObject *self;
1513 PyObject *args;
1514{
1515 char *path;
1516 PyObject *argv;
1517 char **argvlist;
1518 int mode, i, argc;
1519 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1520
1521 /* spawnv has three arguments: (mode, path, argv), where
1522 argv is a list or tuple of strings. */
1523
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001524 if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv))
Guido van Rossuma1065681999-01-25 23:20:23 +00001525 return NULL;
1526 if (PyList_Check(argv)) {
1527 argc = PyList_Size(argv);
1528 getitem = PyList_GetItem;
1529 }
1530 else if (PyTuple_Check(argv)) {
1531 argc = PyTuple_Size(argv);
1532 getitem = PyTuple_GetItem;
1533 }
1534 else {
1535 badarg:
1536 PyErr_BadArgument();
1537 return NULL;
1538 }
1539
1540 argvlist = PyMem_NEW(char *, argc+1);
1541 if (argvlist == NULL)
1542 return NULL;
1543 for (i = 0; i < argc; i++) {
1544 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1545 PyMem_DEL(argvlist);
1546 goto badarg;
1547 }
1548 }
1549 argvlist[argc] = NULL;
1550
Guido van Rossum246bc171999-02-01 23:54:31 +00001551 if (mode == _OLD_P_OVERLAY)
1552 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001553 i = _spawnv(mode, path, argvlist);
1554
1555 PyMem_DEL(argvlist);
1556
1557 if (i == -1)
1558 return posix_error();
1559 else
1560 return Py_BuildValue("i", i);
1561}
1562
1563
1564static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001565"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001566Execute a path with arguments and environment, replacing current process.\n\
1567\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001568 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001569 path: path of executable file\n\
1570 args: tuple or list of arguments\n\
1571 env: dictonary of strings mapping to strings";
1572
1573static PyObject *
1574posix_spawnve(self, args)
1575 PyObject *self;
1576 PyObject *args;
1577{
1578 char *path;
1579 PyObject *argv, *env;
1580 char **argvlist;
1581 char **envlist;
1582 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1583 int mode, i, pos, argc, envc;
1584 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1585
1586 /* spawnve has four arguments: (mode, path, argv, env), where
1587 argv is a list or tuple of strings and env is a dictionary
1588 like posix.environ. */
1589
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001590 if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env))
Guido van Rossuma1065681999-01-25 23:20:23 +00001591 return NULL;
1592 if (PyList_Check(argv)) {
1593 argc = PyList_Size(argv);
1594 getitem = PyList_GetItem;
1595 }
1596 else if (PyTuple_Check(argv)) {
1597 argc = PyTuple_Size(argv);
1598 getitem = PyTuple_GetItem;
1599 }
1600 else {
1601 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1602 return NULL;
1603 }
1604 if (!PyMapping_Check(env)) {
1605 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1606 return NULL;
1607 }
1608
1609 argvlist = PyMem_NEW(char *, argc+1);
1610 if (argvlist == NULL) {
1611 PyErr_NoMemory();
1612 return NULL;
1613 }
1614 for (i = 0; i < argc; i++) {
1615 if (!PyArg_Parse((*getitem)(argv, i),
1616 "s;argv must be list of strings",
1617 &argvlist[i]))
1618 {
1619 goto fail_1;
1620 }
1621 }
1622 argvlist[argc] = NULL;
1623
1624 i = PyMapping_Length(env);
1625 envlist = PyMem_NEW(char *, i + 1);
1626 if (envlist == NULL) {
1627 PyErr_NoMemory();
1628 goto fail_1;
1629 }
1630 envc = 0;
1631 keys = PyMapping_Keys(env);
1632 vals = PyMapping_Values(env);
1633 if (!keys || !vals)
1634 goto fail_2;
1635
1636 for (pos = 0; pos < i; pos++) {
1637 char *p, *k, *v;
1638
1639 key = PyList_GetItem(keys, pos);
1640 val = PyList_GetItem(vals, pos);
1641 if (!key || !val)
1642 goto fail_2;
1643
1644 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1645 !PyArg_Parse(val, "s;non-string value in env", &v))
1646 {
1647 goto fail_2;
1648 }
1649 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1650 if (p == NULL) {
1651 PyErr_NoMemory();
1652 goto fail_2;
1653 }
1654 sprintf(p, "%s=%s", k, v);
1655 envlist[envc++] = p;
1656 }
1657 envlist[envc] = 0;
1658
Guido van Rossum246bc171999-02-01 23:54:31 +00001659 if (mode == _OLD_P_OVERLAY)
1660 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001661 i = _spawnve(mode, path, argvlist, envlist);
1662 if (i == -1)
1663 (void) posix_error();
1664 else
1665 res = Py_BuildValue("i", i);
1666
1667 fail_2:
1668 while (--envc >= 0)
1669 PyMem_DEL(envlist[envc]);
1670 PyMem_DEL(envlist);
1671 fail_1:
1672 PyMem_DEL(argvlist);
1673 Py_XDECREF(vals);
1674 Py_XDECREF(keys);
1675 return res;
1676}
1677#endif /* HAVE_SPAWNV */
1678
1679
Guido van Rossumad0ee831995-03-01 10:34:45 +00001680#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001681static char posix_fork__doc__[] =
1682"fork() -> pid\n\
1683Fork a child process.\n\
1684\n\
1685Return 0 to child process and PID of child to parent process.";
1686
Barry Warsaw53699e91996-12-10 23:23:01 +00001687static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001688posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001689 PyObject *self;
1690 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001691{
1692 int pid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001693 if (!PyArg_ParseTuple(args, ":fork"))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001694 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001695 pid = fork();
1696 if (pid == -1)
1697 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001698 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001699 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001700}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001701#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001702
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001703
Guido van Rossumad0ee831995-03-01 10:34:45 +00001704#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001705static char posix_getegid__doc__[] =
1706"getegid() -> egid\n\
1707Return the current process's effective group id.";
1708
Barry Warsaw53699e91996-12-10 23:23:01 +00001709static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001710posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001711 PyObject *self;
1712 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001713{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001714 if (!PyArg_ParseTuple(args, ":getegid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001715 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001716 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001717}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001718#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001719
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001720
Guido van Rossumad0ee831995-03-01 10:34:45 +00001721#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001722static char posix_geteuid__doc__[] =
1723"geteuid() -> euid\n\
1724Return the current process's effective user id.";
1725
Barry Warsaw53699e91996-12-10 23:23:01 +00001726static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001727posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001728 PyObject *self;
1729 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001730{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001731 if (!PyArg_ParseTuple(args, ":geteuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001732 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001733 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001734}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001735#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001736
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001737
Guido van Rossumad0ee831995-03-01 10:34:45 +00001738#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001739static char posix_getgid__doc__[] =
1740"getgid() -> gid\n\
1741Return the current process's group id.";
1742
Barry Warsaw53699e91996-12-10 23:23:01 +00001743static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001744posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001745 PyObject *self;
1746 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001747{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001748 if (!PyArg_ParseTuple(args, ":getgid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001749 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001750 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001751}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001752#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001753
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001754
1755static char posix_getpid__doc__[] =
1756"getpid() -> pid\n\
1757Return the current process id";
1758
Barry Warsaw53699e91996-12-10 23:23:01 +00001759static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001760posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001761 PyObject *self;
1762 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001763{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001764 if (!PyArg_ParseTuple(args, ":getpid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001765 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001766 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001767}
1768
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001769
Fred Drakec9680921999-12-13 16:37:25 +00001770#ifdef HAVE_GETGROUPS
1771static char posix_getgroups__doc__[] = "\
1772getgroups() -> list of group IDs\n\
1773Return list of supplemental group IDs for the process.";
1774
1775static PyObject *
1776posix_getgroups(self, args)
1777 PyObject *self;
1778 PyObject *args;
1779{
1780 PyObject *result = NULL;
1781
1782 if (PyArg_ParseTuple(args, ":getgroups")) {
1783#ifdef NGROUPS_MAX
1784#define MAX_GROUPS NGROUPS_MAX
1785#else
1786 /* defined to be 16 on Solaris7, so this should be a small number */
1787#define MAX_GROUPS 64
1788#endif
1789 gid_t grouplist[MAX_GROUPS];
1790 int n;
1791
1792 n = getgroups(MAX_GROUPS, grouplist);
1793 if (n < 0)
1794 posix_error();
1795 else {
1796 result = PyList_New(n);
1797 if (result != NULL) {
1798 PyObject *o;
1799 int i;
1800 for (i = 0; i < n; ++i) {
1801 o = PyInt_FromLong((long)grouplist[i]);
1802 if (o == NULL) {
1803 Py_DECREF(result);
1804 result = NULL;
1805 break;
1806 }
1807 PyList_SET_ITEM(result, i, o);
1808 }
1809 }
1810 }
1811 }
1812 return result;
1813}
1814#endif
1815
Guido van Rossumb6775db1994-08-01 11:34:53 +00001816#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001817static char posix_getpgrp__doc__[] =
1818"getpgrp() -> pgrp\n\
1819Return the current process group id.";
1820
Barry Warsaw53699e91996-12-10 23:23:01 +00001821static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001822posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001823 PyObject *self;
1824 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001825{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001826 if (!PyArg_ParseTuple(args, ":getpgrp"))
Guido van Rossum04814471991-06-04 20:23:49 +00001827 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001828#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001829 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001830#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001831 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001832#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001833}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001834#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001836
Guido van Rossumb6775db1994-08-01 11:34:53 +00001837#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001838static char posix_setpgrp__doc__[] =
1839"setpgrp() -> None\n\
1840Make this process a session leader.";
1841
Barry Warsaw53699e91996-12-10 23:23:01 +00001842static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001843posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001844 PyObject *self;
1845 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001846{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001847 if (!PyArg_ParseTuple(args, ":setpgrp"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001848 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001849#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001850 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001851#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001852 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001853#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001854 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001855 Py_INCREF(Py_None);
1856 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001857}
1858
Guido van Rossumb6775db1994-08-01 11:34:53 +00001859#endif /* HAVE_SETPGRP */
1860
Guido van Rossumad0ee831995-03-01 10:34:45 +00001861#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001862static char posix_getppid__doc__[] =
1863"getppid() -> ppid\n\
1864Return the parent's process id.";
1865
Barry Warsaw53699e91996-12-10 23:23:01 +00001866static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001867posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001868 PyObject *self;
1869 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001870{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001871 if (!PyArg_ParseTuple(args, ":getppid"))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001872 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001873 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001874}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001875#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001876
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001877
Fred Drake12c6e2d1999-12-14 21:25:03 +00001878#ifdef HAVE_GETLOGIN
1879static char posix_getlogin__doc__[] = "\
1880getlogin() -> string\n\
1881Return the actual login name.";
1882
1883static PyObject *
1884posix_getlogin(self, args)
1885 PyObject *self;
1886 PyObject *args;
1887{
1888 PyObject *result = NULL;
1889
1890 if (PyArg_ParseTuple(args, ":getlogin")) {
1891 char *name = getlogin();
1892
1893 if (name == NULL)
1894 posix_error();
1895 else
1896 result = PyString_FromString(name);
1897 }
1898 return result;
1899}
1900#endif
1901
Guido van Rossumad0ee831995-03-01 10:34:45 +00001902#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001903static char posix_getuid__doc__[] =
1904"getuid() -> uid\n\
1905Return the current process's user id.";
1906
Barry Warsaw53699e91996-12-10 23:23:01 +00001907static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001908posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001909 PyObject *self;
1910 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001911{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001912 if (!PyArg_ParseTuple(args, ":getuid"))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001913 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001914 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001915}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001916#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001917
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001918
Guido van Rossumad0ee831995-03-01 10:34:45 +00001919#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001920static char posix_kill__doc__[] =
1921"kill(pid, sig) -> None\n\
1922Kill a process with a signal.";
1923
Barry Warsaw53699e91996-12-10 23:23:01 +00001924static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001925posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001926 PyObject *self;
1927 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001928{
1929 int pid, sig;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001930 if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001931 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001932#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001933 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1934 APIRET rc;
1935 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001936 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001937
1938 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1939 APIRET rc;
1940 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001941 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001942
1943 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001944 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001945#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001946 if (kill(pid, sig) == -1)
1947 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001948#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001949 Py_INCREF(Py_None);
1950 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001951}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001952#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001953
Guido van Rossumc0125471996-06-28 18:55:32 +00001954#ifdef HAVE_PLOCK
1955
1956#ifdef HAVE_SYS_LOCK_H
1957#include <sys/lock.h>
1958#endif
1959
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001960static char posix_plock__doc__[] =
1961"plock(op) -> None\n\
1962Lock program segments into memory.";
1963
Barry Warsaw53699e91996-12-10 23:23:01 +00001964static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001965posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001966 PyObject *self;
1967 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001968{
1969 int op;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001970 if (!PyArg_ParseTuple(args, "i:plock", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001971 return NULL;
1972 if (plock(op) == -1)
1973 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001974 Py_INCREF(Py_None);
1975 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001976}
1977#endif
1978
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001980#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001981static char posix_popen__doc__[] =
1982"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1983Open a pipe to/from a command returning a file object.";
1984
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001985#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001986static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001987async_system(const char *command)
1988{
1989 char *p, errormsg[256], args[1024];
1990 RESULTCODES rcodes;
1991 APIRET rc;
1992 char *shell = getenv("COMSPEC");
1993 if (!shell)
1994 shell = "cmd";
1995
1996 strcpy(args, shell);
1997 p = &args[ strlen(args)+1 ];
1998 strcpy(p, "/c ");
1999 strcat(p, command);
2000 p += strlen(p) + 1;
2001 *p = '\0';
2002
2003 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002004 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002005 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002006 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002007 &rcodes, shell);
2008 return rc;
2009}
2010
Guido van Rossumd48f2521997-12-05 22:19:34 +00002011static FILE *
2012popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002013{
2014 HFILE rhan, whan;
2015 FILE *retfd = NULL;
2016 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
2017
Guido van Rossumd48f2521997-12-05 22:19:34 +00002018 if (rc != NO_ERROR) {
2019 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002020 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002021 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002022
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002023 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
2024 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002025
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002026 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2027 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002028
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002029 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
2030 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002031
2032 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002033 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002034 }
2035
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002036 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
2037 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002038
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002039 close(oldfd); /* And Close Saved STDOUT Handle */
2040 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002041
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002042 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
2043 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002044
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002045 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
2046 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002047
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002048 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
2049 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002050
2051 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002052 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002053 }
2054
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002055 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
2056 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002057
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002058 close(oldfd); /* And Close Saved STDIN Handle */
2059 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002060
Guido van Rossumd48f2521997-12-05 22:19:34 +00002061 } else {
2062 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002063 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00002064 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002065}
2066
2067static PyObject *
2068posix_popen(self, args)
2069 PyObject *self;
2070 PyObject *args;
2071{
2072 char *name;
2073 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00002074 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002075 FILE *fp;
2076 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002077 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002078 return NULL;
2079 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00002080 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002081 Py_END_ALLOW_THREADS
2082 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002083 return os2_error(err);
2084
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002085 f = PyFile_FromFile(fp, name, mode, fclose);
2086 if (f != NULL)
2087 PyFile_SetBufSize(f, bufsize);
2088 return f;
2089}
2090
2091#else
Barry Warsaw53699e91996-12-10 23:23:01 +00002092static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00002093posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 PyObject *self;
2095 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00002096{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002097 char *name;
2098 char *mode = "r";
2099 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00002100 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002101 PyObject *f;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002102 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00002103 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002105 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002106 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00002107 if (fp == NULL)
2108 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002109 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002110 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002111 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002112 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00002113}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002114#endif
2115
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002116#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00002117
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002118
Guido van Rossumb6775db1994-08-01 11:34:53 +00002119#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002120static char posix_setuid__doc__[] =
2121"setuid(uid) -> None\n\
2122Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00002123static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002124posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002125 PyObject *self;
2126 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002127{
2128 int uid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002129 if (!PyArg_ParseTuple(args, "i:setuid", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002130 return NULL;
2131 if (setuid(uid) < 0)
2132 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002133 Py_INCREF(Py_None);
2134 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002135}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002136#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002137
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002138
Guido van Rossumb6775db1994-08-01 11:34:53 +00002139#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002140static char posix_setgid__doc__[] =
2141"setgid(gid) -> None\n\
2142Set the current process's group id.";
2143
Barry Warsaw53699e91996-12-10 23:23:01 +00002144static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002145posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002146 PyObject *self;
2147 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002148{
2149 int gid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002150 if (!PyArg_ParseTuple(args, "i:setgid", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002151 return NULL;
2152 if (setgid(gid) < 0)
2153 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002154 Py_INCREF(Py_None);
2155 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002156}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002157#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002158
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002159
Guido van Rossumb6775db1994-08-01 11:34:53 +00002160#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002161static char posix_waitpid__doc__[] =
2162"waitpid(pid, options) -> (pid, status)\n\
2163Wait for completion of a give child process.";
2164
Barry Warsaw53699e91996-12-10 23:23:01 +00002165static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002166posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002167 PyObject *self;
2168 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002169{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002170 int pid, options;
2171#ifdef UNION_WAIT
2172 union wait status;
2173#define status_i (status.w_status)
2174#else
2175 int status;
2176#define status_i status
2177#endif
2178 status_i = 0;
2179
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002180 if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002181 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002182 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002183#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002184 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002185#else
2186 pid = waitpid(pid, &status, options);
2187#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002188 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002189 if (pid == -1)
2190 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002191 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002192 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002193}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002194#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002195
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002196
Guido van Rossumad0ee831995-03-01 10:34:45 +00002197#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002198static char posix_wait__doc__[] =
2199"wait() -> (pid, status)\n\
2200Wait for completion of a child process.";
2201
Barry Warsaw53699e91996-12-10 23:23:01 +00002202static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002203posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002204 PyObject *self;
2205 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002206{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002207 int pid;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002208#ifdef UNION_WAIT
2209 union wait status;
2210#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002211#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002212 int status;
2213#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002214#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002215 if (!PyArg_ParseTuple(args, ":wait"))
2216 return NULL;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002217 status_i = 0;
2218 Py_BEGIN_ALLOW_THREADS
2219 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002220 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002221 if (pid == -1)
2222 return posix_error();
2223 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002224 return Py_BuildValue("ii", pid, status_i);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002225#undef status_i
Guido van Rossum85e3b011991-06-03 12:42:10 +00002226}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002227#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002228
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002229
2230static char posix_lstat__doc__[] =
2231"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2232Like stat(path), but do not follow symbolic links.";
2233
Barry Warsaw53699e91996-12-10 23:23:01 +00002234static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002235posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002236 PyObject *self;
2237 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002238{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002239#ifdef HAVE_LSTAT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002240 return posix_do_stat(self, args, "s:lstat", lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002241#else /* !HAVE_LSTAT */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002242 return posix_do_stat(self, args, "s:lstat", stat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002243#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002244}
2245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002246
Guido van Rossumb6775db1994-08-01 11:34:53 +00002247#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002248static char posix_readlink__doc__[] =
2249"readlink(path) -> path\n\
2250Return a string representing the path to which the symbolic link points.";
2251
Barry Warsaw53699e91996-12-10 23:23:01 +00002252static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002253posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002254 PyObject *self;
2255 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002256{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002257 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002258 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002259 int n;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002260 if (!PyArg_ParseTuple(args, "s:readlink", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002262 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002263 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002265 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002266 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002267 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002268}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002269#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002270
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002271
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002273static char posix_symlink__doc__[] =
2274"symlink(src, dst) -> None\n\
2275Create a symbolic link.";
2276
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002277static PyObject *
2278posix_symlink(self, args)
2279 PyObject *self;
2280 PyObject *args;
2281{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002282 return posix_2str(args, "ss:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002283}
2284#endif /* HAVE_SYMLINK */
2285
2286
2287#ifdef HAVE_TIMES
2288#ifndef HZ
2289#define HZ 60 /* Universal constant :-) */
2290#endif /* HZ */
2291
Guido van Rossumd48f2521997-12-05 22:19:34 +00002292#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2293static long
2294system_uptime()
2295{
2296 ULONG value = 0;
2297
2298 Py_BEGIN_ALLOW_THREADS
2299 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2300 Py_END_ALLOW_THREADS
2301
2302 return value;
2303}
2304
2305static PyObject *
2306posix_times(self, args)
2307 PyObject *self;
2308 PyObject *args;
2309{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002310 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossumd48f2521997-12-05 22:19:34 +00002311 return NULL;
2312
2313 /* Currently Only Uptime is Provided -- Others Later */
2314 return Py_BuildValue("ddddd",
2315 (double)0 /* t.tms_utime / HZ */,
2316 (double)0 /* t.tms_stime / HZ */,
2317 (double)0 /* t.tms_cutime / HZ */,
2318 (double)0 /* t.tms_cstime / HZ */,
2319 (double)system_uptime() / 1000);
2320}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002321#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002322static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002323posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002324 PyObject *self;
2325 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002326{
2327 struct tms t;
2328 clock_t c;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002329 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002330 return NULL;
2331 errno = 0;
2332 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002333 if (c == (clock_t) -1)
2334 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002335 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002336 (double)t.tms_utime / HZ,
2337 (double)t.tms_stime / HZ,
2338 (double)t.tms_cutime / HZ,
2339 (double)t.tms_cstime / HZ,
2340 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002341}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002342#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002343#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002344
2345
Guido van Rossum87755a21996-09-07 00:59:43 +00002346#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002347#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002348static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002349posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002350 PyObject *self;
2351 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002352{
2353 FILETIME create, exit, kernel, user;
2354 HANDLE hProc;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002355 if (!PyArg_ParseTuple(args, ":times"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002356 return NULL;
2357 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002358 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
2359 /* The fields of a FILETIME structure are the hi and lo part
2360 of a 64-bit value expressed in 100 nanosecond units.
2361 1e7 is one second in such units; 1e-7 the inverse.
2362 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
2363 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002364 return Py_BuildValue(
2365 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002366 (double)(kernel.dwHighDateTime*429.4967296 +
2367 kernel.dwLowDateTime*1e-7),
2368 (double)(user.dwHighDateTime*429.4967296 +
2369 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00002370 (double)0,
2371 (double)0,
2372 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002373}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002374#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002375
2376#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002377static char posix_times__doc__[] =
2378"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2379Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002380#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002381
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002382
Guido van Rossumb6775db1994-08-01 11:34:53 +00002383#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002384static char posix_setsid__doc__[] =
2385"setsid() -> None\n\
2386Call the system call setsid().";
2387
Barry Warsaw53699e91996-12-10 23:23:01 +00002388static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002389posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002390 PyObject *self;
2391 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002392{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002393 if (!PyArg_ParseTuple(args, ":setsid"))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002394 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002395 if (setsid() < 0)
2396 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002397 Py_INCREF(Py_None);
2398 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002399}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002400#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002401
Guido van Rossumb6775db1994-08-01 11:34:53 +00002402#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002403static char posix_setpgid__doc__[] =
2404"setpgid(pid, pgrp) -> None\n\
2405Call the system call setpgid().";
2406
Barry Warsaw53699e91996-12-10 23:23:01 +00002407static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002408posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002409 PyObject *self;
2410 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002411{
2412 int pid, pgrp;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002413 if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002414 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002415 if (setpgid(pid, pgrp) < 0)
2416 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002417 Py_INCREF(Py_None);
2418 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002419}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002420#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002421
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002422
Guido van Rossumb6775db1994-08-01 11:34:53 +00002423#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002424static char posix_tcgetpgrp__doc__[] =
2425"tcgetpgrp(fd) -> pgid\n\
2426Return the process group associated with the terminal given by a fd.";
2427
Barry Warsaw53699e91996-12-10 23:23:01 +00002428static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002429posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002430 PyObject *self;
2431 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002432{
2433 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002434 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002435 return NULL;
2436 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002437 if (pgid < 0)
2438 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002439 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002440}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002441#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002442
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002443
Guido van Rossumb6775db1994-08-01 11:34:53 +00002444#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002445static char posix_tcsetpgrp__doc__[] =
2446"tcsetpgrp(fd, pgid) -> None\n\
2447Set the process group associated with the terminal given by a fd.";
2448
Barry Warsaw53699e91996-12-10 23:23:01 +00002449static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002450posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002451 PyObject *self;
2452 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002453{
2454 int fd, pgid;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002455 if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002456 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002457 if (tcsetpgrp(fd, pgid) < 0)
2458 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002459 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002460 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002461}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002462#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002463
Guido van Rossum687dd131993-05-17 08:34:16 +00002464/* Functions acting on file descriptors */
2465
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002466static char posix_open__doc__[] =
2467"open(filename, flag [, mode=0777]) -> fd\n\
2468Open a file (for low level IO).";
2469
Barry Warsaw53699e91996-12-10 23:23:01 +00002470static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002471posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002472 PyObject *self;
2473 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002474{
2475 char *file;
2476 int flag;
2477 int mode = 0777;
2478 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002479 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2480 return NULL;
2481
Barry Warsaw53699e91996-12-10 23:23:01 +00002482 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002483 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002484 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002485 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002486 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002487 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002488}
2489
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002490
2491static char posix_close__doc__[] =
2492"close(fd) -> None\n\
2493Close a file descriptor (for low level IO).";
2494
Barry Warsaw53699e91996-12-10 23:23:01 +00002495static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002496posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002497 PyObject *self;
2498 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002499{
2500 int fd, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002501 if (!PyArg_ParseTuple(args, "i:close", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002502 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002503 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002504 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002505 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002506 if (res < 0)
2507 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002508 Py_INCREF(Py_None);
2509 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002510}
2511
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002512
2513static char posix_dup__doc__[] =
2514"dup(fd) -> fd2\n\
2515Return a duplicate of a file descriptor.";
2516
Barry Warsaw53699e91996-12-10 23:23:01 +00002517static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002518posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002519 PyObject *self;
2520 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002521{
2522 int fd;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002523 if (!PyArg_ParseTuple(args, "i:dup", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002524 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002525 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002526 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002527 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002528 if (fd < 0)
2529 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002530 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002531}
2532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533
2534static char posix_dup2__doc__[] =
2535"dup2(fd, fd2) -> None\n\
2536Duplicate file descriptor.";
2537
Barry Warsaw53699e91996-12-10 23:23:01 +00002538static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002539posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002540 PyObject *self;
2541 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002542{
2543 int fd, fd2, res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002544 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002545 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002546 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002547 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002548 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002549 if (res < 0)
2550 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002551 Py_INCREF(Py_None);
2552 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002553}
2554
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002555
2556static char posix_lseek__doc__[] =
2557"lseek(fd, pos, how) -> newpos\n\
2558Set the current position of a file descriptor.";
2559
Barry Warsaw53699e91996-12-10 23:23:01 +00002560static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002561posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002562 PyObject *self;
2563 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002564{
2565 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002566 off_t pos, res;
2567 PyObject *posobj;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002568 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002569 return NULL;
2570#ifdef SEEK_SET
2571 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2572 switch (how) {
2573 case 0: how = SEEK_SET; break;
2574 case 1: how = SEEK_CUR; break;
2575 case 2: how = SEEK_END; break;
2576 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002577#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002578
2579#if !defined(HAVE_LARGEFILE_SUPPORT)
2580 pos = PyInt_AsLong(posobj);
2581#else
2582 pos = PyLong_Check(posobj) ?
2583 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2584#endif
2585 if (PyErr_Occurred())
2586 return NULL;
2587
Barry Warsaw53699e91996-12-10 23:23:01 +00002588 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002589 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002590 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002591 if (res < 0)
2592 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002593
2594#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002595 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002596#else
2597 return PyLong_FromLongLong(res);
2598#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002599}
2600
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002601
2602static char posix_read__doc__[] =
2603"read(fd, buffersize) -> string\n\
2604Read a file descriptor.";
2605
Barry Warsaw53699e91996-12-10 23:23:01 +00002606static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002607posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002608 PyObject *self;
2609 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002610{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002611 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002612 PyObject *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002613 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002614 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002615 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002616 if (buffer == NULL)
2617 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002618 Py_BEGIN_ALLOW_THREADS
2619 n = read(fd, PyString_AsString(buffer), size);
2620 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002621 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002622 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002623 return posix_error();
2624 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002625 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002626 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002627 return buffer;
2628}
2629
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630
2631static char posix_write__doc__[] =
2632"write(fd, string) -> byteswritten\n\
2633Write a string to a file descriptor.";
2634
Barry Warsaw53699e91996-12-10 23:23:01 +00002635static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002636posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002637 PyObject *self;
2638 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002639{
2640 int fd, size;
2641 char *buffer;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002642 if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002643 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002644 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002645 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002646 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002647 if (size < 0)
2648 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002649 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002650}
2651
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002652
2653static char posix_fstat__doc__[]=
2654"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2655Like stat(), but for an open file descriptor.";
2656
Barry Warsaw53699e91996-12-10 23:23:01 +00002657static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002658posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002659 PyObject *self;
2660 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002661{
2662 int fd;
2663 struct stat st;
2664 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002665 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002666 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002667 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002668 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002669 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002670 if (res != 0)
2671 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002672#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002673 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002674 (long)st.st_mode,
2675 (long)st.st_ino,
2676 (long)st.st_dev,
2677 (long)st.st_nlink,
2678 (long)st.st_uid,
2679 (long)st.st_gid,
2680 (long)st.st_size,
2681 (long)st.st_atime,
2682 (long)st.st_mtime,
2683 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002684#else
2685 return Py_BuildValue("(lLllllLlll)",
2686 (long)st.st_mode,
2687 (LONG_LONG)st.st_ino,
2688 (long)st.st_dev,
2689 (long)st.st_nlink,
2690 (long)st.st_uid,
2691 (long)st.st_gid,
2692 (LONG_LONG)st.st_size,
2693 (long)st.st_atime,
2694 (long)st.st_mtime,
2695 (long)st.st_ctime);
2696#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002697}
2698
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002699
2700static char posix_fdopen__doc__[] =
2701"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2702Return an open file object connected to a file descriptor.";
2703
Barry Warsaw53699e91996-12-10 23:23:01 +00002704static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002705posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002706 PyObject *self;
2707 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002708{
Barry Warsaw53699e91996-12-10 23:23:01 +00002709 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002710 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002711 char *mode = "r";
2712 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002713 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002714 PyObject *f;
2715 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002716 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002717
Barry Warsaw53699e91996-12-10 23:23:01 +00002718 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002719 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002720 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002721 if (fp == NULL)
2722 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002723 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002724 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002725 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002726 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002727}
2728
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002729
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002730#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002731static char posix_pipe__doc__[] =
2732"pipe() -> (read_end, write_end)\n\
2733Create a pipe.";
2734
Barry Warsaw53699e91996-12-10 23:23:01 +00002735static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002736posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002737 PyObject *self;
2738 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002739{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002740#if defined(PYOS_OS2)
2741 HFILE read, write;
2742 APIRET rc;
2743
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002744 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002745 return NULL;
2746
2747 Py_BEGIN_ALLOW_THREADS
2748 rc = DosCreatePipe( &read, &write, 4096);
2749 Py_END_ALLOW_THREADS
2750 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002751 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002752
2753 return Py_BuildValue("(ii)", read, write);
2754#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002755#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002756 int fds[2];
2757 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002758 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum687dd131993-05-17 08:34:16 +00002759 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002760 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002761 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002762 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002763 if (res != 0)
2764 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002765 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002766#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002767 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002768 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002769 BOOL ok;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002770 if (!PyArg_ParseTuple(args, ":pipe"))
Guido van Rossum794d8131994-08-23 13:48:48 +00002771 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002772 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002773 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002774 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002775 if (!ok)
2776 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002777 read_fd = _open_osfhandle((long)read, 0);
2778 write_fd = _open_osfhandle((long)write, 1);
2779 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002780#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002781#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002782}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002783#endif /* HAVE_PIPE */
2784
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002785
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002786#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002787static char posix_mkfifo__doc__[] =
2788"mkfifo(file, [, mode=0666]) -> None\n\
2789Create a FIFO (a POSIX named pipe).";
2790
Barry Warsaw53699e91996-12-10 23:23:01 +00002791static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002792posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002793 PyObject *self;
2794 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002795{
2796 char *file;
2797 int mode = 0666;
2798 int res;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002799 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002800 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002801 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002802 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002803 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002804 if (res < 0)
2805 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002806 Py_INCREF(Py_None);
2807 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002808}
2809#endif
2810
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002811
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002812#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002813static char posix_ftruncate__doc__[] =
2814"ftruncate(fd, length) -> None\n\
2815Truncate a file to a specified length.";
2816
Barry Warsaw53699e91996-12-10 23:23:01 +00002817static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002818posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002819 PyObject *self; /* Not used */
2820 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002821{
2822 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002823 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002824 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002825 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002826
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002827 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
Guido van Rossum94f6f721999-01-06 18:42:14 +00002828 return NULL;
2829
2830#if !defined(HAVE_LARGEFILE_SUPPORT)
2831 length = PyInt_AsLong(lenobj);
2832#else
2833 length = PyLong_Check(lenobj) ?
2834 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2835#endif
2836 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002837 return NULL;
2838
Barry Warsaw53699e91996-12-10 23:23:01 +00002839 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002840 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002841 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002842 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002843 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002844 return NULL;
2845 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002846 Py_INCREF(Py_None);
2847 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002848}
2849#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002850
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002851#ifdef NeXT
2852#define HAVE_PUTENV
2853/* Steve Spicklemire got this putenv from NeXTAnswers */
2854static int
2855putenv(char *newval)
2856{
2857 extern char **environ;
2858
2859 static int firstTime = 1;
2860 char **ep;
2861 char *cp;
2862 int esiz;
2863 char *np;
2864
2865 if (!(np = strchr(newval, '=')))
2866 return 1;
2867 *np = '\0';
2868
2869 /* look it up */
2870 for (ep=environ ; *ep ; ep++)
2871 {
2872 /* this should always be true... */
2873 if (cp = strchr(*ep, '='))
2874 {
2875 *cp = '\0';
2876 if (!strcmp(*ep, newval))
2877 {
2878 /* got it! */
2879 *cp = '=';
2880 break;
2881 }
2882 *cp = '=';
2883 }
2884 else
2885 {
2886 *np = '=';
2887 return 1;
2888 }
2889 }
2890
2891 *np = '=';
2892 if (*ep)
2893 {
2894 /* the string was already there:
2895 just replace it with the new one */
2896 *ep = newval;
2897 return 0;
2898 }
2899
2900 /* expand environ by one */
2901 for (esiz=2, ep=environ ; *ep ; ep++)
2902 esiz++;
2903 if (firstTime)
2904 {
2905 char **epp;
2906 char **newenv;
2907 if (!(newenv = malloc(esiz * sizeof(char *))))
2908 return 1;
2909
2910 for (ep=environ, epp=newenv ; *ep ;)
2911 *epp++ = *ep++;
2912 *epp++ = newval;
2913 *epp = (char *) 0;
2914 environ = newenv;
2915 }
2916 else
2917 {
2918 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2919 return 1;
2920 environ[esiz - 2] = newval;
2921 environ[esiz - 1] = (char *) 0;
2922 firstTime = 0;
2923 }
2924
2925 return 0;
2926}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002927#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002929
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002930#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002931static char posix_putenv__doc__[] =
2932"putenv(key, value) -> None\n\
2933Change or add an environment variable.";
2934
Guido van Rossumbcc20741998-08-04 22:53:56 +00002935#ifdef __BEOS__
2936/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2937int putenv( const char *str );
2938#endif
2939
Fred Drake762e2061999-08-26 17:23:54 +00002940/* Save putenv() parameters as values here, so we can collect them when they
2941 * get re-set with another call for the same key. */
2942static PyObject *posix_putenv_garbage;
2943
Barry Warsaw53699e91996-12-10 23:23:01 +00002944static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002945posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002946 PyObject *self;
2947 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002948{
2949 char *s1, *s2;
2950 char *new;
Fred Drake762e2061999-08-26 17:23:54 +00002951 PyObject *newstr;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002952
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002953 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002954 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002955
2956#if defined(PYOS_OS2)
2957 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2958 APIRET rc;
2959
2960 if (strlen(s2) == 0) /* If New Value is an Empty String */
2961 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2962
2963 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2964 if (rc != NO_ERROR)
2965 return os2_error(rc);
2966
2967 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2968 APIRET rc;
2969
2970 if (strlen(s2) == 0) /* If New Value is an Empty String */
2971 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2972
2973 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2974 if (rc != NO_ERROR)
2975 return os2_error(rc);
2976 } else {
2977#endif
2978
Fred Drake762e2061999-08-26 17:23:54 +00002979 /* XXX This can leak memory -- not easy to fix :-( */
2980 newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2);
2981 if (newstr == NULL)
2982 return PyErr_NoMemory();
2983 new = PyString_AS_STRING(newstr);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002984 (void) sprintf(new, "%s=%s", s1, s2);
2985 if (putenv(new)) {
2986 posix_error();
2987 return NULL;
2988 }
Fred Drake762e2061999-08-26 17:23:54 +00002989 /* Install the first arg and newstr in posix_putenv_garbage;
2990 * this will cause previous value to be collected. This has to
2991 * happen after the real putenv() call because the old value
2992 * was still accessible until then. */
2993 if (PyDict_SetItem(posix_putenv_garbage,
2994 PyTuple_GET_ITEM(args, 0), newstr)) {
2995 /* really not much we can do; just leak */
2996 PyErr_Clear();
2997 }
2998 else {
2999 Py_DECREF(newstr);
3000 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00003001
3002#if defined(PYOS_OS2)
3003 }
3004#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00003005 Py_INCREF(Py_None);
3006 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003007}
Guido van Rossumb6a47161997-09-15 22:54:34 +00003008#endif /* putenv */
3009
3010#ifdef HAVE_STRERROR
3011static char posix_strerror__doc__[] =
3012"strerror(code) -> string\n\
3013Translate an error code to a message string.";
3014
3015PyObject *
3016posix_strerror(self, args)
3017 PyObject *self;
3018 PyObject *args;
3019{
3020 int code;
3021 char *message;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003022 if (!PyArg_ParseTuple(args, "i:strerror", &code))
Guido van Rossumb6a47161997-09-15 22:54:34 +00003023 return NULL;
3024 message = strerror(code);
3025 if (message == NULL) {
3026 PyErr_SetString(PyExc_ValueError,
3027 "strerror code out of range");
3028 return NULL;
3029 }
3030 return PyString_FromString(message);
3031}
3032#endif /* strerror */
3033
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003034
Guido van Rossumc9641791998-08-04 15:26:23 +00003035#ifdef HAVE_SYS_WAIT_H
3036
3037#ifdef WIFSTOPPED
3038static char posix_WIFSTOPPED__doc__[] =
3039"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003040Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003041
3042static PyObject *
3043posix_WIFSTOPPED(self, args)
3044 PyObject *self;
3045 PyObject *args;
3046{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003047#ifdef UNION_WAIT
3048 union wait status;
3049#define status_i (status.w_status)
3050#else
3051 int status;
3052#define status_i status
3053#endif
3054 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003055
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003056 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003057 {
3058 return NULL;
3059 }
3060
3061 return Py_BuildValue("i", WIFSTOPPED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003062#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003063}
3064#endif /* WIFSTOPPED */
3065
3066#ifdef WIFSIGNALED
3067static char posix_WIFSIGNALED__doc__[] =
3068"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00003069Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003070
3071static PyObject *
3072posix_WIFSIGNALED(self, args)
3073 PyObject *self;
3074 PyObject *args;
3075{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003076#ifdef UNION_WAIT
3077 union wait status;
3078#define status_i (status.w_status)
3079#else
3080 int status;
3081#define status_i status
3082#endif
3083 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003084
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003085 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003086 {
3087 return NULL;
3088 }
3089
3090 return Py_BuildValue("i", WIFSIGNALED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003091#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003092}
3093#endif /* WIFSIGNALED */
3094
3095#ifdef WIFEXITED
3096static char posix_WIFEXITED__doc__[] =
3097"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003098Return true if the process returning 'status' exited using the exit()\n\
3099system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003100
3101static PyObject *
3102posix_WIFEXITED(self, args)
3103 PyObject *self;
3104 PyObject *args;
3105{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003106#ifdef UNION_WAIT
3107 union wait status;
3108#define status_i (status.w_status)
3109#else
3110 int status;
3111#define status_i status
3112#endif
3113 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003114
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003115 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003116 {
3117 return NULL;
3118 }
3119
3120 return Py_BuildValue("i", WIFEXITED(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003121#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003122}
3123#endif /* WIFEXITED */
3124
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003125#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00003126static char posix_WEXITSTATUS__doc__[] =
3127"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003128Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003129
3130static PyObject *
3131posix_WEXITSTATUS(self, args)
3132 PyObject *self;
3133 PyObject *args;
3134{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003135#ifdef UNION_WAIT
3136 union wait status;
3137#define status_i (status.w_status)
3138#else
3139 int status;
3140#define status_i status
3141#endif
3142 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003143
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003144 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003145 {
3146 return NULL;
3147 }
3148
3149 return Py_BuildValue("i", WEXITSTATUS(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003150#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003151}
3152#endif /* WEXITSTATUS */
3153
3154#ifdef WTERMSIG
3155static char posix_WTERMSIG__doc__[] =
3156"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003157Return the signal that terminated the process that provided the 'status'\n\
3158value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003159
3160static PyObject *
3161posix_WTERMSIG(self, args)
3162 PyObject *self;
3163 PyObject *args;
3164{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003165#ifdef UNION_WAIT
3166 union wait status;
3167#define status_i (status.w_status)
3168#else
3169 int status;
3170#define status_i status
3171#endif
3172 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003173
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003174 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003175 {
3176 return NULL;
3177 }
3178
3179 return Py_BuildValue("i", WTERMSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003180#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003181}
3182#endif /* WTERMSIG */
3183
3184#ifdef WSTOPSIG
3185static char posix_WSTOPSIG__doc__[] =
3186"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003187Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003188
3189static PyObject *
3190posix_WSTOPSIG(self, args)
3191 PyObject *self;
3192 PyObject *args;
3193{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003194#ifdef UNION_WAIT
3195 union wait status;
3196#define status_i (status.w_status)
3197#else
3198 int status;
3199#define status_i status
3200#endif
3201 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003202
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003203 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003204 {
3205 return NULL;
3206 }
3207
3208 return Py_BuildValue("i", WSTOPSIG(status));
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003209#undef status_i
Guido van Rossumc9641791998-08-04 15:26:23 +00003210}
3211#endif /* WSTOPSIG */
3212
3213#endif /* HAVE_SYS_WAIT_H */
3214
3215
Guido van Rossum94f6f721999-01-06 18:42:14 +00003216#if defined(HAVE_FSTATVFS)
Guido van Rossumd5753e11999-10-19 13:29:23 +00003217#ifdef _SCO_DS
3218/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
3219 needed definitions in sys/statvfs.h */
3220#define _SVID3
3221#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003222#include <sys/statvfs.h>
3223
3224static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003225"fstatvfs(fd) -> \n\
3226 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003227Perform an fstatvfs system call on the given fd.";
3228
3229static PyObject *
3230posix_fstatvfs(self, args)
3231 PyObject *self;
3232 PyObject *args;
3233{
3234 int fd, res;
3235 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003236 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003237 return NULL;
3238 Py_BEGIN_ALLOW_THREADS
3239 res = fstatvfs(fd, &st);
3240 Py_END_ALLOW_THREADS
3241 if (res != 0)
3242 return posix_error();
3243#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003244 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003245 (long) st.f_bsize,
3246 (long) st.f_frsize,
3247 (long) st.f_blocks,
3248 (long) st.f_bfree,
3249 (long) st.f_bavail,
3250 (long) st.f_files,
3251 (long) st.f_ffree,
3252 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003253 (long) st.f_flag,
3254 (long) st.f_namemax);
3255#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003256 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003257 (long) st.f_bsize,
3258 (long) st.f_frsize,
3259 (LONG_LONG) st.f_blocks,
3260 (LONG_LONG) st.f_bfree,
3261 (LONG_LONG) st.f_bavail,
3262 (LONG_LONG) st.f_files,
3263 (LONG_LONG) st.f_ffree,
3264 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003265 (long) st.f_flag,
3266 (long) st.f_namemax);
3267#endif
3268}
3269#endif /* HAVE_FSTATVFS */
3270
3271
3272#if defined(HAVE_STATVFS)
3273#include <sys/statvfs.h>
3274
3275static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003276"statvfs(path) -> \n\
3277 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003278Perform a statvfs system call on the given path.";
3279
3280static PyObject *
3281posix_statvfs(self, args)
3282 PyObject *self;
3283 PyObject *args;
3284{
3285 char *path;
3286 int res;
3287 struct statvfs st;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003288 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
Guido van Rossum94f6f721999-01-06 18:42:14 +00003289 return NULL;
3290 Py_BEGIN_ALLOW_THREADS
3291 res = statvfs(path, &st);
3292 Py_END_ALLOW_THREADS
3293 if (res != 0)
3294 return posix_error_with_filename(path);
3295#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003296 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003297 (long) st.f_bsize,
3298 (long) st.f_frsize,
3299 (long) st.f_blocks,
3300 (long) st.f_bfree,
3301 (long) st.f_bavail,
3302 (long) st.f_files,
3303 (long) st.f_ffree,
3304 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003305 (long) st.f_flag,
3306 (long) st.f_namemax);
3307#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003308 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003309 (long) st.f_bsize,
3310 (long) st.f_frsize,
3311 (LONG_LONG) st.f_blocks,
3312 (LONG_LONG) st.f_bfree,
3313 (LONG_LONG) st.f_bavail,
3314 (LONG_LONG) st.f_files,
3315 (LONG_LONG) st.f_ffree,
3316 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003317 (long) st.f_flag,
3318 (long) st.f_namemax);
3319#endif
3320}
3321#endif /* HAVE_STATVFS */
3322
3323
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003324#ifdef HAVE_TEMPNAM
3325static char posix_tempnam__doc__[] = "\
3326tempnam([dir[, prefix]]) -> string\n\
3327Return a unique name for a temporary file.\n\
3328The directory and a short may be specified as strings; they may be omitted\n\
3329or None if not needed.";
3330
3331static PyObject *
3332posix_tempnam(self, args)
3333 PyObject *self;
3334 PyObject *args;
3335{
3336 PyObject *result = NULL;
3337 char *dir = NULL;
3338 char *pfx = NULL;
3339 char *name;
3340
3341 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
3342 return NULL;
3343 name = tempnam(dir, pfx);
3344 if (name == NULL)
3345 return PyErr_NoMemory();
3346 result = PyString_FromString(name);
3347 free(name);
3348 return result;
3349}
Guido van Rossumd371ff11999-01-25 16:12:23 +00003350#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003351
3352
3353#ifdef HAVE_TMPFILE
3354static char posix_tmpfile__doc__[] = "\
3355tmpfile() -> file object\n\
3356Create a temporary file with no directory entries.";
3357
3358static PyObject *
3359posix_tmpfile(self, args)
3360 PyObject *self;
3361 PyObject *args;
3362{
3363 FILE *fp;
3364
3365 if (!PyArg_ParseTuple(args, ":tmpfile"))
3366 return NULL;
3367 fp = tmpfile();
3368 if (fp == NULL)
3369 return posix_error();
3370 return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose);
3371}
3372#endif
3373
3374
3375#ifdef HAVE_TMPNAM
3376static char posix_tmpnam__doc__[] = "\
3377tmpnam() -> string\n\
3378Return a unique name for a temporary file.";
3379
3380static PyObject *
3381posix_tmpnam(self, args)
3382 PyObject *self;
3383 PyObject *args;
3384{
3385 char buffer[L_tmpnam];
3386 char *name;
3387
3388 if (!PyArg_ParseTuple(args, ":tmpnam"))
3389 return NULL;
Greg Wardb48bc172000-03-01 21:51:56 +00003390#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003391 name = tmpnam_r(buffer);
3392#else
3393 name = tmpnam(buffer);
3394#endif
3395 if (name == NULL) {
3396 PyErr_SetObject(PyExc_OSError,
3397 Py_BuildValue("is", 0,
Greg Wardb48bc172000-03-01 21:51:56 +00003398#ifdef USE_TMPNAM_R
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003399 "unexpected NULL from tmpnam_r"
3400#else
3401 "unexpected NULL from tmpnam"
3402#endif
3403 ));
3404 return NULL;
3405 }
3406 return PyString_FromString(buffer);
3407}
3408#endif
3409
3410
Fred Drakec9680921999-12-13 16:37:25 +00003411/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
3412 * It maps strings representing configuration variable names to
3413 * integer values, allowing those functions to be called with the
3414 * magic names instead of poluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00003415 * rarely-used constants. There are three separate tables that use
3416 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00003417 *
3418 * This code is always included, even if none of the interfaces that
3419 * need it are included. The #if hackery needed to avoid it would be
3420 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00003421 */
3422struct constdef {
3423 char *name;
3424 long value;
3425};
3426
Fred Drake12c6e2d1999-12-14 21:25:03 +00003427static int
3428conv_confname(arg, valuep, table, tablesize)
3429 PyObject *arg;
3430 int *valuep;
3431 struct constdef *table;
3432 size_t tablesize;
3433{
3434 if (PyInt_Check(arg)) {
3435 *valuep = PyInt_AS_LONG(arg);
3436 return 1;
3437 }
3438 if (PyString_Check(arg)) {
3439 /* look up the value in the table using a binary search */
3440 int lo = 0;
3441 int hi = tablesize;
3442 int cmp, mid;
3443 char *confname = PyString_AS_STRING(arg);
3444 while (lo < hi) {
3445 mid = (lo + hi) / 2;
3446 cmp = strcmp(confname, table[mid].name);
3447 if (cmp < 0)
3448 hi = mid;
3449 else if (cmp > 0)
3450 lo = mid + 1;
3451 else {
3452 *valuep = table[mid].value;
3453 return 1;
3454 }
3455 }
3456 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
3457 }
3458 else
3459 PyErr_SetString(PyExc_TypeError,
3460 "configuration names must be strings or integers");
3461 return 0;
3462}
3463
3464
3465#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
3466static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00003467#ifdef _PC_ABI_AIO_XFER_MAX
3468 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
3469#endif
3470#ifdef _PC_ABI_ASYNC_IO
3471 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
3472#endif
Fred Drakec9680921999-12-13 16:37:25 +00003473#ifdef _PC_ASYNC_IO
3474 {"PC_ASYNC_IO", _PC_ASYNC_IO},
3475#endif
3476#ifdef _PC_CHOWN_RESTRICTED
3477 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
3478#endif
3479#ifdef _PC_FILESIZEBITS
3480 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
3481#endif
3482#ifdef _PC_LAST
3483 {"PC_LAST", _PC_LAST},
3484#endif
3485#ifdef _PC_LINK_MAX
3486 {"PC_LINK_MAX", _PC_LINK_MAX},
3487#endif
3488#ifdef _PC_MAX_CANON
3489 {"PC_MAX_CANON", _PC_MAX_CANON},
3490#endif
3491#ifdef _PC_MAX_INPUT
3492 {"PC_MAX_INPUT", _PC_MAX_INPUT},
3493#endif
3494#ifdef _PC_NAME_MAX
3495 {"PC_NAME_MAX", _PC_NAME_MAX},
3496#endif
3497#ifdef _PC_NO_TRUNC
3498 {"PC_NO_TRUNC", _PC_NO_TRUNC},
3499#endif
3500#ifdef _PC_PATH_MAX
3501 {"PC_PATH_MAX", _PC_PATH_MAX},
3502#endif
3503#ifdef _PC_PIPE_BUF
3504 {"PC_PIPE_BUF", _PC_PIPE_BUF},
3505#endif
3506#ifdef _PC_PRIO_IO
3507 {"PC_PRIO_IO", _PC_PRIO_IO},
3508#endif
3509#ifdef _PC_SOCK_MAXBUF
3510 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
3511#endif
3512#ifdef _PC_SYNC_IO
3513 {"PC_SYNC_IO", _PC_SYNC_IO},
3514#endif
3515#ifdef _PC_VDISABLE
3516 {"PC_VDISABLE", _PC_VDISABLE},
3517#endif
3518};
3519
Fred Drakec9680921999-12-13 16:37:25 +00003520static int
3521conv_path_confname(arg, valuep)
3522 PyObject *arg;
3523 int *valuep;
3524{
3525 return conv_confname(arg, valuep, posix_constants_pathconf,
3526 sizeof(posix_constants_pathconf)
3527 / sizeof(struct constdef));
3528}
3529#endif
3530
3531#ifdef HAVE_FPATHCONF
3532static char posix_fpathconf__doc__[] = "\
3533fpathconf(fd, name) -> integer\n\
3534Return the configuration limit name for the file descriptor fd.\n\
3535If there is no limit, return -1.";
3536
3537static PyObject *
3538posix_fpathconf(self, args)
3539 PyObject *self;
3540 PyObject *args;
3541{
3542 PyObject *result = NULL;
3543 int name, fd;
3544
Fred Drake12c6e2d1999-12-14 21:25:03 +00003545 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
3546 conv_path_confname, &name)) {
Fred Drakec9680921999-12-13 16:37:25 +00003547 long limit;
3548
3549 errno = 0;
3550 limit = fpathconf(fd, name);
3551 if (limit == -1 && errno != 0)
3552 posix_error();
3553 else
3554 result = PyInt_FromLong(limit);
3555 }
3556 return result;
3557}
3558#endif
3559
3560
3561#ifdef HAVE_PATHCONF
3562static char posix_pathconf__doc__[] = "\
3563pathconf(path, name) -> integer\n\
3564Return the configuration limit name for the file or directory path.\n\
3565If there is no limit, return -1.";
3566
3567static PyObject *
3568posix_pathconf(self, args)
3569 PyObject *self;
3570 PyObject *args;
3571{
3572 PyObject *result = NULL;
3573 int name;
3574 char *path;
3575
3576 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
3577 conv_path_confname, &name)) {
3578 long limit;
3579
3580 errno = 0;
3581 limit = pathconf(path, name);
Fred Drake12c6e2d1999-12-14 21:25:03 +00003582 if (limit == -1 && errno != 0) {
Fred Drakec9680921999-12-13 16:37:25 +00003583 if (errno == EINVAL)
3584 /* could be a path or name problem */
3585 posix_error();
3586 else
3587 posix_error_with_filename(path);
Fred Drake12c6e2d1999-12-14 21:25:03 +00003588 }
Fred Drakec9680921999-12-13 16:37:25 +00003589 else
3590 result = PyInt_FromLong(limit);
3591 }
3592 return result;
3593}
3594#endif
3595
3596#ifdef HAVE_CONFSTR
3597static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00003598#ifdef _CS_ARCHITECTURE
3599 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
3600#endif
3601#ifdef _CS_HOSTNAME
3602 {"CS_HOSTNAME", _CS_HOSTNAME},
3603#endif
3604#ifdef _CS_HW_PROVIDER
3605 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
3606#endif
3607#ifdef _CS_HW_SERIAL
3608 {"CS_HW_SERIAL", _CS_HW_SERIAL},
3609#endif
3610#ifdef _CS_INITTAB_NAME
3611 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
3612#endif
Fred Drakec9680921999-12-13 16:37:25 +00003613#ifdef _CS_LFS64_CFLAGS
3614 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
3615#endif
3616#ifdef _CS_LFS64_LDFLAGS
3617 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
3618#endif
3619#ifdef _CS_LFS64_LIBS
3620 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
3621#endif
3622#ifdef _CS_LFS64_LINTFLAGS
3623 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
3624#endif
3625#ifdef _CS_LFS_CFLAGS
3626 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
3627#endif
3628#ifdef _CS_LFS_LDFLAGS
3629 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
3630#endif
3631#ifdef _CS_LFS_LIBS
3632 {"CS_LFS_LIBS", _CS_LFS_LIBS},
3633#endif
3634#ifdef _CS_LFS_LINTFLAGS
3635 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
3636#endif
Fred Draked86ed291999-12-15 15:34:33 +00003637#ifdef _CS_MACHINE
3638 {"CS_MACHINE", _CS_MACHINE},
3639#endif
Fred Drakec9680921999-12-13 16:37:25 +00003640#ifdef _CS_PATH
3641 {"CS_PATH", _CS_PATH},
3642#endif
Fred Draked86ed291999-12-15 15:34:33 +00003643#ifdef _CS_RELEASE
3644 {"CS_RELEASE", _CS_RELEASE},
3645#endif
3646#ifdef _CS_SRPC_DOMAIN
3647 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
3648#endif
3649#ifdef _CS_SYSNAME
3650 {"CS_SYSNAME", _CS_SYSNAME},
3651#endif
3652#ifdef _CS_VERSION
3653 {"CS_VERSION", _CS_VERSION},
3654#endif
Fred Drakec9680921999-12-13 16:37:25 +00003655#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
3656 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
3657#endif
3658#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
3659 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
3660#endif
3661#ifdef _CS_XBS5_ILP32_OFF32_LIBS
3662 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
3663#endif
3664#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
3665 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
3666#endif
3667#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
3668 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
3669#endif
3670#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
3671 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
3672#endif
3673#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
3674 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
3675#endif
3676#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
3677 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
3678#endif
3679#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
3680 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
3681#endif
3682#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
3683 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
3684#endif
3685#ifdef _CS_XBS5_LP64_OFF64_LIBS
3686 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
3687#endif
3688#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
3689 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
3690#endif
3691#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
3692 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
3693#endif
3694#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
3695 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
3696#endif
3697#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
3698 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
3699#endif
3700#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
3701 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
3702#endif
Fred Draked86ed291999-12-15 15:34:33 +00003703#ifdef _MIPS_CS_AVAIL_PROCESSORS
3704 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
3705#endif
3706#ifdef _MIPS_CS_BASE
3707 {"MIPS_CS_BASE", _MIPS_CS_BASE},
3708#endif
3709#ifdef _MIPS_CS_HOSTID
3710 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
3711#endif
3712#ifdef _MIPS_CS_HW_NAME
3713 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
3714#endif
3715#ifdef _MIPS_CS_NUM_PROCESSORS
3716 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
3717#endif
3718#ifdef _MIPS_CS_OSREL_MAJ
3719 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
3720#endif
3721#ifdef _MIPS_CS_OSREL_MIN
3722 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
3723#endif
3724#ifdef _MIPS_CS_OSREL_PATCH
3725 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
3726#endif
3727#ifdef _MIPS_CS_OS_NAME
3728 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
3729#endif
3730#ifdef _MIPS_CS_OS_PROVIDER
3731 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
3732#endif
3733#ifdef _MIPS_CS_PROCESSORS
3734 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
3735#endif
3736#ifdef _MIPS_CS_SERIAL
3737 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
3738#endif
3739#ifdef _MIPS_CS_VENDOR
3740 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
3741#endif
Fred Drakec9680921999-12-13 16:37:25 +00003742};
3743
3744static int
3745conv_confstr_confname(arg, valuep)
3746 PyObject *arg;
3747 int *valuep;
3748{
3749 return conv_confname(arg, valuep, posix_constants_confstr,
3750 sizeof(posix_constants_confstr)
3751 / sizeof(struct constdef));
3752}
3753
3754static char posix_confstr__doc__[] = "\
3755confstr(name) -> string\n\
3756Return a string-valued system configuration variable.";
3757
3758static PyObject *
3759posix_confstr(self, args)
3760 PyObject *self;
3761 PyObject *args;
3762{
3763 PyObject *result = NULL;
3764 int name;
3765 char buffer[64];
3766
3767 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
3768 int len = confstr(name, buffer, sizeof(buffer));
3769
Fred Drakec9680921999-12-13 16:37:25 +00003770 errno = 0;
3771 if (len == 0) {
3772 if (errno != 0)
3773 posix_error();
3774 else
3775 result = PyString_FromString("");
3776 }
3777 else {
3778 if (len >= sizeof(buffer)) {
3779 result = PyString_FromStringAndSize(NULL, len);
3780 if (result != NULL)
3781 confstr(name, PyString_AS_STRING(result), len+1);
3782 }
3783 else
3784 result = PyString_FromString(buffer);
3785 }
3786 }
3787 return result;
3788}
3789#endif
3790
3791
3792#ifdef HAVE_SYSCONF
3793static struct constdef posix_constants_sysconf[] = {
3794#ifdef _SC_2_CHAR_TERM
3795 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
3796#endif
3797#ifdef _SC_2_C_BIND
3798 {"SC_2_C_BIND", _SC_2_C_BIND},
3799#endif
3800#ifdef _SC_2_C_DEV
3801 {"SC_2_C_DEV", _SC_2_C_DEV},
3802#endif
3803#ifdef _SC_2_C_VERSION
3804 {"SC_2_C_VERSION", _SC_2_C_VERSION},
3805#endif
3806#ifdef _SC_2_FORT_DEV
3807 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
3808#endif
3809#ifdef _SC_2_FORT_RUN
3810 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
3811#endif
3812#ifdef _SC_2_LOCALEDEF
3813 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
3814#endif
3815#ifdef _SC_2_SW_DEV
3816 {"SC_2_SW_DEV", _SC_2_SW_DEV},
3817#endif
3818#ifdef _SC_2_UPE
3819 {"SC_2_UPE", _SC_2_UPE},
3820#endif
3821#ifdef _SC_2_VERSION
3822 {"SC_2_VERSION", _SC_2_VERSION},
3823#endif
Fred Draked86ed291999-12-15 15:34:33 +00003824#ifdef _SC_ABI_ASYNCHRONOUS_IO
3825 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
3826#endif
3827#ifdef _SC_ACL
3828 {"SC_ACL", _SC_ACL},
3829#endif
Fred Drakec9680921999-12-13 16:37:25 +00003830#ifdef _SC_AIO_LISTIO_MAX
3831 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
3832#endif
Fred Drakec9680921999-12-13 16:37:25 +00003833#ifdef _SC_AIO_MAX
3834 {"SC_AIO_MAX", _SC_AIO_MAX},
3835#endif
3836#ifdef _SC_AIO_PRIO_DELTA_MAX
3837 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
3838#endif
3839#ifdef _SC_ARG_MAX
3840 {"SC_ARG_MAX", _SC_ARG_MAX},
3841#endif
3842#ifdef _SC_ASYNCHRONOUS_IO
3843 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
3844#endif
3845#ifdef _SC_ATEXIT_MAX
3846 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
3847#endif
Fred Draked86ed291999-12-15 15:34:33 +00003848#ifdef _SC_AUDIT
3849 {"SC_AUDIT", _SC_AUDIT},
3850#endif
Fred Drakec9680921999-12-13 16:37:25 +00003851#ifdef _SC_AVPHYS_PAGES
3852 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
3853#endif
3854#ifdef _SC_BC_BASE_MAX
3855 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
3856#endif
3857#ifdef _SC_BC_DIM_MAX
3858 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
3859#endif
3860#ifdef _SC_BC_SCALE_MAX
3861 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
3862#endif
3863#ifdef _SC_BC_STRING_MAX
3864 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
3865#endif
Fred Draked86ed291999-12-15 15:34:33 +00003866#ifdef _SC_CAP
3867 {"SC_CAP", _SC_CAP},
3868#endif
Fred Drakec9680921999-12-13 16:37:25 +00003869#ifdef _SC_CHARCLASS_NAME_MAX
3870 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
3871#endif
3872#ifdef _SC_CHAR_BIT
3873 {"SC_CHAR_BIT", _SC_CHAR_BIT},
3874#endif
3875#ifdef _SC_CHAR_MAX
3876 {"SC_CHAR_MAX", _SC_CHAR_MAX},
3877#endif
3878#ifdef _SC_CHAR_MIN
3879 {"SC_CHAR_MIN", _SC_CHAR_MIN},
3880#endif
3881#ifdef _SC_CHILD_MAX
3882 {"SC_CHILD_MAX", _SC_CHILD_MAX},
3883#endif
3884#ifdef _SC_CLK_TCK
3885 {"SC_CLK_TCK", _SC_CLK_TCK},
3886#endif
3887#ifdef _SC_COHER_BLKSZ
3888 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
3889#endif
3890#ifdef _SC_COLL_WEIGHTS_MAX
3891 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
3892#endif
3893#ifdef _SC_DCACHE_ASSOC
3894 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
3895#endif
3896#ifdef _SC_DCACHE_BLKSZ
3897 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
3898#endif
3899#ifdef _SC_DCACHE_LINESZ
3900 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
3901#endif
3902#ifdef _SC_DCACHE_SZ
3903 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
3904#endif
3905#ifdef _SC_DCACHE_TBLKSZ
3906 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
3907#endif
3908#ifdef _SC_DELAYTIMER_MAX
3909 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
3910#endif
3911#ifdef _SC_EQUIV_CLASS_MAX
3912 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
3913#endif
3914#ifdef _SC_EXPR_NEST_MAX
3915 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
3916#endif
3917#ifdef _SC_FSYNC
3918 {"SC_FSYNC", _SC_FSYNC},
3919#endif
3920#ifdef _SC_GETGR_R_SIZE_MAX
3921 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
3922#endif
3923#ifdef _SC_GETPW_R_SIZE_MAX
3924 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
3925#endif
3926#ifdef _SC_ICACHE_ASSOC
3927 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
3928#endif
3929#ifdef _SC_ICACHE_BLKSZ
3930 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
3931#endif
3932#ifdef _SC_ICACHE_LINESZ
3933 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
3934#endif
3935#ifdef _SC_ICACHE_SZ
3936 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
3937#endif
Fred Draked86ed291999-12-15 15:34:33 +00003938#ifdef _SC_INF
3939 {"SC_INF", _SC_INF},
3940#endif
Fred Drakec9680921999-12-13 16:37:25 +00003941#ifdef _SC_INT_MAX
3942 {"SC_INT_MAX", _SC_INT_MAX},
3943#endif
3944#ifdef _SC_INT_MIN
3945 {"SC_INT_MIN", _SC_INT_MIN},
3946#endif
3947#ifdef _SC_IOV_MAX
3948 {"SC_IOV_MAX", _SC_IOV_MAX},
3949#endif
Fred Draked86ed291999-12-15 15:34:33 +00003950#ifdef _SC_IP_SECOPTS
3951 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
3952#endif
Fred Drakec9680921999-12-13 16:37:25 +00003953#ifdef _SC_JOB_CONTROL
3954 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
3955#endif
Fred Draked86ed291999-12-15 15:34:33 +00003956#ifdef _SC_KERN_POINTERS
3957 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
3958#endif
3959#ifdef _SC_KERN_SIM
3960 {"SC_KERN_SIM", _SC_KERN_SIM},
3961#endif
Fred Drakec9680921999-12-13 16:37:25 +00003962#ifdef _SC_LINE_MAX
3963 {"SC_LINE_MAX", _SC_LINE_MAX},
3964#endif
3965#ifdef _SC_LOGIN_NAME_MAX
3966 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
3967#endif
3968#ifdef _SC_LOGNAME_MAX
3969 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
3970#endif
3971#ifdef _SC_LONG_BIT
3972 {"SC_LONG_BIT", _SC_LONG_BIT},
3973#endif
Fred Draked86ed291999-12-15 15:34:33 +00003974#ifdef _SC_MAC
3975 {"SC_MAC", _SC_MAC},
3976#endif
Fred Drakec9680921999-12-13 16:37:25 +00003977#ifdef _SC_MAPPED_FILES
3978 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
3979#endif
3980#ifdef _SC_MAXPID
3981 {"SC_MAXPID", _SC_MAXPID},
3982#endif
3983#ifdef _SC_MB_LEN_MAX
3984 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
3985#endif
3986#ifdef _SC_MEMLOCK
3987 {"SC_MEMLOCK", _SC_MEMLOCK},
3988#endif
3989#ifdef _SC_MEMLOCK_RANGE
3990 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
3991#endif
3992#ifdef _SC_MEMORY_PROTECTION
3993 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
3994#endif
3995#ifdef _SC_MESSAGE_PASSING
3996 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
3997#endif
Fred Draked86ed291999-12-15 15:34:33 +00003998#ifdef _SC_MMAP_FIXED_ALIGNMENT
3999 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
4000#endif
Fred Drakec9680921999-12-13 16:37:25 +00004001#ifdef _SC_MQ_OPEN_MAX
4002 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
4003#endif
4004#ifdef _SC_MQ_PRIO_MAX
4005 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
4006#endif
Fred Draked86ed291999-12-15 15:34:33 +00004007#ifdef _SC_NACLS_MAX
4008 {"SC_NACLS_MAX", _SC_NACLS_MAX},
4009#endif
Fred Drakec9680921999-12-13 16:37:25 +00004010#ifdef _SC_NGROUPS_MAX
4011 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
4012#endif
4013#ifdef _SC_NL_ARGMAX
4014 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
4015#endif
4016#ifdef _SC_NL_LANGMAX
4017 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
4018#endif
4019#ifdef _SC_NL_MSGMAX
4020 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
4021#endif
4022#ifdef _SC_NL_NMAX
4023 {"SC_NL_NMAX", _SC_NL_NMAX},
4024#endif
4025#ifdef _SC_NL_SETMAX
4026 {"SC_NL_SETMAX", _SC_NL_SETMAX},
4027#endif
4028#ifdef _SC_NL_TEXTMAX
4029 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
4030#endif
4031#ifdef _SC_NPROCESSORS_CONF
4032 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
4033#endif
4034#ifdef _SC_NPROCESSORS_ONLN
4035 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
4036#endif
Fred Draked86ed291999-12-15 15:34:33 +00004037#ifdef _SC_NPROC_CONF
4038 {"SC_NPROC_CONF", _SC_NPROC_CONF},
4039#endif
4040#ifdef _SC_NPROC_ONLN
4041 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
4042#endif
Fred Drakec9680921999-12-13 16:37:25 +00004043#ifdef _SC_NZERO
4044 {"SC_NZERO", _SC_NZERO},
4045#endif
4046#ifdef _SC_OPEN_MAX
4047 {"SC_OPEN_MAX", _SC_OPEN_MAX},
4048#endif
4049#ifdef _SC_PAGESIZE
4050 {"SC_PAGESIZE", _SC_PAGESIZE},
4051#endif
4052#ifdef _SC_PAGE_SIZE
4053 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
4054#endif
4055#ifdef _SC_PASS_MAX
4056 {"SC_PASS_MAX", _SC_PASS_MAX},
4057#endif
4058#ifdef _SC_PHYS_PAGES
4059 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
4060#endif
4061#ifdef _SC_PII
4062 {"SC_PII", _SC_PII},
4063#endif
4064#ifdef _SC_PII_INTERNET
4065 {"SC_PII_INTERNET", _SC_PII_INTERNET},
4066#endif
4067#ifdef _SC_PII_INTERNET_DGRAM
4068 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
4069#endif
4070#ifdef _SC_PII_INTERNET_STREAM
4071 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
4072#endif
4073#ifdef _SC_PII_OSI
4074 {"SC_PII_OSI", _SC_PII_OSI},
4075#endif
4076#ifdef _SC_PII_OSI_CLTS
4077 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
4078#endif
4079#ifdef _SC_PII_OSI_COTS
4080 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
4081#endif
4082#ifdef _SC_PII_OSI_M
4083 {"SC_PII_OSI_M", _SC_PII_OSI_M},
4084#endif
4085#ifdef _SC_PII_SOCKET
4086 {"SC_PII_SOCKET", _SC_PII_SOCKET},
4087#endif
4088#ifdef _SC_PII_XTI
4089 {"SC_PII_XTI", _SC_PII_XTI},
4090#endif
4091#ifdef _SC_POLL
4092 {"SC_POLL", _SC_POLL},
4093#endif
4094#ifdef _SC_PRIORITIZED_IO
4095 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
4096#endif
4097#ifdef _SC_PRIORITY_SCHEDULING
4098 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
4099#endif
4100#ifdef _SC_REALTIME_SIGNALS
4101 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
4102#endif
4103#ifdef _SC_RE_DUP_MAX
4104 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
4105#endif
4106#ifdef _SC_RTSIG_MAX
4107 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
4108#endif
4109#ifdef _SC_SAVED_IDS
4110 {"SC_SAVED_IDS", _SC_SAVED_IDS},
4111#endif
4112#ifdef _SC_SCHAR_MAX
4113 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
4114#endif
4115#ifdef _SC_SCHAR_MIN
4116 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
4117#endif
4118#ifdef _SC_SELECT
4119 {"SC_SELECT", _SC_SELECT},
4120#endif
4121#ifdef _SC_SEMAPHORES
4122 {"SC_SEMAPHORES", _SC_SEMAPHORES},
4123#endif
4124#ifdef _SC_SEM_NSEMS_MAX
4125 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
4126#endif
4127#ifdef _SC_SEM_VALUE_MAX
4128 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
4129#endif
4130#ifdef _SC_SHARED_MEMORY_OBJECTS
4131 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
4132#endif
4133#ifdef _SC_SHRT_MAX
4134 {"SC_SHRT_MAX", _SC_SHRT_MAX},
4135#endif
4136#ifdef _SC_SHRT_MIN
4137 {"SC_SHRT_MIN", _SC_SHRT_MIN},
4138#endif
4139#ifdef _SC_SIGQUEUE_MAX
4140 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
4141#endif
4142#ifdef _SC_SIGRT_MAX
4143 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
4144#endif
4145#ifdef _SC_SIGRT_MIN
4146 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
4147#endif
Fred Draked86ed291999-12-15 15:34:33 +00004148#ifdef _SC_SOFTPOWER
4149 {"SC_SOFTPOWER", _SC_SOFTPOWER},
4150#endif
Fred Drakec9680921999-12-13 16:37:25 +00004151#ifdef _SC_SPLIT_CACHE
4152 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
4153#endif
4154#ifdef _SC_SSIZE_MAX
4155 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
4156#endif
4157#ifdef _SC_STACK_PROT
4158 {"SC_STACK_PROT", _SC_STACK_PROT},
4159#endif
4160#ifdef _SC_STREAM_MAX
4161 {"SC_STREAM_MAX", _SC_STREAM_MAX},
4162#endif
4163#ifdef _SC_SYNCHRONIZED_IO
4164 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
4165#endif
4166#ifdef _SC_THREADS
4167 {"SC_THREADS", _SC_THREADS},
4168#endif
4169#ifdef _SC_THREAD_ATTR_STACKADDR
4170 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
4171#endif
4172#ifdef _SC_THREAD_ATTR_STACKSIZE
4173 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
4174#endif
4175#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
4176 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
4177#endif
4178#ifdef _SC_THREAD_KEYS_MAX
4179 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
4180#endif
4181#ifdef _SC_THREAD_PRIORITY_SCHEDULING
4182 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
4183#endif
4184#ifdef _SC_THREAD_PRIO_INHERIT
4185 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
4186#endif
4187#ifdef _SC_THREAD_PRIO_PROTECT
4188 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
4189#endif
4190#ifdef _SC_THREAD_PROCESS_SHARED
4191 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
4192#endif
4193#ifdef _SC_THREAD_SAFE_FUNCTIONS
4194 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
4195#endif
4196#ifdef _SC_THREAD_STACK_MIN
4197 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
4198#endif
4199#ifdef _SC_THREAD_THREADS_MAX
4200 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
4201#endif
4202#ifdef _SC_TIMERS
4203 {"SC_TIMERS", _SC_TIMERS},
4204#endif
4205#ifdef _SC_TIMER_MAX
4206 {"SC_TIMER_MAX", _SC_TIMER_MAX},
4207#endif
4208#ifdef _SC_TTY_NAME_MAX
4209 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
4210#endif
4211#ifdef _SC_TZNAME_MAX
4212 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
4213#endif
4214#ifdef _SC_T_IOV_MAX
4215 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
4216#endif
4217#ifdef _SC_UCHAR_MAX
4218 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
4219#endif
4220#ifdef _SC_UINT_MAX
4221 {"SC_UINT_MAX", _SC_UINT_MAX},
4222#endif
4223#ifdef _SC_UIO_MAXIOV
4224 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
4225#endif
4226#ifdef _SC_ULONG_MAX
4227 {"SC_ULONG_MAX", _SC_ULONG_MAX},
4228#endif
4229#ifdef _SC_USHRT_MAX
4230 {"SC_USHRT_MAX", _SC_USHRT_MAX},
4231#endif
4232#ifdef _SC_VERSION
4233 {"SC_VERSION", _SC_VERSION},
4234#endif
4235#ifdef _SC_WORD_BIT
4236 {"SC_WORD_BIT", _SC_WORD_BIT},
4237#endif
4238#ifdef _SC_XBS5_ILP32_OFF32
4239 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
4240#endif
4241#ifdef _SC_XBS5_ILP32_OFFBIG
4242 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
4243#endif
4244#ifdef _SC_XBS5_LP64_OFF64
4245 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
4246#endif
4247#ifdef _SC_XBS5_LPBIG_OFFBIG
4248 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
4249#endif
4250#ifdef _SC_XOPEN_CRYPT
4251 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
4252#endif
4253#ifdef _SC_XOPEN_ENH_I18N
4254 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
4255#endif
4256#ifdef _SC_XOPEN_LEGACY
4257 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
4258#endif
4259#ifdef _SC_XOPEN_REALTIME
4260 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
4261#endif
4262#ifdef _SC_XOPEN_REALTIME_THREADS
4263 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
4264#endif
4265#ifdef _SC_XOPEN_SHM
4266 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
4267#endif
4268#ifdef _SC_XOPEN_UNIX
4269 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
4270#endif
4271#ifdef _SC_XOPEN_VERSION
4272 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
4273#endif
4274#ifdef _SC_XOPEN_XCU_VERSION
4275 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
4276#endif
4277#ifdef _SC_XOPEN_XPG2
4278 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
4279#endif
4280#ifdef _SC_XOPEN_XPG3
4281 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
4282#endif
4283#ifdef _SC_XOPEN_XPG4
4284 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
4285#endif
4286};
4287
4288static int
4289conv_sysconf_confname(arg, valuep)
4290 PyObject *arg;
4291 int *valuep;
4292{
4293 return conv_confname(arg, valuep, posix_constants_sysconf,
4294 sizeof(posix_constants_sysconf)
4295 / sizeof(struct constdef));
4296}
4297
4298static char posix_sysconf__doc__[] = "\
4299sysconf(name) -> integer\n\
4300Return an integer-valued system configuration variable.";
4301
4302static PyObject *
4303posix_sysconf(self, args)
4304 PyObject *self;
4305 PyObject *args;
4306{
4307 PyObject *result = NULL;
4308 int name;
4309
4310 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
4311 int value;
4312
4313 errno = 0;
4314 value = sysconf(name);
4315 if (value == -1 && errno != 0)
4316 posix_error();
4317 else
4318 result = PyInt_FromLong(value);
4319 }
4320 return result;
4321}
4322#endif
4323
4324
Fred Drakebec628d1999-12-15 18:31:10 +00004325/* This code is used to ensure that the tables of configuration value names
4326 * are in sorted order as required by conv_confname(), and also to build the
4327 * the exported dictionaries that are used to publish information about the
4328 * names available on the host platform.
4329 *
4330 * Sorting the table at runtime ensures that the table is properly ordered
4331 * when used, even for platforms we're not able to test on. It also makes
4332 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00004333 */
Fred Drakebec628d1999-12-15 18:31:10 +00004334
4335static int
4336cmp_constdefs(v1, v2)
4337 const void *v1;
4338 const void *v2;
4339{
4340 const struct constdef *c1 =
4341 (const struct constdef *) v1;
4342 const struct constdef *c2 =
4343 (const struct constdef *) v2;
4344
4345 return strcmp(c1->name, c2->name);
4346}
4347
4348static int
4349setup_confname_table(table, tablesize, tablename, moddict)
Fred Draked86ed291999-12-15 15:34:33 +00004350 struct constdef *table;
4351 size_t tablesize;
Fred Drakebec628d1999-12-15 18:31:10 +00004352 char * tablename;
4353 PyObject *moddict;
Fred Draked86ed291999-12-15 15:34:33 +00004354{
Fred Drakebec628d1999-12-15 18:31:10 +00004355 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00004356 size_t i;
4357 int status;
Fred Drakebec628d1999-12-15 18:31:10 +00004358
4359 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
4360 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00004361 if (d == NULL)
4362 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004363
Barry Warsaw3155db32000-04-13 15:20:40 +00004364 for (i=0; i < tablesize; ++i) {
4365 PyObject *o = PyInt_FromLong(table[i].value);
4366 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
4367 Py_XDECREF(o);
4368 Py_DECREF(d);
4369 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004370 }
Barry Warsaw3155db32000-04-13 15:20:40 +00004371 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00004372 }
Barry Warsaw3155db32000-04-13 15:20:40 +00004373 status = PyDict_SetItemString(moddict, tablename, d);
4374 Py_DECREF(d);
4375 return status;
Fred Draked86ed291999-12-15 15:34:33 +00004376}
4377
Fred Drakebec628d1999-12-15 18:31:10 +00004378/* Return -1 on failure, 0 on success. */
4379static int
4380setup_confname_tables(moddict)
4381 PyObject *moddict;
Fred Draked86ed291999-12-15 15:34:33 +00004382{
4383#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00004384 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00004385 sizeof(posix_constants_pathconf)
4386 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00004387 "pathconf_names", moddict))
4388 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004389#endif
4390#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00004391 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00004392 sizeof(posix_constants_confstr)
4393 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00004394 "confstr_names", moddict))
4395 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004396#endif
4397#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00004398 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00004399 sizeof(posix_constants_sysconf)
4400 / sizeof(struct constdef),
Fred Drakebec628d1999-12-15 18:31:10 +00004401 "sysconf_names", moddict))
4402 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00004403#endif
Fred Drakebec628d1999-12-15 18:31:10 +00004404 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00004405}
Fred Draked86ed291999-12-15 15:34:33 +00004406
4407
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004408static char posix_abort__doc__[] = "\
4409abort() -> does not return!\n\
4410Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
4411in the hardest way possible on the hosting operating system.";
4412
4413static PyObject *
4414posix_abort(self, args)
4415 PyObject *self;
4416 PyObject *args;
4417{
4418 if (!PyArg_ParseTuple(args, ":abort"))
4419 return NULL;
4420 abort();
4421 /*NOTREACHED*/
4422 Py_FatalError("abort() called from Python code didn't abort!");
4423 return NULL;
4424}
Fred Drakebec628d1999-12-15 18:31:10 +00004425
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004426
4427static PyMethodDef posix_methods[] = {
4428 {"access", posix_access, METH_VARARGS, posix_access__doc__},
4429#ifdef HAVE_TTYNAME
4430 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
4431#endif
4432 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
4433 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004434#ifdef HAVE_CHOWN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004435 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004436#endif /* HAVE_CHOWN */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004437#ifdef HAVE_CTERMID
4438 {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__},
4439#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00004440#ifdef HAVE_GETCWD
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004441 {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00004442#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004443#ifdef HAVE_LINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004444 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004445#endif /* HAVE_LINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004446 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
4447 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
4448 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004449#ifdef HAVE_NICE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004450 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004451#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004452#ifdef HAVE_READLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004453 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004454#endif /* HAVE_READLINK */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004455 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
4456 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
4457 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004458#ifdef HAVE_SYMLINK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004459 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004460#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004461#ifdef HAVE_SYSTEM
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004462 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004463#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004464 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004465#ifdef HAVE_UNAME
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004466 {"uname", posix_uname, METH_VARARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004467#endif /* HAVE_UNAME */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004468 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
4469 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
4470 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004471#ifdef HAVE_TIMES
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004472 {"times", posix_times, METH_VARARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004473#endif /* HAVE_TIMES */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004474 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004475#ifdef HAVE_EXECV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004476 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
4477 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004478#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00004479#ifdef HAVE_SPAWNV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004480 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
4481 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +00004482#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00004483#ifdef HAVE_FORK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004484 {"fork", posix_fork, METH_VARARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004485#endif /* HAVE_FORK */
4486#ifdef HAVE_GETEGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004487 {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004488#endif /* HAVE_GETEGID */
4489#ifdef HAVE_GETEUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004490 {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004491#endif /* HAVE_GETEUID */
4492#ifdef HAVE_GETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004493 {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004494#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00004495#ifdef HAVE_GETGROUPS
4496 {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__},
4497#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004498 {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004499#ifdef HAVE_GETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004500 {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004501#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00004502#ifdef HAVE_GETPPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004503 {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004504#endif /* HAVE_GETPPID */
4505#ifdef HAVE_GETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004506 {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004507#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004508#ifdef HAVE_GETLOGIN
4509 {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__},
4510#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00004511#ifdef HAVE_KILL
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004512 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004513#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00004514#ifdef HAVE_PLOCK
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004515 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00004516#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004517#ifdef HAVE_POPEN
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004518 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004519#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004520#ifdef HAVE_SETUID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004521 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004522#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004523#ifdef HAVE_SETGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004524 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004525#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004526#ifdef HAVE_SETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004527 {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004528#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00004529#ifdef HAVE_WAIT
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004530 {"wait", posix_wait, METH_VARARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00004531#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004532#ifdef HAVE_WAITPID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004533 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004534#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004535#ifdef HAVE_SETSID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004536 {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004537#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004538#ifdef HAVE_SETPGID
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004539 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004540#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004541#ifdef HAVE_TCGETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004542 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004543#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004544#ifdef HAVE_TCSETPGRP
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004545 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004546#endif /* HAVE_TCSETPGRP */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004547 {"open", posix_open, METH_VARARGS, posix_open__doc__},
4548 {"close", posix_close, METH_VARARGS, posix_close__doc__},
4549 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
4550 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
4551 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
4552 {"read", posix_read, METH_VARARGS, posix_read__doc__},
4553 {"write", posix_write, METH_VARARGS, posix_write__doc__},
4554 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
4555 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004556#ifdef HAVE_PIPE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004557 {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004558#endif
4559#ifdef HAVE_MKFIFO
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004560 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004561#endif
4562#ifdef HAVE_FTRUNCATE
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004563 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004564#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004565#ifdef HAVE_PUTENV
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004566 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00004567#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00004568#ifdef HAVE_STRERROR
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004569 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Guido van Rossumb6a47161997-09-15 22:54:34 +00004570#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00004571#ifdef HAVE_FSYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004572 {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00004573#endif
4574#ifdef HAVE_FDATASYNC
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004575 {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00004576#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00004577#ifdef HAVE_SYS_WAIT_H
4578#ifdef WIFSTOPPED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004579 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004580#endif /* WIFSTOPPED */
4581#ifdef WIFSIGNALED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004582 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004583#endif /* WIFSIGNALED */
4584#ifdef WIFEXITED
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004585 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004586#endif /* WIFEXITED */
4587#ifdef WEXITSTATUS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004588 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004589#endif /* WEXITSTATUS */
4590#ifdef WTERMSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004591 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004592#endif /* WTERMSIG */
4593#ifdef WSTOPSIG
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004594 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00004595#endif /* WSTOPSIG */
4596#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004597#ifdef HAVE_FSTATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004598 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00004599#endif
4600#ifdef HAVE_STATVFS
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004601 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00004602#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004603#ifdef HAVE_TMPNAM
4604 {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__},
4605#endif
4606#ifdef HAVE_TEMPNAM
4607 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
4608#endif
4609#ifdef HAVE_TMPNAM
4610 {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__},
4611#endif
Fred Drakec9680921999-12-13 16:37:25 +00004612#ifdef HAVE_CONFSTR
4613 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
4614#endif
4615#ifdef HAVE_SYSCONF
4616 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
4617#endif
4618#ifdef HAVE_FPATHCONF
4619 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
4620#endif
4621#ifdef HAVE_PATHCONF
4622 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
4623#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004624 {"abort", posix_abort, METH_VARARGS, posix_abort__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004625 {NULL, NULL} /* Sentinel */
4626};
4627
4628
Barry Warsaw4a342091996-12-19 23:50:02 +00004629static int
4630ins(d, symbol, value)
4631 PyObject* d;
4632 char* symbol;
4633 long value;
4634{
4635 PyObject* v = PyInt_FromLong(value);
4636 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
4637 return -1; /* triggers fatal error */
4638
4639 Py_DECREF(v);
4640 return 0;
4641}
4642
Guido van Rossumd48f2521997-12-05 22:19:34 +00004643#if defined(PYOS_OS2)
4644/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
4645static int insertvalues(PyObject *d)
4646{
4647 APIRET rc;
4648 ULONG values[QSV_MAX+1];
4649 PyObject *v;
4650 char *ver, tmp[10];
4651
4652 Py_BEGIN_ALLOW_THREADS
4653 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
4654 Py_END_ALLOW_THREADS
4655
4656 if (rc != NO_ERROR) {
4657 os2_error(rc);
4658 return -1;
4659 }
4660
4661 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
4662 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
4663 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
4664 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
4665 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
4666 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
4667 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
4668
4669 switch (values[QSV_VERSION_MINOR]) {
4670 case 0: ver = "2.00"; break;
4671 case 10: ver = "2.10"; break;
4672 case 11: ver = "2.11"; break;
4673 case 30: ver = "3.00"; break;
4674 case 40: ver = "4.00"; break;
4675 case 50: ver = "5.00"; break;
4676 default:
4677 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
4678 values[QSV_VERSION_MINOR]);
4679 ver = &tmp[0];
4680 }
4681
4682 /* Add Indicator of the Version of the Operating System */
4683 v = PyString_FromString(ver);
4684 if (!v || PyDict_SetItemString(d, "version", v) < 0)
4685 return -1;
4686 Py_DECREF(v);
4687
4688 /* Add Indicator of Which Drive was Used to Boot the System */
4689 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
4690 tmp[1] = ':';
4691 tmp[2] = '\0';
4692
4693 v = PyString_FromString(tmp);
4694 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
4695 return -1;
4696 Py_DECREF(v);
4697
4698 return 0;
4699}
4700#endif
4701
Barry Warsaw4a342091996-12-19 23:50:02 +00004702static int
4703all_ins(d)
4704 PyObject* d;
4705{
Guido van Rossum94f6f721999-01-06 18:42:14 +00004706#ifdef F_OK
4707 if (ins(d, "F_OK", (long)F_OK)) return -1;
4708#endif
4709#ifdef R_OK
4710 if (ins(d, "R_OK", (long)R_OK)) return -1;
4711#endif
4712#ifdef W_OK
4713 if (ins(d, "W_OK", (long)W_OK)) return -1;
4714#endif
4715#ifdef X_OK
4716 if (ins(d, "X_OK", (long)X_OK)) return -1;
4717#endif
Fred Drakec9680921999-12-13 16:37:25 +00004718#ifdef NGROUPS_MAX
4719 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
4720#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00004721#ifdef TMP_MAX
4722 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
4723#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00004724#ifdef WNOHANG
4725 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
4726#endif
4727#ifdef O_RDONLY
4728 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
4729#endif
4730#ifdef O_WRONLY
4731 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
4732#endif
4733#ifdef O_RDWR
4734 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
4735#endif
4736#ifdef O_NDELAY
4737 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
4738#endif
4739#ifdef O_NONBLOCK
4740 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
4741#endif
4742#ifdef O_APPEND
4743 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
4744#endif
4745#ifdef O_DSYNC
4746 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
4747#endif
4748#ifdef O_RSYNC
4749 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
4750#endif
4751#ifdef O_SYNC
4752 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
4753#endif
4754#ifdef O_NOCTTY
4755 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
4756#endif
4757#ifdef O_CREAT
4758 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
4759#endif
4760#ifdef O_EXCL
4761 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
4762#endif
4763#ifdef O_TRUNC
4764 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
4765#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00004766#ifdef O_BINARY
4767 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
4768#endif
4769#ifdef O_TEXT
4770 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
4771#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00004772
Guido van Rossum246bc171999-02-01 23:54:31 +00004773#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00004774 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
4775 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
4776 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
4777 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
4778 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00004779#endif
4780
Guido van Rossumd48f2521997-12-05 22:19:34 +00004781#if defined(PYOS_OS2)
4782 if (insertvalues(d)) return -1;
4783#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00004784 return 0;
4785}
4786
4787
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004788#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004789#define INITFUNC initnt
4790#define MODNAME "nt"
4791#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004792#if defined(PYOS_OS2)
4793#define INITFUNC initos2
4794#define MODNAME "os2"
4795#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004796#define INITFUNC initposix
4797#define MODNAME "posix"
4798#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004799#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004800
Guido van Rossum3886bb61998-12-04 18:50:17 +00004801DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004802INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00004803{
Barry Warsaw53699e91996-12-10 23:23:01 +00004804 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00004805
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004806 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004807 posix_methods,
4808 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004809 (PyObject *)NULL,
4810 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00004811 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004812
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004813 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004814 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00004815 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004816 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00004817 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00004818
Barry Warsaw4a342091996-12-19 23:50:02 +00004819 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00004820 return;
4821
Fred Drakebec628d1999-12-15 18:31:10 +00004822 if (setup_confname_tables(d))
4823 return;
4824
Barry Warsawca74da41999-02-09 19:31:45 +00004825 PyDict_SetItemString(d, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00004826
Guido van Rossumb3d39562000-01-31 18:41:26 +00004827#ifdef HAVE_PUTENV
Fred Drake762e2061999-08-26 17:23:54 +00004828 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00004829#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004830}