blob: dd3cb721d2b6711d6d4833d57b6563b2bc0a2e1c [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);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000494#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000495 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000496 (long)st.st_mode,
497 (long)st.st_ino,
498 (long)st.st_dev,
499 (long)st.st_nlink,
500 (long)st.st_uid,
501 (long)st.st_gid,
502 (long)st.st_size,
503 (long)st.st_atime,
504 (long)st.st_mtime,
505 (long)st.st_ctime);
506#else
507 return Py_BuildValue("(lLllllLlll)",
508 (long)st.st_mode,
509 (LONG_LONG)st.st_ino,
510 (long)st.st_dev,
511 (long)st.st_nlink,
512 (long)st.st_uid,
513 (long)st.st_gid,
514 (LONG_LONG)st.st_size,
515 (long)st.st_atime,
516 (long)st.st_mtime,
517 (long)st.st_ctime);
518#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519}
520
521
522/* POSIX methods */
523
Guido van Rossum94f6f721999-01-06 18:42:14 +0000524static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000525"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000526Test for access to a file.";
527
528static PyObject *
529posix_access(self, args)
530 PyObject *self;
531 PyObject *args;
532{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000533 char *path;
534 int mode;
535 int res;
536
537 if (!PyArg_Parse(args, "(si)", &path, &mode))
538 return NULL;
539 Py_BEGIN_ALLOW_THREADS
540 res = access(path, mode);
541 Py_END_ALLOW_THREADS
542 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000543}
544
545static char posix_ttyname__doc__[] =
546"ttyname(fd, mode) -> String\n\
547Return the name of the terminal device connected to 'fd'.";
548
549static PyObject *
550posix_ttyname(self, args)
551 PyObject *self;
552 PyObject *args;
553{
554 PyObject *file;
555 int id;
556 char *ret;
557
Guido van Rossum94f6f721999-01-06 18:42:14 +0000558 if (!PyArg_Parse(args, "i", &id))
559 return NULL;
560
Guido van Rossum94f6f721999-01-06 18:42:14 +0000561 ret = ttyname(id);
562 if (ret == NULL)
563 return(posix_error());
564 return(PyString_FromString(ret));
565}
566
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000567static char posix_chdir__doc__[] =
568"chdir(path) -> None\n\
569Change the current working directory to the specified path.";
570
Barry Warsaw53699e91996-12-10 23:23:01 +0000571static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000573 PyObject *self;
574 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576 return posix_1str(args, chdir);
577}
578
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000579
580static char posix_chmod__doc__[] =
581"chmod(path, mode) -> None\n\
582Change the access permissions of a file.";
583
Barry Warsaw53699e91996-12-10 23:23:01 +0000584static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000586 PyObject *self;
587 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589 return posix_strint(args, chmod);
590}
591
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000592
Guido van Rossumb6775db1994-08-01 11:34:53 +0000593#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000594static char posix_chown__doc__[] =
595"chown(path, uid, gid) -> None\n\
596Change the owner and group id of path to the numeric uid and gid.";
597
Barry Warsaw53699e91996-12-10 23:23:01 +0000598static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000599posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000600 PyObject *self;
601 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000602{
603 return posix_strintint(args, chown);
604}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000605#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000606
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000607
Guido van Rossum36bc6801995-06-14 22:54:23 +0000608#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000609static char posix_getcwd__doc__[] =
610"getcwd() -> path\n\
611Return a string representing the current working directory.";
612
Barry Warsaw53699e91996-12-10 23:23:01 +0000613static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000615 PyObject *self;
616 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617{
618 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000619 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000620 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000622 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000623 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000624 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000625 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000627 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000629#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000631
Guido van Rossumb6775db1994-08-01 11:34:53 +0000632#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000633static char posix_link__doc__[] =
634"link(src, dst) -> None\n\
635Create a hard link to a file.";
636
Barry Warsaw53699e91996-12-10 23:23:01 +0000637static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000638posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000639 PyObject *self;
640 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642 return posix_2str(args, link);
643}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000644#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000646
647static char posix_listdir__doc__[] =
648"listdir(path) -> list_of_strings\n\
649Return a list containing the names of the entries in the directory.\n\
650\n\
651 path: path of directory to list\n\
652\n\
653The list is in arbitrary order. It does not include the special\n\
654entries '.' and '..' even if they are present in the directory.";
655
Barry Warsaw53699e91996-12-10 23:23:01 +0000656static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000657posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000658 PyObject *self;
659 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000661 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000662 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000663#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000664
Guido van Rossumb6775db1994-08-01 11:34:53 +0000665 char *name;
666 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000667 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000668 HANDLE hFindFile;
669 WIN32_FIND_DATA FileData;
670 char namebuf[MAX_PATH+5];
671
Guido van Rossum7e488981998-10-08 02:25:24 +0000672 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000673 return NULL;
674 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000675 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000676 return NULL;
677 }
678 strcpy(namebuf, name);
679 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
680 namebuf[len++] = '/';
681 strcpy(namebuf + len, "*.*");
682
Barry Warsaw53699e91996-12-10 23:23:01 +0000683 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000684 return NULL;
685
686 hFindFile = FindFirstFile(namebuf, &FileData);
687 if (hFindFile == INVALID_HANDLE_VALUE) {
688 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000689 if (errno == ERROR_FILE_NOT_FOUND)
690 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000691 return posix_error();
692 }
693 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000694 if (FileData.cFileName[0] == '.' &&
695 (FileData.cFileName[1] == '\0' ||
696 FileData.cFileName[1] == '.' &&
697 FileData.cFileName[2] == '\0'))
698 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000699 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000700 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000701 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000702 d = NULL;
703 break;
704 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000705 if (PyList_Append(d, v) != 0) {
706 Py_DECREF(v);
707 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000708 d = NULL;
709 break;
710 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000711 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000712 } while (FindNextFile(hFindFile, &FileData) == TRUE);
713
714 if (FindClose(hFindFile) == FALSE) {
715 errno = GetLastError();
716 return posix_error();
717 }
718
719 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000720
Guido van Rossum8d665e61996-06-26 18:22:49 +0000721#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000722#ifdef _MSC_VER /* 16-bit Windows */
723
724#ifndef MAX_PATH
725#define MAX_PATH 250
726#endif
727 char *name, *pt;
728 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000729 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000730 char namebuf[MAX_PATH+5];
731 struct _find_t ep;
732
Guido van Rossum7e488981998-10-08 02:25:24 +0000733 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000734 return NULL;
735 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000736 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000737 return NULL;
738 }
739 strcpy(namebuf, name);
740 for (pt = namebuf; *pt; pt++)
741 if (*pt == '/')
742 *pt = '\\';
743 if (namebuf[len-1] != '\\')
744 namebuf[len++] = '\\';
745 strcpy(namebuf + len, "*.*");
746
Barry Warsaw53699e91996-12-10 23:23:01 +0000747 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000748 return NULL;
749
750 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000751 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
752 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000753 errno = ENOENT;
754 return posix_error();
755 }
756 do {
757 if (ep.name[0] == '.' &&
758 (ep.name[1] == '\0' ||
759 ep.name[1] == '.' &&
760 ep.name[2] == '\0'))
761 continue;
762 strcpy(namebuf, ep.name);
763 for (pt = namebuf; *pt; pt++)
764 if (isupper(*pt))
765 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000766 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000767 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000768 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000769 d = NULL;
770 break;
771 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000772 if (PyList_Append(d, v) != 0) {
773 Py_DECREF(v);
774 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000775 d = NULL;
776 break;
777 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000778 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000779 } while (_dos_findnext(&ep) == 0);
780
781 return d;
782
783#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000784#if defined(PYOS_OS2)
785
786#ifndef MAX_PATH
787#define MAX_PATH CCHMAXPATH
788#endif
789 char *name, *pt;
790 int len;
791 PyObject *d, *v;
792 char namebuf[MAX_PATH+5];
793 HDIR hdir = 1;
794 ULONG srchcnt = 1;
795 FILEFINDBUF3 ep;
796 APIRET rc;
797
Guido van Rossum7e488981998-10-08 02:25:24 +0000798 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000799 return NULL;
800 if (len >= MAX_PATH) {
801 PyErr_SetString(PyExc_ValueError, "path too long");
802 return NULL;
803 }
804 strcpy(namebuf, name);
805 for (pt = namebuf; *pt; pt++)
806 if (*pt == '/')
807 *pt = '\\';
808 if (namebuf[len-1] != '\\')
809 namebuf[len++] = '\\';
810 strcpy(namebuf + len, "*.*");
811
812 if ((d = PyList_New(0)) == NULL)
813 return NULL;
814
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000815 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
816 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000817 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000818 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
819 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
820 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000821
822 if (rc != NO_ERROR) {
823 errno = ENOENT;
824 return posix_error();
825 }
826
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000827 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000828 do {
829 if (ep.achName[0] == '.'
830 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000831 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000832
833 strcpy(namebuf, ep.achName);
834
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000835 /* Leave Case of Name Alone -- In Native Form */
836 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000837
838 v = PyString_FromString(namebuf);
839 if (v == NULL) {
840 Py_DECREF(d);
841 d = NULL;
842 break;
843 }
844 if (PyList_Append(d, v) != 0) {
845 Py_DECREF(v);
846 Py_DECREF(d);
847 d = NULL;
848 break;
849 }
850 Py_DECREF(v);
851 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
852 }
853
854 return d;
855#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000856
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000857 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000858 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000860 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000861 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000863 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000864 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000865 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000867 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000868 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000870 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000871 return NULL;
872 }
873 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000874 if (ep->d_name[0] == '.' &&
875 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000876 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000877 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000878 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000879 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000880 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000881 d = NULL;
882 break;
883 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000884 if (PyList_Append(d, v) != 0) {
885 Py_DECREF(v);
886 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000887 d = NULL;
888 break;
889 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000890 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891 }
892 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000893 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000894
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000896
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000897#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000898#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000899#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900}
901
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000902static char posix_mkdir__doc__[] =
903"mkdir(path [, mode=0777]) -> None\n\
904Create a directory.";
905
Barry Warsaw53699e91996-12-10 23:23:01 +0000906static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000907posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000908 PyObject *self;
909 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000910{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000911 int res;
912 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000913 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000914 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000915 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000916 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000917#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000918 res = mkdir(path);
919#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000920 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000921#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000922 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000923 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000924 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000925 Py_INCREF(Py_None);
926 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000927}
928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000929
Guido van Rossumb6775db1994-08-01 11:34:53 +0000930#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000931static char posix_nice__doc__[] =
932"nice(inc) -> new_priority\n\
933Decrease the priority of process and return new priority.";
934
Barry Warsaw53699e91996-12-10 23:23:01 +0000935static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000936posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000937 PyObject *self;
938 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000939{
940 int increment, value;
941
Barry Warsaw53699e91996-12-10 23:23:01 +0000942 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000943 return NULL;
944 value = nice(increment);
945 if (value == -1)
946 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000947 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000948}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000949#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000950
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000951
952static char posix_rename__doc__[] =
953"rename(old, new) -> None\n\
954Rename a file or directory.";
955
Barry Warsaw53699e91996-12-10 23:23:01 +0000956static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000957posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000958 PyObject *self;
959 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000960{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000961 return posix_2str(args, rename);
962}
963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000964
965static char posix_rmdir__doc__[] =
966"rmdir(path) -> None\n\
967Remove a directory.";
968
Barry Warsaw53699e91996-12-10 23:23:01 +0000969static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000970posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000971 PyObject *self;
972 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000973{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000974 return posix_1str(args, rmdir);
975}
976
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000977
978static char posix_stat__doc__[] =
979"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
980Perform a stat system call on the given path.";
981
Barry Warsaw53699e91996-12-10 23:23:01 +0000982static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000983posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000984 PyObject *self;
985 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000987 return posix_do_stat(self, args, stat);
988}
989
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000990
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000991#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000992static char posix_system__doc__[] =
993"system(command) -> exit_status\n\
994Execute the command (a string) in a subshell.";
995
Barry Warsaw53699e91996-12-10 23:23:01 +0000996static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000997posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000998 PyObject *self;
999 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001001 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001002 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001003 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001004 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001005 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001006 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001007 Py_END_ALLOW_THREADS
1008 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001009}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001010#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001012
1013static char posix_umask__doc__[] =
1014"umask(new_mask) -> old_mask\n\
1015Set the current numeric umask and return the previous umask.";
1016
Barry Warsaw53699e91996-12-10 23:23:01 +00001017static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001018posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001019 PyObject *self;
1020 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001021{
1022 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001023 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001024 return NULL;
1025 i = umask(i);
1026 if (i < 0)
1027 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001028 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001029}
1030
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001031
1032static char posix_unlink__doc__[] =
1033"unlink(path) -> None\n\
1034Remove a file (same as remove(path)).";
1035
1036static char posix_remove__doc__[] =
1037"remove(path) -> None\n\
1038Remove a file (same as unlink(path)).";
1039
Barry Warsaw53699e91996-12-10 23:23:01 +00001040static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001041posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001042 PyObject *self;
1043 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001044{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001045 return posix_1str(args, unlink);
1046}
1047
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001048
Guido van Rossumb6775db1994-08-01 11:34:53 +00001049#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001050static char posix_uname__doc__[] =
1051"uname() -> (sysname, nodename, release, version, machine)\n\
1052Return a tuple identifying the current operating system.";
1053
Barry Warsaw53699e91996-12-10 23:23:01 +00001054static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001055posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001056 PyObject *self;
1057 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001058{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001059 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001060 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001061 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001062 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001063 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001064 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001065 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001066 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001067 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001068 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001069 u.sysname,
1070 u.nodename,
1071 u.release,
1072 u.version,
1073 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001074}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001075#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001076
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001077
1078static char posix_utime__doc__[] =
1079"utime(path, (atime, utime)) -> None\n\
1080Set the access and modified time of the file to the given values.";
1081
Barry Warsaw53699e91996-12-10 23:23:01 +00001082static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001083posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001084 PyObject *self;
1085 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001086{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001087 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001088 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001089 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001090
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001091/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001092#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001093 struct utimbuf buf;
1094#define ATIME buf.actime
1095#define MTIME buf.modtime
1096#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001097#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001098 time_t buf[2];
1099#define ATIME buf[0]
1100#define MTIME buf[1]
1101#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001102#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001103
Barry Warsaw53699e91996-12-10 23:23:01 +00001104 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001106 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001107 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001108 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001109 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001110 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001111 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001112 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001113 Py_INCREF(Py_None);
1114 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001115#undef UTIME_ARG
1116#undef ATIME
1117#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001118}
1119
Guido van Rossum85e3b011991-06-03 12:42:10 +00001120
Guido van Rossum3b066191991-06-04 19:40:25 +00001121/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001123static char posix__exit__doc__[] =
1124"_exit(status)\n\
1125Exit to the system with specified status, without normal exit processing.";
1126
Barry Warsaw53699e91996-12-10 23:23:01 +00001127static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001128posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001129 PyObject *self;
1130 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001131{
1132 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001133 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001134 return NULL;
1135 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001136 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001137}
1138
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001139
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001140#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001141static char posix_execv__doc__[] =
1142"execv(path, args)\n\
1143Execute an executable path with arguments, replacing current process.\n\
1144\n\
1145 path: path of executable file\n\
1146 args: tuple or list of strings";
1147
Barry Warsaw53699e91996-12-10 23:23:01 +00001148static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001149posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001150 PyObject *self;
1151 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001152{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001153 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001154 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001155 char **argvlist;
1156 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001157 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001158
Guido van Rossum89b33251993-10-22 14:26:06 +00001159 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001160 argv is a list or tuple of strings. */
1161
Barry Warsaw53699e91996-12-10 23:23:01 +00001162 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001163 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001164 if (PyList_Check(argv)) {
1165 argc = PyList_Size(argv);
1166 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001167 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001168 else if (PyTuple_Check(argv)) {
1169 argc = PyTuple_Size(argv);
1170 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001171 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001172 else {
1173 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001174 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001175 return NULL;
1176 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001177
Barry Warsaw53699e91996-12-10 23:23:01 +00001178 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001179 if (argvlist == NULL)
1180 return NULL;
1181 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001182 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1183 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001184 goto badarg;
1185 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001186 }
1187 argvlist[argc] = NULL;
1188
Guido van Rossumb6775db1994-08-01 11:34:53 +00001189#ifdef BAD_EXEC_PROTOTYPES
1190 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001191#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001192 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001193#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001194
Guido van Rossum85e3b011991-06-03 12:42:10 +00001195 /* If we get here it's definitely an error */
1196
Barry Warsaw53699e91996-12-10 23:23:01 +00001197 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001198 return posix_error();
1199}
1200
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001201
1202static char posix_execve__doc__[] =
1203"execve(path, args, env)\n\
1204Execute a path with arguments and environment, replacing current process.\n\
1205\n\
1206 path: path of executable file\n\
1207 args: tuple or list of arguments\n\
1208 env: dictonary of strings mapping to strings";
1209
Barry Warsaw53699e91996-12-10 23:23:01 +00001210static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001211posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001212 PyObject *self;
1213 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001214{
1215 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001216 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001217 char **argvlist;
1218 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001219 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001220 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001221 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001222
1223 /* execve has three arguments: (path, argv, env), where
1224 argv is a list or tuple of strings and env is a dictionary
1225 like posix.environ. */
1226
Barry Warsaw53699e91996-12-10 23:23:01 +00001227 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001228 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001229 if (PyList_Check(argv)) {
1230 argc = PyList_Size(argv);
1231 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001232 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001233 else if (PyTuple_Check(argv)) {
1234 argc = PyTuple_Size(argv);
1235 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001236 }
1237 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001238 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001239 return NULL;
1240 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001241 if (!PyMapping_Check(env)) {
1242 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001243 return NULL;
1244 }
1245
Barry Warsaw53699e91996-12-10 23:23:01 +00001246 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001247 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001248 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001249 return NULL;
1250 }
1251 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001252 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001253 "s;argv must be list of strings",
1254 &argvlist[i]))
1255 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001256 goto fail_1;
1257 }
1258 }
1259 argvlist[argc] = NULL;
1260
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001261 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001262 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001263 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001265 goto fail_1;
1266 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001267 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001268 keys = PyMapping_Keys(env);
1269 vals = PyMapping_Values(env);
1270 if (!keys || !vals)
1271 goto fail_2;
1272
1273 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001274 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001275
1276 key = PyList_GetItem(keys, pos);
1277 val = PyList_GetItem(vals, pos);
1278 if (!key || !val)
1279 goto fail_2;
1280
Barry Warsaw53699e91996-12-10 23:23:01 +00001281 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001282 !PyArg_Parse(val, "s;non-string value in env", &v))
1283 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001284 goto fail_2;
1285 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001286
1287#if defined(PYOS_OS2)
1288 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1289 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1290#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001291 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001292 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001293 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001294 goto fail_2;
1295 }
1296 sprintf(p, "%s=%s", k, v);
1297 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001298#if defined(PYOS_OS2)
1299 }
1300#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001301 }
1302 envlist[envc] = 0;
1303
Guido van Rossumb6775db1994-08-01 11:34:53 +00001304
1305#ifdef BAD_EXEC_PROTOTYPES
1306 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001307#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001308 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001309#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001310
1311 /* If we get here it's definitely an error */
1312
1313 (void) posix_error();
1314
1315 fail_2:
1316 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001317 PyMem_DEL(envlist[envc]);
1318 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001319 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001320 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001321 Py_XDECREF(vals);
1322 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001323 return NULL;
1324}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001325#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001326
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001327
Guido van Rossumad0ee831995-03-01 10:34:45 +00001328#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001329static char posix_fork__doc__[] =
1330"fork() -> pid\n\
1331Fork a child process.\n\
1332\n\
1333Return 0 to child process and PID of child to parent process.";
1334
Barry Warsaw53699e91996-12-10 23:23:01 +00001335static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001336posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001337 PyObject *self;
1338 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001339{
1340 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001341 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001342 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001343 pid = fork();
1344 if (pid == -1)
1345 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001346 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001347 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001348}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001349#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001350
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001351
Guido van Rossumad0ee831995-03-01 10:34:45 +00001352#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001353static char posix_getegid__doc__[] =
1354"getegid() -> egid\n\
1355Return the current process's effective group id.";
1356
Barry Warsaw53699e91996-12-10 23:23:01 +00001357static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001358posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001359 PyObject *self;
1360 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001361{
Barry Warsaw53699e91996-12-10 23:23:01 +00001362 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001363 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001364 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001365}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001366#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001367
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001368
Guido van Rossumad0ee831995-03-01 10:34:45 +00001369#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001370static char posix_geteuid__doc__[] =
1371"geteuid() -> euid\n\
1372Return the current process's effective user id.";
1373
Barry Warsaw53699e91996-12-10 23:23:01 +00001374static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001375posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001376 PyObject *self;
1377 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001378{
Barry Warsaw53699e91996-12-10 23:23:01 +00001379 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001380 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001381 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001382}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001383#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001384
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001385
Guido van Rossumad0ee831995-03-01 10:34:45 +00001386#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001387static char posix_getgid__doc__[] =
1388"getgid() -> gid\n\
1389Return the current process's group id.";
1390
Barry Warsaw53699e91996-12-10 23:23:01 +00001391static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001392posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001393 PyObject *self;
1394 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001395{
Barry Warsaw53699e91996-12-10 23:23:01 +00001396 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001397 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001398 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001399}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001400#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001401
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001402
1403static char posix_getpid__doc__[] =
1404"getpid() -> pid\n\
1405Return the current process id";
1406
Barry Warsaw53699e91996-12-10 23:23:01 +00001407static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001408posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001409 PyObject *self;
1410 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001411{
Barry Warsaw53699e91996-12-10 23:23:01 +00001412 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001413 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001414 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001415}
1416
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001417
Guido van Rossumb6775db1994-08-01 11:34:53 +00001418#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001419static char posix_getpgrp__doc__[] =
1420"getpgrp() -> pgrp\n\
1421Return the current process group id.";
1422
Barry Warsaw53699e91996-12-10 23:23:01 +00001423static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001424posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001425 PyObject *self;
1426 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001427{
Barry Warsaw53699e91996-12-10 23:23:01 +00001428 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001429 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001430#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001431 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001432#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001433 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001434#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001435}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001436#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001437
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001438
Guido van Rossumb6775db1994-08-01 11:34:53 +00001439#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001440static char posix_setpgrp__doc__[] =
1441"setpgrp() -> None\n\
1442Make this process a session leader.";
1443
Barry Warsaw53699e91996-12-10 23:23:01 +00001444static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001445posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001446 PyObject *self;
1447 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001448{
Barry Warsaw53699e91996-12-10 23:23:01 +00001449 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001450 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001451#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001452 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001453#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001454 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001455#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001456 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001457 Py_INCREF(Py_None);
1458 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001459}
1460
Guido van Rossumb6775db1994-08-01 11:34:53 +00001461#endif /* HAVE_SETPGRP */
1462
Guido van Rossumad0ee831995-03-01 10:34:45 +00001463#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001464static char posix_getppid__doc__[] =
1465"getppid() -> ppid\n\
1466Return the parent's process id.";
1467
Barry Warsaw53699e91996-12-10 23:23:01 +00001468static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001469posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001470 PyObject *self;
1471 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001472{
Barry Warsaw53699e91996-12-10 23:23:01 +00001473 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001474 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001475 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001476}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001477#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001478
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001479
Guido van Rossumad0ee831995-03-01 10:34:45 +00001480#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001481static char posix_getuid__doc__[] =
1482"getuid() -> uid\n\
1483Return the current process's user id.";
1484
Barry Warsaw53699e91996-12-10 23:23:01 +00001485static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001486posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001487 PyObject *self;
1488 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001489{
Barry Warsaw53699e91996-12-10 23:23:01 +00001490 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001491 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001492 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001493}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001494#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001495
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001496
Guido van Rossumad0ee831995-03-01 10:34:45 +00001497#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001498static char posix_kill__doc__[] =
1499"kill(pid, sig) -> None\n\
1500Kill a process with a signal.";
1501
Barry Warsaw53699e91996-12-10 23:23:01 +00001502static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001503posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001504 PyObject *self;
1505 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001506{
1507 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001508 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001509 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001510#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001511 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1512 APIRET rc;
1513 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001514 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001515
1516 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1517 APIRET rc;
1518 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001519 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001520
1521 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001522 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001523#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001524 if (kill(pid, sig) == -1)
1525 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001526#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001527 Py_INCREF(Py_None);
1528 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001529}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001530#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001531
Guido van Rossumc0125471996-06-28 18:55:32 +00001532#ifdef HAVE_PLOCK
1533
1534#ifdef HAVE_SYS_LOCK_H
1535#include <sys/lock.h>
1536#endif
1537
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001538static char posix_plock__doc__[] =
1539"plock(op) -> None\n\
1540Lock program segments into memory.";
1541
Barry Warsaw53699e91996-12-10 23:23:01 +00001542static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001543posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001544 PyObject *self;
1545 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001546{
1547 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001548 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001549 return NULL;
1550 if (plock(op) == -1)
1551 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001552 Py_INCREF(Py_None);
1553 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001554}
1555#endif
1556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001557
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001558#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001559static char posix_popen__doc__[] =
1560"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1561Open a pipe to/from a command returning a file object.";
1562
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001563#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001564static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001565async_system(const char *command)
1566{
1567 char *p, errormsg[256], args[1024];
1568 RESULTCODES rcodes;
1569 APIRET rc;
1570 char *shell = getenv("COMSPEC");
1571 if (!shell)
1572 shell = "cmd";
1573
1574 strcpy(args, shell);
1575 p = &args[ strlen(args)+1 ];
1576 strcpy(p, "/c ");
1577 strcat(p, command);
1578 p += strlen(p) + 1;
1579 *p = '\0';
1580
1581 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001582 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001583 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001584 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001585 &rcodes, shell);
1586 return rc;
1587}
1588
Guido van Rossumd48f2521997-12-05 22:19:34 +00001589static FILE *
1590popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001591{
1592 HFILE rhan, whan;
1593 FILE *retfd = NULL;
1594 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1595
Guido van Rossumd48f2521997-12-05 22:19:34 +00001596 if (rc != NO_ERROR) {
1597 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001598 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001599 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001600
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001601 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1602 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001603
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001604 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1605 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001606
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001607 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1608 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001609
1610 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001611 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001612 }
1613
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001614 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1615 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001616
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001617 close(oldfd); /* And Close Saved STDOUT Handle */
1618 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001619
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001620 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1621 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001622
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001623 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1624 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001625
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001626 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1627 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001628
1629 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001630 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001631 }
1632
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001633 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1634 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001635
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001636 close(oldfd); /* And Close Saved STDIN Handle */
1637 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001638
Guido van Rossumd48f2521997-12-05 22:19:34 +00001639 } else {
1640 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001641 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001642 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001643}
1644
1645static PyObject *
1646posix_popen(self, args)
1647 PyObject *self;
1648 PyObject *args;
1649{
1650 char *name;
1651 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001652 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001653 FILE *fp;
1654 PyObject *f;
1655 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1656 return NULL;
1657 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001658 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001659 Py_END_ALLOW_THREADS
1660 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001661 return os2_error(err);
1662
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001663 f = PyFile_FromFile(fp, name, mode, fclose);
1664 if (f != NULL)
1665 PyFile_SetBufSize(f, bufsize);
1666 return f;
1667}
1668
1669#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001670static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001671posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001672 PyObject *self;
1673 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001674{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001675 char *name;
1676 char *mode = "r";
1677 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001678 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001679 PyObject *f;
1680 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001681 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001682 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001683 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001684 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001685 if (fp == NULL)
1686 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001687 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001688 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001689 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001690 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001691}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001692#endif
1693
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001694#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001695
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001696
Guido van Rossumb6775db1994-08-01 11:34:53 +00001697#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001698static char posix_setuid__doc__[] =
1699"setuid(uid) -> None\n\
1700Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001701static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001702posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001703 PyObject *self;
1704 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001705{
1706 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001707 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001708 return NULL;
1709 if (setuid(uid) < 0)
1710 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001711 Py_INCREF(Py_None);
1712 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001713}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001714#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001715
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001716
Guido van Rossumb6775db1994-08-01 11:34:53 +00001717#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001718static char posix_setgid__doc__[] =
1719"setgid(gid) -> None\n\
1720Set the current process's group id.";
1721
Barry Warsaw53699e91996-12-10 23:23:01 +00001722static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001723posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001724 PyObject *self;
1725 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001726{
1727 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001728 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001729 return NULL;
1730 if (setgid(gid) < 0)
1731 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001732 Py_INCREF(Py_None);
1733 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001734}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001735#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001736
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001737
Guido van Rossumb6775db1994-08-01 11:34:53 +00001738#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001739static char posix_waitpid__doc__[] =
1740"waitpid(pid, options) -> (pid, status)\n\
1741Wait for completion of a give child process.";
1742
Barry Warsaw53699e91996-12-10 23:23:01 +00001743static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001744posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001745 PyObject *self;
1746 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001747{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001748 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001749 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001750 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001751 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001752#ifdef NeXT
1753 pid = wait4(pid, (union wait *)&sts, options, NULL);
1754#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001755 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001756#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001757 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001758 if (pid == -1)
1759 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001760 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001761 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001762}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001763#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001764
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001765
Guido van Rossumad0ee831995-03-01 10:34:45 +00001766#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001767static char posix_wait__doc__[] =
1768"wait() -> (pid, status)\n\
1769Wait for completion of a child process.";
1770
Barry Warsaw53699e91996-12-10 23:23:01 +00001771static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001772posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001773 PyObject *self;
1774 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001775{
1776 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001777 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001778#ifdef NeXT
1779 pid = wait((union wait *)&sts);
1780#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001781 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001782#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001783 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001784 if (pid == -1)
1785 return posix_error();
1786 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001787 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001788}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001789#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001791
1792static char posix_lstat__doc__[] =
1793"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1794Like stat(path), but do not follow symbolic links.";
1795
Barry Warsaw53699e91996-12-10 23:23:01 +00001796static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001797posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001798 PyObject *self;
1799 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001800{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001801#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001802 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001803#else /* !HAVE_LSTAT */
1804 return posix_do_stat(self, args, stat);
1805#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001806}
1807
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001808
Guido van Rossumb6775db1994-08-01 11:34:53 +00001809#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001810static char posix_readlink__doc__[] =
1811"readlink(path) -> path\n\
1812Return a string representing the path to which the symbolic link points.";
1813
Barry Warsaw53699e91996-12-10 23:23:01 +00001814static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001815posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001816 PyObject *self;
1817 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001818{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001819 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001820 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001821 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001822 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001823 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001824 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001825 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001826 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001827 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001828 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001829 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001830}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001831#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001832
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001833
Guido van Rossumb6775db1994-08-01 11:34:53 +00001834#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001835static char posix_symlink__doc__[] =
1836"symlink(src, dst) -> None\n\
1837Create a symbolic link.";
1838
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001839static PyObject *
1840posix_symlink(self, args)
1841 PyObject *self;
1842 PyObject *args;
1843{
1844 return posix_2str(args, symlink);
1845}
1846#endif /* HAVE_SYMLINK */
1847
1848
1849#ifdef HAVE_TIMES
1850#ifndef HZ
1851#define HZ 60 /* Universal constant :-) */
1852#endif /* HZ */
1853
Guido van Rossumd48f2521997-12-05 22:19:34 +00001854#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1855static long
1856system_uptime()
1857{
1858 ULONG value = 0;
1859
1860 Py_BEGIN_ALLOW_THREADS
1861 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1862 Py_END_ALLOW_THREADS
1863
1864 return value;
1865}
1866
1867static PyObject *
1868posix_times(self, args)
1869 PyObject *self;
1870 PyObject *args;
1871{
1872 if (!PyArg_NoArgs(args))
1873 return NULL;
1874
1875 /* Currently Only Uptime is Provided -- Others Later */
1876 return Py_BuildValue("ddddd",
1877 (double)0 /* t.tms_utime / HZ */,
1878 (double)0 /* t.tms_stime / HZ */,
1879 (double)0 /* t.tms_cutime / HZ */,
1880 (double)0 /* t.tms_cstime / HZ */,
1881 (double)system_uptime() / 1000);
1882}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001883#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00001884static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001885posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001886 PyObject *self;
1887 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001888{
1889 struct tms t;
1890 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001891 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001892 return NULL;
1893 errno = 0;
1894 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001895 if (c == (clock_t) -1)
1896 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001897 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001898 (double)t.tms_utime / HZ,
1899 (double)t.tms_stime / HZ,
1900 (double)t.tms_cutime / HZ,
1901 (double)t.tms_cstime / HZ,
1902 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001903}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001904#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001905#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001906
1907
Guido van Rossum87755a21996-09-07 00:59:43 +00001908#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001909#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001910static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001911posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001912 PyObject *self;
1913 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001914{
1915 FILETIME create, exit, kernel, user;
1916 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001917 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001918 return NULL;
1919 hProc = GetCurrentProcess();
1920 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001921 return Py_BuildValue(
1922 "ddddd",
1923 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1924 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1925 (double)0,
1926 (double)0,
1927 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001928}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001929#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001930
1931#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00001932static char posix_times__doc__[] =
1933"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1934Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001935#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001936
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001937
Guido van Rossumb6775db1994-08-01 11:34:53 +00001938#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001939static char posix_setsid__doc__[] =
1940"setsid() -> None\n\
1941Call the system call setsid().";
1942
Barry Warsaw53699e91996-12-10 23:23:01 +00001943static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001944posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001945 PyObject *self;
1946 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001947{
Barry Warsaw53699e91996-12-10 23:23:01 +00001948 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001949 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001950 if (setsid() < 0)
1951 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001952 Py_INCREF(Py_None);
1953 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001954}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001955#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001956
Guido van Rossumb6775db1994-08-01 11:34:53 +00001957#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001958static char posix_setpgid__doc__[] =
1959"setpgid(pid, pgrp) -> None\n\
1960Call the system call setpgid().";
1961
Barry Warsaw53699e91996-12-10 23:23:01 +00001962static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001963posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001964 PyObject *self;
1965 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001966{
1967 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001968 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001969 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00001970 if (setpgid(pid, pgrp) < 0)
1971 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001972 Py_INCREF(Py_None);
1973 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001974}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001975#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00001976
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001977
Guido van Rossumb6775db1994-08-01 11:34:53 +00001978#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979static char posix_tcgetpgrp__doc__[] =
1980"tcgetpgrp(fd) -> pgid\n\
1981Return the process group associated with the terminal given by a fd.";
1982
Barry Warsaw53699e91996-12-10 23:23:01 +00001983static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00001984posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001985 PyObject *self;
1986 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00001987{
1988 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001989 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00001990 return NULL;
1991 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00001992 if (pgid < 0)
1993 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001994 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00001995}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001996#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00001997
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001998
Guido van Rossumb6775db1994-08-01 11:34:53 +00001999#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002000static char posix_tcsetpgrp__doc__[] =
2001"tcsetpgrp(fd, pgid) -> None\n\
2002Set the process group associated with the terminal given by a fd.";
2003
Barry Warsaw53699e91996-12-10 23:23:01 +00002004static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002005posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002006 PyObject *self;
2007 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002008{
2009 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002010 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002011 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002012 if (tcsetpgrp(fd, pgid) < 0)
2013 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002014 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002015 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002016}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002017#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002018
Guido van Rossum687dd131993-05-17 08:34:16 +00002019/* Functions acting on file descriptors */
2020
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002021static char posix_open__doc__[] =
2022"open(filename, flag [, mode=0777]) -> fd\n\
2023Open a file (for low level IO).";
2024
Barry Warsaw53699e91996-12-10 23:23:01 +00002025static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002026posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002027 PyObject *self;
2028 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002029{
2030 char *file;
2031 int flag;
2032 int mode = 0777;
2033 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002034 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2035 return NULL;
2036
Barry Warsaw53699e91996-12-10 23:23:01 +00002037 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002038 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002039 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002040 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002041 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002042 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002043}
2044
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002045
2046static char posix_close__doc__[] =
2047"close(fd) -> None\n\
2048Close a file descriptor (for low level IO).";
2049
Barry Warsaw53699e91996-12-10 23:23:01 +00002050static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002051posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002052 PyObject *self;
2053 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002054{
2055 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002056 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002057 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002058 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002059 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002060 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002061 if (res < 0)
2062 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002063 Py_INCREF(Py_None);
2064 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002065}
2066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002067
2068static char posix_dup__doc__[] =
2069"dup(fd) -> fd2\n\
2070Return a duplicate of a file descriptor.";
2071
Barry Warsaw53699e91996-12-10 23:23:01 +00002072static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002073posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002074 PyObject *self;
2075 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002076{
2077 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002078 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002079 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002080 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002081 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002082 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002083 if (fd < 0)
2084 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002085 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002086}
2087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088
2089static char posix_dup2__doc__[] =
2090"dup2(fd, fd2) -> None\n\
2091Duplicate file descriptor.";
2092
Barry Warsaw53699e91996-12-10 23:23:01 +00002093static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002094posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002095 PyObject *self;
2096 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002097{
2098 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002099 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002100 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002101 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002102 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002103 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002104 if (res < 0)
2105 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002106 Py_INCREF(Py_None);
2107 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002108}
2109
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002110
2111static char posix_lseek__doc__[] =
2112"lseek(fd, pos, how) -> newpos\n\
2113Set the current position of a file descriptor.";
2114
Barry Warsaw53699e91996-12-10 23:23:01 +00002115static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002116posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002117 PyObject *self;
2118 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002119{
2120 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002121 off_t pos, res;
2122 PyObject *posobj;
2123 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002124 return NULL;
2125#ifdef SEEK_SET
2126 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2127 switch (how) {
2128 case 0: how = SEEK_SET; break;
2129 case 1: how = SEEK_CUR; break;
2130 case 2: how = SEEK_END; break;
2131 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002132#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002133
2134#if !defined(HAVE_LARGEFILE_SUPPORT)
2135 pos = PyInt_AsLong(posobj);
2136#else
2137 pos = PyLong_Check(posobj) ?
2138 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2139#endif
2140 if (PyErr_Occurred())
2141 return NULL;
2142
Barry Warsaw53699e91996-12-10 23:23:01 +00002143 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002144 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002145 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002146 if (res < 0)
2147 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002148
2149#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002150 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002151#else
2152 return PyLong_FromLongLong(res);
2153#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002154}
2155
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002156
2157static char posix_read__doc__[] =
2158"read(fd, buffersize) -> string\n\
2159Read a file descriptor.";
2160
Barry Warsaw53699e91996-12-10 23:23:01 +00002161static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002162posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002163 PyObject *self;
2164 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002165{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002166 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002167 PyObject *buffer;
2168 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002169 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002170 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002171 if (buffer == NULL)
2172 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002173 Py_BEGIN_ALLOW_THREADS
2174 n = read(fd, PyString_AsString(buffer), size);
2175 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002176 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002177 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002178 return posix_error();
2179 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002180 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002181 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002182 return buffer;
2183}
2184
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002185
2186static char posix_write__doc__[] =
2187"write(fd, string) -> byteswritten\n\
2188Write a string to a file descriptor.";
2189
Barry Warsaw53699e91996-12-10 23:23:01 +00002190static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002191posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002192 PyObject *self;
2193 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002194{
2195 int fd, size;
2196 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002197 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002198 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002199 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002200 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002201 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002202 if (size < 0)
2203 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002204 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002205}
2206
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002207
2208static char posix_fstat__doc__[]=
2209"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2210Like stat(), but for an open file descriptor.";
2211
Barry Warsaw53699e91996-12-10 23:23:01 +00002212static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002213posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002214 PyObject *self;
2215 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002216{
2217 int fd;
2218 struct stat st;
2219 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002220 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002221 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002222 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002223 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002224 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002225 if (res != 0)
2226 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002227#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002229 (long)st.st_mode,
2230 (long)st.st_ino,
2231 (long)st.st_dev,
2232 (long)st.st_nlink,
2233 (long)st.st_uid,
2234 (long)st.st_gid,
2235 (long)st.st_size,
2236 (long)st.st_atime,
2237 (long)st.st_mtime,
2238 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002239#else
2240 return Py_BuildValue("(lLllllLlll)",
2241 (long)st.st_mode,
2242 (LONG_LONG)st.st_ino,
2243 (long)st.st_dev,
2244 (long)st.st_nlink,
2245 (long)st.st_uid,
2246 (long)st.st_gid,
2247 (LONG_LONG)st.st_size,
2248 (long)st.st_atime,
2249 (long)st.st_mtime,
2250 (long)st.st_ctime);
2251#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002252}
2253
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002254
2255static char posix_fdopen__doc__[] =
2256"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2257Return an open file object connected to a file descriptor.";
2258
Barry Warsaw53699e91996-12-10 23:23:01 +00002259static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002260posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002261 PyObject *self;
2262 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002263{
Barry Warsaw53699e91996-12-10 23:23:01 +00002264 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002265 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002266 char *mode = "r";
2267 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002268 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002269 PyObject *f;
2270 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002271 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002272
Barry Warsaw53699e91996-12-10 23:23:01 +00002273 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002274 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002275 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002276 if (fp == NULL)
2277 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002278 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002279 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002280 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002281 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002282}
2283
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002284
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002285#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002286static char posix_pipe__doc__[] =
2287"pipe() -> (read_end, write_end)\n\
2288Create a pipe.";
2289
Barry Warsaw53699e91996-12-10 23:23:01 +00002290static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002291posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002292 PyObject *self;
2293 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002294{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002295#if defined(PYOS_OS2)
2296 HFILE read, write;
2297 APIRET rc;
2298
2299 if (!PyArg_Parse(args, ""))
2300 return NULL;
2301
2302 Py_BEGIN_ALLOW_THREADS
2303 rc = DosCreatePipe( &read, &write, 4096);
2304 Py_END_ALLOW_THREADS
2305 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002306 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002307
2308 return Py_BuildValue("(ii)", read, write);
2309#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002310#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002311 int fds[2];
2312 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002313 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002314 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002315 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002316 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002317 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002318 if (res != 0)
2319 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002320 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002321#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002322 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002323 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002324 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002325 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002326 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002327 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002328 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002329 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002330 if (!ok)
2331 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002332 read_fd = _open_osfhandle((long)read, 0);
2333 write_fd = _open_osfhandle((long)write, 1);
2334 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002335#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002336#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002337}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002338#endif /* HAVE_PIPE */
2339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002340
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002341#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002342static char posix_mkfifo__doc__[] =
2343"mkfifo(file, [, mode=0666]) -> None\n\
2344Create a FIFO (a POSIX named pipe).";
2345
Barry Warsaw53699e91996-12-10 23:23:01 +00002346static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002347posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002348 PyObject *self;
2349 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002350{
2351 char *file;
2352 int mode = 0666;
2353 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002354 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002355 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002356 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002357 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002358 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002359 if (res < 0)
2360 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002361 Py_INCREF(Py_None);
2362 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002363}
2364#endif
2365
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002366
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002367#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002368static char posix_ftruncate__doc__[] =
2369"ftruncate(fd, length) -> None\n\
2370Truncate a file to a specified length.";
2371
Barry Warsaw53699e91996-12-10 23:23:01 +00002372static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002373posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002374 PyObject *self; /* Not used */
2375 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002376{
2377 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002378 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002379 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002380 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002381
Guido van Rossum94f6f721999-01-06 18:42:14 +00002382 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2383 return NULL;
2384
2385#if !defined(HAVE_LARGEFILE_SUPPORT)
2386 length = PyInt_AsLong(lenobj);
2387#else
2388 length = PyLong_Check(lenobj) ?
2389 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2390#endif
2391 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002392 return NULL;
2393
Barry Warsaw53699e91996-12-10 23:23:01 +00002394 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002395 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002396 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002397 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002398 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002399 return NULL;
2400 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002401 Py_INCREF(Py_None);
2402 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002403}
2404#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002405
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002406#ifdef NeXT
2407#define HAVE_PUTENV
2408/* Steve Spicklemire got this putenv from NeXTAnswers */
2409static int
2410putenv(char *newval)
2411{
2412 extern char **environ;
2413
2414 static int firstTime = 1;
2415 char **ep;
2416 char *cp;
2417 int esiz;
2418 char *np;
2419
2420 if (!(np = strchr(newval, '=')))
2421 return 1;
2422 *np = '\0';
2423
2424 /* look it up */
2425 for (ep=environ ; *ep ; ep++)
2426 {
2427 /* this should always be true... */
2428 if (cp = strchr(*ep, '='))
2429 {
2430 *cp = '\0';
2431 if (!strcmp(*ep, newval))
2432 {
2433 /* got it! */
2434 *cp = '=';
2435 break;
2436 }
2437 *cp = '=';
2438 }
2439 else
2440 {
2441 *np = '=';
2442 return 1;
2443 }
2444 }
2445
2446 *np = '=';
2447 if (*ep)
2448 {
2449 /* the string was already there:
2450 just replace it with the new one */
2451 *ep = newval;
2452 return 0;
2453 }
2454
2455 /* expand environ by one */
2456 for (esiz=2, ep=environ ; *ep ; ep++)
2457 esiz++;
2458 if (firstTime)
2459 {
2460 char **epp;
2461 char **newenv;
2462 if (!(newenv = malloc(esiz * sizeof(char *))))
2463 return 1;
2464
2465 for (ep=environ, epp=newenv ; *ep ;)
2466 *epp++ = *ep++;
2467 *epp++ = newval;
2468 *epp = (char *) 0;
2469 environ = newenv;
2470 }
2471 else
2472 {
2473 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2474 return 1;
2475 environ[esiz - 2] = newval;
2476 environ[esiz - 1] = (char *) 0;
2477 firstTime = 0;
2478 }
2479
2480 return 0;
2481}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002482#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002483
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002484
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002485#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002486static char posix_putenv__doc__[] =
2487"putenv(key, value) -> None\n\
2488Change or add an environment variable.";
2489
Guido van Rossumbcc20741998-08-04 22:53:56 +00002490#ifdef __BEOS__
2491/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2492int putenv( const char *str );
2493#endif
2494
Barry Warsaw53699e91996-12-10 23:23:01 +00002495static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002496posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002497 PyObject *self;
2498 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002499{
2500 char *s1, *s2;
2501 char *new;
2502
Barry Warsaw53699e91996-12-10 23:23:01 +00002503 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002504 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002505
2506#if defined(PYOS_OS2)
2507 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2508 APIRET rc;
2509
2510 if (strlen(s2) == 0) /* If New Value is an Empty String */
2511 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2512
2513 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2514 if (rc != NO_ERROR)
2515 return os2_error(rc);
2516
2517 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2518 APIRET rc;
2519
2520 if (strlen(s2) == 0) /* If New Value is an Empty String */
2521 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2522
2523 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2524 if (rc != NO_ERROR)
2525 return os2_error(rc);
2526 } else {
2527#endif
2528
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002529 /* XXX This leaks memory -- not easy to fix :-( */
2530 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002531 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002532 (void) sprintf(new, "%s=%s", s1, s2);
2533 if (putenv(new)) {
2534 posix_error();
2535 return NULL;
2536 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002537
2538#if defined(PYOS_OS2)
2539 }
2540#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002541 Py_INCREF(Py_None);
2542 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002543}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002544#endif /* putenv */
2545
2546#ifdef HAVE_STRERROR
2547static char posix_strerror__doc__[] =
2548"strerror(code) -> string\n\
2549Translate an error code to a message string.";
2550
2551PyObject *
2552posix_strerror(self, args)
2553 PyObject *self;
2554 PyObject *args;
2555{
2556 int code;
2557 char *message;
2558 if (!PyArg_ParseTuple(args, "i", &code))
2559 return NULL;
2560 message = strerror(code);
2561 if (message == NULL) {
2562 PyErr_SetString(PyExc_ValueError,
2563 "strerror code out of range");
2564 return NULL;
2565 }
2566 return PyString_FromString(message);
2567}
2568#endif /* strerror */
2569
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002570
Guido van Rossumc9641791998-08-04 15:26:23 +00002571#ifdef HAVE_SYS_WAIT_H
2572
2573#ifdef WIFSTOPPED
2574static char posix_WIFSTOPPED__doc__[] =
2575"WIFSTOPPED(status) -> Boolean\n\
2576See Unix documentation.";
2577
2578static PyObject *
2579posix_WIFSTOPPED(self, args)
2580 PyObject *self;
2581 PyObject *args;
2582{
2583 int status = 0;
2584
2585 if (!PyArg_Parse(args, "i", &status))
2586 {
2587 return NULL;
2588 }
2589
2590 return Py_BuildValue("i", WIFSTOPPED(status));
2591}
2592#endif /* WIFSTOPPED */
2593
2594#ifdef WIFSIGNALED
2595static char posix_WIFSIGNALED__doc__[] =
2596"WIFSIGNALED(status) -> Boolean\n\
2597See Unix documentation.";
2598
2599static PyObject *
2600posix_WIFSIGNALED(self, args)
2601 PyObject *self;
2602 PyObject *args;
2603{
2604 int status = 0;
2605
2606 if (!PyArg_Parse(args, "i", &status))
2607 {
2608 return NULL;
2609 }
2610
2611 return Py_BuildValue("i", WIFSIGNALED(status));
2612}
2613#endif /* WIFSIGNALED */
2614
2615#ifdef WIFEXITED
2616static char posix_WIFEXITED__doc__[] =
2617"WIFEXITED(status) -> Boolean\n\
2618See Unix documentation.";
2619
2620static PyObject *
2621posix_WIFEXITED(self, args)
2622 PyObject *self;
2623 PyObject *args;
2624{
2625 int status = 0;
2626
2627 if (!PyArg_Parse(args, "i", &status))
2628 {
2629 return NULL;
2630 }
2631
2632 return Py_BuildValue("i", WIFEXITED(status));
2633}
2634#endif /* WIFEXITED */
2635
2636#ifdef WIFSTOPPED
2637static char posix_WEXITSTATUS__doc__[] =
2638"WEXITSTATUS(status) -> integer\n\
2639See Unix documentation.";
2640
2641static PyObject *
2642posix_WEXITSTATUS(self, args)
2643 PyObject *self;
2644 PyObject *args;
2645{
2646 int status = 0;
2647
2648 if (!PyArg_Parse(args, "i", &status))
2649 {
2650 return NULL;
2651 }
2652
2653 return Py_BuildValue("i", WEXITSTATUS(status));
2654}
2655#endif /* WEXITSTATUS */
2656
2657#ifdef WTERMSIG
2658static char posix_WTERMSIG__doc__[] =
2659"WTERMSIG(status) -> integer\n\
2660See Unix documentation.";
2661
2662static PyObject *
2663posix_WTERMSIG(self, args)
2664 PyObject *self;
2665 PyObject *args;
2666{
2667 int status = 0;
2668
2669 if (!PyArg_Parse(args, "i", &status))
2670 {
2671 return NULL;
2672 }
2673
2674 return Py_BuildValue("i", WTERMSIG(status));
2675}
2676#endif /* WTERMSIG */
2677
2678#ifdef WSTOPSIG
2679static char posix_WSTOPSIG__doc__[] =
2680"WSTOPSIG(status) -> integer\n\
2681See Unix documentation.";
2682
2683static PyObject *
2684posix_WSTOPSIG(self, args)
2685 PyObject *self;
2686 PyObject *args;
2687{
2688 int status = 0;
2689
2690 if (!PyArg_Parse(args, "i", &status))
2691 {
2692 return NULL;
2693 }
2694
2695 return Py_BuildValue("i", WSTOPSIG(status));
2696}
2697#endif /* WSTOPSIG */
2698
2699#endif /* HAVE_SYS_WAIT_H */
2700
2701
Guido van Rossum94f6f721999-01-06 18:42:14 +00002702#if defined(HAVE_FSTATVFS)
2703#include <sys/statvfs.h>
2704
2705static char posix_fstatvfs__doc__[] =
2706"fstatvfs(fd) -> \
2707(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
2708Perform an fstatvfs system call on the given fd.";
2709
2710static PyObject *
2711posix_fstatvfs(self, args)
2712 PyObject *self;
2713 PyObject *args;
2714{
2715 int fd, res;
2716 struct statvfs st;
2717 if (!PyArg_ParseTuple(args, "i", &fd))
2718 return NULL;
2719 Py_BEGIN_ALLOW_THREADS
2720 res = fstatvfs(fd, &st);
2721 Py_END_ALLOW_THREADS
2722 if (res != 0)
2723 return posix_error();
2724#if !defined(HAVE_LARGEFILE_SUPPORT)
2725 return Py_BuildValue("(lllllllllll)",
2726 (long) st.f_bsize,
2727 (long) st.f_frsize,
2728 (long) st.f_blocks,
2729 (long) st.f_bfree,
2730 (long) st.f_bavail,
2731 (long) st.f_files,
2732 (long) st.f_ffree,
2733 (long) st.f_favail,
2734 (long) st.f_fsid,
2735 (long) st.f_flag,
2736 (long) st.f_namemax);
2737#else
2738 return Py_BuildValue("(llLLLLLLlll)",
2739 (long) st.f_bsize,
2740 (long) st.f_frsize,
2741 (LONG_LONG) st.f_blocks,
2742 (LONG_LONG) st.f_bfree,
2743 (LONG_LONG) st.f_bavail,
2744 (LONG_LONG) st.f_files,
2745 (LONG_LONG) st.f_ffree,
2746 (LONG_LONG) st.f_favail,
2747 (long) st.f_fsid,
2748 (long) st.f_flag,
2749 (long) st.f_namemax);
2750#endif
2751}
2752#endif /* HAVE_FSTATVFS */
2753
2754
2755#if defined(HAVE_STATVFS)
2756#include <sys/statvfs.h>
2757
2758static char posix_statvfs__doc__[] =
2759"statvfs(path) -> \
2760(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
2761Perform a statvfs system call on the given path.";
2762
2763static PyObject *
2764posix_statvfs(self, args)
2765 PyObject *self;
2766 PyObject *args;
2767{
2768 char *path;
2769 int res;
2770 struct statvfs st;
2771 if (!PyArg_ParseTuple(args, "s", &path))
2772 return NULL;
2773 Py_BEGIN_ALLOW_THREADS
2774 res = statvfs(path, &st);
2775 Py_END_ALLOW_THREADS
2776 if (res != 0)
2777 return posix_error_with_filename(path);
2778#if !defined(HAVE_LARGEFILE_SUPPORT)
2779 return Py_BuildValue("(lllllllllll)",
2780 (long) st.f_bsize,
2781 (long) st.f_frsize,
2782 (long) st.f_blocks,
2783 (long) st.f_bfree,
2784 (long) st.f_bavail,
2785 (long) st.f_files,
2786 (long) st.f_ffree,
2787 (long) st.f_favail,
2788 (long) st.f_fsid,
2789 (long) st.f_flag,
2790 (long) st.f_namemax);
2791#else /* HAVE_LARGEFILE_SUPPORT */
2792 return Py_BuildValue("(llLLLLLLlll)",
2793 (long) st.f_bsize,
2794 (long) st.f_frsize,
2795 (LONG_LONG) st.f_blocks,
2796 (LONG_LONG) st.f_bfree,
2797 (LONG_LONG) st.f_bavail,
2798 (LONG_LONG) st.f_files,
2799 (LONG_LONG) st.f_ffree,
2800 (LONG_LONG) st.f_favail,
2801 (long) st.f_fsid,
2802 (long) st.f_flag,
2803 (long) st.f_namemax);
2804#endif
2805}
2806#endif /* HAVE_STATVFS */
2807
2808
Barry Warsaw53699e91996-12-10 23:23:01 +00002809static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00002810 {"access", posix_access, 0, posix_access__doc__},
2811 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002812 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2813 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002814#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002815 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002816#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002817#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002818 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002819#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002820#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002821 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002822#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002823 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2824 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2825 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002826#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002827 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002828#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002829#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002830 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002831#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002832 {"rename", posix_rename, 0, posix_rename__doc__},
2833 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2834 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002835#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002836 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002837#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002838#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002839 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002840#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002841 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002842#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002843 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002844#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002845 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2846 {"remove", posix_unlink, 0, posix_remove__doc__},
2847 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002848#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002849 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002850#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002851 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002852#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002853 {"execv", posix_execv, 0, posix_execv__doc__},
2854 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002855#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002856#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002857 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002858#endif /* HAVE_FORK */
2859#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002860 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002861#endif /* HAVE_GETEGID */
2862#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002863 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002864#endif /* HAVE_GETEUID */
2865#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002866 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002867#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002868 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002869#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002870 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002871#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002872#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002873 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002874#endif /* HAVE_GETPPID */
2875#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002876 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002877#endif /* HAVE_GETUID */
2878#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002879 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002880#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002881#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002882 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002883#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002884#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002885 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002886#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002887#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002888 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002889#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002890#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002891 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002892#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002893#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002894 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002895#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002896#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002897 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002898#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002899#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002900 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002901#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002902#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002903 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002904#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002905#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002906 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002907#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002908#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002909 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002910#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002911#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002912 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002913#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002914 {"open", posix_open, 1, posix_open__doc__},
2915 {"close", posix_close, 0, posix_close__doc__},
2916 {"dup", posix_dup, 0, posix_dup__doc__},
2917 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2918 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2919 {"read", posix_read, 0, posix_read__doc__},
2920 {"write", posix_write, 0, posix_write__doc__},
2921 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2922 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002923#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002924 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002925#endif
2926#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002927 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002928#endif
2929#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002930 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002931#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002932#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002933 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002934#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002935#ifdef HAVE_STRERROR
2936 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2937#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00002938#ifdef HAVE_SYS_WAIT_H
2939#ifdef WIFSTOPPED
2940 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
2941#endif /* WIFSTOPPED */
2942#ifdef WIFSIGNALED
2943 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
2944#endif /* WIFSIGNALED */
2945#ifdef WIFEXITED
2946 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
2947#endif /* WIFEXITED */
2948#ifdef WEXITSTATUS
2949 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
2950#endif /* WEXITSTATUS */
2951#ifdef WTERMSIG
2952 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
2953#endif /* WTERMSIG */
2954#ifdef WSTOPSIG
2955 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
2956#endif /* WSTOPSIG */
2957#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002958#ifdef HAVE_FSTATVFS
2959 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
2960#endif
2961#ifdef HAVE_STATVFS
2962 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
2963#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002964 {NULL, NULL} /* Sentinel */
2965};
2966
2967
Barry Warsaw4a342091996-12-19 23:50:02 +00002968static int
2969ins(d, symbol, value)
2970 PyObject* d;
2971 char* symbol;
2972 long value;
2973{
2974 PyObject* v = PyInt_FromLong(value);
2975 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
2976 return -1; /* triggers fatal error */
2977
2978 Py_DECREF(v);
2979 return 0;
2980}
2981
Guido van Rossumd48f2521997-12-05 22:19:34 +00002982#if defined(PYOS_OS2)
2983/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
2984static int insertvalues(PyObject *d)
2985{
2986 APIRET rc;
2987 ULONG values[QSV_MAX+1];
2988 PyObject *v;
2989 char *ver, tmp[10];
2990
2991 Py_BEGIN_ALLOW_THREADS
2992 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
2993 Py_END_ALLOW_THREADS
2994
2995 if (rc != NO_ERROR) {
2996 os2_error(rc);
2997 return -1;
2998 }
2999
3000 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3001 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3002 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3003 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3004 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3005 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3006 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3007
3008 switch (values[QSV_VERSION_MINOR]) {
3009 case 0: ver = "2.00"; break;
3010 case 10: ver = "2.10"; break;
3011 case 11: ver = "2.11"; break;
3012 case 30: ver = "3.00"; break;
3013 case 40: ver = "4.00"; break;
3014 case 50: ver = "5.00"; break;
3015 default:
3016 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3017 values[QSV_VERSION_MINOR]);
3018 ver = &tmp[0];
3019 }
3020
3021 /* Add Indicator of the Version of the Operating System */
3022 v = PyString_FromString(ver);
3023 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3024 return -1;
3025 Py_DECREF(v);
3026
3027 /* Add Indicator of Which Drive was Used to Boot the System */
3028 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3029 tmp[1] = ':';
3030 tmp[2] = '\0';
3031
3032 v = PyString_FromString(tmp);
3033 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3034 return -1;
3035 Py_DECREF(v);
3036
3037 return 0;
3038}
3039#endif
3040
Barry Warsaw4a342091996-12-19 23:50:02 +00003041static int
3042all_ins(d)
3043 PyObject* d;
3044{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003045#ifdef F_OK
3046 if (ins(d, "F_OK", (long)F_OK)) return -1;
3047#endif
3048#ifdef R_OK
3049 if (ins(d, "R_OK", (long)R_OK)) return -1;
3050#endif
3051#ifdef W_OK
3052 if (ins(d, "W_OK", (long)W_OK)) return -1;
3053#endif
3054#ifdef X_OK
3055 if (ins(d, "X_OK", (long)X_OK)) return -1;
3056#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003057#ifdef WNOHANG
3058 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3059#endif
3060#ifdef O_RDONLY
3061 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3062#endif
3063#ifdef O_WRONLY
3064 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3065#endif
3066#ifdef O_RDWR
3067 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3068#endif
3069#ifdef O_NDELAY
3070 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3071#endif
3072#ifdef O_NONBLOCK
3073 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3074#endif
3075#ifdef O_APPEND
3076 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3077#endif
3078#ifdef O_DSYNC
3079 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3080#endif
3081#ifdef O_RSYNC
3082 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3083#endif
3084#ifdef O_SYNC
3085 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3086#endif
3087#ifdef O_NOCTTY
3088 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3089#endif
3090#ifdef O_CREAT
3091 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3092#endif
3093#ifdef O_EXCL
3094 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3095#endif
3096#ifdef O_TRUNC
3097 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3098#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003099#ifdef O_BINARY
3100 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3101#endif
3102#ifdef O_TEXT
3103 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3104#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003105
3106#if defined(PYOS_OS2)
3107 if (insertvalues(d)) return -1;
3108#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003109 return 0;
3110}
3111
3112
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003113#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003114#define INITFUNC initnt
3115#define MODNAME "nt"
3116#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003117#if defined(PYOS_OS2)
3118#define INITFUNC initos2
3119#define MODNAME "os2"
3120#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003121#define INITFUNC initposix
3122#define MODNAME "posix"
3123#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003124#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003125
Guido van Rossum3886bb61998-12-04 18:50:17 +00003126DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003127INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003128{
Barry Warsaw53699e91996-12-10 23:23:01 +00003129 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003130
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003131 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003132 posix_methods,
3133 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003134 (PyObject *)NULL,
3135 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003136 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003137
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003138 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003139 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003140 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003141 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003142 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003143
Barry Warsaw4a342091996-12-19 23:50:02 +00003144 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003145 return;
3146
Barry Warsawd58d7641998-07-23 16:14:40 +00003147 Py_INCREF(PyExc_OSError);
3148 PosixError = PyExc_OSError;
3149 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003150}