blob: cafb46fee6151bd4548820a3b50f9b9fd947e969 [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 Rossumbfaf3d61997-12-29 20:02:27 +00001786static PyObject *
1787posix_symlink(self, args)
1788 PyObject *self;
1789 PyObject *args;
1790{
1791 return posix_2str(args, symlink);
1792}
1793#endif /* HAVE_SYMLINK */
1794
1795
1796#ifdef HAVE_TIMES
1797#ifndef HZ
1798#define HZ 60 /* Universal constant :-) */
1799#endif /* HZ */
1800
Guido van Rossumd48f2521997-12-05 22:19:34 +00001801#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1802static long
1803system_uptime()
1804{
1805 ULONG value = 0;
1806
1807 Py_BEGIN_ALLOW_THREADS
1808 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1809 Py_END_ALLOW_THREADS
1810
1811 return value;
1812}
1813
1814static PyObject *
1815posix_times(self, args)
1816 PyObject *self;
1817 PyObject *args;
1818{
1819 if (!PyArg_NoArgs(args))
1820 return NULL;
1821
1822 /* Currently Only Uptime is Provided -- Others Later */
1823 return Py_BuildValue("ddddd",
1824 (double)0 /* t.tms_utime / HZ */,
1825 (double)0 /* t.tms_stime / HZ */,
1826 (double)0 /* t.tms_cutime / HZ */,
1827 (double)0 /* t.tms_cstime / HZ */,
1828 (double)system_uptime() / 1000);
1829}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001830#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00001831static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001832posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001833 PyObject *self;
1834 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001835{
1836 struct tms t;
1837 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001838 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001839 return NULL;
1840 errno = 0;
1841 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001842 if (c == (clock_t) -1)
1843 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001844 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001845 (double)t.tms_utime / HZ,
1846 (double)t.tms_stime / HZ,
1847 (double)t.tms_cutime / HZ,
1848 (double)t.tms_cstime / HZ,
1849 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001850}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001851#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001852#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001853
1854
Guido van Rossum87755a21996-09-07 00:59:43 +00001855#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001856#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001857static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001858posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001859 PyObject *self;
1860 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001861{
1862 FILETIME create, exit, kernel, user;
1863 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001864 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001865 return NULL;
1866 hProc = GetCurrentProcess();
1867 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001868 return Py_BuildValue(
1869 "ddddd",
1870 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1871 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1872 (double)0,
1873 (double)0,
1874 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001875}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001876#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001877
1878#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00001879static char posix_times__doc__[] =
1880"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1881Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001882#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001883
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001884
Guido van Rossumb6775db1994-08-01 11:34:53 +00001885#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001886static char posix_setsid__doc__[] =
1887"setsid() -> None\n\
1888Call the system call setsid().";
1889
Barry Warsaw53699e91996-12-10 23:23:01 +00001890static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001891posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001892 PyObject *self;
1893 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001894{
Barry Warsaw53699e91996-12-10 23:23:01 +00001895 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001896 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001897 if (setsid() < 0)
1898 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001899 Py_INCREF(Py_None);
1900 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001901}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001902#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001903
Guido van Rossumb6775db1994-08-01 11:34:53 +00001904#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001905static char posix_setpgid__doc__[] =
1906"setpgid(pid, pgrp) -> None\n\
1907Call the system call setpgid().";
1908
Barry Warsaw53699e91996-12-10 23:23:01 +00001909static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001910posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001911 PyObject *self;
1912 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001913{
1914 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001915 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001916 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001917 if (setpgid(pid, pgrp) < 0)
1918 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001919 Py_INCREF(Py_None);
1920 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001921}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001922#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001923
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001924
Guido van Rossumb6775db1994-08-01 11:34:53 +00001925#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001926static char posix_tcgetpgrp__doc__[] =
1927"tcgetpgrp(fd) -> pgid\n\
1928Return the process group associated with the terminal given by a fd.";
1929
Barry Warsaw53699e91996-12-10 23:23:01 +00001930static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001931posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001932 PyObject *self;
1933 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001934{
1935 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001936 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001937 return NULL;
1938 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001939 if (pgid < 0)
1940 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001941 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001942}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001943#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001944
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001945
Guido van Rossumb6775db1994-08-01 11:34:53 +00001946#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001947static char posix_tcsetpgrp__doc__[] =
1948"tcsetpgrp(fd, pgid) -> None\n\
1949Set the process group associated with the terminal given by a fd.";
1950
Barry Warsaw53699e91996-12-10 23:23:01 +00001951static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001952posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001953 PyObject *self;
1954 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001955{
1956 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001957 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001958 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001959 if (tcsetpgrp(fd, pgid) < 0)
1960 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001961 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001962 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001963}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001964#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001965
Guido van Rossum687dd131993-05-17 08:34:16 +00001966/* Functions acting on file descriptors */
1967
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001968static char posix_open__doc__[] =
1969"open(filename, flag [, mode=0777]) -> fd\n\
1970Open a file (for low level IO).";
1971
Barry Warsaw53699e91996-12-10 23:23:01 +00001972static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001973posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001974 PyObject *self;
1975 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001976{
1977 char *file;
1978 int flag;
1979 int mode = 0777;
1980 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001981 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1982 return NULL;
1983
Barry Warsaw53699e91996-12-10 23:23:01 +00001984 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001985 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001986 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001987 if (fd < 0)
1988 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001989 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001990}
1991
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001992
1993static char posix_close__doc__[] =
1994"close(fd) -> None\n\
1995Close a file descriptor (for low level IO).";
1996
Barry Warsaw53699e91996-12-10 23:23:01 +00001997static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001998posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001999 PyObject *self;
2000 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002001{
2002 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002003 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002004 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002005 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002006 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002007 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002008 if (res < 0)
2009 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002010 Py_INCREF(Py_None);
2011 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002012}
2013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002014
2015static char posix_dup__doc__[] =
2016"dup(fd) -> fd2\n\
2017Return a duplicate of a file descriptor.";
2018
Barry Warsaw53699e91996-12-10 23:23:01 +00002019static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002020posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 PyObject *self;
2022 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002023{
2024 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002025 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002026 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002027 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002028 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002029 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002030 if (fd < 0)
2031 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002032 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002033}
2034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002035
2036static char posix_dup2__doc__[] =
2037"dup2(fd, fd2) -> None\n\
2038Duplicate file descriptor.";
2039
Barry Warsaw53699e91996-12-10 23:23:01 +00002040static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002041posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002042 PyObject *self;
2043 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002044{
2045 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002046 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002047 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002048 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002049 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002050 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002051 if (res < 0)
2052 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002053 Py_INCREF(Py_None);
2054 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002055}
2056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002057
2058static char posix_lseek__doc__[] =
2059"lseek(fd, pos, how) -> newpos\n\
2060Set the current position of a file descriptor.";
2061
Barry Warsaw53699e91996-12-10 23:23:01 +00002062static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002063posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002064 PyObject *self;
2065 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002066{
2067 int fd, how;
2068 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002069 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002070 return NULL;
2071#ifdef SEEK_SET
2072 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2073 switch (how) {
2074 case 0: how = SEEK_SET; break;
2075 case 1: how = SEEK_CUR; break;
2076 case 2: how = SEEK_END; break;
2077 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002078#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00002079 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002080 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002081 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002082 if (res < 0)
2083 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002084 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00002085}
2086
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002087
2088static char posix_read__doc__[] =
2089"read(fd, buffersize) -> string\n\
2090Read a file descriptor.";
2091
Barry Warsaw53699e91996-12-10 23:23:01 +00002092static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002093posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 PyObject *self;
2095 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002096{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002097 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002098 PyObject *buffer;
2099 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002100 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002101 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002102 if (buffer == NULL)
2103 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 Py_BEGIN_ALLOW_THREADS
2105 n = read(fd, PyString_AsString(buffer), size);
2106 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002107 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002108 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002109 return posix_error();
2110 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002111 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002112 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002113 return buffer;
2114}
2115
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002116
2117static char posix_write__doc__[] =
2118"write(fd, string) -> byteswritten\n\
2119Write a string to a file descriptor.";
2120
Barry Warsaw53699e91996-12-10 23:23:01 +00002121static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002122posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002123 PyObject *self;
2124 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002125{
2126 int fd, size;
2127 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002128 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002129 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002130 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002131 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002132 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002133 if (size < 0)
2134 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002135 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002136}
2137
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002138
2139static char posix_fstat__doc__[]=
2140"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2141Like stat(), but for an open file descriptor.";
2142
Barry Warsaw53699e91996-12-10 23:23:01 +00002143static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002144posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002145 PyObject *self;
2146 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002147{
2148 int fd;
2149 struct stat st;
2150 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002151 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002152 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002153 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002154 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002155 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002156 if (res != 0)
2157 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002158 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002159 (long)st.st_mode,
2160 (long)st.st_ino,
2161 (long)st.st_dev,
2162 (long)st.st_nlink,
2163 (long)st.st_uid,
2164 (long)st.st_gid,
2165 (long)st.st_size,
2166 (long)st.st_atime,
2167 (long)st.st_mtime,
2168 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00002169}
2170
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002171
2172static char posix_fdopen__doc__[] =
2173"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2174Return an open file object connected to a file descriptor.";
2175
Barry Warsaw53699e91996-12-10 23:23:01 +00002176static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002177posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002178 PyObject *self;
2179 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002180{
Barry Warsaw53699e91996-12-10 23:23:01 +00002181 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002182 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002183 char *mode = "r";
2184 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002185 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002186 PyObject *f;
2187 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002188 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002189
Barry Warsaw53699e91996-12-10 23:23:01 +00002190 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002191 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002192 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002193 if (fp == NULL)
2194 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002195 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002196 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002197 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002198 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002199}
2200
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002201
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002202#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002203static char posix_pipe__doc__[] =
2204"pipe() -> (read_end, write_end)\n\
2205Create a pipe.";
2206
Barry Warsaw53699e91996-12-10 23:23:01 +00002207static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002208posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002209 PyObject *self;
2210 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002211{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002212#if defined(PYOS_OS2)
2213 HFILE read, write;
2214 APIRET rc;
2215
2216 if (!PyArg_Parse(args, ""))
2217 return NULL;
2218
2219 Py_BEGIN_ALLOW_THREADS
2220 rc = DosCreatePipe( &read, &write, 4096);
2221 Py_END_ALLOW_THREADS
2222 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002223 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002224
2225 return Py_BuildValue("(ii)", read, write);
2226#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002227#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002228 int fds[2];
2229 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002230 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002231 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002232 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002233 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002234 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002235 if (res != 0)
2236 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002237 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002238#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002239 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002240 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002241 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002242 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002243 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002244 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002245 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002246 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002247 if (!ok)
2248 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002249 read_fd = _open_osfhandle((long)read, 0);
2250 write_fd = _open_osfhandle((long)write, 1);
2251 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002252#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002253#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002254}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002255#endif /* HAVE_PIPE */
2256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002257
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002258#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002259static char posix_mkfifo__doc__[] =
2260"mkfifo(file, [, mode=0666]) -> None\n\
2261Create a FIFO (a POSIX named pipe).";
2262
Barry Warsaw53699e91996-12-10 23:23:01 +00002263static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002264posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002265 PyObject *self;
2266 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002267{
2268 char *file;
2269 int mode = 0666;
2270 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002271 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002272 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002273 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002274 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002275 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002276 if (res < 0)
2277 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002278 Py_INCREF(Py_None);
2279 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002280}
2281#endif
2282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002283
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002284#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002285static char posix_ftruncate__doc__[] =
2286"ftruncate(fd, length) -> None\n\
2287Truncate a file to a specified length.";
2288
Barry Warsaw53699e91996-12-10 23:23:01 +00002289static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002290posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002291 PyObject *self; /* Not used */
2292 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002293{
2294 int fd;
2295 long length;
2296 int res;
2297
Barry Warsaw53699e91996-12-10 23:23:01 +00002298 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002299 return NULL;
2300
Barry Warsaw53699e91996-12-10 23:23:01 +00002301 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002302 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002303 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002304 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002305 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002306 return NULL;
2307 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002308 Py_INCREF(Py_None);
2309 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002310}
2311#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002312
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002313#ifdef NeXT
2314#define HAVE_PUTENV
2315/* Steve Spicklemire got this putenv from NeXTAnswers */
2316static int
2317putenv(char *newval)
2318{
2319 extern char **environ;
2320
2321 static int firstTime = 1;
2322 char **ep;
2323 char *cp;
2324 int esiz;
2325 char *np;
2326
2327 if (!(np = strchr(newval, '=')))
2328 return 1;
2329 *np = '\0';
2330
2331 /* look it up */
2332 for (ep=environ ; *ep ; ep++)
2333 {
2334 /* this should always be true... */
2335 if (cp = strchr(*ep, '='))
2336 {
2337 *cp = '\0';
2338 if (!strcmp(*ep, newval))
2339 {
2340 /* got it! */
2341 *cp = '=';
2342 break;
2343 }
2344 *cp = '=';
2345 }
2346 else
2347 {
2348 *np = '=';
2349 return 1;
2350 }
2351 }
2352
2353 *np = '=';
2354 if (*ep)
2355 {
2356 /* the string was already there:
2357 just replace it with the new one */
2358 *ep = newval;
2359 return 0;
2360 }
2361
2362 /* expand environ by one */
2363 for (esiz=2, ep=environ ; *ep ; ep++)
2364 esiz++;
2365 if (firstTime)
2366 {
2367 char **epp;
2368 char **newenv;
2369 if (!(newenv = malloc(esiz * sizeof(char *))))
2370 return 1;
2371
2372 for (ep=environ, epp=newenv ; *ep ;)
2373 *epp++ = *ep++;
2374 *epp++ = newval;
2375 *epp = (char *) 0;
2376 environ = newenv;
2377 }
2378 else
2379 {
2380 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2381 return 1;
2382 environ[esiz - 2] = newval;
2383 environ[esiz - 1] = (char *) 0;
2384 firstTime = 0;
2385 }
2386
2387 return 0;
2388}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002389#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002390
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002391
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002392#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002393static char posix_putenv__doc__[] =
2394"putenv(key, value) -> None\n\
2395Change or add an environment variable.";
2396
Barry Warsaw53699e91996-12-10 23:23:01 +00002397static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002398posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002399 PyObject *self;
2400 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002401{
2402 char *s1, *s2;
2403 char *new;
2404
Barry Warsaw53699e91996-12-10 23:23:01 +00002405 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002406 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002407
2408#if defined(PYOS_OS2)
2409 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2410 APIRET rc;
2411
2412 if (strlen(s2) == 0) /* If New Value is an Empty String */
2413 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2414
2415 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2416 if (rc != NO_ERROR)
2417 return os2_error(rc);
2418
2419 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2420 APIRET rc;
2421
2422 if (strlen(s2) == 0) /* If New Value is an Empty String */
2423 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2424
2425 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2426 if (rc != NO_ERROR)
2427 return os2_error(rc);
2428 } else {
2429#endif
2430
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002431 /* XXX This leaks memory -- not easy to fix :-( */
2432 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002433 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002434 (void) sprintf(new, "%s=%s", s1, s2);
2435 if (putenv(new)) {
2436 posix_error();
2437 return NULL;
2438 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002439
2440#if defined(PYOS_OS2)
2441 }
2442#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002443 Py_INCREF(Py_None);
2444 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002445}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002446#endif /* putenv */
2447
2448#ifdef HAVE_STRERROR
2449static char posix_strerror__doc__[] =
2450"strerror(code) -> string\n\
2451Translate an error code to a message string.";
2452
2453PyObject *
2454posix_strerror(self, args)
2455 PyObject *self;
2456 PyObject *args;
2457{
2458 int code;
2459 char *message;
2460 if (!PyArg_ParseTuple(args, "i", &code))
2461 return NULL;
2462 message = strerror(code);
2463 if (message == NULL) {
2464 PyErr_SetString(PyExc_ValueError,
2465 "strerror code out of range");
2466 return NULL;
2467 }
2468 return PyString_FromString(message);
2469}
2470#endif /* strerror */
2471
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002472
Barry Warsaw53699e91996-12-10 23:23:01 +00002473static PyMethodDef posix_methods[] = {
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002474 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2475 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002476#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002477 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002478#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002479#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002480 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002481#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002482#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002483 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002484#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002485 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2486 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2487 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002488#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002489 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002490#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002491#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002492 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002493#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002494 {"rename", posix_rename, 0, posix_rename__doc__},
2495 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2496 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002497#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002498 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002499#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002500#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002502#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002503 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002504#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002505 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002506#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002507 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2508 {"remove", posix_unlink, 0, posix_remove__doc__},
2509 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002510#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002511 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002512#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002513 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002514#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002515 {"execv", posix_execv, 0, posix_execv__doc__},
2516 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002517#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002518#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002519 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002520#endif /* HAVE_FORK */
2521#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002522 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002523#endif /* HAVE_GETEGID */
2524#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002525 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002526#endif /* HAVE_GETEUID */
2527#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002528 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002529#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002530 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002531#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002532 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002533#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002534#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002535 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002536#endif /* HAVE_GETPPID */
2537#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002538 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002539#endif /* HAVE_GETUID */
2540#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002542#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002543#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002544 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002545#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002546#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002547 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002548#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002549#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002550 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002551#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002552#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002553 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002554#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002555#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002556 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002557#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002558#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002559 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002560#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002562 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002563#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002564#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002565 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002566#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002567#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002568 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002569#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002570#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002571 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002572#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002573#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002574 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002575#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002576 {"open", posix_open, 1, posix_open__doc__},
2577 {"close", posix_close, 0, posix_close__doc__},
2578 {"dup", posix_dup, 0, posix_dup__doc__},
2579 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2580 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2581 {"read", posix_read, 0, posix_read__doc__},
2582 {"write", posix_write, 0, posix_write__doc__},
2583 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2584 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002585#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002586 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002587#endif
2588#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002589 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002590#endif
2591#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002592 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002593#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002594#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002595 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002596#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002597#ifdef HAVE_STRERROR
2598 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2599#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002600 {NULL, NULL} /* Sentinel */
2601};
2602
2603
Barry Warsaw4a342091996-12-19 23:50:02 +00002604static int
2605ins(d, symbol, value)
2606 PyObject* d;
2607 char* symbol;
2608 long value;
2609{
2610 PyObject* v = PyInt_FromLong(value);
2611 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2612 return -1; /* triggers fatal error */
2613
2614 Py_DECREF(v);
2615 return 0;
2616}
2617
Guido van Rossumd48f2521997-12-05 22:19:34 +00002618#if defined(PYOS_OS2)
2619/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
2620static int insertvalues(PyObject *d)
2621{
2622 APIRET rc;
2623 ULONG values[QSV_MAX+1];
2624 PyObject *v;
2625 char *ver, tmp[10];
2626
2627 Py_BEGIN_ALLOW_THREADS
2628 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
2629 Py_END_ALLOW_THREADS
2630
2631 if (rc != NO_ERROR) {
2632 os2_error(rc);
2633 return -1;
2634 }
2635
2636 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
2637 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
2638 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
2639 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
2640 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
2641 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
2642 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
2643
2644 switch (values[QSV_VERSION_MINOR]) {
2645 case 0: ver = "2.00"; break;
2646 case 10: ver = "2.10"; break;
2647 case 11: ver = "2.11"; break;
2648 case 30: ver = "3.00"; break;
2649 case 40: ver = "4.00"; break;
2650 case 50: ver = "5.00"; break;
2651 default:
2652 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
2653 values[QSV_VERSION_MINOR]);
2654 ver = &tmp[0];
2655 }
2656
2657 /* Add Indicator of the Version of the Operating System */
2658 v = PyString_FromString(ver);
2659 if (!v || PyDict_SetItemString(d, "version", v) < 0)
2660 return -1;
2661 Py_DECREF(v);
2662
2663 /* Add Indicator of Which Drive was Used to Boot the System */
2664 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
2665 tmp[1] = ':';
2666 tmp[2] = '\0';
2667
2668 v = PyString_FromString(tmp);
2669 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
2670 return -1;
2671 Py_DECREF(v);
2672
2673 return 0;
2674}
2675#endif
2676
Barry Warsaw4a342091996-12-19 23:50:02 +00002677static int
2678all_ins(d)
2679 PyObject* d;
2680{
2681#ifdef WNOHANG
2682 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
2683#endif
2684#ifdef O_RDONLY
2685 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
2686#endif
2687#ifdef O_WRONLY
2688 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
2689#endif
2690#ifdef O_RDWR
2691 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
2692#endif
2693#ifdef O_NDELAY
2694 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
2695#endif
2696#ifdef O_NONBLOCK
2697 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
2698#endif
2699#ifdef O_APPEND
2700 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
2701#endif
2702#ifdef O_DSYNC
2703 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
2704#endif
2705#ifdef O_RSYNC
2706 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
2707#endif
2708#ifdef O_SYNC
2709 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
2710#endif
2711#ifdef O_NOCTTY
2712 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
2713#endif
2714#ifdef O_CREAT
2715 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
2716#endif
2717#ifdef O_EXCL
2718 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
2719#endif
2720#ifdef O_TRUNC
2721 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
2722#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00002723#ifdef O_BINARY
2724 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
2725#endif
2726#ifdef O_TEXT
2727 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
2728#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00002729
2730#if defined(PYOS_OS2)
2731 if (insertvalues(d)) return -1;
2732#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00002733 return 0;
2734}
2735
2736
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002737#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002738#define INITFUNC initnt
2739#define MODNAME "nt"
2740#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002741#if defined(PYOS_OS2)
2742#define INITFUNC initos2
2743#define MODNAME "os2"
2744#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002745#define INITFUNC initposix
2746#define MODNAME "posix"
2747#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002748#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002749
Guido van Rossumb6775db1994-08-01 11:34:53 +00002750void
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002751INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00002752{
Barry Warsaw53699e91996-12-10 23:23:01 +00002753 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002754
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002755 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002756 posix_methods,
2757 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002758 (PyObject *)NULL,
2759 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002760 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002761
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002762 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002763 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002764 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002765 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00002766 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002767
Barry Warsaw4a342091996-12-19 23:50:02 +00002768 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00002769 return;
2770
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002771 /* Initialize exception */
2772 PosixError = PyErr_NewException("os.error", NULL, NULL);
2773 if (PosixError != NULL)
2774 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002775}