blob: 8174890dd53813c59a6c5814a20d2ff0e05f17d1 [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();
Guido van Rossum617bc191998-08-06 03:23:32 +0000647 if (errno == ERROR_FILE_NOT_FOUND)
648 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000649 return posix_error();
650 }
651 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000652 if (FileData.cFileName[0] == '.' &&
653 (FileData.cFileName[1] == '\0' ||
654 FileData.cFileName[1] == '.' &&
655 FileData.cFileName[2] == '\0'))
656 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000657 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000658 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000659 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660 d = NULL;
661 break;
662 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000663 if (PyList_Append(d, v) != 0) {
664 Py_DECREF(v);
665 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000666 d = NULL;
667 break;
668 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000669 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000670 } while (FindNextFile(hFindFile, &FileData) == TRUE);
671
672 if (FindClose(hFindFile) == FALSE) {
673 errno = GetLastError();
674 return posix_error();
675 }
676
677 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000678
Guido van Rossum8d665e61996-06-26 18:22:49 +0000679#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000680#ifdef _MSC_VER /* 16-bit Windows */
681
682#ifndef MAX_PATH
683#define MAX_PATH 250
684#endif
685 char *name, *pt;
686 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000687 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000688 char namebuf[MAX_PATH+5];
689 struct _find_t ep;
690
Barry Warsaw53699e91996-12-10 23:23:01 +0000691 if (!PyArg_Parse(args, "s#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000692 return NULL;
693 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000694 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000695 return NULL;
696 }
697 strcpy(namebuf, name);
698 for (pt = namebuf; *pt; pt++)
699 if (*pt == '/')
700 *pt = '\\';
701 if (namebuf[len-1] != '\\')
702 namebuf[len++] = '\\';
703 strcpy(namebuf + len, "*.*");
704
Barry Warsaw53699e91996-12-10 23:23:01 +0000705 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000706 return NULL;
707
708 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000709 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
710 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000711 errno = ENOENT;
712 return posix_error();
713 }
714 do {
715 if (ep.name[0] == '.' &&
716 (ep.name[1] == '\0' ||
717 ep.name[1] == '.' &&
718 ep.name[2] == '\0'))
719 continue;
720 strcpy(namebuf, ep.name);
721 for (pt = namebuf; *pt; pt++)
722 if (isupper(*pt))
723 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000724 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000725 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000726 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000727 d = NULL;
728 break;
729 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000730 if (PyList_Append(d, v) != 0) {
731 Py_DECREF(v);
732 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000733 d = NULL;
734 break;
735 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000736 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000737 } while (_dos_findnext(&ep) == 0);
738
739 return d;
740
741#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000742#if defined(PYOS_OS2)
743
744#ifndef MAX_PATH
745#define MAX_PATH CCHMAXPATH
746#endif
747 char *name, *pt;
748 int len;
749 PyObject *d, *v;
750 char namebuf[MAX_PATH+5];
751 HDIR hdir = 1;
752 ULONG srchcnt = 1;
753 FILEFINDBUF3 ep;
754 APIRET rc;
755
756 if (!PyArg_Parse(args, "s#", &name, &len))
757 return NULL;
758 if (len >= MAX_PATH) {
759 PyErr_SetString(PyExc_ValueError, "path too long");
760 return NULL;
761 }
762 strcpy(namebuf, name);
763 for (pt = namebuf; *pt; pt++)
764 if (*pt == '/')
765 *pt = '\\';
766 if (namebuf[len-1] != '\\')
767 namebuf[len++] = '\\';
768 strcpy(namebuf + len, "*.*");
769
770 if ((d = PyList_New(0)) == NULL)
771 return NULL;
772
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000773 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
774 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000775 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000776 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
777 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
778 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000779
780 if (rc != NO_ERROR) {
781 errno = ENOENT;
782 return posix_error();
783 }
784
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000785 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000786 do {
787 if (ep.achName[0] == '.'
788 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000789 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000790
791 strcpy(namebuf, ep.achName);
792
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000793 /* Leave Case of Name Alone -- In Native Form */
794 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000795
796 v = PyString_FromString(namebuf);
797 if (v == NULL) {
798 Py_DECREF(d);
799 d = NULL;
800 break;
801 }
802 if (PyList_Append(d, v) != 0) {
803 Py_DECREF(v);
804 Py_DECREF(d);
805 d = NULL;
806 break;
807 }
808 Py_DECREF(v);
809 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
810 }
811
812 return d;
813#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000814
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000815 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000816 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000818 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000819 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000821 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000822 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000823 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000824 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000825 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000826 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000827 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000828 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000829 return NULL;
830 }
831 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000832 if (ep->d_name[0] == '.' &&
833 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000834 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000835 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000836 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000838 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839 d = NULL;
840 break;
841 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000842 if (PyList_Append(d, v) != 0) {
843 Py_DECREF(v);
844 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000845 d = NULL;
846 break;
847 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000848 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000849 }
850 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000851 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000852
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000853 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000854
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000855#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000856#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000857#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000858}
859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000860static char posix_mkdir__doc__[] =
861"mkdir(path [, mode=0777]) -> None\n\
862Create a directory.";
863
Barry Warsaw53699e91996-12-10 23:23:01 +0000864static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000866 PyObject *self;
867 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000869 int res;
870 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000871 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000872 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000873 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000874 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000875#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000876 res = mkdir(path);
877#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000878 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000879#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000880 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000881 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000882 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000883 Py_INCREF(Py_None);
884 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000885}
886
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000887
Guido van Rossumb6775db1994-08-01 11:34:53 +0000888#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000889static char posix_nice__doc__[] =
890"nice(inc) -> new_priority\n\
891Decrease the priority of process and return new priority.";
892
Barry Warsaw53699e91996-12-10 23:23:01 +0000893static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000894posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000895 PyObject *self;
896 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000897{
898 int increment, value;
899
Barry Warsaw53699e91996-12-10 23:23:01 +0000900 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000901 return NULL;
902 value = nice(increment);
903 if (value == -1)
904 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000905 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000906}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000907#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000908
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000909
910static char posix_rename__doc__[] =
911"rename(old, new) -> None\n\
912Rename a file or directory.";
913
Barry Warsaw53699e91996-12-10 23:23:01 +0000914static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000915posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000916 PyObject *self;
917 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000918{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000919 return posix_2str(args, rename);
920}
921
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000922
923static char posix_rmdir__doc__[] =
924"rmdir(path) -> None\n\
925Remove a directory.";
926
Barry Warsaw53699e91996-12-10 23:23:01 +0000927static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000928posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000929 PyObject *self;
930 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000931{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000932 return posix_1str(args, rmdir);
933}
934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000935
936static char posix_stat__doc__[] =
937"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
938Perform a stat system call on the given path.";
939
Barry Warsaw53699e91996-12-10 23:23:01 +0000940static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000941posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000942 PyObject *self;
943 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000944{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000945 return posix_do_stat(self, args, stat);
946}
947
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000948
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000949#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000950static char posix_system__doc__[] =
951"system(command) -> exit_status\n\
952Execute the command (a string) in a subshell.";
953
Barry Warsaw53699e91996-12-10 23:23:01 +0000954static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000956 PyObject *self;
957 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000958{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000959 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000960 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000961 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000962 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000963 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000964 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +0000965 Py_END_ALLOW_THREADS
966 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000968#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000969
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000970
971static char posix_umask__doc__[] =
972"umask(new_mask) -> old_mask\n\
973Set the current numeric umask and return the previous umask.";
974
Barry Warsaw53699e91996-12-10 23:23:01 +0000975static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000976posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000977 PyObject *self;
978 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000979{
980 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +0000981 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000982 return NULL;
983 i = umask(i);
984 if (i < 0)
985 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000986 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000987}
988
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000989
990static char posix_unlink__doc__[] =
991"unlink(path) -> None\n\
992Remove a file (same as remove(path)).";
993
994static char posix_remove__doc__[] =
995"remove(path) -> None\n\
996Remove a file (same as unlink(path)).";
997
Barry Warsaw53699e91996-12-10 23:23:01 +0000998static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000999posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001000 PyObject *self;
1001 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001002{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001003 return posix_1str(args, unlink);
1004}
1005
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001006
Guido van Rossumb6775db1994-08-01 11:34:53 +00001007#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001008static char posix_uname__doc__[] =
1009"uname() -> (sysname, nodename, release, version, machine)\n\
1010Return a tuple identifying the current operating system.";
1011
Barry Warsaw53699e91996-12-10 23:23:01 +00001012static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001013posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001014 PyObject *self;
1015 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001016{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001017 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001018 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001019 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001020 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001021 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001022 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001023 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001024 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001025 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001026 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001027 u.sysname,
1028 u.nodename,
1029 u.release,
1030 u.version,
1031 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001032}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001033#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001035
1036static char posix_utime__doc__[] =
1037"utime(path, (atime, utime)) -> None\n\
1038Set the access and modified time of the file to the given values.";
1039
Barry Warsaw53699e91996-12-10 23:23:01 +00001040static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001041posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001042 PyObject *self;
1043 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001044{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001045 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001046 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001047 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001048
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001049/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001050#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001051 struct utimbuf buf;
1052#define ATIME buf.actime
1053#define MTIME buf.modtime
1054#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001055#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001056 time_t buf[2];
1057#define ATIME buf[0]
1058#define MTIME buf[1]
1059#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001060#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001061
Barry Warsaw53699e91996-12-10 23:23:01 +00001062 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001063 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001064 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001065 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001066 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001067 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001068 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001069 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001070 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001071 Py_INCREF(Py_None);
1072 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001073#undef UTIME_ARG
1074#undef ATIME
1075#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001076}
1077
Guido van Rossum85e3b011991-06-03 12:42:10 +00001078
Guido van Rossum3b066191991-06-04 19:40:25 +00001079/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001080
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001081static char posix__exit__doc__[] =
1082"_exit(status)\n\
1083Exit to the system with specified status, without normal exit processing.";
1084
Barry Warsaw53699e91996-12-10 23:23:01 +00001085static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001086posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001087 PyObject *self;
1088 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001089{
1090 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001091 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001092 return NULL;
1093 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001094 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001095}
1096
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001097
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001098#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001099static char posix_execv__doc__[] =
1100"execv(path, args)\n\
1101Execute an executable path with arguments, replacing current process.\n\
1102\n\
1103 path: path of executable file\n\
1104 args: tuple or list of strings";
1105
Barry Warsaw53699e91996-12-10 23:23:01 +00001106static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001107posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001108 PyObject *self;
1109 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001110{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001111 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001112 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001113 char **argvlist;
1114 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001115 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001116
Guido van Rossum89b33251993-10-22 14:26:06 +00001117 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001118 argv is a list or tuple of strings. */
1119
Barry Warsaw53699e91996-12-10 23:23:01 +00001120 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001121 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001122 if (PyList_Check(argv)) {
1123 argc = PyList_Size(argv);
1124 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001125 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001126 else if (PyTuple_Check(argv)) {
1127 argc = PyTuple_Size(argv);
1128 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001129 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001130 else {
1131 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001132 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001133 return NULL;
1134 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001135
Barry Warsaw53699e91996-12-10 23:23:01 +00001136 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001137 if (argvlist == NULL)
1138 return NULL;
1139 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001140 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1141 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001142 goto badarg;
1143 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001144 }
1145 argvlist[argc] = NULL;
1146
Guido van Rossumb6775db1994-08-01 11:34:53 +00001147#ifdef BAD_EXEC_PROTOTYPES
1148 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001149#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001150 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001151#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001152
Guido van Rossum85e3b011991-06-03 12:42:10 +00001153 /* If we get here it's definitely an error */
1154
Barry Warsaw53699e91996-12-10 23:23:01 +00001155 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001156 return posix_error();
1157}
1158
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001159
1160static char posix_execve__doc__[] =
1161"execve(path, args, env)\n\
1162Execute a path with arguments and environment, replacing current process.\n\
1163\n\
1164 path: path of executable file\n\
1165 args: tuple or list of arguments\n\
1166 env: dictonary of strings mapping to strings";
1167
Barry Warsaw53699e91996-12-10 23:23:01 +00001168static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001169posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001170 PyObject *self;
1171 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001172{
1173 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001174 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001175 char **argvlist;
1176 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001177 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001178 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001179 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001180
1181 /* execve has three arguments: (path, argv, env), where
1182 argv is a list or tuple of strings and env is a dictionary
1183 like posix.environ. */
1184
Barry Warsaw53699e91996-12-10 23:23:01 +00001185 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001186 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001187 if (PyList_Check(argv)) {
1188 argc = PyList_Size(argv);
1189 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001190 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001191 else if (PyTuple_Check(argv)) {
1192 argc = PyTuple_Size(argv);
1193 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001194 }
1195 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001196 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001197 return NULL;
1198 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001199 if (!PyMapping_Check(env)) {
1200 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001201 return NULL;
1202 }
1203
Barry Warsaw53699e91996-12-10 23:23:01 +00001204 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001205 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001206 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001207 return NULL;
1208 }
1209 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001210 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001211 "s;argv must be list of strings",
1212 &argvlist[i]))
1213 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001214 goto fail_1;
1215 }
1216 }
1217 argvlist[argc] = NULL;
1218
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001219 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001220 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001221 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001222 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001223 goto fail_1;
1224 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001225 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001226 keys = PyMapping_Keys(env);
1227 vals = PyMapping_Values(env);
1228 if (!keys || !vals)
1229 goto fail_2;
1230
1231 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001232 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001233
1234 key = PyList_GetItem(keys, pos);
1235 val = PyList_GetItem(vals, pos);
1236 if (!key || !val)
1237 goto fail_2;
1238
Barry Warsaw53699e91996-12-10 23:23:01 +00001239 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001240 !PyArg_Parse(val, "s;non-string value in env", &v))
1241 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001242 goto fail_2;
1243 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001244
1245#if defined(PYOS_OS2)
1246 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1247 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1248#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001249 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001250 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001251 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001252 goto fail_2;
1253 }
1254 sprintf(p, "%s=%s", k, v);
1255 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001256#if defined(PYOS_OS2)
1257 }
1258#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001259 }
1260 envlist[envc] = 0;
1261
Guido van Rossumb6775db1994-08-01 11:34:53 +00001262
1263#ifdef BAD_EXEC_PROTOTYPES
1264 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001265#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001266 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001267#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001268
1269 /* If we get here it's definitely an error */
1270
1271 (void) posix_error();
1272
1273 fail_2:
1274 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001275 PyMem_DEL(envlist[envc]);
1276 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001277 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001278 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001279 Py_XDECREF(vals);
1280 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001281 return NULL;
1282}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001283#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001285
Guido van Rossumad0ee831995-03-01 10:34:45 +00001286#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001287static char posix_fork__doc__[] =
1288"fork() -> pid\n\
1289Fork a child process.\n\
1290\n\
1291Return 0 to child process and PID of child to parent process.";
1292
Barry Warsaw53699e91996-12-10 23:23:01 +00001293static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001294posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001295 PyObject *self;
1296 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001297{
1298 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001299 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001300 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001301 pid = fork();
1302 if (pid == -1)
1303 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001304 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001306}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001307#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001308
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001309
Guido van Rossumad0ee831995-03-01 10:34:45 +00001310#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001311static char posix_getegid__doc__[] =
1312"getegid() -> egid\n\
1313Return the current process's effective group id.";
1314
Barry Warsaw53699e91996-12-10 23:23:01 +00001315static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001316posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001317 PyObject *self;
1318 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001319{
Barry Warsaw53699e91996-12-10 23:23:01 +00001320 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001321 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001322 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001323}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001324#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001325
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001326
Guido van Rossumad0ee831995-03-01 10:34:45 +00001327#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001328static char posix_geteuid__doc__[] =
1329"geteuid() -> euid\n\
1330Return the current process's effective user id.";
1331
Barry Warsaw53699e91996-12-10 23:23:01 +00001332static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001333posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001334 PyObject *self;
1335 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001336{
Barry Warsaw53699e91996-12-10 23:23:01 +00001337 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001338 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001339 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001340}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001341#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001342
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001343
Guido van Rossumad0ee831995-03-01 10:34:45 +00001344#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001345static char posix_getgid__doc__[] =
1346"getgid() -> gid\n\
1347Return the current process's group id.";
1348
Barry Warsaw53699e91996-12-10 23:23:01 +00001349static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001350posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001351 PyObject *self;
1352 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001353{
Barry Warsaw53699e91996-12-10 23:23:01 +00001354 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001355 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001356 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001357}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001358#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001359
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001360
1361static char posix_getpid__doc__[] =
1362"getpid() -> pid\n\
1363Return the current process id";
1364
Barry Warsaw53699e91996-12-10 23:23:01 +00001365static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001366posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001367 PyObject *self;
1368 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001369{
Barry Warsaw53699e91996-12-10 23:23:01 +00001370 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001371 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001372 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001373}
1374
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001375
Guido van Rossumb6775db1994-08-01 11:34:53 +00001376#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001377static char posix_getpgrp__doc__[] =
1378"getpgrp() -> pgrp\n\
1379Return the current process group id.";
1380
Barry Warsaw53699e91996-12-10 23:23:01 +00001381static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001382posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001383 PyObject *self;
1384 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001385{
Barry Warsaw53699e91996-12-10 23:23:01 +00001386 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001387 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001388#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001389 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001390#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001391 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001392#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001393}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001394#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001395
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001396
Guido van Rossumb6775db1994-08-01 11:34:53 +00001397#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001398static char posix_setpgrp__doc__[] =
1399"setpgrp() -> None\n\
1400Make this process a session leader.";
1401
Barry Warsaw53699e91996-12-10 23:23:01 +00001402static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001403posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001404 PyObject *self;
1405 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001406{
Barry Warsaw53699e91996-12-10 23:23:01 +00001407 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001408 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001409#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001410 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001411#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001412 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001413#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001414 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001415 Py_INCREF(Py_None);
1416 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001417}
1418
Guido van Rossumb6775db1994-08-01 11:34:53 +00001419#endif /* HAVE_SETPGRP */
1420
Guido van Rossumad0ee831995-03-01 10:34:45 +00001421#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001422static char posix_getppid__doc__[] =
1423"getppid() -> ppid\n\
1424Return the parent's process id.";
1425
Barry Warsaw53699e91996-12-10 23:23:01 +00001426static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001427posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001428 PyObject *self;
1429 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001430{
Barry Warsaw53699e91996-12-10 23:23:01 +00001431 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001432 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001433 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001434}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001435#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001436
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001437
Guido van Rossumad0ee831995-03-01 10:34:45 +00001438#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001439static char posix_getuid__doc__[] =
1440"getuid() -> uid\n\
1441Return the current process's user id.";
1442
Barry Warsaw53699e91996-12-10 23:23:01 +00001443static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001444posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001445 PyObject *self;
1446 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001447{
Barry Warsaw53699e91996-12-10 23:23:01 +00001448 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001449 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001450 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001451}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001452#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001453
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001454
Guido van Rossumad0ee831995-03-01 10:34:45 +00001455#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001456static char posix_kill__doc__[] =
1457"kill(pid, sig) -> None\n\
1458Kill a process with a signal.";
1459
Barry Warsaw53699e91996-12-10 23:23:01 +00001460static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001461posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001462 PyObject *self;
1463 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001464{
1465 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001466 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001467 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001468#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001469 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1470 APIRET rc;
1471 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001472 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001473
1474 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1475 APIRET rc;
1476 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001477 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001478
1479 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001480 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001481#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001482 if (kill(pid, sig) == -1)
1483 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001484#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001485 Py_INCREF(Py_None);
1486 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001487}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001488#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001489
Guido van Rossumc0125471996-06-28 18:55:32 +00001490#ifdef HAVE_PLOCK
1491
1492#ifdef HAVE_SYS_LOCK_H
1493#include <sys/lock.h>
1494#endif
1495
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001496static char posix_plock__doc__[] =
1497"plock(op) -> None\n\
1498Lock program segments into memory.";
1499
Barry Warsaw53699e91996-12-10 23:23:01 +00001500static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001501posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001502 PyObject *self;
1503 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001504{
1505 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001506 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001507 return NULL;
1508 if (plock(op) == -1)
1509 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001510 Py_INCREF(Py_None);
1511 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001512}
1513#endif
1514
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001515
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001516#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001517static char posix_popen__doc__[] =
1518"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1519Open a pipe to/from a command returning a file object.";
1520
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001521#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001522static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001523async_system(const char *command)
1524{
1525 char *p, errormsg[256], args[1024];
1526 RESULTCODES rcodes;
1527 APIRET rc;
1528 char *shell = getenv("COMSPEC");
1529 if (!shell)
1530 shell = "cmd";
1531
1532 strcpy(args, shell);
1533 p = &args[ strlen(args)+1 ];
1534 strcpy(p, "/c ");
1535 strcat(p, command);
1536 p += strlen(p) + 1;
1537 *p = '\0';
1538
1539 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001540 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001541 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001542 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001543 &rcodes, shell);
1544 return rc;
1545}
1546
Guido van Rossumd48f2521997-12-05 22:19:34 +00001547static FILE *
1548popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001549{
1550 HFILE rhan, whan;
1551 FILE *retfd = NULL;
1552 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1553
Guido van Rossumd48f2521997-12-05 22:19:34 +00001554 if (rc != NO_ERROR) {
1555 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001556 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001557 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001558
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001559 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1560 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001561
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001562 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1563 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001564
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001565 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1566 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001567
1568 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001569 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001570 }
1571
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001572 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1573 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001574
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001575 close(oldfd); /* And Close Saved STDOUT Handle */
1576 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001577
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001578 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1579 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001580
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001581 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1582 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001583
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001584 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1585 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001586
1587 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001588 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001589 }
1590
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001591 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1592 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001593
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001594 close(oldfd); /* And Close Saved STDIN Handle */
1595 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001596
Guido van Rossumd48f2521997-12-05 22:19:34 +00001597 } else {
1598 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001599 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001600 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001601}
1602
1603static PyObject *
1604posix_popen(self, args)
1605 PyObject *self;
1606 PyObject *args;
1607{
1608 char *name;
1609 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001610 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001611 FILE *fp;
1612 PyObject *f;
1613 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1614 return NULL;
1615 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001616 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001617 Py_END_ALLOW_THREADS
1618 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001619 return os2_error(err);
1620
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001621 f = PyFile_FromFile(fp, name, mode, fclose);
1622 if (f != NULL)
1623 PyFile_SetBufSize(f, bufsize);
1624 return f;
1625}
1626
1627#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001628static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001629posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001630 PyObject *self;
1631 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001632{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001633 char *name;
1634 char *mode = "r";
1635 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001636 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001637 PyObject *f;
1638 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001641 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001642 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001643 if (fp == NULL)
1644 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001645 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001646 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001647 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001648 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001649}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001650#endif
1651
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001652#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001653
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001654
Guido van Rossumb6775db1994-08-01 11:34:53 +00001655#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001656static char posix_setuid__doc__[] =
1657"setuid(uid) -> None\n\
1658Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001659static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001660posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001661 PyObject *self;
1662 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001663{
1664 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001665 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001666 return NULL;
1667 if (setuid(uid) < 0)
1668 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001669 Py_INCREF(Py_None);
1670 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001671}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001672#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001673
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001674
Guido van Rossumb6775db1994-08-01 11:34:53 +00001675#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001676static char posix_setgid__doc__[] =
1677"setgid(gid) -> None\n\
1678Set the current process's group id.";
1679
Barry Warsaw53699e91996-12-10 23:23:01 +00001680static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001681posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001682 PyObject *self;
1683 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001684{
1685 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001686 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001687 return NULL;
1688 if (setgid(gid) < 0)
1689 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001690 Py_INCREF(Py_None);
1691 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001692}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001693#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001694
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001695
Guido van Rossumb6775db1994-08-01 11:34:53 +00001696#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001697static char posix_waitpid__doc__[] =
1698"waitpid(pid, options) -> (pid, status)\n\
1699Wait for completion of a give child process.";
1700
Barry Warsaw53699e91996-12-10 23:23:01 +00001701static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001702posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001703 PyObject *self;
1704 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001705{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001706 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001707 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001708 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001709 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001710#ifdef NeXT
1711 pid = wait4(pid, (union wait *)&sts, options, NULL);
1712#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001713 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001714#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001715 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001716 if (pid == -1)
1717 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001718 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001719 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001720}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001721#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001722
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001723
Guido van Rossumad0ee831995-03-01 10:34:45 +00001724#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001725static char posix_wait__doc__[] =
1726"wait() -> (pid, status)\n\
1727Wait for completion of a child process.";
1728
Barry Warsaw53699e91996-12-10 23:23:01 +00001729static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001730posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001731 PyObject *self;
1732 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001733{
1734 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001735 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001736#ifdef NeXT
1737 pid = wait((union wait *)&sts);
1738#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001739 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001740#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001741 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001742 if (pid == -1)
1743 return posix_error();
1744 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001745 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001746}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001747#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001748
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001749
1750static char posix_lstat__doc__[] =
1751"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1752Like stat(path), but do not follow symbolic links.";
1753
Barry Warsaw53699e91996-12-10 23:23:01 +00001754static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001755posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001756 PyObject *self;
1757 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001758{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001759#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001760 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001761#else /* !HAVE_LSTAT */
1762 return posix_do_stat(self, args, stat);
1763#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001764}
1765
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001766
Guido van Rossumb6775db1994-08-01 11:34:53 +00001767#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001768static char posix_readlink__doc__[] =
1769"readlink(path) -> path\n\
1770Return a string representing the path to which the symbolic link points.";
1771
Barry Warsaw53699e91996-12-10 23:23:01 +00001772static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001773posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001774 PyObject *self;
1775 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001776{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001777 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001778 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001779 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001780 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001781 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001782 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001783 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001784 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001785 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001786 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001787 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001788}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001789#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001791
Guido van Rossumb6775db1994-08-01 11:34:53 +00001792#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001793static char posix_symlink__doc__[] =
1794"symlink(src, dst) -> None\n\
1795Create a symbolic link.";
1796
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001797static PyObject *
1798posix_symlink(self, args)
1799 PyObject *self;
1800 PyObject *args;
1801{
1802 return posix_2str(args, symlink);
1803}
1804#endif /* HAVE_SYMLINK */
1805
1806
1807#ifdef HAVE_TIMES
1808#ifndef HZ
1809#define HZ 60 /* Universal constant :-) */
1810#endif /* HZ */
1811
Guido van Rossumd48f2521997-12-05 22:19:34 +00001812#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1813static long
1814system_uptime()
1815{
1816 ULONG value = 0;
1817
1818 Py_BEGIN_ALLOW_THREADS
1819 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1820 Py_END_ALLOW_THREADS
1821
1822 return value;
1823}
1824
1825static PyObject *
1826posix_times(self, args)
1827 PyObject *self;
1828 PyObject *args;
1829{
1830 if (!PyArg_NoArgs(args))
1831 return NULL;
1832
1833 /* Currently Only Uptime is Provided -- Others Later */
1834 return Py_BuildValue("ddddd",
1835 (double)0 /* t.tms_utime / HZ */,
1836 (double)0 /* t.tms_stime / HZ */,
1837 (double)0 /* t.tms_cutime / HZ */,
1838 (double)0 /* t.tms_cstime / HZ */,
1839 (double)system_uptime() / 1000);
1840}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001841#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00001842static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001843posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001844 PyObject *self;
1845 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001846{
1847 struct tms t;
1848 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001849 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001850 return NULL;
1851 errno = 0;
1852 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001853 if (c == (clock_t) -1)
1854 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001855 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001856 (double)t.tms_utime / HZ,
1857 (double)t.tms_stime / HZ,
1858 (double)t.tms_cutime / HZ,
1859 (double)t.tms_cstime / HZ,
1860 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001861}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001862#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001863#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001864
1865
Guido van Rossum87755a21996-09-07 00:59:43 +00001866#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001867#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001868static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001869posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001870 PyObject *self;
1871 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001872{
1873 FILETIME create, exit, kernel, user;
1874 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001875 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001876 return NULL;
1877 hProc = GetCurrentProcess();
1878 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001879 return Py_BuildValue(
1880 "ddddd",
1881 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1882 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1883 (double)0,
1884 (double)0,
1885 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001886}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001887#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001888
1889#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00001890static char posix_times__doc__[] =
1891"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1892Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001893#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001894
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001895
Guido van Rossumb6775db1994-08-01 11:34:53 +00001896#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001897static char posix_setsid__doc__[] =
1898"setsid() -> None\n\
1899Call the system call setsid().";
1900
Barry Warsaw53699e91996-12-10 23:23:01 +00001901static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001902posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001903 PyObject *self;
1904 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001905{
Barry Warsaw53699e91996-12-10 23:23:01 +00001906 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001907 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001908 if (setsid() < 0)
1909 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001910 Py_INCREF(Py_None);
1911 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001912}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001913#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001914
Guido van Rossumb6775db1994-08-01 11:34:53 +00001915#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001916static char posix_setpgid__doc__[] =
1917"setpgid(pid, pgrp) -> None\n\
1918Call the system call setpgid().";
1919
Barry Warsaw53699e91996-12-10 23:23:01 +00001920static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001921posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001922 PyObject *self;
1923 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001924{
1925 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001926 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001927 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001928 if (setpgid(pid, pgrp) < 0)
1929 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001930 Py_INCREF(Py_None);
1931 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001932}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001933#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001935
Guido van Rossumb6775db1994-08-01 11:34:53 +00001936#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001937static char posix_tcgetpgrp__doc__[] =
1938"tcgetpgrp(fd) -> pgid\n\
1939Return the process group associated with the terminal given by a fd.";
1940
Barry Warsaw53699e91996-12-10 23:23:01 +00001941static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001942posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001943 PyObject *self;
1944 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001945{
1946 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001947 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001948 return NULL;
1949 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001950 if (pgid < 0)
1951 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001952 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001953}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001954#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001956
Guido van Rossumb6775db1994-08-01 11:34:53 +00001957#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001958static char posix_tcsetpgrp__doc__[] =
1959"tcsetpgrp(fd, pgid) -> None\n\
1960Set the process group associated with the terminal given by a fd.";
1961
Barry Warsaw53699e91996-12-10 23:23:01 +00001962static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001963posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001964 PyObject *self;
1965 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001966{
1967 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001968 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001969 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001970 if (tcsetpgrp(fd, pgid) < 0)
1971 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001972 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001973 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001974}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001975#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001976
Guido van Rossum687dd131993-05-17 08:34:16 +00001977/* Functions acting on file descriptors */
1978
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979static char posix_open__doc__[] =
1980"open(filename, flag [, mode=0777]) -> fd\n\
1981Open a file (for low level IO).";
1982
Barry Warsaw53699e91996-12-10 23:23:01 +00001983static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001984posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001985 PyObject *self;
1986 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001987{
1988 char *file;
1989 int flag;
1990 int mode = 0777;
1991 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001992 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1993 return NULL;
1994
Barry Warsaw53699e91996-12-10 23:23:01 +00001995 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001996 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001997 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001998 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001999 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002000 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002001}
2002
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002003
2004static char posix_close__doc__[] =
2005"close(fd) -> None\n\
2006Close a file descriptor (for low level IO).";
2007
Barry Warsaw53699e91996-12-10 23:23:01 +00002008static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002009posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002010 PyObject *self;
2011 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002012{
2013 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002014 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002015 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002016 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002017 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002018 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002019 if (res < 0)
2020 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 Py_INCREF(Py_None);
2022 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002023}
2024
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002025
2026static char posix_dup__doc__[] =
2027"dup(fd) -> fd2\n\
2028Return a duplicate of a file descriptor.";
2029
Barry Warsaw53699e91996-12-10 23:23:01 +00002030static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002031posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002032 PyObject *self;
2033 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002034{
2035 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002036 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002037 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002038 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002039 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002040 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002041 if (fd < 0)
2042 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002043 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002044}
2045
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002046
2047static char posix_dup2__doc__[] =
2048"dup2(fd, fd2) -> None\n\
2049Duplicate file descriptor.";
2050
Barry Warsaw53699e91996-12-10 23:23:01 +00002051static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002052posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002053 PyObject *self;
2054 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002055{
2056 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002057 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002058 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002059 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002060 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002061 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002062 if (res < 0)
2063 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002064 Py_INCREF(Py_None);
2065 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002066}
2067
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002068
2069static char posix_lseek__doc__[] =
2070"lseek(fd, pos, how) -> newpos\n\
2071Set the current position of a file descriptor.";
2072
Barry Warsaw53699e91996-12-10 23:23:01 +00002073static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002074posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 PyObject *self;
2076 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002077{
2078 int fd, how;
2079 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002080 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002081 return NULL;
2082#ifdef SEEK_SET
2083 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2084 switch (how) {
2085 case 0: how = SEEK_SET; break;
2086 case 1: how = SEEK_CUR; break;
2087 case 2: how = SEEK_END; break;
2088 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002089#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00002090 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002091 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002092 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002093 if (res < 0)
2094 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002095 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00002096}
2097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098
2099static char posix_read__doc__[] =
2100"read(fd, buffersize) -> string\n\
2101Read a file descriptor.";
2102
Barry Warsaw53699e91996-12-10 23:23:01 +00002103static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002104posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002105 PyObject *self;
2106 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002107{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002108 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002109 PyObject *buffer;
2110 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002111 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002112 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002113 if (buffer == NULL)
2114 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002115 Py_BEGIN_ALLOW_THREADS
2116 n = read(fd, PyString_AsString(buffer), size);
2117 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002118 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002119 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002120 return posix_error();
2121 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002122 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002123 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002124 return buffer;
2125}
2126
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002127
2128static char posix_write__doc__[] =
2129"write(fd, string) -> byteswritten\n\
2130Write a string to a file descriptor.";
2131
Barry Warsaw53699e91996-12-10 23:23:01 +00002132static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002133posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002134 PyObject *self;
2135 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002136{
2137 int fd, size;
2138 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002139 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002140 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002141 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002142 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002143 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002144 if (size < 0)
2145 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002146 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002147}
2148
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002149
2150static char posix_fstat__doc__[]=
2151"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2152Like stat(), but for an open file descriptor.";
2153
Barry Warsaw53699e91996-12-10 23:23:01 +00002154static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002155posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002156 PyObject *self;
2157 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002158{
2159 int fd;
2160 struct stat st;
2161 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002162 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002163 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002164 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002165 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002166 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002167 if (res != 0)
2168 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002169 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002170 (long)st.st_mode,
2171 (long)st.st_ino,
2172 (long)st.st_dev,
2173 (long)st.st_nlink,
2174 (long)st.st_uid,
2175 (long)st.st_gid,
2176 (long)st.st_size,
2177 (long)st.st_atime,
2178 (long)st.st_mtime,
2179 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00002180}
2181
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002182
2183static char posix_fdopen__doc__[] =
2184"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2185Return an open file object connected to a file descriptor.";
2186
Barry Warsaw53699e91996-12-10 23:23:01 +00002187static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002188posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002189 PyObject *self;
2190 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002191{
Barry Warsaw53699e91996-12-10 23:23:01 +00002192 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002193 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002194 char *mode = "r";
2195 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002196 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002197 PyObject *f;
2198 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002199 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002200
Barry Warsaw53699e91996-12-10 23:23:01 +00002201 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002202 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002203 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002204 if (fp == NULL)
2205 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002206 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002207 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002208 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002209 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002210}
2211
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002212
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002213#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002214static char posix_pipe__doc__[] =
2215"pipe() -> (read_end, write_end)\n\
2216Create a pipe.";
2217
Barry Warsaw53699e91996-12-10 23:23:01 +00002218static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002219posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002220 PyObject *self;
2221 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002222{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002223#if defined(PYOS_OS2)
2224 HFILE read, write;
2225 APIRET rc;
2226
2227 if (!PyArg_Parse(args, ""))
2228 return NULL;
2229
2230 Py_BEGIN_ALLOW_THREADS
2231 rc = DosCreatePipe( &read, &write, 4096);
2232 Py_END_ALLOW_THREADS
2233 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002234 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002235
2236 return Py_BuildValue("(ii)", read, write);
2237#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002238#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002239 int fds[2];
2240 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002241 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002242 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002243 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002244 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002245 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002246 if (res != 0)
2247 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002248 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002249#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002250 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002251 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002252 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002253 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002254 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002255 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002256 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002257 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002258 if (!ok)
2259 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002260 read_fd = _open_osfhandle((long)read, 0);
2261 write_fd = _open_osfhandle((long)write, 1);
2262 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002263#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002264#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002265}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002266#endif /* HAVE_PIPE */
2267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002268
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002269#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002270static char posix_mkfifo__doc__[] =
2271"mkfifo(file, [, mode=0666]) -> None\n\
2272Create a FIFO (a POSIX named pipe).";
2273
Barry Warsaw53699e91996-12-10 23:23:01 +00002274static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002275posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002276 PyObject *self;
2277 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002278{
2279 char *file;
2280 int mode = 0666;
2281 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002282 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002283 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002284 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002285 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002286 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002287 if (res < 0)
2288 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002289 Py_INCREF(Py_None);
2290 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002291}
2292#endif
2293
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002294
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002295#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002296static char posix_ftruncate__doc__[] =
2297"ftruncate(fd, length) -> None\n\
2298Truncate a file to a specified length.";
2299
Barry Warsaw53699e91996-12-10 23:23:01 +00002300static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002301posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002302 PyObject *self; /* Not used */
2303 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002304{
2305 int fd;
2306 long length;
2307 int res;
2308
Barry Warsaw53699e91996-12-10 23:23:01 +00002309 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002310 return NULL;
2311
Barry Warsaw53699e91996-12-10 23:23:01 +00002312 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002313 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002314 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002315 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002316 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002317 return NULL;
2318 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002319 Py_INCREF(Py_None);
2320 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002321}
2322#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002323
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002324#ifdef NeXT
2325#define HAVE_PUTENV
2326/* Steve Spicklemire got this putenv from NeXTAnswers */
2327static int
2328putenv(char *newval)
2329{
2330 extern char **environ;
2331
2332 static int firstTime = 1;
2333 char **ep;
2334 char *cp;
2335 int esiz;
2336 char *np;
2337
2338 if (!(np = strchr(newval, '=')))
2339 return 1;
2340 *np = '\0';
2341
2342 /* look it up */
2343 for (ep=environ ; *ep ; ep++)
2344 {
2345 /* this should always be true... */
2346 if (cp = strchr(*ep, '='))
2347 {
2348 *cp = '\0';
2349 if (!strcmp(*ep, newval))
2350 {
2351 /* got it! */
2352 *cp = '=';
2353 break;
2354 }
2355 *cp = '=';
2356 }
2357 else
2358 {
2359 *np = '=';
2360 return 1;
2361 }
2362 }
2363
2364 *np = '=';
2365 if (*ep)
2366 {
2367 /* the string was already there:
2368 just replace it with the new one */
2369 *ep = newval;
2370 return 0;
2371 }
2372
2373 /* expand environ by one */
2374 for (esiz=2, ep=environ ; *ep ; ep++)
2375 esiz++;
2376 if (firstTime)
2377 {
2378 char **epp;
2379 char **newenv;
2380 if (!(newenv = malloc(esiz * sizeof(char *))))
2381 return 1;
2382
2383 for (ep=environ, epp=newenv ; *ep ;)
2384 *epp++ = *ep++;
2385 *epp++ = newval;
2386 *epp = (char *) 0;
2387 environ = newenv;
2388 }
2389 else
2390 {
2391 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2392 return 1;
2393 environ[esiz - 2] = newval;
2394 environ[esiz - 1] = (char *) 0;
2395 firstTime = 0;
2396 }
2397
2398 return 0;
2399}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002400#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002401
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002402
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002403#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002404static char posix_putenv__doc__[] =
2405"putenv(key, value) -> None\n\
2406Change or add an environment variable.";
2407
Guido van Rossumbcc20741998-08-04 22:53:56 +00002408#ifdef __BEOS__
2409/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2410int putenv( const char *str );
2411#endif
2412
Barry Warsaw53699e91996-12-10 23:23:01 +00002413static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002414posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002415 PyObject *self;
2416 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002417{
2418 char *s1, *s2;
2419 char *new;
2420
Barry Warsaw53699e91996-12-10 23:23:01 +00002421 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002422 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002423
2424#if defined(PYOS_OS2)
2425 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2426 APIRET rc;
2427
2428 if (strlen(s2) == 0) /* If New Value is an Empty String */
2429 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2430
2431 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2432 if (rc != NO_ERROR)
2433 return os2_error(rc);
2434
2435 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2436 APIRET rc;
2437
2438 if (strlen(s2) == 0) /* If New Value is an Empty String */
2439 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2440
2441 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2442 if (rc != NO_ERROR)
2443 return os2_error(rc);
2444 } else {
2445#endif
2446
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002447 /* XXX This leaks memory -- not easy to fix :-( */
2448 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002449 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002450 (void) sprintf(new, "%s=%s", s1, s2);
2451 if (putenv(new)) {
2452 posix_error();
2453 return NULL;
2454 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002455
2456#if defined(PYOS_OS2)
2457 }
2458#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002459 Py_INCREF(Py_None);
2460 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002461}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002462#endif /* putenv */
2463
2464#ifdef HAVE_STRERROR
2465static char posix_strerror__doc__[] =
2466"strerror(code) -> string\n\
2467Translate an error code to a message string.";
2468
2469PyObject *
2470posix_strerror(self, args)
2471 PyObject *self;
2472 PyObject *args;
2473{
2474 int code;
2475 char *message;
2476 if (!PyArg_ParseTuple(args, "i", &code))
2477 return NULL;
2478 message = strerror(code);
2479 if (message == NULL) {
2480 PyErr_SetString(PyExc_ValueError,
2481 "strerror code out of range");
2482 return NULL;
2483 }
2484 return PyString_FromString(message);
2485}
2486#endif /* strerror */
2487
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002488
Guido van Rossumc9641791998-08-04 15:26:23 +00002489#ifdef HAVE_SYS_WAIT_H
2490
2491#ifdef WIFSTOPPED
2492static char posix_WIFSTOPPED__doc__[] =
2493"WIFSTOPPED(status) -> Boolean\n\
2494See Unix documentation.";
2495
2496static PyObject *
2497posix_WIFSTOPPED(self, args)
2498 PyObject *self;
2499 PyObject *args;
2500{
2501 int status = 0;
2502
2503 if (!PyArg_Parse(args, "i", &status))
2504 {
2505 return NULL;
2506 }
2507
2508 return Py_BuildValue("i", WIFSTOPPED(status));
2509}
2510#endif /* WIFSTOPPED */
2511
2512#ifdef WIFSIGNALED
2513static char posix_WIFSIGNALED__doc__[] =
2514"WIFSIGNALED(status) -> Boolean\n\
2515See Unix documentation.";
2516
2517static PyObject *
2518posix_WIFSIGNALED(self, args)
2519 PyObject *self;
2520 PyObject *args;
2521{
2522 int status = 0;
2523
2524 if (!PyArg_Parse(args, "i", &status))
2525 {
2526 return NULL;
2527 }
2528
2529 return Py_BuildValue("i", WIFSIGNALED(status));
2530}
2531#endif /* WIFSIGNALED */
2532
2533#ifdef WIFEXITED
2534static char posix_WIFEXITED__doc__[] =
2535"WIFEXITED(status) -> Boolean\n\
2536See Unix documentation.";
2537
2538static PyObject *
2539posix_WIFEXITED(self, args)
2540 PyObject *self;
2541 PyObject *args;
2542{
2543 int status = 0;
2544
2545 if (!PyArg_Parse(args, "i", &status))
2546 {
2547 return NULL;
2548 }
2549
2550 return Py_BuildValue("i", WIFEXITED(status));
2551}
2552#endif /* WIFEXITED */
2553
2554#ifdef WIFSTOPPED
2555static char posix_WEXITSTATUS__doc__[] =
2556"WEXITSTATUS(status) -> integer\n\
2557See Unix documentation.";
2558
2559static PyObject *
2560posix_WEXITSTATUS(self, args)
2561 PyObject *self;
2562 PyObject *args;
2563{
2564 int status = 0;
2565
2566 if (!PyArg_Parse(args, "i", &status))
2567 {
2568 return NULL;
2569 }
2570
2571 return Py_BuildValue("i", WEXITSTATUS(status));
2572}
2573#endif /* WEXITSTATUS */
2574
2575#ifdef WTERMSIG
2576static char posix_WTERMSIG__doc__[] =
2577"WTERMSIG(status) -> integer\n\
2578See Unix documentation.";
2579
2580static PyObject *
2581posix_WTERMSIG(self, args)
2582 PyObject *self;
2583 PyObject *args;
2584{
2585 int status = 0;
2586
2587 if (!PyArg_Parse(args, "i", &status))
2588 {
2589 return NULL;
2590 }
2591
2592 return Py_BuildValue("i", WTERMSIG(status));
2593}
2594#endif /* WTERMSIG */
2595
2596#ifdef WSTOPSIG
2597static char posix_WSTOPSIG__doc__[] =
2598"WSTOPSIG(status) -> integer\n\
2599See Unix documentation.";
2600
2601static PyObject *
2602posix_WSTOPSIG(self, args)
2603 PyObject *self;
2604 PyObject *args;
2605{
2606 int status = 0;
2607
2608 if (!PyArg_Parse(args, "i", &status))
2609 {
2610 return NULL;
2611 }
2612
2613 return Py_BuildValue("i", WSTOPSIG(status));
2614}
2615#endif /* WSTOPSIG */
2616
2617#endif /* HAVE_SYS_WAIT_H */
2618
2619
Barry Warsaw53699e91996-12-10 23:23:01 +00002620static PyMethodDef posix_methods[] = {
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002621 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2622 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002623#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002624 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002625#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002626#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002627 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002628#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002629#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002631#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002632 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2633 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2634 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002635#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002636 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002637#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002638#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002639 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002640#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002641 {"rename", posix_rename, 0, posix_rename__doc__},
2642 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2643 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002644#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002646#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002647#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002648 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002649#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002650 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002651#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002652 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002653#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002654 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2655 {"remove", posix_unlink, 0, posix_remove__doc__},
2656 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002657#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002658 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002659#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002661#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002662 {"execv", posix_execv, 0, posix_execv__doc__},
2663 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002664#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002665#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002666 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002667#endif /* HAVE_FORK */
2668#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002669 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002670#endif /* HAVE_GETEGID */
2671#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002672 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002673#endif /* HAVE_GETEUID */
2674#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002675 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002676#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002677 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002678#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002679 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002680#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002681#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002682 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002683#endif /* HAVE_GETPPID */
2684#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002685 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002686#endif /* HAVE_GETUID */
2687#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002688 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002689#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002690#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002691 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002692#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002693#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002694 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002695#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002696#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002698#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002699#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002700 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002701#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002702#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002703 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002704#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002705#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002706 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002707#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002708#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002709 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002710#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002711#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002712 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002713#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002714#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002715 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002716#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002717#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002718 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002719#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002720#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002721 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002722#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002723 {"open", posix_open, 1, posix_open__doc__},
2724 {"close", posix_close, 0, posix_close__doc__},
2725 {"dup", posix_dup, 0, posix_dup__doc__},
2726 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2727 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2728 {"read", posix_read, 0, posix_read__doc__},
2729 {"write", posix_write, 0, posix_write__doc__},
2730 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2731 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002732#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002733 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002734#endif
2735#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002736 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002737#endif
2738#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002739 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002740#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002741#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002742 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002743#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002744#ifdef HAVE_STRERROR
2745 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2746#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00002747#ifdef HAVE_SYS_WAIT_H
2748#ifdef WIFSTOPPED
2749 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
2750#endif /* WIFSTOPPED */
2751#ifdef WIFSIGNALED
2752 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
2753#endif /* WIFSIGNALED */
2754#ifdef WIFEXITED
2755 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
2756#endif /* WIFEXITED */
2757#ifdef WEXITSTATUS
2758 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
2759#endif /* WEXITSTATUS */
2760#ifdef WTERMSIG
2761 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
2762#endif /* WTERMSIG */
2763#ifdef WSTOPSIG
2764 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
2765#endif /* WSTOPSIG */
2766#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002767 {NULL, NULL} /* Sentinel */
2768};
2769
2770
Barry Warsaw4a342091996-12-19 23:50:02 +00002771static int
2772ins(d, symbol, value)
2773 PyObject* d;
2774 char* symbol;
2775 long value;
2776{
2777 PyObject* v = PyInt_FromLong(value);
2778 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2779 return -1; /* triggers fatal error */
2780
2781 Py_DECREF(v);
2782 return 0;
2783}
2784
Guido van Rossumd48f2521997-12-05 22:19:34 +00002785#if defined(PYOS_OS2)
2786/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
2787static int insertvalues(PyObject *d)
2788{
2789 APIRET rc;
2790 ULONG values[QSV_MAX+1];
2791 PyObject *v;
2792 char *ver, tmp[10];
2793
2794 Py_BEGIN_ALLOW_THREADS
2795 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
2796 Py_END_ALLOW_THREADS
2797
2798 if (rc != NO_ERROR) {
2799 os2_error(rc);
2800 return -1;
2801 }
2802
2803 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
2804 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
2805 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
2806 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
2807 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
2808 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
2809 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
2810
2811 switch (values[QSV_VERSION_MINOR]) {
2812 case 0: ver = "2.00"; break;
2813 case 10: ver = "2.10"; break;
2814 case 11: ver = "2.11"; break;
2815 case 30: ver = "3.00"; break;
2816 case 40: ver = "4.00"; break;
2817 case 50: ver = "5.00"; break;
2818 default:
2819 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
2820 values[QSV_VERSION_MINOR]);
2821 ver = &tmp[0];
2822 }
2823
2824 /* Add Indicator of the Version of the Operating System */
2825 v = PyString_FromString(ver);
2826 if (!v || PyDict_SetItemString(d, "version", v) < 0)
2827 return -1;
2828 Py_DECREF(v);
2829
2830 /* Add Indicator of Which Drive was Used to Boot the System */
2831 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
2832 tmp[1] = ':';
2833 tmp[2] = '\0';
2834
2835 v = PyString_FromString(tmp);
2836 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
2837 return -1;
2838 Py_DECREF(v);
2839
2840 return 0;
2841}
2842#endif
2843
Barry Warsaw4a342091996-12-19 23:50:02 +00002844static int
2845all_ins(d)
2846 PyObject* d;
2847{
2848#ifdef WNOHANG
2849 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
2850#endif
2851#ifdef O_RDONLY
2852 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
2853#endif
2854#ifdef O_WRONLY
2855 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
2856#endif
2857#ifdef O_RDWR
2858 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
2859#endif
2860#ifdef O_NDELAY
2861 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
2862#endif
2863#ifdef O_NONBLOCK
2864 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
2865#endif
2866#ifdef O_APPEND
2867 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
2868#endif
2869#ifdef O_DSYNC
2870 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
2871#endif
2872#ifdef O_RSYNC
2873 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
2874#endif
2875#ifdef O_SYNC
2876 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
2877#endif
2878#ifdef O_NOCTTY
2879 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
2880#endif
2881#ifdef O_CREAT
2882 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
2883#endif
2884#ifdef O_EXCL
2885 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
2886#endif
2887#ifdef O_TRUNC
2888 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
2889#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00002890#ifdef O_BINARY
2891 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
2892#endif
2893#ifdef O_TEXT
2894 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
2895#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00002896
2897#if defined(PYOS_OS2)
2898 if (insertvalues(d)) return -1;
2899#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00002900 return 0;
2901}
2902
2903
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002904#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002905#define INITFUNC initnt
2906#define MODNAME "nt"
2907#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002908#if defined(PYOS_OS2)
2909#define INITFUNC initos2
2910#define MODNAME "os2"
2911#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002912#define INITFUNC initposix
2913#define MODNAME "posix"
2914#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002915#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002916
Guido van Rossumb6775db1994-08-01 11:34:53 +00002917void
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002918INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00002919{
Barry Warsaw53699e91996-12-10 23:23:01 +00002920 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002921
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002922 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002923 posix_methods,
2924 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002925 (PyObject *)NULL,
2926 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002927 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002928
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002929 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002930 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002931 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002932 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00002933 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002934
Barry Warsaw4a342091996-12-19 23:50:02 +00002935 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00002936 return;
2937
Barry Warsawd58d7641998-07-23 16:14:40 +00002938 Py_INCREF(PyExc_OSError);
2939 PosixError = PyExc_OSError;
2940 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002941}