blob: b53fb0cec651b1d36903539e4f969ac183c64921 [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 Warsawd58d7641998-07-23 16:14:40 +0000328static PyObject *
329posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000330{
Barry Warsaw53699e91996-12-10 23:23:01 +0000331 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332}
Barry Warsawd58d7641998-07-23 16:14:40 +0000333static PyObject *
334posix_error_with_filename(name)
335 char* name;
336{
337 return PyErr_SetFromErrnoWithFilename(PosixError, name);
338}
339
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340
Guido van Rossumd48f2521997-12-05 22:19:34 +0000341#if defined(PYOS_OS2)
342/**********************************************************************
343 * Helper Function to Trim and Format OS/2 Messages
344 **********************************************************************/
345 static void
346os2_formatmsg(char *msgbuf, int msglen, char *reason)
347{
348 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
349
350 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
351 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
352
353 while (lastc > msgbuf && isspace(*lastc))
354 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
355 }
356
357 /* Add Optional Reason Text */
358 if (reason) {
359 strcat(msgbuf, " : ");
360 strcat(msgbuf, reason);
361 }
362}
363
364/**********************************************************************
365 * Decode an OS/2 Operating System Error Code
366 *
367 * A convenience function to lookup an OS/2 error code and return a
368 * text message we can use to raise a Python exception.
369 *
370 * Notes:
371 * The messages for errors returned from the OS/2 kernel reside in
372 * the file OSO001.MSG in the \OS2 directory hierarchy.
373 *
374 **********************************************************************/
375 static char *
376os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
377{
378 APIRET rc;
379 ULONG msglen;
380
381 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
382 Py_BEGIN_ALLOW_THREADS
383 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
384 errorcode, "oso001.msg", &msglen);
385 Py_END_ALLOW_THREADS
386
387 if (rc == NO_ERROR)
388 os2_formatmsg(msgbuf, msglen, reason);
389 else
390 sprintf(msgbuf, "unknown OS error #%d", errorcode);
391
392 return msgbuf;
393}
394
395/* Set an OS/2-specific error and return NULL. OS/2 kernel
396 errors are not in a global variable e.g. 'errno' nor are
397 they congruent with posix error numbers. */
398
399static PyObject * os2_error(int code)
400{
401 char text[1024];
402 PyObject *v;
403
404 os2_strerror(text, sizeof(text), code, "");
405
406 v = Py_BuildValue("(is)", code, text);
407 if (v != NULL) {
408 PyErr_SetObject(PosixError, v);
409 Py_DECREF(v);
410 }
411 return NULL; /* Signal to Python that an Exception is Pending */
412}
413
414#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415
416/* POSIX generic methods */
417
Barry Warsaw53699e91996-12-10 23:23:01 +0000418static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000420 PyObject *args;
421 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000423 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000424 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000425 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000427 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000428 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000429 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000430 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000431 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000432 Py_INCREF(Py_None);
433 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434}
435
Barry Warsaw53699e91996-12-10 23:23:01 +0000436static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000438 PyObject *args;
439 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000441 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000442 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000443 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000445 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000446 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000447 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000448 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000449 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000451 Py_INCREF(Py_None);
452 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453}
454
Barry Warsaw53699e91996-12-10 23:23:01 +0000455static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000457 PyObject *args;
458 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000460 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000462 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000463 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000465 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000466 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000468 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000469 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000470 Py_INCREF(Py_None);
471 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472}
473
Barry Warsaw53699e91996-12-10 23:23:01 +0000474static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000475posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000476 PyObject *args;
477 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000478{
479 char *path;
480 int i,i2;
481 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000482 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000483 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000484 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000485 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000486 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000487 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000488 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_INCREF(Py_None);
490 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000491}
492
Barry Warsaw53699e91996-12-10 23:23:01 +0000493static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000495 PyObject *self;
496 PyObject *args;
497 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498{
499 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000500 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000501 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000502 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000503 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000504 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000505 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000506 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000507 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000508 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000509 return Py_BuildValue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000510 (long)st.st_mode,
511 (long)st.st_ino,
512 (long)st.st_dev,
513 (long)st.st_nlink,
514 (long)st.st_uid,
515 (long)st.st_gid,
516 (long)st.st_size,
517 (long)st.st_atime,
518 (long)st.st_mtime,
519 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520}
521
522
523/* POSIX methods */
524
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000525static char posix_chdir__doc__[] =
526"chdir(path) -> None\n\
527Change the current working directory to the specified path.";
528
Barry Warsaw53699e91996-12-10 23:23:01 +0000529static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000531 PyObject *self;
532 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534 return posix_1str(args, chdir);
535}
536
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000537
538static char posix_chmod__doc__[] =
539"chmod(path, mode) -> None\n\
540Change the access permissions of a file.";
541
Barry Warsaw53699e91996-12-10 23:23:01 +0000542static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000544 PyObject *self;
545 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547 return posix_strint(args, chmod);
548}
549
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000550
Guido van Rossumb6775db1994-08-01 11:34:53 +0000551#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000552static char posix_chown__doc__[] =
553"chown(path, uid, gid) -> None\n\
554Change the owner and group id of path to the numeric uid and gid.";
555
Barry Warsaw53699e91996-12-10 23:23:01 +0000556static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000557posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000558 PyObject *self;
559 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000560{
561 return posix_strintint(args, chown);
562}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000563#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000565
Guido van Rossum36bc6801995-06-14 22:54:23 +0000566#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000567static char posix_getcwd__doc__[] =
568"getcwd() -> path\n\
569Return a string representing the current working directory.";
570
Barry Warsaw53699e91996-12-10 23:23:01 +0000571static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000573 PyObject *self;
574 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575{
576 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000577 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000578 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000580 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000581 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000582 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000583 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000585 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000587#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000589
Guido van Rossumb6775db1994-08-01 11:34:53 +0000590#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000591static char posix_link__doc__[] =
592"link(src, dst) -> None\n\
593Create a hard link to a file.";
594
Barry Warsaw53699e91996-12-10 23:23:01 +0000595static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000597 PyObject *self;
598 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600 return posix_2str(args, link);
601}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000602#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000603
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000604
605static char posix_listdir__doc__[] =
606"listdir(path) -> list_of_strings\n\
607Return a list containing the names of the entries in the directory.\n\
608\n\
609 path: path of directory to list\n\
610\n\
611The list is in arbitrary order. It does not include the special\n\
612entries '.' and '..' even if they are present in the directory.";
613
Barry Warsaw53699e91996-12-10 23:23:01 +0000614static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000615posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000616 PyObject *self;
617 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000618{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000619 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000620 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000621#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000622
Guido van Rossumb6775db1994-08-01 11:34:53 +0000623 char *name;
624 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000625 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000626 HANDLE hFindFile;
627 WIN32_FIND_DATA FileData;
628 char namebuf[MAX_PATH+5];
629
Barry Warsaw53699e91996-12-10 23:23:01 +0000630 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000631 return NULL;
632 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000633 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000634 return NULL;
635 }
636 strcpy(namebuf, name);
637 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
638 namebuf[len++] = '/';
639 strcpy(namebuf + len, "*.*");
640
Barry Warsaw53699e91996-12-10 23:23:01 +0000641 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000642 return NULL;
643
644 hFindFile = FindFirstFile(namebuf, &FileData);
645 if (hFindFile == INVALID_HANDLE_VALUE) {
646 errno = GetLastError();
647 return posix_error();
648 }
649 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000650 if (FileData.cFileName[0] == '.' &&
651 (FileData.cFileName[1] == '\0' ||
652 FileData.cFileName[1] == '.' &&
653 FileData.cFileName[2] == '\0'))
654 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000655 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000656 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000657 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000658 d = NULL;
659 break;
660 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000661 if (PyList_Append(d, v) != 0) {
662 Py_DECREF(v);
663 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000664 d = NULL;
665 break;
666 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000667 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000668 } while (FindNextFile(hFindFile, &FileData) == TRUE);
669
670 if (FindClose(hFindFile) == FALSE) {
671 errno = GetLastError();
672 return posix_error();
673 }
674
675 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000676
Guido van Rossum8d665e61996-06-26 18:22:49 +0000677#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000678#ifdef _MSC_VER /* 16-bit Windows */
679
680#ifndef MAX_PATH
681#define MAX_PATH 250
682#endif
683 char *name, *pt;
684 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000685 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000686 char namebuf[MAX_PATH+5];
687 struct _find_t ep;
688
Barry Warsaw53699e91996-12-10 23:23:01 +0000689 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000690 return NULL;
691 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000692 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000693 return NULL;
694 }
695 strcpy(namebuf, name);
696 for (pt = namebuf; *pt; pt++)
697 if (*pt == '/')
698 *pt = '\\';
699 if (namebuf[len-1] != '\\')
700 namebuf[len++] = '\\';
701 strcpy(namebuf + len, "*.*");
702
Barry Warsaw53699e91996-12-10 23:23:01 +0000703 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000704 return NULL;
705
706 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000707 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
708 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000709 errno = ENOENT;
710 return posix_error();
711 }
712 do {
713 if (ep.name[0] == '.' &&
714 (ep.name[1] == '\0' ||
715 ep.name[1] == '.' &&
716 ep.name[2] == '\0'))
717 continue;
718 strcpy(namebuf, ep.name);
719 for (pt = namebuf; *pt; pt++)
720 if (isupper(*pt))
721 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000722 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000723 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000724 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000725 d = NULL;
726 break;
727 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000728 if (PyList_Append(d, v) != 0) {
729 Py_DECREF(v);
730 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000731 d = NULL;
732 break;
733 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000734 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000735 } while (_dos_findnext(&ep) == 0);
736
737 return d;
738
739#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000740#if defined(PYOS_OS2)
741
742#ifndef MAX_PATH
743#define MAX_PATH CCHMAXPATH
744#endif
745 char *name, *pt;
746 int len;
747 PyObject *d, *v;
748 char namebuf[MAX_PATH+5];
749 HDIR hdir = 1;
750 ULONG srchcnt = 1;
751 FILEFINDBUF3 ep;
752 APIRET rc;
753
754 if (!PyArg_Parse(args, "s#", &name, &len))
755 return NULL;
756 if (len >= MAX_PATH) {
757 PyErr_SetString(PyExc_ValueError, "path too long");
758 return NULL;
759 }
760 strcpy(namebuf, name);
761 for (pt = namebuf; *pt; pt++)
762 if (*pt == '/')
763 *pt = '\\';
764 if (namebuf[len-1] != '\\')
765 namebuf[len++] = '\\';
766 strcpy(namebuf + len, "*.*");
767
768 if ((d = PyList_New(0)) == NULL)
769 return NULL;
770
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000771 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
772 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000773 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000774 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
775 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
776 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000777
778 if (rc != NO_ERROR) {
779 errno = ENOENT;
780 return posix_error();
781 }
782
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000783 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000784 do {
785 if (ep.achName[0] == '.'
786 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000787 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000788
789 strcpy(namebuf, ep.achName);
790
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000791 /* Leave Case of Name Alone -- In Native Form */
792 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000793
794 v = PyString_FromString(namebuf);
795 if (v == NULL) {
796 Py_DECREF(d);
797 d = NULL;
798 break;
799 }
800 if (PyList_Append(d, v) != 0) {
801 Py_DECREF(v);
802 Py_DECREF(d);
803 d = NULL;
804 break;
805 }
806 Py_DECREF(v);
807 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
808 }
809
810 return d;
811#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000812
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000813 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000814 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000816 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000817 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000819 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000820 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000821 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000823 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000824 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000826 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000827 return NULL;
828 }
829 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000830 if (ep->d_name[0] == '.' &&
831 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000832 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000833 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000834 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000835 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000836 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837 d = NULL;
838 break;
839 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000840 if (PyList_Append(d, v) != 0) {
841 Py_DECREF(v);
842 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843 d = NULL;
844 break;
845 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000846 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847 }
848 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000849 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000850
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000851 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000852
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000853#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000854#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000855#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856}
857
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000858static char posix_mkdir__doc__[] =
859"mkdir(path [, mode=0777]) -> None\n\
860Create a directory.";
861
Barry Warsaw53699e91996-12-10 23:23:01 +0000862static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000864 PyObject *self;
865 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000867 int res;
868 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000869 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000870 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000871 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000872 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000873#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000874 res = mkdir(path);
875#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000876 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000877#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000878 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000879 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000880 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000881 Py_INCREF(Py_None);
882 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000883}
884
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000885
Guido van Rossumb6775db1994-08-01 11:34:53 +0000886#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000887static char posix_nice__doc__[] =
888"nice(inc) -> new_priority\n\
889Decrease the priority of process and return new priority.";
890
Barry Warsaw53699e91996-12-10 23:23:01 +0000891static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000892posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000893 PyObject *self;
894 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000895{
896 int increment, value;
897
Barry Warsaw53699e91996-12-10 23:23:01 +0000898 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000899 return NULL;
900 value = nice(increment);
901 if (value == -1)
902 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000903 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000904}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000905#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000906
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000907
908static char posix_rename__doc__[] =
909"rename(old, new) -> None\n\
910Rename a file or directory.";
911
Barry Warsaw53699e91996-12-10 23:23:01 +0000912static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000914 PyObject *self;
915 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000916{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917 return posix_2str(args, rename);
918}
919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000920
921static char posix_rmdir__doc__[] =
922"rmdir(path) -> None\n\
923Remove a directory.";
924
Barry Warsaw53699e91996-12-10 23:23:01 +0000925static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000926posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000927 PyObject *self;
928 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000930 return posix_1str(args, rmdir);
931}
932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000933
934static char posix_stat__doc__[] =
935"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
936Perform a stat system call on the given path.";
937
Barry Warsaw53699e91996-12-10 23:23:01 +0000938static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000939posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000940 PyObject *self;
941 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000942{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000943 return posix_do_stat(self, args, stat);
944}
945
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000946
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000947#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000948static char posix_system__doc__[] =
949"system(command) -> exit_status\n\
950Execute the command (a string) in a subshell.";
951
Barry Warsaw53699e91996-12-10 23:23:01 +0000952static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000953posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000954 PyObject *self;
955 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000956{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000957 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000958 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000959 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000960 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000961 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000962 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +0000963 Py_END_ALLOW_THREADS
964 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000966#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000968
969static char posix_umask__doc__[] =
970"umask(new_mask) -> old_mask\n\
971Set the current numeric umask and return the previous umask.";
972
Barry Warsaw53699e91996-12-10 23:23:01 +0000973static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000974posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000975 PyObject *self;
976 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000977{
978 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +0000979 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000980 return NULL;
981 i = umask(i);
982 if (i < 0)
983 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000984 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000985}
986
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000987
988static char posix_unlink__doc__[] =
989"unlink(path) -> None\n\
990Remove a file (same as remove(path)).";
991
992static char posix_remove__doc__[] =
993"remove(path) -> None\n\
994Remove a file (same as unlink(path)).";
995
Barry Warsaw53699e91996-12-10 23:23:01 +0000996static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000997posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000998 PyObject *self;
999 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001001 return posix_1str(args, unlink);
1002}
1003
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001004
Guido van Rossumb6775db1994-08-01 11:34:53 +00001005#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001006static char posix_uname__doc__[] =
1007"uname() -> (sysname, nodename, release, version, machine)\n\
1008Return a tuple identifying the current operating system.";
1009
Barry Warsaw53699e91996-12-10 23:23:01 +00001010static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001011posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001012 PyObject *self;
1013 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001014{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001015 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001016 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001017 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001018 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001019 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001020 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001021 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001022 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001023 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001024 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001025 u.sysname,
1026 u.nodename,
1027 u.release,
1028 u.version,
1029 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001030}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001031#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001033
1034static char posix_utime__doc__[] =
1035"utime(path, (atime, utime)) -> None\n\
1036Set the access and modified time of the file to the given values.";
1037
Barry Warsaw53699e91996-12-10 23:23:01 +00001038static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001039posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001040 PyObject *self;
1041 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001042{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001043 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001044 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001045 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001046
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001047/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001048#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001049 struct utimbuf buf;
1050#define ATIME buf.actime
1051#define MTIME buf.modtime
1052#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001053#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001054 time_t buf[2];
1055#define ATIME buf[0]
1056#define MTIME buf[1]
1057#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001058#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001059
Barry Warsaw53699e91996-12-10 23:23:01 +00001060 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001061 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001062 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001063 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001064 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001065 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001066 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001067 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001068 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001069 Py_INCREF(Py_None);
1070 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001071#undef UTIME_ARG
1072#undef ATIME
1073#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001074}
1075
Guido van Rossum85e3b011991-06-03 12:42:10 +00001076
Guido van Rossum3b066191991-06-04 19:40:25 +00001077/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001079static char posix__exit__doc__[] =
1080"_exit(status)\n\
1081Exit to the system with specified status, without normal exit processing.";
1082
Barry Warsaw53699e91996-12-10 23:23:01 +00001083static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001084posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001085 PyObject *self;
1086 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001087{
1088 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001089 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001090 return NULL;
1091 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001092 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001093}
1094
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001095
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001096#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001097static char posix_execv__doc__[] =
1098"execv(path, args)\n\
1099Execute an executable path with arguments, replacing current process.\n\
1100\n\
1101 path: path of executable file\n\
1102 args: tuple or list of strings";
1103
Barry Warsaw53699e91996-12-10 23:23:01 +00001104static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001105posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001106 PyObject *self;
1107 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001108{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001109 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001110 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001111 char **argvlist;
1112 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001113 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001114
Guido van Rossum89b33251993-10-22 14:26:06 +00001115 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001116 argv is a list or tuple of strings. */
1117
Barry Warsaw53699e91996-12-10 23:23:01 +00001118 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001119 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001120 if (PyList_Check(argv)) {
1121 argc = PyList_Size(argv);
1122 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001123 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001124 else if (PyTuple_Check(argv)) {
1125 argc = PyTuple_Size(argv);
1126 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001127 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001128 else {
1129 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001130 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001131 return NULL;
1132 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001133
Barry Warsaw53699e91996-12-10 23:23:01 +00001134 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001135 if (argvlist == NULL)
1136 return NULL;
1137 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001138 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1139 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001140 goto badarg;
1141 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001142 }
1143 argvlist[argc] = NULL;
1144
Guido van Rossumb6775db1994-08-01 11:34:53 +00001145#ifdef BAD_EXEC_PROTOTYPES
1146 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001147#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001148 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001149#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001150
Guido van Rossum85e3b011991-06-03 12:42:10 +00001151 /* If we get here it's definitely an error */
1152
Barry Warsaw53699e91996-12-10 23:23:01 +00001153 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001154 return posix_error();
1155}
1156
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001157
1158static char posix_execve__doc__[] =
1159"execve(path, args, env)\n\
1160Execute a path with arguments and environment, replacing current process.\n\
1161\n\
1162 path: path of executable file\n\
1163 args: tuple or list of arguments\n\
1164 env: dictonary of strings mapping to strings";
1165
Barry Warsaw53699e91996-12-10 23:23:01 +00001166static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001167posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001168 PyObject *self;
1169 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001170{
1171 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001172 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001173 char **argvlist;
1174 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001175 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001176 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001177 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001178
1179 /* execve has three arguments: (path, argv, env), where
1180 argv is a list or tuple of strings and env is a dictionary
1181 like posix.environ. */
1182
Barry Warsaw53699e91996-12-10 23:23:01 +00001183 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001184 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001185 if (PyList_Check(argv)) {
1186 argc = PyList_Size(argv);
1187 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001188 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001189 else if (PyTuple_Check(argv)) {
1190 argc = PyTuple_Size(argv);
1191 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001192 }
1193 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001194 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001195 return NULL;
1196 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001197 if (!PyMapping_Check(env)) {
1198 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001199 return NULL;
1200 }
1201
Barry Warsaw53699e91996-12-10 23:23:01 +00001202 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001203 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001204 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001205 return NULL;
1206 }
1207 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001208 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001209 "s;argv must be list of strings",
1210 &argvlist[i]))
1211 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001212 goto fail_1;
1213 }
1214 }
1215 argvlist[argc] = NULL;
1216
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001217 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001218 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001219 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001220 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001221 goto fail_1;
1222 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001223 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001224 keys = PyMapping_Keys(env);
1225 vals = PyMapping_Values(env);
1226 if (!keys || !vals)
1227 goto fail_2;
1228
1229 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001230 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001231
1232 key = PyList_GetItem(keys, pos);
1233 val = PyList_GetItem(vals, pos);
1234 if (!key || !val)
1235 goto fail_2;
1236
Barry Warsaw53699e91996-12-10 23:23:01 +00001237 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001238 !PyArg_Parse(val, "s;non-string value in env", &v))
1239 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001240 goto fail_2;
1241 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001242
1243#if defined(PYOS_OS2)
1244 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1245 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1246#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001247 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001248 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001249 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001250 goto fail_2;
1251 }
1252 sprintf(p, "%s=%s", k, v);
1253 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001254#if defined(PYOS_OS2)
1255 }
1256#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001257 }
1258 envlist[envc] = 0;
1259
Guido van Rossumb6775db1994-08-01 11:34:53 +00001260
1261#ifdef BAD_EXEC_PROTOTYPES
1262 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001263#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001264 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001265#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001266
1267 /* If we get here it's definitely an error */
1268
1269 (void) posix_error();
1270
1271 fail_2:
1272 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001273 PyMem_DEL(envlist[envc]);
1274 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001275 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001276 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001277 Py_XDECREF(vals);
1278 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001279 return NULL;
1280}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001281#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001283
Guido van Rossumad0ee831995-03-01 10:34:45 +00001284#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001285static char posix_fork__doc__[] =
1286"fork() -> pid\n\
1287Fork a child process.\n\
1288\n\
1289Return 0 to child process and PID of child to parent process.";
1290
Barry Warsaw53699e91996-12-10 23:23:01 +00001291static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001292posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001293 PyObject *self;
1294 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001295{
1296 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001297 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001298 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001299 pid = fork();
1300 if (pid == -1)
1301 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001302 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001303 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001304}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001305#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001306
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001307
Guido van Rossumad0ee831995-03-01 10:34:45 +00001308#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001309static char posix_getegid__doc__[] =
1310"getegid() -> egid\n\
1311Return the current process's effective group id.";
1312
Barry Warsaw53699e91996-12-10 23:23:01 +00001313static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001314posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001315 PyObject *self;
1316 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001317{
Barry Warsaw53699e91996-12-10 23:23:01 +00001318 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001319 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001320 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001321}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001322#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001323
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001324
Guido van Rossumad0ee831995-03-01 10:34:45 +00001325#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001326static char posix_geteuid__doc__[] =
1327"geteuid() -> euid\n\
1328Return the current process's effective user id.";
1329
Barry Warsaw53699e91996-12-10 23:23:01 +00001330static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001331posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001332 PyObject *self;
1333 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001334{
Barry Warsaw53699e91996-12-10 23:23:01 +00001335 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001336 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001337 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001338}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001339#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001340
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001341
Guido van Rossumad0ee831995-03-01 10:34:45 +00001342#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001343static char posix_getgid__doc__[] =
1344"getgid() -> gid\n\
1345Return the current process's group id.";
1346
Barry Warsaw53699e91996-12-10 23:23:01 +00001347static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001348posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001349 PyObject *self;
1350 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001351{
Barry Warsaw53699e91996-12-10 23:23:01 +00001352 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001353 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001354 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001355}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001356#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001357
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001358
1359static char posix_getpid__doc__[] =
1360"getpid() -> pid\n\
1361Return the current process id";
1362
Barry Warsaw53699e91996-12-10 23:23:01 +00001363static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001364posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001365 PyObject *self;
1366 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001367{
Barry Warsaw53699e91996-12-10 23:23:01 +00001368 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001369 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001370 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001371}
1372
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001373
Guido van Rossumb6775db1994-08-01 11:34:53 +00001374#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001375static char posix_getpgrp__doc__[] =
1376"getpgrp() -> pgrp\n\
1377Return the current process group id.";
1378
Barry Warsaw53699e91996-12-10 23:23:01 +00001379static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001380posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001381 PyObject *self;
1382 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001383{
Barry Warsaw53699e91996-12-10 23:23:01 +00001384 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001385 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001386#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001387 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001388#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001389 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001390#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001391}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001392#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001394
Guido van Rossumb6775db1994-08-01 11:34:53 +00001395#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001396static char posix_setpgrp__doc__[] =
1397"setpgrp() -> None\n\
1398Make this process a session leader.";
1399
Barry Warsaw53699e91996-12-10 23:23:01 +00001400static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001401posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001402 PyObject *self;
1403 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001404{
Barry Warsaw53699e91996-12-10 23:23:01 +00001405 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001406 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001407#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001408 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001409#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001410 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001411#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001412 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001413 Py_INCREF(Py_None);
1414 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001415}
1416
Guido van Rossumb6775db1994-08-01 11:34:53 +00001417#endif /* HAVE_SETPGRP */
1418
Guido van Rossumad0ee831995-03-01 10:34:45 +00001419#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001420static char posix_getppid__doc__[] =
1421"getppid() -> ppid\n\
1422Return the parent's process id.";
1423
Barry Warsaw53699e91996-12-10 23:23:01 +00001424static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001425posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001426 PyObject *self;
1427 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001428{
Barry Warsaw53699e91996-12-10 23:23:01 +00001429 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001430 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001431 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001432}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001433#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001434
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001435
Guido van Rossumad0ee831995-03-01 10:34:45 +00001436#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001437static char posix_getuid__doc__[] =
1438"getuid() -> uid\n\
1439Return the current process's user id.";
1440
Barry Warsaw53699e91996-12-10 23:23:01 +00001441static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001442posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001443 PyObject *self;
1444 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001445{
Barry Warsaw53699e91996-12-10 23:23:01 +00001446 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001447 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001448 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001449}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001450#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001451
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001452
Guido van Rossumad0ee831995-03-01 10:34:45 +00001453#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001454static char posix_kill__doc__[] =
1455"kill(pid, sig) -> None\n\
1456Kill a process with a signal.";
1457
Barry Warsaw53699e91996-12-10 23:23:01 +00001458static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001459posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001460 PyObject *self;
1461 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001462{
1463 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001464 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001465 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001466#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001467 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1468 APIRET rc;
1469 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001470 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001471
1472 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1473 APIRET rc;
1474 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001475 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001476
1477 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001478 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001479#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001480 if (kill(pid, sig) == -1)
1481 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001482#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001483 Py_INCREF(Py_None);
1484 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001485}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001486#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001487
Guido van Rossumc0125471996-06-28 18:55:32 +00001488#ifdef HAVE_PLOCK
1489
1490#ifdef HAVE_SYS_LOCK_H
1491#include <sys/lock.h>
1492#endif
1493
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001494static char posix_plock__doc__[] =
1495"plock(op) -> None\n\
1496Lock program segments into memory.";
1497
Barry Warsaw53699e91996-12-10 23:23:01 +00001498static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001499posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001500 PyObject *self;
1501 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001502{
1503 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001504 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001505 return NULL;
1506 if (plock(op) == -1)
1507 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001508 Py_INCREF(Py_None);
1509 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001510}
1511#endif
1512
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001513
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001514#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001515static char posix_popen__doc__[] =
1516"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1517Open a pipe to/from a command returning a file object.";
1518
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001519#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001520static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001521async_system(const char *command)
1522{
1523 char *p, errormsg[256], args[1024];
1524 RESULTCODES rcodes;
1525 APIRET rc;
1526 char *shell = getenv("COMSPEC");
1527 if (!shell)
1528 shell = "cmd";
1529
1530 strcpy(args, shell);
1531 p = &args[ strlen(args)+1 ];
1532 strcpy(p, "/c ");
1533 strcat(p, command);
1534 p += strlen(p) + 1;
1535 *p = '\0';
1536
1537 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001538 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001539 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001540 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001541 &rcodes, shell);
1542 return rc;
1543}
1544
Guido van Rossumd48f2521997-12-05 22:19:34 +00001545static FILE *
1546popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001547{
1548 HFILE rhan, whan;
1549 FILE *retfd = NULL;
1550 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1551
Guido van Rossumd48f2521997-12-05 22:19:34 +00001552 if (rc != NO_ERROR) {
1553 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001554 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001555 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001556
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001557 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1558 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001559
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001560 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1561 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001562
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001563 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1564 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001565
1566 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001567 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001568 }
1569
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001570 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1571 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001572
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001573 close(oldfd); /* And Close Saved STDOUT Handle */
1574 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001575
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001576 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1577 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001578
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001579 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1580 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001581
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001582 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1583 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001584
1585 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001586 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001587 }
1588
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001589 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1590 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001591
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001592 close(oldfd); /* And Close Saved STDIN Handle */
1593 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001594
Guido van Rossumd48f2521997-12-05 22:19:34 +00001595 } else {
1596 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001597 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001598 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001599}
1600
1601static PyObject *
1602posix_popen(self, args)
1603 PyObject *self;
1604 PyObject *args;
1605{
1606 char *name;
1607 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001608 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001609 FILE *fp;
1610 PyObject *f;
1611 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1612 return NULL;
1613 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001614 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001615 Py_END_ALLOW_THREADS
1616 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001617 return os2_error(err);
1618
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001619 f = PyFile_FromFile(fp, name, mode, fclose);
1620 if (f != NULL)
1621 PyFile_SetBufSize(f, bufsize);
1622 return f;
1623}
1624
1625#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001626static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001627posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001628 PyObject *self;
1629 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001630{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001631 char *name;
1632 char *mode = "r";
1633 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001634 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001635 PyObject *f;
1636 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001637 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001638 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001639 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001641 if (fp == NULL)
1642 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001643 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001644 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001645 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001646 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001647}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001648#endif
1649
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001650#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001651
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001652
Guido van Rossumb6775db1994-08-01 11:34:53 +00001653#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001654static char posix_setuid__doc__[] =
1655"setuid(uid) -> None\n\
1656Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001657static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001658posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001659 PyObject *self;
1660 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001661{
1662 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001663 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001664 return NULL;
1665 if (setuid(uid) < 0)
1666 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001667 Py_INCREF(Py_None);
1668 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001669}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001670#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001671
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001672
Guido van Rossumb6775db1994-08-01 11:34:53 +00001673#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001674static char posix_setgid__doc__[] =
1675"setgid(gid) -> None\n\
1676Set the current process's group id.";
1677
Barry Warsaw53699e91996-12-10 23:23:01 +00001678static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001679posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001680 PyObject *self;
1681 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001682{
1683 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001684 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001685 return NULL;
1686 if (setgid(gid) < 0)
1687 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001688 Py_INCREF(Py_None);
1689 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001690}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001691#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001692
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001693
Guido van Rossumb6775db1994-08-01 11:34:53 +00001694#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001695static char posix_waitpid__doc__[] =
1696"waitpid(pid, options) -> (pid, status)\n\
1697Wait for completion of a give child process.";
1698
Barry Warsaw53699e91996-12-10 23:23:01 +00001699static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001700posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001701 PyObject *self;
1702 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001703{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001704 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001705 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001706 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001707 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001708#ifdef NeXT
1709 pid = wait4(pid, (union wait *)&sts, options, NULL);
1710#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001711 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001712#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001713 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001714 if (pid == -1)
1715 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001716 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001717 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001718}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001719#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001721
Guido van Rossumad0ee831995-03-01 10:34:45 +00001722#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001723static char posix_wait__doc__[] =
1724"wait() -> (pid, status)\n\
1725Wait for completion of a child process.";
1726
Barry Warsaw53699e91996-12-10 23:23:01 +00001727static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001728posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001729 PyObject *self;
1730 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001731{
1732 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001733 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001734#ifdef NeXT
1735 pid = wait((union wait *)&sts);
1736#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001737 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001738#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001739 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001740 if (pid == -1)
1741 return posix_error();
1742 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001743 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001744}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001745#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001746
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001747
1748static char posix_lstat__doc__[] =
1749"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1750Like stat(path), but do not follow symbolic links.";
1751
Barry Warsaw53699e91996-12-10 23:23:01 +00001752static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001753posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001754 PyObject *self;
1755 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001756{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001757#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001758 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001759#else /* !HAVE_LSTAT */
1760 return posix_do_stat(self, args, stat);
1761#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001762}
1763
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001764
Guido van Rossumb6775db1994-08-01 11:34:53 +00001765#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001766static char posix_readlink__doc__[] =
1767"readlink(path) -> path\n\
1768Return a string representing the path to which the symbolic link points.";
1769
Barry Warsaw53699e91996-12-10 23:23:01 +00001770static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001771posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001772 PyObject *self;
1773 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001774{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001775 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001776 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001777 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001778 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001779 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001780 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001781 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001782 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001783 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001784 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001785 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001786}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001787#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001788
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001789
Guido van Rossumb6775db1994-08-01 11:34:53 +00001790#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001791static char posix_symlink__doc__[] =
1792"symlink(src, dst) -> None\n\
1793Create a symbolic link.";
1794
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001795static PyObject *
1796posix_symlink(self, args)
1797 PyObject *self;
1798 PyObject *args;
1799{
1800 return posix_2str(args, symlink);
1801}
1802#endif /* HAVE_SYMLINK */
1803
1804
1805#ifdef HAVE_TIMES
1806#ifndef HZ
1807#define HZ 60 /* Universal constant :-) */
1808#endif /* HZ */
1809
Guido van Rossumd48f2521997-12-05 22:19:34 +00001810#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1811static long
1812system_uptime()
1813{
1814 ULONG value = 0;
1815
1816 Py_BEGIN_ALLOW_THREADS
1817 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1818 Py_END_ALLOW_THREADS
1819
1820 return value;
1821}
1822
1823static PyObject *
1824posix_times(self, args)
1825 PyObject *self;
1826 PyObject *args;
1827{
1828 if (!PyArg_NoArgs(args))
1829 return NULL;
1830
1831 /* Currently Only Uptime is Provided -- Others Later */
1832 return Py_BuildValue("ddddd",
1833 (double)0 /* t.tms_utime / HZ */,
1834 (double)0 /* t.tms_stime / HZ */,
1835 (double)0 /* t.tms_cutime / HZ */,
1836 (double)0 /* t.tms_cstime / HZ */,
1837 (double)system_uptime() / 1000);
1838}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001839#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00001840static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001841posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001842 PyObject *self;
1843 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001844{
1845 struct tms t;
1846 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001847 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001848 return NULL;
1849 errno = 0;
1850 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001851 if (c == (clock_t) -1)
1852 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001853 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001854 (double)t.tms_utime / HZ,
1855 (double)t.tms_stime / HZ,
1856 (double)t.tms_cutime / HZ,
1857 (double)t.tms_cstime / HZ,
1858 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001859}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001860#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001861#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001862
1863
Guido van Rossum87755a21996-09-07 00:59:43 +00001864#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001865#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001866static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001867posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001868 PyObject *self;
1869 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001870{
1871 FILETIME create, exit, kernel, user;
1872 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001873 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001874 return NULL;
1875 hProc = GetCurrentProcess();
1876 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 return Py_BuildValue(
1878 "ddddd",
1879 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1880 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1881 (double)0,
1882 (double)0,
1883 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001884}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001885#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001886
1887#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00001888static char posix_times__doc__[] =
1889"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1890Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001891#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001892
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001893
Guido van Rossumb6775db1994-08-01 11:34:53 +00001894#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001895static char posix_setsid__doc__[] =
1896"setsid() -> None\n\
1897Call the system call setsid().";
1898
Barry Warsaw53699e91996-12-10 23:23:01 +00001899static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001900posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001901 PyObject *self;
1902 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001903{
Barry Warsaw53699e91996-12-10 23:23:01 +00001904 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001905 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001906 if (setsid() < 0)
1907 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001908 Py_INCREF(Py_None);
1909 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001910}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001911#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001912
Guido van Rossumb6775db1994-08-01 11:34:53 +00001913#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001914static char posix_setpgid__doc__[] =
1915"setpgid(pid, pgrp) -> None\n\
1916Call the system call setpgid().";
1917
Barry Warsaw53699e91996-12-10 23:23:01 +00001918static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001919posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001920 PyObject *self;
1921 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001922{
1923 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001924 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001925 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001926 if (setpgid(pid, pgrp) < 0)
1927 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001928 Py_INCREF(Py_None);
1929 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001930}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001931#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001933
Guido van Rossumb6775db1994-08-01 11:34:53 +00001934#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001935static char posix_tcgetpgrp__doc__[] =
1936"tcgetpgrp(fd) -> pgid\n\
1937Return the process group associated with the terminal given by a fd.";
1938
Barry Warsaw53699e91996-12-10 23:23:01 +00001939static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001940posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001941 PyObject *self;
1942 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001943{
1944 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001945 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001946 return NULL;
1947 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001948 if (pgid < 0)
1949 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001950 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001951}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001952#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001953
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001954
Guido van Rossumb6775db1994-08-01 11:34:53 +00001955#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001956static char posix_tcsetpgrp__doc__[] =
1957"tcsetpgrp(fd, pgid) -> None\n\
1958Set the process group associated with the terminal given by a fd.";
1959
Barry Warsaw53699e91996-12-10 23:23:01 +00001960static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001961posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001962 PyObject *self;
1963 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001964{
1965 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001966 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001967 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001968 if (tcsetpgrp(fd, pgid) < 0)
1969 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001970 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001971 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001972}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001973#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001974
Guido van Rossum687dd131993-05-17 08:34:16 +00001975/* Functions acting on file descriptors */
1976
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001977static char posix_open__doc__[] =
1978"open(filename, flag [, mode=0777]) -> fd\n\
1979Open a file (for low level IO).";
1980
Barry Warsaw53699e91996-12-10 23:23:01 +00001981static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001982posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001983 PyObject *self;
1984 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001985{
1986 char *file;
1987 int flag;
1988 int mode = 0777;
1989 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001990 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1991 return NULL;
1992
Barry Warsaw53699e91996-12-10 23:23:01 +00001993 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001994 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001995 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001996 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001997 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00001998 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001999}
2000
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002001
2002static char posix_close__doc__[] =
2003"close(fd) -> None\n\
2004Close a file descriptor (for low level IO).";
2005
Barry Warsaw53699e91996-12-10 23:23:01 +00002006static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002007posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002008 PyObject *self;
2009 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002010{
2011 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002012 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002013 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002014 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002015 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002016 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002017 if (res < 0)
2018 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002019 Py_INCREF(Py_None);
2020 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002021}
2022
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002023
2024static char posix_dup__doc__[] =
2025"dup(fd) -> fd2\n\
2026Return a duplicate of a file descriptor.";
2027
Barry Warsaw53699e91996-12-10 23:23:01 +00002028static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002029posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002030 PyObject *self;
2031 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002032{
2033 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002034 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002035 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002036 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002037 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002038 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002039 if (fd < 0)
2040 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002041 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002042}
2043
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002044
2045static char posix_dup2__doc__[] =
2046"dup2(fd, fd2) -> None\n\
2047Duplicate file descriptor.";
2048
Barry Warsaw53699e91996-12-10 23:23:01 +00002049static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002050posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002051 PyObject *self;
2052 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002053{
2054 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002055 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002056 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002057 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002058 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002059 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002060 if (res < 0)
2061 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002062 Py_INCREF(Py_None);
2063 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002064}
2065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002066
2067static char posix_lseek__doc__[] =
2068"lseek(fd, pos, how) -> newpos\n\
2069Set the current position of a file descriptor.";
2070
Barry Warsaw53699e91996-12-10 23:23:01 +00002071static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002072posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002073 PyObject *self;
2074 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002075{
2076 int fd, how;
2077 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002078 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002079 return NULL;
2080#ifdef SEEK_SET
2081 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2082 switch (how) {
2083 case 0: how = SEEK_SET; break;
2084 case 1: how = SEEK_CUR; break;
2085 case 2: how = SEEK_END; break;
2086 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002087#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00002088 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002089 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002090 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002091 if (res < 0)
2092 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002093 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00002094}
2095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002096
2097static char posix_read__doc__[] =
2098"read(fd, buffersize) -> string\n\
2099Read a file descriptor.";
2100
Barry Warsaw53699e91996-12-10 23:23:01 +00002101static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002102posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002103 PyObject *self;
2104 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002105{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002106 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002107 PyObject *buffer;
2108 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002109 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002110 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002111 if (buffer == NULL)
2112 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002113 Py_BEGIN_ALLOW_THREADS
2114 n = read(fd, PyString_AsString(buffer), size);
2115 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002116 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002117 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002118 return posix_error();
2119 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002120 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002121 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002122 return buffer;
2123}
2124
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002125
2126static char posix_write__doc__[] =
2127"write(fd, string) -> byteswritten\n\
2128Write a string to a file descriptor.";
2129
Barry Warsaw53699e91996-12-10 23:23:01 +00002130static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002131posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002132 PyObject *self;
2133 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002134{
2135 int fd, size;
2136 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002137 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002138 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002139 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002140 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002141 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002142 if (size < 0)
2143 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002144 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002145}
2146
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002147
2148static char posix_fstat__doc__[]=
2149"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2150Like stat(), but for an open file descriptor.";
2151
Barry Warsaw53699e91996-12-10 23:23:01 +00002152static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002153posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002154 PyObject *self;
2155 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002156{
2157 int fd;
2158 struct stat st;
2159 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002160 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002161 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002162 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002163 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002164 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002165 if (res != 0)
2166 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002167 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002168 (long)st.st_mode,
2169 (long)st.st_ino,
2170 (long)st.st_dev,
2171 (long)st.st_nlink,
2172 (long)st.st_uid,
2173 (long)st.st_gid,
2174 (long)st.st_size,
2175 (long)st.st_atime,
2176 (long)st.st_mtime,
2177 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00002178}
2179
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002180
2181static char posix_fdopen__doc__[] =
2182"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2183Return an open file object connected to a file descriptor.";
2184
Barry Warsaw53699e91996-12-10 23:23:01 +00002185static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002186posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002187 PyObject *self;
2188 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002189{
Barry Warsaw53699e91996-12-10 23:23:01 +00002190 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002191 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002192 char *mode = "r";
2193 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002194 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002195 PyObject *f;
2196 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002197 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002198
Barry Warsaw53699e91996-12-10 23:23:01 +00002199 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002200 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002201 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002202 if (fp == NULL)
2203 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002204 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002205 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002206 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002207 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002208}
2209
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002210
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002211#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002212static char posix_pipe__doc__[] =
2213"pipe() -> (read_end, write_end)\n\
2214Create a pipe.";
2215
Barry Warsaw53699e91996-12-10 23:23:01 +00002216static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002217posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002218 PyObject *self;
2219 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002220{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002221#if defined(PYOS_OS2)
2222 HFILE read, write;
2223 APIRET rc;
2224
2225 if (!PyArg_Parse(args, ""))
2226 return NULL;
2227
2228 Py_BEGIN_ALLOW_THREADS
2229 rc = DosCreatePipe( &read, &write, 4096);
2230 Py_END_ALLOW_THREADS
2231 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002232 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002233
2234 return Py_BuildValue("(ii)", read, write);
2235#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002236#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002237 int fds[2];
2238 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002239 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002240 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002241 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002242 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002243 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002244 if (res != 0)
2245 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002246 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002247#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002248 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002249 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002250 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002251 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002252 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002253 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002254 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002255 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002256 if (!ok)
2257 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002258 read_fd = _open_osfhandle((long)read, 0);
2259 write_fd = _open_osfhandle((long)write, 1);
2260 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002261#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002262#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002263}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002264#endif /* HAVE_PIPE */
2265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002266
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002267#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002268static char posix_mkfifo__doc__[] =
2269"mkfifo(file, [, mode=0666]) -> None\n\
2270Create a FIFO (a POSIX named pipe).";
2271
Barry Warsaw53699e91996-12-10 23:23:01 +00002272static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002273posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002274 PyObject *self;
2275 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002276{
2277 char *file;
2278 int mode = 0666;
2279 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002280 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002281 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002282 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002283 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002284 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002285 if (res < 0)
2286 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002287 Py_INCREF(Py_None);
2288 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002289}
2290#endif
2291
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002292
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002293#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002294static char posix_ftruncate__doc__[] =
2295"ftruncate(fd, length) -> None\n\
2296Truncate a file to a specified length.";
2297
Barry Warsaw53699e91996-12-10 23:23:01 +00002298static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002299posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002300 PyObject *self; /* Not used */
2301 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002302{
2303 int fd;
2304 long length;
2305 int res;
2306
Barry Warsaw53699e91996-12-10 23:23:01 +00002307 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002308 return NULL;
2309
Barry Warsaw53699e91996-12-10 23:23:01 +00002310 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002311 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002312 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002313 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002314 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002315 return NULL;
2316 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002317 Py_INCREF(Py_None);
2318 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002319}
2320#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002321
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002322#ifdef NeXT
2323#define HAVE_PUTENV
2324/* Steve Spicklemire got this putenv from NeXTAnswers */
2325static int
2326putenv(char *newval)
2327{
2328 extern char **environ;
2329
2330 static int firstTime = 1;
2331 char **ep;
2332 char *cp;
2333 int esiz;
2334 char *np;
2335
2336 if (!(np = strchr(newval, '=')))
2337 return 1;
2338 *np = '\0';
2339
2340 /* look it up */
2341 for (ep=environ ; *ep ; ep++)
2342 {
2343 /* this should always be true... */
2344 if (cp = strchr(*ep, '='))
2345 {
2346 *cp = '\0';
2347 if (!strcmp(*ep, newval))
2348 {
2349 /* got it! */
2350 *cp = '=';
2351 break;
2352 }
2353 *cp = '=';
2354 }
2355 else
2356 {
2357 *np = '=';
2358 return 1;
2359 }
2360 }
2361
2362 *np = '=';
2363 if (*ep)
2364 {
2365 /* the string was already there:
2366 just replace it with the new one */
2367 *ep = newval;
2368 return 0;
2369 }
2370
2371 /* expand environ by one */
2372 for (esiz=2, ep=environ ; *ep ; ep++)
2373 esiz++;
2374 if (firstTime)
2375 {
2376 char **epp;
2377 char **newenv;
2378 if (!(newenv = malloc(esiz * sizeof(char *))))
2379 return 1;
2380
2381 for (ep=environ, epp=newenv ; *ep ;)
2382 *epp++ = *ep++;
2383 *epp++ = newval;
2384 *epp = (char *) 0;
2385 environ = newenv;
2386 }
2387 else
2388 {
2389 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2390 return 1;
2391 environ[esiz - 2] = newval;
2392 environ[esiz - 1] = (char *) 0;
2393 firstTime = 0;
2394 }
2395
2396 return 0;
2397}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002398#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002399
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002400
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002401#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002402static char posix_putenv__doc__[] =
2403"putenv(key, value) -> None\n\
2404Change or add an environment variable.";
2405
Barry Warsaw53699e91996-12-10 23:23:01 +00002406static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002407posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002408 PyObject *self;
2409 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002410{
2411 char *s1, *s2;
2412 char *new;
2413
Barry Warsaw53699e91996-12-10 23:23:01 +00002414 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002415 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002416
2417#if defined(PYOS_OS2)
2418 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2419 APIRET rc;
2420
2421 if (strlen(s2) == 0) /* If New Value is an Empty String */
2422 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2423
2424 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2425 if (rc != NO_ERROR)
2426 return os2_error(rc);
2427
2428 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2429 APIRET rc;
2430
2431 if (strlen(s2) == 0) /* If New Value is an Empty String */
2432 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2433
2434 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2435 if (rc != NO_ERROR)
2436 return os2_error(rc);
2437 } else {
2438#endif
2439
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002440 /* XXX This leaks memory -- not easy to fix :-( */
2441 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002442 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002443 (void) sprintf(new, "%s=%s", s1, s2);
2444 if (putenv(new)) {
2445 posix_error();
2446 return NULL;
2447 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002448
2449#if defined(PYOS_OS2)
2450 }
2451#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002452 Py_INCREF(Py_None);
2453 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002454}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002455#endif /* putenv */
2456
2457#ifdef HAVE_STRERROR
2458static char posix_strerror__doc__[] =
2459"strerror(code) -> string\n\
2460Translate an error code to a message string.";
2461
2462PyObject *
2463posix_strerror(self, args)
2464 PyObject *self;
2465 PyObject *args;
2466{
2467 int code;
2468 char *message;
2469 if (!PyArg_ParseTuple(args, "i", &code))
2470 return NULL;
2471 message = strerror(code);
2472 if (message == NULL) {
2473 PyErr_SetString(PyExc_ValueError,
2474 "strerror code out of range");
2475 return NULL;
2476 }
2477 return PyString_FromString(message);
2478}
2479#endif /* strerror */
2480
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002481
Barry Warsaw53699e91996-12-10 23:23:01 +00002482static PyMethodDef posix_methods[] = {
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002483 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2484 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002485#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002486 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002487#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002488#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002489 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002490#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002491#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002492 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002493#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002494 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2495 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2496 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002497#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002498 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002499#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002500#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002502#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002503 {"rename", posix_rename, 0, posix_rename__doc__},
2504 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2505 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002506#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002507 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002508#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002509#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002510 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002511#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002512 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002513#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002514 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002515#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002516 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2517 {"remove", posix_unlink, 0, posix_remove__doc__},
2518 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002519#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002520 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002521#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002522 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002523#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002524 {"execv", posix_execv, 0, posix_execv__doc__},
2525 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002526#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002527#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002528 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002529#endif /* HAVE_FORK */
2530#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002531 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002532#endif /* HAVE_GETEGID */
2533#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002534 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002535#endif /* HAVE_GETEUID */
2536#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002537 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002538#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002539 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002540#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002541 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002542#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002543#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002544 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002545#endif /* HAVE_GETPPID */
2546#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002547 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002548#endif /* HAVE_GETUID */
2549#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002550 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002551#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002552#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002553 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002554#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002555#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002556 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002557#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002558#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002559 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002560#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002562 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002563#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002564#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002565 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002566#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002567#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002568 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002569#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002570#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002571 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002572#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002573#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002574 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002575#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002576#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002577 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002578#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002579#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002580 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002581#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002582#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002583 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002584#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002585 {"open", posix_open, 1, posix_open__doc__},
2586 {"close", posix_close, 0, posix_close__doc__},
2587 {"dup", posix_dup, 0, posix_dup__doc__},
2588 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2589 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2590 {"read", posix_read, 0, posix_read__doc__},
2591 {"write", posix_write, 0, posix_write__doc__},
2592 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2593 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002594#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002595 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002596#endif
2597#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002598 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002599#endif
2600#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002601 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002602#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002603#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002604 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002605#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002606#ifdef HAVE_STRERROR
2607 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2608#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002609 {NULL, NULL} /* Sentinel */
2610};
2611
2612
Barry Warsaw4a342091996-12-19 23:50:02 +00002613static int
2614ins(d, symbol, value)
2615 PyObject* d;
2616 char* symbol;
2617 long value;
2618{
2619 PyObject* v = PyInt_FromLong(value);
2620 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2621 return -1; /* triggers fatal error */
2622
2623 Py_DECREF(v);
2624 return 0;
2625}
2626
Guido van Rossumd48f2521997-12-05 22:19:34 +00002627#if defined(PYOS_OS2)
2628/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
2629static int insertvalues(PyObject *d)
2630{
2631 APIRET rc;
2632 ULONG values[QSV_MAX+1];
2633 PyObject *v;
2634 char *ver, tmp[10];
2635
2636 Py_BEGIN_ALLOW_THREADS
2637 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
2638 Py_END_ALLOW_THREADS
2639
2640 if (rc != NO_ERROR) {
2641 os2_error(rc);
2642 return -1;
2643 }
2644
2645 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
2646 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
2647 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
2648 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
2649 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
2650 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
2651 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
2652
2653 switch (values[QSV_VERSION_MINOR]) {
2654 case 0: ver = "2.00"; break;
2655 case 10: ver = "2.10"; break;
2656 case 11: ver = "2.11"; break;
2657 case 30: ver = "3.00"; break;
2658 case 40: ver = "4.00"; break;
2659 case 50: ver = "5.00"; break;
2660 default:
2661 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
2662 values[QSV_VERSION_MINOR]);
2663 ver = &tmp[0];
2664 }
2665
2666 /* Add Indicator of the Version of the Operating System */
2667 v = PyString_FromString(ver);
2668 if (!v || PyDict_SetItemString(d, "version", v) < 0)
2669 return -1;
2670 Py_DECREF(v);
2671
2672 /* Add Indicator of Which Drive was Used to Boot the System */
2673 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
2674 tmp[1] = ':';
2675 tmp[2] = '\0';
2676
2677 v = PyString_FromString(tmp);
2678 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
2679 return -1;
2680 Py_DECREF(v);
2681
2682 return 0;
2683}
2684#endif
2685
Barry Warsaw4a342091996-12-19 23:50:02 +00002686static int
2687all_ins(d)
2688 PyObject* d;
2689{
2690#ifdef WNOHANG
2691 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
2692#endif
2693#ifdef O_RDONLY
2694 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
2695#endif
2696#ifdef O_WRONLY
2697 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
2698#endif
2699#ifdef O_RDWR
2700 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
2701#endif
2702#ifdef O_NDELAY
2703 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
2704#endif
2705#ifdef O_NONBLOCK
2706 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
2707#endif
2708#ifdef O_APPEND
2709 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
2710#endif
2711#ifdef O_DSYNC
2712 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
2713#endif
2714#ifdef O_RSYNC
2715 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
2716#endif
2717#ifdef O_SYNC
2718 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
2719#endif
2720#ifdef O_NOCTTY
2721 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
2722#endif
2723#ifdef O_CREAT
2724 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
2725#endif
2726#ifdef O_EXCL
2727 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
2728#endif
2729#ifdef O_TRUNC
2730 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
2731#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00002732#ifdef O_BINARY
2733 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
2734#endif
2735#ifdef O_TEXT
2736 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
2737#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00002738
2739#if defined(PYOS_OS2)
2740 if (insertvalues(d)) return -1;
2741#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00002742 return 0;
2743}
2744
2745
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002746#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002747#define INITFUNC initnt
2748#define MODNAME "nt"
2749#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002750#if defined(PYOS_OS2)
2751#define INITFUNC initos2
2752#define MODNAME "os2"
2753#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002754#define INITFUNC initposix
2755#define MODNAME "posix"
2756#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002757#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002758
Guido van Rossumb6775db1994-08-01 11:34:53 +00002759void
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002760INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00002761{
Barry Warsaw53699e91996-12-10 23:23:01 +00002762 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002763
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002764 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002765 posix_methods,
2766 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002767 (PyObject *)NULL,
2768 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002769 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002770
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002771 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002772 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002773 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002774 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00002775 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002776
Barry Warsaw4a342091996-12-19 23:50:02 +00002777 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00002778 return;
2779
Barry Warsawd58d7641998-07-23 16:14:40 +00002780 Py_INCREF(PyExc_OSError);
2781 PosixError = PyExc_OSError;
2782 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002783}