blob: 4e52baa6ea58b66648ad188ca1198f6a5f46e7df [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 Rossumc5a0f531997-12-02 20:36:02 +000078#define HAVE_EXECV 1
79#define HAVE_GETCWD 1
80#define HAVE_SYSTEM 1
81#define HAVE_WAIT 1
82#define HAVE_KILL 1
83#define HAVE_PIPE 1
84#define HAVE_POPEN 1
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000085
Guido van Rossumc5a0f531997-12-02 20:36:02 +000086/* #define HAVE_FORK 1 */
87/* #define HAVE_GETEGID 1 */
88/* #define HAVE_GETEUID 1 */
89/* #define HAVE_GETGID 1 */
90/* #define HAVE_GETPPID 1 */
91/* #define HAVE_GETUID 1 */
92/* #define HAVE_OPENDIR 1 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000093#include <process.h>
94#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000095#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000096#define HAVE_GETCWD 1
97#define HAVE_OPENDIR 1
98#define HAVE_SYSTEM 1
99#if defined(__OS2__)
100#define HAVE_EXECV 1
101#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000102#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000103#include <process.h>
104#else
105#ifdef __BORLANDC__ /* Borland compiler */
106#define HAVE_EXECV 1
107#define HAVE_GETCWD 1
108#define HAVE_GETEGID 1
109#define HAVE_GETEUID 1
110#define HAVE_GETGID 1
111#define HAVE_GETPPID 1
112#define HAVE_GETUID 1
113#define HAVE_KILL 1
114#define HAVE_OPENDIR 1
115#define HAVE_PIPE 1
116#define HAVE_POPEN 1
117#define HAVE_SYSTEM 1
118#define HAVE_WAIT 1
119#else
120#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000121#define HAVE_GETCWD 1
122#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000123#define HAVE_EXECV 1
124#define HAVE_PIPE 1
125#define HAVE_POPEN 1
126#define HAVE_SYSTEM 1
127#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000128#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000129#else /* all other compilers */
130/* Unix functions that the configure script doesn't check for */
131#define HAVE_EXECV 1
132#define HAVE_FORK 1
133#define HAVE_GETCWD 1
134#define HAVE_GETEGID 1
135#define HAVE_GETEUID 1
136#define HAVE_GETGID 1
137#define HAVE_GETPPID 1
138#define HAVE_GETUID 1
139#define HAVE_KILL 1
140#define HAVE_OPENDIR 1
141#define HAVE_PIPE 1
142#define HAVE_POPEN 1
143#define HAVE_SYSTEM 1
144#define HAVE_WAIT 1
145#endif /* _MSC_VER */
146#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000147#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000148#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000149
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000151
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000153#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000154#endif
155
156#ifdef NeXT
157/* NeXT's <unistd.h> and <utime.h> aren't worth much */
158#undef HAVE_UNISTD_H
159#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000160#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161/* #undef HAVE_GETCWD */
162#endif
163
164#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000165/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
166extern int rename();
167extern int pclose();
168extern int lstat();
169extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000170#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#if defined(PYCC_VACPP)
172extern int mkdir Py_PROTO((char *));
173#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000174#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000175extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000177extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000179#endif
180#if defined(__IBMC__) || defined(__IBMCPP__)
181extern int chdir Py_PROTO((char *));
182extern int rmdir Py_PROTO((char *));
183#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000184extern int chdir Py_PROTO((const char *));
185extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000186#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000187extern int chmod Py_PROTO((const char *, mode_t));
188extern int chown Py_PROTO((const char *, uid_t, gid_t));
189extern char *getcwd Py_PROTO((char *, int));
190extern char *strerror Py_PROTO((int));
191extern int link Py_PROTO((const char *, const char *));
192extern int rename Py_PROTO((const char *, const char *));
193extern int stat Py_PROTO((const char *, struct stat *));
194extern int unlink Py_PROTO((const char *));
195extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000197extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000198#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000200extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000201#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000203
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_UTIME_H
207#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000210#ifdef HAVE_SYS_UTIME_H
211#include <sys/utime.h>
212#define HAVE_UTIME_H /* pretend we do for the rest of this file */
213#endif /* HAVE_SYS_UTIME_H */
214
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#ifdef HAVE_SYS_TIMES_H
216#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000217#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
219#ifdef HAVE_SYS_PARAM_H
220#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000221#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222
223#ifdef HAVE_SYS_UTSNAME_H
224#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
227#ifndef MAXPATHLEN
228#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000231#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#define NAMLEN(dirent) strlen((dirent)->d_name)
234#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000235#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#include <direct.h>
237#define NAMLEN(dirent) strlen((dirent)->d_name)
238#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000240#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000241#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#endif
245#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000247#endif
248#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#endif
251#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000253#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#include <direct.h>
255#include <io.h>
256#include <process.h>
257#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000258#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000259#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000260#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#else /* 16-bit Windows */
262#include <dos.h>
263#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000264#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000265#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266
Guido van Rossumd48f2521997-12-05 22:19:34 +0000267#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000269#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270
271/* Return a dictionary corresponding to the POSIX environment table */
272
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000273#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000275#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276
Barry Warsaw53699e91996-12-10 23:23:01 +0000277static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278convertenviron()
279{
Barry Warsaw53699e91996-12-10 23:23:01 +0000280 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000282 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283 if (d == NULL)
284 return NULL;
285 if (environ == NULL)
286 return d;
287 /* XXX This part ignores errors */
288 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000289 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290 char *p = strchr(*e, '=');
291 if (p == NULL)
292 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000293 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294 if (v == NULL)
295 continue;
296 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000297 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000299 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000301#if defined(PYOS_OS2)
302 {
303 APIRET rc;
304 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
305
306 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
307 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
308 PyObject *v = PyString_FromString(buffer);
309 PyDict_SetItemString(d, "BEGINLIBPATH", v);
310 Py_DECREF(v);
311 }
312 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
313 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
314 PyObject *v = PyString_FromString(buffer);
315 PyDict_SetItemString(d, "ENDLIBPATH", v);
316 Py_DECREF(v);
317 }
318 }
319#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320 return d;
321}
322
323
Barry Warsaw53699e91996-12-10 23:23:01 +0000324static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325
326/* Set a POSIX-specific error from errno, and return NULL */
327
Barry Warsaw53699e91996-12-10 23:23:01 +0000328static PyObject * posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000329{
Barry Warsaw53699e91996-12-10 23:23:01 +0000330 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331}
332
Guido van Rossumd48f2521997-12-05 22:19:34 +0000333#if defined(PYOS_OS2)
334/**********************************************************************
335 * Helper Function to Trim and Format OS/2 Messages
336 **********************************************************************/
337 static void
338os2_formatmsg(char *msgbuf, int msglen, char *reason)
339{
340 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
341
342 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
343 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
344
345 while (lastc > msgbuf && isspace(*lastc))
346 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
347 }
348
349 /* Add Optional Reason Text */
350 if (reason) {
351 strcat(msgbuf, " : ");
352 strcat(msgbuf, reason);
353 }
354}
355
356/**********************************************************************
357 * Decode an OS/2 Operating System Error Code
358 *
359 * A convenience function to lookup an OS/2 error code and return a
360 * text message we can use to raise a Python exception.
361 *
362 * Notes:
363 * The messages for errors returned from the OS/2 kernel reside in
364 * the file OSO001.MSG in the \OS2 directory hierarchy.
365 *
366 **********************************************************************/
367 static char *
368os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
369{
370 APIRET rc;
371 ULONG msglen;
372
373 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
374 Py_BEGIN_ALLOW_THREADS
375 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
376 errorcode, "oso001.msg", &msglen);
377 Py_END_ALLOW_THREADS
378
379 if (rc == NO_ERROR)
380 os2_formatmsg(msgbuf, msglen, reason);
381 else
382 sprintf(msgbuf, "unknown OS error #%d", errorcode);
383
384 return msgbuf;
385}
386
387/* Set an OS/2-specific error and return NULL. OS/2 kernel
388 errors are not in a global variable e.g. 'errno' nor are
389 they congruent with posix error numbers. */
390
391static PyObject * os2_error(int code)
392{
393 char text[1024];
394 PyObject *v;
395
396 os2_strerror(text, sizeof(text), code, "");
397
398 v = Py_BuildValue("(is)", code, text);
399 if (v != NULL) {
400 PyErr_SetObject(PosixError, v);
401 Py_DECREF(v);
402 }
403 return NULL; /* Signal to Python that an Exception is Pending */
404}
405
406#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407
408/* POSIX generic methods */
409
Barry Warsaw53699e91996-12-10 23:23:01 +0000410static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000412 PyObject *args;
413 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000415 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000416 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000417 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000419 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000420 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000421 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000422 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000424 Py_INCREF(Py_None);
425 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426}
427
Barry Warsaw53699e91996-12-10 23:23:01 +0000428static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000430 PyObject *args;
431 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000433 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000434 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000435 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000437 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000438 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000439 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000440 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000442 Py_INCREF(Py_None);
443 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444}
445
Barry Warsaw53699e91996-12-10 23:23:01 +0000446static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 PyObject *args;
449 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000451 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000453 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000454 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000456 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000457 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000458 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000459 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 Py_INCREF(Py_None);
462 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463}
464
Barry Warsaw53699e91996-12-10 23:23:01 +0000465static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000466posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 PyObject *args;
468 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000469{
470 char *path;
471 int i,i2;
472 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000473 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000474 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000475 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000476 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000477 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000478 if (res < 0)
479 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000480 Py_INCREF(Py_None);
481 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000482}
483
Barry Warsaw53699e91996-12-10 23:23:01 +0000484static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000486 PyObject *self;
487 PyObject *args;
488 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489{
490 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000491 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000492 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000493 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000495 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000496 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000497 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000498 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000499 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000500 return Py_BuildValue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000501 (long)st.st_mode,
502 (long)st.st_ino,
503 (long)st.st_dev,
504 (long)st.st_nlink,
505 (long)st.st_uid,
506 (long)st.st_gid,
507 (long)st.st_size,
508 (long)st.st_atime,
509 (long)st.st_mtime,
510 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000511}
512
513
514/* POSIX methods */
515
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000516static char posix_chdir__doc__[] =
517"chdir(path) -> None\n\
518Change the current working directory to the specified path.";
519
Barry Warsaw53699e91996-12-10 23:23:01 +0000520static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000522 PyObject *self;
523 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525 return posix_1str(args, chdir);
526}
527
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000528
529static char posix_chmod__doc__[] =
530"chmod(path, mode) -> None\n\
531Change the access permissions of a file.";
532
Barry Warsaw53699e91996-12-10 23:23:01 +0000533static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000535 PyObject *self;
536 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538 return posix_strint(args, chmod);
539}
540
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000541
Guido van Rossumb6775db1994-08-01 11:34:53 +0000542#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000543static char posix_chown__doc__[] =
544"chown(path, uid, gid) -> None\n\
545Change the owner and group id of path to the numeric uid and gid.";
546
Barry Warsaw53699e91996-12-10 23:23:01 +0000547static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000548posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000549 PyObject *self;
550 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000551{
552 return posix_strintint(args, chown);
553}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000554#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000555
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000556
Guido van Rossum36bc6801995-06-14 22:54:23 +0000557#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000558static char posix_getcwd__doc__[] =
559"getcwd() -> path\n\
560Return a string representing the current working directory.";
561
Barry Warsaw53699e91996-12-10 23:23:01 +0000562static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000564 PyObject *self;
565 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566{
567 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000568 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000569 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000571 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000572 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000573 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000574 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000576 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000578#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000580
Guido van Rossumb6775db1994-08-01 11:34:53 +0000581#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000582static char posix_link__doc__[] =
583"link(src, dst) -> None\n\
584Create a hard link to a file.";
585
Barry Warsaw53699e91996-12-10 23:23:01 +0000586static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000588 PyObject *self;
589 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591 return posix_2str(args, link);
592}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000593#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000595
596static char posix_listdir__doc__[] =
597"listdir(path) -> list_of_strings\n\
598Return a list containing the names of the entries in the directory.\n\
599\n\
600 path: path of directory to list\n\
601\n\
602The list is in arbitrary order. It does not include the special\n\
603entries '.' and '..' even if they are present in the directory.";
604
Barry Warsaw53699e91996-12-10 23:23:01 +0000605static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000606posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000607 PyObject *self;
608 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000609{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000610 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000611 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000612#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000613
Guido van Rossumb6775db1994-08-01 11:34:53 +0000614 char *name;
615 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000616 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000617 HANDLE hFindFile;
618 WIN32_FIND_DATA FileData;
619 char namebuf[MAX_PATH+5];
620
Barry Warsaw53699e91996-12-10 23:23:01 +0000621 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000622 return NULL;
623 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000624 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000625 return NULL;
626 }
627 strcpy(namebuf, name);
628 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
629 namebuf[len++] = '/';
630 strcpy(namebuf + len, "*.*");
631
Barry Warsaw53699e91996-12-10 23:23:01 +0000632 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000633 return NULL;
634
635 hFindFile = FindFirstFile(namebuf, &FileData);
636 if (hFindFile == INVALID_HANDLE_VALUE) {
637 errno = GetLastError();
638 return posix_error();
639 }
640 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000641 if (FileData.cFileName[0] == '.' &&
642 (FileData.cFileName[1] == '\0' ||
643 FileData.cFileName[1] == '.' &&
644 FileData.cFileName[2] == '\0'))
645 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000646 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000647 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000648 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000649 d = NULL;
650 break;
651 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000652 if (PyList_Append(d, v) != 0) {
653 Py_DECREF(v);
654 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000655 d = NULL;
656 break;
657 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000658 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000659 } while (FindNextFile(hFindFile, &FileData) == TRUE);
660
661 if (FindClose(hFindFile) == FALSE) {
662 errno = GetLastError();
663 return posix_error();
664 }
665
666 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000667
Guido van Rossum8d665e61996-06-26 18:22:49 +0000668#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000669#ifdef _MSC_VER /* 16-bit Windows */
670
671#ifndef MAX_PATH
672#define MAX_PATH 250
673#endif
674 char *name, *pt;
675 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000676 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000677 char namebuf[MAX_PATH+5];
678 struct _find_t ep;
679
Barry Warsaw53699e91996-12-10 23:23:01 +0000680 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000681 return NULL;
682 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000683 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000684 return NULL;
685 }
686 strcpy(namebuf, name);
687 for (pt = namebuf; *pt; pt++)
688 if (*pt == '/')
689 *pt = '\\';
690 if (namebuf[len-1] != '\\')
691 namebuf[len++] = '\\';
692 strcpy(namebuf + len, "*.*");
693
Barry Warsaw53699e91996-12-10 23:23:01 +0000694 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000695 return NULL;
696
697 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000698 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
699 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000700 errno = ENOENT;
701 return posix_error();
702 }
703 do {
704 if (ep.name[0] == '.' &&
705 (ep.name[1] == '\0' ||
706 ep.name[1] == '.' &&
707 ep.name[2] == '\0'))
708 continue;
709 strcpy(namebuf, ep.name);
710 for (pt = namebuf; *pt; pt++)
711 if (isupper(*pt))
712 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000713 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000714 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000715 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000716 d = NULL;
717 break;
718 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000719 if (PyList_Append(d, v) != 0) {
720 Py_DECREF(v);
721 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000722 d = NULL;
723 break;
724 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000725 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000726 } while (_dos_findnext(&ep) == 0);
727
728 return d;
729
730#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000731#if defined(PYOS_OS2)
732
733#ifndef MAX_PATH
734#define MAX_PATH CCHMAXPATH
735#endif
736 char *name, *pt;
737 int len;
738 PyObject *d, *v;
739 char namebuf[MAX_PATH+5];
740 HDIR hdir = 1;
741 ULONG srchcnt = 1;
742 FILEFINDBUF3 ep;
743 APIRET rc;
744
745 if (!PyArg_Parse(args, "s#", &name, &len))
746 return NULL;
747 if (len >= MAX_PATH) {
748 PyErr_SetString(PyExc_ValueError, "path too long");
749 return NULL;
750 }
751 strcpy(namebuf, name);
752 for (pt = namebuf; *pt; pt++)
753 if (*pt == '/')
754 *pt = '\\';
755 if (namebuf[len-1] != '\\')
756 namebuf[len++] = '\\';
757 strcpy(namebuf + len, "*.*");
758
759 if ((d = PyList_New(0)) == NULL)
760 return NULL;
761
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000762 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
763 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000764 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000765 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
766 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
767 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000768
769 if (rc != NO_ERROR) {
770 errno = ENOENT;
771 return posix_error();
772 }
773
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000774 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000775 do {
776 if (ep.achName[0] == '.'
777 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000778 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000779
780 strcpy(namebuf, ep.achName);
781
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000782 /* Leave Case of Name Alone -- In Native Form */
783 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000784
785 v = PyString_FromString(namebuf);
786 if (v == NULL) {
787 Py_DECREF(d);
788 d = NULL;
789 break;
790 }
791 if (PyList_Append(d, v) != 0) {
792 Py_DECREF(v);
793 Py_DECREF(d);
794 d = NULL;
795 break;
796 }
797 Py_DECREF(v);
798 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
799 }
800
801 return d;
802#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000803
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000804 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000805 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000806 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000807 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000808 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000810 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000811 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000812 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000813 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000814 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000817 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818 return NULL;
819 }
820 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000821 if (ep->d_name[0] == '.' &&
822 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000823 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000824 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000825 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000826 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000827 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000828 d = NULL;
829 break;
830 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000831 if (PyList_Append(d, v) != 0) {
832 Py_DECREF(v);
833 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834 d = NULL;
835 break;
836 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000837 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838 }
839 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000840 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000841
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000842 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000843
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000844#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000845#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000846#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847}
848
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000849static char posix_mkdir__doc__[] =
850"mkdir(path [, mode=0777]) -> None\n\
851Create a directory.";
852
Barry Warsaw53699e91996-12-10 23:23:01 +0000853static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000854posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000855 PyObject *self;
856 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000858 int res;
859 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000860 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000861 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000862 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000863 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000864#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000865 res = mkdir(path);
866#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000867 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000868#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000869 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000870 if (res < 0)
871 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000872 Py_INCREF(Py_None);
873 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000874}
875
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000876
Guido van Rossumb6775db1994-08-01 11:34:53 +0000877#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000878static char posix_nice__doc__[] =
879"nice(inc) -> new_priority\n\
880Decrease the priority of process and return new priority.";
881
Barry Warsaw53699e91996-12-10 23:23:01 +0000882static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000883posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000884 PyObject *self;
885 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000886{
887 int increment, value;
888
Barry Warsaw53699e91996-12-10 23:23:01 +0000889 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000890 return NULL;
891 value = nice(increment);
892 if (value == -1)
893 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000894 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000895}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000896#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000898
899static char posix_rename__doc__[] =
900"rename(old, new) -> None\n\
901Rename a file or directory.";
902
Barry Warsaw53699e91996-12-10 23:23:01 +0000903static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000905 PyObject *self;
906 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000907{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908 return posix_2str(args, rename);
909}
910
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000911
912static char posix_rmdir__doc__[] =
913"rmdir(path) -> None\n\
914Remove a directory.";
915
Barry Warsaw53699e91996-12-10 23:23:01 +0000916static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000918 PyObject *self;
919 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000921 return posix_1str(args, rmdir);
922}
923
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000924
925static char posix_stat__doc__[] =
926"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
927Perform a stat system call on the given path.";
928
Barry Warsaw53699e91996-12-10 23:23:01 +0000929static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000930posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000931 PyObject *self;
932 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000933{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000934 return posix_do_stat(self, args, stat);
935}
936
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000937
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000938#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000939static char posix_system__doc__[] =
940"system(command) -> exit_status\n\
941Execute the command (a string) in a subshell.";
942
Barry Warsaw53699e91996-12-10 23:23:01 +0000943static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000944posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000945 PyObject *self;
946 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000947{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000948 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000949 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000950 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000951 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000952 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000953 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +0000954 Py_END_ALLOW_THREADS
955 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000956}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000957#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000959
960static char posix_umask__doc__[] =
961"umask(new_mask) -> old_mask\n\
962Set the current numeric umask and return the previous umask.";
963
Barry Warsaw53699e91996-12-10 23:23:01 +0000964static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000966 PyObject *self;
967 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000968{
969 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +0000970 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000971 return NULL;
972 i = umask(i);
973 if (i < 0)
974 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000975 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000976}
977
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000978
979static char posix_unlink__doc__[] =
980"unlink(path) -> None\n\
981Remove a file (same as remove(path)).";
982
983static char posix_remove__doc__[] =
984"remove(path) -> None\n\
985Remove a file (same as unlink(path)).";
986
Barry Warsaw53699e91996-12-10 23:23:01 +0000987static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000988posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000989 PyObject *self;
990 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000991{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000992 return posix_1str(args, unlink);
993}
994
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000995
Guido van Rossumb6775db1994-08-01 11:34:53 +0000996#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000997static char posix_uname__doc__[] =
998"uname() -> (sysname, nodename, release, version, machine)\n\
999Return a tuple identifying the current operating system.";
1000
Barry Warsaw53699e91996-12-10 23:23:01 +00001001static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001002posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001003 PyObject *self;
1004 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001005{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001006 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001007 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001008 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001009 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001010 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001011 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001012 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001013 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001014 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001015 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001016 u.sysname,
1017 u.nodename,
1018 u.release,
1019 u.version,
1020 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001021}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001022#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001023
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001024
1025static char posix_utime__doc__[] =
1026"utime(path, (atime, utime)) -> None\n\
1027Set the access and modified time of the file to the given values.";
1028
Barry Warsaw53699e91996-12-10 23:23:01 +00001029static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001030posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001031 PyObject *self;
1032 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001033{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001034 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001035 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001036 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001037
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001038/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001039#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001040 struct utimbuf buf;
1041#define ATIME buf.actime
1042#define MTIME buf.modtime
1043#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001044#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001045 time_t buf[2];
1046#define ATIME buf[0]
1047#define MTIME buf[1]
1048#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001049#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001050
Barry Warsaw53699e91996-12-10 23:23:01 +00001051 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001052 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001053 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001054 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001055 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001056 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001057 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001058 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001059 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001060 Py_INCREF(Py_None);
1061 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001062#undef UTIME_ARG
1063#undef ATIME
1064#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001065}
1066
Guido van Rossum85e3b011991-06-03 12:42:10 +00001067
Guido van Rossum3b066191991-06-04 19:40:25 +00001068/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001069
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001070static char posix__exit__doc__[] =
1071"_exit(status)\n\
1072Exit to the system with specified status, without normal exit processing.";
1073
Barry Warsaw53699e91996-12-10 23:23:01 +00001074static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001075posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001076 PyObject *self;
1077 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001078{
1079 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001080 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001081 return NULL;
1082 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001083 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001084}
1085
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001086
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001087#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001088static char posix_execv__doc__[] =
1089"execv(path, args)\n\
1090Execute an executable path with arguments, replacing current process.\n\
1091\n\
1092 path: path of executable file\n\
1093 args: tuple or list of strings";
1094
Barry Warsaw53699e91996-12-10 23:23:01 +00001095static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001096posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001097 PyObject *self;
1098 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001099{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001100 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001101 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001102 char **argvlist;
1103 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001104 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001105
Guido van Rossum89b33251993-10-22 14:26:06 +00001106 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001107 argv is a list or tuple of strings. */
1108
Barry Warsaw53699e91996-12-10 23:23:01 +00001109 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001110 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001111 if (PyList_Check(argv)) {
1112 argc = PyList_Size(argv);
1113 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001114 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001115 else if (PyTuple_Check(argv)) {
1116 argc = PyTuple_Size(argv);
1117 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001118 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001119 else {
1120 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001121 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001122 return NULL;
1123 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001124
Barry Warsaw53699e91996-12-10 23:23:01 +00001125 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001126 if (argvlist == NULL)
1127 return NULL;
1128 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001129 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1130 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001131 goto badarg;
1132 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001133 }
1134 argvlist[argc] = NULL;
1135
Guido van Rossumb6775db1994-08-01 11:34:53 +00001136#ifdef BAD_EXEC_PROTOTYPES
1137 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001138#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001139 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001140#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001141
Guido van Rossum85e3b011991-06-03 12:42:10 +00001142 /* If we get here it's definitely an error */
1143
Barry Warsaw53699e91996-12-10 23:23:01 +00001144 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001145 return posix_error();
1146}
1147
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001148
1149static char posix_execve__doc__[] =
1150"execve(path, args, env)\n\
1151Execute a path with arguments and environment, replacing current process.\n\
1152\n\
1153 path: path of executable file\n\
1154 args: tuple or list of arguments\n\
1155 env: dictonary of strings mapping to strings";
1156
Barry Warsaw53699e91996-12-10 23:23:01 +00001157static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001158posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001159 PyObject *self;
1160 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001161{
1162 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001163 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001164 char **argvlist;
1165 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001166 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001167 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001168 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001169
1170 /* execve has three arguments: (path, argv, env), where
1171 argv is a list or tuple of strings and env is a dictionary
1172 like posix.environ. */
1173
Barry Warsaw53699e91996-12-10 23:23:01 +00001174 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001175 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001176 if (PyList_Check(argv)) {
1177 argc = PyList_Size(argv);
1178 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001179 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001180 else if (PyTuple_Check(argv)) {
1181 argc = PyTuple_Size(argv);
1182 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001183 }
1184 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001185 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001186 return NULL;
1187 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001188 if (!PyMapping_Check(env)) {
1189 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001190 return NULL;
1191 }
1192
Barry Warsaw53699e91996-12-10 23:23:01 +00001193 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001194 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001195 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001196 return NULL;
1197 }
1198 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001199 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001200 "s;argv must be list of strings",
1201 &argvlist[i]))
1202 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001203 goto fail_1;
1204 }
1205 }
1206 argvlist[argc] = NULL;
1207
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001208 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001209 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001210 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001211 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001212 goto fail_1;
1213 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001214 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001215 keys = PyMapping_Keys(env);
1216 vals = PyMapping_Values(env);
1217 if (!keys || !vals)
1218 goto fail_2;
1219
1220 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001221 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001222
1223 key = PyList_GetItem(keys, pos);
1224 val = PyList_GetItem(vals, pos);
1225 if (!key || !val)
1226 goto fail_2;
1227
Barry Warsaw53699e91996-12-10 23:23:01 +00001228 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001229 !PyArg_Parse(val, "s;non-string value in env", &v))
1230 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001231 goto fail_2;
1232 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001233
1234#if defined(PYOS_OS2)
1235 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1236 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1237#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001238 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001239 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001240 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001241 goto fail_2;
1242 }
1243 sprintf(p, "%s=%s", k, v);
1244 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001245#if defined(PYOS_OS2)
1246 }
1247#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001248 }
1249 envlist[envc] = 0;
1250
Guido van Rossumb6775db1994-08-01 11:34:53 +00001251
1252#ifdef BAD_EXEC_PROTOTYPES
1253 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001254#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001255 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001256#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001257
1258 /* If we get here it's definitely an error */
1259
1260 (void) posix_error();
1261
1262 fail_2:
1263 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 PyMem_DEL(envlist[envc]);
1265 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001266 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001267 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001268 Py_XDECREF(vals);
1269 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001270 return NULL;
1271}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001272#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001273
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001274
Guido van Rossumad0ee831995-03-01 10:34:45 +00001275#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001276static char posix_fork__doc__[] =
1277"fork() -> pid\n\
1278Fork a child process.\n\
1279\n\
1280Return 0 to child process and PID of child to parent process.";
1281
Barry Warsaw53699e91996-12-10 23:23:01 +00001282static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001283posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001284 PyObject *self;
1285 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001286{
1287 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001288 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001289 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001290 pid = fork();
1291 if (pid == -1)
1292 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001293 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001294 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001295}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001296#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001297
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001298
Guido van Rossumad0ee831995-03-01 10:34:45 +00001299#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001300static char posix_getegid__doc__[] =
1301"getegid() -> egid\n\
1302Return the current process's effective group id.";
1303
Barry Warsaw53699e91996-12-10 23:23:01 +00001304static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001305posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001306 PyObject *self;
1307 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001308{
Barry Warsaw53699e91996-12-10 23:23:01 +00001309 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001310 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001311 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001312}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001313#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001314
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001315
Guido van Rossumad0ee831995-03-01 10:34:45 +00001316#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001317static char posix_geteuid__doc__[] =
1318"geteuid() -> euid\n\
1319Return the current process's effective user id.";
1320
Barry Warsaw53699e91996-12-10 23:23:01 +00001321static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001322posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001323 PyObject *self;
1324 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001325{
Barry Warsaw53699e91996-12-10 23:23:01 +00001326 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001327 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001328 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001329}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001330#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001331
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001332
Guido van Rossumad0ee831995-03-01 10:34:45 +00001333#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001334static char posix_getgid__doc__[] =
1335"getgid() -> gid\n\
1336Return the current process's group id.";
1337
Barry Warsaw53699e91996-12-10 23:23:01 +00001338static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001339posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001340 PyObject *self;
1341 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001342{
Barry Warsaw53699e91996-12-10 23:23:01 +00001343 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001344 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001345 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001346}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001347#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001348
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001349
1350static char posix_getpid__doc__[] =
1351"getpid() -> pid\n\
1352Return the current process id";
1353
Barry Warsaw53699e91996-12-10 23:23:01 +00001354static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001355posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001356 PyObject *self;
1357 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001358{
Barry Warsaw53699e91996-12-10 23:23:01 +00001359 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001360 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001361 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001362}
1363
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001364
Guido van Rossumb6775db1994-08-01 11:34:53 +00001365#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001366static char posix_getpgrp__doc__[] =
1367"getpgrp() -> pgrp\n\
1368Return the current process group id.";
1369
Barry Warsaw53699e91996-12-10 23:23:01 +00001370static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001371posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001372 PyObject *self;
1373 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001374{
Barry Warsaw53699e91996-12-10 23:23:01 +00001375 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001376 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001377#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001378 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001379#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001380 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001381#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001382}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001383#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001384
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001385
Guido van Rossumb6775db1994-08-01 11:34:53 +00001386#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001387static char posix_setpgrp__doc__[] =
1388"setpgrp() -> None\n\
1389Make this process a session leader.";
1390
Barry Warsaw53699e91996-12-10 23:23:01 +00001391static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001392posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001393 PyObject *self;
1394 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001395{
Barry Warsaw53699e91996-12-10 23:23:01 +00001396 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001397 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001398#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001399 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001400#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001401 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001402#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001403 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001404 Py_INCREF(Py_None);
1405 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001406}
1407
Guido van Rossumb6775db1994-08-01 11:34:53 +00001408#endif /* HAVE_SETPGRP */
1409
Guido van Rossumad0ee831995-03-01 10:34:45 +00001410#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001411static char posix_getppid__doc__[] =
1412"getppid() -> ppid\n\
1413Return the parent's process id.";
1414
Barry Warsaw53699e91996-12-10 23:23:01 +00001415static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001416posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001417 PyObject *self;
1418 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001419{
Barry Warsaw53699e91996-12-10 23:23:01 +00001420 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001421 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001422 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001423}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001424#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001425
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001426
Guido van Rossumad0ee831995-03-01 10:34:45 +00001427#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001428static char posix_getuid__doc__[] =
1429"getuid() -> uid\n\
1430Return the current process's user id.";
1431
Barry Warsaw53699e91996-12-10 23:23:01 +00001432static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001433posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001434 PyObject *self;
1435 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001436{
Barry Warsaw53699e91996-12-10 23:23:01 +00001437 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001438 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001439 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001440}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001441#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001442
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001443
Guido van Rossumad0ee831995-03-01 10:34:45 +00001444#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001445static char posix_kill__doc__[] =
1446"kill(pid, sig) -> None\n\
1447Kill a process with a signal.";
1448
Barry Warsaw53699e91996-12-10 23:23:01 +00001449static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001450posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001451 PyObject *self;
1452 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001453{
1454 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001455 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001456 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001457#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001458 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1459 APIRET rc;
1460 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001461 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001462
1463 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1464 APIRET rc;
1465 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001466 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001467
1468 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001469 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001470#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001471 if (kill(pid, sig) == -1)
1472 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001473#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001474 Py_INCREF(Py_None);
1475 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001476}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001477#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001478
Guido van Rossumc0125471996-06-28 18:55:32 +00001479#ifdef HAVE_PLOCK
1480
1481#ifdef HAVE_SYS_LOCK_H
1482#include <sys/lock.h>
1483#endif
1484
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001485static char posix_plock__doc__[] =
1486"plock(op) -> None\n\
1487Lock program segments into memory.";
1488
Barry Warsaw53699e91996-12-10 23:23:01 +00001489static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001490posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001491 PyObject *self;
1492 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001493{
1494 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001495 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001496 return NULL;
1497 if (plock(op) == -1)
1498 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001499 Py_INCREF(Py_None);
1500 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001501}
1502#endif
1503
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001504
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001505#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001506static char posix_popen__doc__[] =
1507"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1508Open a pipe to/from a command returning a file object.";
1509
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001510#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001511static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001512async_system(const char *command)
1513{
1514 char *p, errormsg[256], args[1024];
1515 RESULTCODES rcodes;
1516 APIRET rc;
1517 char *shell = getenv("COMSPEC");
1518 if (!shell)
1519 shell = "cmd";
1520
1521 strcpy(args, shell);
1522 p = &args[ strlen(args)+1 ];
1523 strcpy(p, "/c ");
1524 strcat(p, command);
1525 p += strlen(p) + 1;
1526 *p = '\0';
1527
1528 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001529 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001530 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001531 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001532 &rcodes, shell);
1533 return rc;
1534}
1535
Guido van Rossumd48f2521997-12-05 22:19:34 +00001536static FILE *
1537popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001538{
1539 HFILE rhan, whan;
1540 FILE *retfd = NULL;
1541 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1542
Guido van Rossumd48f2521997-12-05 22:19:34 +00001543 if (rc != NO_ERROR) {
1544 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001545 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001546 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001547
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001548 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1549 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001550
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001551 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1552 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001553
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001554 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1555 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001556
1557 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001558 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001559 }
1560
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001561 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1562 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001563
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001564 close(oldfd); /* And Close Saved STDOUT Handle */
1565 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001566
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001567 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1568 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001569
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001570 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1571 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001572
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001573 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1574 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001575
1576 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001577 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001578 }
1579
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001580 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1581 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001582
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001583 close(oldfd); /* And Close Saved STDIN Handle */
1584 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001585
Guido van Rossumd48f2521997-12-05 22:19:34 +00001586 } else {
1587 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001588 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001589 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001590}
1591
1592static PyObject *
1593posix_popen(self, args)
1594 PyObject *self;
1595 PyObject *args;
1596{
1597 char *name;
1598 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001599 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001600 FILE *fp;
1601 PyObject *f;
1602 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1603 return NULL;
1604 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001605 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001606 Py_END_ALLOW_THREADS
1607 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001608 return os2_error(err);
1609
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001610 f = PyFile_FromFile(fp, name, mode, fclose);
1611 if (f != NULL)
1612 PyFile_SetBufSize(f, bufsize);
1613 return f;
1614}
1615
1616#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001617static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001618posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001619 PyObject *self;
1620 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001621{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001622 char *name;
1623 char *mode = "r";
1624 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001625 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001626 PyObject *f;
1627 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001628 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001629 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001630 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001631 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001632 if (fp == NULL)
1633 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001634 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001635 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001636 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001637 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001638}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001639#endif
1640
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001641#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001643
Guido van Rossumb6775db1994-08-01 11:34:53 +00001644#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001645static char posix_setuid__doc__[] =
1646"setuid(uid) -> None\n\
1647Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001648static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001649posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001650 PyObject *self;
1651 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001652{
1653 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001654 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001655 return NULL;
1656 if (setuid(uid) < 0)
1657 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001658 Py_INCREF(Py_None);
1659 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001660}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001661#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001662
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001663
Guido van Rossumb6775db1994-08-01 11:34:53 +00001664#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001665static char posix_setgid__doc__[] =
1666"setgid(gid) -> None\n\
1667Set the current process's group id.";
1668
Barry Warsaw53699e91996-12-10 23:23:01 +00001669static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001670posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001671 PyObject *self;
1672 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001673{
1674 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001675 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001676 return NULL;
1677 if (setgid(gid) < 0)
1678 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001679 Py_INCREF(Py_None);
1680 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001681}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001682#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001683
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001684
Guido van Rossumb6775db1994-08-01 11:34:53 +00001685#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001686static char posix_waitpid__doc__[] =
1687"waitpid(pid, options) -> (pid, status)\n\
1688Wait for completion of a give child process.";
1689
Barry Warsaw53699e91996-12-10 23:23:01 +00001690static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001691posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001692 PyObject *self;
1693 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001694{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001695 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001696 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001697 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001698 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001699#ifdef NeXT
1700 pid = wait4(pid, (union wait *)&sts, options, NULL);
1701#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001702 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001703#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001704 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001705 if (pid == -1)
1706 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001707 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001708 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001709}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001710#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001711
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001712
Guido van Rossumad0ee831995-03-01 10:34:45 +00001713#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001714static char posix_wait__doc__[] =
1715"wait() -> (pid, status)\n\
1716Wait for completion of a child process.";
1717
Barry Warsaw53699e91996-12-10 23:23:01 +00001718static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001719posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001720 PyObject *self;
1721 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001722{
1723 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001724 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001725#ifdef NeXT
1726 pid = wait((union wait *)&sts);
1727#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001728 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001729#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001730 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001731 if (pid == -1)
1732 return posix_error();
1733 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001734 return Py_BuildValue("ii", pid, sts);
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
1739static char posix_lstat__doc__[] =
1740"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1741Like stat(path), but do not follow symbolic links.";
1742
Barry Warsaw53699e91996-12-10 23:23:01 +00001743static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001744posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001745 PyObject *self;
1746 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001747{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001748#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001750#else /* !HAVE_LSTAT */
1751 return posix_do_stat(self, args, stat);
1752#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001753}
1754
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001755
Guido van Rossumb6775db1994-08-01 11:34:53 +00001756#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001757static char posix_readlink__doc__[] =
1758"readlink(path) -> path\n\
1759Return a string representing the path to which the symbolic link points.";
1760
Barry Warsaw53699e91996-12-10 23:23:01 +00001761static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001762posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001763 PyObject *self;
1764 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001765{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001766 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001767 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001768 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001769 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001770 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001771 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001772 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001773 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001774 if (n < 0)
1775 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001776 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001777}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001778#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001779
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001780
Guido van Rossumb6775db1994-08-01 11:34:53 +00001781#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001782static char posix_symlink__doc__[] =
1783"symlink(src, dst) -> None\n\
1784Create a symbolic link.";
1785
Guido van Rossumd48f2521997-12-05 22:19:34 +00001786#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1787static long
1788system_uptime()
1789{
1790 ULONG value = 0;
1791
1792 Py_BEGIN_ALLOW_THREADS
1793 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1794 Py_END_ALLOW_THREADS
1795
1796 return value;
1797}
1798
1799static PyObject *
1800posix_times(self, args)
1801 PyObject *self;
1802 PyObject *args;
1803{
1804 if (!PyArg_NoArgs(args))
1805 return NULL;
1806
1807 /* Currently Only Uptime is Provided -- Others Later */
1808 return Py_BuildValue("ddddd",
1809 (double)0 /* t.tms_utime / HZ */,
1810 (double)0 /* t.tms_stime / HZ */,
1811 (double)0 /* t.tms_cutime / HZ */,
1812 (double)0 /* t.tms_cstime / HZ */,
1813 (double)system_uptime() / 1000);
1814}
1815#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001816static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001817posix_symlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001818 PyObject *self;
1819 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001820{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001821 return posix_2str(args, symlink);
1822}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001823#endif /* HAVE_SYMLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001824
Guido van Rossumb6775db1994-08-01 11:34:53 +00001825#ifdef HAVE_TIMES
1826#ifndef HZ
1827#define HZ 60 /* Universal constant :-) */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001828#endif /* HZ */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001829
Barry Warsaw53699e91996-12-10 23:23:01 +00001830static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001831posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001832 PyObject *self;
1833 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001834{
1835 struct tms t;
1836 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001837 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001838 return NULL;
1839 errno = 0;
1840 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001841 if (c == (clock_t) -1)
1842 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001843 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001844 (double)t.tms_utime / HZ,
1845 (double)t.tms_stime / HZ,
1846 (double)t.tms_cutime / HZ,
1847 (double)t.tms_cstime / HZ,
1848 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001849}
Guido van Rossumd48f2521997-12-05 22:19:34 +00001850#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001851#endif /* HAVE_TIMES */
Guido van Rossum87755a21996-09-07 00:59:43 +00001852#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001853#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001854static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001855posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001856 PyObject *self;
1857 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001858{
1859 FILETIME create, exit, kernel, user;
1860 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001861 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001862 return NULL;
1863 hProc = GetCurrentProcess();
1864 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001865 return Py_BuildValue(
1866 "ddddd",
1867 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1868 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1869 (double)0,
1870 (double)0,
1871 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001872}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001873#endif /* MS_WIN32 */
Roger E. Masse0318fd61997-06-05 22:07:58 +00001874static char posix_times__doc__[] =
1875"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1876Return a tuple of floating point numbers indicating process times.";
Guido van Rossum22db57e1992-04-05 14:25:30 +00001877
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001878
Guido van Rossumb6775db1994-08-01 11:34:53 +00001879#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001880static char posix_setsid__doc__[] =
1881"setsid() -> None\n\
1882Call the system call setsid().";
1883
Barry Warsaw53699e91996-12-10 23:23:01 +00001884static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001885posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001886 PyObject *self;
1887 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001888{
Barry Warsaw53699e91996-12-10 23:23:01 +00001889 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001890 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001891 if (setsid() < 0)
1892 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001893 Py_INCREF(Py_None);
1894 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001895}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001896#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001897
Guido van Rossumb6775db1994-08-01 11:34:53 +00001898#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001899static char posix_setpgid__doc__[] =
1900"setpgid(pid, pgrp) -> None\n\
1901Call the system call setpgid().";
1902
Barry Warsaw53699e91996-12-10 23:23:01 +00001903static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001904posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001905 PyObject *self;
1906 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001907{
1908 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001909 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001910 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001911 if (setpgid(pid, pgrp) < 0)
1912 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001913 Py_INCREF(Py_None);
1914 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001915}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001916#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001917
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001918
Guido van Rossumb6775db1994-08-01 11:34:53 +00001919#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001920static char posix_tcgetpgrp__doc__[] =
1921"tcgetpgrp(fd) -> pgid\n\
1922Return the process group associated with the terminal given by a fd.";
1923
Barry Warsaw53699e91996-12-10 23:23:01 +00001924static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001925posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001926 PyObject *self;
1927 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001928{
1929 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001930 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001931 return NULL;
1932 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001933 if (pgid < 0)
1934 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001935 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001936}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001937#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001938
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001939
Guido van Rossumb6775db1994-08-01 11:34:53 +00001940#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001941static char posix_tcsetpgrp__doc__[] =
1942"tcsetpgrp(fd, pgid) -> None\n\
1943Set the process group associated with the terminal given by a fd.";
1944
Barry Warsaw53699e91996-12-10 23:23:01 +00001945static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001946posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001947 PyObject *self;
1948 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001949{
1950 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001951 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001952 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001953 if (tcsetpgrp(fd, pgid) < 0)
1954 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001955 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001956 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001957}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001958#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001959
Guido van Rossum687dd131993-05-17 08:34:16 +00001960/* Functions acting on file descriptors */
1961
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001962static char posix_open__doc__[] =
1963"open(filename, flag [, mode=0777]) -> fd\n\
1964Open a file (for low level IO).";
1965
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001967posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001968 PyObject *self;
1969 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001970{
1971 char *file;
1972 int flag;
1973 int mode = 0777;
1974 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001975 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1976 return NULL;
1977
Barry Warsaw53699e91996-12-10 23:23:01 +00001978 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001979 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001980 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001981 if (fd < 0)
1982 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001983 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001984}
1985
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001986
1987static char posix_close__doc__[] =
1988"close(fd) -> None\n\
1989Close a file descriptor (for low level IO).";
1990
Barry Warsaw53699e91996-12-10 23:23:01 +00001991static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001992posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001993 PyObject *self;
1994 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001995{
1996 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001997 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00001998 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001999 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002000 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002001 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002002 if (res < 0)
2003 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002004 Py_INCREF(Py_None);
2005 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002006}
2007
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002008
2009static char posix_dup__doc__[] =
2010"dup(fd) -> fd2\n\
2011Return a duplicate of a file descriptor.";
2012
Barry Warsaw53699e91996-12-10 23:23:01 +00002013static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002014posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002015 PyObject *self;
2016 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002017{
2018 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002019 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002020 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002022 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002023 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002024 if (fd < 0)
2025 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002026 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002027}
2028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002029
2030static char posix_dup2__doc__[] =
2031"dup2(fd, fd2) -> None\n\
2032Duplicate file descriptor.";
2033
Barry Warsaw53699e91996-12-10 23:23:01 +00002034static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002035posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002036 PyObject *self;
2037 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002038{
2039 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002040 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002041 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002042 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002043 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002044 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002045 if (res < 0)
2046 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002047 Py_INCREF(Py_None);
2048 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002049}
2050
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002051
2052static char posix_lseek__doc__[] =
2053"lseek(fd, pos, how) -> newpos\n\
2054Set the current position of a file descriptor.";
2055
Barry Warsaw53699e91996-12-10 23:23:01 +00002056static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002057posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002058 PyObject *self;
2059 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002060{
2061 int fd, how;
2062 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002063 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002064 return NULL;
2065#ifdef SEEK_SET
2066 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2067 switch (how) {
2068 case 0: how = SEEK_SET; break;
2069 case 1: how = SEEK_CUR; break;
2070 case 2: how = SEEK_END; break;
2071 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002072#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00002073 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002074 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002076 if (res < 0)
2077 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002078 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00002079}
2080
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002081
2082static char posix_read__doc__[] =
2083"read(fd, buffersize) -> string\n\
2084Read a file descriptor.";
2085
Barry Warsaw53699e91996-12-10 23:23:01 +00002086static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002087posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002088 PyObject *self;
2089 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002090{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002091 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002092 PyObject *buffer;
2093 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002094 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002095 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002096 if (buffer == NULL)
2097 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002098 Py_BEGIN_ALLOW_THREADS
2099 n = read(fd, PyString_AsString(buffer), size);
2100 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002101 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002103 return posix_error();
2104 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002105 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002106 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002107 return buffer;
2108}
2109
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002110
2111static char posix_write__doc__[] =
2112"write(fd, string) -> byteswritten\n\
2113Write a string to a file descriptor.";
2114
Barry Warsaw53699e91996-12-10 23:23:01 +00002115static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002116posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002117 PyObject *self;
2118 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002119{
2120 int fd, size;
2121 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002122 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002123 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002124 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002125 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002126 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002127 if (size < 0)
2128 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002129 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002130}
2131
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002132
2133static char posix_fstat__doc__[]=
2134"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2135Like stat(), but for an open file descriptor.";
2136
Barry Warsaw53699e91996-12-10 23:23:01 +00002137static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002138posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002139 PyObject *self;
2140 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002141{
2142 int fd;
2143 struct stat st;
2144 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002145 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002146 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002147 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002148 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002149 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002150 if (res != 0)
2151 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002152 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002153 (long)st.st_mode,
2154 (long)st.st_ino,
2155 (long)st.st_dev,
2156 (long)st.st_nlink,
2157 (long)st.st_uid,
2158 (long)st.st_gid,
2159 (long)st.st_size,
2160 (long)st.st_atime,
2161 (long)st.st_mtime,
2162 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00002163}
2164
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002165
2166static char posix_fdopen__doc__[] =
2167"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2168Return an open file object connected to a file descriptor.";
2169
Barry Warsaw53699e91996-12-10 23:23:01 +00002170static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002171posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002172 PyObject *self;
2173 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002174{
Barry Warsaw53699e91996-12-10 23:23:01 +00002175 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002176 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002177 char *mode = "r";
2178 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002179 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002180 PyObject *f;
2181 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002182 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002183
Barry Warsaw53699e91996-12-10 23:23:01 +00002184 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002185 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002186 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002187 if (fp == NULL)
2188 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002189 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002190 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002191 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002192 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002193}
2194
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002195
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002196#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002197static char posix_pipe__doc__[] =
2198"pipe() -> (read_end, write_end)\n\
2199Create a pipe.";
2200
Barry Warsaw53699e91996-12-10 23:23:01 +00002201static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002202posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 PyObject *self;
2204 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002205{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002206#if defined(PYOS_OS2)
2207 HFILE read, write;
2208 APIRET rc;
2209
2210 if (!PyArg_Parse(args, ""))
2211 return NULL;
2212
2213 Py_BEGIN_ALLOW_THREADS
2214 rc = DosCreatePipe( &read, &write, 4096);
2215 Py_END_ALLOW_THREADS
2216 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002217 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002218
2219 return Py_BuildValue("(ii)", read, write);
2220#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002221#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002222 int fds[2];
2223 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002224 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002225 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002226 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002227 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002229 if (res != 0)
2230 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002231 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002232#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002233 HANDLE read, write;
2234 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002235 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002236 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002237 Py_BEGIN_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002238 ok = CreatePipe( &read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002239 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002240 if (!ok)
2241 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002242 return Py_BuildValue("(ii)", read, write);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002243#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002244#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002245}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002246#endif /* HAVE_PIPE */
2247
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002248
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002249#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002250static char posix_mkfifo__doc__[] =
2251"mkfifo(file, [, mode=0666]) -> None\n\
2252Create a FIFO (a POSIX named pipe).";
2253
Barry Warsaw53699e91996-12-10 23:23:01 +00002254static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002255posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002256 PyObject *self;
2257 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002258{
2259 char *file;
2260 int mode = 0666;
2261 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002262 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002263 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002265 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002266 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002267 if (res < 0)
2268 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002269 Py_INCREF(Py_None);
2270 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002271}
2272#endif
2273
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002274
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002275#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002276static char posix_ftruncate__doc__[] =
2277"ftruncate(fd, length) -> None\n\
2278Truncate a file to a specified length.";
2279
Barry Warsaw53699e91996-12-10 23:23:01 +00002280static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002281posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002282 PyObject *self; /* Not used */
2283 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002284{
2285 int fd;
2286 long length;
2287 int res;
2288
Barry Warsaw53699e91996-12-10 23:23:01 +00002289 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002290 return NULL;
2291
Barry Warsaw53699e91996-12-10 23:23:01 +00002292 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002293 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002294 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002295 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002296 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002297 return NULL;
2298 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002299 Py_INCREF(Py_None);
2300 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002301}
2302#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002303
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002304#ifdef NeXT
2305#define HAVE_PUTENV
2306/* Steve Spicklemire got this putenv from NeXTAnswers */
2307static int
2308putenv(char *newval)
2309{
2310 extern char **environ;
2311
2312 static int firstTime = 1;
2313 char **ep;
2314 char *cp;
2315 int esiz;
2316 char *np;
2317
2318 if (!(np = strchr(newval, '=')))
2319 return 1;
2320 *np = '\0';
2321
2322 /* look it up */
2323 for (ep=environ ; *ep ; ep++)
2324 {
2325 /* this should always be true... */
2326 if (cp = strchr(*ep, '='))
2327 {
2328 *cp = '\0';
2329 if (!strcmp(*ep, newval))
2330 {
2331 /* got it! */
2332 *cp = '=';
2333 break;
2334 }
2335 *cp = '=';
2336 }
2337 else
2338 {
2339 *np = '=';
2340 return 1;
2341 }
2342 }
2343
2344 *np = '=';
2345 if (*ep)
2346 {
2347 /* the string was already there:
2348 just replace it with the new one */
2349 *ep = newval;
2350 return 0;
2351 }
2352
2353 /* expand environ by one */
2354 for (esiz=2, ep=environ ; *ep ; ep++)
2355 esiz++;
2356 if (firstTime)
2357 {
2358 char **epp;
2359 char **newenv;
2360 if (!(newenv = malloc(esiz * sizeof(char *))))
2361 return 1;
2362
2363 for (ep=environ, epp=newenv ; *ep ;)
2364 *epp++ = *ep++;
2365 *epp++ = newval;
2366 *epp = (char *) 0;
2367 environ = newenv;
2368 }
2369 else
2370 {
2371 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2372 return 1;
2373 environ[esiz - 2] = newval;
2374 environ[esiz - 1] = (char *) 0;
2375 firstTime = 0;
2376 }
2377
2378 return 0;
2379}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002380#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002381
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002382
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002383#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002384static char posix_putenv__doc__[] =
2385"putenv(key, value) -> None\n\
2386Change or add an environment variable.";
2387
Barry Warsaw53699e91996-12-10 23:23:01 +00002388static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002389posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002390 PyObject *self;
2391 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002392{
2393 char *s1, *s2;
2394 char *new;
2395
Barry Warsaw53699e91996-12-10 23:23:01 +00002396 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002397 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002398
2399#if defined(PYOS_OS2)
2400 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2401 APIRET rc;
2402
2403 if (strlen(s2) == 0) /* If New Value is an Empty String */
2404 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2405
2406 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2407 if (rc != NO_ERROR)
2408 return os2_error(rc);
2409
2410 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2411 APIRET rc;
2412
2413 if (strlen(s2) == 0) /* If New Value is an Empty String */
2414 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2415
2416 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2417 if (rc != NO_ERROR)
2418 return os2_error(rc);
2419 } else {
2420#endif
2421
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002422 /* XXX This leaks memory -- not easy to fix :-( */
2423 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002424 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002425 (void) sprintf(new, "%s=%s", s1, s2);
2426 if (putenv(new)) {
2427 posix_error();
2428 return NULL;
2429 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002430
2431#if defined(PYOS_OS2)
2432 }
2433#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002434 Py_INCREF(Py_None);
2435 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002436}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002437#endif /* putenv */
2438
2439#ifdef HAVE_STRERROR
2440static char posix_strerror__doc__[] =
2441"strerror(code) -> string\n\
2442Translate an error code to a message string.";
2443
2444PyObject *
2445posix_strerror(self, args)
2446 PyObject *self;
2447 PyObject *args;
2448{
2449 int code;
2450 char *message;
2451 if (!PyArg_ParseTuple(args, "i", &code))
2452 return NULL;
2453 message = strerror(code);
2454 if (message == NULL) {
2455 PyErr_SetString(PyExc_ValueError,
2456 "strerror code out of range");
2457 return NULL;
2458 }
2459 return PyString_FromString(message);
2460}
2461#endif /* strerror */
2462
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002463
Barry Warsaw53699e91996-12-10 23:23:01 +00002464static PyMethodDef posix_methods[] = {
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002465 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2466 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002467#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002468 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002469#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002470#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002471 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002472#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002473#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002474 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002475#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002476 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2477 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2478 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002479#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002480 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002481#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002482#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002483 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002484#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002485 {"rename", posix_rename, 0, posix_rename__doc__},
2486 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2487 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002488#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002489 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002490#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002491#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002492 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002493#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002494 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002495#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002496 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002497#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002498 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2499 {"remove", posix_unlink, 0, posix_remove__doc__},
2500 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002501#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002502 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002503#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002504 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002505#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002506 {"execv", posix_execv, 0, posix_execv__doc__},
2507 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002508#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002509#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002510 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002511#endif /* HAVE_FORK */
2512#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002513 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002514#endif /* HAVE_GETEGID */
2515#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002516 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002517#endif /* HAVE_GETEUID */
2518#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002519 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002520#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002521 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002522#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002523 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002524#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002525#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002526 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002527#endif /* HAVE_GETPPID */
2528#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002529 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002530#endif /* HAVE_GETUID */
2531#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002532 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002533#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002534#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002535 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002536#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002537#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002539#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002540#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002542#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002543#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002544 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002545#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002546#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002547 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002548#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002549#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002550 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002551#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002552#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002553 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002554#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002555#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002556 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002557#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002558#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002559 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002560#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002562 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002563#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002564#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002565 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002566#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002567 {"open", posix_open, 1, posix_open__doc__},
2568 {"close", posix_close, 0, posix_close__doc__},
2569 {"dup", posix_dup, 0, posix_dup__doc__},
2570 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2571 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2572 {"read", posix_read, 0, posix_read__doc__},
2573 {"write", posix_write, 0, posix_write__doc__},
2574 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2575 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002576#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002577 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002578#endif
2579#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002580 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002581#endif
2582#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002583 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002584#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002585#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002586 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002587#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002588#ifdef HAVE_STRERROR
2589 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2590#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002591 {NULL, NULL} /* Sentinel */
2592};
2593
2594
Barry Warsaw4a342091996-12-19 23:50:02 +00002595static int
2596ins(d, symbol, value)
2597 PyObject* d;
2598 char* symbol;
2599 long value;
2600{
2601 PyObject* v = PyInt_FromLong(value);
2602 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2603 return -1; /* triggers fatal error */
2604
2605 Py_DECREF(v);
2606 return 0;
2607}
2608
Guido van Rossumd48f2521997-12-05 22:19:34 +00002609#if defined(PYOS_OS2)
2610/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
2611static int insertvalues(PyObject *d)
2612{
2613 APIRET rc;
2614 ULONG values[QSV_MAX+1];
2615 PyObject *v;
2616 char *ver, tmp[10];
2617
2618 Py_BEGIN_ALLOW_THREADS
2619 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
2620 Py_END_ALLOW_THREADS
2621
2622 if (rc != NO_ERROR) {
2623 os2_error(rc);
2624 return -1;
2625 }
2626
2627 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
2628 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
2629 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
2630 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
2631 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
2632 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
2633 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
2634
2635 switch (values[QSV_VERSION_MINOR]) {
2636 case 0: ver = "2.00"; break;
2637 case 10: ver = "2.10"; break;
2638 case 11: ver = "2.11"; break;
2639 case 30: ver = "3.00"; break;
2640 case 40: ver = "4.00"; break;
2641 case 50: ver = "5.00"; break;
2642 default:
2643 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
2644 values[QSV_VERSION_MINOR]);
2645 ver = &tmp[0];
2646 }
2647
2648 /* Add Indicator of the Version of the Operating System */
2649 v = PyString_FromString(ver);
2650 if (!v || PyDict_SetItemString(d, "version", v) < 0)
2651 return -1;
2652 Py_DECREF(v);
2653
2654 /* Add Indicator of Which Drive was Used to Boot the System */
2655 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
2656 tmp[1] = ':';
2657 tmp[2] = '\0';
2658
2659 v = PyString_FromString(tmp);
2660 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
2661 return -1;
2662 Py_DECREF(v);
2663
2664 return 0;
2665}
2666#endif
2667
Barry Warsaw4a342091996-12-19 23:50:02 +00002668static int
2669all_ins(d)
2670 PyObject* d;
2671{
2672#ifdef WNOHANG
2673 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
2674#endif
2675#ifdef O_RDONLY
2676 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
2677#endif
2678#ifdef O_WRONLY
2679 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
2680#endif
2681#ifdef O_RDWR
2682 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
2683#endif
2684#ifdef O_NDELAY
2685 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
2686#endif
2687#ifdef O_NONBLOCK
2688 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
2689#endif
2690#ifdef O_APPEND
2691 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
2692#endif
2693#ifdef O_DSYNC
2694 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
2695#endif
2696#ifdef O_RSYNC
2697 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
2698#endif
2699#ifdef O_SYNC
2700 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
2701#endif
2702#ifdef O_NOCTTY
2703 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
2704#endif
2705#ifdef O_CREAT
2706 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
2707#endif
2708#ifdef O_EXCL
2709 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
2710#endif
2711#ifdef O_TRUNC
2712 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
2713#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00002714#ifdef O_BINARY
2715 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
2716#endif
2717#ifdef O_TEXT
2718 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
2719#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00002720
2721#if defined(PYOS_OS2)
2722 if (insertvalues(d)) return -1;
2723#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00002724 return 0;
2725}
2726
2727
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002728#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002729#define INITFUNC initnt
2730#define MODNAME "nt"
2731#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002732#if defined(PYOS_OS2)
2733#define INITFUNC initos2
2734#define MODNAME "os2"
2735#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002736#define INITFUNC initposix
2737#define MODNAME "posix"
2738#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002739#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002740
Guido van Rossumb6775db1994-08-01 11:34:53 +00002741void
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002742INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00002743{
Barry Warsaw53699e91996-12-10 23:23:01 +00002744 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002745
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002746 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002747 posix_methods,
2748 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002749 (PyObject *)NULL,
2750 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002751 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002752
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002753 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002754 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002755 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002756 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00002757 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002758
Barry Warsaw4a342091996-12-19 23:50:02 +00002759 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00002760 return;
2761
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002762 /* Initialize exception */
2763 PosixError = PyErr_NewException("os.error", NULL, NULL);
2764 if (PosixError != NULL)
2765 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002766}