blob: f9f6284bd2b5886fb9817b1adfff2d7577104142 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* POSIX module implementation */
33
Guido van Rossuma4916fa1996-05-23 22:58:55 +000034/* This file is also used for Windows NT and MS-Win. In that case the module
Guido van Rossumad0ee831995-03-01 10:34:45 +000035 actually calls itself 'nt', not 'posix', and a few functions are
36 either unimplemented or implemented differently. The source
Guido van Rossum8d665e61996-06-26 18:22:49 +000037 assumes that for Windows NT, the macro 'MS_WIN32' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +000038 of the compiler used. Different compilers define their own feature
Guido van Rossuma4916fa1996-05-23 22:58:55 +000039 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000040
Guido van Rossuma4916fa1996-05-23 22:58:55 +000041/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000042
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000043static char posix__doc__ [] =
44"This module provides access to operating system functionality that is\n\
45standardized by the C Standard and the POSIX standard (a thinly\n\
46disguised Unix interface). Refer to the library manual and\n\
47corresponding Unix manual entries for more information on calls.";
48
Barry Warsaw53699e91996-12-10 23:23:01 +000049#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000051#if defined(PYOS_OS2)
52#define INCL_DOS
53#define INCL_DOSERRORS
54#define INCL_DOSPROCESS
55#define INCL_NOPMAPI
56#include <os2.h>
57#endif
58
Guido van Rossumb6775db1994-08-01 11:34:53 +000059#include <sys/types.h>
60#include <sys/stat.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +000061#ifdef HAVE_SYS_WAIT_H
62#include <sys/wait.h> /* For WNOHANG */
63#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000064
Guido van Rossuma376cc51996-12-05 23:43:35 +000065#ifdef HAVE_SIGNAL_H
66#include <signal.h>
67#endif
68
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include "mytime.h" /* For clock_t on some systems */
70
71#ifdef HAVE_FCNTL_H
72#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000073#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Guido van Rossuma4916fa1996-05-23 22:58:55 +000075/* Various compilers have only certain posix functions */
Guido van Rossum6d8841c1997-08-14 19:57:39 +000076/* XXX Gosh I wish these were all moved into config.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000077#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000078#include <process.h>
79#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +000080#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000081#define HAVE_GETCWD 1
82#define HAVE_OPENDIR 1
83#define HAVE_SYSTEM 1
84#if defined(__OS2__)
85#define HAVE_EXECV 1
86#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +000087#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000088#include <process.h>
89#else
90#ifdef __BORLANDC__ /* Borland compiler */
91#define HAVE_EXECV 1
92#define HAVE_GETCWD 1
93#define HAVE_GETEGID 1
94#define HAVE_GETEUID 1
95#define HAVE_GETGID 1
96#define HAVE_GETPPID 1
97#define HAVE_GETUID 1
98#define HAVE_KILL 1
99#define HAVE_OPENDIR 1
100#define HAVE_PIPE 1
101#define HAVE_POPEN 1
102#define HAVE_SYSTEM 1
103#define HAVE_WAIT 1
104#else
105#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000106#define HAVE_GETCWD 1
107#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#define HAVE_EXECV 1
109#define HAVE_PIPE 1
110#define HAVE_POPEN 1
111#define HAVE_SYSTEM 1
112#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000113#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#else /* all other compilers */
115/* Unix functions that the configure script doesn't check for */
116#define HAVE_EXECV 1
117#define HAVE_FORK 1
118#define HAVE_GETCWD 1
119#define HAVE_GETEGID 1
120#define HAVE_GETEUID 1
121#define HAVE_GETGID 1
122#define HAVE_GETPPID 1
123#define HAVE_GETUID 1
124#define HAVE_KILL 1
125#define HAVE_OPENDIR 1
126#define HAVE_PIPE 1
127#define HAVE_POPEN 1
128#define HAVE_SYSTEM 1
129#define HAVE_WAIT 1
130#endif /* _MSC_VER */
131#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000132#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000133#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000134
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000136
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000138#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000139#endif
140
141#ifdef NeXT
142/* NeXT's <unistd.h> and <utime.h> aren't worth much */
143#undef HAVE_UNISTD_H
144#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000145#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000146/* #undef HAVE_GETCWD */
147#endif
148
149#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000150/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
151extern int rename();
152extern int pclose();
153extern int lstat();
154extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000156#if defined(PYCC_VACPP)
157extern int mkdir Py_PROTO((char *));
158#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000159#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000160extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000162extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000163#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000164#endif
165#if defined(__IBMC__) || defined(__IBMCPP__)
166extern int chdir Py_PROTO((char *));
167extern int rmdir Py_PROTO((char *));
168#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000169extern int chdir Py_PROTO((const char *));
170extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000172extern int chmod Py_PROTO((const char *, mode_t));
173extern int chown Py_PROTO((const char *, uid_t, gid_t));
174extern char *getcwd Py_PROTO((char *, int));
175extern char *strerror Py_PROTO((int));
176extern int link Py_PROTO((const char *, const char *));
177extern int rename Py_PROTO((const char *, const char *));
178extern int stat Py_PROTO((const char *, struct stat *));
179extern int unlink Py_PROTO((const char *));
180extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000182extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000183#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000185extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000186#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000188
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000189#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#ifdef HAVE_UTIME_H
192#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000193#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000194
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000195#ifdef HAVE_SYS_UTIME_H
196#include <sys/utime.h>
197#define HAVE_UTIME_H /* pretend we do for the rest of this file */
198#endif /* HAVE_SYS_UTIME_H */
199
Guido van Rossumb6775db1994-08-01 11:34:53 +0000200#ifdef HAVE_SYS_TIMES_H
201#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000202#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203
204#ifdef HAVE_SYS_PARAM_H
205#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
208#ifdef HAVE_SYS_UTSNAME_H
209#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
212#ifndef MAXPATHLEN
213#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000216#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000218#define NAMLEN(dirent) strlen((dirent)->d_name)
219#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000220#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#include <direct.h>
222#define NAMLEN(dirent) strlen((dirent)->d_name)
223#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000225#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000227#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#endif
230#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232#endif
233#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#endif
236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#include <direct.h>
240#include <io.h>
241#include <process.h>
242#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000243#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000245#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000246#else /* 16-bit Windows */
247#include <dos.h>
248#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000249#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000250#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
Guido van Rossumd48f2521997-12-05 22:19:34 +0000252#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255
256/* Return a dictionary corresponding to the POSIX environment table */
257
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000258#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000260#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261
Barry Warsaw53699e91996-12-10 23:23:01 +0000262static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263convertenviron()
264{
Barry Warsaw53699e91996-12-10 23:23:01 +0000265 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000267 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 if (d == NULL)
269 return NULL;
270 if (environ == NULL)
271 return d;
272 /* XXX This part ignores errors */
273 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000274 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275 char *p = strchr(*e, '=');
276 if (p == NULL)
277 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000278 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 if (v == NULL)
280 continue;
281 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000282 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000284 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000286#if defined(PYOS_OS2)
287 {
288 APIRET rc;
289 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
290
291 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
292 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
293 PyObject *v = PyString_FromString(buffer);
294 PyDict_SetItemString(d, "BEGINLIBPATH", v);
295 Py_DECREF(v);
296 }
297 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
298 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
299 PyObject *v = PyString_FromString(buffer);
300 PyDict_SetItemString(d, "ENDLIBPATH", v);
301 Py_DECREF(v);
302 }
303 }
304#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 return d;
306}
307
308
Barry Warsaw53699e91996-12-10 23:23:01 +0000309static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310
311/* Set a POSIX-specific error from errno, and return NULL */
312
Barry Warsawd58d7641998-07-23 16:14:40 +0000313static PyObject *
314posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000315{
Barry Warsaw53699e91996-12-10 23:23:01 +0000316 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317}
Barry Warsawd58d7641998-07-23 16:14:40 +0000318static PyObject *
319posix_error_with_filename(name)
320 char* name;
321{
322 return PyErr_SetFromErrnoWithFilename(PosixError, name);
323}
324
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325
Guido van Rossumd48f2521997-12-05 22:19:34 +0000326#if defined(PYOS_OS2)
327/**********************************************************************
328 * Helper Function to Trim and Format OS/2 Messages
329 **********************************************************************/
330 static void
331os2_formatmsg(char *msgbuf, int msglen, char *reason)
332{
333 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
334
335 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
336 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
337
338 while (lastc > msgbuf && isspace(*lastc))
339 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
340 }
341
342 /* Add Optional Reason Text */
343 if (reason) {
344 strcat(msgbuf, " : ");
345 strcat(msgbuf, reason);
346 }
347}
348
349/**********************************************************************
350 * Decode an OS/2 Operating System Error Code
351 *
352 * A convenience function to lookup an OS/2 error code and return a
353 * text message we can use to raise a Python exception.
354 *
355 * Notes:
356 * The messages for errors returned from the OS/2 kernel reside in
357 * the file OSO001.MSG in the \OS2 directory hierarchy.
358 *
359 **********************************************************************/
360 static char *
361os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
362{
363 APIRET rc;
364 ULONG msglen;
365
366 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
367 Py_BEGIN_ALLOW_THREADS
368 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
369 errorcode, "oso001.msg", &msglen);
370 Py_END_ALLOW_THREADS
371
372 if (rc == NO_ERROR)
373 os2_formatmsg(msgbuf, msglen, reason);
374 else
375 sprintf(msgbuf, "unknown OS error #%d", errorcode);
376
377 return msgbuf;
378}
379
380/* Set an OS/2-specific error and return NULL. OS/2 kernel
381 errors are not in a global variable e.g. 'errno' nor are
382 they congruent with posix error numbers. */
383
384static PyObject * os2_error(int code)
385{
386 char text[1024];
387 PyObject *v;
388
389 os2_strerror(text, sizeof(text), code, "");
390
391 v = Py_BuildValue("(is)", code, text);
392 if (v != NULL) {
393 PyErr_SetObject(PosixError, v);
394 Py_DECREF(v);
395 }
396 return NULL; /* Signal to Python that an Exception is Pending */
397}
398
399#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400
401/* POSIX generic methods */
402
Barry Warsaw53699e91996-12-10 23:23:01 +0000403static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000405 PyObject *args;
406 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000408 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000409 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000410 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000412 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000413 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000414 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000415 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000416 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000417 Py_INCREF(Py_None);
418 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
420
Barry Warsaw53699e91996-12-10 23:23:01 +0000421static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000423 PyObject *args;
424 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000426 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000427 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000428 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000430 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000431 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000432 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000433 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000434 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000436 Py_INCREF(Py_None);
437 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438}
439
Barry Warsaw53699e91996-12-10 23:23:01 +0000440static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000442 PyObject *args;
443 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000445 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000447 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000448 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000450 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000451 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000452 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000453 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000454 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000455 Py_INCREF(Py_None);
456 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457}
458
Barry Warsaw53699e91996-12-10 23:23:01 +0000459static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000460posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 PyObject *args;
462 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000463{
464 char *path;
465 int i,i2;
466 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000468 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000469 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000470 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000471 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000472 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000473 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000474 Py_INCREF(Py_None);
475 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000476}
477
Barry Warsaw53699e91996-12-10 23:23:01 +0000478static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000480 PyObject *self;
481 PyObject *args;
482 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483{
484 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000485 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000486 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000487 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000490 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000491 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000492 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000493 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 return Py_BuildValue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000495 (long)st.st_mode,
496 (long)st.st_ino,
497 (long)st.st_dev,
498 (long)st.st_nlink,
499 (long)st.st_uid,
500 (long)st.st_gid,
501 (long)st.st_size,
502 (long)st.st_atime,
503 (long)st.st_mtime,
504 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000505}
506
507
508/* POSIX methods */
509
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000510static char posix_chdir__doc__[] =
511"chdir(path) -> None\n\
512Change the current working directory to the specified path.";
513
Barry Warsaw53699e91996-12-10 23:23:01 +0000514static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000516 PyObject *self;
517 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519 return posix_1str(args, chdir);
520}
521
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000522
523static char posix_chmod__doc__[] =
524"chmod(path, mode) -> None\n\
525Change the access permissions of a file.";
526
Barry Warsaw53699e91996-12-10 23:23:01 +0000527static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000529 PyObject *self;
530 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 return posix_strint(args, chmod);
533}
534
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000535
Guido van Rossumb6775db1994-08-01 11:34:53 +0000536#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000537static char posix_chown__doc__[] =
538"chown(path, uid, gid) -> None\n\
539Change the owner and group id of path to the numeric uid and gid.";
540
Barry Warsaw53699e91996-12-10 23:23:01 +0000541static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000542posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000543 PyObject *self;
544 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000545{
546 return posix_strintint(args, chown);
547}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000548#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000549
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000550
Guido van Rossum36bc6801995-06-14 22:54:23 +0000551#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000552static char posix_getcwd__doc__[] =
553"getcwd() -> path\n\
554Return a string representing the current working directory.";
555
Barry Warsaw53699e91996-12-10 23:23:01 +0000556static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000558 PyObject *self;
559 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560{
561 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000562 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000563 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000565 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000566 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000567 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000568 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000570 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000572#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000574
Guido van Rossumb6775db1994-08-01 11:34:53 +0000575#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000576static char posix_link__doc__[] =
577"link(src, dst) -> None\n\
578Create a hard link to a file.";
579
Barry Warsaw53699e91996-12-10 23:23:01 +0000580static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000582 PyObject *self;
583 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585 return posix_2str(args, link);
586}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000587#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000589
590static char posix_listdir__doc__[] =
591"listdir(path) -> list_of_strings\n\
592Return a list containing the names of the entries in the directory.\n\
593\n\
594 path: path of directory to list\n\
595\n\
596The list is in arbitrary order. It does not include the special\n\
597entries '.' and '..' even if they are present in the directory.";
598
Barry Warsaw53699e91996-12-10 23:23:01 +0000599static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000600posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000601 PyObject *self;
602 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000603{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000604 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000605 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000606#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000607
Guido van Rossumb6775db1994-08-01 11:34:53 +0000608 char *name;
609 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000610 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000611 HANDLE hFindFile;
612 WIN32_FIND_DATA FileData;
613 char namebuf[MAX_PATH+5];
614
Guido van Rossum7e488981998-10-08 02:25:24 +0000615 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000616 return NULL;
617 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000618 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000619 return NULL;
620 }
621 strcpy(namebuf, name);
622 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
623 namebuf[len++] = '/';
624 strcpy(namebuf + len, "*.*");
625
Barry Warsaw53699e91996-12-10 23:23:01 +0000626 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000627 return NULL;
628
629 hFindFile = FindFirstFile(namebuf, &FileData);
630 if (hFindFile == INVALID_HANDLE_VALUE) {
631 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000632 if (errno == ERROR_FILE_NOT_FOUND)
633 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000634 return posix_error();
635 }
636 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000637 if (FileData.cFileName[0] == '.' &&
638 (FileData.cFileName[1] == '\0' ||
639 FileData.cFileName[1] == '.' &&
640 FileData.cFileName[2] == '\0'))
641 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000642 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000643 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000644 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000645 d = NULL;
646 break;
647 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000648 if (PyList_Append(d, v) != 0) {
649 Py_DECREF(v);
650 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000651 d = NULL;
652 break;
653 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000654 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000655 } while (FindNextFile(hFindFile, &FileData) == TRUE);
656
657 if (FindClose(hFindFile) == FALSE) {
658 errno = GetLastError();
659 return posix_error();
660 }
661
662 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000663
Guido van Rossum8d665e61996-06-26 18:22:49 +0000664#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000665#ifdef _MSC_VER /* 16-bit Windows */
666
667#ifndef MAX_PATH
668#define MAX_PATH 250
669#endif
670 char *name, *pt;
671 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000672 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000673 char namebuf[MAX_PATH+5];
674 struct _find_t ep;
675
Guido van Rossum7e488981998-10-08 02:25:24 +0000676 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000677 return NULL;
678 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000679 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000680 return NULL;
681 }
682 strcpy(namebuf, name);
683 for (pt = namebuf; *pt; pt++)
684 if (*pt == '/')
685 *pt = '\\';
686 if (namebuf[len-1] != '\\')
687 namebuf[len++] = '\\';
688 strcpy(namebuf + len, "*.*");
689
Barry Warsaw53699e91996-12-10 23:23:01 +0000690 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000691 return NULL;
692
693 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000694 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
695 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000696 errno = ENOENT;
697 return posix_error();
698 }
699 do {
700 if (ep.name[0] == '.' &&
701 (ep.name[1] == '\0' ||
702 ep.name[1] == '.' &&
703 ep.name[2] == '\0'))
704 continue;
705 strcpy(namebuf, ep.name);
706 for (pt = namebuf; *pt; pt++)
707 if (isupper(*pt))
708 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000709 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000710 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000711 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000712 d = NULL;
713 break;
714 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000715 if (PyList_Append(d, v) != 0) {
716 Py_DECREF(v);
717 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000718 d = NULL;
719 break;
720 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000721 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000722 } while (_dos_findnext(&ep) == 0);
723
724 return d;
725
726#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000727#if defined(PYOS_OS2)
728
729#ifndef MAX_PATH
730#define MAX_PATH CCHMAXPATH
731#endif
732 char *name, *pt;
733 int len;
734 PyObject *d, *v;
735 char namebuf[MAX_PATH+5];
736 HDIR hdir = 1;
737 ULONG srchcnt = 1;
738 FILEFINDBUF3 ep;
739 APIRET rc;
740
Guido van Rossum7e488981998-10-08 02:25:24 +0000741 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000742 return NULL;
743 if (len >= MAX_PATH) {
744 PyErr_SetString(PyExc_ValueError, "path too long");
745 return NULL;
746 }
747 strcpy(namebuf, name);
748 for (pt = namebuf; *pt; pt++)
749 if (*pt == '/')
750 *pt = '\\';
751 if (namebuf[len-1] != '\\')
752 namebuf[len++] = '\\';
753 strcpy(namebuf + len, "*.*");
754
755 if ((d = PyList_New(0)) == NULL)
756 return NULL;
757
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000758 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
759 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000760 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000761 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
762 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
763 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000764
765 if (rc != NO_ERROR) {
766 errno = ENOENT;
767 return posix_error();
768 }
769
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000770 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000771 do {
772 if (ep.achName[0] == '.'
773 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000774 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000775
776 strcpy(namebuf, ep.achName);
777
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000778 /* Leave Case of Name Alone -- In Native Form */
779 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000780
781 v = PyString_FromString(namebuf);
782 if (v == NULL) {
783 Py_DECREF(d);
784 d = NULL;
785 break;
786 }
787 if (PyList_Append(d, v) != 0) {
788 Py_DECREF(v);
789 Py_DECREF(d);
790 d = NULL;
791 break;
792 }
793 Py_DECREF(v);
794 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
795 }
796
797 return d;
798#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000799
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000800 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000801 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000803 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000804 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000805 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000806 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000807 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000808 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000810 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000811 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000813 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000814 return NULL;
815 }
816 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000817 if (ep->d_name[0] == '.' &&
818 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000819 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000820 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000821 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000823 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000824 d = NULL;
825 break;
826 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000827 if (PyList_Append(d, v) != 0) {
828 Py_DECREF(v);
829 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830 d = NULL;
831 break;
832 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000833 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834 }
835 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000836 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000837
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000839
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000840#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000841#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000842#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843}
844
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000845static char posix_mkdir__doc__[] =
846"mkdir(path [, mode=0777]) -> None\n\
847Create a directory.";
848
Barry Warsaw53699e91996-12-10 23:23:01 +0000849static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000850posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000851 PyObject *self;
852 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000853{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000854 int res;
855 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000856 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000857 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000858 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000859 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000860#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000861 res = mkdir(path);
862#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000863 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000864#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000865 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000866 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000867 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000868 Py_INCREF(Py_None);
869 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000870}
871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000872
Guido van Rossumb6775db1994-08-01 11:34:53 +0000873#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000874static char posix_nice__doc__[] =
875"nice(inc) -> new_priority\n\
876Decrease the priority of process and return new priority.";
877
Barry Warsaw53699e91996-12-10 23:23:01 +0000878static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000879posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000880 PyObject *self;
881 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000882{
883 int increment, value;
884
Barry Warsaw53699e91996-12-10 23:23:01 +0000885 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000886 return NULL;
887 value = nice(increment);
888 if (value == -1)
889 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000890 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000891}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000892#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000893
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000894
895static char posix_rename__doc__[] =
896"rename(old, new) -> None\n\
897Rename a file or directory.";
898
Barry Warsaw53699e91996-12-10 23:23:01 +0000899static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000901 PyObject *self;
902 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000903{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904 return posix_2str(args, rename);
905}
906
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000907
908static char posix_rmdir__doc__[] =
909"rmdir(path) -> None\n\
910Remove a directory.";
911
Barry Warsaw53699e91996-12-10 23:23:01 +0000912static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000914 PyObject *self;
915 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000916{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917 return posix_1str(args, rmdir);
918}
919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000920
921static char posix_stat__doc__[] =
922"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
923Perform a stat system call on the given path.";
924
Barry Warsaw53699e91996-12-10 23:23:01 +0000925static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000926posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000927 PyObject *self;
928 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000930 return posix_do_stat(self, args, stat);
931}
932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000933
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000934#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000935static char posix_system__doc__[] =
936"system(command) -> exit_status\n\
937Execute the command (a string) in a subshell.";
938
Barry Warsaw53699e91996-12-10 23:23:01 +0000939static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000940posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000941 PyObject *self;
942 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000943{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000944 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000945 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +0000946 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000947 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000948 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000949 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +0000950 Py_END_ALLOW_THREADS
951 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952}
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000953#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000955
956static char posix_umask__doc__[] =
957"umask(new_mask) -> old_mask\n\
958Set the current numeric umask and return the previous umask.";
959
Barry Warsaw53699e91996-12-10 23:23:01 +0000960static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000961posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000962 PyObject *self;
963 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000964{
965 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +0000966 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 return NULL;
968 i = umask(i);
969 if (i < 0)
970 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000971 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000972}
973
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000974
975static char posix_unlink__doc__[] =
976"unlink(path) -> None\n\
977Remove a file (same as remove(path)).";
978
979static char posix_remove__doc__[] =
980"remove(path) -> None\n\
981Remove a file (same as unlink(path)).";
982
Barry Warsaw53699e91996-12-10 23:23:01 +0000983static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000984posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000985 PyObject *self;
986 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000987{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000988 return posix_1str(args, unlink);
989}
990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000991
Guido van Rossumb6775db1994-08-01 11:34:53 +0000992#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000993static char posix_uname__doc__[] =
994"uname() -> (sysname, nodename, release, version, machine)\n\
995Return a tuple identifying the current operating system.";
996
Barry Warsaw53699e91996-12-10 23:23:01 +0000997static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000998posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000999 PyObject *self;
1000 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001001{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001002 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001003 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001005 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001006 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001007 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001008 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001009 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001010 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001011 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001012 u.sysname,
1013 u.nodename,
1014 u.release,
1015 u.version,
1016 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001017}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001018#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001019
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001020
1021static char posix_utime__doc__[] =
1022"utime(path, (atime, utime)) -> None\n\
1023Set the access and modified time of the file to the given values.";
1024
Barry Warsaw53699e91996-12-10 23:23:01 +00001025static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001026posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001027 PyObject *self;
1028 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001029{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001030 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001031 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001032 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001033
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001034/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001035#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001036 struct utimbuf buf;
1037#define ATIME buf.actime
1038#define MTIME buf.modtime
1039#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001040#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001041 time_t buf[2];
1042#define ATIME buf[0]
1043#define MTIME buf[1]
1044#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001045#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001046
Barry Warsaw53699e91996-12-10 23:23:01 +00001047 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001048 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001049 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001050 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001051 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001052 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001053 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001054 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001055 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001056 Py_INCREF(Py_None);
1057 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001058#undef UTIME_ARG
1059#undef ATIME
1060#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001061}
1062
Guido van Rossum85e3b011991-06-03 12:42:10 +00001063
Guido van Rossum3b066191991-06-04 19:40:25 +00001064/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001065
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001066static char posix__exit__doc__[] =
1067"_exit(status)\n\
1068Exit to the system with specified status, without normal exit processing.";
1069
Barry Warsaw53699e91996-12-10 23:23:01 +00001070static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001071posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001072 PyObject *self;
1073 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001074{
1075 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001076 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001077 return NULL;
1078 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001079 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001080}
1081
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001082
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001083#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001084static char posix_execv__doc__[] =
1085"execv(path, args)\n\
1086Execute an executable path with arguments, replacing current process.\n\
1087\n\
1088 path: path of executable file\n\
1089 args: tuple or list of strings";
1090
Barry Warsaw53699e91996-12-10 23:23:01 +00001091static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001092posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001093 PyObject *self;
1094 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001095{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001096 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001097 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001098 char **argvlist;
1099 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001100 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001101
Guido van Rossum89b33251993-10-22 14:26:06 +00001102 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001103 argv is a list or tuple of strings. */
1104
Barry Warsaw53699e91996-12-10 23:23:01 +00001105 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001106 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001107 if (PyList_Check(argv)) {
1108 argc = PyList_Size(argv);
1109 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001110 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001111 else if (PyTuple_Check(argv)) {
1112 argc = PyTuple_Size(argv);
1113 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001114 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001115 else {
1116 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001117 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001118 return NULL;
1119 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001120
Barry Warsaw53699e91996-12-10 23:23:01 +00001121 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001122 if (argvlist == NULL)
1123 return NULL;
1124 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001125 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1126 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001127 goto badarg;
1128 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001129 }
1130 argvlist[argc] = NULL;
1131
Guido van Rossumb6775db1994-08-01 11:34:53 +00001132#ifdef BAD_EXEC_PROTOTYPES
1133 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001134#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001135 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001136#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001137
Guido van Rossum85e3b011991-06-03 12:42:10 +00001138 /* If we get here it's definitely an error */
1139
Barry Warsaw53699e91996-12-10 23:23:01 +00001140 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001141 return posix_error();
1142}
1143
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001144
1145static char posix_execve__doc__[] =
1146"execve(path, args, env)\n\
1147Execute a path with arguments and environment, replacing current process.\n\
1148\n\
1149 path: path of executable file\n\
1150 args: tuple or list of arguments\n\
1151 env: dictonary of strings mapping to strings";
1152
Barry Warsaw53699e91996-12-10 23:23:01 +00001153static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001154posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001155 PyObject *self;
1156 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001157{
1158 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001159 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001160 char **argvlist;
1161 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001162 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001163 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001164 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001165
1166 /* execve has three arguments: (path, argv, env), where
1167 argv is a list or tuple of strings and env is a dictionary
1168 like posix.environ. */
1169
Barry Warsaw53699e91996-12-10 23:23:01 +00001170 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001171 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001172 if (PyList_Check(argv)) {
1173 argc = PyList_Size(argv);
1174 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001175 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001176 else if (PyTuple_Check(argv)) {
1177 argc = PyTuple_Size(argv);
1178 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001179 }
1180 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001181 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001182 return NULL;
1183 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001184 if (!PyMapping_Check(env)) {
1185 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001186 return NULL;
1187 }
1188
Barry Warsaw53699e91996-12-10 23:23:01 +00001189 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001190 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001191 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001192 return NULL;
1193 }
1194 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001195 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001196 "s;argv must be list of strings",
1197 &argvlist[i]))
1198 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001199 goto fail_1;
1200 }
1201 }
1202 argvlist[argc] = NULL;
1203
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001204 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001205 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001206 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001207 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001208 goto fail_1;
1209 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001210 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001211 keys = PyMapping_Keys(env);
1212 vals = PyMapping_Values(env);
1213 if (!keys || !vals)
1214 goto fail_2;
1215
1216 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001217 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001218
1219 key = PyList_GetItem(keys, pos);
1220 val = PyList_GetItem(vals, pos);
1221 if (!key || !val)
1222 goto fail_2;
1223
Barry Warsaw53699e91996-12-10 23:23:01 +00001224 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001225 !PyArg_Parse(val, "s;non-string value in env", &v))
1226 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001227 goto fail_2;
1228 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001229
1230#if defined(PYOS_OS2)
1231 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1232 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1233#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001234 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001235 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001236 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001237 goto fail_2;
1238 }
1239 sprintf(p, "%s=%s", k, v);
1240 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001241#if defined(PYOS_OS2)
1242 }
1243#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001244 }
1245 envlist[envc] = 0;
1246
Guido van Rossumb6775db1994-08-01 11:34:53 +00001247
1248#ifdef BAD_EXEC_PROTOTYPES
1249 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001250#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001251 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001252#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001253
1254 /* If we get here it's definitely an error */
1255
1256 (void) posix_error();
1257
1258 fail_2:
1259 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001260 PyMem_DEL(envlist[envc]);
1261 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001262 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001263 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001264 Py_XDECREF(vals);
1265 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001266 return NULL;
1267}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001268#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001269
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001270
Guido van Rossumad0ee831995-03-01 10:34:45 +00001271#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001272static char posix_fork__doc__[] =
1273"fork() -> pid\n\
1274Fork a child process.\n\
1275\n\
1276Return 0 to child process and PID of child to parent process.";
1277
Barry Warsaw53699e91996-12-10 23:23:01 +00001278static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001279posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001280 PyObject *self;
1281 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001282{
1283 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001284 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001285 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001286 pid = fork();
1287 if (pid == -1)
1288 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001289 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001290 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001291}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001292#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001293
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001294
Guido van Rossumad0ee831995-03-01 10:34:45 +00001295#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001296static char posix_getegid__doc__[] =
1297"getegid() -> egid\n\
1298Return the current process's effective group id.";
1299
Barry Warsaw53699e91996-12-10 23:23:01 +00001300static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001301posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001302 PyObject *self;
1303 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001304{
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001306 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001307 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001308}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001309#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001310
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001311
Guido van Rossumad0ee831995-03-01 10:34:45 +00001312#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001313static char posix_geteuid__doc__[] =
1314"geteuid() -> euid\n\
1315Return the current process's effective user id.";
1316
Barry Warsaw53699e91996-12-10 23:23:01 +00001317static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001318posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001319 PyObject *self;
1320 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001321{
Barry Warsaw53699e91996-12-10 23:23:01 +00001322 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001323 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001324 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001325}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001326#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001327
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001328
Guido van Rossumad0ee831995-03-01 10:34:45 +00001329#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001330static char posix_getgid__doc__[] =
1331"getgid() -> gid\n\
1332Return the current process's group id.";
1333
Barry Warsaw53699e91996-12-10 23:23:01 +00001334static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001335posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001336 PyObject *self;
1337 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001338{
Barry Warsaw53699e91996-12-10 23:23:01 +00001339 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001340 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001341 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001342}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001343#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001344
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001345
1346static char posix_getpid__doc__[] =
1347"getpid() -> pid\n\
1348Return the current process id";
1349
Barry Warsaw53699e91996-12-10 23:23:01 +00001350static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001351posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001352 PyObject *self;
1353 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001354{
Barry Warsaw53699e91996-12-10 23:23:01 +00001355 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001356 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001357 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001358}
1359
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001360
Guido van Rossumb6775db1994-08-01 11:34:53 +00001361#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001362static char posix_getpgrp__doc__[] =
1363"getpgrp() -> pgrp\n\
1364Return the current process group id.";
1365
Barry Warsaw53699e91996-12-10 23:23:01 +00001366static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001367posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001368 PyObject *self;
1369 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001370{
Barry Warsaw53699e91996-12-10 23:23:01 +00001371 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001372 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001373#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001374 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001375#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001376 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001377#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001378}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001379#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001381
Guido van Rossumb6775db1994-08-01 11:34:53 +00001382#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001383static char posix_setpgrp__doc__[] =
1384"setpgrp() -> None\n\
1385Make this process a session leader.";
1386
Barry Warsaw53699e91996-12-10 23:23:01 +00001387static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001388posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001389 PyObject *self;
1390 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001391{
Barry Warsaw53699e91996-12-10 23:23:01 +00001392 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001393 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001394#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001395 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001396#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001397 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001398#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001399 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001400 Py_INCREF(Py_None);
1401 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001402}
1403
Guido van Rossumb6775db1994-08-01 11:34:53 +00001404#endif /* HAVE_SETPGRP */
1405
Guido van Rossumad0ee831995-03-01 10:34:45 +00001406#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001407static char posix_getppid__doc__[] =
1408"getppid() -> ppid\n\
1409Return the parent's process id.";
1410
Barry Warsaw53699e91996-12-10 23:23:01 +00001411static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001412posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001413 PyObject *self;
1414 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001415{
Barry Warsaw53699e91996-12-10 23:23:01 +00001416 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001417 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001418 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001419}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001420#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001421
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001422
Guido van Rossumad0ee831995-03-01 10:34:45 +00001423#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001424static char posix_getuid__doc__[] =
1425"getuid() -> uid\n\
1426Return the current process's user id.";
1427
Barry Warsaw53699e91996-12-10 23:23:01 +00001428static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001429posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001430 PyObject *self;
1431 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001432{
Barry Warsaw53699e91996-12-10 23:23:01 +00001433 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001434 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001435 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001436}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001437#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001438
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001439
Guido van Rossumad0ee831995-03-01 10:34:45 +00001440#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001441static char posix_kill__doc__[] =
1442"kill(pid, sig) -> None\n\
1443Kill a process with a signal.";
1444
Barry Warsaw53699e91996-12-10 23:23:01 +00001445static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001446posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001447 PyObject *self;
1448 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001449{
1450 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001451 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001452 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001453#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001454 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1455 APIRET rc;
1456 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001457 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001458
1459 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1460 APIRET rc;
1461 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001462 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001463
1464 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001465 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001466#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001467 if (kill(pid, sig) == -1)
1468 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001469#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001470 Py_INCREF(Py_None);
1471 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001472}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001473#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001474
Guido van Rossumc0125471996-06-28 18:55:32 +00001475#ifdef HAVE_PLOCK
1476
1477#ifdef HAVE_SYS_LOCK_H
1478#include <sys/lock.h>
1479#endif
1480
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001481static char posix_plock__doc__[] =
1482"plock(op) -> None\n\
1483Lock program segments into memory.";
1484
Barry Warsaw53699e91996-12-10 23:23:01 +00001485static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001486posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001487 PyObject *self;
1488 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001489{
1490 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001491 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001492 return NULL;
1493 if (plock(op) == -1)
1494 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001495 Py_INCREF(Py_None);
1496 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001497}
1498#endif
1499
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001500
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001501#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001502static char posix_popen__doc__[] =
1503"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1504Open a pipe to/from a command returning a file object.";
1505
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001506#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001507static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001508async_system(const char *command)
1509{
1510 char *p, errormsg[256], args[1024];
1511 RESULTCODES rcodes;
1512 APIRET rc;
1513 char *shell = getenv("COMSPEC");
1514 if (!shell)
1515 shell = "cmd";
1516
1517 strcpy(args, shell);
1518 p = &args[ strlen(args)+1 ];
1519 strcpy(p, "/c ");
1520 strcat(p, command);
1521 p += strlen(p) + 1;
1522 *p = '\0';
1523
1524 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001525 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001526 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001527 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001528 &rcodes, shell);
1529 return rc;
1530}
1531
Guido van Rossumd48f2521997-12-05 22:19:34 +00001532static FILE *
1533popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001534{
1535 HFILE rhan, whan;
1536 FILE *retfd = NULL;
1537 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1538
Guido van Rossumd48f2521997-12-05 22:19:34 +00001539 if (rc != NO_ERROR) {
1540 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001541 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001542 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001543
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001544 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1545 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001546
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001547 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1548 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001549
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001550 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1551 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001552
1553 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001554 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001555 }
1556
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001557 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1558 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001559
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001560 close(oldfd); /* And Close Saved STDOUT Handle */
1561 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001562
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001563 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1564 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001565
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001566 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1567 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001568
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001569 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1570 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001571
1572 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001573 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001574 }
1575
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001576 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1577 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001578
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001579 close(oldfd); /* And Close Saved STDIN Handle */
1580 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001581
Guido van Rossumd48f2521997-12-05 22:19:34 +00001582 } else {
1583 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001584 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001585 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001586}
1587
1588static PyObject *
1589posix_popen(self, args)
1590 PyObject *self;
1591 PyObject *args;
1592{
1593 char *name;
1594 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001595 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001596 FILE *fp;
1597 PyObject *f;
1598 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1599 return NULL;
1600 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001601 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001602 Py_END_ALLOW_THREADS
1603 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001604 return os2_error(err);
1605
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001606 f = PyFile_FromFile(fp, name, mode, fclose);
1607 if (f != NULL)
1608 PyFile_SetBufSize(f, bufsize);
1609 return f;
1610}
1611
1612#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001613static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001614posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001615 PyObject *self;
1616 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001617{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001618 char *name;
1619 char *mode = "r";
1620 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001621 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001622 PyObject *f;
1623 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001624 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001625 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001626 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001627 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001628 if (fp == NULL)
1629 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001630 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001631 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001632 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001633 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001634}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001635#endif
1636
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001637#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001638
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001639
Guido van Rossumb6775db1994-08-01 11:34:53 +00001640#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001641static char posix_setuid__doc__[] =
1642"setuid(uid) -> None\n\
1643Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001644static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001645posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001646 PyObject *self;
1647 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001648{
1649 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001650 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001651 return NULL;
1652 if (setuid(uid) < 0)
1653 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001654 Py_INCREF(Py_None);
1655 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001656}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001657#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001658
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001659
Guido van Rossumb6775db1994-08-01 11:34:53 +00001660#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001661static char posix_setgid__doc__[] =
1662"setgid(gid) -> None\n\
1663Set the current process's group id.";
1664
Barry Warsaw53699e91996-12-10 23:23:01 +00001665static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001666posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001667 PyObject *self;
1668 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001669{
1670 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001671 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001672 return NULL;
1673 if (setgid(gid) < 0)
1674 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001675 Py_INCREF(Py_None);
1676 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001677}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001678#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001680
Guido van Rossumb6775db1994-08-01 11:34:53 +00001681#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682static char posix_waitpid__doc__[] =
1683"waitpid(pid, options) -> (pid, status)\n\
1684Wait for completion of a give child process.";
1685
Barry Warsaw53699e91996-12-10 23:23:01 +00001686static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001687posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001688 PyObject *self;
1689 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001690{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001691 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001692 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001693 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001694 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001695#ifdef NeXT
1696 pid = wait4(pid, (union wait *)&sts, options, NULL);
1697#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001698 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001699#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001700 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001701 if (pid == -1)
1702 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001703 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001704 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001705}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001706#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001707
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001708
Guido van Rossumad0ee831995-03-01 10:34:45 +00001709#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001710static char posix_wait__doc__[] =
1711"wait() -> (pid, status)\n\
1712Wait for completion of a child process.";
1713
Barry Warsaw53699e91996-12-10 23:23:01 +00001714static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001715posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001716 PyObject *self;
1717 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001718{
1719 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001720 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001721#ifdef NeXT
1722 pid = wait((union wait *)&sts);
1723#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001724 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001725#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001726 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001727 if (pid == -1)
1728 return posix_error();
1729 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001730 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001731}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001732#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001733
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001734
1735static char posix_lstat__doc__[] =
1736"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1737Like stat(path), but do not follow symbolic links.";
1738
Barry Warsaw53699e91996-12-10 23:23:01 +00001739static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001740posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001741 PyObject *self;
1742 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001743{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001744#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001745 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001746#else /* !HAVE_LSTAT */
1747 return posix_do_stat(self, args, stat);
1748#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749}
1750
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001751
Guido van Rossumb6775db1994-08-01 11:34:53 +00001752#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001753static char posix_readlink__doc__[] =
1754"readlink(path) -> path\n\
1755Return a string representing the path to which the symbolic link points.";
1756
Barry Warsaw53699e91996-12-10 23:23:01 +00001757static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001758posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001759 PyObject *self;
1760 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001761{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001762 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001763 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001764 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001765 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001766 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001767 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001768 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001769 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001770 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001771 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001772 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001773}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001774#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001776
Guido van Rossumb6775db1994-08-01 11:34:53 +00001777#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001778static char posix_symlink__doc__[] =
1779"symlink(src, dst) -> None\n\
1780Create a symbolic link.";
1781
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001782static PyObject *
1783posix_symlink(self, args)
1784 PyObject *self;
1785 PyObject *args;
1786{
1787 return posix_2str(args, symlink);
1788}
1789#endif /* HAVE_SYMLINK */
1790
1791
1792#ifdef HAVE_TIMES
1793#ifndef HZ
1794#define HZ 60 /* Universal constant :-) */
1795#endif /* HZ */
1796
Guido van Rossumd48f2521997-12-05 22:19:34 +00001797#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1798static long
1799system_uptime()
1800{
1801 ULONG value = 0;
1802
1803 Py_BEGIN_ALLOW_THREADS
1804 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1805 Py_END_ALLOW_THREADS
1806
1807 return value;
1808}
1809
1810static PyObject *
1811posix_times(self, args)
1812 PyObject *self;
1813 PyObject *args;
1814{
1815 if (!PyArg_NoArgs(args))
1816 return NULL;
1817
1818 /* Currently Only Uptime is Provided -- Others Later */
1819 return Py_BuildValue("ddddd",
1820 (double)0 /* t.tms_utime / HZ */,
1821 (double)0 /* t.tms_stime / HZ */,
1822 (double)0 /* t.tms_cutime / HZ */,
1823 (double)0 /* t.tms_cstime / HZ */,
1824 (double)system_uptime() / 1000);
1825}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001826#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00001827static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001828posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001829 PyObject *self;
1830 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001831{
1832 struct tms t;
1833 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001834 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001835 return NULL;
1836 errno = 0;
1837 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001838 if (c == (clock_t) -1)
1839 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001840 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001841 (double)t.tms_utime / HZ,
1842 (double)t.tms_stime / HZ,
1843 (double)t.tms_cutime / HZ,
1844 (double)t.tms_cstime / HZ,
1845 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001846}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001847#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001848#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001849
1850
Guido van Rossum87755a21996-09-07 00:59:43 +00001851#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001852#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001853static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001854posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001855 PyObject *self;
1856 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001857{
1858 FILETIME create, exit, kernel, user;
1859 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001860 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001861 return NULL;
1862 hProc = GetCurrentProcess();
1863 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001864 return Py_BuildValue(
1865 "ddddd",
1866 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1867 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1868 (double)0,
1869 (double)0,
1870 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001871}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001872#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001873
1874#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00001875static char posix_times__doc__[] =
1876"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1877Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001878#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001879
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001880
Guido van Rossumb6775db1994-08-01 11:34:53 +00001881#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001882static char posix_setsid__doc__[] =
1883"setsid() -> None\n\
1884Call the system call setsid().";
1885
Barry Warsaw53699e91996-12-10 23:23:01 +00001886static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001887posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001888 PyObject *self;
1889 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001890{
Barry Warsaw53699e91996-12-10 23:23:01 +00001891 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001892 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001893 if (setsid() < 0)
1894 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001895 Py_INCREF(Py_None);
1896 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001897}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001898#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001899
Guido van Rossumb6775db1994-08-01 11:34:53 +00001900#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001901static char posix_setpgid__doc__[] =
1902"setpgid(pid, pgrp) -> None\n\
1903Call the system call setpgid().";
1904
Barry Warsaw53699e91996-12-10 23:23:01 +00001905static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001906posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001907 PyObject *self;
1908 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001909{
1910 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001911 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001912 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001913 if (setpgid(pid, pgrp) < 0)
1914 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001915 Py_INCREF(Py_None);
1916 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001917}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001918#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001919
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001920
Guido van Rossumb6775db1994-08-01 11:34:53 +00001921#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001922static char posix_tcgetpgrp__doc__[] =
1923"tcgetpgrp(fd) -> pgid\n\
1924Return the process group associated with the terminal given by a fd.";
1925
Barry Warsaw53699e91996-12-10 23:23:01 +00001926static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001927posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001928 PyObject *self;
1929 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001930{
1931 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001932 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001933 return NULL;
1934 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001935 if (pgid < 0)
1936 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001937 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001938}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001939#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001940
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001941
Guido van Rossumb6775db1994-08-01 11:34:53 +00001942#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001943static char posix_tcsetpgrp__doc__[] =
1944"tcsetpgrp(fd, pgid) -> None\n\
1945Set the process group associated with the terminal given by a fd.";
1946
Barry Warsaw53699e91996-12-10 23:23:01 +00001947static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001948posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001949 PyObject *self;
1950 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001951{
1952 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001953 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001954 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001955 if (tcsetpgrp(fd, pgid) < 0)
1956 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00001957 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00001958 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001959}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001960#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001961
Guido van Rossum687dd131993-05-17 08:34:16 +00001962/* Functions acting on file descriptors */
1963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001964static char posix_open__doc__[] =
1965"open(filename, flag [, mode=0777]) -> fd\n\
1966Open a file (for low level IO).";
1967
Barry Warsaw53699e91996-12-10 23:23:01 +00001968static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001969posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001970 PyObject *self;
1971 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001972{
1973 char *file;
1974 int flag;
1975 int mode = 0777;
1976 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00001977 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
1978 return NULL;
1979
Barry Warsaw53699e91996-12-10 23:23:01 +00001980 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001981 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001982 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00001983 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001984 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00001985 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001986}
1987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001988
1989static char posix_close__doc__[] =
1990"close(fd) -> None\n\
1991Close a file descriptor (for low level IO).";
1992
Barry Warsaw53699e91996-12-10 23:23:01 +00001993static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00001994posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001995 PyObject *self;
1996 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00001997{
1998 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001999 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002000 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002001 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002002 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002003 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002004 if (res < 0)
2005 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002006 Py_INCREF(Py_None);
2007 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002008}
2009
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002010
2011static char posix_dup__doc__[] =
2012"dup(fd) -> fd2\n\
2013Return a duplicate of a file descriptor.";
2014
Barry Warsaw53699e91996-12-10 23:23:01 +00002015static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002016posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002017 PyObject *self;
2018 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002019{
2020 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002022 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002023 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002024 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002025 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002026 if (fd < 0)
2027 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002028 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002029}
2030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002031
2032static char posix_dup2__doc__[] =
2033"dup2(fd, fd2) -> None\n\
2034Duplicate file descriptor.";
2035
Barry Warsaw53699e91996-12-10 23:23:01 +00002036static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002037posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002038 PyObject *self;
2039 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002040{
2041 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002042 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002043 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002044 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002045 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002046 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002047 if (res < 0)
2048 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002049 Py_INCREF(Py_None);
2050 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002051}
2052
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002053
2054static char posix_lseek__doc__[] =
2055"lseek(fd, pos, how) -> newpos\n\
2056Set the current position of a file descriptor.";
2057
Barry Warsaw53699e91996-12-10 23:23:01 +00002058static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002059posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002060 PyObject *self;
2061 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002062{
2063 int fd, how;
2064 long pos, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002065 if (!PyArg_Parse(args, "(ili)", &fd, &pos, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002066 return NULL;
2067#ifdef SEEK_SET
2068 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2069 switch (how) {
2070 case 0: how = SEEK_SET; break;
2071 case 1: how = SEEK_CUR; break;
2072 case 2: how = SEEK_END; break;
2073 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002074#endif /* SEEK_END */
Barry Warsaw53699e91996-12-10 23:23:01 +00002075 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002076 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002077 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002078 if (res < 0)
2079 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002080 return PyInt_FromLong(res);
Guido van Rossum687dd131993-05-17 08:34:16 +00002081}
2082
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002083
2084static char posix_read__doc__[] =
2085"read(fd, buffersize) -> string\n\
2086Read a file descriptor.";
2087
Barry Warsaw53699e91996-12-10 23:23:01 +00002088static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002089posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002090 PyObject *self;
2091 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002092{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002093 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 PyObject *buffer;
2095 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002096 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002097 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002098 if (buffer == NULL)
2099 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002100 Py_BEGIN_ALLOW_THREADS
2101 n = read(fd, PyString_AsString(buffer), size);
2102 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002103 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002105 return posix_error();
2106 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002107 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002108 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002109 return buffer;
2110}
2111
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002112
2113static char posix_write__doc__[] =
2114"write(fd, string) -> byteswritten\n\
2115Write a string to a file descriptor.";
2116
Barry Warsaw53699e91996-12-10 23:23:01 +00002117static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002118posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002119 PyObject *self;
2120 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002121{
2122 int fd, size;
2123 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002124 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002125 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002126 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002127 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002128 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002129 if (size < 0)
2130 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002131 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002132}
2133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002134
2135static char posix_fstat__doc__[]=
2136"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2137Like stat(), but for an open file descriptor.";
2138
Barry Warsaw53699e91996-12-10 23:23:01 +00002139static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002140posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002141 PyObject *self;
2142 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002143{
2144 int fd;
2145 struct stat st;
2146 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002147 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002148 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002149 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002150 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002151 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002152 if (res != 0)
2153 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002154 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002155 (long)st.st_mode,
2156 (long)st.st_ino,
2157 (long)st.st_dev,
2158 (long)st.st_nlink,
2159 (long)st.st_uid,
2160 (long)st.st_gid,
2161 (long)st.st_size,
2162 (long)st.st_atime,
2163 (long)st.st_mtime,
2164 (long)st.st_ctime);
Guido van Rossum687dd131993-05-17 08:34:16 +00002165}
2166
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002167
2168static char posix_fdopen__doc__[] =
2169"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2170Return an open file object connected to a file descriptor.";
2171
Barry Warsaw53699e91996-12-10 23:23:01 +00002172static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002173posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002174 PyObject *self;
2175 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002176{
Barry Warsaw53699e91996-12-10 23:23:01 +00002177 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002178 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002179 char *mode = "r";
2180 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002181 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002182 PyObject *f;
2183 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002184 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002185
Barry Warsaw53699e91996-12-10 23:23:01 +00002186 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002187 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002188 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002189 if (fp == NULL)
2190 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002191 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002192 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002193 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002194 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002195}
2196
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002197
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002198#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002199static char posix_pipe__doc__[] =
2200"pipe() -> (read_end, write_end)\n\
2201Create a pipe.";
2202
Barry Warsaw53699e91996-12-10 23:23:01 +00002203static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002204posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002205 PyObject *self;
2206 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002207{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002208#if defined(PYOS_OS2)
2209 HFILE read, write;
2210 APIRET rc;
2211
2212 if (!PyArg_Parse(args, ""))
2213 return NULL;
2214
2215 Py_BEGIN_ALLOW_THREADS
2216 rc = DosCreatePipe( &read, &write, 4096);
2217 Py_END_ALLOW_THREADS
2218 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002219 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002220
2221 return Py_BuildValue("(ii)", read, write);
2222#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002223#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002224 int fds[2];
2225 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002226 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002227 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002229 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002230 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002231 if (res != 0)
2232 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002233 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002234#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002235 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002236 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002237 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002238 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002239 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002240 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002241 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002242 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002243 if (!ok)
2244 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002245 read_fd = _open_osfhandle((long)read, 0);
2246 write_fd = _open_osfhandle((long)write, 1);
2247 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002248#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002249#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002250}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002251#endif /* HAVE_PIPE */
2252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002253
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002254#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002255static char posix_mkfifo__doc__[] =
2256"mkfifo(file, [, mode=0666]) -> None\n\
2257Create a FIFO (a POSIX named pipe).";
2258
Barry Warsaw53699e91996-12-10 23:23:01 +00002259static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002260posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002261 PyObject *self;
2262 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002263{
2264 char *file;
2265 int mode = 0666;
2266 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002267 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002268 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002269 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002270 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002271 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002272 if (res < 0)
2273 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002274 Py_INCREF(Py_None);
2275 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002276}
2277#endif
2278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002279
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002280#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002281static char posix_ftruncate__doc__[] =
2282"ftruncate(fd, length) -> None\n\
2283Truncate a file to a specified length.";
2284
Barry Warsaw53699e91996-12-10 23:23:01 +00002285static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002286posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002287 PyObject *self; /* Not used */
2288 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002289{
2290 int fd;
2291 long length;
2292 int res;
2293
Barry Warsaw53699e91996-12-10 23:23:01 +00002294 if (!PyArg_Parse(args, "(il)", &fd, &length))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002295 return NULL;
2296
Barry Warsaw53699e91996-12-10 23:23:01 +00002297 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002298 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002299 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002300 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002301 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002302 return NULL;
2303 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002304 Py_INCREF(Py_None);
2305 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002306}
2307#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002308
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002309#ifdef NeXT
2310#define HAVE_PUTENV
2311/* Steve Spicklemire got this putenv from NeXTAnswers */
2312static int
2313putenv(char *newval)
2314{
2315 extern char **environ;
2316
2317 static int firstTime = 1;
2318 char **ep;
2319 char *cp;
2320 int esiz;
2321 char *np;
2322
2323 if (!(np = strchr(newval, '=')))
2324 return 1;
2325 *np = '\0';
2326
2327 /* look it up */
2328 for (ep=environ ; *ep ; ep++)
2329 {
2330 /* this should always be true... */
2331 if (cp = strchr(*ep, '='))
2332 {
2333 *cp = '\0';
2334 if (!strcmp(*ep, newval))
2335 {
2336 /* got it! */
2337 *cp = '=';
2338 break;
2339 }
2340 *cp = '=';
2341 }
2342 else
2343 {
2344 *np = '=';
2345 return 1;
2346 }
2347 }
2348
2349 *np = '=';
2350 if (*ep)
2351 {
2352 /* the string was already there:
2353 just replace it with the new one */
2354 *ep = newval;
2355 return 0;
2356 }
2357
2358 /* expand environ by one */
2359 for (esiz=2, ep=environ ; *ep ; ep++)
2360 esiz++;
2361 if (firstTime)
2362 {
2363 char **epp;
2364 char **newenv;
2365 if (!(newenv = malloc(esiz * sizeof(char *))))
2366 return 1;
2367
2368 for (ep=environ, epp=newenv ; *ep ;)
2369 *epp++ = *ep++;
2370 *epp++ = newval;
2371 *epp = (char *) 0;
2372 environ = newenv;
2373 }
2374 else
2375 {
2376 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2377 return 1;
2378 environ[esiz - 2] = newval;
2379 environ[esiz - 1] = (char *) 0;
2380 firstTime = 0;
2381 }
2382
2383 return 0;
2384}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002385#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002386
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002387
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002388#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002389static char posix_putenv__doc__[] =
2390"putenv(key, value) -> None\n\
2391Change or add an environment variable.";
2392
Guido van Rossumbcc20741998-08-04 22:53:56 +00002393#ifdef __BEOS__
2394/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2395int putenv( const char *str );
2396#endif
2397
Barry Warsaw53699e91996-12-10 23:23:01 +00002398static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002399posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002400 PyObject *self;
2401 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002402{
2403 char *s1, *s2;
2404 char *new;
2405
Barry Warsaw53699e91996-12-10 23:23:01 +00002406 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002407 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002408
2409#if defined(PYOS_OS2)
2410 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2411 APIRET rc;
2412
2413 if (strlen(s2) == 0) /* If New Value is an Empty String */
2414 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2415
2416 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2417 if (rc != NO_ERROR)
2418 return os2_error(rc);
2419
2420 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2421 APIRET rc;
2422
2423 if (strlen(s2) == 0) /* If New Value is an Empty String */
2424 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2425
2426 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2427 if (rc != NO_ERROR)
2428 return os2_error(rc);
2429 } else {
2430#endif
2431
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002432 /* XXX This leaks memory -- not easy to fix :-( */
2433 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002434 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002435 (void) sprintf(new, "%s=%s", s1, s2);
2436 if (putenv(new)) {
2437 posix_error();
2438 return NULL;
2439 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002440
2441#if defined(PYOS_OS2)
2442 }
2443#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002444 Py_INCREF(Py_None);
2445 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002446}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002447#endif /* putenv */
2448
2449#ifdef HAVE_STRERROR
2450static char posix_strerror__doc__[] =
2451"strerror(code) -> string\n\
2452Translate an error code to a message string.";
2453
2454PyObject *
2455posix_strerror(self, args)
2456 PyObject *self;
2457 PyObject *args;
2458{
2459 int code;
2460 char *message;
2461 if (!PyArg_ParseTuple(args, "i", &code))
2462 return NULL;
2463 message = strerror(code);
2464 if (message == NULL) {
2465 PyErr_SetString(PyExc_ValueError,
2466 "strerror code out of range");
2467 return NULL;
2468 }
2469 return PyString_FromString(message);
2470}
2471#endif /* strerror */
2472
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002473
Guido van Rossumc9641791998-08-04 15:26:23 +00002474#ifdef HAVE_SYS_WAIT_H
2475
2476#ifdef WIFSTOPPED
2477static char posix_WIFSTOPPED__doc__[] =
2478"WIFSTOPPED(status) -> Boolean\n\
2479See Unix documentation.";
2480
2481static PyObject *
2482posix_WIFSTOPPED(self, args)
2483 PyObject *self;
2484 PyObject *args;
2485{
2486 int status = 0;
2487
2488 if (!PyArg_Parse(args, "i", &status))
2489 {
2490 return NULL;
2491 }
2492
2493 return Py_BuildValue("i", WIFSTOPPED(status));
2494}
2495#endif /* WIFSTOPPED */
2496
2497#ifdef WIFSIGNALED
2498static char posix_WIFSIGNALED__doc__[] =
2499"WIFSIGNALED(status) -> Boolean\n\
2500See Unix documentation.";
2501
2502static PyObject *
2503posix_WIFSIGNALED(self, args)
2504 PyObject *self;
2505 PyObject *args;
2506{
2507 int status = 0;
2508
2509 if (!PyArg_Parse(args, "i", &status))
2510 {
2511 return NULL;
2512 }
2513
2514 return Py_BuildValue("i", WIFSIGNALED(status));
2515}
2516#endif /* WIFSIGNALED */
2517
2518#ifdef WIFEXITED
2519static char posix_WIFEXITED__doc__[] =
2520"WIFEXITED(status) -> Boolean\n\
2521See Unix documentation.";
2522
2523static PyObject *
2524posix_WIFEXITED(self, args)
2525 PyObject *self;
2526 PyObject *args;
2527{
2528 int status = 0;
2529
2530 if (!PyArg_Parse(args, "i", &status))
2531 {
2532 return NULL;
2533 }
2534
2535 return Py_BuildValue("i", WIFEXITED(status));
2536}
2537#endif /* WIFEXITED */
2538
2539#ifdef WIFSTOPPED
2540static char posix_WEXITSTATUS__doc__[] =
2541"WEXITSTATUS(status) -> integer\n\
2542See Unix documentation.";
2543
2544static PyObject *
2545posix_WEXITSTATUS(self, args)
2546 PyObject *self;
2547 PyObject *args;
2548{
2549 int status = 0;
2550
2551 if (!PyArg_Parse(args, "i", &status))
2552 {
2553 return NULL;
2554 }
2555
2556 return Py_BuildValue("i", WEXITSTATUS(status));
2557}
2558#endif /* WEXITSTATUS */
2559
2560#ifdef WTERMSIG
2561static char posix_WTERMSIG__doc__[] =
2562"WTERMSIG(status) -> integer\n\
2563See Unix documentation.";
2564
2565static PyObject *
2566posix_WTERMSIG(self, args)
2567 PyObject *self;
2568 PyObject *args;
2569{
2570 int status = 0;
2571
2572 if (!PyArg_Parse(args, "i", &status))
2573 {
2574 return NULL;
2575 }
2576
2577 return Py_BuildValue("i", WTERMSIG(status));
2578}
2579#endif /* WTERMSIG */
2580
2581#ifdef WSTOPSIG
2582static char posix_WSTOPSIG__doc__[] =
2583"WSTOPSIG(status) -> integer\n\
2584See Unix documentation.";
2585
2586static PyObject *
2587posix_WSTOPSIG(self, args)
2588 PyObject *self;
2589 PyObject *args;
2590{
2591 int status = 0;
2592
2593 if (!PyArg_Parse(args, "i", &status))
2594 {
2595 return NULL;
2596 }
2597
2598 return Py_BuildValue("i", WSTOPSIG(status));
2599}
2600#endif /* WSTOPSIG */
2601
2602#endif /* HAVE_SYS_WAIT_H */
2603
2604
Barry Warsaw53699e91996-12-10 23:23:01 +00002605static PyMethodDef posix_methods[] = {
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002606 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2607 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002608#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002609 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002610#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002611#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002612 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002613#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002614#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002615 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002616#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002617 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2618 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2619 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002620#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002621 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002622#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002623#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002624 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002625#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002626 {"rename", posix_rename, 0, posix_rename__doc__},
2627 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2628 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002629#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002630 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002631#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002632#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002633 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002634#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002635 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002636#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002637 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002638#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002639 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2640 {"remove", posix_unlink, 0, posix_remove__doc__},
2641 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002642#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002643 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002644#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002645 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002646#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002647 {"execv", posix_execv, 0, posix_execv__doc__},
2648 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002649#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002650#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002651 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002652#endif /* HAVE_FORK */
2653#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002654 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002655#endif /* HAVE_GETEGID */
2656#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002657 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002658#endif /* HAVE_GETEUID */
2659#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002660 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002661#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002662 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002663#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002664 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002665#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002666#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002667 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002668#endif /* HAVE_GETPPID */
2669#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002670 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002671#endif /* HAVE_GETUID */
2672#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002673 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002674#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002675#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002676 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002677#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002678#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002679 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002680#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002681#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002682 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002683#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002684#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002685 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002686#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002687#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002688 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002689#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002690#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002691 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002692#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002693#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002694 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002695#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002696#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002697 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002698#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002699#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002700 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002701#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002702#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002703 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002704#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002705#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002706 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002707#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002708 {"open", posix_open, 1, posix_open__doc__},
2709 {"close", posix_close, 0, posix_close__doc__},
2710 {"dup", posix_dup, 0, posix_dup__doc__},
2711 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2712 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2713 {"read", posix_read, 0, posix_read__doc__},
2714 {"write", posix_write, 0, posix_write__doc__},
2715 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2716 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002717#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002718 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002719#endif
2720#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002721 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002722#endif
2723#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002724 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002725#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002726#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002727 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002728#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002729#ifdef HAVE_STRERROR
2730 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2731#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00002732#ifdef HAVE_SYS_WAIT_H
2733#ifdef WIFSTOPPED
2734 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
2735#endif /* WIFSTOPPED */
2736#ifdef WIFSIGNALED
2737 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
2738#endif /* WIFSIGNALED */
2739#ifdef WIFEXITED
2740 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
2741#endif /* WIFEXITED */
2742#ifdef WEXITSTATUS
2743 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
2744#endif /* WEXITSTATUS */
2745#ifdef WTERMSIG
2746 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
2747#endif /* WTERMSIG */
2748#ifdef WSTOPSIG
2749 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
2750#endif /* WSTOPSIG */
2751#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002752 {NULL, NULL} /* Sentinel */
2753};
2754
2755
Barry Warsaw4a342091996-12-19 23:50:02 +00002756static int
2757ins(d, symbol, value)
2758 PyObject* d;
2759 char* symbol;
2760 long value;
2761{
2762 PyObject* v = PyInt_FromLong(value);
2763 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2764 return -1; /* triggers fatal error */
2765
2766 Py_DECREF(v);
2767 return 0;
2768}
2769
Guido van Rossumd48f2521997-12-05 22:19:34 +00002770#if defined(PYOS_OS2)
2771/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
2772static int insertvalues(PyObject *d)
2773{
2774 APIRET rc;
2775 ULONG values[QSV_MAX+1];
2776 PyObject *v;
2777 char *ver, tmp[10];
2778
2779 Py_BEGIN_ALLOW_THREADS
2780 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
2781 Py_END_ALLOW_THREADS
2782
2783 if (rc != NO_ERROR) {
2784 os2_error(rc);
2785 return -1;
2786 }
2787
2788 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
2789 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
2790 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
2791 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
2792 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
2793 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
2794 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
2795
2796 switch (values[QSV_VERSION_MINOR]) {
2797 case 0: ver = "2.00"; break;
2798 case 10: ver = "2.10"; break;
2799 case 11: ver = "2.11"; break;
2800 case 30: ver = "3.00"; break;
2801 case 40: ver = "4.00"; break;
2802 case 50: ver = "5.00"; break;
2803 default:
2804 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
2805 values[QSV_VERSION_MINOR]);
2806 ver = &tmp[0];
2807 }
2808
2809 /* Add Indicator of the Version of the Operating System */
2810 v = PyString_FromString(ver);
2811 if (!v || PyDict_SetItemString(d, "version", v) < 0)
2812 return -1;
2813 Py_DECREF(v);
2814
2815 /* Add Indicator of Which Drive was Used to Boot the System */
2816 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
2817 tmp[1] = ':';
2818 tmp[2] = '\0';
2819
2820 v = PyString_FromString(tmp);
2821 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
2822 return -1;
2823 Py_DECREF(v);
2824
2825 return 0;
2826}
2827#endif
2828
Barry Warsaw4a342091996-12-19 23:50:02 +00002829static int
2830all_ins(d)
2831 PyObject* d;
2832{
2833#ifdef WNOHANG
2834 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
2835#endif
2836#ifdef O_RDONLY
2837 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
2838#endif
2839#ifdef O_WRONLY
2840 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
2841#endif
2842#ifdef O_RDWR
2843 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
2844#endif
2845#ifdef O_NDELAY
2846 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
2847#endif
2848#ifdef O_NONBLOCK
2849 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
2850#endif
2851#ifdef O_APPEND
2852 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
2853#endif
2854#ifdef O_DSYNC
2855 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
2856#endif
2857#ifdef O_RSYNC
2858 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
2859#endif
2860#ifdef O_SYNC
2861 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
2862#endif
2863#ifdef O_NOCTTY
2864 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
2865#endif
2866#ifdef O_CREAT
2867 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
2868#endif
2869#ifdef O_EXCL
2870 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
2871#endif
2872#ifdef O_TRUNC
2873 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
2874#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00002875#ifdef O_BINARY
2876 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
2877#endif
2878#ifdef O_TEXT
2879 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
2880#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00002881
2882#if defined(PYOS_OS2)
2883 if (insertvalues(d)) return -1;
2884#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00002885 return 0;
2886}
2887
2888
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002889#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002890#define INITFUNC initnt
2891#define MODNAME "nt"
2892#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002893#if defined(PYOS_OS2)
2894#define INITFUNC initos2
2895#define MODNAME "os2"
2896#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002897#define INITFUNC initposix
2898#define MODNAME "posix"
2899#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002900#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002901
Guido van Rossum3886bb61998-12-04 18:50:17 +00002902DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002903INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00002904{
Barry Warsaw53699e91996-12-10 23:23:01 +00002905 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002906
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002907 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002908 posix_methods,
2909 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002910 (PyObject *)NULL,
2911 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00002912 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002913
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002914 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002915 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00002916 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00002917 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00002918 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002919
Barry Warsaw4a342091996-12-19 23:50:02 +00002920 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00002921 return;
2922
Barry Warsawd58d7641998-07-23 16:14:40 +00002923 Py_INCREF(PyExc_OSError);
2924 PosixError = PyExc_OSError;
2925 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002926}