blob: 19c7809fdb3235395f196e356bec7585e25b60ce [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 Rossumb6775db1994-08-01 11:34:53 +0000158#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000159#if defined(PYCC_VACPP)
160extern int mkdir Py_PROTO((char *));
161#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000162#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000163extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000165extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000167#endif
168#if defined(__IBMC__) || defined(__IBMCPP__)
169extern int chdir Py_PROTO((char *));
170extern int rmdir Py_PROTO((char *));
171#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000172extern int chdir Py_PROTO((const char *));
173extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000174#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000175extern int chmod Py_PROTO((const char *, mode_t));
176extern int chown Py_PROTO((const char *, uid_t, gid_t));
177extern char *getcwd Py_PROTO((char *, int));
178extern char *strerror Py_PROTO((int));
179extern int link Py_PROTO((const char *, const char *));
180extern int rename Py_PROTO((const char *, const char *));
181extern int stat Py_PROTO((const char *, struct stat *));
182extern int unlink Py_PROTO((const char *));
183extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000185extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000186#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000188extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000189#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000191
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193
Guido van Rossumb6775db1994-08-01 11:34:53 +0000194#ifdef HAVE_UTIME_H
195#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000196#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000198#ifdef HAVE_SYS_UTIME_H
199#include <sys/utime.h>
200#define HAVE_UTIME_H /* pretend we do for the rest of this file */
201#endif /* HAVE_SYS_UTIME_H */
202
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYS_TIMES_H
204#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000205#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206
207#ifdef HAVE_SYS_PARAM_H
208#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000209#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210
211#ifdef HAVE_SYS_UTSNAME_H
212#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214
215#ifndef MAXPATHLEN
216#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000217#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000219#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000221#define NAMLEN(dirent) strlen((dirent)->d_name)
222#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000223#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000224#include <direct.h>
225#define NAMLEN(dirent) strlen((dirent)->d_name)
226#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000228#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000229#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000230#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232#endif
233#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#endif
236#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000238#endif
239#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000241#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#include <direct.h>
243#include <io.h>
244#include <process.h>
245#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000246#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000247#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000248#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#else /* 16-bit Windows */
250#include <dos.h>
251#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000252#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000253#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossumd48f2521997-12-05 22:19:34 +0000255#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000257#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000259#ifdef UNION_WAIT
260/* Emulate some macros on systems that have a union instead of macros */
261
262#ifndef WIFEXITED
263#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
264#endif
265
266#ifndef WEXITSTATUS
267#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
268#endif
269
270#ifndef WTERMSIG
271#define WTERMSIG(u_wait) ((u_wait).w_termsig)
272#endif
273
274#endif /* UNION_WAIT */
275
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276/* Return a dictionary corresponding to the POSIX environment table */
277
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000278#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000280#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281
Barry Warsaw53699e91996-12-10 23:23:01 +0000282static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283convertenviron()
284{
Barry Warsaw53699e91996-12-10 23:23:01 +0000285 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000287 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 if (d == NULL)
289 return NULL;
290 if (environ == NULL)
291 return d;
292 /* XXX This part ignores errors */
293 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000294 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295 char *p = strchr(*e, '=');
296 if (p == NULL)
297 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000298 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 if (v == NULL)
300 continue;
301 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000302 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000304 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000306#if defined(PYOS_OS2)
307 {
308 APIRET rc;
309 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
310
311 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
312 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
313 PyObject *v = PyString_FromString(buffer);
314 PyDict_SetItemString(d, "BEGINLIBPATH", v);
315 Py_DECREF(v);
316 }
317 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
318 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
319 PyObject *v = PyString_FromString(buffer);
320 PyDict_SetItemString(d, "ENDLIBPATH", v);
321 Py_DECREF(v);
322 }
323 }
324#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 return d;
326}
327
328
Barry Warsaw53699e91996-12-10 23:23:01 +0000329static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330
331/* Set a POSIX-specific error from errno, and return NULL */
332
Barry Warsawd58d7641998-07-23 16:14:40 +0000333static PyObject *
334posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000335{
Barry Warsaw53699e91996-12-10 23:23:01 +0000336 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337}
Barry Warsawd58d7641998-07-23 16:14:40 +0000338static PyObject *
339posix_error_with_filename(name)
340 char* name;
341{
342 return PyErr_SetFromErrnoWithFilename(PosixError, name);
343}
344
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345
Guido van Rossumd48f2521997-12-05 22:19:34 +0000346#if defined(PYOS_OS2)
347/**********************************************************************
348 * Helper Function to Trim and Format OS/2 Messages
349 **********************************************************************/
350 static void
351os2_formatmsg(char *msgbuf, int msglen, char *reason)
352{
353 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
354
355 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
356 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
357
358 while (lastc > msgbuf && isspace(*lastc))
359 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
360 }
361
362 /* Add Optional Reason Text */
363 if (reason) {
364 strcat(msgbuf, " : ");
365 strcat(msgbuf, reason);
366 }
367}
368
369/**********************************************************************
370 * Decode an OS/2 Operating System Error Code
371 *
372 * A convenience function to lookup an OS/2 error code and return a
373 * text message we can use to raise a Python exception.
374 *
375 * Notes:
376 * The messages for errors returned from the OS/2 kernel reside in
377 * the file OSO001.MSG in the \OS2 directory hierarchy.
378 *
379 **********************************************************************/
380 static char *
381os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
382{
383 APIRET rc;
384 ULONG msglen;
385
386 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
387 Py_BEGIN_ALLOW_THREADS
388 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
389 errorcode, "oso001.msg", &msglen);
390 Py_END_ALLOW_THREADS
391
392 if (rc == NO_ERROR)
393 os2_formatmsg(msgbuf, msglen, reason);
394 else
395 sprintf(msgbuf, "unknown OS error #%d", errorcode);
396
397 return msgbuf;
398}
399
400/* Set an OS/2-specific error and return NULL. OS/2 kernel
401 errors are not in a global variable e.g. 'errno' nor are
402 they congruent with posix error numbers. */
403
404static PyObject * os2_error(int code)
405{
406 char text[1024];
407 PyObject *v;
408
409 os2_strerror(text, sizeof(text), code, "");
410
411 v = Py_BuildValue("(is)", code, text);
412 if (v != NULL) {
413 PyErr_SetObject(PosixError, v);
414 Py_DECREF(v);
415 }
416 return NULL; /* Signal to Python that an Exception is Pending */
417}
418
419#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420
421/* POSIX generic methods */
422
Barry Warsaw53699e91996-12-10 23:23:01 +0000423static PyObject *
Guido van Rossum21142a01999-01-08 21:05:37 +0000424posix_int(args, func)
425 PyObject *args;
426 int (*func) Py_FPROTO((int));
427{
428 int fd;
429 int res;
430 if (!PyArg_Parse(args, "i", &fd))
431 return NULL;
432 Py_BEGIN_ALLOW_THREADS
433 res = (*func)(fd);
434 Py_END_ALLOW_THREADS
435 if (res < 0)
436 return posix_error();
437 Py_INCREF(Py_None);
438 return Py_None;
439}
440
441
442static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000444 PyObject *args;
445 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000447 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000448 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000449 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000451 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000452 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000453 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000454 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000455 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000456 Py_INCREF(Py_None);
457 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458}
459
Barry Warsaw53699e91996-12-10 23:23:01 +0000460static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000462 PyObject *args;
463 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000465 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000466 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000469 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000470 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000471 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000472 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000473 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000475 Py_INCREF(Py_None);
476 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477}
478
Barry Warsaw53699e91996-12-10 23:23:01 +0000479static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000481 PyObject *args;
482 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000484 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000486 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000487 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000490 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000491 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000492 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000493 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 Py_INCREF(Py_None);
495 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496}
497
Barry Warsaw53699e91996-12-10 23:23:01 +0000498static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000499posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000500 PyObject *args;
501 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000502{
503 char *path;
504 int i,i2;
505 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000506 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000508 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000510 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000511 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000512 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000513 Py_INCREF(Py_None);
514 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000515}
516
Barry Warsaw53699e91996-12-10 23:23:01 +0000517static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000519 PyObject *self;
520 PyObject *args;
521 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522{
523 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000524 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000525 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000526 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000528 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000529 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000530 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000531 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000532 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000533#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000534 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000535 (long)st.st_mode,
536 (long)st.st_ino,
537 (long)st.st_dev,
538 (long)st.st_nlink,
539 (long)st.st_uid,
540 (long)st.st_gid,
541 (long)st.st_size,
542 (long)st.st_atime,
543 (long)st.st_mtime,
544 (long)st.st_ctime);
545#else
546 return Py_BuildValue("(lLllllLlll)",
547 (long)st.st_mode,
548 (LONG_LONG)st.st_ino,
549 (long)st.st_dev,
550 (long)st.st_nlink,
551 (long)st.st_uid,
552 (long)st.st_gid,
553 (LONG_LONG)st.st_size,
554 (long)st.st_atime,
555 (long)st.st_mtime,
556 (long)st.st_ctime);
557#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558}
559
560
561/* POSIX methods */
562
Guido van Rossum94f6f721999-01-06 18:42:14 +0000563static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000564"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000565Test for access to a file.";
566
567static PyObject *
568posix_access(self, args)
569 PyObject *self;
570 PyObject *args;
571{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000572 char *path;
573 int mode;
574 int res;
575
576 if (!PyArg_Parse(args, "(si)", &path, &mode))
577 return NULL;
578 Py_BEGIN_ALLOW_THREADS
579 res = access(path, mode);
580 Py_END_ALLOW_THREADS
581 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000582}
583
Guido van Rossumd371ff11999-01-25 16:12:23 +0000584#ifndef F_OK
585#define F_OK 0
586#endif
587#ifndef R_OK
588#define R_OK 4
589#endif
590#ifndef W_OK
591#define W_OK 2
592#endif
593#ifndef X_OK
594#define X_OK 1
595#endif
596
597#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000598static char posix_ttyname__doc__[] =
599"ttyname(fd, mode) -> String\n\
600Return the name of the terminal device connected to 'fd'.";
601
602static PyObject *
603posix_ttyname(self, args)
604 PyObject *self;
605 PyObject *args;
606{
607 PyObject *file;
608 int id;
609 char *ret;
610
Guido van Rossum94f6f721999-01-06 18:42:14 +0000611 if (!PyArg_Parse(args, "i", &id))
612 return NULL;
613
Guido van Rossum94f6f721999-01-06 18:42:14 +0000614 ret = ttyname(id);
615 if (ret == NULL)
616 return(posix_error());
617 return(PyString_FromString(ret));
618}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000619#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000621static char posix_chdir__doc__[] =
622"chdir(path) -> None\n\
623Change the current working directory to the specified path.";
624
Barry Warsaw53699e91996-12-10 23:23:01 +0000625static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000627 PyObject *self;
628 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630 return posix_1str(args, chdir);
631}
632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000633
634static char posix_chmod__doc__[] =
635"chmod(path, mode) -> None\n\
636Change the access permissions of a file.";
637
Barry Warsaw53699e91996-12-10 23:23:01 +0000638static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 PyObject *self;
641 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643 return posix_strint(args, chmod);
644}
645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000646
Guido van Rossum21142a01999-01-08 21:05:37 +0000647#ifdef HAVE_FSYNC
648static char posix_fsync__doc__[] =
649"fsync(fildes) -> None\n\
650force write of file with filedescriptor to disk.";
651
652static PyObject *
653posix_fsync(self, args)
654 PyObject *self;
655 PyObject *args;
656{
Guido van Rossum21142a01999-01-08 21:05:37 +0000657 return posix_int(args, fsync);
658}
659#endif /* HAVE_FSYNC */
660
661#ifdef HAVE_FDATASYNC
662static char posix_fdatasync__doc__[] =
663"fdatasync(fildes) -> None\n\
664force write of file with filedescriptor to disk.\n\
665 does not force update of metadata.";
666
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000667extern int fdatasync(int); /* Prototype just in case */
668
Guido van Rossum21142a01999-01-08 21:05:37 +0000669static PyObject *
670posix_fdatasync(self, args)
671 PyObject *self;
672 PyObject *args;
673{
Guido van Rossum21142a01999-01-08 21:05:37 +0000674 return posix_int(args, fdatasync);
675}
676#endif /* HAVE_FDATASYNC */
677
678
Guido van Rossumb6775db1994-08-01 11:34:53 +0000679#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000680static char posix_chown__doc__[] =
681"chown(path, uid, gid) -> None\n\
682Change the owner and group id of path to the numeric uid and gid.";
683
Barry Warsaw53699e91996-12-10 23:23:01 +0000684static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000685posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000686 PyObject *self;
687 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000688{
689 return posix_strintint(args, chown);
690}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000691#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000692
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000693
Guido van Rossum36bc6801995-06-14 22:54:23 +0000694#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000695static char posix_getcwd__doc__[] =
696"getcwd() -> path\n\
697Return a string representing the current working directory.";
698
Barry Warsaw53699e91996-12-10 23:23:01 +0000699static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000701 PyObject *self;
702 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703{
704 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000705 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000706 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000708 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000709 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000710 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000711 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000713 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000715#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000717
Guido van Rossumb6775db1994-08-01 11:34:53 +0000718#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000719static char posix_link__doc__[] =
720"link(src, dst) -> None\n\
721Create a hard link to a file.";
722
Barry Warsaw53699e91996-12-10 23:23:01 +0000723static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000724posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000725 PyObject *self;
726 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000728 return posix_2str(args, link);
729}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000730#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000732
733static char posix_listdir__doc__[] =
734"listdir(path) -> list_of_strings\n\
735Return a list containing the names of the entries in the directory.\n\
736\n\
737 path: path of directory to list\n\
738\n\
739The list is in arbitrary order. It does not include the special\n\
740entries '.' and '..' even if they are present in the directory.";
741
Barry Warsaw53699e91996-12-10 23:23:01 +0000742static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000744 PyObject *self;
745 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000747 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000748 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000749#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000750
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751 char *name;
752 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000753 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000754 HANDLE hFindFile;
755 WIN32_FIND_DATA FileData;
756 char namebuf[MAX_PATH+5];
757
Guido van Rossum7e488981998-10-08 02:25:24 +0000758 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000759 return NULL;
760 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000761 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762 return NULL;
763 }
764 strcpy(namebuf, name);
765 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
766 namebuf[len++] = '/';
767 strcpy(namebuf + len, "*.*");
768
Barry Warsaw53699e91996-12-10 23:23:01 +0000769 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770 return NULL;
771
772 hFindFile = FindFirstFile(namebuf, &FileData);
773 if (hFindFile == INVALID_HANDLE_VALUE) {
774 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000775 if (errno == ERROR_FILE_NOT_FOUND)
776 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777 return posix_error();
778 }
779 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000780 if (FileData.cFileName[0] == '.' &&
781 (FileData.cFileName[1] == '\0' ||
782 FileData.cFileName[1] == '.' &&
783 FileData.cFileName[2] == '\0'))
784 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000785 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000787 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 d = NULL;
789 break;
790 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000791 if (PyList_Append(d, v) != 0) {
792 Py_DECREF(v);
793 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794 d = NULL;
795 break;
796 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000797 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 } while (FindNextFile(hFindFile, &FileData) == TRUE);
799
800 if (FindClose(hFindFile) == FALSE) {
801 errno = GetLastError();
802 return posix_error();
803 }
804
805 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000806
Guido van Rossum8d665e61996-06-26 18:22:49 +0000807#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000808#ifdef _MSC_VER /* 16-bit Windows */
809
810#ifndef MAX_PATH
811#define MAX_PATH 250
812#endif
813 char *name, *pt;
814 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000816 char namebuf[MAX_PATH+5];
817 struct _find_t ep;
818
Guido van Rossum7e488981998-10-08 02:25:24 +0000819 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000820 return NULL;
821 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000822 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000823 return NULL;
824 }
825 strcpy(namebuf, name);
826 for (pt = namebuf; *pt; pt++)
827 if (*pt == '/')
828 *pt = '\\';
829 if (namebuf[len-1] != '\\')
830 namebuf[len++] = '\\';
831 strcpy(namebuf + len, "*.*");
832
Barry Warsaw53699e91996-12-10 23:23:01 +0000833 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000834 return NULL;
835
836 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000837 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
838 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000839 errno = ENOENT;
840 return posix_error();
841 }
842 do {
843 if (ep.name[0] == '.' &&
844 (ep.name[1] == '\0' ||
845 ep.name[1] == '.' &&
846 ep.name[2] == '\0'))
847 continue;
848 strcpy(namebuf, ep.name);
849 for (pt = namebuf; *pt; pt++)
850 if (isupper(*pt))
851 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000852 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000853 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000854 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000855 d = NULL;
856 break;
857 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000858 if (PyList_Append(d, v) != 0) {
859 Py_DECREF(v);
860 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000861 d = NULL;
862 break;
863 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000864 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000865 } while (_dos_findnext(&ep) == 0);
866
867 return d;
868
869#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000870#if defined(PYOS_OS2)
871
872#ifndef MAX_PATH
873#define MAX_PATH CCHMAXPATH
874#endif
875 char *name, *pt;
876 int len;
877 PyObject *d, *v;
878 char namebuf[MAX_PATH+5];
879 HDIR hdir = 1;
880 ULONG srchcnt = 1;
881 FILEFINDBUF3 ep;
882 APIRET rc;
883
Guido van Rossum7e488981998-10-08 02:25:24 +0000884 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000885 return NULL;
886 if (len >= MAX_PATH) {
887 PyErr_SetString(PyExc_ValueError, "path too long");
888 return NULL;
889 }
890 strcpy(namebuf, name);
891 for (pt = namebuf; *pt; pt++)
892 if (*pt == '/')
893 *pt = '\\';
894 if (namebuf[len-1] != '\\')
895 namebuf[len++] = '\\';
896 strcpy(namebuf + len, "*.*");
897
898 if ((d = PyList_New(0)) == NULL)
899 return NULL;
900
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000901 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
902 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000903 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000904 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
905 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
906 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000907
908 if (rc != NO_ERROR) {
909 errno = ENOENT;
910 return posix_error();
911 }
912
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000913 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000914 do {
915 if (ep.achName[0] == '.'
916 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000917 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000918
919 strcpy(namebuf, ep.achName);
920
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000921 /* Leave Case of Name Alone -- In Native Form */
922 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000923
924 v = PyString_FromString(namebuf);
925 if (v == NULL) {
926 Py_DECREF(d);
927 d = NULL;
928 break;
929 }
930 if (PyList_Append(d, v) != 0) {
931 Py_DECREF(v);
932 Py_DECREF(d);
933 d = NULL;
934 break;
935 }
936 Py_DECREF(v);
937 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
938 }
939
940 return d;
941#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000942
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000943 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000944 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000945 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000946 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000947 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000949 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000950 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000951 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000953 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000954 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000956 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000957 return NULL;
958 }
959 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000960 if (ep->d_name[0] == '.' &&
961 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000962 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000963 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000964 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000966 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 d = NULL;
968 break;
969 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000970 if (PyList_Append(d, v) != 0) {
971 Py_DECREF(v);
972 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000973 d = NULL;
974 break;
975 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000977 }
978 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000979 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000980
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000981 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000982
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000983#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000984#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000985#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986}
987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000988static char posix_mkdir__doc__[] =
989"mkdir(path [, mode=0777]) -> None\n\
990Create a directory.";
991
Barry Warsaw53699e91996-12-10 23:23:01 +0000992static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000993posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000994 PyObject *self;
995 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000996{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000997 int res;
998 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000999 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +00001000 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001001 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001002 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001003#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001004 res = mkdir(path);
1005#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001006 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001007#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001008 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001009 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001010 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001011 Py_INCREF(Py_None);
1012 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001013}
1014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001015
Guido van Rossumb6775db1994-08-01 11:34:53 +00001016#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001017static char posix_nice__doc__[] =
1018"nice(inc) -> new_priority\n\
1019Decrease the priority of process and return new priority.";
1020
Barry Warsaw53699e91996-12-10 23:23:01 +00001021static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001022posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001023 PyObject *self;
1024 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001025{
1026 int increment, value;
1027
Barry Warsaw53699e91996-12-10 23:23:01 +00001028 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001029 return NULL;
1030 value = nice(increment);
1031 if (value == -1)
1032 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001033 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001034}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001035#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001036
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001037
1038static char posix_rename__doc__[] =
1039"rename(old, new) -> None\n\
1040Rename a file or directory.";
1041
Barry Warsaw53699e91996-12-10 23:23:01 +00001042static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001043posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001044 PyObject *self;
1045 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001046{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001047 return posix_2str(args, rename);
1048}
1049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001050
1051static char posix_rmdir__doc__[] =
1052"rmdir(path) -> None\n\
1053Remove a directory.";
1054
Barry Warsaw53699e91996-12-10 23:23:01 +00001055static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001056posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001057 PyObject *self;
1058 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001059{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001060 return posix_1str(args, rmdir);
1061}
1062
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001063
1064static char posix_stat__doc__[] =
1065"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1066Perform a stat system call on the given path.";
1067
Barry Warsaw53699e91996-12-10 23:23:01 +00001068static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001069posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 PyObject *self;
1071 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001072{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001073 return posix_do_stat(self, args, stat);
1074}
1075
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001076
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001077#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001078static char posix_system__doc__[] =
1079"system(command) -> exit_status\n\
1080Execute the command (a string) in a subshell.";
1081
Barry Warsaw53699e91996-12-10 23:23:01 +00001082static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001083posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001084 PyObject *self;
1085 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001086{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001087 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001088 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001089 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001090 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001091 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001092 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001093 Py_END_ALLOW_THREADS
1094 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001096#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001098
1099static char posix_umask__doc__[] =
1100"umask(new_mask) -> old_mask\n\
1101Set the current numeric umask and return the previous umask.";
1102
Barry Warsaw53699e91996-12-10 23:23:01 +00001103static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001104posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001105 PyObject *self;
1106 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001107{
1108 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001109 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110 return NULL;
1111 i = umask(i);
1112 if (i < 0)
1113 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001114 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001115}
1116
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001117
1118static char posix_unlink__doc__[] =
1119"unlink(path) -> None\n\
1120Remove a file (same as remove(path)).";
1121
1122static char posix_remove__doc__[] =
1123"remove(path) -> None\n\
1124Remove a file (same as unlink(path)).";
1125
Barry Warsaw53699e91996-12-10 23:23:01 +00001126static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001127posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001128 PyObject *self;
1129 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001130{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001131 return posix_1str(args, unlink);
1132}
1133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001134
Guido van Rossumb6775db1994-08-01 11:34:53 +00001135#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001136static char posix_uname__doc__[] =
1137"uname() -> (sysname, nodename, release, version, machine)\n\
1138Return a tuple identifying the current operating system.";
1139
Barry Warsaw53699e91996-12-10 23:23:01 +00001140static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001141posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001142 PyObject *self;
1143 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001144{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001145 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001146 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001147 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001148 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001149 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001150 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001151 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001152 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001153 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001154 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001155 u.sysname,
1156 u.nodename,
1157 u.release,
1158 u.version,
1159 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001160}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001161#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001163
1164static char posix_utime__doc__[] =
1165"utime(path, (atime, utime)) -> None\n\
1166Set the access and modified time of the file to the given values.";
1167
Barry Warsaw53699e91996-12-10 23:23:01 +00001168static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001169posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001170 PyObject *self;
1171 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001173 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001174 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001175 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001176
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001177/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001178#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001179 struct utimbuf buf;
1180#define ATIME buf.actime
1181#define MTIME buf.modtime
1182#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001183#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001184 time_t buf[2];
1185#define ATIME buf[0]
1186#define MTIME buf[1]
1187#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001188#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001189
Barry Warsaw53699e91996-12-10 23:23:01 +00001190 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001192 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001193 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001194 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001195 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001196 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001197 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001198 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001199 Py_INCREF(Py_None);
1200 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001201#undef UTIME_ARG
1202#undef ATIME
1203#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001204}
1205
Guido van Rossum85e3b011991-06-03 12:42:10 +00001206
Guido van Rossum3b066191991-06-04 19:40:25 +00001207/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001209static char posix__exit__doc__[] =
1210"_exit(status)\n\
1211Exit to the system with specified status, without normal exit processing.";
1212
Barry Warsaw53699e91996-12-10 23:23:01 +00001213static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001214posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001215 PyObject *self;
1216 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001217{
1218 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001219 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001220 return NULL;
1221 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001222 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001223}
1224
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001225
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001226#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001227static char posix_execv__doc__[] =
1228"execv(path, args)\n\
1229Execute an executable path with arguments, replacing current process.\n\
1230\n\
1231 path: path of executable file\n\
1232 args: tuple or list of strings";
1233
Barry Warsaw53699e91996-12-10 23:23:01 +00001234static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001235posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001236 PyObject *self;
1237 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001238{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001239 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001240 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001241 char **argvlist;
1242 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001243 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001244
Guido van Rossum89b33251993-10-22 14:26:06 +00001245 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001246 argv is a list or tuple of strings. */
1247
Barry Warsaw53699e91996-12-10 23:23:01 +00001248 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001249 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001250 if (PyList_Check(argv)) {
1251 argc = PyList_Size(argv);
1252 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001253 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001254 else if (PyTuple_Check(argv)) {
1255 argc = PyTuple_Size(argv);
1256 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001257 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001258 else {
1259 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001260 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001261 return NULL;
1262 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001263
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001265 if (argvlist == NULL)
1266 return NULL;
1267 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001268 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1269 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001270 goto badarg;
1271 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001272 }
1273 argvlist[argc] = NULL;
1274
Guido van Rossumb6775db1994-08-01 11:34:53 +00001275#ifdef BAD_EXEC_PROTOTYPES
1276 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001277#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001278 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001279#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001280
Guido van Rossum85e3b011991-06-03 12:42:10 +00001281 /* If we get here it's definitely an error */
1282
Barry Warsaw53699e91996-12-10 23:23:01 +00001283 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001284 return posix_error();
1285}
1286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001287
1288static char posix_execve__doc__[] =
1289"execve(path, args, env)\n\
1290Execute a path with arguments and environment, replacing current process.\n\
1291\n\
1292 path: path of executable file\n\
1293 args: tuple or list of arguments\n\
1294 env: dictonary of strings mapping to strings";
1295
Barry Warsaw53699e91996-12-10 23:23:01 +00001296static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001297posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001298 PyObject *self;
1299 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001300{
1301 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001302 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001303 char **argvlist;
1304 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001305 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001306 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001307 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001308
1309 /* execve has three arguments: (path, argv, env), where
1310 argv is a list or tuple of strings and env is a dictionary
1311 like posix.environ. */
1312
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001314 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001315 if (PyList_Check(argv)) {
1316 argc = PyList_Size(argv);
1317 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001318 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001319 else if (PyTuple_Check(argv)) {
1320 argc = PyTuple_Size(argv);
1321 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001322 }
1323 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001324 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001325 return NULL;
1326 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001327 if (!PyMapping_Check(env)) {
1328 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001329 return NULL;
1330 }
1331
Barry Warsaw53699e91996-12-10 23:23:01 +00001332 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001333 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001334 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001335 return NULL;
1336 }
1337 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001338 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001339 "s;argv must be list of strings",
1340 &argvlist[i]))
1341 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001342 goto fail_1;
1343 }
1344 }
1345 argvlist[argc] = NULL;
1346
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001347 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001348 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001349 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001350 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001351 goto fail_1;
1352 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001353 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001354 keys = PyMapping_Keys(env);
1355 vals = PyMapping_Values(env);
1356 if (!keys || !vals)
1357 goto fail_2;
1358
1359 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001360 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001361
1362 key = PyList_GetItem(keys, pos);
1363 val = PyList_GetItem(vals, pos);
1364 if (!key || !val)
1365 goto fail_2;
1366
Barry Warsaw53699e91996-12-10 23:23:01 +00001367 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001368 !PyArg_Parse(val, "s;non-string value in env", &v))
1369 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001370 goto fail_2;
1371 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001372
1373#if defined(PYOS_OS2)
1374 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1375 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1376#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001377 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001378 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001379 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001380 goto fail_2;
1381 }
1382 sprintf(p, "%s=%s", k, v);
1383 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001384#if defined(PYOS_OS2)
1385 }
1386#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001387 }
1388 envlist[envc] = 0;
1389
Guido van Rossumb6775db1994-08-01 11:34:53 +00001390
1391#ifdef BAD_EXEC_PROTOTYPES
1392 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001393#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001394 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001395#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001396
1397 /* If we get here it's definitely an error */
1398
1399 (void) posix_error();
1400
1401 fail_2:
1402 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001403 PyMem_DEL(envlist[envc]);
1404 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001405 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001406 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001407 Py_XDECREF(vals);
1408 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001409 return NULL;
1410}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001411#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001413
Guido van Rossuma1065681999-01-25 23:20:23 +00001414#ifdef HAVE_SPAWNV
1415static char posix_spawnv__doc__[] =
1416"spawnv(path, args)\n\
1417Execute an executable path with arguments, replacing current process.\n\
1418\n\
1419 path: path of executable file\n\
1420 args: tuple or list of strings";
1421
1422static PyObject *
1423posix_spawnv(self, args)
1424 PyObject *self;
1425 PyObject *args;
1426{
1427 char *path;
1428 PyObject *argv;
1429 char **argvlist;
1430 int mode, i, argc;
1431 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1432
1433 /* spawnv has three arguments: (mode, path, argv), where
1434 argv is a list or tuple of strings. */
1435
1436 if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
1437 return NULL;
1438 if (PyList_Check(argv)) {
1439 argc = PyList_Size(argv);
1440 getitem = PyList_GetItem;
1441 }
1442 else if (PyTuple_Check(argv)) {
1443 argc = PyTuple_Size(argv);
1444 getitem = PyTuple_GetItem;
1445 }
1446 else {
1447 badarg:
1448 PyErr_BadArgument();
1449 return NULL;
1450 }
1451
1452 argvlist = PyMem_NEW(char *, argc+1);
1453 if (argvlist == NULL)
1454 return NULL;
1455 for (i = 0; i < argc; i++) {
1456 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1457 PyMem_DEL(argvlist);
1458 goto badarg;
1459 }
1460 }
1461 argvlist[argc] = NULL;
1462
1463 i = _spawnv(mode, path, argvlist);
1464
1465 PyMem_DEL(argvlist);
1466
1467 if (i == -1)
1468 return posix_error();
1469 else
1470 return Py_BuildValue("i", i);
1471}
1472
1473
1474static char posix_spawnve__doc__[] =
1475"spawnve(path, args, env)\n\
1476Execute a path with arguments and environment, replacing current process.\n\
1477\n\
1478 path: path of executable file\n\
1479 args: tuple or list of arguments\n\
1480 env: dictonary of strings mapping to strings";
1481
1482static PyObject *
1483posix_spawnve(self, args)
1484 PyObject *self;
1485 PyObject *args;
1486{
1487 char *path;
1488 PyObject *argv, *env;
1489 char **argvlist;
1490 char **envlist;
1491 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1492 int mode, i, pos, argc, envc;
1493 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1494
1495 /* spawnve has four arguments: (mode, path, argv, env), where
1496 argv is a list or tuple of strings and env is a dictionary
1497 like posix.environ. */
1498
1499 if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
1500 return NULL;
1501 if (PyList_Check(argv)) {
1502 argc = PyList_Size(argv);
1503 getitem = PyList_GetItem;
1504 }
1505 else if (PyTuple_Check(argv)) {
1506 argc = PyTuple_Size(argv);
1507 getitem = PyTuple_GetItem;
1508 }
1509 else {
1510 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1511 return NULL;
1512 }
1513 if (!PyMapping_Check(env)) {
1514 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1515 return NULL;
1516 }
1517
1518 argvlist = PyMem_NEW(char *, argc+1);
1519 if (argvlist == NULL) {
1520 PyErr_NoMemory();
1521 return NULL;
1522 }
1523 for (i = 0; i < argc; i++) {
1524 if (!PyArg_Parse((*getitem)(argv, i),
1525 "s;argv must be list of strings",
1526 &argvlist[i]))
1527 {
1528 goto fail_1;
1529 }
1530 }
1531 argvlist[argc] = NULL;
1532
1533 i = PyMapping_Length(env);
1534 envlist = PyMem_NEW(char *, i + 1);
1535 if (envlist == NULL) {
1536 PyErr_NoMemory();
1537 goto fail_1;
1538 }
1539 envc = 0;
1540 keys = PyMapping_Keys(env);
1541 vals = PyMapping_Values(env);
1542 if (!keys || !vals)
1543 goto fail_2;
1544
1545 for (pos = 0; pos < i; pos++) {
1546 char *p, *k, *v;
1547
1548 key = PyList_GetItem(keys, pos);
1549 val = PyList_GetItem(vals, pos);
1550 if (!key || !val)
1551 goto fail_2;
1552
1553 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1554 !PyArg_Parse(val, "s;non-string value in env", &v))
1555 {
1556 goto fail_2;
1557 }
1558 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1559 if (p == NULL) {
1560 PyErr_NoMemory();
1561 goto fail_2;
1562 }
1563 sprintf(p, "%s=%s", k, v);
1564 envlist[envc++] = p;
1565 }
1566 envlist[envc] = 0;
1567
1568 i = _spawnve(mode, path, argvlist, envlist);
1569 if (i == -1)
1570 (void) posix_error();
1571 else
1572 res = Py_BuildValue("i", i);
1573
1574 fail_2:
1575 while (--envc >= 0)
1576 PyMem_DEL(envlist[envc]);
1577 PyMem_DEL(envlist);
1578 fail_1:
1579 PyMem_DEL(argvlist);
1580 Py_XDECREF(vals);
1581 Py_XDECREF(keys);
1582 return res;
1583}
1584#endif /* HAVE_SPAWNV */
1585
1586
Guido van Rossumad0ee831995-03-01 10:34:45 +00001587#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001588static char posix_fork__doc__[] =
1589"fork() -> pid\n\
1590Fork a child process.\n\
1591\n\
1592Return 0 to child process and PID of child to parent process.";
1593
Barry Warsaw53699e91996-12-10 23:23:01 +00001594static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001595posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001596 PyObject *self;
1597 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001598{
1599 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001600 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001601 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001602 pid = fork();
1603 if (pid == -1)
1604 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001605 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001606 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001607}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001608#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001609
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001610
Guido van Rossumad0ee831995-03-01 10:34:45 +00001611#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001612static char posix_getegid__doc__[] =
1613"getegid() -> egid\n\
1614Return the current process's effective group id.";
1615
Barry Warsaw53699e91996-12-10 23:23:01 +00001616static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001617posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001618 PyObject *self;
1619 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001620{
Barry Warsaw53699e91996-12-10 23:23:01 +00001621 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001622 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001623 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001624}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001625#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001627
Guido van Rossumad0ee831995-03-01 10:34:45 +00001628#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001629static char posix_geteuid__doc__[] =
1630"geteuid() -> euid\n\
1631Return the current process's effective user id.";
1632
Barry Warsaw53699e91996-12-10 23:23:01 +00001633static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001634posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001635 PyObject *self;
1636 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001637{
Barry Warsaw53699e91996-12-10 23:23:01 +00001638 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001641}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001642#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001644
Guido van Rossumad0ee831995-03-01 10:34:45 +00001645#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001646static char posix_getgid__doc__[] =
1647"getgid() -> gid\n\
1648Return the current process's group id.";
1649
Barry Warsaw53699e91996-12-10 23:23:01 +00001650static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001651posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001652 PyObject *self;
1653 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001654{
Barry Warsaw53699e91996-12-10 23:23:01 +00001655 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001656 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001657 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001658}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001659#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001660
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001661
1662static char posix_getpid__doc__[] =
1663"getpid() -> pid\n\
1664Return the current process id";
1665
Barry Warsaw53699e91996-12-10 23:23:01 +00001666static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001667posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001668 PyObject *self;
1669 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001670{
Barry Warsaw53699e91996-12-10 23:23:01 +00001671 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001672 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001673 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001674}
1675
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001676
Guido van Rossumb6775db1994-08-01 11:34:53 +00001677#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001678static char posix_getpgrp__doc__[] =
1679"getpgrp() -> pgrp\n\
1680Return the current process group id.";
1681
Barry Warsaw53699e91996-12-10 23:23:01 +00001682static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001683posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001684 PyObject *self;
1685 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001686{
Barry Warsaw53699e91996-12-10 23:23:01 +00001687 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001688 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001689#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001690 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001691#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001692 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001693#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001694}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001695#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001697
Guido van Rossumb6775db1994-08-01 11:34:53 +00001698#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001699static char posix_setpgrp__doc__[] =
1700"setpgrp() -> None\n\
1701Make this process a session leader.";
1702
Barry Warsaw53699e91996-12-10 23:23:01 +00001703static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001704posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001705 PyObject *self;
1706 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001707{
Barry Warsaw53699e91996-12-10 23:23:01 +00001708 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001709 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001710#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001711 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001712#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001713 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001714#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001715 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001716 Py_INCREF(Py_None);
1717 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001718}
1719
Guido van Rossumb6775db1994-08-01 11:34:53 +00001720#endif /* HAVE_SETPGRP */
1721
Guido van Rossumad0ee831995-03-01 10:34:45 +00001722#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001723static char posix_getppid__doc__[] =
1724"getppid() -> ppid\n\
1725Return the parent's process id.";
1726
Barry Warsaw53699e91996-12-10 23:23:01 +00001727static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001728posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001729 PyObject *self;
1730 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001731{
Barry Warsaw53699e91996-12-10 23:23:01 +00001732 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001733 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001734 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001735}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001736#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001737
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001738
Guido van Rossumad0ee831995-03-01 10:34:45 +00001739#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001740static char posix_getuid__doc__[] =
1741"getuid() -> uid\n\
1742Return the current process's user id.";
1743
Barry Warsaw53699e91996-12-10 23:23:01 +00001744static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001745posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001746 PyObject *self;
1747 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001748{
Barry Warsaw53699e91996-12-10 23:23:01 +00001749 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001750 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001751 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001752}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001753#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001754
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001755
Guido van Rossumad0ee831995-03-01 10:34:45 +00001756#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001757static char posix_kill__doc__[] =
1758"kill(pid, sig) -> None\n\
1759Kill a process with a signal.";
1760
Barry Warsaw53699e91996-12-10 23:23:01 +00001761static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001762posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001763 PyObject *self;
1764 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001765{
1766 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001767 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001768 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001769#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001770 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1771 APIRET rc;
1772 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001773 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001774
1775 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1776 APIRET rc;
1777 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001778 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001779
1780 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001781 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001782#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001783 if (kill(pid, sig) == -1)
1784 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001785#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001786 Py_INCREF(Py_None);
1787 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001788}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001789#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001790
Guido van Rossumc0125471996-06-28 18:55:32 +00001791#ifdef HAVE_PLOCK
1792
1793#ifdef HAVE_SYS_LOCK_H
1794#include <sys/lock.h>
1795#endif
1796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001797static char posix_plock__doc__[] =
1798"plock(op) -> None\n\
1799Lock program segments into memory.";
1800
Barry Warsaw53699e91996-12-10 23:23:01 +00001801static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001802posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001803 PyObject *self;
1804 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001805{
1806 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001807 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001808 return NULL;
1809 if (plock(op) == -1)
1810 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001811 Py_INCREF(Py_None);
1812 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001813}
1814#endif
1815
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001816
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001817#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001818static char posix_popen__doc__[] =
1819"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1820Open a pipe to/from a command returning a file object.";
1821
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001822#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001823static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001824async_system(const char *command)
1825{
1826 char *p, errormsg[256], args[1024];
1827 RESULTCODES rcodes;
1828 APIRET rc;
1829 char *shell = getenv("COMSPEC");
1830 if (!shell)
1831 shell = "cmd";
1832
1833 strcpy(args, shell);
1834 p = &args[ strlen(args)+1 ];
1835 strcpy(p, "/c ");
1836 strcat(p, command);
1837 p += strlen(p) + 1;
1838 *p = '\0';
1839
1840 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001841 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001842 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001843 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001844 &rcodes, shell);
1845 return rc;
1846}
1847
Guido van Rossumd48f2521997-12-05 22:19:34 +00001848static FILE *
1849popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001850{
1851 HFILE rhan, whan;
1852 FILE *retfd = NULL;
1853 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1854
Guido van Rossumd48f2521997-12-05 22:19:34 +00001855 if (rc != NO_ERROR) {
1856 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001857 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001858 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001859
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001860 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1861 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001862
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001863 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1864 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001865
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001866 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1867 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001868
1869 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001870 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001871 }
1872
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001873 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1874 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001875
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001876 close(oldfd); /* And Close Saved STDOUT Handle */
1877 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001878
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001879 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1880 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001881
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001882 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1883 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001884
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001885 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1886 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001887
1888 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001889 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001890 }
1891
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001892 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1893 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001894
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001895 close(oldfd); /* And Close Saved STDIN Handle */
1896 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001897
Guido van Rossumd48f2521997-12-05 22:19:34 +00001898 } else {
1899 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001900 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001901 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001902}
1903
1904static PyObject *
1905posix_popen(self, args)
1906 PyObject *self;
1907 PyObject *args;
1908{
1909 char *name;
1910 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001911 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001912 FILE *fp;
1913 PyObject *f;
1914 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1915 return NULL;
1916 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001917 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001918 Py_END_ALLOW_THREADS
1919 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001920 return os2_error(err);
1921
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001922 f = PyFile_FromFile(fp, name, mode, fclose);
1923 if (f != NULL)
1924 PyFile_SetBufSize(f, bufsize);
1925 return f;
1926}
1927
1928#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001929static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001930posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001931 PyObject *self;
1932 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001933{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001934 char *name;
1935 char *mode = "r";
1936 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001937 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001938 PyObject *f;
1939 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001940 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001941 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001942 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001943 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001944 if (fp == NULL)
1945 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001946 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001947 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001948 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001949 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001950}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001951#endif
1952
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001953#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001954
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001955
Guido van Rossumb6775db1994-08-01 11:34:53 +00001956#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001957static char posix_setuid__doc__[] =
1958"setuid(uid) -> None\n\
1959Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001960static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001961posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001962 PyObject *self;
1963 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001964{
1965 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001966 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001967 return NULL;
1968 if (setuid(uid) < 0)
1969 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001970 Py_INCREF(Py_None);
1971 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001972}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001973#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001974
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001977static char posix_setgid__doc__[] =
1978"setgid(gid) -> None\n\
1979Set the current process's group id.";
1980
Barry Warsaw53699e91996-12-10 23:23:01 +00001981static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001982posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001983 PyObject *self;
1984 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001985{
1986 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001987 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001988 return NULL;
1989 if (setgid(gid) < 0)
1990 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001991 Py_INCREF(Py_None);
1992 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001993}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001994#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001996
Guido van Rossumb6775db1994-08-01 11:34:53 +00001997#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001998static char posix_waitpid__doc__[] =
1999"waitpid(pid, options) -> (pid, status)\n\
2000Wait for completion of a give child process.";
2001
Barry Warsaw53699e91996-12-10 23:23:01 +00002002static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002003posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002004 PyObject *self;
2005 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002006{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002007 int pid, options;
2008#ifdef UNION_WAIT
2009 union wait status;
2010#define status_i (status.w_status)
2011#else
2012 int status;
2013#define status_i status
2014#endif
2015 status_i = 0;
2016
Barry Warsaw53699e91996-12-10 23:23:01 +00002017 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002018 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002019 Py_BEGIN_ALLOW_THREADS
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002020 pid = wait4(pid, &status, options, NULL);
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002022 if (pid == -1)
2023 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002024 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002025 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002026}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002027#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002029
Guido van Rossumad0ee831995-03-01 10:34:45 +00002030#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002031static char posix_wait__doc__[] =
2032"wait() -> (pid, status)\n\
2033Wait for completion of a child process.";
2034
Barry Warsaw53699e91996-12-10 23:23:01 +00002035static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002036posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002037 PyObject *self;
2038 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002039{
2040 int pid, sts;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002041#ifdef UNION_WAIT
2042 union wait status;
2043#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002044#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002045 int status;
2046#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002047#endif
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002048 status_i = 0;
2049 Py_BEGIN_ALLOW_THREADS
2050 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002051 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002052 if (pid == -1)
2053 return posix_error();
2054 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002055 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002056}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002057#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002058
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002059
2060static char posix_lstat__doc__[] =
2061"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2062Like stat(path), but do not follow symbolic links.";
2063
Barry Warsaw53699e91996-12-10 23:23:01 +00002064static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002065posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002066 PyObject *self;
2067 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002068{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002069#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002071#else /* !HAVE_LSTAT */
2072 return posix_do_stat(self, args, stat);
2073#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002074}
2075
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002076
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002078static char posix_readlink__doc__[] =
2079"readlink(path) -> path\n\
2080Return a string representing the path to which the symbolic link points.";
2081
Barry Warsaw53699e91996-12-10 23:23:01 +00002082static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002083posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002084 PyObject *self;
2085 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002086{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002087 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002088 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002089 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002090 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002091 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002092 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002093 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002095 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002096 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002097 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002098}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002099#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002100
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002101
Guido van Rossumb6775db1994-08-01 11:34:53 +00002102#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002103static char posix_symlink__doc__[] =
2104"symlink(src, dst) -> None\n\
2105Create a symbolic link.";
2106
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002107static PyObject *
2108posix_symlink(self, args)
2109 PyObject *self;
2110 PyObject *args;
2111{
2112 return posix_2str(args, symlink);
2113}
2114#endif /* HAVE_SYMLINK */
2115
2116
2117#ifdef HAVE_TIMES
2118#ifndef HZ
2119#define HZ 60 /* Universal constant :-) */
2120#endif /* HZ */
2121
Guido van Rossumd48f2521997-12-05 22:19:34 +00002122#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2123static long
2124system_uptime()
2125{
2126 ULONG value = 0;
2127
2128 Py_BEGIN_ALLOW_THREADS
2129 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2130 Py_END_ALLOW_THREADS
2131
2132 return value;
2133}
2134
2135static PyObject *
2136posix_times(self, args)
2137 PyObject *self;
2138 PyObject *args;
2139{
2140 if (!PyArg_NoArgs(args))
2141 return NULL;
2142
2143 /* Currently Only Uptime is Provided -- Others Later */
2144 return Py_BuildValue("ddddd",
2145 (double)0 /* t.tms_utime / HZ */,
2146 (double)0 /* t.tms_stime / HZ */,
2147 (double)0 /* t.tms_cutime / HZ */,
2148 (double)0 /* t.tms_cstime / HZ */,
2149 (double)system_uptime() / 1000);
2150}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002151#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002152static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002153posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002154 PyObject *self;
2155 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002156{
2157 struct tms t;
2158 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00002159 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002160 return NULL;
2161 errno = 0;
2162 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002163 if (c == (clock_t) -1)
2164 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002165 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002166 (double)t.tms_utime / HZ,
2167 (double)t.tms_stime / HZ,
2168 (double)t.tms_cutime / HZ,
2169 (double)t.tms_cstime / HZ,
2170 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002171}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002172#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002173#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002174
2175
Guido van Rossum87755a21996-09-07 00:59:43 +00002176#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002177#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002178static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002179posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002180 PyObject *self;
2181 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002182{
2183 FILETIME create, exit, kernel, user;
2184 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00002185 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002186 return NULL;
2187 hProc = GetCurrentProcess();
2188 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00002189 return Py_BuildValue(
2190 "ddddd",
2191 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
2192 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
2193 (double)0,
2194 (double)0,
2195 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002196}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002197#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002198
2199#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002200static char posix_times__doc__[] =
2201"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2202Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002203#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002204
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002205
Guido van Rossumb6775db1994-08-01 11:34:53 +00002206#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002207static char posix_setsid__doc__[] =
2208"setsid() -> None\n\
2209Call the system call setsid().";
2210
Barry Warsaw53699e91996-12-10 23:23:01 +00002211static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002212posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002213 PyObject *self;
2214 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002215{
Barry Warsaw53699e91996-12-10 23:23:01 +00002216 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002217 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002218 if (setsid() < 0)
2219 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002220 Py_INCREF(Py_None);
2221 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002222}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002223#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002224
Guido van Rossumb6775db1994-08-01 11:34:53 +00002225#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002226static char posix_setpgid__doc__[] =
2227"setpgid(pid, pgrp) -> None\n\
2228Call the system call setpgid().";
2229
Barry Warsaw53699e91996-12-10 23:23:01 +00002230static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002231posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002232 PyObject *self;
2233 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002234{
2235 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002236 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002237 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002238 if (setpgid(pid, pgrp) < 0)
2239 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002240 Py_INCREF(Py_None);
2241 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002242}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002243#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002244
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002245
Guido van Rossumb6775db1994-08-01 11:34:53 +00002246#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002247static char posix_tcgetpgrp__doc__[] =
2248"tcgetpgrp(fd) -> pgid\n\
2249Return the process group associated with the terminal given by a fd.";
2250
Barry Warsaw53699e91996-12-10 23:23:01 +00002251static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002252posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002253 PyObject *self;
2254 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002255{
2256 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002257 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002258 return NULL;
2259 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002260 if (pgid < 0)
2261 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002262 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002263}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002264#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002266
Guido van Rossumb6775db1994-08-01 11:34:53 +00002267#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002268static char posix_tcsetpgrp__doc__[] =
2269"tcsetpgrp(fd, pgid) -> None\n\
2270Set the process group associated with the terminal given by a fd.";
2271
Barry Warsaw53699e91996-12-10 23:23:01 +00002272static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002273posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002274 PyObject *self;
2275 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002276{
2277 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002278 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002279 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002280 if (tcsetpgrp(fd, pgid) < 0)
2281 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002282 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002283 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002284}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002285#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002286
Guido van Rossum687dd131993-05-17 08:34:16 +00002287/* Functions acting on file descriptors */
2288
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002289static char posix_open__doc__[] =
2290"open(filename, flag [, mode=0777]) -> fd\n\
2291Open a file (for low level IO).";
2292
Barry Warsaw53699e91996-12-10 23:23:01 +00002293static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002294posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002295 PyObject *self;
2296 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002297{
2298 char *file;
2299 int flag;
2300 int mode = 0777;
2301 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002302 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2303 return NULL;
2304
Barry Warsaw53699e91996-12-10 23:23:01 +00002305 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002306 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002307 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002308 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002309 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002310 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002311}
2312
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002313
2314static char posix_close__doc__[] =
2315"close(fd) -> None\n\
2316Close a file descriptor (for low level IO).";
2317
Barry Warsaw53699e91996-12-10 23:23:01 +00002318static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002319posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002320 PyObject *self;
2321 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002322{
2323 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002324 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002325 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002326 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002327 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002328 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002329 if (res < 0)
2330 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002331 Py_INCREF(Py_None);
2332 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002333}
2334
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002335
2336static char posix_dup__doc__[] =
2337"dup(fd) -> fd2\n\
2338Return a duplicate of a file descriptor.";
2339
Barry Warsaw53699e91996-12-10 23:23:01 +00002340static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002341posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002342 PyObject *self;
2343 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002344{
2345 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002346 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002347 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002348 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002349 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002350 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002351 if (fd < 0)
2352 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002353 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002354}
2355
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002356
2357static char posix_dup2__doc__[] =
2358"dup2(fd, fd2) -> None\n\
2359Duplicate file descriptor.";
2360
Barry Warsaw53699e91996-12-10 23:23:01 +00002361static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002362posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002363 PyObject *self;
2364 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002365{
2366 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002367 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002368 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002369 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002370 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002371 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002372 if (res < 0)
2373 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002374 Py_INCREF(Py_None);
2375 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002376}
2377
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002378
2379static char posix_lseek__doc__[] =
2380"lseek(fd, pos, how) -> newpos\n\
2381Set the current position of a file descriptor.";
2382
Barry Warsaw53699e91996-12-10 23:23:01 +00002383static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002384posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002385 PyObject *self;
2386 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002387{
2388 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002389 off_t pos, res;
2390 PyObject *posobj;
2391 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002392 return NULL;
2393#ifdef SEEK_SET
2394 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2395 switch (how) {
2396 case 0: how = SEEK_SET; break;
2397 case 1: how = SEEK_CUR; break;
2398 case 2: how = SEEK_END; break;
2399 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002400#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002401
2402#if !defined(HAVE_LARGEFILE_SUPPORT)
2403 pos = PyInt_AsLong(posobj);
2404#else
2405 pos = PyLong_Check(posobj) ?
2406 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2407#endif
2408 if (PyErr_Occurred())
2409 return NULL;
2410
Barry Warsaw53699e91996-12-10 23:23:01 +00002411 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002412 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002413 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002414 if (res < 0)
2415 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002416
2417#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002418 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002419#else
2420 return PyLong_FromLongLong(res);
2421#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002422}
2423
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002424
2425static char posix_read__doc__[] =
2426"read(fd, buffersize) -> string\n\
2427Read a file descriptor.";
2428
Barry Warsaw53699e91996-12-10 23:23:01 +00002429static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002430posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002431 PyObject *self;
2432 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002433{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002434 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002435 PyObject *buffer;
2436 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002437 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002438 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002439 if (buffer == NULL)
2440 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002441 Py_BEGIN_ALLOW_THREADS
2442 n = read(fd, PyString_AsString(buffer), size);
2443 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002444 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002445 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002446 return posix_error();
2447 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002448 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002449 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002450 return buffer;
2451}
2452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002453
2454static char posix_write__doc__[] =
2455"write(fd, string) -> byteswritten\n\
2456Write a string to a file descriptor.";
2457
Barry Warsaw53699e91996-12-10 23:23:01 +00002458static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002459posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002460 PyObject *self;
2461 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002462{
2463 int fd, size;
2464 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002465 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002466 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002467 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002468 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002469 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002470 if (size < 0)
2471 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002472 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002473}
2474
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002475
2476static char posix_fstat__doc__[]=
2477"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2478Like stat(), but for an open file descriptor.";
2479
Barry Warsaw53699e91996-12-10 23:23:01 +00002480static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002481posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002482 PyObject *self;
2483 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002484{
2485 int fd;
2486 struct stat st;
2487 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002488 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002489 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002490 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002491 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002492 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002493 if (res != 0)
2494 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002495#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002496 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002497 (long)st.st_mode,
2498 (long)st.st_ino,
2499 (long)st.st_dev,
2500 (long)st.st_nlink,
2501 (long)st.st_uid,
2502 (long)st.st_gid,
2503 (long)st.st_size,
2504 (long)st.st_atime,
2505 (long)st.st_mtime,
2506 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002507#else
2508 return Py_BuildValue("(lLllllLlll)",
2509 (long)st.st_mode,
2510 (LONG_LONG)st.st_ino,
2511 (long)st.st_dev,
2512 (long)st.st_nlink,
2513 (long)st.st_uid,
2514 (long)st.st_gid,
2515 (LONG_LONG)st.st_size,
2516 (long)st.st_atime,
2517 (long)st.st_mtime,
2518 (long)st.st_ctime);
2519#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002520}
2521
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002522
2523static char posix_fdopen__doc__[] =
2524"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2525Return an open file object connected to a file descriptor.";
2526
Barry Warsaw53699e91996-12-10 23:23:01 +00002527static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002528posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002529 PyObject *self;
2530 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002531{
Barry Warsaw53699e91996-12-10 23:23:01 +00002532 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002533 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002534 char *mode = "r";
2535 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002536 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002537 PyObject *f;
2538 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002539 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002540
Barry Warsaw53699e91996-12-10 23:23:01 +00002541 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002542 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002543 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002544 if (fp == NULL)
2545 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002546 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002547 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002548 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002549 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002550}
2551
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002552
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002553#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002554static char posix_pipe__doc__[] =
2555"pipe() -> (read_end, write_end)\n\
2556Create a pipe.";
2557
Barry Warsaw53699e91996-12-10 23:23:01 +00002558static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002559posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002560 PyObject *self;
2561 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002562{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002563#if defined(PYOS_OS2)
2564 HFILE read, write;
2565 APIRET rc;
2566
2567 if (!PyArg_Parse(args, ""))
2568 return NULL;
2569
2570 Py_BEGIN_ALLOW_THREADS
2571 rc = DosCreatePipe( &read, &write, 4096);
2572 Py_END_ALLOW_THREADS
2573 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002574 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002575
2576 return Py_BuildValue("(ii)", read, write);
2577#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002578#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002579 int fds[2];
2580 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002581 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002582 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002583 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002584 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002585 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002586 if (res != 0)
2587 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002588 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002589#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002590 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002591 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002592 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002593 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002594 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002595 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002596 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002597 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002598 if (!ok)
2599 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002600 read_fd = _open_osfhandle((long)read, 0);
2601 write_fd = _open_osfhandle((long)write, 1);
2602 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002603#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002604#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002605}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002606#endif /* HAVE_PIPE */
2607
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002608
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002609#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002610static char posix_mkfifo__doc__[] =
2611"mkfifo(file, [, mode=0666]) -> None\n\
2612Create a FIFO (a POSIX named pipe).";
2613
Barry Warsaw53699e91996-12-10 23:23:01 +00002614static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002615posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002616 PyObject *self;
2617 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002618{
2619 char *file;
2620 int mode = 0666;
2621 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002622 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002623 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002624 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002625 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002626 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002627 if (res < 0)
2628 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002629 Py_INCREF(Py_None);
2630 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002631}
2632#endif
2633
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002634
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002635#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002636static char posix_ftruncate__doc__[] =
2637"ftruncate(fd, length) -> None\n\
2638Truncate a file to a specified length.";
2639
Barry Warsaw53699e91996-12-10 23:23:01 +00002640static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002641posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002642 PyObject *self; /* Not used */
2643 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002644{
2645 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002646 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002647 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002648 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002649
Guido van Rossum94f6f721999-01-06 18:42:14 +00002650 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2651 return NULL;
2652
2653#if !defined(HAVE_LARGEFILE_SUPPORT)
2654 length = PyInt_AsLong(lenobj);
2655#else
2656 length = PyLong_Check(lenobj) ?
2657 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2658#endif
2659 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002660 return NULL;
2661
Barry Warsaw53699e91996-12-10 23:23:01 +00002662 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002663 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002664 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002665 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002666 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002667 return NULL;
2668 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002669 Py_INCREF(Py_None);
2670 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002671}
2672#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002673
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002674#ifdef NeXT
2675#define HAVE_PUTENV
2676/* Steve Spicklemire got this putenv from NeXTAnswers */
2677static int
2678putenv(char *newval)
2679{
2680 extern char **environ;
2681
2682 static int firstTime = 1;
2683 char **ep;
2684 char *cp;
2685 int esiz;
2686 char *np;
2687
2688 if (!(np = strchr(newval, '=')))
2689 return 1;
2690 *np = '\0';
2691
2692 /* look it up */
2693 for (ep=environ ; *ep ; ep++)
2694 {
2695 /* this should always be true... */
2696 if (cp = strchr(*ep, '='))
2697 {
2698 *cp = '\0';
2699 if (!strcmp(*ep, newval))
2700 {
2701 /* got it! */
2702 *cp = '=';
2703 break;
2704 }
2705 *cp = '=';
2706 }
2707 else
2708 {
2709 *np = '=';
2710 return 1;
2711 }
2712 }
2713
2714 *np = '=';
2715 if (*ep)
2716 {
2717 /* the string was already there:
2718 just replace it with the new one */
2719 *ep = newval;
2720 return 0;
2721 }
2722
2723 /* expand environ by one */
2724 for (esiz=2, ep=environ ; *ep ; ep++)
2725 esiz++;
2726 if (firstTime)
2727 {
2728 char **epp;
2729 char **newenv;
2730 if (!(newenv = malloc(esiz * sizeof(char *))))
2731 return 1;
2732
2733 for (ep=environ, epp=newenv ; *ep ;)
2734 *epp++ = *ep++;
2735 *epp++ = newval;
2736 *epp = (char *) 0;
2737 environ = newenv;
2738 }
2739 else
2740 {
2741 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2742 return 1;
2743 environ[esiz - 2] = newval;
2744 environ[esiz - 1] = (char *) 0;
2745 firstTime = 0;
2746 }
2747
2748 return 0;
2749}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002750#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002752
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002753#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002754static char posix_putenv__doc__[] =
2755"putenv(key, value) -> None\n\
2756Change or add an environment variable.";
2757
Guido van Rossumbcc20741998-08-04 22:53:56 +00002758#ifdef __BEOS__
2759/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2760int putenv( const char *str );
2761#endif
2762
Barry Warsaw53699e91996-12-10 23:23:01 +00002763static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002764posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002765 PyObject *self;
2766 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002767{
2768 char *s1, *s2;
2769 char *new;
2770
Barry Warsaw53699e91996-12-10 23:23:01 +00002771 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002772 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002773
2774#if defined(PYOS_OS2)
2775 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2776 APIRET rc;
2777
2778 if (strlen(s2) == 0) /* If New Value is an Empty String */
2779 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2780
2781 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2782 if (rc != NO_ERROR)
2783 return os2_error(rc);
2784
2785 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2786 APIRET rc;
2787
2788 if (strlen(s2) == 0) /* If New Value is an Empty String */
2789 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2790
2791 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2792 if (rc != NO_ERROR)
2793 return os2_error(rc);
2794 } else {
2795#endif
2796
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002797 /* XXX This leaks memory -- not easy to fix :-( */
2798 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002799 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002800 (void) sprintf(new, "%s=%s", s1, s2);
2801 if (putenv(new)) {
2802 posix_error();
2803 return NULL;
2804 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002805
2806#if defined(PYOS_OS2)
2807 }
2808#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002809 Py_INCREF(Py_None);
2810 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002811}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002812#endif /* putenv */
2813
2814#ifdef HAVE_STRERROR
2815static char posix_strerror__doc__[] =
2816"strerror(code) -> string\n\
2817Translate an error code to a message string.";
2818
2819PyObject *
2820posix_strerror(self, args)
2821 PyObject *self;
2822 PyObject *args;
2823{
2824 int code;
2825 char *message;
2826 if (!PyArg_ParseTuple(args, "i", &code))
2827 return NULL;
2828 message = strerror(code);
2829 if (message == NULL) {
2830 PyErr_SetString(PyExc_ValueError,
2831 "strerror code out of range");
2832 return NULL;
2833 }
2834 return PyString_FromString(message);
2835}
2836#endif /* strerror */
2837
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002838
Guido van Rossumc9641791998-08-04 15:26:23 +00002839#ifdef HAVE_SYS_WAIT_H
2840
2841#ifdef WIFSTOPPED
2842static char posix_WIFSTOPPED__doc__[] =
2843"WIFSTOPPED(status) -> Boolean\n\
2844See Unix documentation.";
2845
2846static PyObject *
2847posix_WIFSTOPPED(self, args)
2848 PyObject *self;
2849 PyObject *args;
2850{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002851#ifdef UNION_WAIT
2852 union wait status;
2853#define status_i (status.w_status)
2854#else
2855 int status;
2856#define status_i status
2857#endif
2858 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002859
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002860 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002861 {
2862 return NULL;
2863 }
2864
2865 return Py_BuildValue("i", WIFSTOPPED(status));
2866}
2867#endif /* WIFSTOPPED */
2868
2869#ifdef WIFSIGNALED
2870static char posix_WIFSIGNALED__doc__[] =
2871"WIFSIGNALED(status) -> Boolean\n\
2872See Unix documentation.";
2873
2874static PyObject *
2875posix_WIFSIGNALED(self, args)
2876 PyObject *self;
2877 PyObject *args;
2878{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002879#ifdef UNION_WAIT
2880 union wait status;
2881#define status_i (status.w_status)
2882#else
2883 int status;
2884#define status_i status
2885#endif
2886 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002887
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002888 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002889 {
2890 return NULL;
2891 }
2892
2893 return Py_BuildValue("i", WIFSIGNALED(status));
2894}
2895#endif /* WIFSIGNALED */
2896
2897#ifdef WIFEXITED
2898static char posix_WIFEXITED__doc__[] =
2899"WIFEXITED(status) -> Boolean\n\
2900See Unix documentation.";
2901
2902static PyObject *
2903posix_WIFEXITED(self, args)
2904 PyObject *self;
2905 PyObject *args;
2906{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002907#ifdef UNION_WAIT
2908 union wait status;
2909#define status_i (status.w_status)
2910#else
2911 int status;
2912#define status_i status
2913#endif
2914 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002915
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002916 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002917 {
2918 return NULL;
2919 }
2920
2921 return Py_BuildValue("i", WIFEXITED(status));
2922}
2923#endif /* WIFEXITED */
2924
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002925#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002926static char posix_WEXITSTATUS__doc__[] =
2927"WEXITSTATUS(status) -> integer\n\
2928See Unix documentation.";
2929
2930static PyObject *
2931posix_WEXITSTATUS(self, args)
2932 PyObject *self;
2933 PyObject *args;
2934{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002935#ifdef UNION_WAIT
2936 union wait status;
2937#define status_i (status.w_status)
2938#else
2939 int status;
2940#define status_i status
2941#endif
2942 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002943
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002944 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002945 {
2946 return NULL;
2947 }
2948
2949 return Py_BuildValue("i", WEXITSTATUS(status));
2950}
2951#endif /* WEXITSTATUS */
2952
2953#ifdef WTERMSIG
2954static char posix_WTERMSIG__doc__[] =
2955"WTERMSIG(status) -> integer\n\
2956See Unix documentation.";
2957
2958static PyObject *
2959posix_WTERMSIG(self, args)
2960 PyObject *self;
2961 PyObject *args;
2962{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002963#ifdef UNION_WAIT
2964 union wait status;
2965#define status_i (status.w_status)
2966#else
2967 int status;
2968#define status_i status
2969#endif
2970 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002971
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002972 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002973 {
2974 return NULL;
2975 }
2976
2977 return Py_BuildValue("i", WTERMSIG(status));
2978}
2979#endif /* WTERMSIG */
2980
2981#ifdef WSTOPSIG
2982static char posix_WSTOPSIG__doc__[] =
2983"WSTOPSIG(status) -> integer\n\
2984See Unix documentation.";
2985
2986static PyObject *
2987posix_WSTOPSIG(self, args)
2988 PyObject *self;
2989 PyObject *args;
2990{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002991#ifdef UNION_WAIT
2992 union wait status;
2993#define status_i (status.w_status)
2994#else
2995 int status;
2996#define status_i status
2997#endif
2998 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002999
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003000 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003001 {
3002 return NULL;
3003 }
3004
3005 return Py_BuildValue("i", WSTOPSIG(status));
3006}
3007#endif /* WSTOPSIG */
3008
3009#endif /* HAVE_SYS_WAIT_H */
3010
3011
Guido van Rossum94f6f721999-01-06 18:42:14 +00003012#if defined(HAVE_FSTATVFS)
3013#include <sys/statvfs.h>
3014
3015static char posix_fstatvfs__doc__[] =
3016"fstatvfs(fd) -> \
3017(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
3018Perform an fstatvfs system call on the given fd.";
3019
3020static PyObject *
3021posix_fstatvfs(self, args)
3022 PyObject *self;
3023 PyObject *args;
3024{
3025 int fd, res;
3026 struct statvfs st;
3027 if (!PyArg_ParseTuple(args, "i", &fd))
3028 return NULL;
3029 Py_BEGIN_ALLOW_THREADS
3030 res = fstatvfs(fd, &st);
3031 Py_END_ALLOW_THREADS
3032 if (res != 0)
3033 return posix_error();
3034#if !defined(HAVE_LARGEFILE_SUPPORT)
3035 return Py_BuildValue("(lllllllllll)",
3036 (long) st.f_bsize,
3037 (long) st.f_frsize,
3038 (long) st.f_blocks,
3039 (long) st.f_bfree,
3040 (long) st.f_bavail,
3041 (long) st.f_files,
3042 (long) st.f_ffree,
3043 (long) st.f_favail,
3044 (long) st.f_fsid,
3045 (long) st.f_flag,
3046 (long) st.f_namemax);
3047#else
3048 return Py_BuildValue("(llLLLLLLlll)",
3049 (long) st.f_bsize,
3050 (long) st.f_frsize,
3051 (LONG_LONG) st.f_blocks,
3052 (LONG_LONG) st.f_bfree,
3053 (LONG_LONG) st.f_bavail,
3054 (LONG_LONG) st.f_files,
3055 (LONG_LONG) st.f_ffree,
3056 (LONG_LONG) st.f_favail,
3057 (long) st.f_fsid,
3058 (long) st.f_flag,
3059 (long) st.f_namemax);
3060#endif
3061}
3062#endif /* HAVE_FSTATVFS */
3063
3064
3065#if defined(HAVE_STATVFS)
3066#include <sys/statvfs.h>
3067
3068static char posix_statvfs__doc__[] =
3069"statvfs(path) -> \
3070(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
3071Perform a statvfs system call on the given path.";
3072
3073static PyObject *
3074posix_statvfs(self, args)
3075 PyObject *self;
3076 PyObject *args;
3077{
3078 char *path;
3079 int res;
3080 struct statvfs st;
3081 if (!PyArg_ParseTuple(args, "s", &path))
3082 return NULL;
3083 Py_BEGIN_ALLOW_THREADS
3084 res = statvfs(path, &st);
3085 Py_END_ALLOW_THREADS
3086 if (res != 0)
3087 return posix_error_with_filename(path);
3088#if !defined(HAVE_LARGEFILE_SUPPORT)
3089 return Py_BuildValue("(lllllllllll)",
3090 (long) st.f_bsize,
3091 (long) st.f_frsize,
3092 (long) st.f_blocks,
3093 (long) st.f_bfree,
3094 (long) st.f_bavail,
3095 (long) st.f_files,
3096 (long) st.f_ffree,
3097 (long) st.f_favail,
3098 (long) st.f_fsid,
3099 (long) st.f_flag,
3100 (long) st.f_namemax);
3101#else /* HAVE_LARGEFILE_SUPPORT */
3102 return Py_BuildValue("(llLLLLLLlll)",
3103 (long) st.f_bsize,
3104 (long) st.f_frsize,
3105 (LONG_LONG) st.f_blocks,
3106 (LONG_LONG) st.f_bfree,
3107 (LONG_LONG) st.f_bavail,
3108 (LONG_LONG) st.f_files,
3109 (LONG_LONG) st.f_ffree,
3110 (LONG_LONG) st.f_favail,
3111 (long) st.f_fsid,
3112 (long) st.f_flag,
3113 (long) st.f_namemax);
3114#endif
3115}
3116#endif /* HAVE_STATVFS */
3117
3118
Barry Warsaw53699e91996-12-10 23:23:01 +00003119static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003120 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003121#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003122 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003123#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003124 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3125 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003126#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003127 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003128#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003129#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003130 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003131#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003132#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003133 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003134#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003135 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3136 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3137 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003138#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003140#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003141#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003142 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003143#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003144 {"rename", posix_rename, 0, posix_rename__doc__},
3145 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3146 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003147#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003148 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003149#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003150#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003151 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003152#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003153 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003154#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003155 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003156#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003157 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3158 {"remove", posix_unlink, 0, posix_remove__doc__},
3159 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003160#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003161 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003162#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003163 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003164#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165 {"execv", posix_execv, 0, posix_execv__doc__},
3166 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003167#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003168#ifdef HAVE_SPAWNV
3169 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3170 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3171#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003172#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003174#endif /* HAVE_FORK */
3175#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003176 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003177#endif /* HAVE_GETEGID */
3178#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003179 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003180#endif /* HAVE_GETEUID */
3181#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003182 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003183#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003184 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003185#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003186 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003187#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003188#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003189 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003190#endif /* HAVE_GETPPID */
3191#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003192 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003193#endif /* HAVE_GETUID */
3194#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003195 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003196#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003197#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003199#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003200#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003201 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003202#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003203#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003204 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003205#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003206#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003207 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003208#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003209#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003211#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003212#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003214#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003215#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003216 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003217#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003218#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003220#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003221#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003223#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003224#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003225 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003226#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003227#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003228 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003229#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003230 {"open", posix_open, 1, posix_open__doc__},
3231 {"close", posix_close, 0, posix_close__doc__},
3232 {"dup", posix_dup, 0, posix_dup__doc__},
3233 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3234 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3235 {"read", posix_read, 0, posix_read__doc__},
3236 {"write", posix_write, 0, posix_write__doc__},
3237 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3238 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003239#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003240 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003241#endif
3242#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003243 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003244#endif
3245#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003246 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003247#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003248#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003249 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003250#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003251#ifdef HAVE_STRERROR
3252 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3253#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003254#ifdef HAVE_FSYNC
3255 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3256#endif
3257#ifdef HAVE_FDATASYNC
3258 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3259#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003260#ifdef HAVE_SYS_WAIT_H
3261#ifdef WIFSTOPPED
3262 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3263#endif /* WIFSTOPPED */
3264#ifdef WIFSIGNALED
3265 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3266#endif /* WIFSIGNALED */
3267#ifdef WIFEXITED
3268 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3269#endif /* WIFEXITED */
3270#ifdef WEXITSTATUS
3271 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3272#endif /* WEXITSTATUS */
3273#ifdef WTERMSIG
3274 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3275#endif /* WTERMSIG */
3276#ifdef WSTOPSIG
3277 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3278#endif /* WSTOPSIG */
3279#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003280#ifdef HAVE_FSTATVFS
3281 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3282#endif
3283#ifdef HAVE_STATVFS
3284 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3285#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003286 {NULL, NULL} /* Sentinel */
3287};
3288
3289
Barry Warsaw4a342091996-12-19 23:50:02 +00003290static int
3291ins(d, symbol, value)
3292 PyObject* d;
3293 char* symbol;
3294 long value;
3295{
3296 PyObject* v = PyInt_FromLong(value);
3297 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3298 return -1; /* triggers fatal error */
3299
3300 Py_DECREF(v);
3301 return 0;
3302}
3303
Guido van Rossumd48f2521997-12-05 22:19:34 +00003304#if defined(PYOS_OS2)
3305/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3306static int insertvalues(PyObject *d)
3307{
3308 APIRET rc;
3309 ULONG values[QSV_MAX+1];
3310 PyObject *v;
3311 char *ver, tmp[10];
3312
3313 Py_BEGIN_ALLOW_THREADS
3314 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3315 Py_END_ALLOW_THREADS
3316
3317 if (rc != NO_ERROR) {
3318 os2_error(rc);
3319 return -1;
3320 }
3321
3322 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3323 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3324 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3325 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3326 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3327 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3328 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3329
3330 switch (values[QSV_VERSION_MINOR]) {
3331 case 0: ver = "2.00"; break;
3332 case 10: ver = "2.10"; break;
3333 case 11: ver = "2.11"; break;
3334 case 30: ver = "3.00"; break;
3335 case 40: ver = "4.00"; break;
3336 case 50: ver = "5.00"; break;
3337 default:
3338 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3339 values[QSV_VERSION_MINOR]);
3340 ver = &tmp[0];
3341 }
3342
3343 /* Add Indicator of the Version of the Operating System */
3344 v = PyString_FromString(ver);
3345 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3346 return -1;
3347 Py_DECREF(v);
3348
3349 /* Add Indicator of Which Drive was Used to Boot the System */
3350 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3351 tmp[1] = ':';
3352 tmp[2] = '\0';
3353
3354 v = PyString_FromString(tmp);
3355 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3356 return -1;
3357 Py_DECREF(v);
3358
3359 return 0;
3360}
3361#endif
3362
Barry Warsaw4a342091996-12-19 23:50:02 +00003363static int
3364all_ins(d)
3365 PyObject* d;
3366{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003367#ifdef F_OK
3368 if (ins(d, "F_OK", (long)F_OK)) return -1;
3369#endif
3370#ifdef R_OK
3371 if (ins(d, "R_OK", (long)R_OK)) return -1;
3372#endif
3373#ifdef W_OK
3374 if (ins(d, "W_OK", (long)W_OK)) return -1;
3375#endif
3376#ifdef X_OK
3377 if (ins(d, "X_OK", (long)X_OK)) return -1;
3378#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003379#ifdef WNOHANG
3380 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3381#endif
3382#ifdef O_RDONLY
3383 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3384#endif
3385#ifdef O_WRONLY
3386 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3387#endif
3388#ifdef O_RDWR
3389 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3390#endif
3391#ifdef O_NDELAY
3392 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3393#endif
3394#ifdef O_NONBLOCK
3395 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3396#endif
3397#ifdef O_APPEND
3398 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3399#endif
3400#ifdef O_DSYNC
3401 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3402#endif
3403#ifdef O_RSYNC
3404 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3405#endif
3406#ifdef O_SYNC
3407 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3408#endif
3409#ifdef O_NOCTTY
3410 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3411#endif
3412#ifdef O_CREAT
3413 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3414#endif
3415#ifdef O_EXCL
3416 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3417#endif
3418#ifdef O_TRUNC
3419 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3420#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003421#ifdef O_BINARY
3422 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3423#endif
3424#ifdef O_TEXT
3425 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3426#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003427
3428#if defined(PYOS_OS2)
3429 if (insertvalues(d)) return -1;
3430#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003431 return 0;
3432}
3433
3434
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003435#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003436#define INITFUNC initnt
3437#define MODNAME "nt"
3438#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003439#if defined(PYOS_OS2)
3440#define INITFUNC initos2
3441#define MODNAME "os2"
3442#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003443#define INITFUNC initposix
3444#define MODNAME "posix"
3445#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003446#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003447
Guido van Rossum3886bb61998-12-04 18:50:17 +00003448DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003449INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003450{
Barry Warsaw53699e91996-12-10 23:23:01 +00003451 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003452
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003453 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003454 posix_methods,
3455 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003456 (PyObject *)NULL,
3457 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003458 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003459
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003460 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003461 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003462 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003463 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003464 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003465
Barry Warsaw4a342091996-12-19 23:50:02 +00003466 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003467 return;
3468
Barry Warsawd58d7641998-07-23 16:14:40 +00003469 Py_INCREF(PyExc_OSError);
3470 PosixError = PyExc_OSError;
3471 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003472}